/* Any copyright is dedicated to the Public Domain. http://creativecommons.org/publicdomain/zero/1.0/ */ /* globals browser */ "use strict"; /** * Test helpers shared by the devtools server xpcshell tests related to webextensions. */ const { FileUtils } = ChromeUtils.importESModule( "resource://gre/modules/FileUtils.sys.mjs" ); const { ExtensionTestUtils } = ChromeUtils.importESModule( "resource://testing-common/ExtensionXPCShellUtils.sys.mjs" ); const { CommandsFactory, } = require("resource://devtools/shared/commands/commands-factory.js"); /** * Loads and starts up a test extension given the provided extension configuration. * * @param {Object} extConfig - The extension configuration object * @return {ExtensionWrapper} extension - Resolves with an extension object once the * extension has started up. */ async function startupExtension(extConfig) { const extension = ExtensionTestUtils.loadExtension(extConfig); await extension.startup(); return extension; } exports.startupExtension = startupExtension; /** * Initializes the extensionStorage actor for a given extension. This is effectively * what happens when the addon storage panel is opened in the browser. * * @param {String} - id, The addon id * @return {Object} - Resolves with the DevTools "commands" objact and the extensionStorage * resource/front. */ async function openAddonStoragePanel(id) { const commands = await CommandsFactory.forAddon(id); await commands.targetCommand.startListening(); // Fetch the EXTENSION_STORAGE resource. // Unfortunately, we can't use resourceCommand.waitForNextResource as it would destroy // the actor by immediately unwatching for the resource type. const extensionStorage = await new Promise(resolve => { commands.resourceCommand.watchResources( [commands.resourceCommand.TYPES.EXTENSION_STORAGE], { onAvailable(resources) { resolve(resources[0]); }, } ); }); return { commands, extensionStorage }; } exports.openAddonStoragePanel = openAddonStoragePanel; /** * Builds the extension configuration object passed into ExtensionTestUtils.loadExtension * * @param {Object} options - Options, if any, to add to the configuration * @param {Function} options.background - A function comprising the test extension's * background script if provided * @param {Object} options.files - An object whose keys correspond to file names and * values map to the file contents * @param {Object} options.manifest - An object representing the extension's manifest * @return {Object} - The extension configuration object */ function getExtensionConfig(options = {}) { const { manifest, ...otherOptions } = options; const baseConfig = { manifest: { ...manifest, permissions: ["storage"], }, useAddonManager: "temporary", }; return { ...baseConfig, ...otherOptions, }; } exports.getExtensionConfig = getExtensionConfig; /** * Shared files for a test extension that has no background page but adds storage * items via a transient extension page in a tab */ const ext_no_bg = { files: { "extension_page_in_tab.html": `