Bug 1111022 - Tests for the SelfSupport backend. r=gfritzsche, a=lmandel
authorAlessio Placitelli <alessio.placitelli@gmail.com>
Fri, 09 Jan 2015 11:18:00 +0100
changeset 250114 e7a0f9cd3482
parent 250113 41fd4a4df8ac
child 250115 f8c1586998ef
push id4503
push userryanvm@gmail.com
push date2015-02-27 21:34 +0000
treeherdermozilla-beta@87d76aead804 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgfritzsche, lmandel
bugs1111022
milestone37.0
Bug 1111022 - Tests for the SelfSupport backend. r=gfritzsche, a=lmandel
browser/modules/test/browser.ini
browser/modules/test/browser_SelfSupportBackend.js
layout/tools/reftest/reftest-preferences.js
testing/mochitest/browser-test.js
testing/profiles/prefs_general.js
testing/xpcshell/head.js
--- a/browser/modules/test/browser.ini
+++ b/browser/modules/test/browser.ini
@@ -7,13 +7,17 @@ support-files =
 skip-if = e10s
 support-files =
   contentSearch.js
   contentSearchBadImage.xml
   contentSearchSuggestions.sjs
   contentSearchSuggestions.xml
 [browser_NetworkPrioritizer.js]
 skip-if = e10s # Bug 666804 - Support NetworkPrioritizer in e10s
+[browser_SelfSupportBackend.js]
+support-files =
+  ../../components/uitour/test/uitour.html
+  ../../components/uitour/UITour-lib.js
 [browser_SignInToWebsite.js]
 skip-if = e10s # Bug 941426 - SignIntoWebsite.jsm not e10s friendly
 [browser_taskbar_preview.js]
 run-if = os == "win"
 skip-if = e10s # Bug 666808 - AeroPeek support for e10s
