OLD | NEW |
---|---|
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <html> | 2 <html> |
3 <head> | 3 <head> |
4 <title>Push API Test</title> | 4 <title>Push API Test</title> |
5 <script> | 5 <script> |
6 var lastMessageFromServiceWorker = null; | |
7 | |
6 function sendResultToTest(result) { | 8 function sendResultToTest(result) { |
7 console.log(result); | 9 console.log(result); |
8 if (window.domAutomationController) { | 10 if (window.domAutomationController) { |
9 domAutomationController.send('' + result); | 11 domAutomationController.send('' + result); |
10 } | 12 } |
11 } | 13 } |
12 | 14 |
13 function sendErrorToTest(error) { | 15 function sendErrorToTest(error) { |
14 sendResultToTest(error.name + ' - ' + error.message); | 16 sendResultToTest(error.name + ' - ' + error.message); |
15 } | 17 } |
16 | 18 |
17 function registerServiceWorker() { | 19 function registerServiceWorker() { |
18 navigator.serviceWorker.register('service_worker.js').then(function(swRegi stration) { | 20 navigator.serviceWorker.register('service_worker.js').then( |
19 console.log(swRegistration); | 21 function(swRegistration) { |
20 sendResultToTest('ok'); | 22 console.log(swRegistration); |
21 }, sendErrorToTest); | 23 sendResultToTest('ok - service worker registered'); |
24 }, sendErrorToTest); | |
22 } | 25 } |
23 | 26 |
24 function registerPush(senderId) { | 27 function registerPush(senderId) { |
25 navigator.serviceWorker.ready.then(function() { | 28 navigator.serviceWorker.ready.then(function() { |
26 navigator.push.register(senderId).then(function(pushRegistration) { | 29 navigator.push.register(senderId).then(function(pushRegistration) { |
27 sendResultToTest(pushRegistration.pushEndpoint + ' - ' + pushRegistrat ion.pushRegistrationId); | 30 sendResultToTest(pushRegistration.pushEndpoint + ' - ' + |
31 pushRegistration.pushRegistrationId); | |
28 }, sendErrorToTest); | 32 }, sendErrorToTest); |
29 }, sendErrorToTest); | 33 }, sendErrorToTest); |
30 } | 34 } |
35 | |
36 function doPostMessage() { | |
37 navigator.serviceWorker.controller.postMessage('Message from page.'); | |
38 } | |
39 | |
40 addEventListener('message', function(event) { | |
41 console.log(event); | |
42 console.log(event.data); | |
43 lastMessageFromServiceWorker = event.data; | |
44 }, false); | |
45 | |
46 function isControlled() { | |
47 if (navigator.serviceWorker.controller) { | |
48 sendResultToTest('true - is controlled'); | |
49 } else { | |
50 sendResultToTest('false - is not controlled'); | |
51 } | |
52 } | |
53 | |
54 function getLastMessageFromServiceWorker(loops) { | |
55 loops = loops || 0; | |
56 if (!lastMessageFromServiceWorker && loops < 10) { | |
fgorski
2014/10/23 16:29:51
Isn't there a way to implement asynchronous test c
Michael van Ouwerkerk
2014/10/23 16:39:52
Well, domAutomationController.send must be called
| |
57 setTimeout(function() { | |
58 getLastMessageFromServiceWorker(loops + 1); | |
59 }, 0); | |
60 return; | |
61 } | |
62 sendResultToTest(lastMessageFromServiceWorker); | |
63 lastMessageFromServiceWorker = null; | |
64 } | |
31 </script> | 65 </script> |
32 </head> | 66 </head> |
33 <body>Push API Test</body> | 67 <body> |
68 <h1>Push API Test</h1> | |
fgorski
2014/10/23 16:29:51
What do yo need this for?
Michael van Ouwerkerk
2014/10/23 16:39:52
It's for manually stepping through the test. Some
| |
69 <div> | |
70 <button onclick="registerServiceWorker();">registerServiceWorker()</button> | |
71 <button onclick="registerPush();">registerPush()</button> | |
72 <button onclick="isControlled();">isControlled()</button> | |
73 <button onclick="doPostMessage();">doPostMessage()</button> | |
74 <button onclick="getLastMessageFromServiceWorker();"> | |
75 getLastMessageFromServiceWorker()</button> | |
76 </div> | |
77 </body> | |
34 </html> | 78 </html> |
OLD | NEW |