Bug 1516957 - Part 3 - Share more test functions. r=bgrins
authorPaolo Amadini <paolo.mozmail@amadzone.org>
Mon, 31 Dec 2018 12:15:38 +0000
changeset 509341 dda920bc62ddcb484abc9622e1c47f3fb0f47b0e
parent 509340 214d0e9f78255628d8f302e543fe4a4740ec33f3
child 509342 c1a87fe81d16a114e8df100a1ee9ce29a90f275d
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 3 - Share more test functions. r=bgrins This results in querying the document more often, but makes the test code more concise. Differential Revision: https://phabricator.services.mozilla.com/D15515
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_basic.js
+++ b/browser/components/aboutconfig/test/browser/browser_basic.js
@@ -12,45 +12,40 @@ add_task(async function setup() {
 add_task(async function test_load_title() {
   await AboutConfigTest.withNewTab(async function() {
     Assert.equal(this.document.title, "about:config");
   });
 });
 
 add_task(async function test_load_settings() {
   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;
-    }
-
     // Test if page contains elements.
-    Assert.ok(getRow("plugins.testmode"));
-    Assert.ok(getRow("dom.vr.enabled"));
-    Assert.ok(getRow("accessibility.AOM.enabled"));
+    Assert.ok(this.getRow("plugins.testmode"));
+    Assert.ok(this.getRow("dom.vr.enabled"));
+    Assert.ok(this.getRow("accessibility.AOM.enabled"));
 
     // Test if the modified state is displayed for the right prefs.
     let prefArray = Services.prefs.getChildList("");
     let nameOfEdited = prefArray.find(
       name => Services.prefs.prefHasUserValue(name));
     let nameOfDefault = prefArray.find(
       name => !Services.prefs.prefHasUserValue(name));
-    Assert.ok(!getRow(nameOfDefault).classList.contains("has-user-value"));
-    Assert.ok(getRow(nameOfEdited).classList.contains("has-user-value"));
+    Assert.ok(!this.getRow(nameOfDefault).hasClass("has-user-value"));
+    Assert.ok(this.getRow(nameOfEdited).hasClass("has-user-value"));
 
     // Test to see if values are localized.
-    Assert.equal(getValue("font.language.group"), "x-western");
-    Assert.equal(getValue("intl.ellipsis"), "\u2026");
+    Assert.equal(this.getRow("font.language.group").value, "x-western");
+    Assert.equal(this.getRow("intl.ellipsis").value, "\u2026");
     Assert.equal(
-      getValue("gecko.handlerService.schemes.mailto.1.uriTemplate"),
+      this.getRow("gecko.handlerService.schemes.mailto.1.uriTemplate").value,
       "https://mail.google.com/mail/?extsrc=mailto&url=%s");
 
-    // Test to see if user created value is not empty string when it matches /^chrome:\/\/.+\/locale\/.+\.properties/.
-    Assert.equal(getValue("random.user.pref"),
+    // Test to see if user created value is not empty string when it matches
+    // /^chrome:\/\/.+\/locale\/.+\.properties/.
+    Assert.equal(this.getRow("random.user.pref").value,
       "chrome://test/locale/testing.properties");
-    // Test to see if empty string when value matches /^chrome:\/\/.+\/locale\/.+\.properties/ and an exception is thrown.
-    Assert.equal(getValue("gecko.handlerService.schemes.irc.1.name"), "");
+
+    // Test to see if empty string when value matches
+    // /^chrome:\/\/.+\/locale\/.+\.properties/ and an exception is thrown.
+    Assert.equal(this.getRow("gecko.handlerService.schemes.irc.1.name").value,
+      "");
   });
 });
