Bug 1284742 - Replace profile directory traversal with a generated directory tree in dom/filesystem/test/test_basic.html. r?mystor draft
authorAndrew Comminos <andrew@comminos.com>
Tue, 02 Aug 2016 15:48:13 -0400
changeset 395745 35bf04c988ca6dce8204f82f1017b0d59ccad42d
parent 394995 ffac2798999c5b84f1b4605a1280994bb665a406
child 527050 3f8f1d5eafdf7379133ac61826b50637548974fc
push id24834
push userbmo:andrew@comminos.com
push dateTue, 02 Aug 2016 20:19:11 +0000
reviewersmystor
bugs1284742
milestone51.0a1
Bug 1284742 - Replace profile directory traversal with a generated directory tree in dom/filesystem/test/test_basic.html. r?mystor MozReview-Commit-ID: HSX9i5KDDEj
dom/filesystem/tests/script_fileList.js
dom/filesystem/tests/test_basic.html
--- a/dom/filesystem/tests/script_fileList.js
+++ b/dom/filesystem/tests/script_fileList.js
@@ -3,16 +3,56 @@ Cu.importGlobalProperties(["File"]);
 
 function createProfDFile() {
   return Cc["@mozilla.org/file/directory_service;1"]
            .getService(Ci.nsIDirectoryService)
            .QueryInterface(Ci.nsIProperties)
            .get('ProfD', Ci.nsIFile);
 }
 
+// Creates a parametric arity directory hierarchy as a function of depth.
+// Each directory contains one leaf file, and subdirectories of depth [1, depth).
+// e.g. for depth 3:
+//
+// subdir3
+// - file.txt
+// - subdir2
+//   - file.txt
+//   - subdir1
+//     - file.txt
+// - subdir1
+//   - file.txt
+//
+// Returns the parent directory of the subtree.
+function createTreeFile(depth, parent) {
+  if (!parent) {
+    parent = Cc["@mozilla.org/file/directory_service;1"]
+                .getService(Ci.nsIDirectoryService)
+                .QueryInterface(Ci.nsIProperties)
+                .get('TmpD', Ci.nsIFile);
+    parent.append('dir-tree-test');
+    parent.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0o700);
+  }
+
+  var nextFile = parent.clone();
+  if (depth == 0) {
+    nextFile.append('file.txt');
+    nextFile.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0o600);
+  } else {
+    nextFile.append('subdir' + depth);
+    nextFile.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0o700);
+    // Decrement the maximal depth by one for each level of nesting.
+    for (i = 0; i < depth; i++) {
+      createTreeFile(i, nextFile);
+    }
+  }
+
+  return parent;
+}
+
 function createRootFile() {
   var testFile = createProfDFile();
 
   // Let's go back to the root of the FileSystem
   while (true) {
     var parent = testFile.parent;
     if (!parent) {
       break;
@@ -47,26 +87,32 @@ function createTestFile() {
   return tmpFile;
 }
 
 addMessageListener("dir.open", function (e) {
   var testFile;
 
   switch (e.path) {
     case 'ProfD':
+      // Note that files in the profile directory are not guaranteed to persist-
+      // see bug 1284742.
       testFile = createProfDFile();
       break;
 
     case 'root':
       testFile = createRootFile();
       break;
 
     case 'test':
       testFile = createTestFile();
       break;
+
+    case 'tree':
+      testFile = createTreeFile(3);
+      break;
   }
 
   sendAsyncMessage("dir.opened", {
     dir: testFile.path
   });
 });
 
 addMessageListener("file.open", function (e) {
--- a/dom/filesystem/tests/test_basic.html
+++ b/dom/filesystem/tests/test_basic.html
@@ -124,17 +124,17 @@ function test_inputGetFiles() {
 
   script.addMessageListener("dir.opened", onOpened);
   script.sendAsyncMessage("dir.open", { path: 'test' });
 }
 
 var tests = [
   function() { setup_tests(next); },
 
-  function() { create_fileList('ProfD') },
+  function() { create_fileList('tree') },
   function() { test_basic(directory, next); },
   function() { test_getFilesAndDirectories(directory, true, next); },
   function() { test_getFiles(directory, false, next); },
   function() { test_getFiles(directory, true, next); },
 
   function() { create_fileList('test') },
   function() { test_getFiles_recursiveComparison(directory, next); },