Bug 901456 - Fix test_lastModificationFilter.html to verify timestamps when creating test files. r=janv, a=test-only
authorDave Hylands <dhylands@mozilla.com>
Fri, 22 Nov 2013 10:55:36 -0800
changeset 167539 153b8972b169685040d39aa1c70588ba9c17b316
parent 167538 0a2d470b157422a1d306c287c04c369cd625abe2
child 167540 f403af8356f9114fb6fcee7e61b8e377f7f3b42e
push id428
push userbbajaj@mozilla.com
push dateTue, 28 Jan 2014 00:16:25 +0000
treeherdermozilla-release@cd72a7ff3a75 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjanv, test-only
bugs901456
milestone27.0a2
Bug 901456 - Fix test_lastModificationFilter.html to verify timestamps when creating test files. r=janv, a=test-only
dom/devicestorage/test/test_lastModificationFilter.html
--- a/dom/devicestorage/test/test_lastModificationFilter.html
+++ b/dom/devicestorage/test/test_lastModificationFilter.html
@@ -20,24 +20,24 @@ https://bugzilla.mozilla.org/show_bug.cg
   
 </div>
 <pre id="test">
 <script class="testbody" type="text/javascript">
 
 devicestorage_setup();
 
 var oldFiles = ["a.png", "b.png", "c.png"];
+var timeFile = "t.png";
 var newFiles = ["d.png", "e.png", "f.png"];
 
 var storage = navigator.getDeviceStorage('pictures');
 var prefix = "devicestorage/" + randomFilename(12);
-var callback;
-var files;
 var i;
 var timestamp;
+var lastFileAddedTimestamp;
 
 function verifyAndDelete(prefix, files, e) {
   if (e.target.result == null) {
     ok(files.length == 0, "when the enumeration is done, we shouldn't have any files in this array")
     dump("We still have length = " + files.length + "\n");
     dump(files + "\n");
     devicestorage_cleanup();
     return;
@@ -57,78 +57,112 @@ function verifyAndDelete(prefix, files, 
     filename = filename.substring(prefix.length + 1); // Remove prefix
   }
 
   var index = files.indexOf(filename);
   ok(index > -1, "filename should be in the enumeration : " + e.target.result.name);
   if (index == -1)
     return;
 
-  files.remove(index);
+  files.splice(index, 1);
 
   // clean up
   var cleanup = storage.delete(e.target.result.name);
   cleanup.onsuccess = function(e) {}
 }
 
-function addSuccess(e) {
-  i = i + 1;
-  if (i == files.length) {
-    callback();
-    return;
+function addFile(filename, callback) {
+  var addReq = storage.addNamed(createRandomBlob('image/png'), prefix + '/' + filename);
+  addReq.onsuccess = function(e) {
+    // After adding the file, we go ahead and grab the timestamp of the file
+    // that we just added
+    var getReq = storage.get(prefix + '/' + filename);
+    getReq.onsuccess = function(e) {
+      lastFileAddedTimestamp = e.target.result.lastModifiedDate;
+      callback();
+    }
+    getReq.onerror = function(e) {
+      ok(false, "getError was called : " + e.target.error.name);
+      devicestorage_cleanup();
+    };
   }
-  addFile(files[i]);
+  addReq.onerror = function(e) {
+    ok(false, "addError was called : " + e.target.error.name);
+    devicestorage_cleanup();
+  }
 }
 
-function addError(e) {
-  ok(false, "addError was called : " + e.target.error.name);
-  devicestorage_cleanup();
+function addFileArray(fileArray, callback) {
+  var i = 0;
+  function addNextFile() {
+    i = i + 1;
+    if (i == fileArray.length) {
+      callback();
+      return;
+    }
+    addFile(fileArray[i], addNextFile);
+  }
+  addFile(fileArray[0], addNextFile);
 }
 
-function addFile(filename){
-  var req = storage.addNamed(createRandomBlob('image/png'), prefix + '/' + files[i]);
-  req.onsuccess = addSuccess;
-  req.onerror = addError;
+function delFile(filename, callback) {
+  var req = storage.delete(prefix + '/' + filename);
+  req.onsuccess = function(e) {
+    callback();
+  };
+  req.onerror = function(e) {
+    ok(false, "delError was called : " + e.target.error.name);
+    devicestorage_cleanup();
+  };
 }
 
 function afterNewFiles() {
   var cursor = storage.enumerate(prefix, {"since": timestamp});
   cursor.onsuccess = function(e) {
     verifyAndDelete(prefix, newFiles, e);
     if (e.target.result) {
       e.target.continue();
     }
   };
   cursor.onerror = function (e) {
     ok(false, "handleError was called : " + e.target.error.name);
     devicestorage_cleanup();
   };
 }
 
-function addNewFiles() {
-  i = 0;
-  files = newFiles;
-  callback = afterNewFiles;
-  addFile(files[0]);
-}
-
-function beforeNewFiles() {
-  timestamp = new Date();
-  setTimeout(addNewFiles, 1000);
+function waitForTimestampChange() {
+  // We've added a new file. See if the timestamp differs from
+  // oldFileAddedTimestamp, and if it's the same wait for a bit
+  // and try again.
+  if (lastFileAddedTimestamp.valueOf() === timestamp.valueOf()) {
+    delFile(timeFile, function() {
+      setTimeout(function() {
+        addFile(timeFile, waitForTimestampChange);
+      }, 1000);
+    });
+  } else {
+    timestamp = lastFileAddedTimestamp;
+    // The timestamp has changed. Go ahead and add the rest of the new files
+    delFile(timeFile, function() {
+      addFileArray(newFiles, afterNewFiles);
+    });
+  }
 }
 
 function afterOldFiles() {
-  setTimeout(beforeNewFiles, 1000);
+  timestamp = lastFileAddedTimestamp;
+  setTimeout(function() {
+    // We've added our old files and waited for a second.
+    // Add a new file until the timestamp changes
+    addFile(timeFile, waitForTimestampChange);
+  }, 1000);
 }
 
 function addOldFiles() {
-  i = 0;
-  files = oldFiles;
-  callback = afterOldFiles;
-  addFile(files[0]);
+  addFileArray(oldFiles, afterOldFiles);
 }
 
 addOldFiles();
 
 </script>
 </pre>
 </body>
 </html>