Bug 768654 - Device Storage - Add test that verifies the content written. r=bent
☠☠ backed out by 7db81ae082e7 ☠ ☠
authorDoug Turner <dougt@dougt.org>
Fri, 13 Jul 2012 09:36:19 -0700
changeset 99261 b3f14a850269e3d99505b4a39c47e77f78932a02
parent 99260 592bfc94e62829fc239c17aa19f19f10e088aace
child 99262 f9d877d78294a2825c04248ce698628c4078d4ce
push id11919
push userdougt@mozilla.com
push dateSat, 14 Jul 2012 03:37:07 +0000
treeherdermozilla-inbound@c7335ca109a4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbent
bugs768654
milestone16.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 768654 - Device Storage - Add test that verifies the content written. r=bent
dom/devicestorage/test/devicestorage_common.js
dom/devicestorage/test/test_basic.html
dom/devicestorage/test/test_enumerate.html
--- a/dom/devicestorage/test/devicestorage_common.js
+++ b/dom/devicestorage/test/devicestorage_common.js
@@ -9,43 +9,47 @@ var oldVal = false;
 Array.prototype.remove = function(from, to) {
   var rest = this.slice((to || from) + 1 || this.length);
   this.length = from < 0 ? this.length + from : from;
   return this.push.apply(this, rest);
 };
 
 function devicestorage_setup() {
   SimpleTest.waitForExplicitFinish();
-  try {
-    oldVal = SpecialPowers.getBoolPref("device.storage.enabled");
-  } catch(e) {}
-  SpecialPowers.setBoolPref("device.storage.enabled", true);
-  SpecialPowers.setBoolPref("device.storage.testing", true);
-  SpecialPowers.setBoolPref("device.storage.prompt.testing", true);
+  if (SpecialPowers.isMainProcess()) {
+    try {
+      oldVal = SpecialPowers.getBoolPref("device.storage.enabled");
+    } catch(e) {}
+    SpecialPowers.setBoolPref("device.storage.enabled", true);
+    SpecialPowers.setBoolPref("device.storage.testing", true);
+    SpecialPowers.setBoolPref("device.storage.prompt.testing", true);
+  }
 }
 
 function devicestorage_cleanup() {
-  SpecialPowers.setBoolPref("device.storage.enabled", oldVal);
-  SpecialPowers.setBoolPref("device.storage.testing", false);
-  SpecialPowers.setBoolPref("device.storage.prompt.testing", false);
+  if (SpecialPowers.isMainProcess()) {
+    SpecialPowers.setBoolPref("device.storage.enabled", oldVal);
+    SpecialPowers.setBoolPref("device.storage.testing", false);
+    SpecialPowers.setBoolPref("device.storage.prompt.testing", false);
+  }
   SimpleTest.finish();
 }
 
 function getRandomBuffer() {
   var size = 1024;
   var buffer = new ArrayBuffer(size);
   var view = new Uint8Array(buffer);
   for (var i = 0; i < size; i++) {
     view[i] = parseInt(Math.random() * 255);
   }
   return buffer;
 }
 
 function createRandomBlob() {
- return blob = new Blob([getRandomBuffer()], {type: 'binary/random'});
+  return blob = new Blob([getRandomBuffer()], {type: 'binary/random'});
 }
 
 function randomFilename(l) {
     var set = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
     var result = "";
     for (var i=0; i<l; i++) {
 	var r = Math.floor(set.length * Math.random());
 	result += set.substring(r, r + 1);
--- a/dom/devicestorage/test/test_basic.html
+++ b/dom/devicestorage/test/test_basic.html
@@ -20,30 +20,33 @@ https://bugzilla.mozilla.org/show_bug.cg
   
 </div>
 <pre id="test">
 <script class="testbody" type="text/javascript">
 
 devicestorage_setup();
 
 var gFileName = "devicestorage/hi";
+var gData = "My name is Doug Turner.  My IRC nick is DougT.  I like Maple cookies."
+var gDataBlob = new Blob([gData], {type: 'text/plain'});
 
 function getAfterDeleteSuccess(e) {
   ok(false, "file was deleted not successfully");
   devicestorage_cleanup();
 }
 
 function getAfterDeleteError(e) {
   ok(true, "file was deleted successfully");
   devicestorage_cleanup();
 }
 
 function deleteSuccess(e) {
 
   ok(e.target.result == gFileName, "File name should match");
+  dump(e.target.result + "\n")
 
   var storage = navigator.getDeviceStorage("profile");
   request = storage[0].get(e.target.result);
   request.onsuccess = getAfterDeleteSuccess;
   request.onerror = getAfterDeleteError;
 
 }
 
@@ -53,25 +56,41 @@ function deleteError(e) {
 }
 
 function getSuccess(e) {
   var storage = navigator.getDeviceStorage("profile");
   ok(navigator.getDeviceStorage, "Should have getDeviceStorage");
 
   ok(e.target.result.name == gFileName, "File name should match");
 
-  request = storage[0].delete(e.target.result.name)
-  request.onsuccess = deleteSuccess;
-  request.onerror = deleteError;
+  var name = e.target.result.name;
+
+  var reader = new FileReader();
+  reader.readAsArrayBuffer(gDataBlob);
+  reader.onload = function(e) {
+    readerCallback(e);
+
+    request = storage[0].delete(name)
+    request.onsuccess = deleteSuccess;
+    request.onerror = deleteError;
+  }
+}
+
+function readerCallback(e) {
+
+  ab = e.target.result;
+
+  is(ab.byteLength, gData.length, "wrong arraybuffer byteLength");
+  var u8v = new Uint8Array(ab);
+  is(String.fromCharCode.apply(String, u8v), gData, "wrong values");
 }
 
 function getError(e) {
   ok(false, "getError was called : " + e.target.error.name);
-  SpecialPowers.setBoolPref("device.storage.enabled", oldVal);
-  SimpleTest.finish();
+  devicestorage_cleanup();
 }
 
 function addSuccess(e) {
 
   ok(e.target.result == gFileName, "File name should match");
 
   var storage = navigator.getDeviceStorage("profile");
   request = storage[0].get(gFileName);
@@ -81,20 +100,22 @@ function addSuccess(e) {
   ok(true, "addSuccess was called");
 }
 
 function addError(e) {
   ok(false, "addError was called : " + e.target.error.name);
   devicestorage_cleanup();
 }
 
-var storage = navigator.getDeviceStorage("profile");
 ok(navigator.getDeviceStorage, "Should have getDeviceStorage");
 
-request = storage[0].addNamed(createRandomBlob(), "devicestorage/hi");
+var storage = navigator.getDeviceStorage("profile");
+ok(storage, "Should have gotten a storage");
+
+request = storage[0].addNamed(gDataBlob, "devicestorage/hi");
 ok(request, "Should have a non-null request");
 
 request.onsuccess = addSuccess;
 request.onerror = addError;
 
 </script>
 </pre>
 </body>
--- a/dom/devicestorage/test/test_enumerate.html
+++ b/dom/devicestorage/test/test_enumerate.html
@@ -23,17 +23,17 @@ https://bugzilla.mozilla.org/show_bug.cg
 <script class="testbody" type="text/javascript">
 
 devicestorage_setup();
 
 function enumerateSuccess(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);
+    dump("We still have length = " + files.length + "\n");
     devicestorage_cleanup();
     return;
   }
   
   var filename = e.target.result.name;
 
   var index = files.indexOf(filename);
   files.remove(index);