Bug 1043110 - Part 3: test case for widget navigation issue, and a little refactor. r=fabrice
authorJunior Hsu <juhsu@mozilla.com>
Mon, 13 Apr 2015 15:06:23 +0800
changeset 287114 fb2a33f119cd80d69f9d9c3546791b1a8a81bf6d
parent 287113 623942e8b05210ac1deec9ebb1380b9e298bbc29
child 287115 b268245c9b6f8a95261762527ec223b31fb67915
push id5067
push userraliiev@mozilla.com
push dateMon, 21 Sep 2015 14:04:52 +0000
treeherdermozilla-beta@14221ffe5b2f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfabrice
bugs1043110
milestone42.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 1043110 - Part 3: test case for widget navigation issue, and a little refactor. r=fabrice Refactor detail 1. s/'/"/ 2. change the script in |loadFrameScript| to let it have syntax highlight 3. Remove irrelative datastore things
dom/apps/tests/file_test_widget.js
--- a/dom/apps/tests/file_test_widget.js
+++ b/dom/apps/tests/file_test_widget.js
@@ -1,15 +1,15 @@
-var gWidgetManifestURL = 'http://test/tests/dom/apps/tests/file_app.sjs?apptype=widget&getmanifest=true';
-var gInvalidWidgetManifestURL = 'http://test/tests/dom/apps/tests/file_app.sjs?apptype=invalidWidget&getmanifest=true';
+var gWidgetManifestURL = "http://test/tests/dom/apps/tests/file_app.sjs?apptype=widget&getmanifest=true";
+var gInvalidWidgetManifestURL = "http://test/tests/dom/apps/tests/file_app.sjs?apptype=invalidWidget&getmanifest=true";
 var gApp;
 var gHasBrowserPermission;
 
