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

Side by Side Diff: third_party/WebKit/LayoutTests/http/tests/serviceworker/chromium/usecounter.html

Issue 2658603003: ServiceWorker: Enable UseCounter for ServiceWorkerGlobalScope (Closed)
Patch Set: int32_t -> uint32_t Created 3 years, 10 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 <!DOCTYPE html>
2 <title>Service Worker: UseCounter</title>
3 <script src="../../resources/testharness.js"></script>
4 <script src="../../resources/testharnessreport.js"></script>
5 <script src="../resources/test-helpers.js"></script>
6 <script>
7
8 const kFeature = 675; // From UseCounter.h
9 const kDeprecatedFeature = 538; // From Deprecation.h
10
11 function isUseCounted(win, feature) {
12 return win.internals.isUseCounted(win.document, feature);
13 }
14
15 function observeUseCounter(win, feature) {
16 return win.internals.observeUseCounter(win.document, feature);
17 }
18
19 // Use a window instead of an iframe because UseCounter is shared among frames
20 // in a document and these tests cannot be conducted in such an environment.
21 // A window has its own UseCounter.
22 function openWindow(url) {
23 return new Promise(resolve => {
24 let win = window.open(url, '_blank');
25 add_completion_callback(() => win.close());
26 window.onmessage = e => {
27 assert_equals(e.data, 'LOADED');
28 resolve(win);
29 };
30 });
31 }
32
33 promise_test(t => {
34 const kUrl = 'resources/usecounter-worker.js';
35 const kScope = 'resources/usecounter-window.html?basic';
36 let worker;
37 let win1;
38 let win2;
39
40 return service_worker_unregister_and_register(t, kUrl, kScope)
41 .then(registration => {
42 add_completion_callback(function() { registration.unregister(); });
43 worker = registration.installing;
44 return wait_for_state(t, registration.installing, 'activated');
45 })
46 .then(() => { return openWindow(kScope); })
47 .then(win => {
48 win1 = win;
49 return openWindow(kScope);
50 })
51 .then(win => {
52 win2 = win;
53
54 assert_false(isUseCounted(win1, kFeature));
55 assert_false(isUseCounted(win2, kFeature));
56
57 // Request to count a feature.
58 worker.postMessage({type: 'COUNT_FEATURE', feature: kFeature});
59 return Promise.all([
60 observeUseCounter(win1, kFeature),
61 observeUseCounter(win2, kFeature)
62 ]);
63 })
64 .then(() => {
65 // API use on ServiceWorkerGlobalScope should be recorded in all
66 // controlled windows.
67 assert_true(isUseCounted(win1, kFeature));
68 assert_true(isUseCounted(win2, kFeature));
69
70 assert_false(isUseCounted(win1, kDeprecatedFeature));
71 assert_false(isUseCounted(win2, kDeprecatedFeature));
72
73 // Request to count a deprecated feature.
74 worker.postMessage(
75 {type: 'COUNT_DEPRECATION', feature: kDeprecatedFeature});
76 return Promise.all([
77 observeUseCounter(win1, kDeprecatedFeature),
78 observeUseCounter(win2, kDeprecatedFeature)
79 ]);
80 })
81 .then(() => {
82 // Deprecated API use on ServiceWorkerGlobalScope should be recorded
83 // in all controlled windows.
84 assert_true(isUseCounted(win1, kDeprecatedFeature));
85 assert_true(isUseCounted(win2, kDeprecatedFeature));
86
87 return openWindow(kScope);
88 })
89 .then(win => {
90 assert_true(isUseCounted(win, kFeature));
91 assert_true(isUseCounted(win, kDeprecatedFeature));
92 });
93 }, 'UseCounter on ServiceWorkerGlobalScope');
94
95 promise_test(t => {
96 const kUrl = 'resources/usecounter-worker.js';
97 const kScope = 'resources/usecounter-window.html?claim';
98 let worker;
99 let win1;
100 let win2;
101
102 return openWindow(kScope)
103 .then(win => {
104 win1 = win;
105 return openWindow(kScope);
106 })
107 .then(win => {
108 win2 = win;
109 return service_worker_unregister_and_register(t, kUrl, kScope)
110 })
111 .then(registration => {
112 add_completion_callback(function() { registration.unregister(); });
113 worker = registration.installing;
114 return wait_for_state(t, registration.installing, 'activated');
115 })
116 .then(() => {
117 // Request to count a feature.
118 worker.postMessage({type: 'COUNT_FEATURE', feature: kFeature});
119 return new Promise(resolve => {
120 navigator.serviceWorker.onmessage = resolve;
121 // There is no way to verify that API use is never counted. As a
122 // workaround, wait for only one round-trip.
123 worker.postMessage({type: 'PING'});
124 });
125 })
126 .then(e => {
127 assert_equals(e.data.type, 'PONG');
128
129 // API use on ServiceWorkerGlobalScope should not be recorded in
130 // windows because they are not controlled yet.
131 assert_false(isUseCounted(win1, kFeature));
132 assert_false(isUseCounted(win2, kFeature));
133
134 // Request to count a deprecated feature.
135 worker.postMessage(
136 {type: 'COUNT_DEPRECATION', feature: kDeprecatedFeature});
137 return new Promise(resolve => {
138 navigator.serviceWorker.onmessage = resolve;
139 // There is no way to verify that API use is never counted. As a
140 // workaround, wait for only one round-trip.
141 worker.postMessage({type: 'PING'});
142 });
143 })
144 .then(e => {
145 assert_equals(e.data.type, 'PONG');
146
147 // Deprecated API use on ServiceWorkerGlobalScope should not be
148 // recorded in windows because they are not controlled yet.
149 assert_false(isUseCounted(win1, kDeprecatedFeature));
150 assert_false(isUseCounted(win2, kDeprecatedFeature));
151
152 assert_equals(win1.navigator.serviceWorker.controller, null);
153 assert_equals(win2.navigator.serviceWorker.controller, null);
154
155 // Request to claim.
156 return new Promise(resolve => {
157 navigator.serviceWorker.onmessage = resolve;
158 worker.postMessage({type: 'CLAIM'});
159 });
160 })
161 .then(e => {
162 assert_equals(e.data.type, 'CLAIMED');
163 assert_false(e.data.restarted);
164 assert_not_equals(win1.navigator.serviceWorker.controller, null);
165 assert_not_equals(win2.navigator.serviceWorker.controller, null);
166
167 // The windows are now controlled by the service worker. Their
168 // UseCounter should be synchronized with worker's counter.
169 assert_true(isUseCounted(win1, kFeature));
170 assert_true(isUseCounted(win2, kFeature));
171 assert_true(isUseCounted(win1, kDeprecatedFeature));
172 assert_true(isUseCounted(win2, kDeprecatedFeature));
173 });
174 }, 'UseCounter on ServiceWorkerGlobalScope - A use counter owned by newly ' +
175 'controlled window should be synchronized with worker\'s counter');
176
177 // Test that features used during service worker installation are persisted.
178 // This test could be non-deterministic because there is no handy way to
179 // sweep out on-memory representation of ServiceWorker in the browser process
180 // and make sure to restore it from the storage.
181 promise_test(t => {
182 const kUrl = 'resources/usecounter-worker.js';
183 const kScope = 'resources/usecounter-window.html' +
184 '?type=features-during-install' +
185 '&feature=' + kFeature +
186 '&deprecated=' + kDeprecatedFeature;
187 let worker;
188 let win1;
189 let win2;
190
191 return openWindow(kScope)
192 .then(win => {
193 win1 = win;
194 return openWindow(kScope);
195 })
196 .then(win => {
197 win2 = win;
198 // A service worker will call some APIs during the install event.
199 return service_worker_unregister_and_register(t, kUrl, kScope)
200 })
201 .then(registration => {
202 add_completion_callback(function() { registration.unregister(); });
203 worker = registration.installing;
204 return wait_for_state(t, registration.installing, 'activated');
205 })
206 .then(e => {
207 assert_equals(win1.navigator.serviceWorker.controller, null);
208 assert_equals(win2.navigator.serviceWorker.controller, null);
209
210 // API use on ServiceWorkerGlobalScope should not be recorded in
211 // windows because they are not controlled yet.
212 assert_false(isUseCounted(win1, kFeature));
213 assert_false(isUseCounted(win2, kFeature));
214 assert_false(isUseCounted(win1, kDeprecatedFeature));
215 assert_false(isUseCounted(win2, kDeprecatedFeature));
216
217 // Terminate the service worker.
218 internals.terminateServiceWorker(worker);
219
220 // Request to claim. This will restart the service worker.
221 return new Promise(resolve => {
222 navigator.serviceWorker.onmessage = resolve;
223 worker.postMessage({type: 'CLAIM'});
224 });
225 })
226 .then(e => {
227 assert_equals(e.data.type, 'CLAIMED');
228 assert_true(e.data.restarted);
229 assert_not_equals(win1.navigator.serviceWorker.controller, null);
230 assert_not_equals(win2.navigator.serviceWorker.controller, null);
231
232 // The windows are now controlled by the service worker. Their
233 // UseCounter should be synchronized with worker's counter retrieved
234 // from the storage.
235 assert_true(isUseCounted(win1, kFeature));
236 assert_true(isUseCounted(win2, kFeature));
237 assert_true(isUseCounted(win1, kDeprecatedFeature));
238 assert_true(isUseCounted(win2, kDeprecatedFeature));
239 });
240 }, 'UseCounter on ServiceWorkerGlobalScope - counts during the install ' +
241 'event should be persisted');
242
243 // TODO(nhiroki): Test that features used after service worker installation are
244 // not persisted. This could be impossible because there is no handy way to
245 // sweep out on-memory representation of ServiceWorker in the browser process
246 // and make sure to restore it from the storage.
247
248 promise_test(t => {
249 const kUrl = 'resources/usecounter-worker.js';
250 const kScope = 'resources/usecounter-window.html?type=skip-waiting';
251 let worker1;
252 let worker2;
253 let win1;
254 let win2;
255
256 return service_worker_unregister_and_register(t, kUrl, kScope)
257 .then(registration => {
258 add_completion_callback(function() { registration.unregister(); });
259 worker1 = registration.installing;
260 return wait_for_state(t, registration.installing, 'activated');
261 })
262 .then(() => { return openWindow(kScope); })
263 .then(win => {
264 win1 = win;
265 assert_false(isUseCounted(win1, kFeature));
266
267 // Request to count a feature.
268 worker1.postMessage({type: 'COUNT_FEATURE', feature: kFeature});
269 return observeUseCounter(win1, kFeature);
270 })
271 .then(e => {
272 // API use on ServiceWorkerGlobalScope should be recorded in a
273 // controlled window.
274 assert_true(isUseCounted(win1, kFeature));
275
276 // Update a controller using skipWaiting().
277 return navigator.serviceWorker.register(
278 kUrl + '?skip-waiting', {scope: kScope});
279 })
280 .then(registration => {
281 add_completion_callback(function() { registration.unregister(); });
282 worker2 = registration.installing;
283 // Wait until the new worker gets activated.
284 return wait_for_state(t, worker2, 'activated');
285 })
286 .then(() => { return openWindow(kScope); })
287 .then(win => {
288 // This window wasn't controlled by the previous worker.
289 win2 = win;
290 assert_not_equals(win2.navigator.serviceWorker.controller, undefined);
291
292 // An updated worker does not take over the previous counter, so API
293 // use on the previous worker should not be recorded in the newly
294 // controlled window.
295 assert_true(isUseCounted(win1, kFeature));
296 assert_false(isUseCounted(win2, kFeature));
297
298 assert_false(isUseCounted(win1, kDeprecatedFeature));
299 assert_false(isUseCounted(win2, kDeprecatedFeature));
300
301 // Request to count a deprecated feature.
302 worker2.postMessage(
303 {type: 'COUNT_DEPRECATION', feature: kDeprecatedFeature});
304 return Promise.all([
305 observeUseCounter(win1, kDeprecatedFeature),
306 observeUseCounter(win2, kDeprecatedFeature)
307 ]);
308 })
309 .then(e => {
310 // Deprecated API use on the updated worker should be recorded in
311 // all controlled windows.
312 assert_true(isUseCounted(win1, kFeature));
313 assert_false(isUseCounted(win2, kFeature));
314 assert_true(isUseCounted(win1, kDeprecatedFeature));
315 assert_true(isUseCounted(win2, kDeprecatedFeature));
316 });
317 }, 'UseCounter on ServiceWorkerGlobalScope - an updated worker should not ' +
318 'take over a previous counter');
319
320 // TODO(nhiroki): Test a case where ServiceWorker controls SharedWorker that is
321 // connected from multiple windows. In such a case, API use on ServiceWorker
322 // should be propagated to all connecting windows via SharedWorker.
323
324 </script>
325 </html>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698