Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WPT] Refactor credential-management to use test-only-api.js #25538

Merged
merged 1 commit into from Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion credential-management/otpcredential-get-basics.https.html
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
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
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
@@ -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
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