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 scope = 'resources/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}, [messageChann
el.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(funct
ion (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 'installed': | |
53 assert_equals(lastServiceWorkerState, 'installing'); | |
54 break; | |
55 case 'activating': | |
56 assert_equals(lastServiceWorkerState, 'installed'); | |
57 break; | |
58 case 'activated': | |
59 assert_equals(lastServiceWorkerState, 'activating'); | |
60 break; | |
61 default: | |
62 // We won't see 'redundant' because onstatechange is | |
63 // overwritten before calling unregister. | |
64 assert_unreached('Unexpected state: ' + sw.state); | |
65 } | |
66 | |
67 lastServiceWorkerState = sw.state; | |
68 if (sw.state === 'activated') | |
69 resolve(); | |
70 }); | |
71 })); | |
72 | |
73 Promise.all([sawMessage, sawActive]).then(t.step_func(function() { | |
74 assert_array_equals(serviceWorkerStates, | |
75 ['installing', 'installed', 'activating', 'activ
ated'], | |
76 'Service worker should pass through all states')
; | |
77 | |
78 assert_equals(currentChangeCount, 0, | |
79 'Should not see current changes since document is out
of scope'); | |
80 | |
81 assert_equals(receivedMessageFromPort, 'Ack for: registering doc'); | |
82 | |
83 var sawRedundant = new Promise(t.step_func(function(resolve) { | |
84 sw.onstatechange = t.step_func(function() { | |
85 assert_equals(sw.state, 'redundant'); | |
86 resolve(); | |
87 }); | |
88 })); | |
89 registration.unregister(); | |
90 sawRedundant.then(t.step_func(function() { | |
91 t.done(); | |
92 })); | |
93 })); | |
94 } | |
95 }); | |
96 </script> | |
OLD | NEW |