summaryrefslogtreecommitdiffstats
path: root/test/wpt/tests/service-workers/service-worker/update-bytecheck.https.html
blob: 3e5a28bb67cb75624a5dd98bd15e137e55c87b0f (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
<!doctype html>
<meta charset=utf-8>
<title></title>
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="resources/testharness-helpers.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script>
// Tests of updating a service worker. This file contains non-cors cases only.

/*
 * @param string main
 *   Decide the content of the main script, where 'default' is for constant
 *   content while 'time' is for time-variant content.
 * @param string imported
 *   Decide the content of the imported script, where 'default' is for constant
 *   content while 'time' is for time-variant content.
 */
const settings = [{main: 'default', imported: 'default'},
                  {main: 'default', imported: 'time'   },
                  {main: 'time',    imported: 'default'},
                  {main: 'time',    imported: 'time'   }];

const host_info = get_host_info();
settings.forEach(({main, imported}) => {
  promise_test(async (t) => {
    // Empty path results in the same origin imported scripts.
    const path = '';
    const script = 'resources/bytecheck-worker.py' +
                   '?main=' + main +
                   '&imported=' + imported +
                   '&path=' + path +
                   '&type=classic';
    const scope = 'resources/blank.html';

    // Register a service worker.
    const swr = await service_worker_unregister_and_register(t, script, scope);
    t.add_cleanup(() => swr.unregister());
    const sw = await wait_for_update(t, swr);
    await wait_for_state(t, sw, 'activated');
    assert_array_equals([swr.active, swr.waiting, swr.installing],
                        [sw, null, null]);

    // Update the service worker registration.
    await swr.update();

    // If there should be a new service worker.
    if (main === 'time' || imported === 'time') {
      return wait_for_update(t, swr);
    }
    // Otherwise, make sure there is no newly created service worker.
    assert_array_equals([swr.active, swr.waiting, swr.installing],
                        [sw, null, null]);
  }, `Test(main: ${main}, imported: ${imported})`);
});

settings.forEach(({main, imported}) => {
  promise_test(async (t) => {
    // Empty path results in the same origin imported scripts.
    const path = './';
    const script = 'resources/bytecheck-worker.py' +
                   '?main=' + main +
                   '&imported=' + imported +
                   '&path=' + path +
                   '&type=module';
    const scope = 'resources/blank.html';

    // Register a module service worker.
    const swr = await service_worker_unregister_and_register(t, script, scope,
                                                            {type: 'module'});

    t.add_cleanup(() => swr.unregister());
    const sw = await wait_for_update(t, swr);
    await wait_for_state(t, sw, 'activated');
    assert_array_equals([swr.active, swr.waiting, swr.installing],
                        [sw, null, null]);

    // Update the service worker registration.
    await swr.update();

    // If there should be a new service worker.
    if (main === 'time' || imported === 'time') {
      return wait_for_update(t, swr);
    }
    // Otherwise, make sure there is no newly created service worker.
    assert_array_equals([swr.active, swr.waiting, swr.installing],
                        [sw, null, null]);
  }, `Test module script(main: ${main}, imported: ${imported})`);
});
</script>