summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/registration-end-to-end.https.html
blob: 1af4582d387412f47d82a94b2fe7050a5994644a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<!DOCTYPE html>
<title>Service Worker: registration end-to-end</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
var t = async_test('Registration: end-to-end');
t.step(function() {

    var scope = 'resources/in-scope/';
    var serviceWorkerStates = [];
    var lastServiceWorkerState = '';
    var receivedMessageFromPort = '';

    assert_true(navigator.serviceWorker instanceof ServiceWorkerContainer);
    assert_equals(typeof navigator.serviceWorker.register, 'function');
    assert_equals(typeof navigator.serviceWorker.getRegistration, 'function');

    service_worker_unregister_and_register(
        t, 'resources/end-to-end-worker.js', scope)
      .then(onRegister)
      .catch(unreached_rejection(t));

    function sendMessagePort(worker, from) {
        var messageChannel = new MessageChannel();
        worker.postMessage({from:from, port:messageChannel.port2}, [messageChannel.port2]);
        return messageChannel.port1;
    }

    function onRegister(registration) {
        var sw = registration.installing;
        serviceWorkerStates.push(sw.state);
        lastServiceWorkerState = sw.state;

        var sawMessage = new Promise(t.step_func(function(resolve) {
            sendMessagePort(sw, 'registering doc').onmessage = t.step_func(function (e) {
                receivedMessageFromPort = e.data;
                resolve();
            });
        }));

        var sawActive = new Promise(t.step_func(function(resolve) {
            sw.onstatechange = t.step_func(function() {
                serviceWorkerStates.push(sw.state);

                switch (sw.state) {
                case 'installed':
                    assert_equals(lastServiceWorkerState, 'installing');
                    break;
                case 'activating':
                    assert_equals(lastServiceWorkerState, 'installed');
                    break;
                case 'activated':
                    assert_equals(lastServiceWorkerState, 'activating');
                    break;
                default:
                    // We won't see 'redundant' because onstatechange is
                    // overwritten before calling unregister.
                    assert_unreached('Unexpected state: ' + sw.state);
                }

                lastServiceWorkerState = sw.state;
                if (sw.state === 'activated')
                    resolve();
            });
        }));

        Promise.all([sawMessage, sawActive]).then(t.step_func(function() {
            assert_array_equals(serviceWorkerStates,
                                ['installing', 'installed', 'activating', 'activated'],
                                'Service worker should pass through all states');

            assert_equals(receivedMessageFromPort, 'Ack for: registering doc');

            var sawRedundant = new Promise(t.step_func(function(resolve) {
                sw.onstatechange = t.step_func(function() {
                    assert_equals(sw.state, 'redundant');
                    resolve();
                });
            }));
            registration.unregister();
            sawRedundant.then(t.step_func(function() {
                t.done();
            }));
        }));
    }
});
</script>