Bug 1501423 - Add ability to reset a modified pref on new "about:config" page. r=paolo
authormatthias <matthias@kammueller.eu>
Mon, 26 Nov 2018 10:26:20 +0000
changeset 504411 76207eef2ca82b95660a0c60adb2823da634f0a6
parent 504410 89e474b0a492ba2748e3bd6d7eb851a33c786a72
child 504412 6a00129133730106dd9472b091456357f683b904
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspaolo
bugs1501423
milestone65.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 1501423 - Add ability to reset a modified pref on new "about:config" page. r=paolo Differential Revision: https://phabricator.services.mozilla.com/D12230
browser/components/aboutconfig/content/aboutconfig.js
browser/components/aboutconfig/content/aboutconfig.notftl
browser/components/aboutconfig/test/browser/browser.ini
browser/components/aboutconfig/test/browser/browser_edit.js
--- a/browser/components/aboutconfig/content/aboutconfig.js
+++ b/browser/components/aboutconfig/content/aboutconfig.js
@@ -33,27 +33,41 @@ function onLoad() {
   gPrefArray.sort((a, b) => a.name > b.name);
 
   document.getElementById("search").addEventListener("keypress", function(e) {
     if (e.code == "Enter") {
       filterPrefs();
     }
   });
 
-  document.getElementById("prefs").appendChild(createPrefsFragment(gPrefArray));
   document.getElementById("prefs").addEventListener("click", (event) => {
     if (event.target.localName != "button") {
       return;
     }
     let prefRow = event.target.closest("tr");
     let prefName = prefRow.getAttribute("aria-label");
-    Services.prefs.clearUserPref(prefName);
-    gPrefArray.splice(gPrefArray.findIndex(pref => pref.name == prefName), 1);
-    prefRow.remove();
+    let button = event.target.closest("button");
+    if (button.classList.contains("button-reset")) {
+      // Reset pref and update gPrefArray.
+      Services.prefs.clearUserPref(prefName);
+      let pref = gPrefArray.find(p => p.name == prefName);
+      pref.value = Preferences.get(prefName);
+      pref.hasUserValue = false;
+      // Update UI.
+      prefRow.textContent = "";
+      prefRow.classList.remove("has-user-value");
+      prefRow.appendChild(getPrefRow(pref));
+    } else {
+      Services.prefs.clearUserPref(prefName);
+      gPrefArray.splice(gPrefArray.findIndex(pref => pref.name == prefName), 1);
+      prefRow.remove();
+    }
   });
+
+  document.getElementById("prefs").appendChild(createPrefsFragment(gPrefArray));
 }
 
 function filterPrefs() {
   let substring = document.getElementById("search").value.trim();
   let fragment = createPrefsFragment(gPrefArray.filter(pref => pref.name.includes(substring)));
   document.getElementById("prefs").textContent = "";
   document.getElementById("prefs").appendChild(fragment);
 }
@@ -62,43 +76,53 @@ function createPrefsFragment(prefArray) 
   let fragment = document.createDocumentFragment();
   for (let pref of prefArray) {
     let row = document.createElement("tr");
     if (pref.hasUserValue) {
       row.classList.add("has-user-value");
     }
     row.setAttribute("aria-label", pref.name);
 
-    let nameCell = document.createElement("td");
-    // Add <wbr> behind dots to prevent line breaking in random mid-word places.
-    let parts = pref.name.split(".");
-    for (let i = 0; i < parts.length - 1; i++) {
-      nameCell.append(parts[i] + ".", document.createElement("wbr"));
-    }
-    nameCell.append(parts[parts.length - 1]);
-    row.appendChild(nameCell);
-
-    let valueCell = document.createElement("td");
-    valueCell.classList.add("cell-value");
-    valueCell.textContent = pref.value;
-    row.appendChild(valueCell);
-
-    let buttonCell = document.createElement("td");
-    if (!pref.hasDefaultValue) {
-      let button = document.createElement("button");
-      document.l10n.setAttributes(button, "about-config-pref-delete");
-      buttonCell.appendChild(button);
-    }
-    row.appendChild(buttonCell);
-
+    row.appendChild(getPrefRow(pref));
     fragment.appendChild(row);
   }
   return fragment;
 }
 
