Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / serviceworker / unregister-then-register-new-script.html
1 <!DOCTYPE html>
2 <script src="../resources/testharness.js"></script>
3 <script src="../resources/testharnessreport.js"></script>
4 <script src="resources/test-helpers.js"></script>
5 <script>
6 var worker_url = 'resources/empty-worker.js';
7
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';
11     var iframe;
12     var registration;
13     var unloaded = false;
14
15     service_worker_unregister_and_register(t, worker_url, scope)
16       .then(function(r) {
17           registration = r;
18           return wait_for_update(t, registration);
19         })
20       .then(function(worker) {
21           return wait_for_state(t, worker, 'activated');
22         })
23       .then(function() {
24           return with_iframe(scope);
25         })
26       .then(function(frame) {
27           iframe = frame;
28           return registration.unregister();
29         })
30       .then(function() {
31           setTimeout(function() {
32               unloaded = true;
33               unload_iframe(iframe);
34             }, 10);
35           return navigator.serviceWorker.register(new_worker_url,
36                                                   { scope: scope });
37         })
38       .then(function(new_registration) {
39           assert_true(unloaded,
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();
46         })
47       .then(function() {
48           t.done();
49         })
50       .catch(unreached_rejection(t));
51   }, 'Registering a new script URL does not resolve until unregistered ' +
52      'registration is cleared');
53
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';
57     var iframe;
58     var registration;
59
60     service_worker_unregister_and_register(t, worker_url, scope)
61       .then(function(r) {
62           registration = r;
63           return wait_for_update(t, registration);
64         })
65       .then(function(worker) {
66           return wait_for_state(t, worker, 'activated');
67         })
68       .then(function() {
69           return with_iframe(scope);
70         })
71       .then(function(frame) {
72           iframe = frame;
73           return registration.unregister();
74         })
75       .then(function() {
76           var promise = navigator.serviceWorker.register(new_worker_url,
77                                                          { scope: scope });
78           unload_iframe(iframe);
79           return promise;
80         })
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);
92         })
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');
102         })
103       .then(function() {
104           return with_iframe(scope);
105         })
106       .then(function(frame) {
107           assert_equals(
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();
113         })
114       .then(function() {
115           t.done();
116         })
117       .catch(unreached_rejection(t));
118 }, 'Registering a new script URL while an unregistered registration is in use');
119
120 async_test(function(t) {
121     var scope = 'resources/scope/unregister-then-register-new-script-that-404s';
122     var iframe;
123     var registration;
124
125     service_worker_unregister_and_register(t, worker_url, scope)
126       .then(function(r) {
127           registration = r;
128           return wait_for_update(t, registration);
129         })
130       .then(function(worker) {
131           return wait_for_state(t, worker, 'activated');
132         })
133       .then(function() {
134           return with_iframe(scope);
135         })
136       .then(function(frame) {
137           iframe = frame;
138           return registration.unregister();
139         })
140       .then(function() {
141           var promise = navigator.serviceWorker.register('this-will-404',
142                                                          { scope: scope });
143           unload_iframe(iframe);
144           return promise;
145         })
146       .then(
147         function() {
148           assert_unreached('register should reject the promise');
149         },
150         function() {
151           return with_iframe(scope);
152         })
153       .then(function(frame) {
154           assert_equals(frame.contentWindow.navigator.serviceWorker.controller,
155                         null,
156                         'document should not load with a controller');
157           unload_iframe(frame);
158           t.done();
159         })
160       .catch(unreached_rejection(t));
161 }, 'Registering a new script URL that 404s does not resurrect an ' +
162        'unregistered registration');
163
164 async_test(function(t) {
165     var scope = 'resources/scope/unregister-then-register-reject-install-worker';
166     var iframe;
167     var registration;
168
169     service_worker_unregister_and_register(t, worker_url, scope)
170       .then(function(r) {
171           registration = r;
172           return wait_for_update(t, registration);
173         })
174       .then(function(worker) {
175           return wait_for_state(t, worker, 'activated');
176         })
177       .then(function() {
178           return with_iframe(scope);
179         })
180       .then(function(frame) {
181           iframe = frame;
182           return registration.unregister();
183         })
184       .then(function() {
185           var promise = navigator.serviceWorker.register(
186               'resources/reject-install-worker.js', { scope: scope });
187           unload_iframe(iframe);
188           return promise;
189         })
190       .then(function(r) {
191           registration = r;
192           return wait_for_update(t, registration);
193         })
194       .then(function(worker) {
195           return wait_for_state(t, worker, 'redundant');
196         })
197       .then(function() {
198           return with_iframe(scope);
199         })
200       .then(function(frame) {
201           assert_equals(frame.contentWindow.navigator.serviceWorker.controller,
202                         null,
203                         'document should not load with a controller');
204           unload_iframe(frame);
205           return registration.unregister();
206         })
207       .then(function() {
208           t.done();
209         })
210       .catch(unreached_rejection(t));
211   }, 'Registering a new script URL that fails to install does not resurrect ' +
212        'an unregistered registration');
213 </script>