OLD | NEW |
---|---|
(Empty) | |
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 serviceWorkerStates = []; | |
11 var lastServiceWorkerState = ''; | |
12 var receivedMessageFromPort = ''; | |
13 var currentChangeCount = 0; | |
14 | |
15 assert_true(navigator.serviceWorker instanceof ServiceWorkerContainer); | |
16 assert_equals(typeof navigator.serviceWorker.register, 'function'); | |
17 assert_equals(typeof navigator.serviceWorker.unregister, 'function'); | |
18 | |
19 navigator.serviceWorker.oncurrentchange = function() { | |
20 ++currentChangeCount; | |
21 }; | |
22 | |
23 // This always tests fresh registration, so unregister it first. | |
24 navigator.serviceWorker.unregister('/in-scope/*').then( | |
25 t.step_func(doRegister), | |
26 unreached_func(t, 'unregister should not reject') | |
27 ); | |
28 | |
29 function doRegister() { | |
30 navigator.serviceWorker.register( | |
31 'resources/end-to-end-worker.js', | |
32 {scope: '/in-scope/*'} | |
33 ).then( | |
34 t.step_func(onRegister) | |
35 ).catch( | |
36 unreached_func(t, 'Registration failed') | |
37 ); | |
38 } | |
39 | |
40 function sendMessagePort(worker, from) { | |
41 var messageChannel = new MessageChannel(); | |
42 worker.postMessage({from:from, port:messageChannel.port2}, [messageChann el.port2]); | |
43 return messageChannel.port1; | |
44 } | |
45 | |
46 | |
47 var sawMessage, sawActive; | |
48 var messagePromise = new Promise(function(resolve) { sawMessage = resolve; } ); | |
49 var activePromise = new Promise(function(resolve) { sawActive = resolve; }); | |
50 | |
51 function onRegister(sw) { | |
52 serviceWorkerStates.push(sw.state); | |
53 lastServiceWorkerState = sw.state; | |
54 | |
55 var sawMessage = new Promise(t.step_func(function(resolve) { | |
56 sendMessagePort(sw, 'registering doc').onmessage = t.step_func(funct ion (e) { | |
57 receivedMessageFromPort = e.data; | |
58 resolve(); | |
59 }); | |
60 })); | |
61 | |
62 var sawActive = new Promise(t.step_func(function(resolve) { | |
63 sw.onstatechange = t.step_func(function() { | |
64 serviceWorkerStates.push(sw.state); | |
65 | |
66 switch (sw.state) { | |
67 case 'installing': | |
68 // FIXME: Not currently seen. | |
kinuko
2014/04/30 04:11:43
Is this issue fixed by Matt's change?
falken
2014/04/30 04:21:32
Yes, I expect this is fixed in ToT (since Chromium
| |
69 assert_equals(lastServiceWorkerState, 'parsed'); | |
70 break; | |
71 case 'installed': | |
72 assert_equals(lastServiceWorkerState, 'installing'); | |
73 break; | |
74 case 'activating': | |
75 assert_equals(lastServiceWorkerState, 'installed'); | |
76 break; | |
77 case 'active': | |
78 assert_equals(lastServiceWorkerState, 'activating'); | |
79 break; | |
80 default: | |
81 unreached_func(t, 'Unexpected state: ' + sw.state); | |
82 } | |
83 | |
84 lastServiceWorkerState = sw.state; | |
85 if (sw.state === 'active') | |
86 resolve(); | |
87 }); | |
88 })); | |
89 | |
90 Promise.all([sawMessage, sawActive]).then(t.step_func(function() { | |
91 assert_array_equals(serviceWorkerStates, | |
92 ['installing', 'installed', 'activating', 'activ e'], | |
93 'Service worker should pass through four states' ); | |
94 | |
95 assert_equals(currentChangeCount, 0, | |
96 'Should not see current changes since document is out of scope'); | |
97 | |
98 assert_equals(receivedMessageFromPort, 'Ack for: registering doc'); | |
99 | |
100 t.done(); | |
101 })); | |
102 } | |
103 }); | |
104 </script> | |
OLD | NEW |