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>
7 var t
= async_test('Registration: end-to-end');
10 var scope
= 'resources/in-scope/';
11 var serviceWorkerStates
= [];
12 var lastServiceWorkerState
= '';
13 var receivedMessageFromPort
= '';
14 var currentChangeCount
= 0;
16 assert_true(navigator
.serviceWorker
instanceof ServiceWorkerContainer
);
17 assert_equals(typeof navigator
.serviceWorker
.register
, 'function');
18 assert_equals(typeof navigator
.serviceWorker
.getRegistration
, 'function');
20 navigator
.serviceWorker
.oncurrentchange = function() {
24 service_worker_unregister_and_register(
25 t
, 'resources/end-to-end-worker.js', scope
)
27 .catch(unreached_rejection(t
));
29 function sendMessagePort(worker
, from) {
30 var messageChannel
= new MessageChannel();
31 worker
.postMessage({from:from, port
:messageChannel
.port2
}, [messageChannel
.port2
]);
32 return messageChannel
.port1
;
35 function onRegister(registration
) {
36 var sw
= registration
.installing
;
37 serviceWorkerStates
.push(sw
.state
);
38 lastServiceWorkerState
= sw
.state
;
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
;
47 var sawActive
= new Promise(t
.step_func(function(resolve
) {
48 sw
.onstatechange
= t
.step_func(function() {
49 serviceWorkerStates
.push(sw
.state
);
53 assert_equals(lastServiceWorkerState
, 'installing');
56 assert_equals(lastServiceWorkerState
, 'installed');
59 assert_equals(lastServiceWorkerState
, 'activating');
62 // We won't see 'redundant' because onstatechange is
63 // overwritten before calling unregister.
64 assert_unreached('Unexpected state: ' + sw
.state
);
67 lastServiceWorkerState
= sw
.state
;
68 if (sw
.state
=== 'activated')
73 Promise
.all([sawMessage
, sawActive
]).then(t
.step_func(function() {
74 assert_array_equals(serviceWorkerStates
,
75 ['installing', 'installed', 'activating', 'activated'],
76 'Service worker should pass through all states');
78 assert_equals(currentChangeCount
, 0,
79 'Should not see current changes since document is out of scope');
81 assert_equals(receivedMessageFromPort
, 'Ack for: registering doc');
83 var sawRedundant
= new Promise(t
.step_func(function(resolve
) {
84 sw
.onstatechange
= t
.step_func(function() {
85 assert_equals(sw
.state
, 'redundant');
89 registration
.unregister();
90 sawRedundant
.then(t
.step_func(function() {