Bug 782231 - Testsuite for the new features of DirectoryIterator. r=froydnj
authorDavid Rajchenbach-Teller <dteller@mozilla.com>
Fri, 21 Sep 2012 23:36:15 -0400
changeset 107796 11ca43377445f5c2d7d8843bd317e3a04bebbaa9
parent 107795 a1a658f7c1d62e63dd394699dd484c26884053d5
child 107797 e1f3deefa340ec7916607db71979d6478253f61b
push id23509
push userryanvm@gmail.com
push dateSat, 22 Sep 2012 12:28:38 +0000
treeherdermozilla-central@b461a7cd250e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs782231
milestone18.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 782231 - Testsuite for the new features of DirectoryIterator. r=froydnj
toolkit/components/osfile/tests/mochi/worker_test_osfile_front.js
--- a/toolkit/components/osfile/tests/mochi/worker_test_osfile_front.js
+++ b/toolkit/components/osfile/tests/mochi/worker_test_osfile_front.js
@@ -444,16 +444,73 @@ function test_iter_dir()
       ok(lastAccess.getFullYear() >= year - 1 && lastAccess.getFullYear() <= year, "test_iter_dir: consistent lastAccess date");
     }
 
   }
   ok(encountered_tmp_file, "test_iter_dir: We have found the temporary file");
 
   ok(true, "test_iter_dir: Cleaning up");
   iterator.close();
+
+  // Testing nextBatch()
+  iterator = new OS.File.DirectoryIterator(parent);
+  let allentries = [x for(x in iterator)];
+  iterator.close();
+
+  ok(allentries.length >= 14, "test_iter_dir: Meta-check: the test directory should contain at least 14 items");
+
+  iterator = new OS.File.DirectoryIterator(parent);
+  let firstten = iterator.nextBatch(10);
+  is(firstten.length, 10, "test_iter_dir: nextBatch(10) returns 10 items");
+  for (let i = 0; i < firstten.length; ++i) {
+    is(allentries[i].path, firstten[i].path, "test_iter_dir: Checking that batch returns the correct entries");
+  }
+  let nextthree = iterator.nextBatch(3);
+  is(nextthree.length, 3, "test_iter_dir: nextBatch(3) returns 3 items");
+  for (let i = 0; i < nextthree.length; ++i) {
+    is(allentries[i + firstten.length].path, nextthree[i].path, "test_iter_dir: Checking that batch 2 returns the correct entries");
+  }
+  let everythingelse = iterator.nextBatch();
+  ok(everythingelse.length >= 1, "test_iter_dir: nextBatch() returns at least one item");
+  for (let i = 0; i < everythingelse.length; ++i) {
+    is(allentries[i + firstten.length + nextthree.length].path, everythingelse[i].path, "test_iter_dir: Checking that batch 3 returns the correct entries");
+  }
+  is(iterator.nextBatch().length, 0, "test_iter_dir: Once there is nothing left, nextBatch returns an empty array");
+  iterator.close();
+
+  iterator = new OS.File.DirectoryIterator(parent);
+  iterator.close();
+  is(iterator.nextBatch().length, 0, "test_iter_dir: nextBatch on closed iterator returns an empty array");
+
+  iterator = new OS.File.DirectoryIterator(parent);
+  let allentries2 = iterator.nextBatch();
+  is(allentries.length, allentries2.length, "test_iter_dir: Checking that getBatch(null) returns the right number of entries");
+  for (let i = 0; i < allentries.length; ++i) {
+    is(allentries[i].path, allentries2[i].path, "test_iter_dir: Checking that getBatch(null) returns everything in the right order");
+  }
+  iterator.close();
+
+  // Test forEach
+  iterator = new OS.File.DirectoryIterator(parent);
+  let index = 0;
+  iterator.forEach(
+    function cb(entry, aIndex, aIterator) {
+      is(index, aIndex, "test_iter_dir: Checking that forEach index is correct");
+      ok(iterator == aIterator, "test_iter_dir: Checking that right iterator is passed");
+      if (index < 10) {
+        is(allentries[index].path, entry.path, "test_iter_dir: Checking that forEach entry is correct");
+      } else if (index == 10) {
+        iterator.close();
+      } else {
+        ok(false, "test_iter_dir: Checking that forEach can be stopped early");
+      }
+      ++index;
+  });
+  iterator.close();
+
   ok(true, "test_iter_dir: Complete");
 }
 
 function test_position() {
   ok(true, "test_position: Starting");
 
   ok("POS_START" in OS.File, "test_position: POS_START exists");
   ok("POS_CURRENT" in OS.File, "test_position: POS_CURRENT exists");