Bug 1516957 - Part 2 - Create a base test class and unify tab creation functions. r=bgrins
authorPaolo Amadini <paolo.mozmail@amadzone.org>
Mon, 31 Dec 2018 11:42:00 +0000
changeset 509340 214d0e9f78255628d8f302e543fe4a4740ec33f3
parent 509339 c762e770dec4e6e968809dd22bcc59f27d1a4a1b
child 509341 dda920bc62ddcb484abc9622e1c47f3fb0f47b0e
push id10547
push userffxbld-merge
push dateMon, 21 Jan 2019 13:03:58 +0000
treeherdermozilla-beta@24ec1916bffe [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbgrins
bugs1516957
milestone66.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 1516957 - Part 2 - Create a base test class and unify tab creation functions. r=bgrins Differential Revision: https://phabricator.services.mozilla.com/D15514
browser/components/aboutconfig/test/browser/browser.ini
browser/components/aboutconfig/test/browser/browser_basic.js
browser/components/aboutconfig/test/browser/browser_edit.js
browser/components/aboutconfig/test/browser/browser_locked.js
browser/components/aboutconfig/test/browser/browser_search.js
browser/components/aboutconfig/test/browser/browser_warning.js
browser/components/aboutconfig/test/browser/head.js
--- a/browser/components/aboutconfig/test/browser/browser.ini
+++ b/browser/components/aboutconfig/test/browser/browser.ini
@@ -1,9 +1,11 @@
 [DEFAULT]
+support-files =
+  head.js
 
 [browser_basic.js]
 [browser_warning.js]
 [browser_edit.js]
 skip-if = debug # Bug 1507747
 [browser_search.js]
 skip-if = debug # Bug 1507747
 [browser_locked.js]
--- a/browser/components/aboutconfig/test/browser/browser_basic.js
+++ b/browser/components/aboutconfig/test/browser/browser_basic.js
@@ -1,40 +1,28 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
-const PAGE_URL = "chrome://browser/content/aboutconfig/aboutconfig.html";
-
 add_task(async function setup() {
   await SpecialPowers.pushPrefEnv({
     set: [
       ["random.user.pref", "chrome://test/locale/testing.properties"],
     ],
   });
 });
 
 add_task(async function test_load_title() {
-  await BrowserTestUtils.withNewTab({
-    gBrowser,
-    url: PAGE_URL,
-  }, async browser => {
-    info("about:config loaded");
-    await content.document.l10n.ready;
-    Assert.equal(content.document.title, "about:config");
+  await AboutConfigTest.withNewTab(async function() {
+    Assert.equal(this.document.title, "about:config");
   });
 });
 
 add_task(async function test_load_settings() {
-  await BrowserTestUtils.withNewTab({
-    gBrowser,
-    url: PAGE_URL,
-  }, async browser => {
-    content.document.querySelector("button").click();
-
-    let list = [...content.document.getElementById("prefs")
+  await AboutConfigTest.withNewTab(async function() {
+    let list = [...this.document.getElementById("prefs")
       .getElementsByTagName("tr")];
     function getRow(name) {
       return list.find(row => row.querySelector("td").textContent == name);
     }
     function getValue(name) {
       return getRow(name).querySelector("td.cell-value").textContent;
     }
 
--- a/browser/components/aboutconfig/test/browser/browser_edit.js
+++ b/browser/components/aboutconfig/test/browser/browser_edit.js
@@ -1,15 +1,11 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
-ChromeUtils.import("resource://gre/modules/Preferences.jsm", this);
-
-const PAGE_URL = "chrome://browser/content/aboutconfig/aboutconfig.html";
-
 add_task(async function setup() {
   await SpecialPowers.pushPrefEnv({
     set: [
       ["test.aboutconfig.modify.boolean", true],
       ["test.aboutconfig.modify.number", 1337],
       ["test.aboutconfig.modify.string", "the answer to the life the universe and everything"],
     ],
   });
@@ -17,137 +13,117 @@ add_task(async function setup() {
   registerCleanupFunction(() => {
     Services.prefs.clearUserPref("accessibility.typeaheadfind.autostart");
     Services.prefs.clearUserPref("accessibility.typeaheadfind.soundURL");
     Services.prefs.clearUserPref("accessibility.typeaheadfind.casesensitive");
   });
 });
 
 add_task(async function test_add_user_pref() {
-  await BrowserTestUtils.withNewTab({
-    gBrowser,
-    url: PAGE_URL,
-  }, async browser => {
-    await content.document.querySelector("button").click();
-
+  await AboutConfigTest.withNewTab(async function() {
     Assert.ok(!Services.prefs.getChildList("").find(pref => pref == "testPref"));
-    let search = content.document.getElementById("search");
+    let search = this.document.getElementById("search");
     search.value = "testPref";
     search.focus();
 
     for (let [buttonSelector, expectedValue] of [
       [".add-true", true],
       [".add-false", false],
       [".add-Number", 0],
       [".add-String", ""],
     ]) {
       EventUtils.sendKey("return");
 
-      content.document.querySelector("#prefs button" + buttonSelector).click();
+      this.document.querySelector("#prefs button" + buttonSelector).click();
       Assert.ok(Services.prefs.getChildList("").find(pref => pref == "testPref"));
       Assert.ok(Preferences.get("testPref") === expectedValue);
-      content.document.querySelector("#prefs button[data-l10n-id='about-config-pref-delete']").click();
-      search = content.document.getElementById("search");
+      this.document.querySelector("#prefs button[data-l10n-id='about-config-pref-delete']").click();
+      search = this.document.getElementById("search");
       search.value = "testPref";
       search.focus();
     }
   });
 });
 
 add_task(async function test_delete_user_pref() {
   Services.prefs.setBoolPref("userAddedPref", true);
-  await BrowserTestUtils.withNewTab({
-    gBrowser,
-    url: PAGE_URL,
-  }, async browser => {
-    await content.document.querySelector("button").click();
-
-    let list = [...content.document.getElementById("prefs")
+  await AboutConfigTest.withNewTab(async function() {
+    let list = [...this.document.getElementById("prefs")
       .getElementsByTagName("tr")];
     function getRow(name) {
       return list.find(row => row.querySelector("td").textContent == name);
     }
     Assert.ok(getRow("userAddedPref"));
     getRow("userAddedPref").lastChild.lastChild.click();
-    list = [...content.document.getElementById("prefs")
+    list = [...this.document.getElementById("prefs")
       .getElementsByTagName("tr")];
     Assert.ok(!getRow("userAddedPref"));
     Assert.ok(!Services.prefs.getChildList("").includes("userAddedPref"));
 
     // Search for nothing to test gPrefArray
-    let search = content.document.getElementById("search");
+    let search = this.document.getElementById("search");
     search.focus();
     EventUtils.sendKey("return");
 
-    list = [...content.document.getElementById("prefs")
+    list = [...this.document.getElementById("prefs")
       .getElementsByTagName("tr")];
     Assert.ok(!getRow("userAddedPref"));
   });
 });
 
 add_task(async function test_reset_user_pref() {
   await SpecialPowers.pushPrefEnv({"set": [["browser.autofocus", false]]});
 
-  await BrowserTestUtils.withNewTab({
-    gBrowser,
-    url: PAGE_URL,
-  }, async browser => {
-    await content.document.querySelector("button").click();
-
+  await AboutConfigTest.withNewTab(async function() {
     function getRow(name) {
       return list.find(row => row.querySelector("td").textContent == name);
     }
     function getValue(name) {
       return getRow(name).querySelector("td.cell-value").textContent;
     }
     let testPref = "browser.autofocus";
     // Click reset.
-    let list = [...content.document.getElementById("prefs")
+    let list = [...this.document.getElementById("prefs")
       .getElementsByTagName("tr")];
     let row = getRow(testPref);
     row.lastChild.lastChild.click();
     // Check new layout and reset.
-    list = [...content.document.getElementById("prefs")
+    list = [...this.document.getElementById("prefs")
       .getElementsByTagName("tr")];
     Assert.ok(!row.classList.contains("has-user-value"));
     Assert.equal(row.lastChild.childNodes.length, 0);
     Assert.ok(!Services.prefs.prefHasUserValue(testPref));
     Assert.equal(getValue(testPref), "" + Preferences.get(testPref));
 
     // Search for nothing to test gPrefArray
-    let search = content.document.getElementById("search");
+    let search = this.document.getElementById("search");
     search.focus();
     EventUtils.sendKey("return");
 
     // Check new layout and reset.
-    list = [...content.document.getElementById("prefs")
+    list = [...this.document.getElementById("prefs")
       .getElementsByTagName("tr")];
     row = getRow(testPref);
     Assert.ok(!row.classList.contains("has-user-value"));
     Assert.equal(row.lastChild.childNodes.length, 0);
     Assert.equal(getValue(testPref), "" + Preferences.get(testPref));
   });
 });
 
 add_task(async function test_modify() {
-  await BrowserTestUtils.withNewTab({
-    gBrowser,
-    url: PAGE_URL,
-  }, async browser => {
-    content.document.querySelector("button").click();
-
+  await AboutConfigTest.withNewTab(async function() {
     function getRow(name, list) {
       return list.find(row => row.querySelector("td").textContent == name);
     }
     function getValue(name, list) {
       return getRow(name, list).querySelector("td.cell-value").textContent;
     }
 
     // Test toggle for boolean prefs.
-    let list = [...content.document.getElementById("prefs")
+    let list = [...this.document.getElementById("prefs")
       .getElementsByTagName("tr")];
     for (let nameOfBoolPref of [
       "test.aboutconfig.modify.boolean",
       "accessibility.typeaheadfind.autostart",
     ]) {
       let row = getRow(nameOfBoolPref, list);
       // Do this a two times to reset the pref.
       for (let i = 0; i < 2; i++) {
--- a/browser/components/aboutconfig/test/browser/browser_locked.js
+++ b/browser/components/aboutconfig/test/browser/browser_locked.js
@@ -1,13 +1,11 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
-const PAGE_URL = "chrome://browser/content/aboutconfig/aboutconfig.html";
-
 add_task(async function setup() {
   await SpecialPowers.pushPrefEnv({
     set: [
       ["test.aboutconfig.a", "some value"],
     ],
   });
 });
 
@@ -16,23 +14,18 @@ add_task(async function test_locked() {
     Services.prefs.unlockPref("browser.search.searchEnginesURL");
     Services.prefs.unlockPref("test.aboutconfig.a");
     Services.prefs.unlockPref("accessibility.AOM.enabled");
   });
 
   Services.prefs.lockPref("browser.search.searchEnginesURL");
   Services.prefs.lockPref("test.aboutconfig.a");
   Services.prefs.lockPref("accessibility.AOM.enabled");
-  await BrowserTestUtils.withNewTab({
-    gBrowser,
-    url: PAGE_URL,
-  }, async browser => {
-    await content.document.querySelector("button").click();
-
-    let list = [...content.document.getElementById("prefs")
+  await AboutConfigTest.withNewTab(async function() {
+    let list = [...this.document.getElementById("prefs")
       .getElementsByTagName("tr")];
     function getRow(name) {
       return list.find(row => row.querySelector("td").textContent == name);
     }
     function getValue(name) {
         return getRow(name).querySelector("td.cell-value").textContent;
     }
     function getButton(name) {
--- a/browser/components/aboutconfig/test/browser/browser_search.js
+++ b/browser/components/aboutconfig/test/browser/browser_search.js
@@ -1,80 +1,73 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
-ChromeUtils.import("resource://gre/modules/Preferences.jsm", this);
-
-const PAGE_URL = "chrome://browser/content/aboutconfig/aboutconfig.html";
-
 add_task(async function setup() {
   await SpecialPowers.pushPrefEnv({
     set: [
       ["test.aboutconfig.a", "test value 1"],
       ["test.aboutconfig.ab", "test value 2"],
       ["test.aboutconfig.b", "test value 3"],
     ],
   });
 });
 
 add_task(async function test_search() {
-  await BrowserTestUtils.withNewTab({
-    gBrowser,
-    url: PAGE_URL,
-  }, async browser => {
+  await AboutConfigTest.withNewTab(async function() {
     let prefArray = Services.prefs.getChildList("");
-    await content.document.querySelector("button").click();
+    await this.document.querySelector("button").click();
 
     // The total number of preferences may change at any time because of
     // operations running in the background, so we only test approximately.
     // The change in count would be because of one or two added preferences,
     // but we tolerate a difference of up to 50 preferences just to be safe.
     // We want thousands of prefs instead of a few dozen that are filtered.
-    Assert.greater(content.document.getElementById("prefs").childElementCount,
+    Assert.greater(this.document.getElementById("prefs").childElementCount,
                    prefArray.length - 50);
 
     // Filter a subset of preferences. The "browser.download." branch is
     // chosen because it is very unlikely that its preferences would be
     // modified by other code during the execution of this test.
-    let search = content.document.getElementById("search");
+    let search = this.document.getElementById("search");
     search.value = "wser.down   ";
     search.focus();
     EventUtils.sendKey("return");
 
     let filteredPrefArray =
         prefArray.filter(pref => pref.includes("wser.down"));
     // Adding +1 to the list since button does not match an exact
     // preference name then a row is added for the user to add a
     // new button preference if desired
-    Assert.equal(content.document.getElementById("prefs").childElementCount,
+    Assert.equal(this.document.getElementById("prefs").childElementCount,
                  filteredPrefArray.length + 1);
 
     // Test empty search returns all preferences.
     search.value = "";
     search.focus();
     EventUtils.sendKey("return");
 
     // The total number of preferences may change at any time because of
     // operations running in the background, so we only test approximately.
     // The change in count would be because of one or two added preferences,
     // but we tolerate a difference of up to 50 preferences just to be safe.
     // We want thousands of prefs instead of a few dozen that are filtered.
-    Assert.greater(content.document.getElementById("prefs").childElementCount,
+    Assert.greater(this.document.getElementById("prefs").childElementCount,
                    prefArray.length - 50);
 
     // Test invalid search returns no preferences.
     search.value = "aJunkValueasdf";
     search.focus();
     EventUtils.sendKey("return");
 
     // Expecting 1 row to be returned since it offers the ability to add
-    Assert.equal(content.document.getElementById("prefs").childElementCount,
+    Assert.equal(this.document.getElementById("prefs").childElementCount,
                  1);
 
     // Test added preferences search returns 2 preferences.
     search.value = "test.aboutconfig.a";
     search.focus();
     EventUtils.sendKey("return");
 
-    Assert.equal(content.document.getElementById("prefs").childElementCount,
+    Assert.equal(this.document.getElementById("prefs").childElementCount,
                  2);
   });
 });
--- a/browser/components/aboutconfig/test/browser/browser_warning.js
+++ b/browser/components/aboutconfig/test/browser/browser_warning.js
@@ -1,36 +1,28 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
-const PAGE_URL = "chrome://browser/content/aboutconfig/aboutconfig.html";
-
 add_task(async function setup() {
   await SpecialPowers.pushPrefEnv({
     set: [
       ["browser.aboutConfig.showWarning", true],
     ],
   });
 });
 
 add_task(async function test_load_warningpage() {
-  await BrowserTestUtils.withNewTab({
-    gBrowser,
-    url: PAGE_URL,
-  }, async browser => {
-    info("about:config loaded");
+  await AboutConfigTest.withNewTab(async function() {
     // Test that the warning page is presented:
-    Assert.equal(content.document.getElementsByTagName("button").length, 1);
-    Assert.equal(content.document.getElementById("search"), undefined);
-    Assert.equal(content.document.getElementById("prefs"), undefined);
+    Assert.equal(this.document.getElementsByTagName("button").length, 1);
+    Assert.equal(this.document.getElementById("search"), undefined);
+    Assert.equal(this.document.getElementById("prefs"), undefined);
 
     // Disable checkbox and reload.
-    content.document.getElementById("showWarningNextTime").click();
-    content.document.querySelector("button").click();
-  });
-  await BrowserTestUtils.withNewTab({
-    gBrowser,
-    url: PAGE_URL,
-  }, async browser => {
-    Assert.ok(content.document.getElementById("search"));
-    Assert.ok(content.document.getElementById("prefs"));
-  });
+    this.document.getElementById("showWarningNextTime").click();
+    this.document.querySelector("button").click();
+  }, { dontBypassWarning: true });
+
+  await AboutConfigTest.withNewTab(async function() {
+    Assert.ok(this.document.getElementById("search"));
+    Assert.ok(this.document.getElementById("prefs"));
+  }, { dontBypassWarning: true });
 });
new file mode 100644
--- /dev/null
+++ b/browser/components/aboutconfig/test/browser/head.js
@@ -0,0 +1,30 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+ChromeUtils.import("resource://gre/modules/Preferences.jsm", this);
+
+class AboutConfigTest {
+  static withNewTab(testFn, options = {}) {
+    return BrowserTestUtils.withNewTab({
+      gBrowser,
+      url: "chrome://browser/content/aboutconfig/aboutconfig.html",
+    }, async browser => {
+      let scope = new this(browser);
+      await scope.setupNewTab(options);
+      await testFn.call(scope);
+    });
+  }
+
+  constructor(browser) {
+    this.document = browser.contentDocument;
+  }
+
+  async setupNewTab(options) {
+    await this.document.l10n.ready;
+    if (!options.dontBypassWarning) {
+      this.document.querySelector("button").click();
+    }
+  }
+}