Commit c6b62080 authored by Luciano Pacheco's avatar Luciano Pacheco Committed by Commit Bot

Files app: Change js_test_gen_html() to generate HTML for JS module unittest

Note: per [1] JS modules must be served with text/javascript HTTP
Content-Type. Change file_manager_jstest_base.cc to add Content-Type
headers.

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules

Bug: 1133186
Change-Id: I6a5436984052d74af267634f6fe0db70424b51a8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2470039Reviewed-by: default avatarNoel Gordon <noel@chromium.org>
Commit-Queue: Noel Gordon <noel@chromium.org>
Auto-Submit: Luciano Pacheco <lucmult@chromium.org>
Cr-Commit-Position: refs/heads/master@{#819263}
parent 845becb3
......@@ -90,6 +90,8 @@ class TestFilesDataSource : public content::URLDataSource {
std::move(callback).Run(response.get());
}
bool ShouldServeMimeTypeAsContentTypeHeader() override { return true; }
// It currently only serves HTML/JS/CSS/SVG.
std::string GetMimeType(const std::string& path) override {
if (base::EndsWith(path, ".html", base::CompareCase::INSENSITIVE_ASCII)) {
......
......@@ -27,6 +27,12 @@ import("//third_party/closure_compiler/js_unit_tests.gni")
# generates HTMLImport instead of <script>. Only the main element is
# imported.
#
# js_module:
# Boolean indicating that it's a JS module so the HTML file will only
# contain <script type="module" src="..._unittest.m.js"> and traditional
# <script>s for mocks.
#
#
# Non-Polymer example:
# js_test_gen_html("folder_tests") {
# deps = [
......@@ -70,6 +76,7 @@ template("js_test_gen_html") {
"deps",
"mocks",
"html_import",
"js_module",
])
testonly = true
sources = []
......@@ -94,6 +101,10 @@ template("js_test_gen_html") {
args += [ "--html_import" ]
}
if (defined(js_module) && js_module) {
args += [ "--js_module" ]
}
if (defined(mocks)) {
args += [ "--mocks" ] + rebase_path(mocks, root_build_dir)
data = mocks
......
......@@ -22,9 +22,21 @@ window.addEventListener('error', function(e) {
<body>
"""
_SCRIPT = r'<script src="%s"></script>'
_CLASSIC_SCRIPT = r'<script src="%s"></script>'
_JS_MODULE = r'<script type="module" src="%s"></script>'
_JS_MODULE_REGISTER_TESTS = r'''
<script>
// Push all entities to global namespace to be visible to the test harness:
// ui/webui/resources/js/webui_resource_test.js
import('%s').then(TestModule => {
for (const name in TestModule) {
window[name] = TestModule[name];
}
});
</script>
'''
_IMPORT = r'<link rel="import" href="%s">'
_HTML_IMPORT_POLYFIL = _SCRIPT % (
_HTML_IMPORT_POLYFIL = _CLASSIC_SCRIPT % (
'chrome://resources/polymer/v1_0/html-imports/html-imports.min.js')
_HTML_FOOTER = r"""
......@@ -33,13 +45,13 @@ _HTML_FOOTER = r"""
"""
def _process_deps(unique_deps, html_import, target_name):
def _process_deps(unique_deps, dep_type, target_name):
"""Processes all deps strings, yielding each HTML tag to include the dep.
Args:
unique_deps: Iterator of strings, for all deps to be processed.
html_import: Boolean: Enables the use of HTMLImport for the main Polymer
element being tested.
dep_type: String: 'classic_script' | 'js_module' |
'js_module_register_tests' | 'html_import'.
target_name: Current test target name, used to infer the main Polymer
element for HTMLImport. element_unitest => element.js/element.html.
......@@ -86,12 +98,39 @@ def _process_deps(unique_deps, html_import, target_name):
# included, instead we <link rel=import> its HTML file which in turn
# includes the JS file. Note that all other JS deps are included as
# <script>.
if html_import and dep.endswith(implementation_file):
if dep_type == 'html_import'and dep.endswith(implementation_file):
dep = dep.replace('.js', '.html')
yield _IMPORT % (dep)
elif dep_type == 'js_module':
yield _JS_MODULE % (dep)
elif dep_type == 'js_module_register_tests':
yield _JS_MODULE_REGISTER_TESTS % (dep)
else:
# Normal dep, just return the <script src="dep.js">
yield _SCRIPT % (dep)
yield _CLASSIC_SCRIPT % (dep)
def _process_js_module(input_file, output_filename, mocks, target_name):
"""Generates the HTML for a unittest based on JS Modules.
Args:
input_file: The path for the unittest JS module.
output_filename: The path/filename for HTML to be generated.
mocks: List of strings, JS file names that will be included in the bottom to
overwrite JS implementation from deps.
target_name: Current test target name, used to infer the main Polymer
element for HTMLImport. element_unitest => element.js/element.html.
"""
with open(output_filename, 'w') as out:
out.write(_HTML_FILE)
for dep in _process_deps(mocks, 'js_module', target_name):
out.write(dep + '\n')
for dep in _process_deps([input_file], 'js_module', target_name):
out.write(dep + '\n')
for dep in _process_deps([input_file], 'js_module_register_tests',
target_name):
out.write(dep + '\n')
def _process(deps, output_filename, mocks, html_import, target_name):
......@@ -114,9 +153,10 @@ def _process(deps, output_filename, mocks, html_import, target_name):
if html_import:
out.write(_HTML_IMPORT_POLYFIL + '\n')
for dep in _process_deps(mocks, html_import, target_name):
dep_type = 'html_import' if html_import else 'classic_script'
for dep in _process_deps(mocks, dep_type, target_name):
out.write(dep + '\n')
for dep in _process_deps(deps, html_import, target_name):
for dep in _process_deps(deps, dep_type, target_name):
out.write(dep + '\n')
out.write(_HTML_FOOTER)
......@@ -147,8 +187,23 @@ def main():
'--html_import',
action='store_true',
help='Enable HTMLImports, used for Polymer elements')
parser.add_argument(
'--js_module',
action='store_true',
help='Enable JS Modules for the unittest file.')
args = parser.parse_args()
if args.js_module:
# Convert from:
# gen/ui/file_manager/file_manager/common/js/example_unittest.m.js_library
# To:
# ui/file_manager/file_manager/common/js/example_unittest.m.js
path_test_file = args.input.replace('gen/', '', 1)
path_test_file = path_test_file.replace('.js_library', '.js')
_process_js_module(path_test_file, args.output, args.mocks,
args.target_name)
return
# Append closure path to sys.path to be able to import js_unit_test.
sys.path.append(os.path.join(args.src_path, 'third_party/closure_compiler'))
from js_binary import CrawlDepsTree
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment