summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/cross-origin-opener-policy/resource-popup.https.html
blob: 481cceb72f0d0ab47f79dc0cdf84ad442a420509 (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
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>Cross-Origin-Opener-Policy forces browsing context switch in various popup document types</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>

<p>These tests create a "parent" popup window which is an HTML document with a
specific Cross-Origin-Opener-Policy. The parent creates a "child" popup window
which is a non-HTML document with a Cross-Origin-Opener-Policy of its own. The
parent waits for the child's location to change from "<code>about:blank</code>"
and then inspects its <code>name</code> and <code>closed</code> properties
which it reports back to the test context.</p>

<p>The HTTP `Refresh` header is used to ensure the child eventually closes
itself (since proper observance of COOP will prevent the parent from closing
the child in some cases).</p>

<script>
'use strict';

const coop_resource_test = ({parentCoop, resourceCoop, resource, resourceName, validate}) => {
  async_test((t) => {
    const bc = new BroadcastChannel(token());
    bc.onmessage = t.step_func_done(({data}) => validate(data));
    const childLocation = resource +
      `?pipe=header(Refresh,2;url=/html/cross-origin-opener-policy/resources/resource-cleanup.html?channel=${bc.name})` +
      (resourceCoop ? `|header(Cross-Origin-Opener-Policy,${resourceCoop})` : '');
    const parentLocation = 'resources/resource-popup.html' +
      `?channel_name=${bc.name}` +
      `&resource=${encodeURIComponent(childLocation)}` +
      `&resource_name=${resourceName}` +
      (parentCoop ? `&pipe=header(Cross-Origin-Opener-Policy,${parentCoop})` : '');

    open(parentLocation);

    t.add_cleanup(() => {
      // Close the "parent" popup and the "child" popup if it has already
      // redirected to the HTML document.
      bc.postMessage(null);
      // Prepare to close the "child" popup in the case that it has not yet
      // redirected to the the HTML document.
      bc.onmessage = () => bc.postMessage(null);
    });
  }, `${resource} - parent COOP: "${parentCoop}"; child COOP: "${resourceCoop}"`);
};

const resources = [
  '/common/dummy.xml',
  '/images/red.png',
  '/common/text-plain.txt',
  '/media/2x2-green.mp4',
];

for (const resource of resources) {
  coop_resource_test({
    parentCoop: '',
    resourceCoop: 'same-origin',
    resource,
    resourceName: 'foobar',
    validate(data) {
      assert_equals(data.name, null);
      assert_equals(data.closed, true);
    }
  });

  coop_resource_test({
    parentCoop: 'same-origin',
    resourceCoop: '',
    resource,
    resourceName: 'foobar',
    validate(data) {
      assert_equals(data.name, null);
      assert_equals(data.closed, true);
    }
  });

  coop_resource_test({
    parentCoop: 'same-origin',
    resourceCoop: 'same-origin',
    resource,
    resourceName: 'foobar',
    validate(data) {
      assert_equals(data.name, 'foobar');
      assert_equals(data.closed, false);
    }
  });
}
</script>
</html>