Commit 652d8f66 authored by Ken Rockot's avatar Ken Rockot Committed by Chromium LUCI CQ

Migrate WebBluetooth WPT to Mojo JS modules

Bug: 1004256
Change-Id: I3b2b26128d0dce591fefe323a730b827e160bb70
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2618679
Commit-Queue: Ken Rockot <rockot@google.com>
Reviewed-by: default avatarMichael Moss <mmoss@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#842698}
parent a83fbf91
...@@ -459,27 +459,22 @@ FILES = [ ...@@ -459,27 +459,22 @@ FILES = [
}, },
# MojoJS support data (WebBluetooth): # MojoJS support data (WebBluetooth):
{ {
'filename': 'gen/content/test/data/mojo_web_test_helper_test.mojom.js', 'filename': 'gen/device/bluetooth/public/mojom/uuid.mojom.m.js',
'buildtype': ['dev', 'official'], 'buildtype': ['dev', 'official'],
'archive': 'mojojs.zip', 'archive': 'mojojs.zip',
}, },
{ {
'filename': 'gen/device/bluetooth/public/mojom/uuid.mojom.js', 'filename': 'gen/url/mojom/origin.mojom.m.js',
'buildtype': ['dev', 'official'], 'buildtype': ['dev', 'official'],
'archive': 'mojojs.zip', 'archive': 'mojojs.zip',
}, },
{ {
'filename': 'gen/url/mojom/origin.mojom.js', 'filename': 'gen/device/bluetooth/public/mojom/test/fake_bluetooth.mojom.m.js',
'buildtype': ['dev', 'official'], 'buildtype': ['dev', 'official'],
'archive': 'mojojs.zip', 'archive': 'mojojs.zip',
}, },
{ {
'filename': 'gen/device/bluetooth/public/mojom/test/fake_bluetooth.mojom.js', 'filename': 'gen/content/web_test/common/fake_bluetooth_chooser.mojom.m.js',
'buildtype': ['dev', 'official'],
'archive': 'mojojs.zip',
},
{
'filename': 'gen/content/web_test/common/fake_bluetooth_chooser.mojom.js',
'buildtype': ['dev', 'official'], 'buildtype': ['dev', 'official'],
'archive': 'mojojs.zip', 'archive': 'mojojs.zip',
}, },
......
...@@ -29,19 +29,13 @@ function loadScript(path) { ...@@ -29,19 +29,13 @@ function loadScript(path) {
* @returns {Promise<void>} Resolves when Chromium specific setup is complete. * @returns {Promise<void>} Resolves when Chromium specific setup is complete.
*/ */
async function performChromiumSetup() { async function performChromiumSetup() {
const chromiumResources = [
'/gen/content/test/data/mojo_web_test_helper_test.mojom.js',
'/gen/device/bluetooth/public/mojom/uuid.mojom.js',
'/gen/url/mojom/origin.mojom.js',
'/gen/device/bluetooth/public/mojom/test/fake_bluetooth.mojom.js',
'/gen/content/web_test/common/fake_bluetooth_chooser.mojom.js',
];
// Determine path prefixes. // Determine path prefixes.
let resPrefix = '/resources'; let resPrefix = '/resources';
const extra = ['/resources/chromium/web-bluetooth-test.js']; const chromiumResources = ['/resources/chromium/web-bluetooth-test.js'];
const pathname = window.location.pathname; const pathname = window.location.pathname;
if (pathname.includes('/wpt_internal/')) { if (pathname.includes('/wpt_internal/')) {
extra.push('/wpt_internal/bluetooth/resources/bluetooth-fake-adapter.js'); chromiumResources.push(
'/wpt_internal/bluetooth/resources/bluetooth-fake-adapter.js');
} }
await loadScript(`${resPrefix}/test-only-api.js`); await loadScript(`${resPrefix}/test-only-api.js`);
...@@ -49,11 +43,12 @@ async function performChromiumSetup() { ...@@ -49,11 +43,12 @@ async function performChromiumSetup() {
return; return;
} }
await loadMojoResources(chromiumResources); for (const path of chromiumResources) {
for (const path of extra) {
await loadScript(path); await loadScript(path);
} }
await initializeChromiumResources();
// Call setBluetoothFakeAdapter() to clean up any fake adapters left over by // Call setBluetoothFakeAdapter() to clean up any fake adapters left over by
// legacy tests. Legacy tests that use setBluetoothFakeAdapter() sometimes // legacy tests. Legacy tests that use setBluetoothFakeAdapter() sometimes
// fail to clean their fake adapter. This is not a problem for these tests // fail to clean their fake adapter. This is not a problem for these tests
......
...@@ -716,13 +716,13 @@ WEB-PLATFORM.TEST:web-bundle/subresource-loading/*.html ...@@ -716,13 +716,13 @@ WEB-PLATFORM.TEST:web-bundle/subresource-loading/*.html
# Tests that depend on resources in /gen/ in Chromium: # Tests that depend on resources in /gen/ in Chromium:
# https://github.com/web-platform-tests/wpt/issues/16455 # https://github.com/web-platform-tests/wpt/issues/16455
# Please consult with ecosystem-infra@chromium.org before adding more. # Please consult with ecosystem-infra@chromium.org before adding more.
MISSING DEPENDENCY: bluetooth/resources/bluetooth-test.js
MISSING DEPENDENCY: contacts/resources/helpers.js MISSING DEPENDENCY: contacts/resources/helpers.js
MISSING DEPENDENCY: credential-management/support/otpcredential-helper.js MISSING DEPENDENCY: credential-management/support/otpcredential-helper.js
MISSING DEPENDENCY: generic-sensor/resources/generic-sensor-helpers.js MISSING DEPENDENCY: generic-sensor/resources/generic-sensor-helpers.js
MISSING DEPENDENCY: idle-detection/resources/idle-detection-helper.js MISSING DEPENDENCY: idle-detection/resources/idle-detection-helper.js
MISSING DEPENDENCY: mediacapture-image/resources/imagecapture-helpers.js MISSING DEPENDENCY: mediacapture-image/resources/imagecapture-helpers.js
MISSING DEPENDENCY: orientation-event/resources/orientation-event-helpers.js MISSING DEPENDENCY: orientation-event/resources/orientation-event-helpers.js
MISSING DEPENDENCY: resources/chromium/web-bluetooth-test.js
MISSING DEPENDENCY: resources/chromium/webusb-test.js MISSING DEPENDENCY: resources/chromium/webusb-test.js
MISSING DEPENDENCY: resources/test-only-api.js MISSING DEPENDENCY: resources/test-only-api.js
MISSING DEPENDENCY: screen_enumeration/resources/screenenumeration-helpers.js MISSING DEPENDENCY: screen_enumeration/resources/screenenumeration-helpers.js
......
'use strict'; 'use strict';
const content = {};
const bluetooth = {};
const MOJO_CHOOSER_EVENT_TYPE_MAP = {};
function toMojoCentralState(state) { function toMojoCentralState(state) {
switch (state) { switch (state) {
case 'absent': case 'absent':
...@@ -54,58 +58,16 @@ function convertToMojoMap(record, keyFn, isNumberKey = false) { ...@@ -54,58 +58,16 @@ function convertToMojoMap(record, keyFn, isNumberKey = false) {
return map; return map;
} }
// Mapping of the property names of
// BluetoothCharacteristicProperties defined in
// https://webbluetoothcg.github.io/web-bluetooth/#characteristicproperties
// to property names of the CharacteristicProperties mojo struct.
const CHARACTERISTIC_PROPERTIES_WEB_TO_MOJO = {
broadcast: 'broadcast',
read: 'read',
write_without_response: 'write_without_response',
write: 'write',
notify: 'notify',
indicate: 'indicate',
authenticatedSignedWrites: 'authenticated_signed_writes',
extended_properties: 'extended_properties',
};
// Mapping of the Mojo ChooserEventType enum to a string.
const MOJO_CHOOSER_EVENT_TYPE_MAP = (() => {
const ChooserEventType = content.mojom.ChooserEventType;
return {
[ChooserEventType.CHOOSER_OPENED]: 'chooser-opened',
[ChooserEventType.CHOOSER_CLOSED]: 'chooser-closed',
[ChooserEventType.ADAPTER_REMOVED]: 'adapter-removed',
[ChooserEventType.ADAPTER_DISABLED]: 'adapter-disabled',
[ChooserEventType.ADAPTER_ENABLED]: 'adapter-enabled',
[ChooserEventType.DISCOVERY_FAILED_TO_START]: 'discovery-failed-to-start',
[ChooserEventType.DISCOVERING]: 'discovering',
[ChooserEventType.DISCOVERY_IDLE]: 'discovery-idle',
[ChooserEventType.ADD_OR_UPDATE_DEVICE]: 'add-or-update-device',
}
})();
function ArrayToMojoCharacteristicProperties(arr) { function ArrayToMojoCharacteristicProperties(arr) {
let struct = new bluetooth.mojom.CharacteristicProperties(); const struct = {};
arr.forEach(property => { struct[property] = true; });
arr.forEach(val => {
let mojo_property =
CHARACTERISTIC_PROPERTIES_WEB_TO_MOJO[val];
if (struct.hasOwnProperty(mojo_property))
struct[mojo_property] = true;
else
throw `Invalid member '${val}' for CharacteristicProperties`;
});
return struct; return struct;
} }
class FakeBluetooth { class FakeBluetooth {
constructor() { constructor() {
this.fake_bluetooth_ptr_ = new bluetooth.mojom.FakeBluetoothPtr(); this.fake_bluetooth_ptr_ = new bluetooth.mojom.FakeBluetoothRemote();
Mojo.bindInterface(bluetooth.mojom.FakeBluetooth.name, this.fake_bluetooth_ptr_.$.bindNewPipeAndPassReceiver().bindInBrowser('process');
mojo.makeRequest(this.fake_bluetooth_ptr_).handle, 'process');
this.fake_central_ = null; this.fake_central_ = null;
} }
...@@ -232,7 +194,7 @@ class FakeCentral { ...@@ -232,7 +194,7 @@ class FakeCentral {
} }
await this.fake_central_ptr_.simulateAdvertisementReceived( await this.fake_central_ptr_.simulateAdvertisementReceived(
new bluetooth.mojom.ScanResult(clonedScanResult)); clonedScanResult);
return this.fetchOrCreatePeripheral_(clonedScanResult.deviceAddress); return this.fetchOrCreatePeripheral_(clonedScanResult.deviceAddress);
} }
...@@ -572,21 +534,17 @@ class FakeRemoteGATTDescriptor { ...@@ -572,21 +534,17 @@ class FakeRemoteGATTDescriptor {
// and records the events produced by the Bluetooth chooser. // and records the events produced by the Bluetooth chooser.
class FakeChooser { class FakeChooser {
constructor() { constructor() {
let fakeBluetoothChooserFactoryPtr = let fakeBluetoothChooserFactoryRemote =
new content.mojom.FakeBluetoothChooserFactoryPtr(); new content.mojom.FakeBluetoothChooserFactoryRemote();
Mojo.bindInterface(content.mojom.FakeBluetoothChooserFactory.name, fakeBluetoothChooserFactoryRemote.$.bindNewPipeAndPassReceiver().bindInBrowser('process');
mojo.makeRequest(fakeBluetoothChooserFactoryPtr).handle, 'process');
this.fake_bluetooth_chooser_ptr_ = this.fake_bluetooth_chooser_ptr_ =
new content.mojom.FakeBluetoothChooserPtr(); new content.mojom.FakeBluetoothChooserRemote();
this.fake_bluetooth_chooser_client_receiver_ =
let clientPtrInfo = new mojo.AssociatedInterfacePtrInfo(); new content.mojom.FakeBluetoothChooserClientReceiver(this);
this.fake_bluetooth_chooser_client_binding_ = fakeBluetoothChooserFactoryRemote.createFakeBluetoothChooser(
new mojo.AssociatedBinding(content.mojom.FakeBluetoothChooserClient, this.fake_bluetooth_chooser_ptr_.$.bindNewPipeAndPassReceiver(),
this, mojo.makeRequest(clientPtrInfo)); this.fake_bluetooth_chooser_client_receiver_.$.associateAndPassRemote());
fakeBluetoothChooserFactoryPtr.createFakeBluetoothChooser(
mojo.makeRequest(this.fake_bluetooth_chooser_ptr_), clientPtrInfo);
this.events_ = new Array(); this.events_ = new Array();
this.event_listener_ = null; this.event_listener_ = null;
...@@ -639,12 +597,31 @@ class FakeChooser { ...@@ -639,12 +597,31 @@ class FakeChooser {
} }
} }
// If this line fails, it means that current environment does not support the async function initializeChromiumResources() {
// Web Bluetooth Test API. content.mojom = await import(
try { '/gen/content/web_test/common/fake_bluetooth_chooser.mojom.m.js');
navigator.bluetooth.test = new FakeBluetooth(); bluetooth.mojom = await import(
} catch { '/gen/device/bluetooth/public/mojom/test/fake_bluetooth.mojom.m.js');
const map = MOJO_CHOOSER_EVENT_TYPE_MAP;
const types = content.mojom.ChooserEventType;
map[types.CHOOSER_OPENED] = 'chooser-opened';
map[types.CHOOSER_CLOSED] = 'chooser-closed';
map[types.ADAPTER_REMOVED] = 'adapter-removed';
map[types.ADAPTER_DISABLED] = 'adapter-disabled';
map[types.ADAPTER_ENABLED] = 'adapter-enabled';
map[types.DISCOVERY_FAILED_TO_START] = 'discovery-failed-to-start';
map[types.DISCOVERING] = 'discovering';
map[types.DISCOVERY_IDLE] = 'discovery-idle';
map[types.ADD_OR_UPDATE_DEVICE] = 'add-or-update-device';
// If this line fails, it means that current environment does not support the
// Web Bluetooth Test API.
try {
navigator.bluetooth.test = new FakeBluetooth();
} catch {
throw 'Web Bluetooth Test API is not implemented on this ' + throw 'Web Bluetooth Test API is not implemented on this ' +
'environment. See the bluetooth README at ' + 'environment. See the bluetooth README at ' +
'https://github.com/web-platform-tests/wpt/blob/master/bluetooth/README.md#web-bluetooth-testing'; 'https://github.com/web-platform-tests/wpt/blob/master/bluetooth/README.md#web-bluetooth-testing';
}
} }
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