Skip to content

Commit 1f0720b

Browse files
W. James MacLeanchromium-wpt-export-bot
authored andcommitted
Move about:srcdoc WPT tests to external.
There is nothing about these tests that requires them to be internal, so moving them to an external directory. Bug: 40165505 Change-Id: I3fe343b35c147dccdb1af46e8057afaaffa4182e Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5799985 Commit-Queue: Dominic Farolino <[email protected]> Reviewed-by: Dominic Farolino <[email protected]> Cr-Commit-Position: refs/heads/main@{#1344620}
1 parent c0ba65c commit 1f0720b

File tree

6 files changed

+243
-0
lines changed

6 files changed

+243
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// META: script=./resources/testharness.js
2+
// META: script=./resources/testharnessreport.js
3+
4+
function run_test(cross_origin, same_doc, sandbox, name) {
5+
promise_test(async t => {
6+
const child_frame = document.createElement('iframe');
7+
8+
const child_relative_path = './resources/child_with_srcdoc_subframe.window.html';
9+
if (cross_origin) {
10+
const new_origin = new URL('http://{{hosts[][www]}}:{{ports[http][1]}}');
11+
const child_url_same_site = new URL(child_relative_path, location.href);
12+
child_frame.src = new_origin.origin + child_url_same_site.pathname;
13+
} else {
14+
child_frame.src = child_relative_path;
15+
}
16+
const iframe_load = new Promise(resolve => {
17+
child_frame.onload = resolve;
18+
});
19+
document.body.appendChild(child_frame);
20+
await iframe_load;
21+
22+
let load_count = 0;
23+
const test_finished = new Promise(resolve => {
24+
window.onmessage = (e) => {
25+
load_count++;
26+
if (load_count == 1) {
27+
assert_equals(e.data, "about:srcdoc");
28+
// Allow the main frame to try and set the grand child's location.
29+
if (same_doc) {
30+
if (!sandbox) {
31+
// If `sandbox` is set, the child will self-navigate, otherwise
32+
// the main frame initiates the same-document navigation.
33+
frames[0][0].location = "about:srcdoc#the_anchor";
34+
}
35+
} else {
36+
frames[0][0].location = "about:srcdoc";
37+
}
38+
} else if (load_count == 2) {
39+
if (same_doc) {
40+
// The result for same_doc is the same whether cross_origin is set
41+
// or not.
42+
assert_equals(e.data, "about:srcdoc#the_anchor");
43+
} else if (cross_origin) {
44+
const child_url = new URL(child_frame.src);
45+
const expected_data = "SecurityError: " +
46+
"Failed to read a named property 'href' from 'Location': " +
47+
"Blocked a frame with origin \"" + child_url.origin +
48+
"\" from accessing a cross-origin frame."
49+
assert_equals(String(e.data), expected_data);
50+
} else {
51+
assert_equals(e.data, "about:srcdoc");
52+
}
53+
resolve();
54+
}
55+
}
56+
});
57+
let cmd_str = "load grandchild";
58+
if (sandbox) {
59+
cmd_str += " sandbox";
60+
}
61+
child_frame.contentWindow.postMessage(cmd_str, "*");
62+
await test_finished;
63+
64+
t.add_cleanup(() => child_frame.remove())
65+
}, name);
66+
}
67+
68+
onload = () => {
69+
// A cross-origin frame cannot set about:srcdoc but can do same-doc navigations.
70+
run_test(cross_origin = true, same_doc = false, sandbox = false, name =
71+
"cross-origin grandparent sets location to about:srcdoc");
72+
run_test(cross_origin = true, same_doc = true, sandbox = false, name =
73+
"cross-origin grandparent sets location in same-doc navigation");
74+
75+
// A same-origin frame can set about:srcdoc and also do same-doc navigations.
76+
run_test(cross_origin = false, same_doc = false, sandbox = false, name =
77+
"same-origin grandparent sets location to about:srcdoc");
78+
run_test(cross_origin = false, same_doc = true, sandbox = false, name =
79+
"same-origin grandparent sets location in same-doc navigation");
80+
81+
// For the sandboxed srcdoc cases, the srcdoc will be cross-origin to
82+
// everything but itself, but it should be able to navigate itself same-
83+
// document.
84+
run_test(cross_origin = false, same_doc = true, sandbox = true, name =
85+
"same-origin grandparent with sandboxed srcdoc grandchild that self navigates");
86+
run_test(cross_origin = true, same_doc = true, sandbox = true, name =
87+
"cross-origin grandparent with sandboxed srcdoc grandchild that self navigate");
88+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// META: script=./resources/testharness.js
2+
// META: script=./resources/testharnessreport.js
3+
4+
function run_test(cross_origin, sandbox, name) {
5+
promise_test(async test => {
6+
let child_frame = document.createElement('iframe');
7+
8+
let load_count = 0;
9+
const test_finished = new Promise(resolve => {
10+
window.onmessage = (e) => {
11+
load_count++;
12+
if (load_count == 1) {
13+
// Initial load.
14+
assert_equals(e.data, "about:srcdoc");
15+
// Navigate child to a different page.
16+
child_frame.src = "./resources/child2.html";
17+
} else if (load_count == 2) {
18+
// Make sure we navigated away from the frame with the srcdoc, then
19+
// go back.
20+
assert_equals(e.data, child_frame.src);
21+
history.back();
22+
} else if (load_count == 3) {
23+
// Verify the session restore was able to load the srcdoc.
24+
assert_equals(e.data, "about:srcdoc");
25+
resolve();
26+
}
27+
};
28+
});
29+
let cmd_str = "load grandchild";
30+
if (sandbox) {
31+
cmd_str += " sandbox";
32+
}
33+
34+
// It would be nice not to have to hardcode the entire relative path for the
35+
// child in the cross-origin case.
36+
let filename = "child_with_static_srcdoc.html";
37+
if (sandbox) {
38+
let filename = "child_with_static_sandbox_srcdoc.html"
39+
}
40+
41+
const child_relative_path = './resources/' + filename;
42+
if (cross_origin) {
43+
const new_origin = new URL('http://{{hosts[][www]}}:{{ports[http][1]}}');
44+
const child_url_same_site = new URL(child_relative_path, location.href);
45+
child_frame.src = new_origin.origin + child_url_same_site.pathname;
46+
} else {
47+
child_frame.src = child_relative_path;
48+
}
49+
50+
document.body.appendChild(child_frame);
51+
await test_finished;
52+
53+
// Cleanup.
54+
document.body.removeChild(child_frame);
55+
}, name);
56+
}
57+
58+
onload = () => {
59+
// Four tests to make sure the about:srcdoc loads when being restored from
60+
// session history. The srcdoc itself can be either sandboxed or not, and
61+
// the caller of history.back() can be cross-origin or same-origin to the
62+
// oarent of the srcdoc.
63+
run_test(cross_origin = true, sandbox = false,
64+
name = "Grandparent with cross-origin srdoc grandchild session");
65+
run_test(cross_origin = true, sandbox = true,
66+
name = "Grandparent with cross-origin sandboxed srdoc grandchild session");
67+
run_test(cross_origin = false, sandbox = false,
68+
name = "Grandparent with same-origin srdoc grandchild session");
69+
run_test(cross_origin = false, sandbox = true,
70+
name = "Grandparent with same-origin sandboxed srdoc grandchild session");
71+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<script>
5+
onload = () => {
6+
parent.postMessage(location.href, "*");
7+
};
8+
</script>
9+
</head>
10+
<body>child 2</body>
11+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
window.onmessage = (e) => {
2+
assert_true(e.data.includes("load grandchild"));
3+
4+
let srcdoc_content = `
5+
<p>Grandchild</p><a id='the_anchor'>The Anchor</a>
6+
<script>
7+
document.body.onhashchange = () => {
8+
window.top.postMessage(location.href, '*');
9+
};
10+
</scr` + "ipt>";
11+
const sandbox = e.data.includes("sandbox");
12+
if (sandbox) {
13+
srcdoc_content += `
14+
<script>
15+
document.body.onload = () => {
16+
window.top.postMessage(location.href, '*');
17+
window.location = 'about:srcdoc#the_anchor';
18+
}
19+
</scr` + "ipt>";
20+
}
21+
22+
let grandchild_frame = document.createElement('iframe');
23+
grandchild_frame.onload = () => {
24+
// Each time the grandchild frame loads, send its location to the
25+
// parent. If that fails, send the error message.
26+
// For the sandbox case, the child directly sends the href value before
27+
// self-navigating.
28+
if (!sandbox) {
29+
let result;
30+
try {
31+
result = grandchild_frame.contentWindow.location.href;
32+
} catch (error) {
33+
result = error;
34+
}
35+
e.source.postMessage(result, "*");
36+
}
37+
};
38+
if (sandbox) {
39+
grandchild_frame.sandbox = "allow-scripts";
40+
}
41+
grandchild_frame.srcdoc = srcdoc_content;
42+
document.body.appendChild(grandchild_frame);
43+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<script>
5+
window.onmessage = (e) => {
6+
parent.postMessage(e.data, "*");
7+
};
8+
</script>
9+
</head>
10+
<body>
11+
<iframe sandbox="allow-scripts" srcdoc=
12+
"<p>Grandchild</p><script>onload = () => { parent.postMessage(location.href,'*'); }</script>">
13+
</iframe>
14+
</body>
15+
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<script>
5+
window.onmessage = (e) => {
6+
parent.postMessage(e.data, "*");
7+
};
8+
</script>
9+
</head>
10+
<body>
11+
<iframe srcdoc=
12+
"<p>Grandchild</p><script>onload = () => { parent.postMessage(location.href,'*'); }</script>">
13+
</iframe>
14+
</body>
15+
</html>

0 commit comments

Comments
 (0)