--- a/browser/components/aboutconfig/test/browser/browser_edit.js
+++ b/browser/components/aboutconfig/test/browser/browser_edit.js
@@ -41,140 +41,109 @@ add_task(async function test_add_user_pr
       search.focus();
     }
   });
 });
 
 add_task(async function test_delete_user_pref() {
   Services.prefs.setBoolPref("userAddedPref", true);
   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 = [...this.document.getElementById("prefs")
-      .getElementsByTagName("tr")];
-    Assert.ok(!getRow("userAddedPref"));
+    let row = this.getRow("userAddedPref");
+    row.element.lastChild.lastChild.click();
+    Assert.ok(!this.getRow("userAddedPref"));
     Assert.ok(!Services.prefs.getChildList("").includes("userAddedPref"));
 
     // Search for nothing to test gPrefArray
     let search = this.document.getElementById("search");
     search.focus();
     EventUtils.sendKey("return");
 
-    list = [...this.document.getElementById("prefs")
-      .getElementsByTagName("tr")];
-    Assert.ok(!getRow("userAddedPref"));
+    Assert.ok(!this.getRow("userAddedPref"));
   });
 });
 
 add_task(async function test_reset_user_pref() {
   await SpecialPowers.pushPrefEnv({"set": [["browser.autofocus", false]]});
 
   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 = [...this.document.getElementById("prefs")
-      .getElementsByTagName("tr")];
-    let row = getRow(testPref);
-    row.lastChild.lastChild.click();
+    let row = this.getRow(testPref);
+    row.element.lastChild.lastChild.click();
     // Check new layout and reset.
-    list = [...this.document.getElementById("prefs")
-      .getElementsByTagName("tr")];
-    Assert.ok(!row.classList.contains("has-user-value"));
-    Assert.equal(row.lastChild.childNodes.length, 0);
+    Assert.ok(!row.hasClass("has-user-value"));
+    Assert.equal(row.element.lastChild.childNodes.length, 0);
     Assert.ok(!Services.prefs.prefHasUserValue(testPref));
-    Assert.equal(getValue(testPref), "" + Preferences.get(testPref));
+    Assert.equal(this.getRow(testPref).value, "" + Preferences.get(testPref));
 
     // Search for nothing to test gPrefArray
     let search = this.document.getElementById("search");
     search.focus();
     EventUtils.sendKey("return");
 
     // Check new layout and reset.
-    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));
+    row = this.getRow(testPref);
+    Assert.ok(!row.hasClass("has-user-value"));
+    Assert.equal(row.element.lastChild.childNodes.length, 0);
+    Assert.equal(this.getRow(testPref).value, "" + Preferences.get(testPref));
   });
 });
 
 add_task(async function test_modify() {
   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 = [...this.document.getElementById("prefs")
-      .getElementsByTagName("tr")];
     for (let nameOfBoolPref of [
       "test.aboutconfig.modify.boolean",
       "accessibility.typeaheadfind.autostart",
     ]) {
-      let row = getRow(nameOfBoolPref, list);
+      let row = this.getRow(nameOfBoolPref);
       // Do this a two times to reset the pref.
       for (let i = 0; i < 2; i++) {
         row.querySelector("td.cell-edit").firstChild.click();
         // Check new layout and saving in backend.
-        Assert.equal(getValue(nameOfBoolPref, list),
+        Assert.equal(this.getRow(nameOfBoolPref).value,
           "" + Preferences.get(nameOfBoolPref));
         let prefHasUserValue = Services.prefs.prefHasUserValue(nameOfBoolPref);
-        Assert.equal(row.classList.contains("has-user-value"), prefHasUserValue);
-        Assert.equal(row.lastChild.childNodes.length > 0, prefHasUserValue);
+        Assert.equal(row.hasClass("has-user-value"), prefHasUserValue);
+        Assert.equal(row.element.lastChild.childNodes.length > 0, prefHasUserValue);
       }
     }
 
     // Test abort of edit by starting with string and continuing with editing Int pref.
-    let row = getRow("test.aboutconfig.modify.string", list);
+    let row = this.getRow("test.aboutconfig.modify.string");
     row.querySelector("td.cell-edit").firstChild.click();
     row.querySelector("td.cell-value").firstChild.firstChild.value = "test";
-    let intRow = getRow("test.aboutconfig.modify.number", list);
+    let intRow = this.getRow("test.aboutconfig.modify.number");
     intRow.querySelector("td.cell-edit").firstChild.click();
     Assert.equal(intRow.querySelector("td.cell-value").firstChild.firstChild.value,
       Preferences.get("test.aboutconfig.modify.number"));
-    Assert.equal(getValue("test.aboutconfig.modify.string", list),
+    Assert.equal(this.getRow("test.aboutconfig.modify.string").value,
       "" + Preferences.get("test.aboutconfig.modify.string"));
     Assert.equal(row.querySelector("td.cell-value").textContent,
       Preferences.get("test.aboutconfig.modify.string"));
 
     // Test regex check for Int pref.
     intRow.querySelector("td.cell-value").firstChild.firstChild.value += "a";
     intRow.querySelector("td.cell-edit").firstChild.click();
     Assert.ok(!intRow.querySelector("td.cell-value").firstChild.firstChild.checkValidity());
 
     // Test correct saving and DOM-update.
     for (let prefName of [
       "test.aboutconfig.modify.string",
       "test.aboutconfig.modify.number",
       "accessibility.typeaheadfind.soundURL",
       "accessibility.typeaheadfind.casesensitive",
     ]) {
-      row = getRow(prefName, list);
+      row = this.getRow(prefName);
       // Activate edit and check displaying.
       row.querySelector("td.cell-edit").firstChild.click();
       Assert.equal(row.querySelector("td.cell-value").firstChild.firstChild.value,
         Preferences.get(prefName));
       row.querySelector("td.cell-value").firstChild.firstChild.value = "42";
       // Save and check saving.
       row.querySelector("td.cell-edit").firstChild.click();
-      Assert.equal(getValue(prefName, list),
-        "" + Preferences.get(prefName));
+      Assert.equal(row.value, "" + Preferences.get(prefName));
       let prefHasUserValue = Services.prefs.prefHasUserValue(prefName);
-      Assert.equal(row.lastChild.childNodes.length > 0, prefHasUserValue);
-      Assert.equal(row.classList.contains("has-user-value"), prefHasUserValue);
+      Assert.equal(row.element.lastChild.childNodes.length > 0, prefHasUserValue);
+      Assert.equal(row.hasClass("has-user-value"), prefHasUserValue);
     }
   });
 });
