Bug 1361080 - add unit test for aboutdevtools page;r=ochameau draft
authorJulian Descottes <jdescottes@mozilla.com>
Tue, 03 Oct 2017 23:34:44 +0200
changeset 678541 e806c2cf1301eec1510f6d5476941ba100affd04
parent 678540 9166f477e2d38351e836aa14c53a420bd9cbddf7
child 678545 ddee82467e7d2b6b171e4cc19b40fbf425f95cc8
child 678547 e2c9e26b27b1bf8788739f8d6384c19473ffdd23
push id83952
push userjdescottes@mozilla.com
push dateWed, 11 Oct 2017 14:45:10 +0000
reviewersochameau
bugs1361080
milestone58.0a1
Bug 1361080 - add unit test for aboutdevtools page;r=ochameau MozReview-Commit-ID: CZV1MIj80Zf
devtools/shim/aboutdevtools/moz.build
devtools/shim/aboutdevtools/test/.eslintrc.js
devtools/shim/aboutdevtools/test/browser.ini
devtools/shim/aboutdevtools/test/browser_aboutdevtools_enables_devtools.js
devtools/shim/aboutdevtools/test/head.js
--- a/devtools/shim/aboutdevtools/moz.build
+++ b/devtools/shim/aboutdevtools/moz.build
@@ -3,8 +3,10 @@
 # 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/.
 
 EXTRA_COMPONENTS += [
   'aboutdevtools-registration.js',
   'aboutdevtools.manifest',
 ]
+
+BROWSER_CHROME_MANIFESTS += ['test/browser.ini']
new file mode 100644
--- /dev/null
+++ b/devtools/shim/aboutdevtools/test/.eslintrc.js
@@ -0,0 +1,6 @@
+"use strict";
+
+module.exports = {
+  // Extend from the shared list of defined globals for mochitests.
+  "extends": "../../../.eslintrc.mochitests.js"
+};
new file mode 100644
--- /dev/null
+++ b/devtools/shim/aboutdevtools/test/browser.ini
@@ -0,0 +1,7 @@
+[DEFAULT]
+tags = devtools
+subsuite = devtools
+support-files =
+  head.js
+
+[browser_aboutdevtools_enables_devtools.js]
new file mode 100644
--- /dev/null
+++ b/devtools/shim/aboutdevtools/test/browser_aboutdevtools_enables_devtools.js
@@ -0,0 +1,36 @@
+/* vim: set ts=2 et sw=2 tw=80: */
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+/* eslint-env browser */
+
+add_task(async function () {
+  pushPref("devtools.enabled", false);
+
+  let {tab, doc, win} = await openAboutDevTools();
+
+  let installPage = doc.getElementById("install-page");
+  let welcomePage = doc.getElementById("welcome-page");
+
+  info("Check that about:devtools is in the correct state with devtools.enabled=false");
+  ok(!installPage.hasAttribute("hidden"), "install screen is visible");
+  ok(welcomePage.hasAttribute("hidden"), "welcome screen is hidden");
+
+  info("Click on the install button to enable DevTools.");
+  let installButton = doc.getElementById("install");
+  EventUtils.synthesizeMouseAtCenter(installButton, {}, win);
+
+  info("Wait until the UI updates");
+  await waitUntil(() => installPage.hasAttribute("hidden") === true);
+  ok(!welcomePage.hasAttribute("hidden"), "welcome screen is visible");
+  ok(Services.prefs.getBoolPref("devtools.enabled"),
+    "The preference devtools.enabled has been flipped to true.");
+
+  // Flip the devtools.enabled preference back to false, otherwise the pushPref cleanup
+  // times out.
+  Services.prefs.setBoolPref("devtools.enabled", false);
+
+  await removeTab(tab);
+});
new file mode 100644
--- /dev/null
+++ b/devtools/shim/aboutdevtools/test/head.js
@@ -0,0 +1,92 @@
+/* vim: set ts=2 et sw=2 tw=80: */
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+/* eslint no-unused-vars: [2, {"vars": "local"}] */
+
+"use strict";
+
+const { utils: Cu } = Components;
+const { Services } = Cu.import("resource://gre/modules/Services.jsm", {});
+
+// All test are asynchronous
+waitForExplicitFinish();
+
+/**
+ * Waits until a predicate returns true.
+ *
+ * @param function predicate
+ *        Invoked once in a while until it returns true.
+ * @param number interval [optional]
+ *        How often the predicate is invoked, in milliseconds.
+ */
+const waitUntil = function (predicate, interval = 100) {
+  if (predicate()) {
+    return Promise.resolve(true);
+  }
+  return new Promise(resolve => {
+    setTimeout(function () {
+      waitUntil(predicate, interval).then(() => resolve(true));
+    }, interval);
+  });
+};
+
+/**
+ * Open the provided url in a new tab.
+ */
+const addTab = async function (url) {
+  info("Adding a new tab with URL: " + url);
+
+  let { gBrowser } = window;
+
+  let tab = BrowserTestUtils.addTab(gBrowser, url);
+  gBrowser.selectedTab = tab;
+
+  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
+
+  info("Tab added and finished loading");
+
+  return tab;
+};
+
+/**
+ * Remove the given tab.
+ * @param {Object} tab The tab to be removed.
+ * @return Promise<undefined> resolved when the tab is successfully removed.
+ */
+const removeTab = async function (tab) {
+  info("Removing tab.");
+
+  let { gBrowser } = tab.ownerGlobal;
+
+  await new Promise(resolve => {
+    gBrowser.tabContainer.addEventListener("TabClose", resolve, {once: true});
+    gBrowser.removeTab(tab);
+  });
+
+  info("Tab removed and finished closing");
+};
+
+/**
+ * Open a new tab on about:devtools
+ */
+const openAboutDevTools = async function () {
+  info("Open about:devtools programmatically in a new tab");
+  let tab = await addTab("about:devtools");
+
+  let browser = tab.linkedBrowser;
+  let doc = browser.contentDocument;
+  let win = browser.contentWindow;
+
+  return {tab, doc, win};
+};
+
+/**
+ * Copied from devtools shared-head.js.
+ * Set a temporary value for a preference, that will be cleaned up after the test.
+ */
+const pushPref = function (preferenceName, value) {
+  return new Promise(resolve => {
+    let options = {"set": [[preferenceName, value]]};
+    SpecialPowers.pushPrefEnv(options, resolve);
+  });
+};