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

Side by Side Diff: third_party/WebKit/LayoutTests/http/tests/notifications/instrumentation-service-worker.js

Issue 1907443007: Use promises in notifications tests and enable controlling the page. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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 importScripts('/resources/testharness-helpers.js'); 1 importScripts('/resources/testharness-helpers.js');
2 2
3 // For copying Notification.data. Currently a deep copy algorithm is used. Note 3 // For copying Notification.data. Currently a deep copy algorithm is used. Note
4 // that the robustness of this function (and also |assert_object_equals| in 4 // that the robustness of this function (and also |assert_object_equals| in
5 // testharness.js) affects the types of possible testing can be done. 5 // testharness.js) affects the types of possible testing can be done.
6 // TODO(peter): change this to a structured clone algorithm. 6 // TODO(peter): change this to a structured clone algorithm.
7 function cloneObject(src) { 7 function cloneObject(src) {
8 if (typeof src != 'object' || src === null) 8 if (typeof src != 'object' || src === null)
9 return src; 9 return src;
10 var dst = Array.isArray(src) ? [] : {}; 10 var dst = Array.isArray(src) ? [] : {};
11 for (var property in src) { 11 for (var property in src) {
12 if (src.hasOwnProperty(property)) 12 if (src.hasOwnProperty(property))
13 dst[property] = cloneObject(src[property]); 13 dst[property] = cloneObject(src[property]);
14 } 14 }
15 return dst; 15 return dst;
16 } 16 }
17 17
18 // Copies the serializable attributes of |notification|. 18 // Copies the serializable attributes of |notification|.
19 function cloneNotification(notification) { 19 function cloneNotification(notification) {
20 var copiedNotification = JSON.parse(stringifyDOMObject(notification)); 20 var copiedNotification = JSON.parse(stringifyDOMObject(notification));
21 copiedNotification.data = cloneObject(notification.data); 21 copiedNotification.data = cloneObject(notification.data);
22 return copiedNotification; 22 return copiedNotification;
23 } 23 }
24 24
25 // Allows a document to exercise the Notifications API within a service worker b y sending commands. 25 // Allows a document to exercise the Notifications API within a service worker b y sending commands.
26 var messagePort = null; 26 var messagePort = null;
27 27
28 addEventListener('message', function(workerEvent) { 28 // All urls of requests that have been routed through the fetch event handler.
29 var fetchHistory = [];
30
31 addEventListener('install', event => {
32 event.waitUntil(skipWaiting());
33 });
34
35 addEventListener('activate', event => {
36 event.waitUntil(clients.claim());
37 });
38
39 addEventListener('message', workerEvent => {
29 messagePort = workerEvent.data; 40 messagePort = workerEvent.data;
30 41
31 // Listen to incoming commands on the message port. 42 // Listen to incoming commands on the message port.
32 messagePort.onmessage = function(event) { 43 messagePort.onmessage = event => {
33 if (typeof event.data != 'object' || !event.data.command) 44 if (typeof event.data != 'object' || !event.data.command)
34 return; 45 return;
35 46
36 switch (event.data.command) { 47 switch (event.data.command) {
37 case 'permission': 48 case 'permission':
38 messagePort.postMessage({ command: event.data.command, 49 messagePort.postMessage({ command: event.data.command,
39 value: Notification.permission }); 50 value: Notification.permission });
40 break; 51 break;
41 52
42 case 'show': 53 case 'show':
43 registration.showNotification(event.data.title, event.data.optio ns).then(function() { 54 registration.showNotification(event.data.title, event.data.optio ns).then(() => {
44 messagePort.postMessage({ command: event.data.command, 55 messagePort.postMessage({ command: event.data.command,
45 success: true }); 56 success: true });
46 }, function(error) { 57 }, error => {
47 messagePort.postMessage({ command: event.data.command, 58 messagePort.postMessage({ command: event.data.command,
48 success: false, 59 success: false,
49 message: error.message }); 60 message: error.message });
50 }); 61 });
51 break; 62 break;
52 63
64 case 'get-fetch-history':
Peter Beverloo 2016/04/21 17:20:56 nit: try to scope changes like this to the change
Michael van Ouwerkerk 2016/04/22 09:44:22 Acknowledged.
65 messagePort.postMessage({ command: event.data.command,
66 fetchHistory: fetchHistory });
67 break;
68
53 case 'get': 69 case 'get':
54 var filter = {}; 70 var filter = {};
55 if (typeof (event.data.filter) !== 'undefined') 71 if (typeof (event.data.filter) !== 'undefined')
56 filter = event.data.filter; 72 filter = event.data.filter;
57 73
58 registration.getNotifications(filter).then(function(notification s) { 74 registration.getNotifications(filter).then(notifications => {
59 var clonedNotifications = []; 75 var clonedNotifications = [];
60 for (var notification of notifications) 76 for (var notification of notifications)
61 clonedNotifications.push(cloneNotification(notification) ); 77 clonedNotifications.push(cloneNotification(notification) );
62 78
63 messagePort.postMessage({ command: event.data.command, 79 messagePort.postMessage({ command: event.data.command,
64 success: true, 80 success: true,
65 notifications: clonedNotifications }); 81 notifications: clonedNotifications });
66 }, function(error) { 82 }, error => {
67 messagePort.postMessage({ command: event.data.command, 83 messagePort.postMessage({ command: event.data.command,
68 success: false, 84 success: false,
69 message: error.message }); 85 message: error.message });
70 }); 86 });
71 break; 87 break;
72 88
73 case 'request-permission-exists': 89 case 'request-permission-exists':
74 messagePort.postMessage({ command: event.data.command, 90 messagePort.postMessage({ command: event.data.command,
75 value: 'requestPermission' in Notifica tion }); 91 value: 'requestPermission' in Notifica tion });
76 break; 92 break;
77 93
78 default: 94 default:
79 messagePort.postMessage({ command: 'error', message: 'Invalid co mmand: ' + event.data.command }); 95 messagePort.postMessage({ command: 'error', message: 'Invalid co mmand: ' + event.data.command });
80 break; 96 break;
81 } 97 }
82 }; 98 };
83 99
84 // Notify the controller that the worker is now available. 100 // Notify the controller that the worker is now available.
85 messagePort.postMessage('ready'); 101 messagePort.postMessage('ready');
86 }); 102 });
87 103
88 addEventListener('notificationclick', function(event) { 104 addEventListener('notificationclick', event => {
89 var notificationCopy = cloneNotification(event.notification); 105 var notificationCopy = cloneNotification(event.notification);
90 106
91 // Notifications containing "ACTION:CLOSE" in their message will be closed 107 // Notifications containing "ACTION:CLOSE" in their message will be closed
92 // immediately by the Service Worker. 108 // immediately by the Service Worker.
93 if (event.notification.body.indexOf('ACTION:CLOSE') != -1) 109 if (event.notification.body.indexOf('ACTION:CLOSE') != -1)
94 event.notification.close(); 110 event.notification.close();
95 111
96 // Notifications containing "ACTION:OPENWINDOW" in their message will attemp t 112 // Notifications containing "ACTION:OPENWINDOW" in their message will attemp t
97 // to open a new window for an example URL. 113 // to open a new window for an example URL.
98 if (event.notification.body.indexOf('ACTION:OPENWINDOW') != -1) 114 if (event.notification.body.indexOf('ACTION:OPENWINDOW') != -1)
99 event.waitUntil(clients.openWindow('https://example.com/')); 115 event.waitUntil(clients.openWindow('https://example.com/'));
100 116
101 messagePort.postMessage({ command: 'click', 117 messagePort.postMessage({ command: 'click',
102 notification: notificationCopy, 118 notification: notificationCopy,
103 action: event.action }); 119 action: event.action });
104 }); 120 });
105 121
106 addEventListener('notificationclose', function(event) { 122 addEventListener('notificationclose', event => {
107 var notificationCopy = cloneNotification(event.notification); 123 var notificationCopy = cloneNotification(event.notification);
108 messagePort.postMessage({ command: 'close', 124 messagePort.postMessage({ command: 'close',
109 notification: notificationCopy }); 125 notification: notificationCopy });
110 }); 126 });
127
128 addEventListener('fetch', event => {
129 fetchHistory.push(event.request.url);
130 event.respondWith(fetch(event.request));
131 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698