Bug 1166580 - Add tests for mozHasPendingMessage. r=fabrice, a=jocheng
authorKan-Ru Chen <kanru@kanru.info>
Tue, 19 May 2015 18:22:36 +0800
changeset 238446 6e4eaf59efda9d59d034aac13d740d0e229c5f96
parent 238445 9f090b47fed8b579e2e2b606522e35796b25b2ad
child 238447 a8bd63f5742412742f8a453dfadfa3f71bb7dd5b
push id624
push userryanvm@gmail.com
push dateThu, 21 May 2015 01:28:21 +0000
treeherdermozilla-b2g37_v2_2@6e4eaf59efda [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfabrice, jocheng
bugs1166580
milestone37.0
Bug 1166580 - Add tests for mozHasPendingMessage. r=fabrice, a=jocheng
dom/messages/SystemMessagePermissionsChecker.jsm
dom/messages/moz.build
dom/messages/test/chrome.ini
dom/messages/test/file_hasPendingMessage.html
dom/messages/test/test_hasPendingMessage.html
--- a/dom/messages/SystemMessagePermissionsChecker.jsm
+++ b/dom/messages/SystemMessagePermissionsChecker.jsm
@@ -62,16 +62,17 @@ this.SystemMessagePermissionsTable = {
   "cellbroadcast-received": {
     "cellbroadcast": []
   },
   "connection": { },
   "captive-portal": {
     "wifi-manage": []
   },
   "dummy-system-message": { }, // for system message testing framework
+  "dummy-system-message2": { }, // for system message testing framework
   "headset-button": { },
   "icc-stkcommand": {
     "settings": ["read", "write"]
   },
   "media-button": { },
   "networkstats-alarm": {
     "networkstats-manage": []
   },
--- a/dom/messages/moz.build
+++ b/dom/messages/moz.build
@@ -13,8 +13,9 @@ EXTRA_COMPONENTS += [
     'SystemMessageManager.manifest',
 ]
 
 EXTRA_JS_MODULES += [
     'SystemMessagePermissionsChecker.jsm',
 ]
 
 MOCHITEST_MANIFESTS += ['test/mochitest.ini']
+MOCHITEST_CHROME_MANIFESTS += ['test/chrome.ini']
new file mode 100644
--- /dev/null
+++ b/dom/messages/test/chrome.ini
@@ -0,0 +1,3 @@
+[test_hasPendingMessage.html]
+skip-if = (buildapp != "browser") || e10s
+support-files = file_hasPendingMessage.html
new file mode 100644
--- /dev/null
+++ b/dom/messages/test/file_hasPendingMessage.html
@@ -0,0 +1,41 @@
+<!DOCTYPE HTML>
+<html>
+  <head>
+    <script type="application/javascript;version=1.8">
+      "use strict";
+      if (navigator.mozHasPendingMessage("dummy-system-message")) {
+        alert("before load");
+      }
+      window.addEventListener("load", e => {
+        if (navigator.mozHasPendingMessage("dummy-system-message")) {
+          alert("load");
+        }
+      });
+      window.addEventListener("hashchange", e => {
+        switch (window.location.hash) {
+          case "#checkPendingMessages":
+            if (navigator.mozHasPendingMessage("dummy-system-message2")) {
+              alert("after launch");
+            }
+            break;
+          case "#setMessageHandler":
+            navigator.mozSetMessageHandler("dummy-system-message", () => {
+              alert("handle message");
+              if (!navigator.mozHasPendingMessage("dummy-system-message")) {
+                alert("no hasPendingMessages");
+              }
+            });
+            break;
+          case "#setMessageHandler2":
+            navigator.mozSetMessageHandler("dummy-system-message2", () => {
+              alert("handle message");
+              if (!navigator.mozHasPendingMessage("dummy-system-message2")) {
+                alert("no hasPendingMessages");
+              }
+            });
+            break;
+        }
+      });
+    </script>
+  </head>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/messages/test/test_hasPendingMessage.html
@@ -0,0 +1,96 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>Test navigator.mozHasPendingMessage</title>
+  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="chrome://global/skin"/>
+  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
+  <script type="application/javascript;version=1.8">
+    "use strict";
+
+    const { utils: Cu, interfaces: Ci, classes: Cc } = Components;
+    Cu.import("resource://gre/modules/Services.jsm");
+
+    SimpleTest.waitForExplicitFinish();
+
+    function expectAlert(iframe, message, assertion) {
+      return new Promise((resolve, reject) => {
+        iframe.addEventListener("mozbrowsershowmodalprompt", function cb(e) {
+          iframe.removeEventListener("mozbrowsershowmodalprompt", cb);
+          is(e.detail.message, message, assertion);
+          resolve();
+        });
+      });
+    }
+
+    function runTest(isRemote) {
+      return new Promise((finish, reject) => {
+        let testURL = "http://mochi.test:8888/chrome/dom/messages/test/file_hasPendingMessage.html";
+        let manifestURL = "http://example.org/manifest.webapp";
+        let testURI = Services.io.newURI(testURL, null, null);
+        let manifestURI = Services.io.newURI(manifestURL, null, null);
+        let body = document.getElementsByTagName("body")[0];
+        let test_frame = document.createElement("iframe");
+        test_frame.setAttribute("mozbrowser", "true");
+        test_frame.setAttribute("remote", isRemote);
+        test_frame.setAttribute("src", testURL);
+        test_frame.setAttribute("mozapp", manifestURL);
+
+        // Step 0. Register Page or install app
+        let smi = Cc["@mozilla.org/system-message-internal;1"]
+                  .getService(Ci.nsISystemMessagesInternal);
+        smi.registerPage("dummy-system-message", testURI, manifestURI);
+        smi.registerPage("dummy-system-message2", testURI, manifestURI);
+        // XXX lie to SystemMessageInternal to unblock sendMessage
+        Services.obs.notifyObservers(null, "webapps-registry-ready", null);
+        // Step 1. Send system message
+        smi.sendMessage("dummy-system-message", null, testURI, manifestURI);
+        // Step 2. Run app & check hasPendingMessages
+        body.appendChild(test_frame);
+        expectAlert(test_frame, "before load", "hasPendingMessages before load").then(() => {
+          // Step 3. Wait load & check hasPendingMessages
+          return expectAlert(test_frame, "load", "hasPendingMessages after load");
+        }).then(() => {
+          // Step 4. Send second system message
+          smi.sendMessage("dummy-system-message2", null, testURI, manifestURI);
+          // Step 5. Check hasPendingMessages
+          test_frame.src = testURL + "#checkPendingMessages";
+          return expectAlert(test_frame, "after launch", "hasPendingMessages after app launch");
+        }).then(() => {
+          // Step 6. mozSetMessageHandler
+          test_frame.src = testURL + "#setMessageHandler";
+          return expectAlert(test_frame, "handle message", "message handled");
+        }).then(() => {
+          // Setp 7. Check hasPendingMessages
+          return expectAlert(test_frame, "no hasPendingMessages",
+                             "no hasPendingMessages after mozSetMessageHandler");
+        }).then(() => {
+          // Step 6. mozSetMessageHandler2
+          test_frame.src = testURL + "#setMessageHandler2";
+          return expectAlert(test_frame, "handle message", "message handled");
+        }).then(() => {
+          // Setp 7. Check hasPendingMessages2
+          return expectAlert(test_frame, "no hasPendingMessages",
+                             "no hasPendingMessages after mozSetMessageHandler");
+        }).then(() => {
+          test_frame.remove();
+          finish();
+        });
+      });
+    }
+
+    window.addEventListener("load", function onLoad() {
+      window.removeEventListener("load", onLoad);
+      SpecialPowers.pushPrefEnv({'set': [["dom.mozBrowserFramesEnabled", true],
+                                         ["dom.sysmsg.enabled", true]]}, () => {
+        runTest(/* isRemote= */false)
+          .then(() => runTest(/* isRemote= */true))
+          .then(SimpleTest.finish);
+      });
+    });
+  </script>
+</head>
+<body>
+</body>
+</html>