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..0e7d40e94bc6d3a80f814e29083ffc8ecb27ed81 100644 |
--- a/chrome/test/data/push_messaging/test.html |
+++ b/chrome/test/data/push_messaging/test.html |
@@ -3,8 +3,10 @@ |
<head> |
<title>Push API Test</title> |
<script> |
+ var lastMessageFromServiceWorker = null; |
+ |
function sendResultToTest(result) { |
- console.log(result); |
+ console.log('sendResultToTest: ' + result); |
if (window.domAutomationController) { |
domAutomationController.send('' + result); |
} |
@@ -15,20 +17,83 @@ |
} |
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) { |
+ 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 isControlled() { |
+ if (navigator.serviceWorker.controller) { |
+ sendResultToTest('true - is controlled'); |
+ } else { |
+ sendResultToTest('false - is not controlled'); |
+ } |
+ } |
+ |
+ function doPostMessage() { |
+ navigator.serviceWorker.controller.postMessage('Message from page.'); |
+ } |
+ |
+ addEventListener('message', function(event) { |
+ var message = JSON.parse(event.data); |
+ asyncData.setData(message.type, message.data); |
+ }, false); |
+ |
+ // For getting and setting data that might not be available yet at the time |
+ // it is requested by the test. |
+ function AsyncData() { |
+ this.received = {}; |
+ this.requested = {}; |
Peter Beverloo
2014/10/24 14:54:58
I'd like to get rid of the maps here, and change A
Michael van Ouwerkerk
2014/10/27 15:14:30
Done.
|
+ } |
+ |
+ // Sends the data to the test if it is available. If not, marks it as |
+ // requested. |
+ AsyncData.prototype.getData = function(field) { |
+ console.log('getData: ' + field); |
+ if (this.received[field]) { |
+ sendResultToTest(this.received[field]); |
+ this.received[field] = null; |
+ this.requested[field] = false; |
+ } else { |
+ this.requested[field] = true; |
+ } |
+ }; |
+ |
+ // Stores the data if it was not requested yet. If it was requested already, |
+ // sends the data to the test. |
+ AsyncData.prototype.setData = function(field, data) { |
+ console.log('setData ' + field + ', ' + data); |
+ if (this.requested[field]) { |
+ sendResultToTest(data); |
+ this.received[field] = null; |
+ this.requested[field] = false; |
+ } else { |
+ this.received[field] = data; |
+ } |
+ } |
+ |
+ var asyncData = new AsyncData(); |
</script> |
</head> |
- <body>Push API Test</body> |
+ <body> |
+ <h1>Push API Test</h1> |
+ <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> |
Peter Beverloo
2014/10/24 14:54:58
While I'm not necessarily against adding some manu
fgorski
2014/10/24 17:09:24
+1. However, I think if you document it well, they
Michael van Ouwerkerk
2014/10/27 15:14:30
Done.
Michael van Ouwerkerk
2014/10/27 15:14:30
Deleted.
|
+ </div> |
+ </body> |
</html> |