+function getPrefRow(pref) {
+  let rowFragment = document.createDocumentFragment();
+  let nameCell = document.createElement("td");
+  // Add <wbr> behind dots to prevent line breaking in random mid-word places.
+  let parts = pref.name.split(".");
+  for (let i = 0; i < parts.length - 1; i++) {
+    nameCell.append(parts[i] + ".", document.createElement("wbr"));
+  }
+  nameCell.append(parts[parts.length - 1]);
+  rowFragment.appendChild(nameCell);
+
+  let valueCell = document.createElement("td");
+  valueCell.classList.add("cell-value");
+  valueCell.textContent = pref.value;
+  rowFragment.appendChild(valueCell);
+
+  let buttonCell = document.createElement("td");
+  if (pref.hasUserValue) {
+    let button = document.createElement("button");
+    if (!pref.hasDefaultValue) {
+      document.l10n.setAttributes(button, "about-config-pref-delete");
+    } else {
+      document.l10n.setAttributes(button, "about-config-pref-reset");
+      button.className = "button-reset";
+    }
+    buttonCell.appendChild(button);
+  }
+  rowFragment.appendChild(buttonCell);
+  return rowFragment;
+}
+
 function prefHasDefaultValue(name) {
   try {
     switch (Services.prefs.getPrefType(name)) {
       case Ci.nsIPrefBranch.PREF_STRING:
         gDefaultBranch.getStringPref(name);
         return true;
       case Ci.nsIPrefBranch.PREF_INT:
         gDefaultBranch.getIntPref(name);
--- a/browser/components/aboutconfig/content/aboutconfig.notftl
+++ b/browser/components/aboutconfig/content/aboutconfig.notftl
@@ -1,10 +1,11 @@
 # 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/.
 
 about-config-title = about:config
 
-about-config-pref-delete = Delete
-
 about-config-search =
     .placeholder = Search
+
+about-config-pref-reset = Reset
+about-config-pref-delete = Delete
--- a/browser/components/aboutconfig/test/browser/browser.ini
+++ b/browser/components/aboutconfig/test/browser/browser.ini
@@ -1,6 +1,7 @@
 [DEFAULT]
 
 [browser_basic.js]
 [browser_edit.js]
+skip-if = debug # Bug 1507747
 [browser_search.js]
 skip-if = debug # Bug 1507747
--- a/browser/components/aboutconfig/test/browser/browser_edit.js
+++ b/browser/components/aboutconfig/test/browser/browser_edit.js
@@ -3,24 +3,93 @@
 
 const PAGE_URL = "chrome://browser/content/aboutconfig/aboutconfig.html";
 
 add_task(async function test_delete_user_pref() {
   Services.prefs.setBoolPref("userAddedPref", true);
   await BrowserTestUtils.withNewTab({
     gBrowser,
     url: PAGE_URL,
-  }, browser => {
-    return ContentTask.spawn(browser, null, () => {
+  }, async browser => {
+    await ContentTask.spawn(browser, null, () => {
       let list = [...content.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")
         .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");
+      search.focus();
+    });
+
+    EventUtils.sendKey("return");
+    await ContentTask.spawn(browser, null, () => {
+      let list = [...content.document.getElementById("prefs")
+        .getElementsByTagName("tr")];
+      function getRow(name) {
+        return list.find(row => row.querySelector("td").textContent == name);
+      }
+      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 ContentTask.spawn(browser, null, () => {
+      ChromeUtils.import("resource://gre/modules/Preferences.jsm");
+
+      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")
+        .getElementsByTagName("tr")];
+      let row = getRow(testPref);
+      row.lastChild.lastChild.click();
+      // Check new layout and reset.
+      list = [...content.document.getElementById("prefs")
+        .getElementsByTagName("tr")];
+      Assert.ok(!row.classList.contains("has-user-value"));
+      Assert.equal(row.childNodes[2].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");
+      search.focus();
+    });
+
+    EventUtils.sendKey("return");
+    await ContentTask.spawn(browser, null, () => {
+      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";
+      // Check new layout and reset.
+      let list = [...content.document.getElementById("prefs")
+        .getElementsByTagName("tr")];
+      let row = getRow(testPref);
+      Assert.ok(!row.classList.contains("has-user-value"));
+      Assert.equal(row.childNodes[2].childNodes.length, 0);
+      Assert.equal(getValue(testPref), "" + Preferences.get(testPref));
+    });
+  });
+});