2 <script src="../resources/testharness.js"></script>
3 <script src="../resources/testharnessreport.js"></script>
4 <script src="resources/test-helpers.js"></script>
6 var worker_url = 'resources/empty-worker.js';
8 async_test(function(t) {
9 var scope = 'resources/scope/register-waits-for-unregistered-registration-to-clear';
10 var new_worker_url = worker_url + '?new';
15 service_worker_unregister_and_register(t, worker_url, scope)
18 return wait_for_update(t, registration);
20 .then(function(worker) {
21 return wait_for_state(t, worker, 'activated');
24 return with_iframe(scope);
26 .then(function(frame) {
28 return registration.unregister();
31 setTimeout(function() {
33 unload_iframe(iframe);
35 return navigator.serviceWorker.register(new_worker_url,
38 .then(function(new_registration) {
40 'register should not resolve until iframe unloaded');
41 assert_equals(registration.installing, null,
42 'registration.installing');
43 assert_equals(registration.waiting, null, 'registration.waiting');
44 assert_equals(registration.active, null, 'registration.active');
45 return new_registration.unregister();
50 .catch(unreached_rejection(t));
51 }, 'Registering a new script URL does not resolve until unregistered ' +
52 'registration is cleared');
54 async_test(function(t) {
55 var scope = 'resources/scope/unregister-then-register-new-script-that-exists';
56 var new_worker_url = worker_url + '?new';
60 service_worker_unregister_and_register(t, worker_url, scope)
63 return wait_for_update(t, registration);
65 .then(function(worker) {
66 return wait_for_state(t, worker, 'activated');
69 return with_iframe(scope);
71 .then(function(frame) {
73 return registration.unregister();
76 var promise = navigator.serviceWorker.register(new_worker_url,
78 unload_iframe(iframe);
81 .then(function(new_registration) {
82 assert_not_equals(registration, new_registration,
83 'register() should resolve to a new registration');
84 assert_equals(registration.installing, null,
85 'old registration.installing');
86 assert_equals(registration.waiting, null,
87 'old registration.waiting');
88 assert_equals(registration.active, null,
89 'old registration.active');
90 registration = new_registration;
91 return wait_for_update(t, registration);
93 .then(function(worker) {
94 assert_equals(registration.installing.scriptURL,
95 normalizeURL(new_worker_url),
96 'new registration.installing');
97 assert_equals(registration.waiting, null,
98 'new registration.waiting');
99 assert_equals(registration.active, null,
100 'new registration.active');
101 return wait_for_state(t, worker, 'activated');
104 return with_iframe(scope);
106 .then(function(frame) {
108 frame.contentWindow.navigator.serviceWorker.controller.scriptURL,
109 normalizeURL(new_worker_url),
110 'the new worker should control a new document');
111 unload_iframe(frame);
112 return registration.unregister();
117 .catch(unreached_rejection(t));
118 }, 'Registering a new script URL while an unregistered registration is in use');
120 async_test(function(t) {
121 var scope = 'resources/scope/unregister-then-register-new-script-that-404s';
125 service_worker_unregister_and_register(t, worker_url, scope)
128 return wait_for_update(t, registration);
130 .then(function(worker) {
131 return wait_for_state(t, worker, 'activated');
134 return with_iframe(scope);
136 .then(function(frame) {
138 return registration.unregister();
141 var promise = navigator.serviceWorker.register('this-will-404',
143 unload_iframe(iframe);
148 assert_unreached('register should reject the promise');
151 return with_iframe(scope);
153 .then(function(frame) {
154 assert_equals(frame.contentWindow.navigator.serviceWorker.controller,
156 'document should not load with a controller');
157 unload_iframe(frame);
160 .catch(unreached_rejection(t));
161 }, 'Registering a new script URL that 404s does not resurrect an ' +
162 'unregistered registration');
164 async_test(function(t) {
165 var scope = 'resources/scope/unregister-then-register-reject-install-worker';
169 service_worker_unregister_and_register(t, worker_url, scope)
172 return wait_for_update(t, registration);
174 .then(function(worker) {
175 return wait_for_state(t, worker, 'activated');
178 return with_iframe(scope);
180 .then(function(frame) {
182 return registration.unregister();
185 var promise = navigator.serviceWorker.register(
186 'resources/reject-install-worker.js', { scope: scope });
187 unload_iframe(iframe);
192 return wait_for_update(t, registration);
194 .then(function(worker) {
195 return wait_for_state(t, worker, 'redundant');
198 return with_iframe(scope);
200 .then(function(frame) {
201 assert_equals(frame.contentWindow.navigator.serviceWorker.controller,
203 'document should not load with a controller');
204 unload_iframe(frame);
205 return registration.unregister();
210 .catch(unreached_rejection(t));
211 }, 'Registering a new script URL that fails to install does not resurrect ' +
212 'an unregistered registration');