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 = [
},
# 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'],
'archive': 'mojojs.zip',
},
{
'filename': 'gen/device/bluetooth/public/mojom/uuid.mojom.js',
'filename': 'gen/url/mojom/origin.mojom.m.js',
'buildtype': ['dev', 'official'],
'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'],
'archive': 'mojojs.zip',
},
{
'filename': 'gen/device/bluetooth/public/mojom/test/fake_bluetooth.mojom.js',
'buildtype': ['dev', 'official'],
'archive': 'mojojs.zip',
},
{
'filename': 'gen/content/web_test/common/fake_bluetooth_chooser.mojom.js',
'filename': 'gen/content/web_test/common/fake_bluetooth_chooser.mojom.m.js',
'buildtype': ['dev', 'official'],
'archive': 'mojojs.zip',
},
......
......@@ -29,19 +29,13 @@ function loadScript(path) {
* @returns {Promise<void>} Resolves when Chromium specific setup is complete.
*/
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.
let resPrefix = '/resources';
const extra = ['/resources/chromium/web-bluetooth-test.js'];
const chromiumResources = ['/resources/chromium/web-bluetooth-test.js'];
const pathname = window.location.pathname;
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`);
......@@ -49,11 +43,12 @@ async function performChromiumSetup() {
return;
}
await loadMojoResources(chromiumResources);
for (const path of extra) {
for (const path of chromiumResources) {
await loadScript(path);
}
await initializeChromiumResources();
// 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
......
......@@ -716,13 +716,13 @@ WEB-PLATFORM.TEST:web-bundle/subresource-loading/*.html
# Tests that depend on resources in /gen/ in Chromium:
# https://github.com/web-platform-tests/wpt/issues/16455
# 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: credential-management/support/otpcredential-helper.js
MISSING DEPENDENCY: generic-sensor/resources/generic-sensor-helpers.js
MISSING DEPENDENCY: idle-detection/resources/idle-detection-helper.js
MISSING DEPENDENCY: mediacapture-image/resources/imagecapture-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/test-only-api.js
MISSING DEPENDENCY: screen_enumeration/resources/screenenumeration-helpers.js
......
'use strict';
const content = {};
const bluetooth = {};
const MOJO_CHOOSER_EVENT_TYPE_MAP = {};
function toMojoCentralState(state) {
switch (state) {
case 'absent':
......@@ -54,58 +58,16 @@ function convertToMojoMap(record, keyFn, isNumberKey = false) {
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) {
let struct = new bluetooth.mojom.CharacteristicProperties();
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`;
});
const struct = {};
arr.forEach(property => { struct[property] = true; });
return struct;
}
class FakeBluetooth {
constructor() {
this.fake_bluetooth_ptr_ = new bluetooth.mojom.FakeBluetoothPtr();
Mojo.bindInterface(bluetooth.mojom.FakeBluetooth.name,
mojo.makeRequest(this.fake_bluetooth_ptr_).handle, 'process');
this.fake_bluetooth_ptr_ = new bluetooth.mojom.FakeBluetoothRemote();
this.fake_bluetooth_ptr_.$.bindNewPipeAndPassReceiver().bindInBrowser('process');
this.fake_central_ = null;
}
......@@ -232,7 +194,7 @@ class FakeCentral {
}
await this.fake_central_ptr_.simulateAdvertisementReceived(
new bluetooth.mojom.ScanResult(clonedScanResult));
clonedScanResult);
return this.fetchOrCreatePeripheral_(clonedScanResult.deviceAddress);
}
......@@ -572,21 +534,17 @@ class FakeRemoteGATTDescriptor {
// and records the events produced by the Bluetooth chooser.
class FakeChooser {
constructor() {
let fakeBluetoothChooserFactoryPtr =
new content.mojom.FakeBluetoothChooserFactoryPtr();
Mojo.bindInterface(content.mojom.FakeBluetoothChooserFactory.name,
mojo.makeRequest(fakeBluetoothChooserFactoryPtr).handle, 'process');
let fakeBluetoothChooserFactoryRemote =
new content.mojom.FakeBluetoothChooserFactoryRemote();
fakeBluetoothChooserFactoryRemote.$.bindNewPipeAndPassReceiver().bindInBrowser('process');
this.fake_bluetooth_chooser_ptr_ =
new content.mojom.FakeBluetoothChooserPtr();
let clientPtrInfo = new mojo.AssociatedInterfacePtrInfo();
this.fake_bluetooth_chooser_client_binding_ =
new mojo.AssociatedBinding(content.mojom.FakeBluetoothChooserClient,
this, mojo.makeRequest(clientPtrInfo));
fakeBluetoothChooserFactoryPtr.createFakeBluetoothChooser(
mojo.makeRequest(this.fake_bluetooth_chooser_ptr_), clientPtrInfo);
new content.mojom.FakeBluetoothChooserRemote();
this.fake_bluetooth_chooser_client_receiver_ =
new content.mojom.FakeBluetoothChooserClientReceiver(this);
fakeBluetoothChooserFactoryRemote.createFakeBluetoothChooser(
this.fake_bluetooth_chooser_ptr_.$.bindNewPipeAndPassReceiver(),
this.fake_bluetooth_chooser_client_receiver_.$.associateAndPassRemote());
this.events_ = new Array();
this.event_listener_ = null;
......@@ -639,12 +597,31 @@ class FakeChooser {
}
}
// If this line fails, it means that current environment does not support the
// Web Bluetooth Test API.
try {
async function initializeChromiumResources() {
content.mojom = await import(
'/gen/content/web_test/common/fake_bluetooth_chooser.mojom.m.js');
bluetooth.mojom = await import(
'/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 {
} catch {
throw 'Web Bluetooth Test API is not implemented on this ' +
'environment. See the bluetooth README at ' +
'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