Bug 1499363 - Fix userScript test failure on beta due to runWithPrefs not restoring the initial prefs values. r=robwu
authorLuca Greco <lgreco@mozilla.com>
Thu, 18 Oct 2018 17:14:24 +0000
changeset 490323 99d43f9d3918d1bddf2be683e9b9d8c9de6d688f
parent 490322 545ff4f7bc5ca0cd013eda97efb96e5a5759c810
child 490324 25be011e674c5a4cb1acfbfaeb68e757a2979236
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewersrobwu
bugs1499363
milestone64.0a1
Bug 1499363 - Fix userScript test failure on beta due to runWithPrefs not restoring the initial prefs values. r=robwu Differential Revision: https://phabricator.services.mozilla.com/D9107
toolkit/components/extensions/test/xpcshell/head.js
--- a/toolkit/components/extensions/test/xpcshell/head.js
+++ b/toolkit/components/extensions/test/xpcshell/head.js
@@ -108,42 +108,76 @@ function cleanupDir(dir) {
         return reject(`Failed to cleanup directory: ${dir}`);
       }
       setTimeout(tryToRemoveDir, 100);
     }
     tryToRemoveDir();
   });
 }
 
-// Run a test with the specified preferences and then clear them
+// Run a test with the specified preferences and then restores their initial values
 // right after the test function run (whether it passes or fails).
 async function runWithPrefs(prefsToSet, testFn) {
-  try {
-    for (let [pref, value] of prefsToSet) {
+  const setPrefs = (prefs) => {
+    for (let [pref, value] of prefs) {
+      if (value === undefined) {
+        // Clear any pref that didn't have a user value.
+        info(`Clearing pref "${pref}"`);
+        Services.prefs.clearUserPref(pref);
+        continue;
+      }
+
       info(`Setting pref "${pref}": ${value}`);
       switch (typeof(value)) {
         case "boolean":
           Services.prefs.setBoolPref(pref, value);
           break;
         case "number":
           Services.prefs.setIntPref(pref, value);
           break;
         case "string":
           Services.prefs.setStringPref(pref, value);
           break;
         default:
           throw new Error("runWithPrefs doesn't support this pref type yet");
       }
     }
+  };
+
+  const getPrefs = (prefs) => {
+    return prefs.map(([pref, value]) => {
+      info(`Getting initial pref value for "${pref}"`);
+      if (!Services.prefs.prefHasUserValue(pref)) {
+        // Check if the pref doesn't have a user value.
+        return [pref, undefined];
+      }
+      switch (typeof value) {
+        case "boolean":
+          return [pref, Services.prefs.getBoolPref(pref)];
+        case "number":
+          return [pref, Services.prefs.getIntPref(pref)];
+        case "string":
+          return [pref, Services.prefs.getStringPref(pref)];
+        default:
+          throw new Error("runWithPrefs doesn't support this pref type yet");
+      }
+    });
+  };
+
+  let initialPrefsValues = [];
+
+  try {
+    initialPrefsValues = getPrefs(prefsToSet);
+
+    setPrefs(prefsToSet);
+
     await testFn();
   } finally {
-    for (let [prefName] of prefsToSet) {
-      info(`Clearing pref "${prefName}"`);
-      Services.prefs.clearUserPref(prefName);
-    }
+    info("Restoring initial preferences values on exit");
+    setPrefs(initialPrefsValues);
   }
 }
 
 // "Handling User Input" test helpers.
 
 let extensionHandlers = new WeakSet();
 
 function handlingUserInputFrameScript() {