Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(535)

Side by Side Diff: chrome/test/data/push_messaging/push_test.js

Issue 1816123002: Add testing for subscription from service workers (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix asan errors Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 'use strict'; 5 'use strict';
6 6
7 var resultQueue = new ResultQueue(); 7 var resultQueue = new ResultQueue();
8 var pushSubscription = null;
9
10 // NIST P-256 public key made available to tests. Must be an uncompressed
11 // point in accordance with SEC1 2.3.3.
12 var applicationServerKey = new Uint8Array([
13 0x04, 0x55, 0x52, 0x6A, 0xA5, 0x6E, 0x8E, 0xAA, 0x47, 0x97, 0x36, 0x10, 0xC1,
14 0x66, 0x3C, 0x1E, 0x65, 0xBF, 0xA1, 0x7B, 0xEE, 0x48, 0xC9, 0xC6, 0xBB, 0xBF,
15 0x02, 0x18, 0x53, 0x72, 0x1D, 0x0C, 0x7B, 0xA9, 0xE3, 0x11, 0xB7, 0x03, 0x52,
16 0x21, 0xD3, 0x71, 0x90, 0x13, 0xA8, 0xC1, 0xCF, 0xED, 0x20, 0xF7, 0x1F, 0xD1,
17 0x7F, 0xF2, 0x76, 0xB6, 0x01, 0x20, 0xD8, 0x35, 0xA5, 0xD9, 0x3C, 0x43, 0xFD
18 ]);
19 8
20 var pushSubscriptionOptions = { 9 var pushSubscriptionOptions = {
21 userVisibleOnly: true 10 userVisibleOnly: true
22 }; 11 };
23 12
24 // Sends data back to the test. This must be in response to an earlier 13 // Sends data back to the test. This must be in response to an earlier
25 // request, but it's ok to respond asynchronously. The request blocks until 14 // request, but it's ok to respond asynchronously. The request blocks until
26 // the response is sent. 15 // the response is sent.
27 function sendResultToTest(result) { 16 function sendResultToTest(result) {
28 console.log('sendResultToTest: ' + result); 17 console.log('sendResultToTest: ' + result);
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 if (element) { 100 if (element) {
112 element.href = 'manifest_no_sender_id.json'; 101 element.href = 'manifest_no_sender_id.json';
113 sendResultToTest('sender id removed from manifest'); 102 sendResultToTest('sender id removed from manifest');
114 } else { 103 } else {
115 sendResultToTest('unable to find manifest element'); 104 sendResultToTest('unable to find manifest element');
116 } 105 }
117 } 106 }
118 107
119 // This is the old style of push subscriptions which we are phasing away 108 // This is the old style of push subscriptions which we are phasing away
120 // from, where the subscription used a sender ID instead of public key. 109 // from, where the subscription used a sender ID instead of public key.
121 function subscribePushWithoutKey() { 110 function documentSubscribePushWithoutKey() {
122 navigator.serviceWorker.ready.then(function(swRegistration) { 111 navigator.serviceWorker.ready.then(function(swRegistration) {
123 return swRegistration.pushManager.subscribe( 112 return swRegistration.pushManager.subscribe(
124 pushSubscriptionOptions) 113 pushSubscriptionOptions)
125 .then(function(subscription) { 114 .then(function(subscription) {
126 pushSubscription = subscription;
127 sendResultToTest(subscription.endpoint); 115 sendResultToTest(subscription.endpoint);
128 }); 116 });
129 }).catch(sendErrorToTest); 117 }).catch(sendErrorToTest);
130 } 118 }
131 119
132 function subscribePush() { 120 function documentSubscribePush() {
133 navigator.serviceWorker.ready.then(function(swRegistration) { 121 navigator.serviceWorker.ready.then(function(swRegistration) {
134 pushSubscriptionOptions.applicationServerKey = applicationServerKey.buffer; 122 pushSubscriptionOptions.applicationServerKey = kApplicationServerKey.buffer;
135 return swRegistration.pushManager.subscribe(pushSubscriptionOptions) 123 return swRegistration.pushManager.subscribe(pushSubscriptionOptions)
136 .then(function(subscription) { 124 .then(function(subscription) {
137 pushSubscription = subscription;
138 sendResultToTest(subscription.endpoint); 125 sendResultToTest(subscription.endpoint);
139 }); 126 });
140 }).catch(sendErrorToTest); 127 }).catch(sendErrorToTest);
141 } 128 }
142 129
143 function subscribePushBadKey() { 130 function documentSubscribePushBadKey() {
144 navigator.serviceWorker.ready.then(function(swRegistration) { 131 navigator.serviceWorker.ready.then(function(swRegistration) {
145 var invalidApplicationServerKey = Uint8Array.from(applicationServerKey); 132 var invalidApplicationServerKey = Uint8Array.from(kApplicationServerKey);
146 invalidApplicationServerKey[0] = 0x05; 133 invalidApplicationServerKey[0] = 0x05;
147 pushSubscriptionOptions.applicationServerKey = 134 pushSubscriptionOptions.applicationServerKey =
148 invalidApplicationServerKey.buffer; 135 invalidApplicationServerKey.buffer;
149 return swRegistration.pushManager.subscribe(pushSubscriptionOptions) 136 return swRegistration.pushManager.subscribe(pushSubscriptionOptions)
150 .then(function(subscription) { 137 .then(function(subscription) {
151 pushSubscription = subscription;
152 sendResultToTest(subscription.endpoint); 138 sendResultToTest(subscription.endpoint);
153 }); 139 });
154 }).catch(sendErrorToTest); 140 }).catch(sendErrorToTest);
155 } 141 }
156 142
143 function workerSubscribePush() {
144 // Send the message to the worker for it to subscribe
145 navigator.serviceWorker.controller.postMessage({command: 'workerSubscribe'});
146 }
147
148 function workerSubscribePushNoKey() {
149 // The worker will try to subscribe without providing a key. This should
150 // succeed if the worker was previously subscribed and fail otherwise.
151 navigator.serviceWorker.controller.postMessage(
152 {command: 'workerSubscribeNoKey'});
153 }
154
157 function GetP256dh() { 155 function GetP256dh() {
158 navigator.serviceWorker.ready.then(function(swRegistration) { 156 navigator.serviceWorker.ready.then(function(swRegistration) {
159 return swRegistration.pushManager.getSubscription() 157 return swRegistration.pushManager.getSubscription()
160 .then(function(subscription) { 158 .then(function(subscription) {
161 sendResultToTest(btoa(String.fromCharCode.apply(null, 159 sendResultToTest(btoa(String.fromCharCode.apply(null,
162 new Uint8Array(subscription.getKey('p256dh'))))); 160 new Uint8Array(subscription.getKey('p256dh')))));
163 }); 161 });
164 }).catch(sendErrorToTest); 162 }).catch(sendErrorToTest);
165 } 163 }
166 164
167 function permissionState() { 165 function permissionState() {
168 navigator.serviceWorker.ready.then(function(swRegistration) { 166 navigator.serviceWorker.ready.then(function(swRegistration) {
169 return swRegistration.pushManager.permissionState(pushSubscriptionOptions) 167 return swRegistration.pushManager.permissionState(pushSubscriptionOptions)
170 .then(function(permission) { 168 .then(function(permission) {
171 sendResultToTest('permission status - ' + permission); 169 sendResultToTest('permission status - ' + permission);
172 }); 170 });
173 }).catch(sendErrorToTest); 171 }).catch(sendErrorToTest);
174 } 172 }
175 173
176 function isControlled() { 174 function isControlled() {
177 if (navigator.serviceWorker.controller) { 175 if (navigator.serviceWorker.controller) {
178 sendResultToTest('true - is controlled'); 176 sendResultToTest('true - is controlled');
179 } else { 177 } else {
180 sendResultToTest('false - is not controlled'); 178 sendResultToTest('false - is not controlled');
181 } 179 }
182 } 180 }
183 181
184 function unsubscribePush() { 182 function unsubscribePush() {
185 if (!pushSubscription) { 183 navigator.serviceWorker.ready.then(function(swRegistration) {
186 sendResultToTest('unsubscribe error: no subscription'); 184 if (!swRegistration) {
187 return; 185 sendResultToTest('unsubscribe result: false');
188 } 186 return;
189 187 }
190 pushSubscription.unsubscribe().then(function(result) { 188 swRegistration.pushManager.getSubscription().then(function(pushSubscription)
191 sendResultToTest('unsubscribe result: ' + result); 189 {
192 }, function(error) { 190 if (!pushSubscription) {
193 sendResultToTest('unsubscribe error: ' + error.name + ': ' + error.message); 191 sendResultToTest('unsubscribe result: false');
192 return;
193 }
194 pushSubscription.unsubscribe().then(function(result) {
195 sendResultToTest('unsubscribe result: ' + result);
196 }, function(error) {
197 sendResultToTest('unsubscribe error: ' + error.message);
198 })
199 })
194 }); 200 });
195 } 201 }
196 202
197 function hasSubscription() { 203 function hasSubscription() {
198 navigator.serviceWorker.ready.then(function(swRegistration) { 204 navigator.serviceWorker.ready.then(function(swRegistration) {
199 return swRegistration.pushManager.getSubscription(); 205 return swRegistration.pushManager.getSubscription();
200 }).then(function(subscription) { 206 }).then(function(subscription) {
201 sendResultToTest(subscription ? 'true - subscribed' 207 sendResultToTest(subscription ? 'true - subscribed'
202 : 'false - not subscribed'); 208 : 'false - not subscribed');
203 }).catch(sendErrorToTest); 209 }).catch(sendErrorToTest);
204 } 210 }
205 211
206 navigator.serviceWorker.addEventListener('message', function(event) { 212 navigator.serviceWorker.addEventListener('message', function(event) {
207 var message = JSON.parse(event.data); 213 var message = JSON.parse(event.data);
208 if (message.type == 'push') 214 if (message.type == 'push')
209 resultQueue.push(message.data); 215 resultQueue.push(message.data);
216 else
217 sendResultToTest(message.data);
210 }, false); 218 }, false);
OLDNEW
« no previous file with comments | « chrome/test/data/push_messaging/push_constants.js ('k') | chrome/test/data/push_messaging/service_worker.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698