summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/ServiceWorkerGlobalScope/resources/registration-attribute-worker.js
blob: c98acbcfb0607434e9065c7ef5624b380c64fb50 (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
importScripts('../../resources/test-helpers.sub.js');
importScripts('../../resources/worker-testharness.js');

var events_seen = [];

assert_equals(
  self.registration.scope,
  normalizeURL('scope/registration-attribute'),
  'On worker script evaluation, registration attribute should be set');
assert_equals(
  self.registration.installing,
  null,
  'On worker script evaluation, installing worker should be null');
assert_equals(
  self.registration.waiting,
  null,
  'On worker script evaluation, waiting worker should be null');
assert_equals(
  self.registration.active,
  null,
  'On worker script evaluation, active worker should be null');

self.registration.addEventListener('updatefound', function() {
    events_seen.push('updatefound');

    assert_equals(
      self.registration.scope,
      normalizeURL('scope/registration-attribute'),
      'On updatefound event, registration attribute should be set');
    assert_equals(
      self.registration.installing.scriptURL,
      normalizeURL('registration-attribute-worker.js'),
      'On updatefound event, installing worker should be set');
    assert_equals(
      self.registration.waiting,
      null,
      'On updatefound event, waiting worker should be null');
    assert_equals(
      self.registration.active,
      null,
      'On updatefound event, active worker should be null');

    assert_equals(
      self.registration.installing.state,
      'installing',
      'On updatefound event, worker should be in the installing state');

    var worker = self.registration.installing;
    self.registration.installing.addEventListener('statechange', function() {
        events_seen.push('statechange(' + worker.state + ')');
      });
  });

self.addEventListener('install', function(e) {
    events_seen.push('install');

    assert_equals(
      self.registration.scope,
      normalizeURL('scope/registration-attribute'),
      'On install event, registration attribute should be set');
    assert_equals(
      self.registration.installing.scriptURL,
      normalizeURL('registration-attribute-worker.js'),
      'On install event, installing worker should be set');
    assert_equals(
      self.registration.waiting,
      null,
      'On install event, waiting worker should be null');
    assert_equals(
      self.registration.active,
      null,
      'On install event, active worker should be null');

    assert_equals(
      self.registration.installing.state,
      'installing',
      'On install event, worker should be in the installing state');
  });

self.addEventListener('activate', function(e) {
    events_seen.push('activate');

    assert_equals(
      self.registration.scope,
      normalizeURL('scope/registration-attribute'),
      'On activate event, registration attribute should be set');
    assert_equals(
      self.registration.installing,
      null,
      'On activate event, installing worker should be null');
    assert_equals(
      self.registration.waiting,
      null,
      'On activate event, waiting worker should be null');
    assert_equals(
      self.registration.active.scriptURL,
      normalizeURL('registration-attribute-worker.js'),
      'On activate event, active worker should be set');

    assert_equals(
      self.registration.active.state,
      'activating',
      'On activate event, worker should be in the activating state');
  });

self.addEventListener('fetch', function(e) {
    events_seen.push('fetch');

    assert_equals(
      self.registration.scope,
      normalizeURL('scope/registration-attribute'),
      'On fetch event, registration attribute should be set');
    assert_equals(
      self.registration.installing,
      null,
      'On fetch event, installing worker should be null');
    assert_equals(
      self.registration.waiting,
      null,
      'On fetch event, waiting worker should be null');
    assert_equals(
      self.registration.active.scriptURL,
      normalizeURL('registration-attribute-worker.js'),
      'On fetch event, active worker should be set');

    assert_equals(
      self.registration.active.state,
      'activated',
      'On fetch event, worker should be in the activated state');

    e.respondWith(new Response(events_seen));
  });