Bug 1033362 Implement basic mochitests for MozLoopAPI. r=dolske,r=glandium
authorMark Banner <standard8@mozilla.com>
Tue, 15 Jul 2014 10:03:03 +0100
changeset 194150 3cbe70a2249ca755777b9b902f035d9457fd378a
parent 194149 f1a8a13674dd46a68eaf8ff8463b76ddfaba128a
child 194151 5075aec17a1ec6c3dbbdebdcfe690cc29b5d5f35
push id27139
push userkwierso@gmail.com
push dateTue, 15 Jul 2014 23:58:05 +0000
treeherdermozilla-central@95f2ad2787b3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdolske, glandium
bugs1033362
milestone33.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 1033362 Implement basic mochitests for MozLoopAPI. r=dolske,r=glandium
browser/components/loop/moz.build
browser/components/loop/test/mochitest/browser.ini
browser/components/loop/test/mochitest/browser_mozLoop_charPref.js
browser/components/loop/test/mochitest/browser_mozLoop_doNotDisturb.js
browser/components/loop/test/mochitest/head.js
--- a/browser/components/loop/moz.build
+++ b/browser/components/loop/moz.build
@@ -5,14 +5,18 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 JAR_MANIFESTS += ['jar.mn']
 
 JS_MODULES_PATH = 'modules/loop'
 
 XPCSHELL_TESTS_MANIFESTS += ['test/xpcshell/xpcshell.ini']
 
+BROWSER_CHROME_MANIFESTS += [
+    'test/mochitest/browser.ini',
+]
+
 EXTRA_JS_MODULES += [
     'MozLoopAPI.jsm',
     'MozLoopPushHandler.jsm',
     'MozLoopService.jsm',
     'MozLoopWorker.js',
 ]
new file mode 100644
--- /dev/null
+++ b/browser/components/loop/test/mochitest/browser.ini
@@ -0,0 +1,6 @@
+[DEFAULT]
+support-files =
+    head.js
+
+[browser_mozLoop_charPref.js]
+[browser_mozLoop_doNotDisturb.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/loop/test/mochitest/browser_mozLoop_charPref.js
@@ -0,0 +1,26 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/**
+ * This is an integration test from navigator.mozLoop through to the end
+ * effects - rather than just testing MozLoopAPI alone.
+ */
+
+add_task(loadLoopPanel);
+
+add_task(function* test_mozLoop_charPref() {
+  registerCleanupFunction(function () {
+    Services.prefs.clearUserPref("loop.test");
+  });
+
+  Assert.ok(gMozLoopAPI, "mozLoop should exist");
+
+  // Test setLoopCharPref
+  gMozLoopAPI.setLoopCharPref("test", "foo");
+  Assert.equal(Services.prefs.getCharPref("loop.test"), "foo",
+               "should set loop pref value correctly");
+
+  // Test getLoopCharPref
+  Assert.equal(gMozLoopAPI.getLoopCharPref("test"), "foo",
+               "should get loop pref value correctly");
+});
new file mode 100644
--- /dev/null
+++ b/browser/components/loop/test/mochitest/browser_mozLoop_doNotDisturb.js
@@ -0,0 +1,27 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/**
+ * This is an integration test from navigator.mozLoop through to the end
+ * effects - rather than just testing MozLoopAPI alone.
+ */
+
+add_task(loadLoopPanel);
+
+add_task(function* test_mozLoop_doNotDisturb() {
+  registerCleanupFunction(function () {
+    Services.prefs.clearUserPref("loop.do_not_disturb");
+  });
+
+  Assert.ok(gMozLoopAPI, "mozLoop should exist");
+
+  // Test doNotDisturb (getter)
+  Services.prefs.setBoolPref("loop.do_not_disturb", true);
+  Assert.equal(gMozLoopAPI.doNotDisturb, true,
+               "Do not disturb should be true");
+
+  // Test doNotDisturb (setter)
+  gMozLoopAPI.doNotDisturb = false;
+  Assert.equal(Services.prefs.getBoolPref("loop.do_not_disturb"), false,
+               "Do not disturb should be false");
+});
new file mode 100644
--- /dev/null
+++ b/browser/components/loop/test/mochitest/head.js
@@ -0,0 +1,71 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+var gMozLoopAPI;
+
+function promiseGetMozLoopAPI() {
+  let deferred = Promise.defer();
+  let loopPanel = document.getElementById("loop-notification-panel");
+  let btn = document.getElementById("loop-call-button");
+
+  // Wait for the popup to be shown, then we can get the iframe and
+  // wait for the iframe's load to be completed.
+  loopPanel.addEventListener("popupshown", function onpopupshown() {
+    loopPanel.removeEventListener("popupshown", onpopupshown, true);
+    let iframe = document.getElementById(btn.getAttribute("notificationFrameId"));
+
+    if (iframe.contentDocument &&
+        iframe.contentDocument.readyState == "complete") {
+      gMozLoopAPI = iframe.contentWindow.navigator.wrappedJSObject.mozLoop;
+
+      deferred.resolve();
+    } else {
+      iframe.addEventListener("load", function panelOnLoad(e) {
+        iframe.removeEventListener("load", panelOnLoad, true);
+
+        gMozLoopAPI = iframe.contentWindow.navigator.wrappedJSObject.mozLoop;
+
+        // We do this in an execute soon to allow any other event listeners to
+        // be handled, just in case.
+        deferred.resolve();
+      }, true);
+    }
+  }, true);
+
+  // Now we're setup, click the button.
+  btn.click();
+
+  // Remove the iframe after each test. This also avoids mochitest complaining
+  // about leaks on shutdown as we intentionally hold the iframe open for the
+  // life of the application.
+  registerCleanupFunction(function() {
+    loopPanel.hidePopup();
+    loopPanel.removeChild(document.getElementById(btn.getAttribute("notificationFrameId")));
+  });
+
+  return deferred.promise;
+}
+
+/**
+ * Loads the loop panel by clicking the button and waits for its open to complete.
+ * It also registers
+ *
+ * This assumes that the tests are running in a generatorTest.
+ */
+function loadLoopPanel() {
+  // Set prefs to ensure we don't access the network externally.
+  Services.prefs.setCharPref("services.push.serverURL", "ws://localhost/");
+  Services.prefs.setCharPref("loop.server", "http://localhost/");
+
+  registerCleanupFunction(function() {
+    Services.prefs.clearUserPref("services.push.serverURL");
+    Services.prefs.clearUserPref("loop.server");
+  });
+
+  // Turn off animations to make tests quicker.
+  let loopPanel = document.getElementById("loop-notification-panel");
+  loopPanel.setAttribute("animate", "false");
+
+  // Now get the actual API.
+  yield promiseGetMozLoopAPI();
+}