Chromium Code Reviews| Index: chrome/test/data/push_messaging/test.html |
| diff --git a/chrome/test/data/push_messaging/test.html b/chrome/test/data/push_messaging/test.html |
| index 407695698f89695224c755778a9469741c1da770..71375423686f98574242a7a7659bce0cf5523eb2 100644 |
| --- a/chrome/test/data/push_messaging/test.html |
| +++ b/chrome/test/data/push_messaging/test.html |
| @@ -3,6 +3,8 @@ |
| <head> |
| <title>Push API Test</title> |
| <script> |
| + var lastMessageFromServiceWorker = null; |
| + |
| function sendResultToTest(result) { |
| console.log(result); |
| if (window.domAutomationController) { |
| @@ -15,20 +17,62 @@ |
| } |
| function registerServiceWorker() { |
| - navigator.serviceWorker.register('service_worker.js').then(function(swRegistration) { |
| - console.log(swRegistration); |
| - sendResultToTest('ok'); |
| - }, sendErrorToTest); |
| + navigator.serviceWorker.register('service_worker.js').then( |
| + function(swRegistration) { |
| + console.log(swRegistration); |
| + sendResultToTest('ok - service worker registered'); |
| + }, sendErrorToTest); |
| } |
| function registerPush(senderId) { |
| navigator.serviceWorker.ready.then(function() { |
| navigator.push.register(senderId).then(function(pushRegistration) { |
| - sendResultToTest(pushRegistration.pushEndpoint + ' - ' + pushRegistration.pushRegistrationId); |
| + sendResultToTest(pushRegistration.pushEndpoint + ' - ' + |
| + pushRegistration.pushRegistrationId); |
| }, sendErrorToTest); |
| }, sendErrorToTest); |
| } |
| + |
| + function doPostMessage() { |
| + navigator.serviceWorker.controller.postMessage('Message from page.'); |
| + } |
| + |
| + addEventListener('message', function(event) { |
| + console.log(event); |
| + console.log(event.data); |
| + lastMessageFromServiceWorker = event.data; |
| + }, false); |
| + |
| + function isControlled() { |
| + if (navigator.serviceWorker.controller) { |
| + sendResultToTest('true - is controlled'); |
| + } else { |
| + sendResultToTest('false - is not controlled'); |
| + } |
| + } |
| + |
| + function getLastMessageFromServiceWorker(loops) { |
| + loops = loops || 0; |
| + 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
|
| + setTimeout(function() { |
| + getLastMessageFromServiceWorker(loops + 1); |
| + }, 0); |
| + return; |
| + } |
| + sendResultToTest(lastMessageFromServiceWorker); |
| + lastMessageFromServiceWorker = null; |
| + } |
| </script> |
| </head> |
| - <body>Push API Test</body> |
| + <body> |
| + <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
|
| + <div> |
| + <button onclick="registerServiceWorker();">registerServiceWorker()</button> |
| + <button onclick="registerPush();">registerPush()</button> |
| + <button onclick="isControlled();">isControlled()</button> |
| + <button onclick="doPostMessage();">doPostMessage()</button> |
| + <button onclick="getLastMessageFromServiceWorker();"> |
| + getLastMessageFromServiceWorker()</button> |
| + </div> |
| + </body> |
| </html> |