diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /toolkit/modules/tests/xpcshell/test_web_channel_broker.js | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/modules/tests/xpcshell/test_web_channel_broker.js')
-rw-r--r-- | toolkit/modules/tests/xpcshell/test_web_channel_broker.js | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/toolkit/modules/tests/xpcshell/test_web_channel_broker.js b/toolkit/modules/tests/xpcshell/test_web_channel_broker.js new file mode 100644 index 0000000000..523699468d --- /dev/null +++ b/toolkit/modules/tests/xpcshell/test_web_channel_broker.js @@ -0,0 +1,78 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const { WebChannelBroker } = ChromeUtils.import( + "resource://gre/modules/WebChannel.jsm" +); + +const VALID_WEB_CHANNEL_ID = "id"; +const URL_STRING = "http://example.com"; +const VALID_WEB_CHANNEL_ORIGIN = Services.io.newURI(URL_STRING); + +/** + * Test WebChannelBroker channel map + */ +add_test(function test_web_channel_broker_channel_map() { + let channel = {}; + let channel2 = {}; + + Assert.equal(WebChannelBroker._channelMap.size, 0); + + // make sure _channelMap works correctly + WebChannelBroker.registerChannel(channel); + Assert.equal(WebChannelBroker._channelMap.size, 1); + + WebChannelBroker.registerChannel(channel2); + Assert.equal(WebChannelBroker._channelMap.size, 2); + + WebChannelBroker.unregisterChannel(channel); + Assert.equal(WebChannelBroker._channelMap.size, 1); + + // make sure the correct channel is unregistered + Assert.ok(!WebChannelBroker._channelMap.has(channel)); + Assert.ok(WebChannelBroker._channelMap.has(channel2)); + + WebChannelBroker.unregisterChannel(channel2); + Assert.equal(WebChannelBroker._channelMap.size, 0); + + run_next_test(); +}); + +/** + * Test WebChannelBroker _listener test + */ +add_task(function test_web_channel_broker_listener() { + return new Promise((resolve, reject) => { + var channel = { + id: VALID_WEB_CHANNEL_ID, + _originCheckCallback: requestPrincipal => { + return VALID_WEB_CHANNEL_ORIGIN.prePath === requestPrincipal.origin; + }, + deliver(data, sender) { + Assert.equal(data.id, VALID_WEB_CHANNEL_ID); + Assert.equal(data.message.command, "hello"); + Assert.notEqual(sender, undefined); + WebChannelBroker.unregisterChannel(channel); + resolve(); + }, + }; + + WebChannelBroker.registerChannel(channel); + + var mockEvent = { + id: VALID_WEB_CHANNEL_ID, + message: { + command: "hello", + }, + }; + + WebChannelBroker.tryToDeliver(mockEvent, { + browsingContext: {}, + principal: { + origin: URL_STRING, + }, + }); + }); +}); |