Skip to content

Commit

Permalink
HTML: Add tentative tests for preload scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
zcorpan committed Jul 8, 2020
1 parent e4a45e8 commit a69d3b1
Show file tree
Hide file tree
Showing 16 changed files with 346 additions and 0 deletions.
21 changes: 21 additions & 0 deletions html/syntax/preload-scanner.tentative/document-write/img-src.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<meta charset=utf-8>
<!-- DO NOT EDIT! This file has been generated by /html/syntax/preload-scanner.tentative/tools/generate.py. -->
<title>Preload scanner, document.write(): img-src</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/common/utils.js></script>
<script src=/html/syntax/preload-scanner.tentative/resources/preload-scanner-util.js></script>
<script>
setup({single_test: true});
const uuid = token();
expect_fetched_onload(uuid, true).then(done);
document.write(`
<script src=/common/slow.py><\/script>
<script>
document.write('<plaintext>');
<\/script>
<img src=/html/syntax/preload-scanner.tentative/resources/stash.py?action=put&uuid=${uuid}>
`);
</script>

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<meta charset=utf-8>
<!-- DO NOT EDIT! This file has been generated by /html/syntax/preload-scanner.tentative/tools/generate.py. -->
<title>Preload scanner, document.write(): picture-source-no-img</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/common/utils.js></script>
<script src=/html/syntax/preload-scanner.tentative/resources/preload-scanner-util.js></script>
<script>
setup({single_test: true});
const uuid = token();
expect_fetched_onload(uuid, false).then(done);
document.write(`
<script src=/common/slow.py><\/script>
<script>
document.write('<plaintext>');
<\/script>
<picture><source srcset=/html/syntax/preload-scanner.tentative/resources/stash.py?action=put&uuid=${uuid}></picture>
`);
</script>

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<meta charset=utf-8>
<!-- DO NOT EDIT! This file has been generated by /html/syntax/preload-scanner.tentative/tools/generate.py. -->
<title>Preload scanner, document.write(): script-src-unsupported-type</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/common/utils.js></script>
<script src=/html/syntax/preload-scanner.tentative/resources/preload-scanner-util.js></script>
<script>
setup({single_test: true});
const uuid = token();
expect_fetched_onload(uuid, false).then(done);
document.write(`
<script src=/common/slow.py><\/script>
<script>
document.write('<plaintext>');
<\/script>
<script src=/html/syntax/preload-scanner.tentative/resources/stash.py?action=put&uuid=${uuid} type=text/plain><\/script>
`);
</script>

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<meta charset=utf-8>
<!-- DO NOT EDIT! This file has been generated by /html/syntax/preload-scanner.tentative/tools/generate.py. -->
<title>Preload scanner, document.write(): script-src</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/common/utils.js></script>
<script src=/html/syntax/preload-scanner.tentative/resources/preload-scanner-util.js></script>
<script>
setup({single_test: true});
const uuid = token();
expect_fetched_onload(uuid, true).then(done);
document.write(`
<script src=/common/slow.py><\/script>
<script>
document.write('<plaintext>');
<\/script>
<script src=/html/syntax/preload-scanner.tentative/resources/stash.py?action=put&uuid=${uuid}><\/script>
`);
</script>

