Skip to content

Commit 8d71043

Browse files
Hexcleschromium-wpt-export-bot
authored andcommitted
[WPT] Introduce a test-only-api helper script
including a helper function to load Mojo JS bindings. This will enable us to have better control over loading of *.mojom.js (notably disable automatic dependency loading) and reduce code duplication. Refactor WebXR tests to use this helper script, and add missing dependencies that were previously auto loaded. (WebUSB & WebBluetooth changes to follow.) Note that upstream WPT still does not have *.mojom.js available so tests will continue to fail there (but with a clearer error). Test results on Chromium waterfall should not change. Bug: 1094512 Change-Id: If660c4788c185bc7baf9ce6edbb691333e509d4a Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2276652 Reviewed-by: Stephen McGruer <[email protected]> Reviewed-by: Alexander Cooper <[email protected]> Commit-Queue: Robert Ma <[email protected]> Cr-Commit-Position: refs/heads/master@{#786746}
1 parent dea212b commit 8d71043

File tree

4 files changed

+103
-22
lines changed

4 files changed

+103
-22
lines changed

lint.ignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@ MISSING-LINK: css/filter-effects/*.any.js
688688

689689
# Tests that use WebKit/Blink testing APIs
690690
LAYOUTTESTS APIS: import-maps/common/resources/common-test-helper.js
691+
LAYOUTTESTS APIS: resources/test-only-api.js
691692
LAYOUTTESTS APIS: resources/chromium/enable-hyperlink-auditing.js
692693
LAYOUTTESTS APIS: resources/chromium/generic_sensor_mocks.js
693694
LAYOUTTESTS APIS: resources/chromium/nfc-mock.js
@@ -709,6 +710,7 @@ WEB-PLATFORM.TEST:web-bundle/resources/wbn/*.wbn
709710
# Tests that depend on resources in /gen/ in Chromium:
710711
# https://github.com/web-platform-tests/wpt/issues/16455
711712
# Please consult with [email protected] before adding more.
713+
MISSING DEPENDENCY: resources/test-only-api.js
712714
MISSING DEPENDENCY: idle-detection/interceptor.https.html
713715
MISSING DEPENDENCY: credential-management/support/otpcredential-helper.js
714716
MISSING DEPENDENCY: web-nfc/resources/nfc-helpers.js

resources/test-only-api.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'use strict';
2+
3+
/* Whether the browser is Chromium-based with MojoJS enabled */
4+
const isChromiumBased = 'MojoInterfaceInterceptor' in self;
5+
/* Whether the browser is WebKit-based with internal test-only API enabled */
6+
const isWebKitBased = !isChromiumBased && 'internals' in self;
7+
8+
/**
9+
* Loads a script in a window or worker.
10+
*
11+
* @param {string} path - A script path
12+
* @returns {Promise}
13+
*/
14+
function loadScript(path) {
15+
if (typeof document === 'undefined') {
16+
// Workers (importScripts is synchronous and may throw.)
17+
importScripts(path);
18+
return Promise.resolve();
19+
} else {
20+
// Window
21+
const script = document.createElement('script');
22+
script.src = path;
23+
script.async = false;
24+
const p = new Promise((resolve, reject) => {
25+
script.onload = () => { resolve(); };
26+
script.onerror = e => { reject(e); };
27+
})
28+
document.head.appendChild(script);
29+
return p;
30+
}
31+
}
32+
33+
/**
34+
* A helper for Chromium-based browsers to load Mojo JS bindingds
35+
*
36+
* This is an async function that works in both workers and windows. It first
37+
* loads mojo_bindings.js, disables automatic dependency loading, and loads all
38+
* resources sequentially. The promise resolves if everything loads
39+
* successfully, or rejects if any exception is raised. If testharness.js is
40+
* used, an uncaught exception will terminate the test with a harness error
41+
* (unless `allow_uncaught_exception` is true), which is usually the desired
42+
* behaviour. Only call this function if isChromiumBased === true.
43+
*
44+
* @param {Array.<string>} resources - A list of scripts to load: Mojo JS
45+
* bindings should be of the form '/gen/../*.mojom.js', the ordering of which
46+
* does not matter. Do not include mojo_bindings.js in this list. You may
47+
* include other non-mojom.js scripts for convenience.
48+
* @returns {Promise}
49+
*/
50+
async function loadMojoResources(resources) {
51+
if (!isChromiumBased) {
52+
throw new Error('MojoJS not enabled; start Chrome with --enable-blink-features=MojoJS,MojoJSTest');
53+
}
54+
if (resources.length == 0) {
55+
return;
56+
}
57+
58+
// We want to load mojo_bindings.js separately to set mojo.config.
59+
if (resources.some(p => p.endsWith('/mojo_bindings.js'))) {
60+
throw new Error('Do not load mojo_bindings.js explicitly.');
61+
}
62+
await loadScript('/gen/layout_test_data/mojo/public/js/mojo_bindings.js');
63+
mojo.config.autoLoadMojomDeps = false;
64+
65+
for (const path of resources) {
66+
await loadScript(path);
67+
}
68+
}

resources/test-only-api.js.headers

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Content-Type: text/javascript; charset=utf-8
2+
Cache-Control: max-age=3600

webxr/resources/webxr_util.js

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
// These tests rely on the User Agent providing an implementation of the
24
// WebXR Testing API (https://github.com/immersive-web/webxr-test-api).
35
//
@@ -9,22 +11,34 @@
911

1012
// Debugging message helper, by default does nothing. Implementations can
1113
// override this.
12-
var xr_debug = function(name, msg) {}
13-
var isChromiumBased = 'MojoInterfaceInterceptor' in self;
14-
var isWebKitBased = 'internals' in self && 'xrTest' in internals;
14+
var xr_debug = function(name, msg) {};
1515

1616
function xr_promise_test(name, func, properties) {
1717
promise_test(async (t) => {
1818
// Perform any required test setup:
1919
xr_debug(name, 'setup');
2020

21+
// Only set up once.
2122
if (!navigator.xr.test) {
23+
// Load test-only API helpers.
24+
const script = document.createElement('script');
25+
script.src = '/resources/test-only-api.js';
26+
script.async = false;
27+
const p = new Promise((resolve, reject) => {
28+
script.onload = () => { resolve(); };
29+
script.onerror = e => { reject(e); };
30+
})
31+
document.head.appendChild(script);
32+
await p;
33+
2234
if (isChromiumBased) {
2335
// Chrome setup
2436
await loadChromiumResources();
2537
} else if (isWebKitBased) {
2638
// WebKit setup
2739
await setupWebKitWebXRTestAPI();
40+
} else {
41+
assert_implements(false, "missing navigator.xr.test");
2842
}
2943
}
3044

@@ -185,19 +199,27 @@ function forEachWebxrObject(callback) {
185199
}
186200

187201
// Code for loading test API in Chromium.
188-
function loadChromiumResources() {
202+
async function loadChromiumResources() {
189203
let chromiumResources = [
190-
'/gen/layout_test_data/mojo/public/js/mojo_bindings.js',
191204
'/gen/mojo/public/mojom/base/time.mojom.js',
192-
'/gen/gpu/ipc/common/mailbox_holder.mojom.js',
205+
'/gen/mojo/public/mojom/base/shared_memory.mojom.js',
206+
'/gen/mojo/public/mojom/base/unguessable_token.mojom.js',
193207
'/gen/gpu/ipc/common/sync_token.mojom.js',
194-
'/gen/ui/display/mojom/display.mojom.js',
208+
'/gen/gpu/ipc/common/mailbox.mojom.js',
209+
'/gen/gpu/ipc/common/mailbox_holder.mojom.js',
195210
'/gen/ui/gfx/geometry/mojom/geometry.mojom.js',
211+
'/gen/ui/gfx/mojom/native_handle_types.mojom.js',
212+
'/gen/ui/gfx/mojom/buffer_types.mojom.js',
213+
'/gen/ui/gfx/mojom/color_space.mojom.js',
214+
'/gen/ui/gfx/mojom/display_color_spaces.mojom.js',
196215
'/gen/ui/gfx/mojom/gpu_fence_handle.mojom.js',
197216
'/gen/ui/gfx/mojom/transform.mojom.js',
217+
'/gen/ui/display/mojom/display.mojom.js',
218+
'/gen/device/gamepad/public/mojom/gamepad.mojom.js',
198219
'/gen/device/vr/public/mojom/vr_service.mojom.js',
199220
'/resources/chromium/webxr-test-math-helper.js',
200221
'/resources/chromium/webxr-test.js',
222+
// Required only by resources/chromium/webxr-test.js
201223
'/resources/testdriver.js',
202224
'/resources/testdriver-vendor.js',
203225
];
@@ -210,22 +232,9 @@ function loadChromiumResources() {
210232
chromiumResources = chromiumResources.concat(additionalChromiumResources);
211233
}
212234

213-
let chain = Promise.resolve();
214-
chromiumResources.forEach(path => {
215-
let script = document.createElement('script');
216-
script.src = path;
217-
script.async = false;
218-
chain = chain.then(() => new Promise(resolve => {
219-
script.onload = () => resolve();
220-
}));
221-
document.head.appendChild(script);
222-
});
223-
224-
chain = chain.then(() => {
225-
xr_debug = navigator.xr.test.Debug;
226-
});
235+
await loadMojoResources(chromiumResources);
227236

228-
return chain;
237+
xr_debug = navigator.xr.test.Debug;
229238
}
230239

231240
function setupWebKitWebXRTestAPI() {

0 commit comments

Comments
 (0)