Bug 1446508 - Create test file that makes it easier to add tests for simple policies. r=MattN a=test-only DEVEDITION_60_0b7_BUILD1 DEVEDITION_60_0b7_RELEASE FENNEC_60_0b7_BUILD1 FENNEC_60_0b7_RELEASE FIREFOX_60_0b7_BUILD1 FIREFOX_60_0b7_RELEASE
authorFelipe Gomes <felipc@gmail.com>
Mon, 26 Mar 2018 13:39:58 -0300
changeset 460378 45249cb4b63e49e38d91e6424d509579620bb858
parent 460377 8bb565a35f45362c86e7a6844d87b8b260c994fd
child 460379 118f7f30f68af571995954f4df4bcd7ca2243947
push id8925
push userfelipc@gmail.com
push dateMon, 26 Mar 2018 16:41:03 +0000
treeherdermozilla-beta@45249cb4b63e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMattN, test-only
bugs1446508
milestone60.0
Bug 1446508 - Create test file that makes it easier to add tests for simple policies. r=MattN a=test-only
browser/components/enterprisepolicies/tests/EnterprisePolicyTesting.jsm
browser/components/enterprisepolicies/tests/browser/browser.ini
browser/components/enterprisepolicies/tests/browser/browser_policies_basic_tests.js
browser/components/enterprisepolicies/tests/browser/browser_policies_runOnce_helper.js
browser/components/enterprisepolicies/tests/browser/browser_policies_setAndLockPref_API.js
browser/components/enterprisepolicies/tests/browser/browser_policies_simple_policies.js
browser/components/enterprisepolicies/tests/browser/browser_policies_simple_pref_policies.js
browser/components/enterprisepolicies/tests/browser/browser_policy_clear_blocked_cookies.js
browser/components/enterprisepolicies/tests/browser/browser_policy_default_browser_check.js
browser/components/enterprisepolicies/tests/browser/browser_policy_disable_formhistory.js
browser/components/enterprisepolicies/tests/browser/browser_policy_disable_fxaccounts.js
browser/components/enterprisepolicies/tests/browser/browser_policy_disable_fxscreenshots.js
browser/components/enterprisepolicies/tests/browser/browser_policy_disable_pocket.js
browser/components/enterprisepolicies/tests/browser/browser_policy_enable_tracking_protection.js
browser/components/enterprisepolicies/tests/browser/browser_policy_proxy.js
browser/components/enterprisepolicies/tests/browser/browser_policy_remember_passwords.js
browser/components/enterprisepolicies/tests/browser/browser_policy_set_homepage.js
browser/components/enterprisepolicies/tests/browser/disable_fxscreenshots/browser.ini
browser/components/enterprisepolicies/tests/browser/disable_fxscreenshots/browser_policy_disable_fxscreenshots.js
browser/components/enterprisepolicies/tests/browser/disable_fxscreenshots/config_disable_fxscreenshots.json
browser/components/enterprisepolicies/tests/browser/head.js
browser/components/enterprisepolicies/tests/moz.build
--- a/browser/components/enterprisepolicies/tests/EnterprisePolicyTesting.jsm
+++ b/browser/components/enterprisepolicies/tests/EnterprisePolicyTesting.jsm
@@ -1,22 +1,24 @@
 /* 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/. */
 
 "use strict";
 
+ChromeUtils.import("resource://gre/modules/Preferences.jsm");
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 ChromeUtils.import("resource://gre/modules/osfile.jsm");
+ChromeUtils.import("resource://testing-common/Assert.jsm");
 ChromeUtils.defineModuleGetter(this, "FileTestUtils",
                                "resource://testing-common/FileTestUtils.jsm");
 
-this.EXPORTED_SYMBOLS = ["EnterprisePolicyTesting"];
+var EXPORTED_SYMBOLS = ["EnterprisePolicyTesting", "PoliciesPrefTracker"];
 