new file mode 100644
--- /dev/null
+++ b/browser/modules/test/browser_SelfSupportBackend.js
@@ -0,0 +1,158 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+// Pass an empty scope object to the import to prevent "leaked window property"
+// errors in tests.
+let Preferences = Cu.import("resource://gre/modules/Preferences.jsm", {}).Preferences;
+let PromiseUtils = Cu.import("resource://gre/modules/PromiseUtils.jsm", {}).PromiseUtils;
+let SelfSupportBackend =
+  Cu.import("resource:///modules/SelfSupportBackend.jsm", {}).SelfSupportBackend;
+
+const PREF_SELFSUPPORT_ENABLED = "browser.selfsupport.enabled";
+const PREF_SELFSUPPORT_URL = "browser.selfsupport.url";
+const PREF_UITOUR_ENABLED = "browser.uitour.enabled";
+
+const TEST_WAIT_RETRIES = 60;
+
+const TEST_PAGE_URL = getRootDirectory(gTestPath) + "uitour.html";
+
+/**
+ * Find a browser, with an IFRAME as parent, who has aURL as the source attribute.
+ *
+ * @param aURL The URL to look for to identify the browser.
+ *
+ * @returns {Object} The browser element or null on failure.
+ */
+function findSelfSupportBrowser(aURL) {
+  let frames = Services.appShell.hiddenDOMWindow.document.querySelectorAll('iframe');
+  for (let frame of frames) {
+    try {
+      let browser = frame.contentDocument.getElementById("win").querySelectorAll('browser')[0];
+      let url = browser.getAttribute("src");
+      if (url == aURL) {
+        return browser;
+      }
+    } catch (e) {
+      continue;
+    }
+  }
+  return null;
+}
+
+/**
+ * Wait for self support page to load.
+ *
+ * @param aURL The URL to look for to identify the browser.
+ *
+ * @returns {Promise} Return a promise which is resolved when SelfSupport page is fully
+ *          loaded.
+ */
+function promiseSelfSupportLoad(aURL) {
+  return new Promise((resolve, reject) => {
+    // Find the SelfSupport browser.
+    let browserPromise = waitForConditionPromise(() => !!findSelfSupportBrowser(aURL),
+                                                 "SelfSupport browser not found.",
+                                                 TEST_WAIT_RETRIES);
+
+    // Once found, append a "load" listener to catch page loads.
+    browserPromise.then(() => {
+      let browser = findSelfSupportBrowser(aURL);
+      if (browser.contentDocument.readyState === "complete") {
+        resolve(browser);
+      } else {
+        let handler = () => {
+          browser.removeEventListener("load", handler, true);
+          resolve(browser);
+        };
+        browser.addEventListener("load", handler, true);
+      }
+    }, reject);
+  });
+}
+
+/**
+ * Wait for self support to close.
+ *
+ * @param aURL The URL to look for to identify the browser.
+ *
+ * @returns {Promise} Return a promise which is resolved when SelfSupport browser cannot
+ *          be found anymore.
+ */
+function promiseSelfSupportClose(aURL) {
+  return waitForConditionPromise(() => !findSelfSupportBrowser(aURL),
+                                 "SelfSupport browser is still open.", TEST_WAIT_RETRIES);
+}
+
+/**
+ * Prepare the test environment.
+ */
+add_task(function* setupEnvironment() {
+  // We always run the SelfSupportBackend in tests to check for weird behaviours.
+  // Disable it to test its start-up.
+  SelfSupportBackend.uninit();
+
+  // Testing prefs are set via |user_pref|, so we need to get their value in order
+  // to restore them.
+  let selfSupportEnabled = Preferences.get(PREF_SELFSUPPORT_ENABLED, true);
+  let uitourEnabled = Preferences.get(PREF_UITOUR_ENABLED, false);
+  let selfSupportURL = Preferences.get(PREF_SELFSUPPORT_URL, "");
+
+  // Enable the SelfSupport backend and set the page URL. We also make sure UITour
+  // is enabled.
+  Preferences.set(PREF_SELFSUPPORT_ENABLED, true);
+  Preferences.set(PREF_UITOUR_ENABLED, true);
+  Preferences.set(PREF_SELFSUPPORT_URL, TEST_PAGE_URL);
+
+  registerCleanupFunction(() => {
+    Preferences.set(PREF_SELFSUPPORT_ENABLED, selfSupportEnabled);
+    Preferences.set(PREF_UITOUR_ENABLED, uitourEnabled);
+    Preferences.set(PREF_SELFSUPPORT_URL, selfSupportURL);
+  });
+});
+
+/**
+ * Test that the self support page can use the UITour API and close itself.
+ */
+add_task(function* test_selfSupport() {
+  // Initialise the SelfSupport backend and trigger the load.
+  SelfSupportBackend.init();
+
+  // SelfSupportBackend waits for "sessionstore-windows-restored" to start loading. Send it.
+  info("Sending sessionstore-windows-restored");
+  Services.obs.notifyObservers(null, "sessionstore-windows-restored", null);
+
+  // Wait for the SelfSupport page to load.
+  info("Waiting for the SelfSupport local page to load.");
+  let selfSupportBrowser = yield promiseSelfSupportLoad(TEST_PAGE_URL);
+  Assert.ok(!!selfSupportBrowser, "SelfSupport browser must exist.");
+
+  // Get a reference to the UITour API.
+  info("Testing access to the UITour API.");
+  let contentWindow =
+    Cu.waiveXrays(selfSupportBrowser.contentDocument.defaultView);
+  let uitourAPI = contentWindow.Mozilla.UITour;
+
+  // Test the UITour API with a ping.
+  let pingPromise = new Promise((resolve) => {
+    uitourAPI.ping(resolve);
+  });
+  yield pingPromise;
+
+  // Close SelfSupport from content.
+  contentWindow.close();
+
+  // Wait until SelfSupport closes.
+  info("Waiting for the SelfSupport to close.");
+  yield promiseSelfSupportClose(TEST_PAGE_URL);
+
+  // Find the SelfSupport browser, again. We don't expect to find it.
+  selfSupportBrowser = findSelfSupportBrowser(TEST_PAGE_URL);
+  Assert.ok(!selfSupportBrowser, "SelfSupport browser must not exist.");
+
+  // We shouldn't need this, but let's keep it to make sure closing SelfSupport twice
+  // doesn't create any problem.
+  SelfSupportBackend.uninit();
+});
--- a/layout/tools/reftest/reftest-preferences.js
+++ b/layout/tools/reftest/reftest-preferences.js
@@ -53,8 +53,11 @@
     // Disable the auto-hide feature of touch caret to avoid potential
     // intermittent issues.
     branch.setIntPref("touchcaret.expiration.time", 0);
 
     // Tell the search service we are running in the US.  This also has the
     // desired side-effect of preventing our geoip lookup.
     branch.setBoolPref("browser.search.isUS", true);
     branch.setCharPref("browser.search.countryCode", "US");
