summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/multiple-register.https.html
blob: 752e132fc13accb94f58e3e824c64fe72c7d4222 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<body>
<script>
var worker_url = 'resources/empty-worker.js';

async_test(function(t) {
  var scope = 'resources/scope/subsequent-register-from-same-window';
  var registration;

  service_worker_unregister_and_register(t, worker_url, scope)
    .then(function(r) {
        registration = r;
        return wait_for_state(t, r.installing, 'activated');
      })
    .then(function() {
        return navigator.serviceWorker.register(worker_url, { scope: scope });
      })
    .then(function(new_registration) {
        assert_equals(new_registration, registration,
                      'register should resolve to the same registration');
        assert_equals(new_registration.active, registration.active,
                      'register should resolve to the same worker');
        assert_equals(new_registration.active.state, 'activated',
                      'the worker should be in state "activated"');
        return registration.unregister();
      })
    .then(function() { t.done(); })
    .catch(unreached_rejection(t));
}, 'Subsequent registrations resolve to the same registration object');

async_test(function(t) {
  var scope = 'resources/scope/subsequent-register-from-different-iframe';
  var frame;
  var registration;

  service_worker_unregister_and_register(t, worker_url, scope)
    .then(function(r) {
        registration = r;
        return wait_for_state(t, r.installing, 'activated');
      })
    .then(function() { return with_iframe('resources/404.py'); })
    .then(function(f) {
        frame = f;
        return frame.contentWindow.navigator.serviceWorker.register(
            'empty-worker.js',
            { scope: 'scope/subsequent-register-from-different-iframe' });
      })
    .then(function(new_registration) {
        assert_not_equals(
          registration, new_registration,
          'register should resolve to a different registration');
        assert_equals(
          registration.scope, new_registration.scope,
          'registrations should have the same scope');

        assert_equals(
          registration.installing, null,
          'installing worker should be null');
        assert_equals(
          new_registration.installing, null,
          'installing worker should be null');
        assert_equals(
          registration.waiting, null,
          'waiting worker should be null')
        assert_equals(
          new_registration.waiting, null,
          'waiting worker should be null')

        assert_not_equals(
          registration.active, new_registration.active,
          'registration should have a different active worker');
        assert_equals(
          registration.active.scriptURL,
          new_registration.active.scriptURL,
          'active workers should have the same script URL');
        assert_equals(
          registration.active.state,
          new_registration.active.state,
          'active workers should be in the same state');

        frame.remove();
        return registration.unregister();
      })
    .then(function() { t.done(); })
    .catch(unreached_rejection(t));
}, 'Subsequent registrations from a different iframe resolve to the ' +
       'different registration object but they refer to the same ' +
       'registration and workers');

async_test(function(t) {
  var scope = 'resources/scope/concurrent-register';

  service_worker_unregister(t, scope)
    .then(function() {
        var promises = [];
        for (var i = 0; i < 10; ++i) {
          promises.push(navigator.serviceWorker.register(worker_url,
                                                         { scope: scope }));
        }
        return Promise.all(promises);
      })
    .then(function(registrations) {
        registrations.forEach(function(registration) {
            assert_equals(registration, registrations[0],
                          'register should resolve to the same registration');
          });
        return registrations[0].unregister();
      })
    .then(function() { t.done(); })
    .catch(unreached_rejection(t));
}, 'Concurrent registrations resolve to the same registration object');

</script>
</body>