-this.EnterprisePolicyTesting = {
+var EnterprisePolicyTesting = {
   // |json| must be an object representing the desired policy configuration, OR a
   // path to the JSON file containing the policy configuration.
   setupPolicyEngineWithJson: async function setupPolicyEngineWithJson(json, customSchema) {
     let filePath;
     if (typeof(json) == "object") {
       filePath = FileTestUtils.getTempFile("policies.json").path;
 
       // This file gets automatically deleted by FileTestUtils
@@ -45,21 +47,104 @@ this.EnterprisePolicyTesting = {
       let schemaModule = ChromeUtils.import("resource:///modules/policies/schema.jsm", {});
       schemaModule.schema = customSchema;
     }
 
     Services.obs.notifyObservers(null, "EnterprisePolicies:Restart");
     return promise;
   },
 
+  checkPolicyPref(prefName, expectedValue, expectedLockedness) {
+    if (expectedLockedness !== undefined) {
+      Assert.equal(Preferences.locked(prefName), expectedLockedness, `Pref ${prefName} is correctly locked/unlocked`);
+    }
+
+    Assert.equal(Preferences.get(prefName), expectedValue, `Pref ${prefName} has the correct value`);
+  },
+
   resetRunOnceState: function resetRunOnceState() {
     const runOnceBaseKeys = [
       "browser.policies.runonce.",
       "browser.policies.runOncePerModification."
     ];
     for (let base of runOnceBaseKeys) {
       for (let key of Services.prefs.getChildList(base, {})) {
         if (Services.prefs.prefHasUserValue(key))
           Services.prefs.clearUserPref(key);
       }
     }
   },
 };
+
+/**
+ * This helper will track prefs that have been changed
+ * by the policy engine through the setAndLockPref and
+ * setDefaultPref APIs (from Policies.jsm) and make sure
+ * that they are restored to their original values when
+ * the test ends or another test case restarts the engine.
+ */
+var PoliciesPrefTracker = {
+  _originalFunc: null,
+  _originalValues: new Map(),
+
+  start() {
+    let PoliciesBackstage = ChromeUtils.import("resource:///modules/policies/Policies.jsm", {});
+    this._originalFunc = PoliciesBackstage.setDefaultPref;
+    PoliciesBackstage.setDefaultPref = this.hoistedSetDefaultPref.bind(this);
+  },
+
+  stop() {
+    this.restoreDefaultValues();
+
+    let PoliciesBackstage = ChromeUtils.import("resource:///modules/policies/Policies.jsm", {});
+    PoliciesBackstage.setDefaultPref = this._originalFunc;
+    this._originalFunc = null;
+  },
+
+  hoistedSetDefaultPref(prefName, prefValue) {
+    // If this pref is seen multiple times, the very first
+    // value seen is the one that is actually the default.
+    if (!this._originalValues.has(prefName)) {
+      let defaults = new Preferences({defaultBranch: true});
+      let stored = {};
+
+      if (defaults.has(prefName)) {
+        stored.originalDefaultValue = defaults.get(prefName);
+      }
+
+      if (Preferences.isSet(prefName) &&
+          Preferences.get(prefName) == prefValue) {
+        // If a user value exists, and we're changing the default
+        // value to be th same as the user value, that will cause
+        // the user value to be dropped. In that case, let's also
+        // store it to ensure that we restore everything correctly.
+        stored.originalUserValue = Preferences.get(prefName);
+      }
+
+      this._originalValues.set(prefName, stored);
+    }
+
+    // Now that we've stored the original values, call the
+    // original setDefaultPref function.
+    this._originalFunc(prefName, prefValue);
+  },
+
+  restoreDefaultValues() {
+    let defaults = new Preferences({defaultBranch: true});
+
+    for (let [prefName, stored] of this._originalValues) {
+      // If a pref was used through setDefaultPref instead
+      // of setAndLockPref, it wasn't locked, but calling
+      // unlockPref is harmless
+      Preferences.unlock(prefName);
+
+      if (stored.originalDefaultValue) {
+        defaults.set(prefName, stored.originalDefaultValue);
+      }
+
+      if (stored.originalUserValue) {
+        Preferences.set(prefName, stored.originalUserValue);
+      }
+    }
+
+    this._originalValues.clear();
+  },
+};
--- a/browser/components/enterprisepolicies/tests/browser/browser.ini
+++ b/browser/components/enterprisepolicies/tests/browser/browser.ini
@@ -1,42 +1,39 @@
 [DEFAULT]
 prefs =
   browser.policies.enabled=true
 support-files =
   head.js
   config_popups_cookies_addons_flash.json
   config_broken_json.json
 
+[browser_policies_basic_tests.js]
 [browser_policies_broken_json.js]
 [browser_policies_enterprise_only.js]
 [browser_policies_notice_in_aboutpreferences.js]
 [browser_policies_popups_cookies_addons_flash.js]
 [browser_policies_runOnce_helper.js]
 [browser_policies_setAndLockPref_API.js]
-[browser_policies_simple_policies.js]
+[browser_policies_simple_pref_policies.js]
 [browser_policies_sorted_alphabetically.js]
 [browser_policies_validate_and_parse_API.js]
 [browser_policy_app_update.js]
 [browser_policy_block_about_addons.js]
 [browser_policy_block_about_config.js]
 [browser_policy_block_about_profiles.js]
 [browser_policy_block_about_support.js]
 [browser_policy_block_set_desktop_background.js]
 [browser_policy_bookmarks.js]
 [browser_policy_clear_blocked_cookies.js]
 [browser_policy_default_browser_check.js]
 [browser_policy_disable_feedback_commands.js]
-[browser_policy_disable_formhistory.js]
 [browser_policy_disable_fxaccounts.js]
-[browser_policy_disable_fxscreenshots.js]
 [browser_policy_disable_masterpassword.js]
 [browser_policy_disable_pdfjs.js]
 [browser_policy_disable_pocket.js]
 [browser_policy_disable_privatebrowsing.js]
 [browser_policy_disable_safemode.js]
 [browser_policy_disable_shield.js]
 [browser_policy_display_bookmarks.js]
 [browser_policy_display_menu.js]
-[browser_policy_enable_tracking_protection.js]
 [browser_policy_proxy.js]
-[browser_policy_remember_passwords.js]
 [browser_policy_set_homepage.js]
rename from browser/components/enterprisepolicies/tests/browser/browser_policies_simple_policies.js
rename to browser/components/enterprisepolicies/tests/browser/browser_policies_basic_tests.js
--- a/browser/components/enterprisepolicies/tests/browser/browser_policies_runOnce_helper.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policies_runOnce_helper.js
@@ -11,12 +11,9 @@ function callback() {
 }
 
 add_task(async function test_runonce_helper() {
   runOnce("test_action", callback);
   is(runCount, 1, "Callback ran for the first time.");
 
   runOnce("test_action", callback);
   is(runCount, 1, "Callback didn't run again.");
-
-  // clean-up
-  Services.prefs.clearUserPref("browser.policies.runonce.test_action");
 });
--- a/browser/components/enterprisepolicies/tests/browser/browser_policies_setAndLockPref_API.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policies_setAndLockPref_API.js
@@ -1,64 +1,45 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
 "use strict";
 
-let { Policies, setAndLockPref } = ChromeUtils.import("resource:///modules/policies/Policies.jsm", {});
-
-function checkPref(prefName, expectedValue) {
-  let prefType, prefValue;
-  switch (typeof(expectedValue)) {
-    case "boolean":
-      prefType = Services.prefs.PREF_BOOL;
-      prefValue = Services.prefs.getBoolPref(prefName);
-      break;
-
-    case "number":
-      prefType = Services.prefs.PREF_INT;
-      prefValue = Services.prefs.getIntPref(prefName);
-      break;
-
-    case "string":
-      prefType = Services.prefs.PREF_STRING;
-      prefValue = Services.prefs.getStringPref(prefName);
-      break;
-  }
-
-  ok(Services.prefs.prefIsLocked(prefName), `Pref ${prefName} is correctly locked`);
-  is(Services.prefs.getPrefType(prefName), prefType, `Pref ${prefName} has the correct type`);
-  is(prefValue, expectedValue, `Pref ${prefName} has the correct value`);
-}
+let {
+  Policies,
+  setAndLockPref,
+  setDefaultPref,
+} = ChromeUtils.import("resource:///modules/policies/Policies.jsm", {});
 
 add_task(async function test_API_directly() {
+  await setupPolicyEngineWithJson("");
   setAndLockPref("policies.test.boolPref", true);
-  checkPref("policies.test.boolPref", true);
+  checkLockedPref("policies.test.boolPref", true);
 
   // Check that a previously-locked pref can be changed
   // (it will be unlocked first).
   setAndLockPref("policies.test.boolPref", false);
-  checkPref("policies.test.boolPref", false);
+  checkLockedPref("policies.test.boolPref", false);
 
   setAndLockPref("policies.test.intPref", 0);
-  checkPref("policies.test.intPref", 0);
+  checkLockedPref("policies.test.intPref", 0);
 
   setAndLockPref("policies.test.stringPref", "policies test");
-  checkPref("policies.test.stringPref", "policies test");
+  checkLockedPref("policies.test.stringPref", "policies test");
 
   // Test that user values do not override the prefs, and the get*Pref call
   // still return the value set through setAndLockPref
   Services.prefs.setBoolPref("policies.test.boolPref", true);
-  checkPref("policies.test.boolPref", false);
+  checkLockedPref("policies.test.boolPref", false);
 
   Services.prefs.setIntPref("policies.test.intPref", 10);
-  checkPref("policies.test.intPref", 0);
+  checkLockedPref("policies.test.intPref", 0);
 
   Services.prefs.setStringPref("policies.test.stringPref", "policies test");
-  checkPref("policies.test.stringPref", "policies test");
+  checkLockedPref("policies.test.stringPref", "policies test");
 
   try {
     // Test that a non-integer value is correctly rejected, even though
     // typeof(val) == "number"
     setAndLockPref("policies.test.intPref", 1.5);
     ok(false, "Integer value should be rejected");
   } catch (ex) {
     ok(true, "Integer value was rejected");
@@ -115,16 +96,48 @@ add_task(async function test_API_through
         }
       }
     }
   );
 
   is(Services.policies.status, Ci.nsIEnterprisePolicies.ACTIVE, "Engine is active");
 
   // The expected values come from config_setAndLockPref.json
-  checkPref("policies.test2.boolPref", true);
-  checkPref("policies.test2.intPref", 42);
-  checkPref("policies.test2.stringPref", "policies test 2");
+  checkLockedPref("policies.test2.boolPref", true);
+  checkLockedPref("policies.test2.intPref", 42);
+  checkLockedPref("policies.test2.stringPref", "policies test 2");
 
   delete Policies.bool_policy;
   delete Policies.int_policy;
   delete Policies.string_policy;
 });
