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

Side by Side Diff: chrome/test/data/push_messaging/test.html

Issue 778243002: Push API: Return cached registration if available. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@store2
Patch Set: Add test, refactor and rebase Created 6 years 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 <!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 <link rel="manifest" href="manifest.json"> 5 <link rel="manifest" href="manifest.json">
6 <script> 6 <script src="test.js"></script>
7 var pushData = new FutureData();
8
9 // Sends data back to the test. This must be in response to an earlier
10 // request, but it's ok to respond asynchronously. The request blocks until
11 // the response is sent.
12 function sendResultToTest(result) {
13 console.log('sendResultToTest: ' + result);
14 if (window.domAutomationController) {
15 domAutomationController.send('' + result);
16 }
17 }
18
19 function sendErrorToTest(error) {
20 sendResultToTest(error.name + ' - ' + error.message);
21 }
22
23 // A container for a single piece of data. The data does not have to be
24 // available yet when the getter is called, as all responses to the test are
25 // asynchronous.
26 function FutureData() {
27 this.data = null;
28 this.waiting = false;
29 }
30
31 // Sends the data to the test if it is available. Otherwise sets the
32 // |waiting| flag.
33 FutureData.prototype.get = function() {
34 if (this.data) {
35 sendResultToTest(this.data);
36 } else {
37 this.waiting = true;
38 }
39 };
40
41 // Sets a new data value. If the |waiting| flag is on, it is turned off and
42 // the data is sent to the test.
43 FutureData.prototype.set = function(data) {
44 this.data = data;
45 if (this.waiting) {
46 sendResultToTest(data);
47 this.waiting = false;
48 }
49 };
50
51 FutureData.prototype.getImmediately = function() {
52 sendResultToTest(this.data);
53 };
54
55 // Notification permission has been coalesced with Push permission. After
56 // this is granted, Push API registration can succeed.
57 function requestNotificationPermission() {
58 Notification.requestPermission(function(permission) {
59 sendResultToTest('permission status - ' + permission);
60 });
61 }
62
63 function registerServiceWorker() {
64 navigator.serviceWorker.register('service_worker.js', {scope: './'}).then(
65 function(swRegistration) {
66 sendResultToTest('ok - service worker registered');
67 }, sendErrorToTest);
68 }
69
70 function unregisterServiceWorker() {
71 navigator.serviceWorker.getRegistration().then(function(swRegistration) {
72 swRegistration.unregister().then(function(result) {
73 sendResultToTest('service worker unregistration status: ' + result);
74 })
75 }).catch(sendErrorToTest);
76 }
77
78 function removeManifest() {
79 var element = document.querySelector('link[rel="manifest"]');
80 if (element) {
81 element.parentNode.removeChild(element);
82 sendResultToTest('manifest removed');
83 } else
84 sendResultToTest('unable to find manifest element');
85 }
86
87 function registerPush() {
88 navigator.serviceWorker.ready.then(function(swRegistration) {
89 // TODO(mvanouwerkerk): Cleanup once the final API is exposed.
90 var pushManager = swRegistration.pushManager || navigator.push;
91 pushManager.register().then(function(pushRegistration) {
92 sendResultToTest(pushRegistration.pushEndpoint + ' - ' +
93 pushRegistration.pushRegistrationId);
94 }, sendErrorToTest);
95 }, sendErrorToTest);
96 }
97
98 function hasPermission() {
99 navigator.serviceWorker.ready.then(function(swRegistration) {
100 // TODO(mvanouwerkerk): Cleanup once the final API is exposed.
101 var pushManager = swRegistration.pushManager || navigator.push;
102 pushManager.hasPermission().then(function(permission) {
103 sendResultToTest('permission status - ' + permission);
104 }, sendErrorToTest);
105 }, sendErrorToTest);
106 }
107
108 function isControlled() {
109 if (navigator.serviceWorker.controller) {
110 sendResultToTest('true - is controlled');
111 } else {
112 sendResultToTest('false - is not controlled');
113 }
114 }
115
116 addEventListener('message', function(event) {
117 var message = JSON.parse(event.data);
118 if (message.type == 'push') {
119 pushData.set(message.data);
120 }
121 }, false);
122 </script>
123 </head> 7 </head>
124 <body> 8 <body>
125 <h1>Push API Test</h1> 9 <h1>Push API Test</h1>
126 </body> 10 </body>
127 </html> 11 </html>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698