summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_MessageManagerProxy.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /toolkit/components/extensions/test/xpcshell/test_ext_MessageManagerProxy.js
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/extensions/test/xpcshell/test_ext_MessageManagerProxy.js')
-rw-r--r--toolkit/components/extensions/test/xpcshell/test_ext_MessageManagerProxy.js77
1 files changed, 77 insertions, 0 deletions
diff --git a/toolkit/components/extensions/test/xpcshell/test_ext_MessageManagerProxy.js b/toolkit/components/extensions/test/xpcshell/test_ext_MessageManagerProxy.js
new file mode 100644
index 0000000000..cb6ec47b52
--- /dev/null
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_MessageManagerProxy.js
@@ -0,0 +1,77 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+const { MessageManagerProxy } = ChromeUtils.importESModule(
+ "resource://gre/modules/MessageManagerProxy.sys.mjs"
+);
+
+class TestMessageManagerProxy extends MessageManagerProxy {
+ constructor(contentPage, identifier) {
+ super(contentPage.browser);
+ this.identifier = identifier;
+ this.contentPage = contentPage;
+ this.deferred = null;
+ }
+
+ // Registers message listeners. Call dispose() once you've finished.
+ async setupPingPongListeners() {
+ await this.contentPage.loadFrameScript(`() => {
+ this.addMessageListener("test:MessageManagerProxy:Ping", ({data}) => {
+ this.sendAsyncMessage("test:MessageManagerProxy:Pong", "${this.identifier}:" + data);
+ });
+ }`);
+
+ // Register the listener here instead of during testPingPong, to make sure
+ // that the listener is correctly registered during the whole test.
+ this.addMessageListener("test:MessageManagerProxy:Pong", event => {
+ ok(
+ this.deferred,
+ `[${this.identifier}] expected to be waiting for ping-pong`
+ );
+ this.deferred.resolve(event.data);
+ this.deferred = null;
+ });
+ }
+
+ async testPingPong(description) {
+ equal(this.deferred, null, "should not be waiting for a message");
+ this.deferred = Promise.withResolvers();
+ this.sendAsyncMessage("test:MessageManagerProxy:Ping", description);
+ let result = await this.deferred.promise;
+ equal(result, `${this.identifier}:${description}`, "Expected ping-pong");
+ }
+}
+
+// Tests that MessageManagerProxy continues to proxy messages after docshells
+// have been swapped.
+add_task(async function test_message_after_swapdocshells() {
+ let page1 = await ExtensionTestUtils.loadContentPage("about:blank");
+ let page2 = await ExtensionTestUtils.loadContentPage("about:blank");
+
+ let testProxyOne = new TestMessageManagerProxy(page1, "page1");
+ let testProxyTwo = new TestMessageManagerProxy(page2, "page2");
+
+ await testProxyOne.setupPingPongListeners();
+ await testProxyTwo.setupPingPongListeners();
+
+ await testProxyOne.testPingPong("after setup (to 1)");
+ await testProxyTwo.testPingPong("after setup (to 2)");
+
+ page1.browser.swapDocShells(page2.browser);
+
+ await testProxyOne.testPingPong("after docshell swap (to 1)");
+ await testProxyTwo.testPingPong("after docshell swap (to 2)");
+
+ // Swap again to verify that listeners are repeatedly moved when needed.
+ page1.browser.swapDocShells(page2.browser);
+
+ await testProxyOne.testPingPong("after another docshell swap (to 1)");
+ await testProxyTwo.testPingPong("after another docshell swap (to 2)");
+
+ // Verify that dispose() works regardless of the browser's validity.
+ await testProxyOne.dispose();
+ await page1.close();
+ await page2.close();
+ await testProxyTwo.dispose();
+});