/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */ /* vim: set sts=2 sw=2 et tw=80: */ "use strict"; AddonTestUtils.createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1"); createHttpServer({ hosts: ["example.com"] }); add_setup(() => { // Need a profile to init Glean. do_get_profile(); Services.fog.initializeFOG(); }); async function test_mozExtensionGlobalMatch({ expectAllowed }) { const testMatchesWindowGlobal = async ({ allowed, extId, subframeURL }) => { const { MatchPatternSet, WebExtensionContentScript, WebExtensionPolicy } = Cu.getGlobalForObject(Services); const policy = WebExtensionPolicy.getByID(extId); const script = new WebExtensionContentScript(policy, { // This content script matches would be matching any extension document, // but we will be call matchesWindowGlobal only for a specific document // and so we expect the warning log to only be emitted for that // specific document (and not emitted for the other extension page). matches: new MatchPatternSet([policy.getURL("*")]), // The content script instance should be configured to match all // frames if we are testing a moz-extension subframe. allFrames: !!subframeURL, matchAboutBlank: false, }); let targetWin = this.content; if (subframeURL) { info("Creating moz-extension subframe"); const iframe = this.content.document.createElement("iframe"); iframe.src = subframeURL; const promiseFrameLoaded = new Promise(resolve => { iframe.addEventListener("load", resolve, { once: true }); }); this.content.document.body.append(iframe); info("Waiting for subframe to be fully loaded"); await promiseFrameLoaded; info("Subframe load is completed: " + iframe.contentDocument.documentURI); targetWin = iframe.contentWindow; } info( "Call WebExtensionContentScript.matchesWindowGlobal and wait for the console message" ); const results = await new Promise(resolve => { let matchResult; let listener = { QueryInterface: ChromeUtils.generateQI(["nsIConsoleListener"]), observe(msg) { info(`Got console message: ${msg.message}`); const msgFound = msg.message.match( /Content Script execution in moz-extension document has been deprecated/ ); if (msgFound) { Services.console.unregisterListener(this); resolve({ matchResult, consoleMsg: msg }); } }, }; Services.console.registerListener(listener); info(`Testing WebExtensionContentScript matchesWindowGlobal`); matchResult = script.matchesWindowGlobal(targetWin.windowGlobalChild); }); info("Assert matchesWindowGlobal result and logged warning messages"); Assert.equal( results.matchResult, allowed, `Got the expect matchesWindowGlobal result` ); const expectWarningOutcomeMsg = allowed ? "and will be removed in" : "and it has been blocked"; ok(results.consoleMsg, "Expect deprecation warning message to be logged"); ok( results.consoleMsg?.message.includes(expectWarningOutcomeMsg), `Expect the warning message to include "${expectWarningOutcomeMsg}"` ); const { documentURI } = targetWin.document; ok( results.consoleMsg.message.includes(`{file: "${documentURI}"`), `Expect the warning message to be associated to the extension page url: ${documentURI}` ); Assert.equal( results.consoleMsg?.logLevel, Ci.nsIConsoleMessage.warn, "Extpect log level to be 'warn'." ); Assert.ok( results.consoleMsg instanceof Ci.nsIScriptError, "Expect console message to be an instance of nsIScriptError" ); Assert.equal( results.consoleMsg.innerWindowID, targetWin.windowGlobalChild.innerWindowId, "Expect the warning to be associated to the right innerWindowID" ); }; const extension = ExtensionTestUtils.loadExtension({ manifest: { web_accessible_resources: ["extdoc.html"], }, files: { "extdoc.html": `