test_observer_remoting.js (3937B)
1 "use strict"; 2 3 const pushNotifier = Cc["@mozilla.org/push/Notifier;1"].getService( 4 Ci.nsIPushNotifier 5 ); 6 7 add_task(async function test_observer_remoting() { 8 do_get_profile(); 9 if (isParent) { 10 await testInParent(); 11 } else { 12 await testInChild(); 13 } 14 }); 15 16 const childTests = [ 17 { 18 text: "Hello from child!", 19 principal: Services.scriptSecurityManager.getSystemPrincipal(), 20 }, 21 ]; 22 23 const parentTests = [ 24 { 25 text: "Hello from parent!", 26 principal: Services.scriptSecurityManager.getSystemPrincipal(), 27 }, 28 ]; 29 30 async function testInParent() { 31 setPrefs(); 32 // Register observers for notifications from the child, then run the test in 33 // the child and wait for the notifications. 34 let promiseNotifications = childTests.reduce( 35 (p, test) => 36 p.then(_ => waitForNotifierObservers(test, /* shouldNotify = */ false)), 37 Promise.resolve() 38 ); 39 let promiseFinished = run_test_in_child("./test_observer_remoting.js"); 40 await promiseNotifications; 41 42 // Wait until the child is listening for notifications from the parent. 43 await do_await_remote_message("push_test_observer_remoting_child_ready"); 44 45 // Fire an observer notification in the parent that should be forwarded to 46 // the child. 47 await parentTests.reduce( 48 (p, test) => 49 p.then(_ => waitForNotifierObservers(test, /* shouldNotify = */ true)), 50 Promise.resolve() 51 ); 52 53 // Wait for the child to exit. 54 await promiseFinished; 55 } 56 57 async function testInChild() { 58 // Fire an observer notification in the child that should be forwarded to 59 // the parent. 60 await childTests.reduce( 61 (p, test) => 62 p.then(_ => waitForNotifierObservers(test, /* shouldNotify = */ true)), 63 Promise.resolve() 64 ); 65 66 // Register observers for notifications from the parent, let the parent know 67 // we're ready, and wait for the notifications. 68 let promiseNotifierObservers = parentTests.reduce( 69 (p, test) => 70 p.then(_ => waitForNotifierObservers(test, /* shouldNotify = */ false)), 71 Promise.resolve() 72 ); 73 do_send_remote_message("push_test_observer_remoting_child_ready"); 74 await promiseNotifierObservers; 75 } 76 77 var waitForNotifierObservers = async function ( 78 { text, principal }, 79 shouldNotify = false 80 ) { 81 let notifyPromise = promiseObserverNotification( 82 PushServiceComponent.pushTopic 83 ); 84 let subChangePromise = promiseObserverNotification( 85 PushServiceComponent.subscriptionChangeTopic 86 ); 87 let subModifiedPromise = promiseObserverNotification( 88 PushServiceComponent.subscriptionModifiedTopic 89 ); 90 91 let scope = "chrome://test-scope"; 92 let data = new TextEncoder().encode(text); 93 94 if (shouldNotify) { 95 pushNotifier.notifyPushWithData(scope, principal, "", data); 96 pushNotifier.notifySubscriptionChange(scope, principal); 97 pushNotifier.notifySubscriptionModified(scope, principal); 98 } 99 100 let { data: notifyScope, subject: notifySubject } = await notifyPromise; 101 equal( 102 notifyScope, 103 scope, 104 "Should fire push notifications with the correct scope" 105 ); 106 let message = notifySubject.QueryInterface(Ci.nsIPushMessage); 107 equal( 108 message.principal, 109 principal, 110 "Should include the principal in the push message" 111 ); 112 strictEqual(message.data.text(), text, "Should include data"); 113 114 let { data: subChangeScope, subject: subChangePrincipal } = 115 await subChangePromise; 116 equal( 117 subChangeScope, 118 scope, 119 "Should fire subscription change notifications with the correct scope" 120 ); 121 equal( 122 subChangePrincipal, 123 principal, 124 "Should pass the principal as the subject of a change notification" 125 ); 126 127 let { data: subModifiedScope, subject: subModifiedPrincipal } = 128 await subModifiedPromise; 129 equal( 130 subModifiedScope, 131 scope, 132 "Should fire subscription modified notifications with the correct scope" 133 ); 134 equal( 135 subModifiedPrincipal, 136 principal, 137 "Should pass the principal as the subject of a modified notification" 138 ); 139 };