From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- dom/push/test/xpcshell/test_observer_remoting.js | 139 +++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 dom/push/test/xpcshell/test_observer_remoting.js (limited to 'dom/push/test/xpcshell/test_observer_remoting.js') diff --git a/dom/push/test/xpcshell/test_observer_remoting.js b/dom/push/test/xpcshell/test_observer_remoting.js new file mode 100644 index 0000000000..086fdeab80 --- /dev/null +++ b/dom/push/test/xpcshell/test_observer_remoting.js @@ -0,0 +1,139 @@ +"use strict"; + +const pushNotifier = Cc["@mozilla.org/push/Notifier;1"].getService( + Ci.nsIPushNotifier +); + +add_task(async function test_observer_remoting() { + do_get_profile(); + if (isParent) { + await testInParent(); + } else { + await testInChild(); + } +}); + +const childTests = [ + { + text: "Hello from child!", + principal: Services.scriptSecurityManager.getSystemPrincipal(), + }, +]; + +const parentTests = [ + { + text: "Hello from parent!", + principal: Services.scriptSecurityManager.getSystemPrincipal(), + }, +]; + +async function testInParent() { + setPrefs(); + // Register observers for notifications from the child, then run the test in + // the child and wait for the notifications. + let promiseNotifications = childTests.reduce( + (p, test) => + p.then(_ => waitForNotifierObservers(test, /* shouldNotify = */ false)), + Promise.resolve() + ); + let promiseFinished = run_test_in_child("./test_observer_remoting.js"); + await promiseNotifications; + + // Wait until the child is listening for notifications from the parent. + await do_await_remote_message("push_test_observer_remoting_child_ready"); + + // Fire an observer notification in the parent that should be forwarded to + // the child. + await parentTests.reduce( + (p, test) => + p.then(_ => waitForNotifierObservers(test, /* shouldNotify = */ true)), + Promise.resolve() + ); + + // Wait for the child to exit. + await promiseFinished; +} + +async function testInChild() { + // Fire an observer notification in the child that should be forwarded to + // the parent. + await childTests.reduce( + (p, test) => + p.then(_ => waitForNotifierObservers(test, /* shouldNotify = */ true)), + Promise.resolve() + ); + + // Register observers for notifications from the parent, let the parent know + // we're ready, and wait for the notifications. + let promiseNotifierObservers = parentTests.reduce( + (p, test) => + p.then(_ => waitForNotifierObservers(test, /* shouldNotify = */ false)), + Promise.resolve() + ); + do_send_remote_message("push_test_observer_remoting_child_ready"); + await promiseNotifierObservers; +} + +var waitForNotifierObservers = async function ( + { text, principal }, + shouldNotify = false +) { + let notifyPromise = promiseObserverNotification( + PushServiceComponent.pushTopic + ); + let subChangePromise = promiseObserverNotification( + PushServiceComponent.subscriptionChangeTopic + ); + let subModifiedPromise = promiseObserverNotification( + PushServiceComponent.subscriptionModifiedTopic + ); + + let scope = "chrome://test-scope"; + let data = new TextEncoder().encode(text); + + if (shouldNotify) { + pushNotifier.notifyPushWithData(scope, principal, "", data); + pushNotifier.notifySubscriptionChange(scope, principal); + pushNotifier.notifySubscriptionModified(scope, principal); + } + + let { data: notifyScope, subject: notifySubject } = await notifyPromise; + equal( + notifyScope, + scope, + "Should fire push notifications with the correct scope" + ); + let message = notifySubject.QueryInterface(Ci.nsIPushMessage); + equal( + message.principal, + principal, + "Should include the principal in the push message" + ); + strictEqual(message.data.text(), text, "Should include data"); + + let { data: subChangeScope, subject: subChangePrincipal } = + await subChangePromise; + equal( + subChangeScope, + scope, + "Should fire subscription change notifications with the correct scope" + ); + equal( + subChangePrincipal, + principal, + "Should pass the principal as the subject of a change notification" + ); + + let { data: subModifiedScope, subject: subModifiedPrincipal } = + await subModifiedPromise; + equal( + subModifiedScope, + scope, + "Should fire subscription modified notifications with the correct scope" + ); + equal( + subModifiedPrincipal, + principal, + "Should pass the principal as the subject of a modified notification" + ); +}; -- cgit v1.2.3