diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /devtools/shared/tests/xpcshell/test_console_filtering.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-upstream.tar.xz firefox-esr-upstream.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/shared/tests/xpcshell/test_console_filtering.js')
-rw-r--r-- | devtools/shared/tests/xpcshell/test_console_filtering.js | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/devtools/shared/tests/xpcshell/test_console_filtering.js b/devtools/shared/tests/xpcshell/test_console_filtering.js new file mode 100644 index 0000000000..74d4795895 --- /dev/null +++ b/devtools/shared/tests/xpcshell/test_console_filtering.js @@ -0,0 +1,156 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const { console, ConsoleAPI } = ChromeUtils.importESModule( + "resource://gre/modules/Console.sys.mjs" +); +const { + ConsoleAPIListener, +} = require("resource://devtools/server/actors/webconsole/listeners/console-api.js"); + +var seenMessages = 0; +var seenTypes = 0; + +var onConsoleAPICall = function (message) { + if (message.consoleID && message.consoleID == "addon/foo") { + Assert.equal(message.level, "warn"); + Assert.equal(message.arguments[0], "Warning from foo"); + seenTypes |= 1; + } else if (message.addonId == "bar") { + Assert.equal(message.level, "error"); + Assert.equal(message.arguments[0], "Error from bar"); + seenTypes |= 2; + } else { + Assert.equal(message.level, "log"); + Assert.equal(message.arguments[0], "Hello from default console"); + seenTypes |= 4; + } + seenMessages++; +}; + +let policy; +registerCleanupFunction(() => { + policy.active = false; +}); + +function createFakeAddonWindow({ addonId } = {}) { + const uuidGen = Services.uuid; + const uuid = uuidGen.generateUUID().number.slice(1, -1); + + if (policy) { + policy.active = false; + } + /* globals MatchPatternSet, WebExtensionPolicy */ + policy = new WebExtensionPolicy({ + id: addonId, + mozExtensionHostname: uuid, + baseURL: "file:///", + allowedOrigins: new MatchPatternSet([]), + localizeCallback() {}, + }); + policy.active = true; + + const baseURI = Services.io.newURI(`moz-extension://${uuid}/`); + const principal = Services.scriptSecurityManager.createContentPrincipal( + baseURI, + {} + ); + const chromeWebNav = Services.appShell.createWindowlessBrowser(true); + const { docShell } = chromeWebNav; + docShell.createAboutBlankContentViewer(principal, principal); + const addonWindow = docShell.contentViewer.DOMDocument.defaultView; + + return { addonWindow, chromeWebNav }; +} + +/** + * Tests that the consoleID property of the ConsoleAPI options gets passed + * through to console messages. + */ +function run_test() { + // console1 Test Console.sys.mjs messages tagged by the Addon SDK + // are still filtered correctly. + const console1 = new ConsoleAPI({ + consoleID: "addon/foo", + }); + + // console2 - WebExtension page's console messages tagged + // by 'originAttributes.addonId' are filtered correctly. + const { addonWindow, chromeWebNav } = createFakeAddonWindow({ + addonId: "bar", + }); + const console2 = addonWindow.console; + + // console - Plain console object (messages are tagged with window ids + // and originAttributes, but the addonId will be empty). + console.log("Hello from default console"); + + console1.warn("Warning from foo"); + console2.error("Error from bar"); + + let listener = new ConsoleAPIListener(null, onConsoleAPICall); + listener.init(); + let messages = listener.getCachedMessages(); + + seenTypes = 0; + seenMessages = 0; + messages.forEach(onConsoleAPICall); + Assert.equal(seenMessages, 3); + Assert.equal(seenTypes, 7); + + seenTypes = 0; + seenMessages = 0; + console.log("Hello from default console"); + console1.warn("Warning from foo"); + console2.error("Error from bar"); + Assert.equal(seenMessages, 3); + Assert.equal(seenTypes, 7); + + listener.destroy(); + + listener = new ConsoleAPIListener(null, onConsoleAPICall, { addonId: "foo" }); + listener.init(); + messages = listener.getCachedMessages(); + + seenTypes = 0; + seenMessages = 0; + messages.forEach(onConsoleAPICall); + Assert.equal(seenMessages, 2); + Assert.equal(seenTypes, 1); + + seenTypes = 0; + seenMessages = 0; + console.log("Hello from default console"); + console1.warn("Warning from foo"); + console2.error("Error from bar"); + Assert.equal(seenMessages, 1); + Assert.equal(seenTypes, 1); + + listener.destroy(); + + listener = new ConsoleAPIListener(null, onConsoleAPICall, { addonId: "bar" }); + listener.init(); + messages = listener.getCachedMessages(); + + seenTypes = 0; + seenMessages = 0; + messages.forEach(onConsoleAPICall); + Assert.equal(seenMessages, 3); + Assert.equal(seenTypes, 2); + + seenTypes = 0; + seenMessages = 0; + console.log("Hello from default console"); + console1.warn("Warning from foo"); + console2.error("Error from bar"); + + Assert.equal(seenMessages, 1); + Assert.equal(seenTypes, 2); + + listener.destroy(); + + // Close the addon window's chromeWebNav. + chromeWebNav.close(); +} |