diff options
Diffstat (limited to 'toolkit/components/extensions/test/mochitest/test_chrome_ext_background_debug_global.html')
-rw-r--r-- | toolkit/components/extensions/test/mochitest/test_chrome_ext_background_debug_global.html | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/toolkit/components/extensions/test/mochitest/test_chrome_ext_background_debug_global.html b/toolkit/components/extensions/test/mochitest/test_chrome_ext_background_debug_global.html new file mode 100644 index 000000000..0edf5ea86 --- /dev/null +++ b/toolkit/components/extensions/test/mochitest/test_chrome_ext_background_debug_global.html @@ -0,0 +1,166 @@ +<!DOCTYPE HTML> +<html> +<head> + <title>WebExtension test</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script> + <script src="chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js"></script> + <script type="text/javascript" src="chrome_head.js"></script> + <script type="text/javascript" src="head.js"></script> + <link rel="stylesheet" href="chrome://mochikit/contents/tests/SimpleTest/test.css"/> +</head> +<body> + +<script type="text/javascript"> +"use strict"; + +Cu.import("resource://devtools/client/framework/ToolboxProcess.jsm"); +Cu.import("resource://gre/modules/AddonManager.jsm"); + +const { + XPIProvider, +} = Components.utils.import("resource://gre/modules/addons/XPIProvider.jsm"); + +/** + * This test is asserting that ext-backgroundPage.js successfully sets its + * debug global in the AddonWrapper provided by XPIProvider.jsm + * + * It does _not_ test any functionality in devtools and does not guarantee + * debugging is actually working correctly end-to-end. + */ + +function background() { + window.testThing = "test!"; + browser.test.notifyPass("background script ran"); +} + +const ID = "debug@tests.mozilla.org"; +let extensionData = { + useAddonManager: "temporary", + background, + manifest: { + applications: {gecko: {id: ID}}, + }, +}; + +add_task(function* () { + let extension = ExtensionTestUtils.loadExtension(extensionData); + yield extension.startup(); + + yield extension.awaitFinish("background script ran"); + + yield new Promise(function(resolve) { + window.BrowserToolboxProcess.emit("connectionchange", "opened", { + setAddonOptions(id, options) { + if (id === ID) { + let context = Cu.waiveXrays(options.global); + ok(context.chrome, "global context has a chrome object"); + ok(context.browser, "global context has a browser object"); + is("test!", context.testThing, "global context is the background script context"); + resolve(); + } + }, + }); + }); + + let addon = yield new Promise((resolve, reject) => { + AddonManager.getAddonByID(ID, addon => addon ? resolve(addon) : reject()); + }); + + ok(addon, `Got the addon wrapper for ${addon.id}`); + + function waitForDebugGlobalChanges(times, initialAddonInstanceID) { + return new Promise((resolve) => { + AddonManager.addAddonListener({ + count: 0, + notNullGlobalsCount: 0, + undefinedPrivateWrappersCount: 0, + lastAddonInstanceID: initialAddonInstanceID, + onPropertyChanged(newAddon, changedPropNames) { + if (newAddon.id != addon.id || + !changedPropNames.includes("debugGlobal")) { + return; + } + + ok(!(newAddon.setDebugGlobal) && !(newAddon.getDebugGlobal), + "The addon wrapper should not be a PrivateWrapper"); + + let activeAddon = XPIProvider.activeAddons.get(addon.id); + + let addonInstanceID; + + if (!activeAddon) { + // The addon has been disable, the preferred global should be null + addonInstanceID = this.lastAddonInstanceID; + delete this.lastAddonInstanceID; + } else { + addonInstanceID = activeAddon.instanceID; + this.lastAddonInstanceID = addonInstanceID; + } + + ok(addonInstanceID, `Got the addon instanceID for ${addon.id}`); + + AddonManager.getAddonByInstanceID(addonInstanceID).then((privateWrapper) => { + this.count += 1; + + if (!privateWrapper) { + // The addon has been uninstalled + this.undefinedPrivateWrappersCount += 1; + } else { + ok((privateWrapper.getDebugGlobal), "Got the addon PrivateWrapper"); + + if (privateWrapper.getDebugGlobal()) { + this.notNullGlobalsCount += 1; + } + } + + if (this.count == times) { + AddonManager.removeAddonListener(this); + resolve({ + counters: { + count: this.count, + notNullGlobalsCount: this.notNullGlobalsCount, + undefinedPrivateWrappersCount: this.undefinedPrivateWrappersCount, + }, + lastAddonInstanceID: this.lastAddonInstanceID, + }); + } + }); + }, + }); + }); + } + + // two calls expected, one for the shutdown and one for the startup + // of the background page. + let waitForDebugGlobalChangesOnReload = waitForDebugGlobalChanges(2); + + info("Addon reload..."); + yield addon.reload(); + + info("Addon completed startup after reload"); + + let { + counters: reloadCounters, + lastAddonInstanceID, + } = yield waitForDebugGlobalChangesOnReload; + + isDeeply(reloadCounters, {count: 2, notNullGlobalsCount: 1, undefinedPrivateWrappersCount: 0}, + "Got the expected number of onPropertyChanged calls on reload"); + + // one more call expected for the shutdown. + let waitForDebugGlobalChangesOnShutdown = waitForDebugGlobalChanges(1, lastAddonInstanceID); + + info("extension unloading..."); + yield extension.unload(); + info("extension unloaded"); + + let {counters: unloadCounters} = yield waitForDebugGlobalChangesOnShutdown; + + isDeeply(unloadCounters, {count: 1, notNullGlobalsCount: 0, undefinedPrivateWrappersCount: 1}, + "Got the expected number of onPropertyChanged calls on shutdown"); +}); +</script> + +</body> +</html> |