Skip to content

Commit

Permalink
[WPT] Refactor credential-management to use test-only-api.js
Browse files Browse the repository at this point in the history
With necessary changes to make Mojo lite bindings available in WPT:
* Release mojo/public/mojom/base/*.mojom*.js (mojo_bindings_lite.js was
  already released).
* Add an optional `lite` param to loadMojoResources().

Fixed: 1123987, 1126628
Change-Id: I35d37aa848e7a6333e6185450280c44afd6438c8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2410676
Reviewed-by: Michael Moss <[email protected]>
Reviewed-by: Reilly Grant <[email protected]>
Commit-Queue: Robert Ma <[email protected]>
Cr-Commit-Position: refs/heads/master@{#807253}
  • Loading branch information
Hexcles authored and chromium-wpt-export-bot committed Sep 16, 2020
1 parent bb71279 commit 1d14e82
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 36 deletions.
3 changes: 2 additions & 1 deletion credential-management/otpcredential-get-basics.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<title>Tests OTPCredential</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="./support/otpcredential-helper.js"></script>
<script src="/resources/test-only-api.js"></script>
<script src="support/otpcredential-helper.js"></script>
<script>
'use strict';

Expand Down
9 changes: 6 additions & 3 deletions credential-management/support/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ and the underlying operating system and mock its behavior.

Usage:

1. Include `<script src="./support/otpcredential-helper.js"></script>` in your
test
2. Set expectations
1. Include the following in your test:
```html
<script src="/resources/test-only-api.js"></script>
<script src="support/otpcredential-helper.js"></script>
```
2. Set expectations
```javascript
await expect(receive).andReturn(() => {
// mock behavior
})
Expand Down
35 changes: 11 additions & 24 deletions credential-management/support/otpcredential-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,32 @@
// these tests the browser must be run with these options:
// // --enable-blink-features=MojoJS,MojoJSTest

async function loadChromiumResources() {
if (!window.MojoInterfaceInterceptor) {
// Do nothing on non-Chromium-based browsers or when the Mojo bindings are
// not present in the global namespace.
return;
}
const Status = {};

async function loadChromiumResources() {
const resources = [
'/gen/layout_test_data/mojo/public/js/mojo_bindings_lite.js',
'/gen/mojo/public/mojom/base/time.mojom-lite.js',
'/gen/third_party/blink/public/mojom/sms/sms_receiver.mojom-lite.js',
'/resources/chromium/mock-sms-receiver.js',
];

await Promise.all(resources.map(path => {
const script = document.createElement('script');
script.src = path;
script.async = false;
const promise = new Promise((resolve, reject) => {
script.onload = resolve;
script.onerror = reject;
});
document.head.appendChild(script);
return promise;
}));
await loadMojoResources(resources, true);
await loadScript('/resources/chromium/mock-sms-receiver.js');

Status.kSuccess = blink.mojom.SmsStatus.kSuccess;
Status.kTimeout = blink.mojom.SmsStatus.kTimeout;
Status.kCancelled = blink.mojom.SmsStatus.kCancelled;
};

const Status = {};

async function create_sms_provider() {
if (typeof SmsProvider === 'undefined') {
await loadChromiumResources();
if (isChromiumBased) {
await loadChromiumResources();
} else {
throw new Error('Mojo testing interface is not available.');
}
}
if (typeof SmsProvider == 'undefined') {
throw new Error('Mojo testing interface is not available.');
if (typeof SmsProvider === 'undefined') {
throw new Error('Failed to set up SmsProvider.');
}
return new SmsProvider();
}
Expand Down
3 changes: 2 additions & 1 deletion credential-management/support/otpcredential-iframe.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<!doctype html>
<script src="./otpcredential-helper.js"></script>
<script src="/resources/test-only-api.js"></script>
<script src="otpcredential-helper.js"></script>
<script>
'use strict';

Expand Down
30 changes: 23 additions & 7 deletions resources/test-only-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ function loadScript(path) {
* Only call this function if isChromiumBased === true.
*
* @param {Array.<string>} resources - A list of scripts to load: Mojo JS
* bindings should be of the form '/gen/../*.mojom.js', the ordering of which
* does not matter. Do not include mojo_bindings.js in this list.
* bindings should be of the form '/gen/../*.mojom.js' or
* '/gen/../*.mojom-lite.js' (requires `lite` to be true); the order does not
* matter. Do not include 'mojo_bindings.js' or 'mojo_bindings_lite.js'.
* @param {boolean=} lite - Whether the lite bindings (*.mojom-lite.js) are used
* (default is false).
* @returns {Promise}
*/
async function loadMojoResources(resources) {
async function loadMojoResources(resources, lite = false) {
if (!isChromiumBased) {
throw new Error('MojoJS not enabled; start Chrome with --enable-blink-features=MojoJS,MojoJSTest');
}
Expand All @@ -70,13 +73,26 @@ async function loadMojoResources(resources) {
if (path.endsWith('/mojo_bindings.js')) {
throw new Error('Do not load mojo_bindings.js explicitly.');
}
if (! /^\/gen\/.*\.mojom\.js$/.test(path)) {
throw new Error(`Unrecognized resource path: ${path}`);
if (path.endsWith('/mojo_bindings_lite.js')) {
throw new Error('Do not load mojo_bindings_lite.js explicitly.');
}
if (lite) {
if (! /^\/gen\/.*\.mojom-lite\.js$/.test(path)) {
throw new Error(`Unrecognized resource path: ${path}`);
}
} else {
if (! /^\/gen\/.*\.mojom\.js$/.test(path)) {
throw new Error(`Unrecognized resource path: ${path}`);
}
}
}

await loadScript(genPrefix + '/gen/layout_test_data/mojo/public/js/mojo_bindings.js');
mojo.config.autoLoadMojomDeps = false;
if (lite) {
await loadScript(genPrefix + '/gen/layout_test_data/mojo/public/js/mojo_bindings_lite.js');
} else {
await loadScript(genPrefix + '/gen/layout_test_data/mojo/public/js/mojo_bindings.js');
mojo.config.autoLoadMojomDeps = false;
}

for (const path of resources) {
await loadScript(genPrefix + path);
Expand Down

0 comments on commit 1d14e82

Please sign in to comment.