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

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: Removed rebase added duplicate code Created 4 years, 9 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 8
10 // NIST P-256 public key made available to tests. Must be an uncompressed 9 // NIST P-256 public key made available to tests. Must be an uncompressed
11 // point in accordance with SEC1 2.3.3. 10 // point in accordance with SEC1 2.3.3.
12 var applicationServerKey = new Uint8Array([ 11 var applicationServerKey = new Uint8Array([
13 0x04, 0x55, 0x52, 0x6A, 0xA5, 0x6E, 0x8E, 0xAA, 0x47, 0x97, 0x36, 0x10, 0xC1, 12 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, 13 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, 14 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, 15 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 16 0x7F, 0xF2, 0x76, 0xB6, 0x01, 0x20, 0xD8, 0x35, 0xA5, 0xD9, 0x3C, 0x43, 0xFD
18 ]); 17 ]);
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 } 115 }
117 } 116 }
118 117
119 // This is the old style of push subscriptions which we are phasing away 118 // 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. 119 // from, where the subscription used a sender ID instead of public key.
121 function subscribePushWithoutKey() { 120 function subscribePushWithoutKey() {
122 navigator.serviceWorker.ready.then(function(swRegistration) { 121 navigator.serviceWorker.ready.then(function(swRegistration) {
123 return swRegistration.pushManager.subscribe( 122 return swRegistration.pushManager.subscribe(
124 pushSubscriptionOptions) 123 pushSubscriptionOptions)
125 .then(function(subscription) { 124 .then(function(subscription) {
126 pushSubscription = subscription;
127 sendResultToTest(subscription.endpoint); 125 sendResultToTest(subscription.endpoint);
128 }); 126 });
129 }).catch(sendErrorToTest); 127 }).catch(sendErrorToTest);
130 } 128 }
131 129
132 function subscribePush() { 130 function subscribePush() {
133 navigator.serviceWorker.ready.then(function(swRegistration) { 131 navigator.serviceWorker.ready.then(function(swRegistration) {
134 pushSubscriptionOptions.applicationServerKey = applicationServerKey.buffer; 132 pushSubscriptionOptions.applicationServerKey = applicationServerKey.buffer;
135 return swRegistration.pushManager.subscribe(pushSubscriptionOptions) 133 return swRegistration.pushManager.subscribe(pushSubscriptionOptions)
136 .then(function(subscription) { 134 .then(function(subscription) {
137 pushSubscription = subscription;
138 sendResultToTest(subscription.endpoint); 135 sendResultToTest(subscription.endpoint);
139 }); 136 });
140 }).catch(sendErrorToTest); 137 }).catch(sendErrorToTest);
141 } 138 }
142 139
143 function subscribePushBadKey() { 140 function subscribePushBadKey() {
144 navigator.serviceWorker.ready.then(function(swRegistration) { 141 navigator.serviceWorker.ready.then(function(swRegistration) {
145 var invalidApplicationServerKey = Uint8Array.from(applicationServerKey); 142 var invalidApplicationServerKey = Uint8Array.from(applicationServerKey);
146 invalidApplicationServerKey[0] = 0x05; 143 invalidApplicationServerKey[0] = 0x05;
147 pushSubscriptionOptions.applicationServerKey = 144 pushSubscriptionOptions.applicationServerKey =
148 invalidApplicationServerKey.buffer; 145 invalidApplicationServerKey.buffer;
149 return swRegistration.pushManager.subscribe(pushSubscriptionOptions) 146 return swRegistration.pushManager.subscribe(pushSubscriptionOptions)
150 .then(function(subscription) { 147 .then(function(subscription) {
151 pushSubscription = subscription;
152 sendResultToTest(subscription.endpoint); 148 sendResultToTest(subscription.endpoint);
153 }); 149 });
154 }).catch(sendErrorToTest); 150 }).catch(sendErrorToTest);
155 } 151 }
156 152
153 function workerSubscribePush() {
154 // Send the message to the worker for it to subscribe
155 navigator.serviceWorker.controller.postMessage({command: 'workerSubscribe'});
156 }
157
158 function workerSubscribePermissionFail() {
Michael van Ouwerkerk 2016/03/22 11:31:37 This function appears to be unused.
harkness 2016/03/24 16:56:23 Removed. It was duplicate testing anyway.
159 // Attempt to subscribe with invalid useVisibleOnly flag
Michael van Ouwerkerk 2016/03/22 11:31:37 Is |useVisibleOnly| an intentional misspelling the
harkness 2016/03/24 16:56:23 Acknowledged.
160 navigator.serviceWorker.controller.postMessage(
161 {command: 'workerSubscribePermissionFail'});
162 }
163
164 function workerSubscribePushNoKey() {
165 // The worker will try to subscribe without providing a key. This should
166 // succeed if the worker was previously subscribed and fail otherwise.
167 navigator.serviceWorker.controller.postMessage(
168 {command: 'workerSubscribeNoKey'});
169 }
170
157 function GetP256dh() { 171 function GetP256dh() {
158 navigator.serviceWorker.ready.then(function(swRegistration) { 172 navigator.serviceWorker.ready.then(function(swRegistration) {
159 return swRegistration.pushManager.getSubscription() 173 return swRegistration.pushManager.getSubscription()
160 .then(function(subscription) { 174 .then(function(subscription) {
161 sendResultToTest(btoa(String.fromCharCode.apply(null, 175 sendResultToTest(btoa(String.fromCharCode.apply(null,
162 new Uint8Array(subscription.getKey('p256dh'))))); 176 new Uint8Array(subscription.getKey('p256dh')))));
163 }); 177 });
164 }).catch(sendErrorToTest); 178 }).catch(sendErrorToTest);
165 } 179 }
166 180
167 function permissionState() { 181 function permissionState() {
168 navigator.serviceWorker.ready.then(function(swRegistration) { 182 navigator.serviceWorker.ready.then(function(swRegistration) {
169 return swRegistration.pushManager.permissionState(pushSubscriptionOptions) 183 return swRegistration.pushManager.permissionState(pushSubscriptionOptions)
170 .then(function(permission) { 184 .then(function(permission) {
171 sendResultToTest('permission status - ' + permission); 185 sendResultToTest('permission status - ' + permission);
172 }); 186 });
173 }).catch(sendErrorToTest); 187 }).catch(sendErrorToTest);
174 } 188 }
175 189
176 function isControlled() { 190 function isControlled() {
177 if (navigator.serviceWorker.controller) { 191 if (navigator.serviceWorker.controller) {
178 sendResultToTest('true - is controlled'); 192 sendResultToTest('true - is controlled');
179 } else { 193 } else {
180 sendResultToTest('false - is not controlled'); 194 sendResultToTest('false - is not controlled');
181 } 195 }
182 } 196 }
183 197
184 function unsubscribePush() { 198 function unsubscribePush() {
185 if (!pushSubscription) { 199 navigator.serviceWorker.ready.then(function(swRegistration) {
harkness 2016/03/21 15:05:02 Previously unsubscribe used a variable that was se
186 sendResultToTest('unsubscribe error: no subscription'); 200 if (!swRegistration) {
187 return; 201 sendResultToTest('unsubscribe result: false');
188 } 202 return;
189 203 }
190 pushSubscription.unsubscribe().then(function(result) { 204 swRegistration.pushManager.getSubscription().then(function(pushSubscription)
191 sendResultToTest('unsubscribe result: ' + result); 205 {
192 }, function(error) { 206 if (!pushSubscription) {
193 sendResultToTest('unsubscribe error: ' + error.name + ': ' + error.message); 207 sendResultToTest('unsubscribe result: false');
208 return;
209 }
210 pushSubscription.unsubscribe().then(function(result) {
211 sendResultToTest('unsubscribe result: ' + result);
212 }, function(error) {
213 sendResultToTest('unsubscribe error: ' + error.message);
214 })
215 })
194 }); 216 });
195 } 217 }
196 218
197 function hasSubscription() { 219 function hasSubscription() {
198 navigator.serviceWorker.ready.then(function(swRegistration) { 220 navigator.serviceWorker.ready.then(function(swRegistration) {
199 return swRegistration.pushManager.getSubscription(); 221 return swRegistration.pushManager.getSubscription();
200 }).then(function(subscription) { 222 }).then(function(subscription) {
201 sendResultToTest(subscription ? 'true - subscribed' 223 sendResultToTest(subscription ? 'true - subscribed'
202 : 'false - not subscribed'); 224 : 'false - not subscribed');
203 }).catch(sendErrorToTest); 225 }).catch(sendErrorToTest);
204 } 226 }
205 227
206 navigator.serviceWorker.addEventListener('message', function(event) { 228 navigator.serviceWorker.addEventListener('message', function(event) {
207 var message = JSON.parse(event.data); 229 var message = JSON.parse(event.data);
208 if (message.type == 'push') 230 if (message.type == 'push')
209 resultQueue.push(message.data); 231 resultQueue.push(message.data);
232 else
233 sendResultToTest(message.data);
210 }, false); 234 }, false);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698