--- a/browser/components/aboutconfig/test/browser/browser_locked.js
+++ b/browser/components/aboutconfig/test/browser/browser_locked.js
@@ -15,49 +15,37 @@ add_task(async function test_locked() {
     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 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) {
-      return getRow(name).querySelector("button");
-    }
-
     // Test locked default string pref.
-    let lockedPref = getRow("browser.search.searchEnginesURL");
-    Assert.ok(lockedPref.classList.contains("locked"));
-    Assert.equal(getValue("browser.search.searchEnginesURL"), "https://addons.mozilla.org/%LOCALE%/firefox/search-engines/");
-    Assert.ok(getButton("browser.search.searchEnginesURL").classList.contains("button-edit"));
-    Assert.equal(getButton("browser.search.searchEnginesURL").disabled, true);
+    let lockedPref = this.getRow("browser.search.searchEnginesURL");
+    Assert.ok(lockedPref.hasClass("locked"));
+    Assert.equal(lockedPref.value, "https://addons.mozilla.org/%LOCALE%/firefox/search-engines/");
+    Assert.ok(lockedPref.firstButton.classList.contains("button-edit"));
+    Assert.ok(lockedPref.firstButton.disabled);
 
     // Test locked default boolean pref.
-    lockedPref = getRow("accessibility.AOM.enabled");
-    Assert.ok(lockedPref.classList.contains("locked"));
-    Assert.equal(getValue("accessibility.AOM.enabled"), "false");
-    Assert.ok(getButton("accessibility.AOM.enabled").classList.contains("button-toggle"));
-    Assert.equal(getButton("accessibility.AOM.enabled").disabled, true);
+    lockedPref = this.getRow("accessibility.AOM.enabled");
+    Assert.ok(lockedPref.hasClass("locked"));
+    Assert.equal(lockedPref.value, "false");
+    Assert.ok(lockedPref.firstButton.classList.contains("button-toggle"));
+    Assert.ok(lockedPref.firstButton.disabled);
 
     // Test locked user added pref.
-    lockedPref = getRow("test.aboutconfig.a");
-    Assert.ok(lockedPref.classList.contains("locked"));
-    Assert.equal(getValue("test.aboutconfig.a"), "");
-    Assert.ok(getButton("test.aboutconfig.a").classList.contains("button-edit"));
-    Assert.equal(getButton("test.aboutconfig.a").disabled, true);
+    lockedPref = this.getRow("test.aboutconfig.a");
+    Assert.ok(lockedPref.hasClass("locked"));
+    Assert.equal(lockedPref.value, "");
+    Assert.ok(lockedPref.firstButton.classList.contains("button-edit"));
+    Assert.ok(lockedPref.firstButton.disabled);
 
     // Test pref not locked
-    let unlockedPref = getRow("accessibility.indicator.enabled");
-    Assert.equal(unlockedPref.classList.contains("locked"), false);
-    Assert.equal(getValue("accessibility.indicator.enabled"), "false");
-    Assert.ok(getButton("accessibility.indicator.enabled").classList.contains("button-toggle"));
-    Assert.equal(getButton("accessibility.indicator.enabled").disabled, false);
+    let unlockedPref = this.getRow("accessibility.indicator.enabled");
+    Assert.ok(!unlockedPref.hasClass("locked"));
+    Assert.equal(unlockedPref.value, "false");
+    Assert.ok(unlockedPref.firstButton.classList.contains("button-toggle"));
+    Assert.ok(!unlockedPref.firstButton.disabled);
   });
 });
