Bug 1429150 - Add a test for disabling Firefox app updates draft
authorKirk Steuber <ksteuber@mozilla.com>
Wed, 07 Feb 2018 15:40:54 -0800
changeset 752626 f71f6ce8655a6b3f0012d0791c938ff6fad89646
parent 752625 eb38c57de57a288f67156271c09bd867b1fb2643
push id98323
push userksteuber@mozilla.com
push dateThu, 08 Feb 2018 17:59:14 +0000
bugs1429150
milestone60.0a1
Bug 1429150 - Add a test for disabling Firefox app updates MozReview-Commit-ID: 1WfCcG1R5ay
browser/components/enterprisepolicies/EnterprisePolicies.js
browser/components/enterprisepolicies/tests/browser/disable_app_update/browser.ini
browser/components/enterprisepolicies/tests/browser/disable_app_update/browser_policy_disable_app_update.js
browser/components/enterprisepolicies/tests/browser/disable_app_update/config_disable_app_update.json
browser/components/enterprisepolicies/tests/moz.build
--- a/browser/components/enterprisepolicies/EnterprisePolicies.js
+++ b/browser/components/enterprisepolicies/EnterprisePolicies.js
@@ -15,16 +15,22 @@ XPCOMUtils.defineLazyModuleGetters(this,
 // This is the file that will be searched for in the
 // ${InstallDir}/distribution folder.
 const POLICIES_FILENAME = "policies.json";
 
 // For easy testing, modify the helpers/sample.json file,
 // and set PREF_ALTERNATE_PATH in firefox.js as:
 // /your/repo/browser/components/enterprisepolicies/helpers/sample.json
 const PREF_ALTERNATE_PATH     = "browser.policies.alternatePath";
+// For testing, we may want to set PREF_ALTERNATE_PATH to point to a file
+// relative to the test root directory. In order to enable this, the string
+// below may be placed at the beginning of that preference value and it will
+// be replaced with the path to the test root directory.
+const MAGIC_TEST_ROOT_PREFIX  = "<test-root>";
+const PREF_TEST_ROOT          = "mochitest.testRoot";
 
 // This pref is meant to be temporary: it will only be used while we're
 // testing this feature without rolling it out officially. When the
 // policy engine is released, this pref should be removed.
 const PREF_ENABLED            = "browser.policies.enabled";
 const PREF_LOGLEVEL           = "browser.policies.loglevel";
 
 XPCOMUtils.defineLazyGetter(this, "log", () => {
@@ -314,16 +320,28 @@ class JSONPoliciesProvider {
     let alternatePath = Services.prefs.getStringPref(PREF_ALTERNATE_PATH, "");
 
     if (alternatePath && !configFile.exists()) {
       // We only want to use the alternate file path if the file on the install
       // folder doesn't exist. Otherwise it'd be possible for a user to override
       // the admin-provided policies by changing the user-controlled prefs.
       // This pref is only meant for tests, so it's fine to use this extra
       // synchronous configFile.exists() above.
+      if (alternatePath.startsWith(MAGIC_TEST_ROOT_PREFIX)) {
+        // Intentionally not using a default value on this pref lookup. If no
+        // test root is set, we are not currently testing and this function
+        // should throw rather than returning something.
+        let testRoot = Services.prefs.getStringPref(PREF_TEST_ROOT);
+        let relativePath = alternatePath.substring(MAGIC_TEST_ROOT_PREFIX.length);
+        if (AppConstants.platform == "win") {
+          relativePath = relativePath.replace(/\//g, "\\");
+        }
+        alternatePath = testRoot + relativePath;
+      }
+
       configFile = Cc["@mozilla.org/file/local;1"]
                      .createInstance(Ci.nsIFile);
       configFile.initWithPath(alternatePath);
     }
 
     return configFile;
   }
 
new file mode 100644
--- /dev/null
+++ b/browser/components/enterprisepolicies/tests/browser/disable_app_update/browser.ini
@@ -0,0 +1,8 @@
+[DEFAULT]
+prefs =
+  browser.policies.enabled=true
+  browser.policies.alternatePath='<test-root>/browser/components/enterprisepolicies/tests/browser/disable_app_update/config_disable_app_update.json'
+support-files =
+  config_disable_app_update.json
+
+[browser_policy_disable_app_update.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/enterprisepolicies/tests/browser/disable_app_update/browser_policy_disable_app_update.js
@@ -0,0 +1,14 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+var updateService = Cc["@mozilla.org/updates/update-service;1"].
+                    getService(Ci.nsIApplicationUpdateService);
+
+add_task(async function test_updates_post_policy() {
+  is(Services.policies.isAllowed("appUpdate"), false,
+     "appUpdate should be disabled by policy.");
+
+  is(updateService.canCheckForUpdates, false,
+     "Should not be able to check for updates with DisableAppUpdate enabled.");
+});
new file mode 100644
--- /dev/null
+++ b/browser/components/enterprisepolicies/tests/browser/disable_app_update/config_disable_app_update.json
@@ -0,0 +1,5 @@
+{
+  "policies": {
+    "DisableAppUpdate": true
+  }
+}
--- a/browser/components/enterprisepolicies/tests/moz.build
+++ b/browser/components/enterprisepolicies/tests/moz.build
@@ -3,10 +3,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/.
 
 with Files("**"):
     BUG_COMPONENT = ("Firefox", "General")
 
 BROWSER_CHROME_MANIFESTS += [
-    'browser/browser.ini'
+    'browser/browser.ini',
+    'browser/disable_app_update/browser.ini'
 ]