17 changes: 17 additions & 0 deletions html/syntax/preload-scanner.tentative/page-load/img-src.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<meta charset=utf-8>
<!-- DO NOT EDIT! This file has been generated by /html/syntax/preload-scanner.tentative/tools/generate.py. -->
<title>Preload scanner, page load: img-src</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/common/utils.js></script>
<script src=/html/syntax/preload-scanner.tentative/resources/preload-scanner-util.js></script>
<body>
<script>
setup({single_test: true});
const uuid = token();
const iframe = document.createElement('iframe');
iframe.src = `resources/img-src-framed.sub.html?uuid=${uuid}`;
document.body.appendChild(iframe);
expect_fetched_onload(uuid, true).then(done);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<meta charset=utf-8>
<!-- DO NOT EDIT! This file has been generated by /html/syntax/preload-scanner.tentative/tools/generate.py. -->
<title>Preload scanner, page load: picture-source-no-img</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/common/utils.js></script>
<script src=/html/syntax/preload-scanner.tentative/resources/preload-scanner-util.js></script>
<body>
<script>
setup({single_test: true});
const uuid = token();
const iframe = document.createElement('iframe');
iframe.src = `resources/picture-source-no-img-framed.sub.html?uuid=${uuid}`;
document.body.appendChild(iframe);
expect_fetched_onload(uuid, false).then(done);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<meta charset=utf-8>
<!-- DO NOT EDIT! This file has been generated by /html/syntax/preload-scanner.tentative/tools/generate.py. -->
<title>Preload scanner, page load (helper file): img-src</title>
<script src=/common/slow.py></script>
<script>
document.write('<plaintext>');
</script>
<img src=/html/syntax/preload-scanner.tentative/resources/stash.py?action=put&uuid={{GET[uuid]}}>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<meta charset=utf-8>
<!-- DO NOT EDIT! This file has been generated by /html/syntax/preload-scanner.tentative/tools/generate.py. -->
<title>Preload scanner, page load (helper file): picture-source-no-img</title>
<script src=/common/slow.py></script>
<script>
document.write('<plaintext>');
</script>
<picture><source srcset=/html/syntax/preload-scanner.tentative/resources/stash.py?action=put&uuid={{GET[uuid]}}></picture>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<meta charset=utf-8>
<!-- DO NOT EDIT! This file has been generated by /html/syntax/preload-scanner.tentative/tools/generate.py. -->
<title>Preload scanner, page load (helper file): script-src</title>
<script src=/common/slow.py></script>
<script>
document.write('<plaintext>');
</script>
<script src=/html/syntax/preload-scanner.tentative/resources/stash.py?action=put&uuid={{GET[uuid]}}></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<meta charset=utf-8>
<!-- DO NOT EDIT! This file has been generated by /html/syntax/preload-scanner.tentative/tools/generate.py. -->
<title>Preload scanner, page load (helper file): script-src-unsupported-type</title>
<script src=/common/slow.py></script>
<script>
document.write('<plaintext>');
</script>
<script src=/html/syntax/preload-scanner.tentative/resources/stash.py?action=put&uuid={{GET[uuid]}} type=text/plain></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<meta charset=utf-8>
<!-- DO NOT EDIT! This file has been generated by /html/syntax/preload-scanner.tentative/tools/generate.py. -->
<title>Preload scanner, page load: script-src-unsupported-type</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/common/utils.js></script>
<script src=/html/syntax/preload-scanner.tentative/resources/preload-scanner-util.js></script>
<body>
<script>
setup({single_test: true});
const uuid = token();
const iframe = document.createElement('iframe');
iframe.src = `resources/script-src-unsupported-type-framed.sub.html?uuid=${uuid}`;
document.body.appendChild(iframe);
expect_fetched_onload(uuid, false).then(done);
</script>
17 changes: 17 additions & 0 deletions html/syntax/preload-scanner.tentative/page-load/script-src.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<meta charset=utf-8>
<!-- DO NOT EDIT! This file has been generated by /html/syntax/preload-scanner.tentative/tools/generate.py. -->
<title>Preload scanner, page load: script-src</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/common/utils.js></script>
<script src=/html/syntax/preload-scanner.tentative/resources/preload-scanner-util.js></script>
<body>
<script>
setup({single_test: true});
const uuid = token();
const iframe = document.createElement('iframe');
iframe.src = `resources/script-src-framed.sub.html?uuid=${uuid}`;
document.body.appendChild(iframe);
expect_fetched_onload(uuid, true).then(done);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function expect_fetched_onload(uuid, expectation) {
return new Promise((resolve, reject) => {
addEventListener('load', async () => {
const response = await fetch(`/html/syntax/preload-scanner.tentative/resources/stash.py?action=take&uuid=${uuid}`);
const result = await response.text();
if (expectation) {
assert_equals(result, 'fetched', 'expected the preload scanner to prefetch');
} else {
assert_equals(result, '', 'expected the preload scanner to NOT prefetch');
}
resolve();
});
});
}
5 changes: 5 additions & 0 deletions html/syntax/preload-scanner.tentative/resources/stash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def main(request, response):
if request.GET[b"action"] == b"put":
request.server.stash.put(request.GET[b"uuid"], b"fetched")
return u''
return request.server.stash.take(request.GET[b"uuid"])
138 changes: 138 additions & 0 deletions html/syntax/preload-scanner.tentative/tools/generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#!/usr/bin/env python3

# Usage: python3 generate.py
#
# This will remove all existing files in the generated directories and generate new tests.


# Notes on potential confusion with the 3 string substitution features in different layers:
#
# - In Python strings when calling .format(): {something} or {}
# To get a literal {} use {{}}.
# The template_* variables are ones below are those that will use .format().
# https://docs.python.org/3/library/string.html#formatstrings
# - JS template literals: ${something}
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
# - wptserve server-side substitution when generating a response: {{GET[something]}}
# https://web-platform-tests.org/writing-tests/server-pipes.html#sub

