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

Side by Side Diff: third_party/WebKit/LayoutTests/http/tests/notifications/resources/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: Rebase. Address peter's comments. 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
(Empty)
1 importScripts('/resources/testharness-helpers.js');
2
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
5 // testharness.js) affects the types of possible testing can be done.
6 // TODO(peter): change this to a structured clone algorithm.
7 function cloneObject(src) {
8 if (typeof src != 'object' || src === null)
9 return src;
10 var dst = Array.isArray(src) ? [] : {};
11 for (var property in src) {
12 if (src.hasOwnProperty(property))
13 dst[property] = cloneObject(src[property]);
14 }
15 return dst;
16 }
17
18 // Copies the serializable attributes of |notification|.
19 function cloneNotification(notification) {
20 var copiedNotification = JSON.parse(stringifyDOMObject(notification));
21 copiedNotification.data = cloneObject(notification.data);
22 return copiedNotification;
23 }
24
25 // Allows a document to exercise the Notifications API within a service worker b y sending commands.
26 var messagePort = null;
27
28 addEventListener('message', function(workerEvent) {
29 messagePort = workerEvent.data;
30
31 // Listen to incoming commands on the message port.
32 messagePort.onmessage = function(event) {
33 if (typeof event.data != 'object' || !event.data.command)
34 return;
35
36 switch (event.data.command) {
37 case 'permission':
38 messagePort.postMessage({ command: event.data.command,
39 value: Notification.permission });
40 break;
41
42 case 'show':
43 registration.showNotification(event.data.title, event.data.optio ns).then(function() {
44 messagePort.postMessage({ command: event.data.command,
45 success: true });
46 }, function(error) {
47 messagePort.postMessage({ command: event.data.command,
48 success: false,
49 message: error.message });
50 });
51 break;
52
53 case 'get':
54 var filter = {};
55 if (typeof (event.data.filter) !== 'undefined')
56 filter = event.data.filter;
57
58 registration.getNotifications(filter).then(function(notification s) {
59 var clonedNotifications = [];
60 for (var notification of notifications)
61 clonedNotifications.push(cloneNotification(notification) );
62
63 messagePort.postMessage({ command: event.data.command,
64 success: true,
65 notifications: clonedNotifications });
66 }, function(error) {
67 messagePort.postMessage({ command: event.data.command,
68 success: false,
69 message: error.message });
70 });
71 break;
72
73 case 'request-permission-exists':
74 messagePort.postMessage({ command: event.data.command,
75 value: 'requestPermission' in Notifica tion });
76 break;
77
78 default:
79 messagePort.postMessage({ command: 'error', message: 'Invalid co mmand: ' + event.data.command });
80 break;
81 }
82 };
83
84 // Notify the controller that the worker is now available.
85 messagePort.postMessage('ready');
86 });
87
88 addEventListener('notificationclick', function(event) {
89 var notificationCopy = cloneNotification(event.notification);
90
91 // Notifications containing "ACTION:CLOSE" in their message will be closed
92 // immediately by the Service Worker.
93 if (event.notification.body.indexOf('ACTION:CLOSE') != -1)
94 event.notification.close();
95
96 // Notifications containing "ACTION:OPENWINDOW" in their message will attemp t
97 // to open a new window for an example URL.
98 if (event.notification.body.indexOf('ACTION:OPENWINDOW') != -1)
99 event.waitUntil(clients.openWindow('https://example.com/'));
100
101 messagePort.postMessage({ command: 'click',
102 notification: notificationCopy,
103 action: event.action });
104 });
105
106 addEventListener('notificationclose', function(event) {
107 var notificationCopy = cloneNotification(event.notification);
108 messagePort.postMessage({ command: 'close',
109 notification: notificationCopy });
110 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698