--- a/browser/components/aboutconfig/test/browser/browser_search.js
+++ b/browser/components/aboutconfig/test/browser/browser_search.js
@@ -16,58 +16,53 @@ add_task(async function test_search() {
     let prefArray = Services.prefs.getChildList("");
     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(this.document.getElementById("prefs").childElementCount,
-                   prefArray.length - 50);
+    Assert.greater(this.rows.length, 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 = 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(this.document.getElementById("prefs").childElementCount,
-                 filteredPrefArray.length + 1);
+    Assert.equal(this.rows.length, 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(this.document.getElementById("prefs").childElementCount,
-                   prefArray.length - 50);
+    Assert.greater(this.rows.length, 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(this.document.getElementById("prefs").childElementCount,
-                 1);
+    Assert.equal(this.rows.length, 1);
 
     // Test added preferences search returns 2 preferences.
     search.value = "test.aboutconfig.a";
     search.focus();
     EventUtils.sendKey("return");
 
-    Assert.equal(this.document.getElementById("prefs").childElementCount,
-                 2);
+    Assert.equal(this.rows.length, 2);
   });
 });
--- a/browser/components/aboutconfig/test/browser/browser_warning.js
+++ b/browser/components/aboutconfig/test/browser/browser_warning.js
@@ -8,18 +8,18 @@ add_task(async function setup() {
     ],
   });
 });
 
 add_task(async function test_load_warningpage() {
   await AboutConfigTest.withNewTab(async function() {
     // Test that the warning page is presented:
     Assert.equal(this.document.getElementsByTagName("button").length, 1);
-    Assert.equal(this.document.getElementById("search"), undefined);
-    Assert.equal(this.document.getElementById("prefs"), undefined);
+    Assert.ok(!this.document.getElementById("search"));
+    Assert.ok(!this.document.getElementById("prefs"));
 
     // Disable checkbox and reload.
     this.document.getElementById("showWarningNextTime").click();
     this.document.querySelector("button").click();
   }, { dontBypassWarning: true });
 
   await AboutConfigTest.withNewTab(async function() {
     Assert.ok(this.document.getElementById("search"));
--- a/browser/components/aboutconfig/test/browser/head.js
+++ b/browser/components/aboutconfig/test/browser/head.js
@@ -1,15 +1,41 @@
 /* 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 AboutConfigRowTest {
+  constructor(element) {
+    this.element = element;
+  }
+
+  querySelector(selector) {
+    return this.element.querySelector(selector);
+  }
+
+  get name() {
+    return this.querySelector("td").textContent;
+  }
+
+  get value() {
+    return this.querySelector("td.cell-value").textContent;
+  }
+
+  get firstButton() {
+    return this.querySelector("button");
+  }
+
+  hasClass(className) {
+    return this.element.classList.contains(className);
+  }
+}
+
 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);
@@ -22,9 +48,26 @@ class AboutConfigTest {
   }
 
   async setupNewTab(options) {
     await this.document.l10n.ready;
     if (!options.dontBypassWarning) {
       this.document.querySelector("button").click();
     }
   }
+
+  /**
+   * Array of AboutConfigRowTest objects, one for each row in the main table.
+   */
+  get rows() {
+    let elements = this.document.getElementById("prefs")
+                                .getElementsByTagName("tr");
+    return Array.map(elements, element => new AboutConfigRowTest(element));
+  }
+
+  /**
+   * Returns the AboutConfigRowTest object for the row in the main table which
+   * corresponds to the given preference name, or undefined if none is present.
+   */
+  getRow(name) {
+    return this.rows.find(row => row.name == name);
+  }
 }