Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / serviceworker / getregistrations.html
blobff054eedff8a2a9f59c003f7b2c80c2a3c21aa04
1 <!DOCTYPE html>
2 <title>Service Worker: getRegistrations()</title>
3 <script src="../resources/testharness.js"></script>
4 <script src="../resources/testharnessreport.js"></script>
5 <script src="../resources/testharness-helpers.js"></script>
6 <script src="resources/test-helpers.js"></script>
7 <script src="../resources/get-host-info.js"></script>
8 <script>
9 // Purge the existing registrations for the origin.
10 // getRegistrations() is used in order to avoid adding additional complexity
11 // e.g. adding an internal function.
12 promise_test(function(t) {
13 var resolve;
14 var timer;
15 var p = new Promise(function(r) { resolve = r; });
16 navigator.serviceWorker.getRegistrations()
17 .then(function(regs) {
18 return Promise.all(regs.map(function(r) { r.unregister(); }));
20 .then(function() {
21 // As registration.unregister() promises resolve before the
22 // corresponding registrations are deleted from the storage, we must
23 // wait until the registrations are actually removed from the storage.
24 // Spec reference: https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#unregister-algorithm
25 timer = setInterval(function() {
26 navigator.serviceWorker.getRegistrations()
27 .then(function(regs) {
28 if (regs.length == 0) {
29 clearInterval(timer);
30 resolve();
32 });
33 }, 100);
34 });
35 return p;
36 }, 'Purge the existing registrations.');
38 promise_test(function(t) {
39 var scope = 'resources/scope/getregistrations/normal';
40 var script = 'resources/empty-worker.js';
41 var registrations = [];
42 return service_worker_unregister_and_register(t, script, scope)
43 .then(function(r) {
44 registrations.push(r);
45 return navigator.serviceWorker.getRegistrations();
47 .then(function(value) {
48 assert_registration_array_equals(
49 value,
50 registrations,
51 'getRegistrations should resolve with array of registrations.');
52 return service_worker_unregister(t, scope);
53 });
54 }, 'Register then getRegistrations');
56 promise_test(function(t) {
57 var scope1 = 'resources/scope/getregistrations/scope1';
58 var scope2 = 'resources/scope/getregistrations/scope2';
59 var script = 'resources/empty-worker.js';
60 var registrations = [];
61 return service_worker_unregister_and_register(t, script, scope1)
62 .then(function(r) {
63 registrations.push(r);
64 return service_worker_unregister_and_register(t, script, scope2);
66 .then(function(r) {
67 registrations.push(r);
68 return navigator.serviceWorker.getRegistrations();
70 .then(function(value) {
71 assert_registration_array_equals(
72 value,
73 registrations,
74 'getRegistrations should resolve with array of registrations.');
75 return service_worker_unregister(t, scope1);
77 .then(function() {
78 return service_worker_unregister(t, scope2);
79 });
80 }, 'Register multiple times then getRegistrations');
82 promise_test(function(t) {
83 var scope = 'resources/scope/getregistrations/register-unregister';
84 var script = 'resources/empty-worker.js';
85 return service_worker_unregister_and_register(t, script, scope)
86 .then(function(registration) {
87 return registration.unregister();
89 .then(function() {
90 return navigator.serviceWorker.getRegistrations();
92 .then(function(value) {
93 assert_array_equals(
94 value,
95 [],
96 'getRegistrations should resolve with an empty array.');
97 });
98 }, 'Register then Unregister then getRegistrations');
100 promise_test(function(t) {
101 // Top-level window's origin: http://127.0.0.1:8000.
102 // Frame's origin: http://localhost:8000.
103 var host_info = get_host_info();
104 var frame_url = host_info['HTTP_REMOTE_ORIGIN'] +
105 '/serviceworker/resources/frame-for-getregistrations.html';
106 var scope = 'resources/scope-for-getregistrations';
107 var script = 'resources/empty-worker.js';
108 var frame;
109 var registrations = [];
111 return with_iframe(frame_url)
112 .then(function(f) {
113 frame = f;
115 var resolve;
116 var p = new Promise(function(r) { resolve = r; });
118 var channel = new MessageChannel();
120 channel.port1.onmessage = function(e) {
121 // Frame's registration is registered.
122 if (e.data == 'registered') {
123 // Top-level window registers a registration scoped
124 // http://127.0.0.1:8000/serviceworker/resources/scope-for-getregistrations.
125 service_worker_unregister_and_register(t, script, scope)
126 .then(function(r) {
127 registrations.push(r);
128 return navigator.serviceWorker.getRegistrations();
130 .then(function(value) {
131 assert_registration_array_equals(value, registrations,
132 'getRegistrations should return only the same origin ' +
133 'registrations.');
134 channel.port1.postMessage('unregister');
136 } else if (e.data == 'unregistered') {
137 resolve();
140 frame.contentWindow.postMessage('register', '*', [channel.port2]);
141 return p;
143 .then(function() {
144 frame.remove();
145 return service_worker_unregister(t, scope);
147 }, 'getRegistrations promise resolves only with same origin registrations.');
149 done();
150 </script>