test_handler_service.js (2911B)
1 "use strict"; 2 3 // Here we test that if an xpcom component is registered with the category 4 // manager for push notifications against a specific scope, that service is 5 // instantiated before the message is delivered. 6 7 const { MockRegistrar } = ChromeUtils.importESModule( 8 "resource://testing-common/MockRegistrar.sys.mjs" 9 ); 10 const { ChromePushSubscription } = ChromeUtils.importESModule( 11 "resource://gre/modules/ChromePushSubscription.sys.mjs" 12 ); 13 14 let pushService = Cc["@mozilla.org/push/Service;1"].getService( 15 Ci.nsIPushService 16 ); 17 18 function PushServiceHandler() { 19 // Register a push observer. 20 this.observed = []; 21 Services.obs.addObserver(this, pushService.pushTopic); 22 Services.obs.addObserver(this, pushService.subscriptionChangeTopic); 23 Services.obs.addObserver(this, pushService.subscriptionModifiedTopic); 24 } 25 26 PushServiceHandler.prototype = { 27 classID: Components.ID("{bb7c5199-c0f7-4976-9f6d-1306e32c5591}"), 28 QueryInterface: ChromeUtils.generateQI([]), 29 30 observe(subject, topic, data) { 31 this.observed.push({ subject, topic, data }); 32 }, 33 }; 34 35 let handlerService = new PushServiceHandler(); 36 37 add_test(function test_service_instantiation() { 38 const CONTRACT_ID = "@mozilla.org/dom/push/test/PushServiceHandler;1"; 39 let scope = "chrome://test-scope"; 40 41 MockRegistrar.register(CONTRACT_ID, handlerService); 42 Services.catMan.addCategoryEntry("push", scope, CONTRACT_ID, false, false); 43 44 let pushNotifier = Cc["@mozilla.org/push/Notifier;1"].getService( 45 Ci.nsIPushNotifier 46 ); 47 let principal = Services.scriptSecurityManager.getSystemPrincipal(); 48 pushNotifier.notifyPush(scope, principal, ""); 49 50 equal(handlerService.observed.length, 1); 51 equal(handlerService.observed[0].topic, pushService.pushTopic); 52 let message = handlerService.observed[0].subject.QueryInterface( 53 Ci.nsIPushMessage 54 ); 55 equal(message.principal, principal); 56 strictEqual(message.data, null); 57 equal(handlerService.observed[0].data, scope); 58 59 // and a subscription change. 60 pushNotifier.notifySubscriptionChange( 61 scope, 62 principal, 63 new ChromePushSubscription({ 64 endpoint: "xpcshell", 65 lastPush: 0, 66 pushCount: 0, 67 p256dhKey: [], 68 p256dhPrivateKey: [], 69 authenticationSecret: [], 70 appServerKey: [], 71 quota: 0, 72 systemRecord: true, 73 }) 74 ); 75 equal(handlerService.observed.length, 2); 76 equal(handlerService.observed[1].topic, pushService.subscriptionChangeTopic); 77 equal(handlerService.observed[1].subject, principal); 78 equal(handlerService.observed[1].data, scope); 79 80 // and a subscription modified event. 81 pushNotifier.notifySubscriptionModified(scope, principal); 82 equal(handlerService.observed.length, 3); 83 equal( 84 handlerService.observed[2].topic, 85 pushService.subscriptionModifiedTopic 86 ); 87 equal(handlerService.observed[2].subject, principal); 88 equal(handlerService.observed[2].data, scope); 89 90 run_next_test(); 91 });