gecko-dev/browser/components/enterprisepolicies/tests/EnterprisePolicyTesting.jsm
Kris Maglione e930b89c34 Bug 1514594: Part 3 - Change ChromeUtils.import API.
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8

This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:

  ChromeUtils.import("resource://gre/modules/Services.jsm");

is approximately the same as the following, in the new model:

  var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs

This was done using the followng script:

https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16750

--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 10:18:31 -08:00

153 lines
5.4 KiB
JavaScript

/* 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 {Preferences} = ChromeUtils.import("resource://gre/modules/Preferences.jsm");
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
const {OS} = ChromeUtils.import("resource://gre/modules/osfile.jsm");
const {Assert} = ChromeUtils.import("resource://testing-common/Assert.jsm");
ChromeUtils.defineModuleGetter(this, "FileTestUtils",
"resource://testing-common/FileTestUtils.jsm");
var EXPORTED_SYMBOLS = ["EnterprisePolicyTesting", "PoliciesPrefTracker"];
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
// at the end of the test run.
await OS.File.writeAtomic(filePath, JSON.stringify(json), {
encoding: "utf-8",
});
} else {
filePath = json;
}
Services.prefs.setStringPref("browser.policies.alternatePath", filePath);
let promise = new Promise(resolve => {
Services.obs.addObserver(function observer() {
Services.obs.removeObserver(observer, "EnterprisePolicies:AllPoliciesApplied");
resolve();
}, "EnterprisePolicies:AllPoliciesApplied");
});
// Clear any previously used custom schema
Cu.unload("resource:///modules/policies/schema.jsm");
if (customSchema) {
let schemaModule = ChromeUtils.import("resource:///modules/policies/schema.jsm", null);
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", null);
this._originalFunc = PoliciesBackstage.setDefaultPref;
PoliciesBackstage.setDefaultPref = this.hoistedSetDefaultPref.bind(this);
},
stop() {
this.restoreDefaultValues();
let PoliciesBackstage = ChromeUtils.import("resource:///modules/policies/Policies.jsm", null);
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);
} else {
stored.originalDefaultValue = undefined;
}
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 !== undefined) {
defaults.set(prefName, stored.originalDefaultValue);
} else {
Services.prefs.getDefaultBranch("").deleteBranch(prefName);
}
if (stored.originalUserValue !== undefined) {
Preferences.set(prefName, stored.originalUserValue);
}
}
this._originalValues.clear();
},
};