File: update-import-scripts.https.html

package info (click to toggle)
firefox-esr 91.13.0esr-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,375,652 kB
  • sloc: cpp: 5,762,054; javascript: 5,481,714; ansic: 3,121,191; python: 851,492; asm: 331,172; xml: 178,949; java: 155,554; sh: 63,704; makefile: 20,127; perl: 12,825; yacc: 4,583; cs: 3,846; objc: 3,026; lex: 1,720; exp: 762; pascal: 635; php: 436; lisp: 260; awk: 231; ruby: 103; sed: 53; sql: 46; csh: 45
file content (127 lines) | stat: -rw-r--r-- 5,338 bytes parent folder | download | duplicates (8)
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
<!DOCTYPE html>
<meta charset="utf-8">
<title>Tests for importScripts: import scripts ignored error</title>
<script src="/common/utils.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<body>
<script>
// This file contains tests to check if imported scripts appropriately updated.

const SCOPE = 'resources/simple.txt';

// Create a service worker (update-worker-from-file.py), which is initially
// |initial_worker| and |updated_worker| later.
async function prepare_ready_update_worker_from_file(
    t, initial_worker, updated_worker) {
  const key = token();
  const worker_url = `resources/update-worker-from-file.py?` +
      `First=${initial_worker}&Second=${updated_worker}&Key=${key}`;
  const expected_url = normalizeURL(worker_url);

  const registration = await service_worker_unregister_and_register(
      t, worker_url, SCOPE);
  await wait_for_state(t, registration.installing, 'activated');
  assert_equals(registration.installing, null,
                'prepare_ready: installing');
  assert_equals(registration.waiting, null,
                'prepare_ready: waiting');
  assert_equals(registration.active.scriptURL, expected_url,
                'prepare_ready: active');
  return [registration, expected_url];
}

// Create a service worker using the script under resources/.
async function prepare_ready_normal_worker(t, filename, additional_params='') {
  const key = token();
  const worker_url = `resources/${filename}?Key=${key}&${additional_params}`;
  const expected_url = normalizeURL(worker_url);

  const registration = await service_worker_unregister_and_register(
      t, worker_url, SCOPE);
  await wait_for_state(t, registration.installing, 'activated');
  assert_equals(registration.installing, null,
                'prepare_ready: installing');
  assert_equals(registration.waiting, null,
                'prepare_ready: waiting');
  assert_equals(registration.active.scriptURL, expected_url,
                'prepare_ready: active');
  return [registration, expected_url];
}

function assert_installing_and_active(registration, expected_url) {
  assert_equals(registration.installing.scriptURL, expected_url,
                'assert_installing_and_active: installing');
  assert_equals(registration.waiting, null,
                'assert_installing_and_active: waiting');
  assert_equals(registration.active.scriptURL, expected_url,
                'assert_installing_and_active: active');
}

function assert_waiting_and_active(registration, expected_url) {
  assert_equals(registration.installing, null,
                'assert_waiting_and_active: installing');
  assert_equals(registration.waiting.scriptURL, expected_url,
                'assert_waiting_and_active: waiting');
  assert_equals(registration.active.scriptURL, expected_url,
                'assert_waiting_and_active: active');
}

function assert_active_only(registration, expected_url) {
  assert_equals(registration.installing, null,
                'assert_active_only: installing');
  assert_equals(registration.waiting, null,
                'assert_active_only: waiting');
  assert_equals(registration.active.scriptURL, expected_url,
                'assert_active_only: active');
}

promise_test(async t => {
  const [registration, expected_url] =
      await prepare_ready_update_worker_from_file(
          t, 'empty.js', 'import-scripts-404.js');
  t.add_cleanup(() => registration.unregister());

  await promise_rejects_js(t, TypeError, registration.update());
  assert_active_only(registration, expected_url);
}, 'update() should fail when a new worker imports an unavailable script.');

promise_test(async t => {
  const [registration, expected_url] =
      await prepare_ready_update_worker_from_file(
          t, 'import-scripts-404-after-update.js', 'empty.js');
  t.add_cleanup(() => registration.unregister());

  await Promise.all([registration.update(), wait_for_update(t, registration)]);
  assert_installing_and_active(registration, expected_url);

  await wait_for_state(t, registration.installing, 'installed');
  assert_waiting_and_active(registration, expected_url);

  await wait_for_state(t, registration.waiting, 'activated');
  assert_active_only(registration, expected_url);
}, 'update() should succeed when the old imported script no longer exist but ' +
   "the new worker doesn't import it.");

promise_test(async t => {
  const [registration, expected_url] = await prepare_ready_normal_worker(
    t, 'import-scripts-404-after-update.js');
  t.add_cleanup(() => registration.unregister());

  await registration.update();
  assert_active_only(registration, expected_url);
}, 'update() should treat 404 on imported scripts as no change.');

promise_test(async t => {
  const [registration, expected_url] = await prepare_ready_normal_worker(
    t, 'import-scripts-404-after-update-plus-update-worker.js',
       `AdditionalKey=${token()}`);
  t.add_cleanup(() => registration.unregister());

  await promise_rejects_js(t, TypeError, registration.update());
  assert_active_only(registration, expected_url);
}, 'update() should find an update in an imported script but update() should ' +
   'result in failure due to missing the other imported script.');
</script>
</body>