Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / serviceworker / registration-end-to-end.html
1 <!DOCTYPE html>
2 <title>Service Worker: registration end-to-end</title>
3 <script src="../resources/testharness.js"></script>
4 <script src="../resources/testharnessreport.js"></script>
5 <script src="resources/test-helpers.js"></script>
6 <script>
7 var t = async_test('Registration: end-to-end');
8 t.step(function() {
9
10     var scope = '/in-scope/';
11     var serviceWorkerStates = [];
12     var lastServiceWorkerState = '';
13     var receivedMessageFromPort = '';
14     var currentChangeCount = 0;
15
16     assert_true(navigator.serviceWorker instanceof ServiceWorkerContainer);
17     assert_equals(typeof navigator.serviceWorker.register, 'function');
18     assert_equals(typeof navigator.serviceWorker.getRegistration, 'function');
19
20     navigator.serviceWorker.oncurrentchange = function() {
21         ++currentChangeCount;
22     };
23
24     service_worker_unregister_and_register(
25         t, 'resources/end-to-end-worker.js', scope)
26       .then(onRegister)
27       .catch(unreached_rejection(t));
28
29     function sendMessagePort(worker, from) {
30         var messageChannel = new MessageChannel();
31         worker.postMessage({from:from, port:messageChannel.port2}, [messageChannel.port2]);
32         return messageChannel.port1;
33     }
34
35     function onRegister(registration) {
36         var sw = registration.installing;
37         serviceWorkerStates.push(sw.state);
38         lastServiceWorkerState = sw.state;
39
40         var sawMessage = new Promise(t.step_func(function(resolve) {
41             sendMessagePort(sw, 'registering doc').onmessage = t.step_func(function (e) {
42                 receivedMessageFromPort = e.data;
43                 resolve();
44             });
45         }));
46
47         var sawActive = new Promise(t.step_func(function(resolve) {
48             sw.onstatechange = t.step_func(function() {
49                 serviceWorkerStates.push(sw.state);
50
51                 switch (sw.state) {
52                 case 'installing':
53                     assert_equals(lastServiceWorkerState, 'parsed');
54                     break;
55                 case 'installed':
56                     assert_equals(lastServiceWorkerState, 'installing');
57                     break;
58                 case 'activating':
59                     assert_equals(lastServiceWorkerState, 'installed');
60                     break;
61                 case 'activated':
62                     assert_equals(lastServiceWorkerState, 'activating');
63                     break;
64                 default:
65                     // We won't see 'redundant' because onstatechange is
66                     // overwritten before calling unregister.
67                     assert_unreached('Unexpected state: ' + sw.state);
68                 }
69
70                 lastServiceWorkerState = sw.state;
71                 if (sw.state === 'activated')
72                     resolve();
73             });
74         }));
75
76         Promise.all([sawMessage, sawActive]).then(t.step_func(function() {
77             assert_array_equals(serviceWorkerStates,
78                                 ['parsed', 'installing', 'installed', 'activating', 'activated'],
79                                 'Service worker should pass through all states');
80
81             assert_equals(currentChangeCount, 0,
82                           'Should not see current changes since document is out of scope');
83
84             assert_equals(receivedMessageFromPort, 'Ack for: registering doc');
85
86             var sawRedundant = new Promise(t.step_func(function(resolve) {
87                 sw.onstatechange = t.step_func(function() {
88                     assert_equals(sw.state, 'redundant');
89                     resolve();
90                 });
91             }));
92             registration.unregister();
93             sawRedundant.then(t.step_func(function() {
94                 t.done();
95             }));
96         }));
97     }
98 });
99 </script>