head.js (5411B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const lazy = {}; 8 9 const { Preferences } = ChromeUtils.importESModule( 10 "resource://gre/modules/Preferences.sys.mjs" 11 ); 12 const { SearchSettings } = ChromeUtils.importESModule( 13 "moz-src:///toolkit/components/search/SearchSettings.sys.mjs" 14 ); 15 const { updateAppInfo, getAppInfo } = ChromeUtils.importESModule( 16 "resource://testing-common/AppInfo.sys.mjs" 17 ); 18 const { FileTestUtils } = ChromeUtils.importESModule( 19 "resource://testing-common/FileTestUtils.sys.mjs" 20 ); 21 const { PermissionTestUtils } = ChromeUtils.importESModule( 22 "resource://testing-common/PermissionTestUtils.sys.mjs" 23 ); 24 ChromeUtils.defineESModuleGetters(lazy, { 25 SearchTestUtils: "resource://testing-common/SearchTestUtils.sys.mjs", 26 }); 27 const { EnterprisePolicyTesting } = ChromeUtils.importESModule( 28 "resource://testing-common/EnterprisePolicyTesting.sys.mjs" 29 ); 30 const { ExtensionTestUtils } = ChromeUtils.importESModule( 31 "resource://testing-common/ExtensionXPCShellUtils.sys.mjs" 32 ); 33 34 updateAppInfo({ 35 name: "XPCShell", 36 ID: "xpcshell@tests.mozilla.org", 37 version: "48", 38 platformVersion: "48", 39 }); 40 41 // This initializes the policy engine for xpcshell tests 42 let policies = Cc["@mozilla.org/enterprisepolicies;1"].getService( 43 Ci.nsIObserver 44 ); 45 policies.observe(null, "policies-startup", null); 46 47 SearchSettings.SETTINGS_INVALIDATION_DELAY = 100; 48 49 async function setupPolicyEngineWithJson(json, customSchema) { 50 if (typeof json != "object") { 51 let filePath = do_get_file(json ? json : "non-existing-file.json").path; 52 return EnterprisePolicyTesting.setupPolicyEngineWithJson( 53 filePath, 54 customSchema 55 ); 56 } 57 return EnterprisePolicyTesting.setupPolicyEngineWithJson(json, customSchema); 58 } 59 60 /** 61 * Loads a new enterprise policy, and re-initialise the search service 62 * with the new policy. Also waits for the search service to write the settings 63 * file to disk. 64 * 65 * @param {object} json 66 * The enterprise policy to use. 67 * @param {object} customSchema 68 * A custom schema to use to validate the enterprise policy. 69 */ 70 async function setupPolicyEngineWithJsonWithSearch(json, customSchema) { 71 Services.search.wrappedJSObject.reset(); 72 if (typeof json != "object") { 73 let filePath = do_get_file(json ? json : "non-existing-file.json").path; 74 await EnterprisePolicyTesting.setupPolicyEngineWithJson( 75 filePath, 76 customSchema 77 ); 78 } else { 79 await EnterprisePolicyTesting.setupPolicyEngineWithJson(json, customSchema); 80 } 81 let settingsWritten = lazy.SearchTestUtils.promiseSearchNotification( 82 "write-settings-to-disk-complete" 83 ); 84 await Services.search.init(); 85 await settingsWritten; 86 } 87 88 function checkLockedPref(prefName, prefValue) { 89 equal( 90 Preferences.locked(prefName), 91 true, 92 `Pref ${prefName} is correctly locked` 93 ); 94 strictEqual( 95 Preferences.get(prefName), 96 prefValue, 97 `Pref ${prefName} has the correct value` 98 ); 99 } 100 101 function checkUnlockedPref(prefName, prefValue) { 102 equal( 103 Preferences.locked(prefName), 104 false, 105 `Pref ${prefName} is correctly unlocked` 106 ); 107 strictEqual( 108 Preferences.get(prefName), 109 prefValue, 110 `Pref ${prefName} has the correct value` 111 ); 112 } 113 114 function checkUserPref(prefName, prefValue) { 115 strictEqual( 116 Preferences.get(prefName), 117 prefValue, 118 `Pref ${prefName} has the correct value` 119 ); 120 } 121 122 function checkClearPref(prefName) { 123 equal( 124 Services.prefs.prefHasUserValue(prefName), 125 false, 126 `Pref ${prefName} has no user value` 127 ); 128 } 129 130 function checkDefaultPref(prefName, prefValue) { 131 let defaultPrefBranch = Services.prefs.getDefaultBranch(""); 132 let prefType = defaultPrefBranch.getPrefType(prefName); 133 notEqual( 134 prefType, 135 Services.prefs.PREF_INVALID, 136 `Pref ${prefName} is set on the default branch` 137 ); 138 strictEqual( 139 Preferences.get(prefName), 140 prefValue, 141 `Pref ${prefName} has the correct value` 142 ); 143 } 144 145 function checkUnsetPref(prefName) { 146 let defaultPrefBranch = Services.prefs.getDefaultBranch(""); 147 let prefType = defaultPrefBranch.getPrefType(prefName); 148 equal( 149 prefType, 150 Services.prefs.PREF_INVALID, 151 `Pref ${prefName} is not set on the default branch` 152 ); 153 } 154 155 async function assertManagementAPIInstallType(addonId, expectedInstallType) { 156 const addon = await AddonManager.getAddonByID(addonId); 157 const expectInstalledByPolicy = expectedInstallType === "admin"; 158 equal( 159 addon.isInstalledByEnterprisePolicy, 160 expectInstalledByPolicy, 161 `Addon should ${ 162 expectInstalledByPolicy ? "be" : "NOT be" 163 } marked as installed by enterprise policy` 164 ); 165 const policy = WebExtensionPolicy.getByID(addonId); 166 const pageURL = policy.extension.baseURI.resolve( 167 "_generated_background_page.html" 168 ); 169 const page = await ExtensionTestUtils.loadContentPage(pageURL); 170 const { id, installType } = await page.spawn([], async () => { 171 const res = await this.content.wrappedJSObject.browser.management.getSelf(); 172 return { id: res.id, installType: res.installType }; 173 }); 174 await page.close(); 175 Assert.equal(id, addonId, "Got results for the expected addon id"); 176 Assert.equal( 177 installType, 178 expectedInstallType, 179 "Got the expected installType on policy installed extension" 180 ); 181 }