summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/extendable-event-waituntil.https.html
blob: 33b4eac5c185e911023094f210ce4db9b6dddadb (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<!DOCTYPE html>
<title>ExtendableEvent: waitUntil</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
function runTest(test, scope, onRegister) {
  var script = 'resources/extendable-event-waituntil.js?' + scope;
  return service_worker_unregister_and_register(test, script, scope)
    .then(function(registration) {
        test.add_cleanup(function() {
            return service_worker_unregister(test, scope);
          });

        return onRegister(registration.installing);
      });
}

// Sends a SYN to the worker and asynchronously listens for an ACK; sets
// |obj.synced| to true once ack'd.
function syncWorker(worker, obj) {
  var channel = new MessageChannel();
  worker.postMessage({port: channel.port2}, [channel.port2]);
  return new Promise(function(resolve) {
      channel.port1.onmessage = resolve;
    }).then(function(e) {
      var message = e.data;
      assert_equals(message, 'SYNC',
                    'Should receive sync message from worker.');
      obj.synced = true;
      channel.port1.postMessage('ACK');
    });
}

promise_test(function(t) {
    // Passing scope as the test switch for worker script.
    var scope = 'resources/install-fulfilled';
    var onRegister = function(worker) {
        var obj = {};

        return Promise.all([
            syncWorker(worker, obj),
            wait_for_state(t, worker, 'installed')
          ]).then(function() {
              assert_true(
                obj.synced,
                'state should be "installed" after the waitUntil promise ' +
                    'for "oninstall" is fulfilled.');
              service_worker_unregister_and_done(t, scope);
            });
      };
    return runTest(t, scope, onRegister);
  }, 'Test install event waitUntil fulfilled');

promise_test(function(t) {
    var scope = 'resources/install-multiple-fulfilled';
    var onRegister = function(worker) {
        var obj1 = {};
        var obj2 = {};

        return Promise.all([
            syncWorker(worker, obj1),
            syncWorker(worker, obj2),
            wait_for_state(t, worker, 'installed')
          ]).then(function() {
              assert_true(
                obj1.synced && obj2.synced,
                'state should be "installed" after all waitUntil promises ' +
                    'for "oninstall" are fulfilled.');
            });
      };
    return runTest(t, scope, onRegister);
  }, 'Test ExtendableEvent multiple waitUntil fulfilled.');

promise_test(function(t) {
    var scope = 'resources/install-reject-precedence';
    var onRegister = function(worker) {
        var obj1 = {};
        var obj2 = {};

        return Promise.all([
            syncWorker(worker, obj1)
              .then(function() {
                  syncWorker(worker, obj2);
                }),
            wait_for_state(t, worker, 'redundant')
          ]).then(function() {
              assert_true(
                obj1.synced,
                'The "redundant" state was entered after the first "extend ' +
                  'lifetime promise" resolved.'
              );
              assert_true(
                obj2.synced,
                'The "redundant" state was entered after the third "extend ' +
                  'lifetime promise" resolved.'
              );
            });
      };
    return runTest(t, scope, onRegister);
  }, 'Test ExtendableEvent waitUntil reject precedence.');

promise_test(function(t) {
    var scope = 'resources/activate-fulfilled';
    var onRegister = function(worker) {
        var obj = {};
        return wait_for_state(t, worker, 'activating')
          .then(function() {
              return Promise.all([
                syncWorker(worker, obj),
                wait_for_state(t, worker, 'activated')
              ]);
            })
          .then(function() {
              assert_true(
                obj.synced,
                'state should be "activated" after the waitUntil promise ' +
                    'for "onactivate" is fulfilled.');
            });
      };
    return runTest(t, scope, onRegister);
  }, 'Test activate event waitUntil fulfilled');

promise_test(function(t) {
    var scope = 'resources/install-rejected';
    var onRegister = function(worker) {
        return wait_for_state(t, worker, 'redundant');
      };
    return runTest(t, scope, onRegister);
  }, 'Test install event waitUntil rejected');

promise_test(function(t) {
    var scope = 'resources/activate-rejected';
    var onRegister = function(worker) {
        return wait_for_state(t, worker, 'activated');
      };
    return runTest(t, scope, onRegister);
  }, 'Test activate event waitUntil rejected.');

</script>