Bug 840124 - implement postMessage API for remote report (tests) r=jaws, f=gps+gavin
☠☠ backed out by 5c46d8839b7f ☠ ☠
authorMike Connor <mconnor@mozilla.com>
Thu, 14 Mar 2013 21:25:04 -0400
changeset 125518 12e2ef135ea80a3eab2b93fb7493e5a94e30c191
parent 125517 59ef39cbab731e14dd2e2c4fe627b0693c020c6b
child 125519 036412ce176a3890c528f76e98583c202c0dcd9d
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersjaws
bugs840124
milestone22.0a1
Bug 840124 - implement postMessage API for remote report (tests) r=jaws, f=gps+gavin
browser/base/content/test/Makefile.in
browser/base/content/test/browser_aboutHealthReport.js
browser/base/content/test/healthreport_testPostMessage.html
browser/base/content/test/healthreport_testRemoteCommands.html
--- a/browser/base/content/test/Makefile.in
+++ b/browser/base/content/test/Makefile.in
@@ -310,16 +310,19 @@ endif
                  browser_URLBarSetURI.js \
                  browser_bookmark_titles.js \
                  browser_pageInfo_plugins.js \
                  browser_pageInfo.js \
                  feed_tab.html \
                  browser_pluginCrashCommentAndURL.js \
                  pluginCrashCommentAndURL.html \
                  browser_private_no_prompt.js \
