Bug 1434164 - Support inlining policies.json file on test files. r=Gijs
MozReview-Commit-ID: Ff4bQvkyGiV
--- a/browser/components/enterprisepolicies/tests/browser/browser.ini
+++ b/browser/components/enterprisepolicies/tests/browser/browser.ini
@@ -1,17 +1,14 @@
[DEFAULT]
prefs =
browser.policies.enabled=true
support-files =
head.js
- config_dont_check_default_browser.json
config_popups_cookies_addons_flash.json
- config_setAndLockPref.json
- config_simple_policies.json
config_broken_json.json
config_block_about_config.json
[browser_policies_broken_json.js]
[browser_policies_popups_cookies_addons_flash.js]
[browser_policies_setAndLockPref_API.js]
[browser_policies_simple_policies.js]
[browser_policies_validate_and_parse_API.js]
--- a/browser/components/enterprisepolicies/tests/browser/browser_policies_setAndLockPref_API.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policies_setAndLockPref_API.js
@@ -90,18 +90,26 @@ add_task(async function test_API_through
Policies.string_policy = {
onBeforeUIStartup(manager, param) {
setAndLockPref("policies.test2.stringPref", param);
}
};
await setupPolicyEngineWithJson(
- "config_setAndLockPref.json",
- /* custom schema */
+ // policies.json
+ {
+ "policies": {
+ "bool_policy": true,
+ "int_policy": 42,
+ "string_policy": "policies test 2"
+ }
+ },
+
+ // custom schema
{
properties: {
"bool_policy": {
"type": "boolean"
},
"int_policy": {
"type": "integer"
--- a/browser/components/enterprisepolicies/tests/browser/browser_policies_simple_policies.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policies_simple_policies.js
@@ -47,18 +47,27 @@ add_task(async function test_simple_poli
Policies.simple_policy3 = {
onAllWindowsRestored(manager, param) {
is(param, false, "Param matches what was passed in config file");
policy3Ran = true;
}
};
await setupPolicyEngineWithJson(
- "config_simple_policies.json",
- /* custom schema */
+ // policies.json
+ {
+ "policies": {
+ "simple_policy0": true,
+ "simple_policy1": true,
+ "simple_policy2": true,
+ "simple_policy3": false
+ }
+ },
+
+ // custom schema
{
properties: {
"simple_policy0": {
"type": "boolean"
},
"simple_policy1": {
"type": "boolean"
--- a/browser/components/enterprisepolicies/tests/browser/browser_policy_default_browser_check.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policy_default_browser_check.js
@@ -8,17 +8,21 @@ const { ShellService } = Cu.import("reso
add_task(async function test_default_browser_check() {
ShellService._checkedThisSession = false;
// On a normal profile, the default is true. However, this gets set to false on the
// testing profile. Let's start with true for a sanity check.
ShellService.shouldCheckDefaultBrowser = true;
is(ShellService.shouldCheckDefaultBrowser, true, "Sanity check");
- await setupPolicyEngineWithJson("config_dont_check_default_browser.json");
+ await setupPolicyEngineWithJson({
+ "policies": {
+ "dont_check_default_browser": true
+ }
+ });
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");
deleted file mode 100644
--- a/browser/components/enterprisepolicies/tests/browser/config_dont_check_default_browser.json
+++ /dev/null
@@ -1,5 +0,0 @@
-{
- "policies": {
- "dont_check_default_browser": true
- }
-}
deleted file mode 100644
--- a/browser/components/enterprisepolicies/tests/browser/config_setAndLockPref.json
+++ /dev/null
@@ -1,7 +0,0 @@
-{
- "policies": {
- "bool_policy": true,
- "int_policy": 42,
- "string_policy": "policies test 2"
- }
-}
deleted file mode 100644
--- a/browser/components/enterprisepolicies/tests/browser/config_simple_policies.json
+++ /dev/null
@@ -1,8 +0,0 @@
-{
- "policies": {
- "simple_policy0": true,
- "simple_policy1": true,
- "simple_policy2": true,
- "simple_policy3": false
- }
-}
--- a/browser/components/enterprisepolicies/tests/browser/head.js
+++ b/browser/components/enterprisepolicies/tests/browser/head.js
@@ -1,16 +1,31 @@
/* 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";
-async function setupPolicyEngineWithJson(jsonName, customSchema) {
- let filePath = getTestFilePath(jsonName ? jsonName : "non-existing-file.json");
+XPCOMUtils.defineLazyModuleGetter(this, "FileTestUtils",
+ "resource://testing-common/FileTestUtils.jsm");
+
+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 = getTestFilePath(json ? json : "non-existing-file.json");
+ }
+
Services.prefs.setStringPref("browser.policies.alternatePath", filePath);
let resolve = null;
let promise = new Promise((r) => resolve = r);
Services.obs.addObserver(function observer() {
Services.obs.removeObserver(observer, "EnterprisePolicies:AllPoliciesApplied");
resolve();