+
+add_task(async function test_pref_tracker() {
+  // Tests the test harness functionality that tracks usage of
+  // the setAndLockPref and setDefualtPref APIs.
+
+  let defaults = Services.prefs.getDefaultBranch("");
+
+  // Test prefs that had a default value and got changed to another
+  defaults.setIntPref("test1.pref1", 10);
+  defaults.setStringPref("test1.pref2", "test");
+
+  setAndLockPref("test1.pref1", 20);
+  setDefaultPref("test1.pref2", "NEW VALUE");
+
+  PoliciesPrefTracker.restoreDefaultValues();
+
+  is(Services.prefs.getIntPref("test1.pref1"), 10, "Expected value for test1.pref1");
+  is(Services.prefs.getStringPref("test1.pref2"), "test", "Expected value for test1.pref2");
+  is(Services.prefs.prefIsLocked("test1.pref1"), false, "test1.pref1 got unlocked");
+
+  // Test a pref that had a default value and a user value
+  defaults.setIntPref("test2.pref1", 10);
+  Services.prefs.setIntPref("test2.pref1", 20);
+
+  setAndLockPref("test2.pref1", 20);
+
+  PoliciesPrefTracker.restoreDefaultValues();
+
+  is(Services.prefs.getIntPref("test2.pref1"), 20, "Correct user value");
+  is(defaults.getIntPref("test2.pref1"), 10, "Correct default value");
+  is(Services.prefs.prefIsLocked("test2.pref1"), false, "felipe pref is not locked");
+});
new file mode 100644
--- /dev/null
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policies_simple_pref_policies.js
@@ -0,0 +1,90 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+/*
+ * Use this file to add tests to policies that are
+ * simple pref flips.
+ *
+ * It's best to make a test to actually test the feature
+ * instead of the pref flip, but if that feature is well
+ * covered by tests, including that its pref actually works,
+ * it's OK to have the policy test here just to ensure
+ * that the right pref values are set.
+ */
+
+const POLICIES_TESTS = [
+  /*
+   * Example:
+   * {
+   *   // Policies to be set at once through the engine
+   *   policies: { "DisableFoo": true, "ConfigureBar": 42 },
+   *
+   *   // Locked prefs to check
+   *   lockedPrefs: { "feature.foo": false },
+   *
+   *   // Unlocked prefs to check
+   *   unlockedPrefs: { "bar.baz": 42 }
+   * },
+   */
+
+   // POLICY: RememberPasswords
+  {
+    policies: { "RememberPasswords": false },
+    lockedPrefs: { "signon.rememberSignons": false },
+  },
+  {
+    policies: { "RememberPasswords": true },
+    lockedPrefs: { "signon.rememberSignons": true },
+  },
+
+  // POLICY: DisableFormHistory
+  {
+    policies: { "DisableFormHistory": true },
+    lockedPrefs: { "browser.formfill.enable": false },
+  },
+
+  // POLICY: EnableTrackingProtection
+  {
+    policies: {
+      "EnableTrackingProtection": {
+        "Value": true
+      }
+    },
+    unlockedPrefs: {
+      "privacy.trackingprotection.enabled": true,
+      "privacy.trackingprotection.pbmode.enabled": true,
+    }
+  },
+  {
+    policies: {
+      "EnableTrackingProtection": {
+        "Value": false,
+        "Locked": true
+      }
+    },
+    lockedPrefs: {
+      "privacy.trackingprotection.enabled": false,
+      "privacy.trackingprotection.pbmode.enabled": false,
+    }
+  },
+];
+
+add_task(async function test_policy_remember_passwords() {
+  for (let test of POLICIES_TESTS) {
+    await setupPolicyEngineWithJson({
+      "policies": test.policies
+    });
+
+    info("Checking policy: " + Object.keys(test.policies)[0]);
+
+    for (let [prefName, prefValue] of Object.entries(test.lockedPrefs || {})) {
+      checkLockedPref(prefName, prefValue);
+    }
+
+    for (let [prefName, prefValue] of Object.entries(test.unlockedPrefs || {})) {
+      checkUnlockedPref(prefName, prefValue);
+    }
+  }
+ });
--- a/browser/components/enterprisepolicies/tests/browser/browser_policy_clear_blocked_cookies.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policy_clear_blocked_cookies.js
@@ -1,14 +1,13 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 "use strict";
 
 add_task(async function setup() {
-  EnterprisePolicyTesting.resetRunOnceState();
   const expiry = Date.now() + 24 * 60 * 60;
   Services.cookies.add("example.com", "/", "secure", "true", true, false, false, expiry, {});
   Services.cookies.add("example.com", "/", "insecure", "true", false, false, false, expiry, {});
   Services.cookies.add("example.org", "/", "secure", "true", true, false, false, expiry, {});
   Services.cookies.add("example.org", "/", "insecure", "true", false, false, false, expiry, {});
   Services.cookies.add("example.net", "/", "secure", "true", true, false, false, expiry, {});
   await setupPolicyEngineWithJson({
     "policies": {
@@ -54,10 +53,9 @@ add_task(async function test_cookies_for
   is(JSON.stringify(cookies), JSON.stringify(expected),
      "All stored cookies for blocked origins should be cleared");
 });
 
 add_task(function teardown() {
   for (let host of ["example.com", "example.org", "example.net"]) {
     Services.cookies.removeCookiesWithOriginAttributes("{}", host);
   }
-  EnterprisePolicyTesting.resetRunOnceState();
 });
--- a/browser/components/enterprisepolicies/tests/browser/browser_policy_default_browser_check.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policy_default_browser_check.js
@@ -19,13 +19,9 @@ add_task(async function test_default_bro
   });
 
   is(ShellService.shouldCheckDefaultBrowser, false, "Policy changed it to not check");
 
   // Try to change it to true and check that it doesn't take effect
   ShellService.shouldCheckDefaultBrowser = true;
 
   is(ShellService.shouldCheckDefaultBrowser, false, "Policy is enforced");
-
-  // Unlock the pref because if it stays locked, and this test runs twice in a row,
-  // the first sanity check will fail.
-  Services.prefs.unlockPref("browser.shell.checkDefaultBrowser");
 });
deleted file mode 100644
--- a/browser/components/enterprisepolicies/tests/browser/browser_policy_disable_formhistory.js
+++ /dev/null
@@ -1,15 +0,0 @@
-/* Any copyright is dedicated to the Public Domain.
- * http://creativecommons.org/publicdomain/zero/1.0/ */
-
-"use strict";
-
-add_task(async function test_policy_disable_formhistory() {
-  await setupPolicyEngineWithJson({
-    "policies": {
-      "DisableFormHistory": true
-    }
-  });
-
-  is(Services.prefs.getBoolPref("browser.formfill.enable"), false, "FormHistory has been disabled");
-  is(Services.prefs.prefIsLocked("browser.formfill.enable"), true, "FormHistory pref has been locked");
-});
--- a/browser/components/enterprisepolicies/tests/browser/browser_policy_disable_fxaccounts.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policy_disable_fxaccounts.js
@@ -8,16 +8,9 @@ add_task(async function test_policy_disa
 
   await setupPolicyEngineWithJson({
     "policies": {
       "DisableFirefoxAccounts": true
     }
   });
 
   is(gSync.SYNC_ENABLED, false, "Sync is disabled after setting the policy.");
-
-  // Manually clean-up the change made by the policy engine.
-  // This is needed in case this test runs twice in a row
-  // (such as in test-verify), in order for the first check
-  // to pass again.
-  Services.prefs.unlockPref("identity.fxaccounts.enabled");
-  Services.prefs.getDefaultBranch("").setBoolPref("identity.fxaccounts.enabled", true);
 });
