Bug 1446508 - Move the helper function checkLockedPref to a common location to be used by more tests. r=MattN
MozReview-Commit-ID: Aih5aasTQaN
--- a/browser/components/enterprisepolicies/tests/EnterprisePolicyTesting.jsm
+++ b/browser/components/enterprisepolicies/tests/EnterprisePolicyTesting.jsm
@@ -1,16 +1,18 @@
/* 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"];
this.EnterprisePolicyTesting = {
// |json| must be an object representing the desired policy configuration, OR a
// path to the JSON file containing the policy configuration.
@@ -45,16 +47,24 @@ 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))
--- 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,40 @@
/* 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`);
-}
-
add_task(async function test_API_directly() {
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 +91,16 @@ 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;
});
--- 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",
--- a/browser/components/enterprisepolicies/tests/browser/head.js
+++ b/browser/components/enterprisepolicies/tests/browser/head.js
@@ -9,16 +9,24 @@ const {EnterprisePolicyTesting} = Chrome
async function setupPolicyEngineWithJson(json, customSchema) {
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() {