head.js (3702B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 /* exported assertNewTabResourceMapping */ 7 8 /** 9 * This head.js file is shared between the browser/extensions/newtab xpcshell 10 * tests as well as browser/components/newtab xpcshell tests. 11 */ 12 13 const { AppConstants } = ChromeUtils.importESModule( 14 "resource://gre/modules/AppConstants.sys.mjs" 15 ); 16 17 const { AddonTestUtils } = ChromeUtils.importESModule( 18 "resource://testing-common/AddonTestUtils.sys.mjs" 19 ); 20 21 const { ExtensionTestUtils } = ChromeUtils.importESModule( 22 "resource://testing-common/ExtensionXPCShellUtils.sys.mjs" 23 ); 24 25 ChromeUtils.defineESModuleGetters(this, { 26 AboutNewTab: "resource:///modules/AboutNewTab.sys.mjs", 27 AddonManager: "resource://gre/modules/AddonManager.sys.mjs", 28 AddonManagerPrivate: "resource://gre/modules/AddonManager.sys.mjs", 29 ExtensionParent: "resource://gre/modules/ExtensionParent.sys.mjs", 30 FileUtils: "resource://gre/modules/FileUtils.sys.mjs", 31 }); 32 33 do_get_profile(); 34 35 // The following initializations are necessary in order to install the newtab 36 // built-in addon, if we're configured to do so. 37 ExtensionTestUtils.init(this); 38 AddonTestUtils.init(this); 39 AddonTestUtils.overrideCertDB(); 40 41 /** 42 * Finds the built-in newtab addon code in the runtime environment directory 43 * and then installs it. 44 * 45 * @returns {Promise<undefined>} 46 * Resolves once the addon has been installed. 47 */ 48 async function loadExtension() { 49 const scopes = AddonManager.SCOPE_PROFILE | AddonManager.SCOPE_APPLICATION; 50 Services.prefs.setIntPref("extensions.enabledScopes", scopes); 51 52 const EXTENSION_ID = "newtab@mozilla.org"; 53 const builtinsConfig = await fetch( 54 "chrome://browser/content/built_in_addons.json" 55 ).then(res => res.json()); 56 57 await AddonTestUtils.overrideBuiltIns({ 58 system: [], 59 builtins: builtinsConfig.builtins.filter( 60 entry => entry.addon_id === EXTENSION_ID 61 ), 62 }); 63 64 await AddonTestUtils.promiseShutdownManager({ clearL10nRegistry: false }); 65 await AddonTestUtils.promiseStartupManager(); 66 67 const addon = await AddonManager.getAddonByID(EXTENSION_ID); 68 Assert.ok(addon, "Expect newtab addon to be found"); 69 } 70 71 add_setup(async function head_initialize() { 72 AddonTestUtils.createAppInfo( 73 "xpcshell@tests.mozilla.org", 74 "XPCShell", 75 "1", 76 "142" 77 ); 78 await AddonTestUtils.promiseStartupManager(); 79 80 if (AppConstants.BROWSER_NEWTAB_AS_ADDON) { 81 Services.prefs.setBoolPref("extensions.experiments.enabled", true); 82 await loadExtension(); 83 } 84 AboutNewTab.init(); 85 }); 86 87 /** 88 * Asserts that New Tab resource and chrome URI have been 89 * mapped to the expected rootURI. 90 91 * @param {string} [expectedRootURISpec] 92 * A optional root URI spec to derive the expected resource://newtab 93 * and chrome://newtab resource mapping to expect to have been registered. 94 * Defaults to the built-in newtab add-on root URI. 95 */ 96 function assertNewTabResourceMapping(expectedRootURISpec = null) { 97 const resProto = Cc[ 98 "@mozilla.org/network/protocol;1?name=resource" 99 ].getService(Ci.nsIResProtocolHandler); 100 const chromeRegistry = Cc["@mozilla.org/chrome/chrome-registry;1"].getService( 101 Ci.nsIChromeRegistry 102 ); 103 const expectedSpec = 104 expectedRootURISpec ?? 105 `${resProto.getSubstitution("builtin-addons").spec}newtab/`; 106 Assert.equal( 107 resProto.getSubstitution("newtab")?.spec, 108 expectedSpec, 109 "Got the expected resource://newtab/ substitution" 110 ); 111 Assert.equal( 112 chromeRegistry.convertChromeURL( 113 Services.io.newURI("chrome://newtab/content/css/") 114 )?.spec, 115 `${expectedSpec}data/css/`, 116 "Got the expected chrome://newtab/content substitution" 117 ); 118 }