+
+    // Make sure SelfSupport doesn't hit the network.
+    branch.setCharPref("browser.selfsupport.url", "https://%(server)s/selfsupport-dummy/");
--- a/testing/mochitest/browser-test.js
+++ b/testing/mochitest/browser-test.js
@@ -20,16 +20,19 @@ XPCOMUtils.defineLazyModuleGetter(this, 
   "resource://gre/modules/Services.jsm");
 
 XPCOMUtils.defineLazyModuleGetter(this, "CustomizationTabPreloader",
   "resource:///modules/CustomizationTabPreloader.jsm");
 
 XPCOMUtils.defineLazyModuleGetter(this, "ContentSearch",
   "resource:///modules/ContentSearch.jsm");
 
+XPCOMUtils.defineLazyModuleGetter(this, "SelfSupportBackend",
+  "resource:///modules/SelfSupportBackend.jsm");
+
 const SIMPLETEST_OVERRIDES =
   ["ok", "is", "isnot", "ise", "todo", "todo_is", "todo_isnot", "info", "expectAssertions", "requestCompleteLog"];
 
 window.addEventListener("load", function testOnLoad() {
   window.removeEventListener("load", testOnLoad);
   window.addEventListener("MozAfterPaint", function testOnMozAfterPaint() {
     window.removeEventListener("MozAfterPaint", testOnMozAfterPaint);
     setTimeout(testInit, 0);
@@ -516,16 +519,17 @@ Tester.prototype = {
 
           // Destroy preloaded browsers.
           if (gBrowser._preloadedBrowser) {
             let browser = gBrowser._preloadedBrowser;
             gBrowser._preloadedBrowser = null;
             gBrowser.getNotificationBox(browser).remove();
           }
 
+          SelfSupportBackend.uninit();
           CustomizationTabPreloader.uninit();
           SocialFlyout.unload();
           SocialShare.uninit();
           TabView.uninit();
         }
 
         // Schedule GC and CC runs before finishing in order to detect
         // DOM windows leaked by our tests or the tested code. Note that we
--- a/testing/profiles/prefs_general.js
+++ b/testing/profiles/prefs_general.js
@@ -274,16 +274,19 @@ user_pref("browser.uitour.url", "http://
 
 // Don't show the search first run UI by default
 user_pref("browser.search.highlightCount", 0);
 // Tell the search service we are running in the US.  This also has the desired
 // side-effect of preventing our geoip lookup.
 user_pref("browser.search.isUS", true);
 user_pref("browser.search.countryCode", "US");
 
+// Make sure the self support tab doesn't hit the network.
+user_pref("browser.selfsupport.url", "https://%(server)s/selfsupport-dummy/");
+
 user_pref("media.eme.enabled", true);
 
 #if defined(XP_WIN)
 user_pref("media.decoder.heuristic.dormant.timeout", 0);
 #endif
 
 // Don't prompt about e10s
 user_pref("browser.displayedE10SPrompt.1", 5);
--- a/testing/xpcshell/head.js
+++ b/testing/xpcshell/head.js
@@ -1408,10 +1408,11 @@ try {
 
 // We need to avoid hitting the network with certain components.
 try {
   if (runningInParent) {
     let prefs = Components.classes["@mozilla.org/preferences-service;1"]
       .getService(Components.interfaces.nsIPrefBranch);
 
     prefs.setCharPref("media.gmp-manager.url.override", "http://%(server)s/dummy-gmp-manager.xml");
+    prefs.setCharPref("browser.selfsupport.url", "https://%(server)s/selfsupport-dummy/");
   }
 } catch (e) { }