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>
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
) {
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(); }));
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) {
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
)
44 registrations
.push(r
);
45 return navigator
.serviceWorker
.getRegistrations();
47 .then(function(value
) {
48 assert_registration_array_equals(
51 'getRegistrations should resolve with array of registrations.');
52 return service_worker_unregister(t
, scope
);
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
)
63 registrations
.push(r
);
64 return service_worker_unregister_and_register(t
, script
, scope2
);
67 registrations
.push(r
);
68 return navigator
.serviceWorker
.getRegistrations();
70 .then(function(value
) {
71 assert_registration_array_equals(
74 'getRegistrations should resolve with array of registrations.');
75 return service_worker_unregister(t
, scope1
);
78 return service_worker_unregister(t
, scope2
);
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();
90 return navigator
.serviceWorker
.getRegistrations();
92 .then(function(value
) {
96 'getRegistrations should resolve with an empty array.');
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';
109 var registrations
= [];
111 return with_iframe(frame_url
)
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
)
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 ' +
134 channel
.port1
.postMessage('unregister');
136 } else if (e
.data
== 'unregistered') {
140 frame
.contentWindow
.postMessage('register', '*', [channel
.port2
]);
145 return service_worker_unregister(t
, scope
);
147 }, 'getRegistrations promise resolves only with same origin registrations.');