test_addon_debugging_connect.js (3420B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const { ExtensionTestUtils } = ChromeUtils.importESModule( 7 "resource://testing-common/ExtensionXPCShellUtils.sys.mjs" 8 ); 9 10 const lazy = {}; 11 ChromeUtils.defineESModuleGetters(lazy, { 12 ExtensionParent: "resource://gre/modules/ExtensionParent.sys.mjs", 13 }); 14 15 const { createAppInfo, promiseStartupManager } = AddonTestUtils; 16 17 AddonTestUtils.init(this); 18 createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "42"); 19 20 ExtensionTestUtils.init(this); 21 22 // Bug 1302702 - Test connect to a webextension addon 23 add_task( 24 { 25 // This test needs to run only when the extension are running in a separate 26 // child process, otherwise the thread actor would pause the main process and this 27 // test would get stuck. 28 skip_if: () => !WebExtensionPolicy.useRemoteWebExtensions, 29 }, 30 async function test_webextension_addon_debugging_connect() { 31 await promiseStartupManager(); 32 33 // Install and start a test webextension. 34 const extension = ExtensionTestUtils.loadExtension({ 35 useAddonManager: "temporary", 36 background() { 37 const { browser } = this; 38 browser.test.log("background script executed"); 39 // window is available in background scripts 40 // eslint-disable-next-line no-undef 41 browser.test.sendMessage("background page ready", window.location.href); 42 }, 43 }); 44 await extension.startup(); 45 const bgPageURL = await extension.awaitMessage("background page ready"); 46 47 const commands = await CommandsFactory.forAddon(extension.id); 48 const { targetCommand } = commands; 49 50 // Connect to the target addon actor and wait for the updated list of frames. 51 await targetCommand.startListening(); 52 const topTarget = targetCommand.targetFront; 53 const selectedTarget = targetCommand.selectedTargetFront; 54 55 equal( 56 topTarget.isFallbackExtensionDocument, 57 true, 58 "The top target is about the fallback document" 59 ); 60 equal( 61 selectedTarget.isFallbackExtensionDocument, 62 false, 63 "The background page target is automatically selected" 64 ); 65 equal(selectedTarget.url, bgPageURL, "The background page url is correct"); 66 67 const threadFront = await topTarget.getFront("thread"); 68 69 ok(threadFront, "Got a threadFront for the target addon"); 70 equal(threadFront.paused, false, "The addon threadActor isn't paused"); 71 72 equal( 73 lazy.ExtensionParent.DebugUtils.debugBrowserPromises.size, 74 1, 75 "The expected number of debug browser has been created by the addon actor" 76 ); 77 78 // Reload the addon through the RDP protocol. 79 await targetCommand.reloadTopLevelTarget(); 80 81 info("Wait background page to be fully reloaded"); 82 await extension.awaitMessage("background page ready"); 83 84 equal( 85 lazy.ExtensionParent.DebugUtils.debugBrowserPromises.size, 86 1, 87 "The number of debug browser has not been changed after an addon reload" 88 ); 89 90 await commands.destroy(); 91 92 // Check that if we close the debugging client without uninstalling the addon, 93 // the webextension debugging actor should release the debug browser. 94 equal( 95 lazy.ExtensionParent.DebugUtils.debugBrowserPromises.size, 96 0, 97 "The debug browser has been released when the RDP connection has been closed" 98 ); 99 100 await extension.unload(); 101 } 102 );