Bug 949325 - C++ wrapper to support DataStore API on the worker (part 3-3, test sync cases on workers). r=baku
authorGene Lian <clian@mozilla.com>
Wed, 12 Mar 2014 17:03:29 +0800
changeset 181368 27cd1a1fd1d7f8ec0384888f0936fd6c8e1b5f0b
parent 181367 2962581fca93390ce780179fd67cf906f239b13a
child 181369 8a69388a1a2116e1b9c3b36e11bd78ba9b98d378
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewersbaku
bugs949325
milestone32.0a1
Bug 949325 - C++ wrapper to support DataStore API on the worker (part 3-3, test sync cases on workers). r=baku
dom/datastore/tests/file_sync.html
dom/datastore/tests/file_sync_worker.html
dom/datastore/tests/file_sync_worker.js
dom/datastore/tests/mochitest.ini
dom/datastore/tests/test_sync_worker.html
--- a/dom/datastore/tests/file_sync.html
+++ b/dom/datastore/tests/file_sync.html
@@ -367,62 +367,62 @@
         runTest();
       });
     },
 
     function() {
       var steps = [ { operation: 'clear', },
                     { operation: 'add', id: 1, data: 123 },
                     { operation: 'add', id: 2, data: 42 },
-		    { operation: 'add', id: 'foobar', data: 2 } ]
+                    { operation: 'add', id: 'foobar', data: 2 } ]
       testCursor(gCursor, steps);
     },
 
     function() {
       gStore.put(43, 2).then(function(id) {
         gRevisions.push(gStore.revisionId);
         runTest();
       });
     },
 
     function() {
       var steps = [ { operation: 'clear', },
                     { operation: 'add', id: 1, data: 123 },
                     { operation: 'add', id: 2, data: 43 },
-		    { operation: 'add', id: 'foobar', data: 2 } ]
+                    { operation: 'add', id: 'foobar', data: 2 } ]
       testCursor(gCursor, steps);
     },
 
     function() {
       gStore.remove(2).then(function(id) {
         gRevisions.push(gStore.revisionId);
         runTest();
       });
     },
 
     function() {
       var steps = [ { operation: 'clear', },
                     { operation: 'add', id: 1, data: 123 },
-		    { operation: 'add', id: 'foobar', data: 2 } ]
+                    { operation: 'add', id: 'foobar', data: 2 } ]
       testCursor(gCursor, steps);
     },
 
     function() {
       gStore.add(42).then(function(id) {
         ok(true, "Item: " + id + " added");
         gRevisions.push(gStore.revisionId);
         runTest();
       });
     },
 
     function() {
       var steps = [ { operation: 'clear', },
                     { operation: 'add', id: 1, data: 123 },
                     { operation: 'add', id: 4, data: 42 },
-		    { operation: 'add', id: 'foobar', data: 2 } ]
+                    { operation: 'add', id: 'foobar', data: 2 } ]
       testCursor(gCursor, steps);
     },
 
     function() {
       gStore.clear().then(function() {
         gRevisions.push(gStore.revisionId);
         runTest();
       });
new file mode 100644
--- /dev/null
+++ b/dom/datastore/tests/file_sync_worker.html
@@ -0,0 +1,30 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>Test for DataStore - sync</title>
+</head>
+<body>
+<div id="container"></div>
+  <script type="application/javascript;version=1.7">
+
+  var messages = [];
+  var worker = new Worker("file_sync_worker.js");
+
+  worker.onmessage = function(event) {
+    messages.push(event.data)
+
+    if (event.data == 'DONE') {
+      // Free the worker when all the tests are done.
+      worker.terminate();
+
+      // Fire message to the test_sync_worker.html.
+      for (var i = 0; i < messages.length; i++) {
+        alert(messages[i]);
+      }
+    }
+  }
+
+  </script>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/datastore/tests/file_sync_worker.js
@@ -0,0 +1,477 @@
+  var gStore;
+  var gRevisions = [];
+  var gCursor;
+  var gExpectedEvents = true;
+
+  function is(a, b, msg) {
+    postMessage((a === b ? 'OK' : 'KO') + ' ' + msg)
+  }
+
+  function ok(a, msg) {
+    postMessage((a ? 'OK' : 'KO')+ ' ' + msg)
+  }
+
+  function cbError() {
+    postMessage('KO error');
+  }
+
+  function finish() {
+    postMessage('DONE');
+  }
+
+  function testGetDataStores() {
+    navigator.getDataStores('foo').then(function(stores) {
+      is(stores.length, 1, "getDataStores('foo') returns 1 element");
+
+      gStore = stores[0];
+      gRevisions.push(gStore.revisionId);
+
+      gStore.onchange = function(aEvent) {
+        ok(gExpectedEvents, "Events received!");
+        runTest();
+      }
+
+      runTest();
+    }, cbError);
+  }
+
+  function testBasicInterface() {
+    var cursor = gStore.sync();
+    ok(cursor, "Cursor is created");
+
+    // TODO This needs more love for running on workers. Tend to fire a
+    // follow-up for this...
+    // is(cursor.store, gStore, "Cursor.store is the store");
+
+    ok("next" in cursor, "Cursor.next exists");
+    ok("close" in cursor, "Cursor.close exists");
+
+    cursor.close();
+
+    runTest();
+  }
+
+  function testCursor(cursor, steps) {
+    if (!steps.length) {
+      runTest();
+      return;
+    }
+
+    var step = steps.shift();
+    cursor.next().then(function(data) {
+      ok(!!data, "Cursor.next returns data");
+      is(data.operation, step.operation, "Waiting for operation: '" + step.operation + "' received '" + data.operation + "'");
+
+
+      switch (data.operation) {
+        case 'done':
+          is(/[0-9a-zA-Z]{8}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{12}/.test(data.revisionId), true, "done has a valid revisionId");
+          is (data.revisionId, gRevisions[gRevisions.length-1], "Last revision matches");
+          break;
+
+        case 'add':
+        case 'update':
+          if ('id' in step) {
+            is(data.id, step.id, "next() add: id matches: " + data.id + " " + step.id);
+          }
+
+          if ('data' in step) {
+            is(data.data, step.data, "next() add: data matches: " + data.data + " " + step.data);
+          }
+
+          break;
+
+        case 'remove':
+          if ('id' in step) {
+            is(data.id, step.id, "next() add: id matches: " + data.id + " " + step.id);
+          }
+
+          break;
+      }
+
+      testCursor(cursor, steps);
+    });
+  }
+
+  var tests = [
+    // Test for GetDataStore
+    testGetDataStores,
+
+    // interface test
+    testBasicInterface,
+
+    // empty DataStore
+    function() {
+      var cursor = gStore.sync();
+      var steps = [ { operation: 'clear' },
+                    { operation: 'done' },
+                    { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    function() {
+      gExpectedEvents = false;
+      var cursor = gStore.sync('wrong revision ID');
+      var steps = [ { operation: 'clear' },
+                    { operation: 'done' },
+                    { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    function() {
+      var cursor = gStore.sync(gRevisions[0]);
+      var steps = [ { operation: 'done' },
+                    { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    // Test add from scratch
+    function() {
+      gExpectedEvents = true;
+
+      gStore.add(1).then(function(id) {
+        gRevisions.push(gStore.revisionId);
+        ok(true, "Item: " + id + " added");
+      });
+    },
+
+    function() {
+      gStore.add(2,"foobar").then(function(id) {
+        gRevisions.push(gStore.revisionId);
+        ok(true, "Item: " + id + " added");
+      });
+    },
+
+    function() {
+      gStore.add(3,3).then(function(id) {
+        gRevisions.push(gStore.revisionId);
+        ok(true, "Item: " + id + " added");
+      });
+    },
+
+    function() {
+      gExpectedEvents = false;
+      var cursor = gStore.sync();
+      var steps = [ { operation: 'clear', },
+                    { operation: 'add', id: 1, data: 1 },
+                    { operation: 'add', id: 3, data: 3 },
+                    { operation: 'add', id: 'foobar', data: 2 },
+                    { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    function() {
+      var cursor = gStore.sync('wrong revision ID');
+      var steps = [ { operation: 'clear', },
+                    { operation: 'add', id: 1, data: 1 },
+                    { operation: 'add', id: 3, data: 3 },
+                    { operation: 'add', id: 'foobar', data: 2 },
+                    { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    function() {
+      var cursor = gStore.sync(gRevisions[0]);
+      var steps = [ { operation: 'add', id: 1, data: 1 },
+                    { operation: 'add', id: 'foobar', data: 2 },
+                    { operation: 'add', id: 3, data: 3 },
+                    { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    function() {
+      var cursor = gStore.sync(gRevisions[1]);
+      var steps = [ { operation: 'add', id: 'foobar', data: 2 },
+                    { operation: 'add', id: 3, data: 3 },
+                    { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    function() {
+      var cursor = gStore.sync(gRevisions[2]);
+      var steps = [ { operation: 'add', id: 3, data: 3 },
+                    { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    function() {
+      var cursor = gStore.sync(gRevisions[3]);
+      var steps = [ { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    // Test after an update
+    function() {
+      gExpectedEvents = true;
+      gStore.put(123, 1).then(function() {
+        gRevisions.push(gStore.revisionId);
+      });
+    },
+
+    function() {
+      gExpectedEvents = false;
+      var cursor = gStore.sync();
+      var steps = [ { operation: 'clear', },
+                    { operation: 'add', id: 1, data: 123 },
+                    { operation: 'add', id: 3, data: 3 },
+                    { operation: 'add', id: 'foobar', data: 2 },
+                    { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    function() {
+      var cursor = gStore.sync('wrong revision ID');
+      var steps = [ { operation: 'clear', },
+                    { operation: 'add', id: 1, data: 123 },
+                    { operation: 'add', id: 3, data: 3 },
+                    { operation: 'add', id: 'foobar', data: 2 },
+                    { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    function() {
+      var cursor = gStore.sync(gRevisions[0]);
+      var steps = [ { operation: 'add', id: 1, data: 123 },
+                    { operation: 'add', id: 'foobar', data: 2 },
+                    { operation: 'add', id: 3, data: 3 },
+                    { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    function() {
+      var cursor = gStore.sync(gRevisions[1]);
+      var steps = [ { operation: 'add', id: 'foobar', data: 2 },
+                    { operation: 'add', id: 3, data: 3 },
+                    { operation: 'update', id: 1, data: 123 },
+                    { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    function() {
+      var cursor = gStore.sync(gRevisions[2]);
+      var steps = [ { operation: 'add', id: 3, data: 3 },
+                    { operation: 'update', id: 1, data: 123 },
+                    { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    function() {
+      var cursor = gStore.sync(gRevisions[3]);
+      var steps = [ { operation: 'update', id: 1, data: 123 },
+                    { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    function() {
+      var cursor = gStore.sync(gRevisions[4]);
+      var steps = [ { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    // Test after a remove
+    function() {
+      gExpectedEvents = true;
+      gStore.remove(3).then(function() {
+        gRevisions.push(gStore.revisionId);
+      });
+    },
+
+    function() {
+      gExpectedEvents = false;
+      var cursor = gStore.sync();
+      var steps = [ { operation: 'clear', },
+                    { operation: 'add', id: 1, data: 123 },
+                    { operation: 'add', id: 'foobar', data: 2 },
+                    { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    function() {
+      var cursor = gStore.sync('wrong revision ID');
+      var steps = [ { operation: 'clear', },
+                    { operation: 'add', id: 1, data: 123 },
+                    { operation: 'add', id: 'foobar', data: 2 },
+                    { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    function() {
+      var cursor = gStore.sync(gRevisions[0]);
+      var steps = [ { operation: 'add', id: 1, data: 123 },
+                    { operation: 'add', id: 'foobar', data: 2 },
+                    { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    function() {
+      var cursor = gStore.sync(gRevisions[1]);
+      var steps = [ { operation: 'add', id: 'foobar', data: 2 },
+                    { operation: 'update', id: 1, data: 123 },
+                    { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    function() {
+      var cursor = gStore.sync(gRevisions[2]);
+      var steps = [ { operation: 'update', id: 1, data: 123 },
+                    { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    function() {
+      var cursor = gStore.sync(gRevisions[3]);
+      var steps = [ { operation: 'update', id: 1, data: 123 },
+                    { operation: 'remove', id: 3 },
+                    { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    function() {
+      var cursor = gStore.sync(gRevisions[4]);
+      var steps = [ { operation: 'remove', id: 3 },
+                    { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    function() {
+      var cursor = gStore.sync(gRevisions[5]);
+      var steps = [ { operation: 'done' }];
+      testCursor(cursor, steps);
+    },
+
+    // New events when the cursor is active
+    function() {
+      gCursor = gStore.sync();
+      var steps = [ { operation: 'clear', },
+                    { operation: 'add', id: 1, data: 123 },
+                    { operation: 'add', id: 'foobar', data: 2 } ];
+      testCursor(gCursor, steps);
+    },
+
+    function() {
+      gStore.add(42, 2).then(function(id) {
+        ok(true, "Item: " + id + " added");
+        gRevisions.push(gStore.revisionId);
+        runTest();
+      });
+    },
+
+    function() {
+      var steps = [ { operation: 'clear', },
+                    { operation: 'add', id: 1, data: 123 },
+                    { operation: 'add', id: 2, data: 42 },
+                    { operation: 'add', id: 'foobar', data: 2 } ]
+      testCursor(gCursor, steps);
+    },
+
+    function() {
+      gStore.put(43, 2).then(function(id) {
+        gRevisions.push(gStore.revisionId);
+        runTest();
+      });
+    },
+
+    function() {
+      var steps = [ { operation: 'clear', },
+                    { operation: 'add', id: 1, data: 123 },
+                    { operation: 'add', id: 2, data: 43 },
+                    { operation: 'add', id: 'foobar', data: 2 } ]
+      testCursor(gCursor, steps);
+    },
+
+    function() {
+      gStore.remove(2).then(function(id) {
+        gRevisions.push(gStore.revisionId);
+        runTest();
+      });
+    },
+
+    function() {
+      var steps = [ { operation: 'clear', },
+                    { operation: 'add', id: 1, data: 123 },
+                    { operation: 'add', id: 'foobar', data: 2 } ]
+      testCursor(gCursor, steps);
+    },
+
+    function() {
+      gStore.add(42).then(function(id) {
+        ok(true, "Item: " + id + " added");
+        gRevisions.push(gStore.revisionId);
+        runTest();
+      });
+    },
+
+    function() {
+      var steps = [ { operation: 'clear', },
+                    { operation: 'add', id: 1, data: 123 },
+                    { operation: 'add', id: 4, data: 42 },
+                    { operation: 'add', id: 'foobar', data: 2 } ]
+      testCursor(gCursor, steps);
+    },
+
+    function() {
+      gStore.clear().then(function() {
+        gRevisions.push(gStore.revisionId);
+        runTest();
+      });
+    },
+
+    function() {
+      var steps = [ { operation: 'clear' } ];
+      testCursor(gCursor, steps);
+    },
+
+    function() {
+      gStore.add(42).then(function(id) {
+        ok(true, "Item: " + id + " added");
+        gRevisions.push(gStore.revisionId);
+        runTest();
+      });
+    },
+
+    function() {
+      var steps = [ { operation: 'clear', },
+                    { operation: 'add', id: 5, data: 42 } ];
+      testCursor(gCursor, steps);
+    },
+
+    function() {
+      gStore.clear().then(function() {
+        gRevisions.push(gStore.revisionId);
+        runTest();
+      });
+    },
+
+    function() {
+      gStore.add(42).then(function(id) {
+        ok(true, "Item: " + id + " added");
+        gRevisions.push(gStore.revisionId);
+        runTest();
+      });
+    },
+
+    function() {
+      var steps = [ { operation: 'clear' },
+                    { operation: 'add', id: 6, data: 42 },
+                    { operation: 'done'} ];
+      testCursor(gCursor, steps);
+    },
+
+    function() {
+      gExpectedEvents = true;
+      gStore.add(42).then(function(id) {
+      });
+    }
+  ];
+
+  function runTest() {
+    if (!tests.length) {
+      finish();
+      return;
+    }
+
+    var test = tests.shift();
+    test();
+  }
+
+  runTest();
\ No newline at end of file
--- a/dom/datastore/tests/mochitest.ini
+++ b/dom/datastore/tests/mochitest.ini
@@ -8,16 +8,18 @@ support-files =
   file_basic_worker.js
   file_changes.html
   file_changes2.html
   file_app.sjs
   file_app.template.webapp
   file_app2.template.webapp
   file_arrays.html
   file_sync.html
+  file_sync_worker.html
+  file_sync_worker.js
   file_bug924104.html
   file_certifiedApp.html
   file_keys.html
   file_duplicate.html
   file_bug976311.html
   file_bug976311.template.webapp
   file_bug986056.html
   file_bug986056.template.webapp
@@ -32,16 +34,17 @@ skip-if = (buildapp == 'b2g' && (toolkit
 [test_basic_worker.html]
 [test_changes.html]
 skip-if = (toolkit == 'gonk' && debug) #intermittent failures, bug 961021
 [test_arrays.html]
 skip-if = (toolkit == 'gonk' && debug) #debug-only failure; time out
 [test_oop.html]
 skip-if = (buildapp == 'b2g' && (toolkit != 'gonk' || debug)) # b2g-debug(debug-only failure; time out) b2g-desktop(Bug 931116, b2g desktop specific, initial triage)
 [test_sync.html]
+[test_sync_worker.html]
 skip-if = (buildapp == 'b2g' && (toolkit != 'gonk' || debug)) # b2g-debug(debug-only failure; time out) b2g-desktop(Bug 931116, b2g desktop specific, initial triage)
 [test_bug924104.html]
 [test_certifiedApp.html]
 skip-if = (toolkit == 'gonk' && debug) #debug-only failure; time out
 [test_keys.html]
 [test_duplicate.html]
 [test_bug976311.html]
 [test_bug986056.html]
new file mode 100644
--- /dev/null
+++ b/dom/datastore/tests/test_sync_worker.html
@@ -0,0 +1,126 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>Test for DataStore - sync</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+<div id="container"></div>
+  <script type="application/javascript;version=1.7">
+
+  var gHostedManifestURL = 'http://test/tests/dom/datastore/tests/file_app.sjs?testToken=file_sync_worker.html';
+  var gApp;
+
+  function cbError() {
+    ok(false, "Error callback invoked");
+    finish();
+  }
+
+  function installApp() {
+    var request = navigator.mozApps.install(gHostedManifestURL);
+    request.onerror = cbError;
+    request.onsuccess = function() {
+      gApp = request.result;
+      runTest();
+    }
+  }
+
+  function uninstallApp() {
+    // Uninstall the app.
+    var request = navigator.mozApps.mgmt.uninstall(gApp);
+    request.onerror = cbError;
+    request.onsuccess = function() {
+      // All done.
+      info("All done");
+      runTest();
+    }
+  }
+
+  function testApp() {
+    var ifr = document.createElement('iframe');
+    ifr.setAttribute('mozbrowser', 'true');
+    ifr.setAttribute('mozapp', gApp.manifestURL);
+    ifr.setAttribute('src', gApp.manifest.launch_path);
+    var domParent = document.getElementById('container');
+
+    // Set us up to listen for messages from the app.
+    var listener = function(e) {
+      var message = e.detail.message;
+      if (/^OK/.exec(message)) {
+        ok(true, "Message from app: " + message);
+      } else if (/KO/.exec(message)) {
+        ok(false, "Message from app: " + message);
+      } else if (/DONE/.exec(message)) {
+        ok(true, "Messaging from app complete");
+        ifr.removeEventListener('mozbrowsershowmodalprompt', listener);
+        domParent.removeChild(ifr);
+        runTest();
+      }
+    }
+
+    // This event is triggered when the app calls "alert".
+    ifr.addEventListener('mozbrowsershowmodalprompt', listener, false);
+    domParent.appendChild(ifr);
+  }
+
+  var tests = [
+    // Permissions
+    function() {
+      SpecialPowers.pushPermissions(
+        [{ "type": "browser", "allow": 1, "context": document },
+         { "type": "embed-apps", "allow": 1, "context": document },
+         { "type": "webapps-manage", "allow": 1, "context": document }], runTest);
+    },
+
+    // Preferences
+    function() {
+      SpecialPowers.pushPrefEnv({"set": [["dom.datastore.enabled", true],
+                                         ["dom.testing.ignore_ipc_principal", true],
+                                         ["dom.testing.datastore_enabled_for_hosted_apps", true]]}, runTest);
+    },
+
+    function() {
+      SpecialPowers.setBoolPref("dom.mozBrowserFramesEnabled", true);
+      runTest();
+    },
+
+    // No confirmation needed when an app is installed
+    function() {
+      SpecialPowers.autoConfirmAppInstall(runTest);
+    },
+
+    // Installing the app
+    installApp,
+
+    // Run tests in app
+    testApp,
+
+    // Uninstall the app
+    uninstallApp
+  ];
+
+  function runTest() {
+    if (!tests.length) {
+      finish();
+      return;
+    }
+
+    var test = tests.shift();
+    test();
+  }
+
+  function finish() {
+    SimpleTest.finish();
+  }
+
+  if (SpecialPowers.isMainProcess()) {
+    SpecialPowers.Cu.import("resource://gre/modules/DataStoreChangeNotifier.jsm");
+  }
+
+  SimpleTest.waitForExplicitFinish();
+  runTest();
+  </script>
+</body>
+</html>