Commit 3f9ff2a8 authored by Ovidio Henriquez's avatar Ovidio Henriquez Committed by Commit Bot

bluetooth: async/await for test helper methods

This change updates the bluetooth-test.js and bluetooth-helpers.js file
to use async/await to improve readability.

Bug: 1070816
Change-Id: I26b3f66f971f73860e6f2e89ef63709b0da6d164
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2160087Reviewed-by: default avatarJames Hollyer <jameshollyer@google.com>
Reviewed-by: default avatarVincent Scheib <scheib@chromium.org>
Commit-Queue: Ovidio de Jesús Ruiz-Henríquez <odejesush@chromium.org>
Cr-Commit-Position: refs/heads/master@{#763374}
parent 7af054d3
......@@ -24,12 +24,11 @@ function loadScript(path) {
* @returns {Promise<void>} A promise chain that resolves when all scripts have
* finished loading.
*/
function loadScripts(paths) {
let chain = Promise.resolve();
async function loadScripts(paths) {
for (let path of paths) {
chain = chain.then(() => loadScript(path));
await loadScript(path);
}
return chain;
return;
}
/**
......@@ -42,7 +41,7 @@ function loadScripts(paths) {
* Bluetooth Blink Web Tests have been migrated into this repository.
* @returns {Promise<void>} Resolves when Chromium specific setup is complete.
*/
function performChromiumSetup() {
async function performChromiumSetup() {
// Make sure we are actually on Chromium with Mojo enabled.
if (typeof Mojo === 'undefined') {
return;
......@@ -65,7 +64,7 @@ function performChromiumSetup() {
'/js-test-resources/bluetooth/bluetooth-fake-adapter.js',
];
}
return loadScripts([
await loadScripts([
`${genPrefix}/layout_test_data/mojo/public/js/mojo_bindings.js`,
`${genPrefix}/content/test/data/mojo_web_test_helper_test.mojom.js`,
`${genPrefix}/device/bluetooth/public/mojom/uuid.mojom.js`,
......@@ -73,19 +72,18 @@ function performChromiumSetup() {
`${genPrefix}/device/bluetooth/public/mojom/test/fake_bluetooth.mojom.js`,
`${genPrefix}/content/shell/common/web_test/fake_bluetooth_chooser.mojom.js`,
`${prefix}/web-bluetooth-test.js`,
].concat(extra))
// Call setBluetoothFakeAdapter() to clean up any fake adapters left over
// by legacy tests.
// Legacy tests that use setBluetoothFakeAdapter() sometimes fail to clean
// their fake adapter. This is not a problem for these tests because the
// next setBluetoothFakeAdapter() will clean it up anyway but it is a
// problem for the new tests that do not use setBluetoothFakeAdapter().
// TODO(https://crbug.com/569709): Remove once setBluetoothFakeAdapter is
// no longer used.
.then(
() => typeof setBluetoothFakeAdapter === 'undefined' ?
undefined :
setBluetoothFakeAdapter(''));
].concat(extra));
// Call setBluetoothFakeAdapter() to clean up any fake adapters left over by
// legacy tests. Legacy tests that use setBluetoothFakeAdapter() sometimes
// fail to clean their fake adapter. This is not a problem for these tests
// because the next setBluetoothFakeAdapter() will clean it up anyway but it
// is a problem for the new tests that do not use setBluetoothFakeAdapter().
// TODO(https://crbug.com/569709): Remove once setBluetoothFakeAdapter is no
// longer used.
if (typeof setBluetoothFakeAdapter !== 'undefined') {
setBluetoothFakeAdapter('');
}
}
/**
......@@ -99,16 +97,13 @@ function performChromiumSetup() {
* rejects if the test failed.
*/
function bluetooth_test(test_function, name, properties) {
Promise.resolve().then(
() => promise_test(
t => Promise
.resolve()
return promise_test(async (t) => {
// Trigger Chromium-specific setup.
.then(performChromiumSetup)
.then(() => test_function(t))
.then(() => navigator.bluetooth.test.allResponsesConsumed())
.then(consumed => assert_true(consumed)),
name, properties));
await performChromiumSetup();
await test_function(t);
let consumed = await navigator.bluetooth.test.allResponsesConsumed();
assert_true(consumed);
}, name, properties);
}
/**
......@@ -138,12 +133,11 @@ function waitForDocumentReady() {
* @returns {Promise<*>} Resolves when the user activation has been simulated
* with the result of |callback|.
*/
function callWithTrustedClick(callback) {
return waitForDocumentReady().then(() => new Promise(resolve => {
let button =
document.createElement('button');
button.textContent =
'click to continue test';
async function callWithTrustedClick(callback) {
await waitForDocumentReady();
return new Promise(resolve => {
let button = document.createElement('button');
button.textContent = 'click to continue test';
button.style.display = 'block';
button.style.fontSize = '20px';
button.style.padding = '10px';
......@@ -153,7 +147,7 @@ function callWithTrustedClick(callback) {
};
document.body.appendChild(button);
test_driver.click(button);
}));
});
}
/**
......@@ -356,7 +350,7 @@ function assert_promise_resolves_after_event(
* @returns {Promise<void>} Resolves if no events were fired.
*/
function assert_no_events(object, event_name) {
return new Promise((resolve, reject) => {
return new Promise((resolve) => {
let event_listener = (e) => {
object.removeEventListener(event_name, event_listener);
assert_unreached('Object should not fire an event.');
......
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