Skip to content

Commit 01d7d02

Browse files
committed
Added tentative tests for location.ancestorOrigins
This follows the current revamping of the spec found at whatwg/html#11560 and will change with it, potentially.
1 parent 15a96f5 commit 01d7d02

File tree

3 files changed

+210
-0
lines changed

3 files changed

+210
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<!DOCTYPE html>
2+
<title>
3+
Location#ancestorOrigins test
4+
</title>
5+
<script src="/resources/testharness.js"></script>
6+
<script src="/resources/testharnessreport.js"></script>
7+
<script src="/resources/testdriver.js"></script>
8+
<script src="/resources/testdriver-vendor.js"></script>
9+
<script src="./resources/create-iframe.js"></script>
10+
11+
<body>
12+
<script>
13+
function waitFor(action, frameName) {
14+
return new Promise((resolve) => {
15+
window.addEventListener("message", function listener(e) {
16+
if (e.data.event === action && e.data.name === frameName) {
17+
window.removeEventListener("message", listener);
18+
resolve(event.data);
19+
}
20+
});
21+
});
22+
}
23+
24+
const frameCreatorPath = "html/browsers/history/the-location-interface/resources/recursive-iframe.html"
25+
26+
// first src shares origin with self.origin here
27+
let iframeSources = [
28+
{
29+
src: `http://{{hosts[][]}}:{{ports[http][0]}}/${frameCreatorPath}?a`,
30+
},
31+
{
32+
src: `http://{{hosts[alt][]}}:{{ports[http][0]}}/${frameCreatorPath}?b`,
33+
},
34+
{
35+
src: `http://{{hosts[][www]}}:{{ports[http][0]}}/${frameCreatorPath}?c`,
36+
},
37+
];
38+
39+
// origins A, B, C
40+
iframeSources.forEach(f => {
41+
const url = new URL(f.src, location.origin);
42+
f.origin = url.origin;
43+
})
44+
45+
const A = iframeSources[0]
46+
const B = iframeSources[1];
47+
const C = iframeSources[2];
48+
49+
const policy = {
50+
Default: "",
51+
NoRef: "no-referrer",
52+
Same: "same-origin"
53+
}
54+
55+
const config = (name, f, referrerPolicy, sandbox = false) => ({ name: name, src: f.src, referrerPolicy: referrerPolicy, sandbox: sandbox })
56+
57+
// Remember the implicit top-level origin A
58+
const testCases = [
59+
{ // test no-referrer on iframe
60+
frames: [
61+
config("B1", B, policy.NoRef)
62+
],
63+
expected: [
64+
["null"]
65+
]
66+
},
67+
{ // test all default referrer policy
68+
frames: [
69+
config("B1", B, policy.Default),
70+
config("A2", A, policy.Default)
71+
],
72+
expected: [
73+
[A.origin],
74+
[B.origin, A.origin]
75+
]
76+
},
77+
{ // test toggle of masked
78+
frames: [
79+
config("B1", B, policy.Default),
80+
config("A2", A, policy.NoRef)
81+
],
82+
expected: [
83+
[A.origin],
84+
["null", A.origin]
85+
]
86+
},
87+
{ // another test of toggle of masked
88+
frames: [
89+
config("B1", B, policy.Default),
90+
config("B2", B, policy.Default),
91+
config("A2", A, policy.NoRef)
92+
],
93+
expected: [
94+
[A.origin],
95+
[B.origin, A.origin],
96+
["null", "null", A.origin]
97+
]
98+
},
99+
{ // test sandboxed/opaque origin frames
100+
frames: [
101+
config("C1", C, policy.Default, /* sandbox */ true),
102+
config("B2", B, policy.Default),
103+
],
104+
expected: [
105+
[A.origin],
106+
["null", A.origin],
107+
]
108+
},
109+
{ // 5 - Test same-origin referrer policy
110+
frames: [
111+
config("B1", B, policy.Default),
112+
config("A2", A, policy.Default),
113+
config("B2", B, policy.Same)
114+
],
115+
expected: [
116+
[A.origin],
117+
[B.origin, A.origin],
118+
["null", B.origin, A.origin]
119+
]
120+
}
121+
]
122+
123+
testCases.forEach((cfg, index) => {
124+
promise_test(async (t) => {
125+
const iframeDetails = cfg.frames[0];
126+
127+
const topFrameAncestorOrigins = waitFor("sentOrigins", iframeDetails.name);
128+
const iframe = document.createElement("iframe");
129+
await configAndNavigateIFrame(iframe, iframeDetails);
130+
const res = await topFrameAncestorOrigins;
131+
132+
t.add_cleanup(() => {
133+
iframe.remove();
134+
});
135+
136+
let results = [res];
137+
138+
for(let i = 1; i < cfg.frames.length; ++i) {
139+
// name of frame, that shall create a new frame
140+
const parentName = cfg.frames[i - 1].name;
141+
iframe.contentWindow.postMessage({ action: "addIframe", request: cfg.frames[i], name: parentName }, "*");
142+
const res = await waitFor("sentOrigins", cfg.frames[i].name);
143+
results.push(res);
144+
}
145+
146+
let i = 0;
147+
for(const { event, name, origins } of results) {
148+
assert_equals(origins.length, cfg.expected[i].length, `[test configuration ${index}, frame ${name}]: Unexpected length of ancestorOrigins list`);
149+
150+
for(let j = 0; j < origins.length; ++j) {
151+
assert_equals(origins[j], cfg.expected[i][j], `[test configuration ${index}, frame ${name}]: Unexpected origin`)
152+
}
153+
i+=1;
154+
}
155+
}, `Location.ancestorOrigins configuration ${index}`);
156+
157+
})
158+
</script>
159+
</body>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
async function configAndNavigateIFrame(iframe, { src, name, referrerPolicy, sandbox}) {
2+
iframe.name = name;
3+
iframe.style.width = "100%";
4+
iframe.style.height = "100%";
5+
iframe.referrerPolicy = referrerPolicy
6+
if (sandbox) {
7+
// postMessage needs to work
8+
iframe.setAttribute("sandbox", "allow-scripts");
9+
}
10+
document.body.appendChild(iframe);
11+
await new Promise((resolve) => {
12+
iframe.addEventListener("load", resolve, { once: true });
13+
iframe.src = src;
14+
});
15+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8" />
3+
<title>Recursive IFrame Fullscreen API success reporter</title>
4+
<body>
5+
<script src="/resources/testdriver.js"></script>
6+
<script src="/resources/testdriver-vendor.js"></script>
7+
<script src="./create-iframe.js"></script>
8+
<script>
9+
let iframe = null;
10+
11+
window.addEventListener("message", async (e) => {
12+
// Massage is not for us, try to pass it on...
13+
if (e.data.name !== window.name) {
14+
iframe?.contentWindow.postMessage(e.data, "*");
15+
return;
16+
}
17+
switch (e.data.action) {
18+
case "addIframe":
19+
iframe = document.createElement("iframe");
20+
await configAndNavigateIFrame(iframe, e.data.request);
21+
break;
22+
default:
23+
window.top.postMessage(e.data, "*");
24+
}
25+
});
26+
27+
let origs = location.ancestorOrigins;
28+
let origins = [];
29+
for(let i = 0; i < origs.length; ++i) {
30+
origins.push(origs.item(i));
31+
}
32+
33+
window.top.postMessage({ event: "sentOrigins", name: window.name, origins: origins }, "*");
34+
</script>
35+
</body>
36+

0 commit comments

Comments
 (0)