+                 browser_aboutHealthReport.js \
+                 healthreport_testPostMessage.html \
+                 healthreport_testRemoteCommands.html \
                  $(NULL)
 
 # Disable test on Windows due to frequent failures (bug 841341)
 ifneq (windows,$(MOZ_WIDGET_TOOLKIT))
 _BROWSER_FILES += \
                 browser_popupNotification.js \
                 $(NULL)
 endif
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/browser_aboutHealthReport.js
@@ -0,0 +1,153 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+XPCOMUtils.defineLazyModuleGetter(this, "Promise",
+  "resource://gre/modules/commonjs/sdk/core/promise.js");
+XPCOMUtils.defineLazyModuleGetter(this, "Task",
+  "resource://gre/modules/Task.jsm");
+
+registerCleanupFunction(function() {
+  // Ensure we don't pollute prefs for next tests.
+  try {
+    Services.prefs.clearUserPref("datareporting.healthreport.about.reportUrl");
+    let policy = Cc["@mozilla.org/datareporting/service;1"]
+                 .getService(Ci.nsISupports)
+                 .wrappedJSObject
+                 .policy;
+        policy.recordHealthReportUploadEnabled(true,
+                                           "Resetting after tests.");
+  } catch (ex) {}
+});
+
+let gTests = [
+
+{
+  desc: "Check that about:healthreport loads correctly",
+  setup: function ()
+  {
+  },
+  run: function ()
+  {
+    is(gBrowser.selectedTab.linkedBrowser.currentURI.spec, "about:healthreport", "Page loaded correctly");
+  }
+},
+
+
+{
+  desc: "Test the postMessage API",
+  setup: function ()
+  {
+    Services.prefs.setCharPref("datareporting.healthreport.about.reportUrl", 
+                               "https://example.com/browser/browser/base/content/test/healthreport_testPostMessage.html");
+  },
+  run: function ()
+  {
+    let deferred = Promise.defer();
+
+    let acks = {}
+    try {
+      let win = gBrowser.contentWindow;
+      win.addEventListener("message", function process(e) {
+        acks[e.data.type] = true;
+        if (Object.keys(acks).length == 2) {
+          ok(acks["prefs"],   "Prefs sent ok");
+          ok(acks["payload"], "Payload sent ok");
+          win.removeEventListener("message", process, false, true);
+          deferred.resolve();
+        }
+      }, false, true);
+
+    } catch(e) {
+      deferred.resolve()
+    }
+    return deferred.promise;
+  }
+},
+
+{
+  desc: "Test the remote commands",
+  setup: function ()
+  {
+    Services.prefs.setCharPref("datareporting.healthreport.about.reportUrl",
+                               "https://example.com/browser/browser/base/content/test/healthreport_testRemoteCommands.html");
+  },
+  run: function ()
+  {
+    let deferred = Promise.defer();
+
+    let policy = Cc["@mozilla.org/datareporting/service;1"]
+                 .getService(Ci.nsISupports)
+                 .wrappedJSObject
+                 .policy;
+
+    let acks = {}
+    try {
+      let win = gBrowser.contentWindow;
+      win.addEventListener("message", function testLoad(e) {
+        if (!e.data.command)
+          return;
+
+        acks[e.data.command] = true;
+        switch (e.data.command) {
+          case "DisableDataSubmission":
+            is(policy.healthReportUploadEnabled, false, "FHR successfully disabled");
+            break;
+          case "EnableDataSubmission":
+            is(policy.healthReportUploadEnabled, true,  "FHR successfully enabled");
+            break;
+          case "RequestCurrentPrefs":
+          case "RequestCurrentPayload":
+            ok(true, e.data.command + " successfully processed");
+        }
+        if (Object.keys(acks).length == 4) {
+          deferred.resolve();
+        }
+      }, false, true);
+
+    } catch(e) {
+      ok(false, "Failed to get all commands");
+      deferred.reject();
+    }
+    return deferred.promise;
+  }
+},
+
+
+]; // gTests
+
+function test()
+{
+  waitForExplicitFinish();
+
+  Task.spawn(function () {
+    for (let test of gTests) {
+      info(test.desc);
+      test.setup();
+
+      yield promiseNewTabLoadEvent("about:healthreport");
+
+      yield test.run();
+
+      gBrowser.removeCurrentTab();
+    }
+
+    finish();
+  });
+}
+
+function promiseNewTabLoadEvent(aUrl, aEventType="load")
+{
+  let deferred = Promise.defer();
+  let tab = gBrowser.selectedTab = gBrowser.addTab(aUrl);
+  tab.linkedBrowser.addEventListener(aEventType, function load(event) {
+    tab.linkedBrowser.removeEventListener(aEventType, load, true);
+    let iframe = tab.linkedBrowser.contentDocument.getElementById("remote-report");
+      iframe.addEventListener("load", function frameLoad(e) {
+        iframe.removeEventListener("load", frameLoad, false);
+        deferred.resolve();
+      }, false);
+    }, true);
+  return deferred.promise;
+}
+
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/healthreport_testPostMessage.html
@@ -0,0 +1,24 @@
+<html>
+  <head>
+    <meta charset="utf-8">
+
+<script>
+
+function init() {
+  window.addEventListener("message", receiveMessage, false);
+}
+
+function receiveMessage(event) {
+  acknowledgeMessage(event.data.type);
+}
+
+function acknowledgeMessage(type) {
+  var data = {type: type};
+  window.parent.postMessage(data, "*");
+}
+
+</script>
+  </head>
+  <body onload="init()">
+  </body>
+</html>
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/healthreport_testRemoteCommands.html
@@ -0,0 +1,36 @@
+<html>
+  <head>
+    <meta charset="utf-8">
+
+<script>
+
+function init() {
+  window.addEventListener("message", function process(e) {runNextTest(e)}, false);
+}
+
+var i = 0;
+function runNextTest(e) {
+  if (e.data.type != "prefs" && e.data.type != "payload")
+    return;
+
+  var tests = ["DisableDataSubmission","RequestCurrentPrefs","RequestCurrentPayload","EnableDataSubmission"];
+  sendToBrowser(tests[i]);
+  dispatchMessage(tests[i]);
+  i++;
+}
+
+function dispatchMessage(cmd) {
+  var data = {command: cmd};
+  window.parent.postMessage(data, "*");
+}
+
+function sendToBrowser(type) {
+  var event = new CustomEvent("RemoteHealthReportCommand", {detail: {command: type}, bubbles: true});
+  document.dispatchEvent(event);
+}
+
+</script>
+  </head>
+  <body onload="init()">
+  </body>
+</html>