--- a/browser/components/enterprisepolicies/tests/browser/browser_policy_disable_pocket.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policy_disable_pocket.js
@@ -19,18 +19,10 @@ add_task(async function test_disable_fir
 
     await setupPolicyEngineWithJson({
       "policies": {
         "DisablePocket": true
       }
     });
 
     await checkPocket(false);
-
-    // Clear the change made by the engine to restore Pocket.
-    // This is needed in case this test runs twice in a row
-    // (such as in test-verify), in order for the first sanity check
-    // to pass again.
-    await setupPolicyEngineWithJson("");
-    Services.prefs.unlockPref(PREF_POCKET);
-    Services.prefs.getDefaultBranch("").setBoolPref(PREF_POCKET, true);
   });
 });
deleted file mode 100644
--- a/browser/components/enterprisepolicies/tests/browser/browser_policy_enable_tracking_protection.js
+++ /dev/null
@@ -1,36 +0,0 @@
-/* Any copyright is dedicated to the Public Domain.
- * http://creativecommons.org/publicdomain/zero/1.0/ */
-
-"use strict";
-
-add_task(async function test_policy_enable_tracking_protection1() {
-  await setupPolicyEngineWithJson({
-    "policies": {
-      "EnableTrackingProtection": {
-        "Value": true
-      }
-    }
-  });
-
-  is(Services.prefs.getBoolPref("privacy.trackingprotection.enabled"), true, "Tracking protection has been enabled by default.");
-  is(Services.prefs.getBoolPref("privacy.trackingprotection.pbmode.enabled"), true, "Tracking protection has been enabled by default in private browsing mode.");
-  is(Services.prefs.prefIsLocked("privacy.trackingprotection.enabled"), false, "Tracking protection pref is not locked.");
-});
-
-add_task(async function test_policy_enable_tracking_protection_locked() {
-  await setupPolicyEngineWithJson({
-    "policies": {
-      "EnableTrackingProtection": {
-        "Value": false,
-        "Locked": true
-      }
-    }
-  });
-
-  is(Services.prefs.getBoolPref("privacy.trackingprotection.enabled"), false, "Tracking protection has been disabled by default.");
-  is(Services.prefs.getBoolPref("privacy.trackingprotection.pbmode.enabled"), false, "Tracking protection has been disabled by default in private browsing mode.");
-  is(Services.prefs.prefIsLocked("privacy.trackingprotection.enabled"), true, "Tracking protection pref is locked.");
-
-  Services.prefs.unlockPref("privacy.trackingprotection.enabled");
-  Services.prefs.unlockPref("privacy.trackingprotection.pbmode.enabled");
-});
--- a/browser/components/enterprisepolicies/tests/browser/browser_policy_proxy.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policy_proxy.js
@@ -1,38 +1,12 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 "use strict";
 