import os, shutil

target_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Test data

tests = [
# title, template_testcase_markup, expect_load
(u'script-src', u'<script src={}></script>', u'true'),
(u'script-src-unsupported-type', u'<script src={} type=text/plain></script>', u'false'),
(u'img-src', u'<img src={}>', u'true'),
(u'picture-source-no-img', u'<picture><source srcset={}></picture>', u'false'),
]

preamble = u"""<!DOCTYPE html>
<meta charset=utf-8>
<!-- DO NOT EDIT! This file has been generated by /html/syntax/preload-scanner.tentative/tools/generate.py. -->"""

url_wptserve_sub = u"/html/syntax/preload-scanner.tentative/resources/stash.py?action=put&uuid={{GET[uuid]}}"
url_js_sub = u"/html/syntax/preload-scanner.tentative/resources/stash.py?action=put&uuid=${uuid}"


# Templates

# Scenario: page load

template_pageload_toplevel = u"""{preamble}
<title>Preload scanner, page load: {title}</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/common/utils.js></script>
<script src=/html/syntax/preload-scanner.tentative/resources/preload-scanner-util.js></script>
<body>
<script>
setup({{single_test: true}});
const uuid = token();
const iframe = document.createElement('iframe');
iframe.src = `resources/{title}-framed.sub.html?uuid=${{uuid}}`;
document.body.appendChild(iframe);
expect_fetched_onload(uuid, {expect_load}).then(done);
</script>
"""

template_pageload_framed = u"""{preamble}
<title>Preload scanner, page load (helper file): {title}</title>
<script src=/common/slow.py></script>
<script>
document.write('<plaintext>');
</script>
{testcase_markup}
"""

# Scenario: document.write()

template_docwrite = u"""{preamble}
<title>Preload scanner, document.write(): {title}</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/common/utils.js></script>
<script src=/html/syntax/preload-scanner.tentative/resources/preload-scanner-util.js></script>
<script>
setup({{single_test: true}});
const uuid = token();
expect_fetched_onload(uuid, {expect_load}).then(done);
document.write(`
<script src=/common/slow.py><\\/script>
<script>
document.write('<plaintext>');
<\\/script>
{testcase_markup}
`);
</script>
"""

# Scenario: <link rel=prerender> - TODO(zcorpan)

template_prerender_toplevel = u"""{preamble}
<title>Preload scanner, prerender: {title}</title>
...
"""

template_prerender_linked = u"""{preamble}
<title>Preload scanner, prerender (helper file): {title}</title>
...
"""

# Generate tests

def wipe_dir(path):
path = os.path.join(target_dir, path)
if os.path.isdir(path) == False:
os.mkdir(path)
for filename in os.listdir(path):
file_path = os.path.join(path, filename)
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)

def write_file(path, content):
path = os.path.join(target_dir, path)
file = open(os.path.join(target_dir, path), 'w')
file.write(content)
file.close()

wipe_dir(u"page-load")
wipe_dir(u"page-load/resources")
wipe_dir(u"document-write")

for testcase in tests:
title, template_testcase_markup, expect_load = testcase

pageload_testcase_markup = template_testcase_markup.format(url_wptserve_sub)
pageload_toplevel = template_pageload_toplevel.format(preamble=preamble, title=title, expect_load=expect_load)
write_file(f"page-load/{title}.html", pageload_toplevel)
pageload_framed = template_pageload_framed.format(preamble=preamble, title=title, testcase_markup=pageload_testcase_markup)
write_file(f"page-load/resources/{title}-framed.sub.html", pageload_framed)

docwrite_testcase_markup = template_testcase_markup.format(url_js_sub).replace(u"</script>", u"<\/script>")
docwrite = template_docwrite.format(preamble=preamble, title=title, expect_load=expect_load, testcase_markup=docwrite_testcase_markup)
write_file(f"document-write/{title}.html", docwrite)
1 change: 1 addition & 0 deletions update-built-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ html/tools/build.sh
python mimesniff/mime-types/resources/generated-mime-types.py
python3 css/css-ui/tools/appearance-build-webkit-reftests.py
python3 WebIDL/tools/generate-setlike.py
python3 html/syntax/preload-scanner.tentative/tools/generate.py

0 comments on commit a69d3b1

Please sign in to comment.