-function onError() {
-  ok(false, "Error callback invoked");
+function onError(msg) {
+  ok(false, "Error callback invoked: " + msg);
   finish();
 }
 
 function installApp(path) {
   var request = navigator.mozApps.install(path);
   request.onerror = onError;
   request.onsuccess = function() {
     gApp = request.result;
@@ -28,79 +28,95 @@ function uninstallApp() {
 
     runTest();
   }
 }
 
 function testApp(isValidWidget) {
   info("Test widget feature. IsValidWidget: " + isValidWidget);
 
-  var ifr = document.createElement('iframe');
-  ifr.setAttribute('mozbrowser', 'true');
-  ifr.setAttribute('mozwidget', gApp.manifestURL);
-  ifr.setAttribute('src', gApp.origin+gApp.manifest.launch_path);
+  var ifr = document.createElement("iframe");
+  ifr.setAttribute("mozbrowser", "true");
+  ifr.setAttribute("mozwidget", gApp.manifestURL);
+  ifr.setAttribute("src", gApp.origin + gApp.manifest.launch_path);
 
-  var domParent = document.getElementById('container');
+  var domParent = document.getElementById("container");
   domParent.appendChild(ifr);
 
-  var mm = SpecialPowers.getBrowserFrameMessageManager(ifr);
-  mm.addMessageListener('OK', function(msg) {
-    ok(isValidWidget, "Message from widget: " + SpecialPowers.wrap(msg).json);
-  });
-  mm.addMessageListener('KO', function(msg) {
-    ok(!isValidWidget, "Message from widget: " + SpecialPowers.wrap(msg).json);
-  });
-  mm.addMessageListener('DONE', function(msg) {
-    ok(true, "Message from widget complete: "+SpecialPowers.wrap(msg).json);
+  ifr.addEventListener("mozbrowserloadend", function _onloadend() {
+    ok(true, "receive mozbrowserloadend");
+    // Test limited browser API feature only for valid widget case
+    if (isValidWidget) {
+      testLimitedBrowserAPI(ifr);
+    }
+    SimpleTest.executeSoon(() => loadFrameScript(mm,
+                                                 checkIsWidgetScript,
+                                                 true));
+  }, false);
+
+  ifr.addEventListener("mozbrowsererror", (event) => {
+    ok(!isValidWidget, "receive mozbrowsererror: " + JSON.stringify(event.detail));
     domParent.removeChild(ifr);
     runTest();
   });
 
-  ifr.addEventListener('mozbrowserloadend', function() {
-    ok(true, "receive mozbrowserloadend");
+  // Callback of frameScript
+  var mm = SpecialPowers.getBrowserFrameMessageManager(ifr);
+  mm.addMessageListener("OK", function(msg) {
+    ok(isValidWidget, "Message from widget: " + SpecialPowers.wrap(msg).json);
+  });
+  mm.addMessageListener("KO", function(msg) {
+    ok(!isValidWidget, "Message from widget: " + SpecialPowers.wrap(msg).json);
+  });
+  mm.addMessageListener("DONE", function _done(msg) {
+    ok(true, "Message from widget complete: " + SpecialPowers.wrap(msg).json);
 
-    // Test limited browser API feature only for valid widget case
-    if (isValidWidget) {
-      testLimitedBrowserAPI(ifr);
-    }
-    SimpleTest.executeSoon(()=>loadFrameScript(mm));
-  }, false);
+    mm.removeMessageListener("DONE", _done);
+    mm.addMessageListener("DONE", function _done(msg) {
+      ok(true, "Message from widget complete: " + SpecialPowers.wrap(msg).json);
+      runTest();
+    });
+
+    info("set src to a invalid page");
+    ifr.setAttribute("src", gApp.origin);
+    isValidWidget = false;
+  });
 
   // Test limited browser API feature only for valid widget case
   if (!isValidWidget) {
     return;
   }
 
   [
-    'mozbrowsertitlechange',
-    'mozbrowseropenwindow',
-    'mozbrowserscroll',
-    'mozbrowserasyncscroll'
+    "mozbrowsertitlechange",
+    "mozbrowseropenwindow",
+    "mozbrowserscroll",
+    "mozbrowserasyncscroll"
   ].forEach( function(topic) {
     ifr.addEventListener(topic, function() {
       ok(false, topic + " should be hidden");
     }, false);
   });
 }
 
 function testLimitedBrowserAPI(ifr) {
   var securitySensitiveCalls = [
-    { api: 'sendMouseEvent'      , args: ['mousedown', 0, 0, 0, 0, 0] },
-    { api: 'sendTouchEvent'      , args: ['touchstart', [0], [0], [0], [1], [1], [0], [1], 1, 0] },
-    { api: 'goBack'              , args: [] },
-    { api: 'goForward'           , args: [] },
-    { api: 'reload'              , args: [] },
-    { api: 'stop'                , args: [] },
-    { api: 'download'            , args: ['http://example.org'] },
-    { api: 'purgeHistory'        , args: [] },
-    { api: 'getScreenshot'       , args: [0, 0] },
-    { api: 'zoom'                , args: [0.1] },
-    { api: 'getCanGoBack'        , args: [] },
-    { api: 'getCanGoForward'     , args: [] },
-    { api: 'getContentDimensions', args: [] }
+    { api: "sendMouseEvent"      , args: ["mousedown", 0, 0, 0, 0, 0] },
+    { api: "sendTouchEvent"      , args: ["touchstart", [0], [0], [0], [1], [1], [0], [1], 1, 0] },
+    { api: "goBack"              , args: [] },
+    { api: "goForward"           , args: [] },
+    { api: "reload"              , args: [] },
+    { api: "stop"                , args: [] },
+    { api: "download"            , args: ["http://example.org"] },
+    { api: "purgeHistory"        , args: [] },
+    { api: "getScreenshot"       , args: [0, 0] },
+    { api: "zoom"                , args: [0.1] },
+    { api: "getCanGoBack"        , args: [] },
+    { api: "getCanGoForward"     , args: [] },
+    { api: "getContentDimensions", args: [] }
   ];
   securitySensitiveCalls.forEach( function(call) {
     if (gHasBrowserPermission) {
       isnot(typeof ifr[call.api], "undefined", call.api + " should be defined");
       var didThrow;
       try {
         ifr[call.api].apply(ifr, call.args);
       } catch (e) {
@@ -109,94 +125,79 @@ function testLimitedBrowserAPI(ifr) {
       }
       is(didThrow, DOMException.INVALID_NODE_TYPE_ERR, "call " + call.api + " should throw exception");
     } else {
       is(typeof ifr[call.api], "undefined", call.api + " should be hidden for widget");
     }
   });
 }
 
-function loadFrameScript(mm) {
-  var script = 'data:,\
-  function ok(p, msg) { \
-  if (p) { \
-  sendAsyncMessage("OK", msg); \
-} else { \
-  sendAsyncMessage("KO", msg); \
-} \
-} \
-  \
-  function is(a, b, msg) { \
-  if (a == b) { \
-  sendAsyncMessage("OK", a + " == " + b + " - " + msg); \
-} else { \
-  sendAsyncMessage("KO", a + " != " + b + " - " + msg); \
-} \
-} \
-  \
-  function finish() { \
-  sendAsyncMessage("DONE",""); \
-} \
-  \
-  function onError() { \
-  ok(false, "Error callback invoked"); \
-  finish(); \
-} \
-  \
-  function checkWidget(widget) { \
-  /*For invalid widget case, ignore the following check*/\
-  if (widget) { \
-  var widgetName = "Really Rapid Release (APPTYPETOKEN)"; \
-  is(widget.origin, "http://test", "Widget origin should be correct"); \
-  is(widget.installOrigin, "http://mochi.test:8888", "Install origin should be correct"); \
-} \
-  finish(); \
-} \
-  \
-  var request = content.window.navigator.mozApps.getSelf(); \
-  request.onsuccess = function() { \
-  var widget = request.result; \
-  ok(widget,"Should be a widget"); \
-  checkWidget(widget); \
-}; \
-  request.onerror = onError; \
-  content.window.open("about:blank"); /*test mozbrowseropenwindow*/ \
-  content.window.scrollTo(4000, 4000); /*test mozbrowser(async)scroll*/ \
-  ';
+function loadFrameScript(mm, frameScript, testMozbrowserEvent) {
+  var script = "data:,(" + frameScript.toString() + ")(" + testMozbrowserEvent + ");";
   mm.loadFrameScript(script, /* allowDelayedLoad = */ false);
 }
 
+function checkIsWidgetScript(testMozbrowserEvent) {
+  function ok(p, msg) {
+    if (p) {
+    sendAsyncMessage("OK", msg);
+    } else {
+    sendAsyncMessage("KO", msg);
+    }
+  }
+
+  function is(a, b, msg) {
+    if (a == b) {
+      sendAsyncMessage("OK", a + " == " + b + " - " + msg);
+    } else {
+      sendAsyncMessage("KO", a + " != " + b + " - " + msg);
+    }
+  }
+
+  function finish() {
+    sendAsyncMessage("DONE", "");
+  }
+
+  function onError() {
+    ok(false, "Error callback invoked");
+    finish();
+  }
+
+  var request = content.window.navigator.mozApps.getSelf();
+  request.onsuccess = function() {
+    var widget = request.result;
+    ok(widget, "Should" + (widget ? "" : " not") + " be a widget");
+    finish();
+  };
+  request.onerror = onError;
+
+  if (testMozbrowserEvent) {
+    content.window.open("about:blank"); /* test mozbrowseropenwindow */
+    content.window.scrollTo(4000, 4000); /* test mozbrowser(async)scroll */
+  }
+}
+
 var tests = [
   // Permissions
   function() {
     SpecialPowers.pushPermissions(
       [{ "type": "browser", "allow": gHasBrowserPermission ? 1 : 0, "context": document },
        { "type": "embed-widgets", "allow": 1, "context": document },
        { "type": "webapps-manage", "allow": 1, "context": document }], runTest);
   },
 
   // Preferences
   function() {
     SpecialPowers.pushPrefEnv({"set": [["dom.mozBrowserFramesEnabled", true],
-                                       ["dom.enable_widgets", true],
-                                       ["dom.datastore.sysMsgOnChangeShortTimeoutSec", 1],
-                                       ["dom.datastore.sysMsgOnChangeLongTimeoutSec", 3]]}, runTest);
-  },
-
-  function() {
-    if (SpecialPowers.isMainProcess()) {
-      SpecialPowers.Cu.import("resource://gre/modules/DataStoreChangeNotifier.jsm");
-    }
-
-    SpecialPowers.setAllAppsLaunchable(true);
-    runTest();
+                                       ["dom.enable_widgets", true]]}, runTest);
   },
 
   // No confirmation needed when an app is installed
   function() {
+    SpecialPowers.setAllAppsLaunchable(true);
     SpecialPowers.autoConfirmAppInstall(() => {
       SpecialPowers.autoConfirmAppUninstall(runTest);
     });
   },
 
   // Installing the app
   ()=>installApp(gWidgetManifestURL),