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