Skip to content

Commit

Permalink
[Import Maps] Remove Blink internals dependency from WPT/import-maps/…
Browse files Browse the repository at this point in the history
…common

Quick draft.

Note:

https://web-platform-tests.org/writing-tests/testdriver-extension-tutorial.html
provides two methods, `testdriver-vendor.js` and `testdriver-extra.js`.
I'm not sure which method to use, but in this CL
I used `testdriver-vendor.js` method, because

- It was easier to implement.
- So far the API is expected only for testing and not needed to be
  exposed for automation.

This CL contradicts with:
https://web-platform-tests.org/writing-tests/testdriver.html
> NB: presently, testdriver.js only works in the top-level
> test browsing context (and not therefore in any frame or
> window opened from it).
because `test_driver` is accessed from inside `<iframe>`s
in this CL.
Perhaps this is OK in this case, as the `resolve_module_specifier`
implementation works inside <iframe>s, while other `test_driver`
method might stop working there.

Bug: 1026809, WICG/import-maps#170
Change-Id: I8684ec544382e3036b9885e09f0c3a4d97208da5
  • Loading branch information
hiroshige-g authored and LukeZielinski committed Dec 5, 2019
1 parent ec26269 commit fd0a733
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 17 deletions.
41 changes: 25 additions & 16 deletions import-maps/common/resources/common-test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ function parse(importMap, importMapBaseURL) {
},
{once: true});

const host = location.origin;

const testHTML = `
<script src="${host}/resources/testdriver.js"></script>
<script src="${host}/resources/testdriver-vendor.js"></script>
<script>
// Handle errors around fetching, parsing and registering import maps.
let registrationResult;
Expand All @@ -39,20 +43,23 @@ function parse(importMap, importMapBaseURL) {
// Handle specifier resolution requests from the parent frame.
window.addEventListener('message', event => {
try {
// URL resolution is tested using Chromium's internals.
// TODO(hiroshige): Remove the Chromium-specific dependency.
const result = internals.resolveModuleSpecifier(
event.data.specifier,
event.data.baseURL,
document);
parent.postMessage({type: 'ResolutionSuccess', result: result}, '*');
} catch (e) {
// We post error names instead of error objects themselves and
// re-create error objects later, to avoid issues around serializing
// error objects which is a quite new feature.
parent.postMessage({type: 'ResolutionFailure', result: e.name}, '*');
}
// Override the base URL.
const base = document.createElement('base');
base.setAttribute('href', event.data.baseURL);
document.head.insertBefore(base, document.head.firstChild);
test_driver.resolve_module_specifier(event.data.specifier)
.then(result => {
parent.postMessage(
{type: 'ResolutionSuccess', result: result}, '*');
})
.catch(e => {
// We post error names instead of error objects themselves and
// re-create error objects later, to avoid issues around
// serializing error objects which is a quite new feature.
parent.postMessage(
{type: 'ResolutionFailure', result: e.name}, '*');
});
});
</script>
<script type="importmap" onerror="onScriptError(event)">
Expand All @@ -61,9 +68,11 @@ function parse(importMap, importMapBaseURL) {
`;

if (new URL(importMapBaseURL).protocol === 'data:') {
iframe.src = 'data:text/html;base64,' + btoa(testHTML);
iframe.src =
`data:text/html;base64,<head></head>${btoa(testHTML)}`;
} else {
iframe.srcdoc = `<base href="${importMapBaseURL}">` + testHTML;
iframe.srcdoc =
`<head><base href="${importMapBaseURL}"></head>${testHTML}`;
}

document.body.appendChild(iframe);
Expand Down
1 change: 0 additions & 1 deletion lint.whitelist
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,6 @@ MISSING-LINK: css/filter-effects/*.any.js
# Tests that use WebKit/Blink testing APIs
LAYOUTTESTS APIS: css/css-regions/interactivity/*
LAYOUTTESTS APIS: import-maps/resources/jest-test-helper.js
LAYOUTTESTS APIS: import-maps/common/resources/common-test-helper.js
LAYOUTTESTS APIS: resources/chromium/generic_sensor_mocks.js
LAYOUTTESTS APIS: resources/chromium/nfc-mock.js
LAYOUTTESTS APIS: resources/chromium/webxr-test.js
Expand Down
29 changes: 29 additions & 0 deletions resources/testdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,21 @@
set_user_verified: function(authenticator_id, uv) {
return window.test_driver_internal.set_user_verified(authenticator_id, uv);
},

/**
* Resolves the given specifier, possibly with import maps.
* https://wicg.github.io/import-maps/#resolve-a-module-specifier
*
* @param {String} specifier - the specifier to be resolved.
*
* @returns {Promise} resolved with serialization of the resulting URL
* as a {String},
* or rejected on specifier resolution failures.
*/
resolve_module_specifier: function(specifier) {
return window.test_driver_internal.resolve_module_specifier(
specifier);
},
};

window.test_driver_internal = {
Expand Down Expand Up @@ -569,5 +584,19 @@
set_user_verified: function(authenticator_id, uv) {
return Promise.reject(new Error("unimplemented"));
},

/**
* Resolves the given specifier, possibly with import maps.
* https://wicg.github.io/import-maps/#resolve-a-module-specifier
*
* @param {String} specifier - the specifier to be resolved.
*
* @returns {Promise} resolved with serialization of the resulting URL
* as a {String},
* or rejected on specifier resolution failures.
*/
resolve_module_specifier: function(specifier) {
return Promise.reject(new Error("unimplemented"));
},
};
})();

0 comments on commit fd0a733

Please sign in to comment.