-function checkPref(prefName, expectedValue, expectedLockedness) {
-  let prefType, prefValue;
-  switch (typeof(expectedValue)) {
-    case "boolean":
-      prefType = Services.prefs.PREF_BOOL;
-      prefValue = Services.prefs.getBoolPref(prefName);
-      break;
-
-    case "number":
-      prefType = Services.prefs.PREF_INT;
-      prefValue = Services.prefs.getIntPref(prefName);
-      break;
-
-    case "string":
-      prefType = Services.prefs.PREF_STRING;
-      prefValue = Services.prefs.getStringPref(prefName);
-      break;
-  }
-
-  if (expectedLockedness !== undefined) {
-    is(Services.prefs.prefIsLocked(prefName), expectedLockedness, `Pref ${prefName} is correctly locked`);
-  }
-  is(Services.prefs.getPrefType(prefName), prefType, `Pref ${prefName} has the correct type`);
-  is(prefValue, expectedValue, `Pref ${prefName} has the correct value`);
-}
-
 add_task(async function test_proxy_modes_and_autoconfig() {
   // Directly test the proxy Mode and AutoconfigURL parameters through
   // the API instead of the policy engine, because the test harness
   // uses these prefs, and changing them interfere with the harness.
 
   // Checks that every Mode value translates correctly to the expected pref value
   let { ProxyPolicies, PROXY_TYPES_MAP } = ChromeUtils.import("resource:///modules/policies/ProxyPolicies.jsm", {});
 
@@ -54,50 +28,50 @@ add_task(async function test_proxy_boole
     "policies": {
       "Proxy": {
         "UseProxyForDNS": false,
         "AutoLogin": false,
       }
     }
   });
 
-  checkPref("network.proxy.socks_remote_dns", false);
-  checkPref("signon.autologin.proxy", false);
+  checkUnlockedPref("network.proxy.socks_remote_dns", false);
+  checkUnlockedPref("signon.autologin.proxy", false);
 
   await setupPolicyEngineWithJson({
     "policies": {
       "Proxy": {
         "UseProxyForDNS": true,
         "AutoLogin": true,
       }
     }
   });
 
-  checkPref("network.proxy.socks_remote_dns", true);
-  checkPref("signon.autologin.proxy", true);
+  checkUnlockedPref("network.proxy.socks_remote_dns", true);
+  checkUnlockedPref("signon.autologin.proxy", true);
 });
 
 add_task(async function test_proxy_socks_and_passthrough() {
   await setupPolicyEngineWithJson({
     "policies": {
       "Proxy": {
         "SOCKSVersion": 4,
         "Passthrough": "a, b, c"
       }
     }
   });
 
-  checkPref("network.proxy.socks_version", 4);
-  checkPref("network.proxy.no_proxies_on", "a, b, c");
+  checkUnlockedPref("network.proxy.socks_version", 4);
+  checkUnlockedPref("network.proxy.no_proxies_on", "a, b, c");
 });
 
 add_task(async function test_proxy_addresses() {
   function checkProxyPref(proxytype, address, port) {
-    checkPref(`network.proxy.${proxytype}`, address);
-    checkPref(`network.proxy.${proxytype}_port`, port);
+    checkUnlockedPref(`network.proxy.${proxytype}`, address);
+    checkUnlockedPref(`network.proxy.${proxytype}_port`, port);
   }
 
   await setupPolicyEngineWithJson({
     "policies": {
       "Proxy": {
         "HTTPProxy": "http.proxy.example.com:10",
         "FTPProxy": "ftp.proxy.example.com:20",
         "SSLProxy": "ssl.proxy.example.com:30",
deleted file mode 100644
--- a/browser/components/enterprisepolicies/tests/browser/browser_policy_remember_passwords.js
+++ /dev/null
@@ -1,25 +0,0 @@
-/* Any copyright is dedicated to the Public Domain.
- * http://creativecommons.org/publicdomain/zero/1.0/ */
-
-"use strict";
-
-add_task(async function test_policy_remember_passwords() {
-  await setupPolicyEngineWithJson({
-    "policies": {
-      "RememberPasswords": false
-    }
-  });
-
-  is(Services.prefs.getBoolPref("signon.rememberSignons"), false, "Logins & Passwords have been disabled");
-  is(Services.prefs.prefIsLocked("signon.rememberSignons"), true, "Logins & Passwords pref has been locked");
-
-
-  await setupPolicyEngineWithJson({
-    "policies": {
-      "RememberPasswords": true
-    }
-  });
-
-  is(Services.prefs.getBoolPref("signon.rememberSignons"), true, "Logins & Passwords have been enabled");
-  is(Services.prefs.prefIsLocked("signon.rememberSignons"), true, "Logins & Passwords pref has been locked");
-});
--- a/browser/components/enterprisepolicies/tests/browser/browser_policy_set_homepage.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policy_set_homepage.js
@@ -1,42 +1,18 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 "use strict";
 
-// Prefs that will be touched by the test and need to be reset when the test
-// cleans up.
-const TOUCHED_PREFS = {
-  "browser.startup.homepage": "String",
-  "browser.startup.page": "Int",
-  "pref.browser.homepage.disable_button.current_page": "Bool",
-  "pref.browser.homepage.disable_button.bookmark_page": "Bool",
-  "pref.browser.homepage.disable_button.restore_default": "Bool",
-  "browser.policies.runOncePerModification.setHomepage": "String",
-};
-let ORIGINAL_PREF_VALUE = {};
-
-add_task(function read_original_pref_values() {
-  for (let pref in TOUCHED_PREFS) {
-    let prefType = TOUCHED_PREFS[pref];
-    ORIGINAL_PREF_VALUE[pref] =
-      Services.prefs[`get${prefType}Pref`](pref, undefined);
-  }
-});
 registerCleanupFunction(function restore_pref_values() {
-  let defaults = Services.prefs.getDefaultBranch("");
-  for (let pref in TOUCHED_PREFS) {
-    Services.prefs.unlockPref(pref);
-    Services.prefs.clearUserPref(pref);
-    let originalValue = ORIGINAL_PREF_VALUE[pref];
-    let prefType = TOUCHED_PREFS[pref];
-    if (originalValue !== undefined) {
-      defaults[`set${prefType}Pref`](pref, originalValue);
-    }
-  }
+  // These two prefs are set as user prefs in case the "Locked"
+  // option from this policy was not used. In this case, it won't
+  // be tracked nor restored by the PoliciesPrefTracker.
+  Services.prefs.clearUserPref("browser.startup.homepage");
+  Services.prefs.clearUserPref("browser.startup.page");
 });
 
 add_task(async function homepage_test_simple() {
   await setupPolicyEngineWithJson({
     "policies": {
       "Homepage": {
         "URL": "http://example1.com/"
       }
copy from browser/components/enterprisepolicies/tests/browser/disable_developer_tools/browser.ini
copy to browser/components/enterprisepolicies/tests/browser/disable_fxscreenshots/browser.ini
--- a/browser/components/enterprisepolicies/tests/browser/disable_developer_tools/browser.ini
+++ b/browser/components/enterprisepolicies/tests/browser/disable_fxscreenshots/browser.ini
@@ -1,8 +1,9 @@
 [DEFAULT]
 prefs =
   browser.policies.enabled=true
-  browser.policies.alternatePath='<test-root>/browser/components/enterprisepolicies/tests/browser/disable_developer_tools/config_disable_developer_tools.json'
+  browser.policies.alternatePath='<test-root>/browser/components/enterprisepolicies/tests/browser/disable_fxscreenshots/config_disable_fxscreenshots.json'
+  extensions.screenshots.disabled=false
 support-files =
-  config_disable_developer_tools.json
+  config_disable_fxscreenshots.json
 
-[browser_policy_disable_developer_tools.js]
+[browser_policy_disable_fxscreenshots.js]
rename from browser/components/enterprisepolicies/tests/browser/browser_policy_disable_fxscreenshots.js
rename to browser/components/enterprisepolicies/tests/browser/disable_fxscreenshots/browser_policy_disable_fxscreenshots.js
--- a/browser/components/enterprisepolicies/tests/browser/browser_policy_disable_fxscreenshots.js
+++ b/browser/components/enterprisepolicies/tests/browser/disable_fxscreenshots/browser_policy_disable_fxscreenshots.js
@@ -7,32 +7,23 @@ const PREF_DISABLE_FX_SCREENSHOTS = "ext
 
 async function checkScreenshots(shouldBeEnabled) {
   return BrowserTestUtils.waitForCondition(() => {
     return !!PageActions.actionForID("screenshots") == shouldBeEnabled;
   }, "Expecting screenshots to be " + shouldBeEnabled);
 }
 
 add_task(async function test_disable_firefox_screenshots() {
-  await BrowserTestUtils.withNewTab("data:text/html,Test", async function() {
-    // Firefox Screenshots are disabled in tests, so make sure we enable
-    // it first to ensure that the test is valid.
-    Services.prefs.setBoolPref(PREF_DISABLE_FX_SCREENSHOTS, false);
-    await checkScreenshots(true);
+  // Dynamically toggling the PREF_DISABLE_FX_SCREENSHOTS is very finicky, because
+  // that pref is being watched, and it makes the Firefox Screenshots system add-on
+  // to start or stop, causing intermittency.
+  //
+  // Firefox Screenshots is disabled by default on tests (in prefs_general.js).
+  // What we do here to test this policy is to enable it on this specific
+  // test folder (through browser.ini) and then we let the policy engine
+  // be responsible for disabling Firefox Screenshots in this case.
 
-    await setupPolicyEngineWithJson({
-      "policies": {
-        "DisableFirefoxScreenshots": true
-      }
-    });
+  is(Services.prefs.getBoolPref(PREF_DISABLE_FX_SCREENSHOTS), true, "Screenshots pref is disabled");
 
-    is(Services.policies.status, Services.policies.ACTIVE, "Policy engine is active");
-    await checkScreenshots(false);
-
-    // Clear the change we made and make sure it remains disabled.
-    await setupPolicyEngineWithJson("");
-
-    Services.prefs.unlockPref(PREF_DISABLE_FX_SCREENSHOTS);
-    Services.prefs.clearUserPref(PREF_DISABLE_FX_SCREENSHOTS);
-
+  await BrowserTestUtils.withNewTab("data:text/html,Test", async function() {
     await checkScreenshots(false);
   });
 });
new file mode 100644
--- /dev/null
+++ b/browser/components/enterprisepolicies/tests/browser/disable_fxscreenshots/config_disable_fxscreenshots.json
@@ -0,0 +1,5 @@
+{
+  "policies": {
+    "DisableFirefoxScreenshots": true
+  }
+}
--- a/browser/components/enterprisepolicies/tests/browser/head.js
+++ b/browser/components/enterprisepolicies/tests/browser/head.js
@@ -1,29 +1,46 @@
 /* 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/. */
 
 "use strict";
 
-const {EnterprisePolicyTesting} = ChromeUtils.import("resource://testing-common/EnterprisePolicyTesting.jsm", {});
+const {
+  EnterprisePolicyTesting,
+  PoliciesPrefTracker,
+} = ChromeUtils.import("resource://testing-common/EnterprisePolicyTesting.jsm", {});
+
+PoliciesPrefTracker.start();
 
 async function setupPolicyEngineWithJson(json, customSchema) {
+  PoliciesPrefTracker.restoreDefaultValues();
   if (typeof(json) != "object") {
     let filePath = getTestFilePath(json ? json : "non-existing-file.json");
     return EnterprisePolicyTesting.setupPolicyEngineWithJson(filePath, customSchema);
   }
   return EnterprisePolicyTesting.setupPolicyEngineWithJson(json, customSchema);
 }
 
+function checkLockedPref(prefName, prefValue) {
+  EnterprisePolicyTesting.checkPolicyPref(prefName, prefValue, true);
+}
+
+function checkUnlockedPref(prefName, prefValue) {
+  EnterprisePolicyTesting.checkPolicyPref(prefName, prefValue, false);
+}
+
 add_task(async function policies_headjs_startWithCleanSlate() {
   if (Services.policies.status != Ci.nsIEnterprisePolicies.INACTIVE) {
     await setupPolicyEngineWithJson("");
   }
   is(Services.policies.status, Ci.nsIEnterprisePolicies.INACTIVE, "Engine is inactive at the start of the test");
 });
 
 registerCleanupFunction(async function policies_headjs_finishWithCleanSlate() {
   if (Services.policies.status != Ci.nsIEnterprisePolicies.INACTIVE) {
     await setupPolicyEngineWithJson("");
   }
   is(Services.policies.status, Ci.nsIEnterprisePolicies.INACTIVE, "Engine is inactive at the end of the test");
+
+  EnterprisePolicyTesting.resetRunOnceState();
+  PoliciesPrefTracker.stop();
 });
--- a/browser/components/enterprisepolicies/tests/moz.build
+++ b/browser/components/enterprisepolicies/tests/moz.build
@@ -7,13 +7,14 @@
 with Files("**"):
     BUG_COMPONENT = ("Firefox", "General")
 
 BROWSER_CHROME_MANIFESTS += [
     'browser/browser.ini',
     'browser/disable_app_update/browser.ini',
     'browser/disable_default_bookmarks/browser.ini',
     'browser/disable_developer_tools/browser.ini',
+    'browser/disable_fxscreenshots/browser.ini',
 ]
 
 TESTING_JS_MODULES += [
     'EnterprisePolicyTesting.jsm',
 ]