From d1685bd9d5c1ac9d6588b255656411cea4dba45e Mon Sep 17 00:00:00 2001 From: Simon Pieters Date: Wed, 8 Jul 2020 23:14:17 +0200 Subject: [PATCH] HTML: Add tentative tests for preload scanner See https://github.com/whatwg/html/issues/5624 --- .../document-write/img-src.html | 21 +++ .../document-write/picture-source-no-img.html | 21 +++ .../script-src-unsupported-type.html | 21 +++ .../document-write/script-src.html | 21 +++ .../page-load/img-src.html | 17 +++ .../page-load/picture-source-no-img.html | 17 +++ .../resources/img-src-framed.sub.html | 9 ++ .../picture-source-no-img-framed.sub.html | 9 ++ .../resources/script-src-framed.sub.html | 9 ++ ...cript-src-unsupported-type-framed.sub.html | 9 ++ .../script-src-unsupported-type.html | 17 +++ .../page-load/script-src.html | 17 +++ .../resources/preload-scanner-util.js | 14 ++ .../resources/stash.py | 5 + .../tools/generate.py | 138 ++++++++++++++++++ update-built-tests.sh | 1 + 16 files changed, 346 insertions(+) create mode 100644 html/syntax/preload-scanner.tentative/document-write/img-src.html create mode 100644 html/syntax/preload-scanner.tentative/document-write/picture-source-no-img.html create mode 100644 html/syntax/preload-scanner.tentative/document-write/script-src-unsupported-type.html create mode 100644 html/syntax/preload-scanner.tentative/document-write/script-src.html create mode 100644 html/syntax/preload-scanner.tentative/page-load/img-src.html create mode 100644 html/syntax/preload-scanner.tentative/page-load/picture-source-no-img.html create mode 100644 html/syntax/preload-scanner.tentative/page-load/resources/img-src-framed.sub.html create mode 100644 html/syntax/preload-scanner.tentative/page-load/resources/picture-source-no-img-framed.sub.html create mode 100644 html/syntax/preload-scanner.tentative/page-load/resources/script-src-framed.sub.html create mode 100644 html/syntax/preload-scanner.tentative/page-load/resources/script-src-unsupported-type-framed.sub.html create mode 100644 html/syntax/preload-scanner.tentative/page-load/script-src-unsupported-type.html create mode 100644 html/syntax/preload-scanner.tentative/page-load/script-src.html create mode 100644 html/syntax/preload-scanner.tentative/resources/preload-scanner-util.js create mode 100755 html/syntax/preload-scanner.tentative/resources/stash.py create mode 100755 html/syntax/preload-scanner.tentative/tools/generate.py diff --git a/html/syntax/preload-scanner.tentative/document-write/img-src.html b/html/syntax/preload-scanner.tentative/document-write/img-src.html new file mode 100644 index 00000000000000..720b98ac6290eb --- /dev/null +++ b/html/syntax/preload-scanner.tentative/document-write/img-src.html @@ -0,0 +1,21 @@ + + + +Preload scanner, document.write(): img-src + + + + + + diff --git a/html/syntax/preload-scanner.tentative/document-write/picture-source-no-img.html b/html/syntax/preload-scanner.tentative/document-write/picture-source-no-img.html new file mode 100644 index 00000000000000..f9747bb8d5b70f --- /dev/null +++ b/html/syntax/preload-scanner.tentative/document-write/picture-source-no-img.html @@ -0,0 +1,21 @@ + + + +Preload scanner, document.write(): picture-source-no-img + + + + + + diff --git a/html/syntax/preload-scanner.tentative/document-write/script-src-unsupported-type.html b/html/syntax/preload-scanner.tentative/document-write/script-src-unsupported-type.html new file mode 100644 index 00000000000000..68d335a210ec08 --- /dev/null +++ b/html/syntax/preload-scanner.tentative/document-write/script-src-unsupported-type.html @@ -0,0 +1,21 @@ + + + +Preload scanner, document.write(): script-src-unsupported-type + + + + + + diff --git a/html/syntax/preload-scanner.tentative/document-write/script-src.html b/html/syntax/preload-scanner.tentative/document-write/script-src.html new file mode 100644 index 00000000000000..69cefe53d3d03e --- /dev/null +++ b/html/syntax/preload-scanner.tentative/document-write/script-src.html @@ -0,0 +1,21 @@ + + + +Preload scanner, document.write(): script-src + + + + + + diff --git a/html/syntax/preload-scanner.tentative/page-load/img-src.html b/html/syntax/preload-scanner.tentative/page-load/img-src.html new file mode 100644 index 00000000000000..dddfa5d03c63df --- /dev/null +++ b/html/syntax/preload-scanner.tentative/page-load/img-src.html @@ -0,0 +1,17 @@ + + + +Preload scanner, page load: img-src + + + + + + diff --git a/html/syntax/preload-scanner.tentative/page-load/picture-source-no-img.html b/html/syntax/preload-scanner.tentative/page-load/picture-source-no-img.html new file mode 100644 index 00000000000000..cc7ddbd5b8c3aa --- /dev/null +++ b/html/syntax/preload-scanner.tentative/page-load/picture-source-no-img.html @@ -0,0 +1,17 @@ + + + +Preload scanner, page load: picture-source-no-img + + + + + + diff --git a/html/syntax/preload-scanner.tentative/page-load/resources/img-src-framed.sub.html b/html/syntax/preload-scanner.tentative/page-load/resources/img-src-framed.sub.html new file mode 100644 index 00000000000000..cfd9b91cbfe90b --- /dev/null +++ b/html/syntax/preload-scanner.tentative/page-load/resources/img-src-framed.sub.html @@ -0,0 +1,9 @@ + + + +Preload scanner, page load (helper file): img-src + + + diff --git a/html/syntax/preload-scanner.tentative/page-load/resources/picture-source-no-img-framed.sub.html b/html/syntax/preload-scanner.tentative/page-load/resources/picture-source-no-img-framed.sub.html new file mode 100644 index 00000000000000..e40c1505382ace --- /dev/null +++ b/html/syntax/preload-scanner.tentative/page-load/resources/picture-source-no-img-framed.sub.html @@ -0,0 +1,9 @@ + + + +Preload scanner, page load (helper file): picture-source-no-img + + + diff --git a/html/syntax/preload-scanner.tentative/page-load/resources/script-src-framed.sub.html b/html/syntax/preload-scanner.tentative/page-load/resources/script-src-framed.sub.html new file mode 100644 index 00000000000000..deeaa373fec7a2 --- /dev/null +++ b/html/syntax/preload-scanner.tentative/page-load/resources/script-src-framed.sub.html @@ -0,0 +1,9 @@ + + + +Preload scanner, page load (helper file): script-src + + + diff --git a/html/syntax/preload-scanner.tentative/page-load/resources/script-src-unsupported-type-framed.sub.html b/html/syntax/preload-scanner.tentative/page-load/resources/script-src-unsupported-type-framed.sub.html new file mode 100644 index 00000000000000..e07bea55c9c10b --- /dev/null +++ b/html/syntax/preload-scanner.tentative/page-load/resources/script-src-unsupported-type-framed.sub.html @@ -0,0 +1,9 @@ + + + +Preload scanner, page load (helper file): script-src-unsupported-type + + + diff --git a/html/syntax/preload-scanner.tentative/page-load/script-src-unsupported-type.html b/html/syntax/preload-scanner.tentative/page-load/script-src-unsupported-type.html new file mode 100644 index 00000000000000..a2f13d84de8904 --- /dev/null +++ b/html/syntax/preload-scanner.tentative/page-load/script-src-unsupported-type.html @@ -0,0 +1,17 @@ + + + +Preload scanner, page load: script-src-unsupported-type + + + + + + diff --git a/html/syntax/preload-scanner.tentative/page-load/script-src.html b/html/syntax/preload-scanner.tentative/page-load/script-src.html new file mode 100644 index 00000000000000..930fda14a4dabe --- /dev/null +++ b/html/syntax/preload-scanner.tentative/page-load/script-src.html @@ -0,0 +1,17 @@ + + + +Preload scanner, page load: script-src + + + + + + diff --git a/html/syntax/preload-scanner.tentative/resources/preload-scanner-util.js b/html/syntax/preload-scanner.tentative/resources/preload-scanner-util.js new file mode 100644 index 00000000000000..c6e5831a00bde0 --- /dev/null +++ b/html/syntax/preload-scanner.tentative/resources/preload-scanner-util.js @@ -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(); + }); + }); +} diff --git a/html/syntax/preload-scanner.tentative/resources/stash.py b/html/syntax/preload-scanner.tentative/resources/stash.py new file mode 100755 index 00000000000000..370266f86eeb74 --- /dev/null +++ b/html/syntax/preload-scanner.tentative/resources/stash.py @@ -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"]) diff --git a/html/syntax/preload-scanner.tentative/tools/generate.py b/html/syntax/preload-scanner.tentative/tools/generate.py new file mode 100755 index 00000000000000..f49383e6cc0d42 --- /dev/null +++ b/html/syntax/preload-scanner.tentative/tools/generate.py @@ -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'', u'true'), + (u'script-src-unsupported-type', u'', u'false'), + (u'img-src', u'', u'true'), + (u'picture-source-no-img', u'', u'false'), +] + +preamble = u""" + +""" + +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} +Preload scanner, page load: {title} + + + + + + +""" + +template_pageload_framed = u"""{preamble} +Preload scanner, page load (helper file): {title} + + +{testcase_markup} +""" + +# Scenario: document.write() + +template_docwrite = u"""{preamble} +Preload scanner, document.write(): {title} + + + + + + +""" + +# Scenario: - TODO(zcorpan) + +template_prerender_toplevel = u"""{preamble} +Preload scanner, prerender: {title} +... +""" + +template_prerender_linked = u"""{preamble} +Preload scanner, prerender (helper file): {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"", 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) diff --git a/update-built-tests.sh b/update-built-tests.sh index 3bc5e367948d9d..bdc40953249eb9 100755 --- a/update-built-tests.sh +++ b/update-built-tests.sh @@ -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