Bug 1435977 - Make policies test begin and end with the police engine inactive. r=Gijs
authorFelipe Gomes <felipc@gmail.com>
Wed, 07 Feb 2018 20:37:54 -0200
changeset 447369 89c2810aac3e2a7405485c5cb17f54fca8f1480b
parent 447368 5ceb1098fef34d35a79e25a495949c61dfa34e61
child 447370 0b156d8a695ffc557fad0c49c47840aeeed5c316
push id135
push userfmarier@mozilla.com
push dateSat, 10 Feb 2018 02:56:15 +0000
reviewersGijs
bugs1435977
milestone60.0a1
Bug 1435977 - Make policies test begin and end with the police engine inactive. r=Gijs Some tests do a sanity check for features at the beginning. This works fine for a single test, and also from one test to another which are usually activating different policies. But this fails when the same test runs more than once, because the engine was left at the end in a state where that same policy was active. Note that this does not cover all the clean-up that a test must do to properly run more than once. For example, locked prefs are left locked, because the engine does support reversing all the policy code that ran. We might add more auto-reversal support in the future, but for now it's up to each test. MozReview-Commit-ID: KZ2LeKFTC0A
browser/components/enterprisepolicies/tests/browser/browser_policies_broken_json.js
browser/components/enterprisepolicies/tests/browser/browser_policies_popups_cookies_addons_flash.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_policy_disable_fxscreenshots.js
browser/components/enterprisepolicies/tests/browser/head.js
--- a/browser/components/enterprisepolicies/tests/browser/browser_policies_broken_json.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policies_broken_json.js
@@ -1,14 +1,10 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
 "use strict";
 
-add_task(async function test_clean_slate() {
-  await startWithCleanSlate();
-});
-
 add_task(async function test_broken_json() {
   await setupPolicyEngineWithJson("config_broken_json.json");
 
   is(Services.policies.status, Ci.nsIEnterprisePolicies.FAILED, "Engine was correctly set to the error state");
 });
--- a/browser/components/enterprisepolicies/tests/browser/browser_policies_popups_cookies_addons_flash.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policies_popups_cookies_addons_flash.js
@@ -2,20 +2,16 @@
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
 "use strict";
 
 function URI(str) {
   return Services.io.newURI(str);
 }
 
-add_task(async function test_start_with_disabled_engine() {
-  await startWithCleanSlate();
-});
-
 add_task(async function test_setup_preexisting_permissions() {
   // Pre-existing ALLOW permissions that should be overriden
   // with DENY.
   Services.perms.add(URI("https://www.pre-existing-allow.com"),
                      "popup",
                      Ci.nsIPermissionManager.ALLOW_ACTION,
                      Ci.nsIPermissionManager.EXPIRE_SESSION);
 
--- a/browser/components/enterprisepolicies/tests/browser/browser_policies_setAndLockPref_API.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policies_setAndLockPref_API.js
@@ -1,17 +1,13 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
 "use strict";
 
-add_task(async function test_clean_slate() {
-  await startWithCleanSlate();
-});
-
 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);
--- a/browser/components/enterprisepolicies/tests/browser/browser_policies_simple_policies.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policies_simple_policies.js
@@ -1,17 +1,13 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
 "use strict";
 
-add_task(async function test_clean_slate() {
-  await startWithCleanSlate();
-});
-
 add_task(async function test_simple_policies() {
   await ContentTask.spawn(gBrowser.selectedBrowser, null, async function() {
     // Initialize the service in the content process, in case it hasn't
     // already started.
     Services.policies;
   });
 
   let { Policies } = ChromeUtils.import("resource:///modules/policies/Policies.jsm", {});
--- a/browser/components/enterprisepolicies/tests/browser/browser_policy_disable_fxscreenshots.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policy_disable_fxscreenshots.js
@@ -8,19 +8,16 @@ 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() {
-    await setupPolicyEngineWithJson("");
-    is(Services.policies.status, Services.policies.INACTIVE, "Start with no policies");
-
     // 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);
 
     await setupPolicyEngineWithJson({
       "policies": {
         "DisableFirefoxScreenshots": true
--- a/browser/components/enterprisepolicies/tests/browser/head.js
+++ b/browser/components/enterprisepolicies/tests/browser/head.js
@@ -38,12 +38,21 @@ async function setupPolicyEngineWithJson
     let schemaModule = ChromeUtils.import("resource:///modules/policies/schema.jsm", {});
     schemaModule.schema = customSchema;
   }
 
   Services.obs.notifyObservers(null, "EnterprisePolicies:Restart");
   return promise;
 }
 
-async function startWithCleanSlate() {
-  await setupPolicyEngineWithJson("");
-  is(Services.policies.status, Ci.nsIEnterprisePolicies.INACTIVE, "Engine is inactive");
-}
+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");
+});