summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/anonymous-iframe/serviceworker-partitioning.tentative.https.window.js
blob: 14997a475433e73860d67813d6806d76b0142ce8 (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
// META: script=/common/utils.js

const sw_url = location.pathname.replace(/[^/]*$/, '') +
      "./resources/serviceworker-partitioning-helper.js";

promise_test(async t => {
  // Create 4 iframes (two normal and two credentialless ones) and register
  // a serviceworker with the same scope and url in all of them.
  //
  // Registering the same service worker again with the same url and
  // scope is a no-op. However, credentialless iframes get partitioned
  // service workers, so we should have a total of 2 service workers
  // at the end (one for the normal iframes and one for the credentialless
  // ones).
  let iframes = await Promise.all([
    { name: "normal", credentialless: false},
    { name: "normal_control", credentialless: false},
    { name: "credentialless", credentialless: true},
    { name: "credentialless_control", credentialless: true},
  ].map(async ({name, credentialless}) => {

    let iframe = await new Promise(resolve => {
      let iframe = document.createElement('iframe');
      iframe.onload = () => resolve(iframe);
      iframe.src = '/common/blank.html';
      if (credentialless) iframe.credentialless = true;
      document.body.append(iframe);
    });

    let sw = await new Promise(resolve => {
      iframe.contentWindow.navigator.serviceWorker.register(sw_url)
        .then(r => {
          add_completion_callback(_ => r.unregister());
          resolve(r.active || r.installing || r.waiting);
        });
    });
    return { iframe: iframe, name: name, sw: sw };
  }));

  // Setup a MessageChannel for each pair (iframe, serviceworker).
  // Ping each serviceworker telling him which iframe it belongs to.
  iframes.forEach((iframe, i) => {
    iframe.channel = new MessageChannel();
    iframe.sw.postMessage({ from: iframe.name }, [iframe.channel.port2]);
  });

  let msg_promises = iframes.map(iframe => new Promise(resolve => {
    iframe.channel.port1.onmessage = event => resolve(event.data);
  }));

  // Ping each (iframe, serviceworker) asking for which messages it got.
  iframes.map(iframe => iframe.sw.postMessage({ check: iframe.name }));

  // Collect all replies.
  let msgs = await Promise.all(msg_promises);

  // The "normal" iframe serviceworker belongs to the "normal" and the
  // "normal_control" iframes.
  assert_true(!!msgs[0]["normal"]);
  assert_true(!!msgs[0]["normal_control"]);
  assert_false(!!msgs[0]["credentialless"]);
  assert_false(!!msgs[0]["credentialless_control"]);

  // The "normal_control" iframe shares the same serviceworker as the "normal"
  // iframe.
  assert_true(!!msgs[1]["normal"]);
  assert_true(!!msgs[1]["normal_control"]);
  assert_false(!!msgs[1]["credentialless"]);
  assert_false(!!msgs[1]["credentialless_control"]);

  // The "credentialless" iframe serviceworker belongs to the "credentialless"
  // and the "credentialless_control" iframes.
  assert_false(!!msgs[2]["normal"]);
  assert_false(!!msgs[2]["normal_control"]);
  assert_true(!!msgs[2]["credentialless"]);
  assert_true(!!msgs[2]["credentialless_control"]);

  // The "credentialless_control" iframe shares the same serviceworker as the
  // "credentialless" iframe.
  assert_false(!!msgs[3]["normal"]);
  assert_false(!!msgs[3]["normal_control"]);
  assert_true(!!msgs[3]["credentialless"]);
  assert_true(!!msgs[3]["credentialless_control"]);

}, "credentialless iframes get partitioned service workers.");