2 <script src=
"../resources/testharness.js"></script>
3 <script src=
"../resources/testharnessreport.js"></script>
4 <script src=
"resources/test-helpers.js"></script>
6 var script1
= normalizeURL('resources/empty-worker.js');
7 var script2
= normalizeURL('resources/empty-worker.js?new');
9 async_test(function(t
) {
10 var scope
= 'resources/scope/register-new-script-concurrently';
11 var register_promise1
;
12 var register_promise2
;
14 service_worker_unregister(t
, scope
)
16 register_promise1
= navigator
.serviceWorker
.register(script1
,
18 register_promise2
= navigator
.serviceWorker
.register(script2
,
20 return register_promise1
;
22 .then(function(registration
) {
23 assert_equals(registration
.installing
.scriptURL
, script1
,
24 'on first register, first script should be installing');
25 assert_equals(registration
.waiting
, null,
26 'on first register, waiting should be null');
27 assert_equals(registration
.active
, null,
28 'on first register, active should be null');
29 return register_promise2
;
31 .then(function(registration
) {
33 registration
.installing
.scriptURL
, script2
,
34 'on second register, second script should be installing');
35 // Spec allows racing: the first register may have finished
36 // or the second one could have terminated the installing worker.
37 assert_true(registration
.waiting
== null ||
38 registration
.waiting
.scriptURL
== script1
,
39 'on second register, .waiting should be null or the ' +
41 assert_true(registration
.active
== null ||
42 (registration
.waiting
== null &&
43 registration
.active
.scriptURL
== script1
),
44 'on second register, .active should be null or the ' +
46 return registration
.unregister();
51 .catch(unreached_rejection(t
));
52 }, 'Register different scripts concurrently');
54 async_test(function(t
) {
55 var scope
= 'resources/scope/register-then-register-new-script';
58 service_worker_unregister_and_register(t
, script1
, scope
)
61 return wait_for_state(t
, registration
.installing
, 'activated');
64 assert_equals(registration
.installing
, null,
65 'on activated, installing should be null');
66 assert_equals(registration
.waiting
, null,
67 'on activated, waiting should be null');
68 assert_equals(registration
.active
.scriptURL
, script1
,
69 'on activated, the first script should be active');
70 return navigator
.serviceWorker
.register(script2
, {scope
:scope
});
74 assert_equals(registration
.installing
.scriptURL
, script2
,
75 'on second register, the second script should be ' +
77 assert_equals(registration
.waiting
, null,
78 'on second register, waiting should be null');
79 assert_equals(registration
.active
.scriptURL
, script1
,
80 'on second register, the first script should be ' +
82 return wait_for_state(t
, registration
.installing
, 'installed');
85 assert_equals(registration
.installing
, null,
86 'on installed, installing should be null');
87 assert_equals(registration
.waiting
.scriptURL
, script2
,
88 'on installed, the second script should be waiting');
89 assert_equals(registration
.active
.scriptURL
, script1
,
90 'on installed, the first script should be active');
91 return registration
.unregister();
96 .catch(unreached_rejection(t
));
97 }, 'Register then register new script URL');
99 async_test(function(t
) {
100 var scope
= 'resources/scope/register-then-register-new-script-404';
103 service_worker_unregister_and_register(t
, script1
, scope
)
106 return wait_for_state(t
, registration
.installing
, 'activated');
109 assert_equals(registration
.installing
, null,
110 'on activated, installing should be null');
111 assert_equals(registration
.waiting
, null,
112 'on activated, waiting should be null');
113 assert_equals(registration
.active
.scriptURL
, script1
,
114 'on activated, the first script should be active');
115 return navigator
.serviceWorker
.register('this-will-404.js',
119 function() { assert_unreached('register should reject'); },
121 assert_equals(registration
.installing
, null,
122 'on rejected, installing should be null');
123 assert_equals(registration
.waiting
, null,
124 'on rejected, waiting should be null');
125 assert_equals(registration
.active
.scriptURL
, script1
,
126 'on rejected, the first script should be active');
127 return registration
.unregister();
132 .catch(unreached_rejection(t
));
133 }, 'Register then register new script URL that 404s');
135 async_test(function(t
) {
136 var scope
= 'resources/scope/register-then-register-new-script-reject-install';
137 var reject_script
= normalizeURL('resources/reject-install-worker.js');
140 service_worker_unregister_and_register(t
, script1
, scope
)
143 return wait_for_state(t
, registration
.installing
, 'activated');
146 assert_equals(registration
.installing
, null,
147 'on activated, installing should be null');
148 assert_equals(registration
.waiting
, null,
149 'on activated, waiting should be null');
150 assert_equals(registration
.active
.scriptURL
, script1
,
151 'on activated, the first script should be active');
152 return navigator
.serviceWorker
.register(reject_script
, {scope
:scope
});
156 assert_equals(registration
.installing
.scriptURL
, reject_script
,
157 'on update, the second script should be installing');
158 assert_equals(registration
.waiting
, null,
159 'on update, waiting should be null');
160 assert_equals(registration
.active
.scriptURL
, script1
,
161 'on update, the first script should be active');
162 return wait_for_state(t
, registration
.installing
, 'redundant');
165 assert_equals(registration
.installing
, null,
166 'on redundant, installing should be null');
167 assert_equals(registration
.waiting
, null,
168 'on redundant, waiting should be null');
169 assert_equals(registration
.active
.scriptURL
, script1
,
170 'on redundant, the first script should be active');
171 return registration
.unregister();
176 .catch(unreached_rejection(t
));
177 }, 'Register then register new script that does not install');
179 async_test(function(t
) {
180 var scope
= 'resources/scope/register-new-script-controller';
184 service_worker_unregister_and_register(t
, script1
, scope
)
187 return wait_for_state(t
, registration
.installing
, 'activated');
190 return with_iframe(scope
);
192 .then(function(frame
) {
194 return navigator
.serviceWorker
.register(script2
, { scope
: scope
})
198 return wait_for_state(t
, registration
.installing
, 'installed');
201 var sw_container
= iframe
.contentWindow
.navigator
.serviceWorker
;
202 assert_equals(sw_container
.controller
.scriptURL
, script1
,
203 'the old version should control the old doc');
204 return with_iframe(scope
);
206 .then(function(frame
) {
207 var sw_container
= frame
.contentWindow
.navigator
.serviceWorker
;
208 assert_equals(sw_container
.controller
.scriptURL
, script1
,
209 'the old version should control a new doc');
210 var onactivated_promise
= wait_for_state(t
,
211 registration
.waiting
,
215 return onactivated_promise
;
218 return with_iframe(scope
);
220 .then(function(frame
) {
221 var sw_container
= frame
.contentWindow
.navigator
.serviceWorker
;
222 assert_equals(sw_container
.controller
.scriptURL
, script2
,
223 'the new version should control a new doc');
225 return registration
.unregister();
230 .catch(unreached_rejection(t
));
231 }, 'Register same-scope new script url effect on controller');