How to use initializeFakeCentral method in wpt

Best JavaScript code snippet using wpt

bluetooth-fake-devices.js

Source:bluetooth-fake-devices.js Github

copy

Full Screen

...187 ]);188}189/** @type {FakeCentral} The fake adapter for the current test. */190let fake_central = null;191async function initializeFakeCentral({state = 'powered-on'}) {192 if (!fake_central) {193 fake_central = await navigator.bluetooth.test.simulateCentral({state});194 }195}196/**197 * A dictionary for specifying fake Bluetooth device setup options.198 * @typedef {{address: !string, name: !string,199 * knownServiceUUIDs: !Array<string>, connectable: !boolean,200 * serviceDiscoveryComplete: !boolean}}201 */202let FakeDeviceOptions;203/**204 * @typedef {{fakeDeviceOptions: FakeDeviceOptions,205 * requestDeviceOptions: RequestDeviceOptions}}206 */207let SetupOptions;208/**209 * Default options for setting up a Bluetooth device.210 * @type {FakeDeviceOptions}211 */212const fakeDeviceOptionsDefault = {213 address: '00:00:00:00:00:00',214 name: 'LE Device',215 knownServiceUUIDs: [],216 connectable: false,217 serviceDiscoveryComplete: false,218};219/**220 * A dictionary containing the fake Bluetooth device object. The dictionary can221 * optionally contain its fake services and its BluetoothDevice counterpart.222 * @typedef {{fake_peripheral: !FakePeripheral,223 * fake_services: Object<string, FakeService>,224 * device: BluetoothDevice}}225 */226let FakeDevice;227/**228 * Creates a SetupOptions object using |setupOptionsDefault| as the base options229 * object with the options from |setupOptionsOverride| overriding these230 * defaults.231 * @param {SetupOptions} setupOptionsDefault The default options object to use232 * as the base.233 * @param {SetupOptions} setupOptionsOverride The options to override the234 * defaults with.235 * @returns {SetupOptions} The merged setup options containing the defaults with236 * the overrides applied.237 */238function createSetupOptions(setupOptionsDefault, setupOptionsOverride) {239 // Merge the properties of |setupOptionsDefault| and |setupOptionsOverride|240 // without modifying |setupOptionsDefault|.241 let fakeDeviceOptions = Object.assign(242 {...setupOptionsDefault.fakeDeviceOptions},243 setupOptionsOverride.fakeDeviceOptions);244 let requestDeviceOptions = Object.assign(245 {...setupOptionsDefault.requestDeviceOptions},246 setupOptionsOverride.requestDeviceOptions);247 return {fakeDeviceOptions, requestDeviceOptions};248}249/**250 * Adds a preconnected device with the given options. A preconnected device is a251 * device that has been paired with the system previously. This can be done if,252 * for example, the user pairs the device using the OS'es settings.253 *254 * By default, the preconnected device will be set up using the255 * |fakeDeviceOptionsDefault| and will not use a RequestDeviceOption object.256 * This means that the device will not be requested during the setup.257 *258 * If |setupOptionsOverride| is provided, these options will override the259 * defaults. If |setupOptionsOverride| includes the requestDeviceOptions260 * property, then the device will be requested using those options.261 * @param {SetupOptions} setupOptionsOverride An object containing options for262 * setting up a fake Bluetooth device and for requesting the device.263 * @returns {Promise<FakeDevice>} The device fake initialized with the264 * parameter values.265 */266async function setUpPreconnectedFakeDevice(setupOptionsOverride) {267 await initializeFakeCentral({state: 'powered-on'});268 let setupOptions = createSetupOptions(269 {fakeDeviceOptions: fakeDeviceOptionsDefault}, setupOptionsOverride);270 // Simulate the fake peripheral.271 let preconnectedDevice = {};272 preconnectedDevice.fake_peripheral =273 await fake_central.simulatePreconnectedPeripheral({274 address: setupOptions.fakeDeviceOptions.address,275 name: setupOptions.fakeDeviceOptions.name,276 knownServiceUUIDs: setupOptions.fakeDeviceOptions.knownServiceUUIDs,277 });278 if (setupOptions.fakeDeviceOptions.connectable) {279 await preconnectedDevice.fake_peripheral.setNextGATTConnectionResponse(280 {code: HCI_SUCCESS});281 }282 // Add known services.283 preconnectedDevice.fake_services = new Map();284 for (let service of setupOptions.fakeDeviceOptions.knownServiceUUIDs) {285 let fake_service = await preconnectedDevice.fake_peripheral.addFakeService(286 {uuid: service});287 preconnectedDevice.fake_services.set(service, fake_service);288 }289 // Request the device if options have been provided.290 if (setupOptions.requestDeviceOptions) {291 preconnectedDevice.device =292 await requestDeviceWithTrustedClick(setupOptions.requestDeviceOptions);293 }294 // Set up services discovered state.295 if (setupOptions.fakeDeviceOptions.serviceDiscoveryComplete) {296 await preconnectedDevice.fake_peripheral.setNextGATTDiscoveryResponse(297 {code: HCI_SUCCESS});298 }299 return preconnectedDevice;300}301/**302 * Deprecated: Use setUpPreconnectedFakeDevice() instead.303 * Simulates a preconnected device with |address|, |name| and304 * |knownServiceUUIDs|. A preconnected device is a device that has been paired305 * with the system previously. This can be done if, for example, the user pairs306 * the device using the OS'es settings.307 * TODO(https://crbug.com/1070816): Remove this method when all uses have been308 * converted to using setUpPreconnectedFakeDevice();309 * @param {string} address The device MAC address.310 * @param {string} name The device name.311 * @param {Array<string>} knownServiceUUIDs An array of GATT service UUIDs to312 * set up the fake with.313 * @returns {Promise<FakePeripheral>} The fake devices are initialized with the314 * parameter values.315 */316async function setUpPreconnectedDevice({317 address = '00:00:00:00:00:00',318 name = 'LE Device',319 knownServiceUUIDs = []320}) {321 await initializeFakeCentral({state: 'powered-on'});322 return await fake_central.simulatePreconnectedPeripheral({323 address: address,324 name: name,325 knownServiceUUIDs: knownServiceUUIDs,326 });327}328/** Blocklisted GATT Device Helper Methods */329/** @type {FakeDeviceOptions} */330const blocklistFakeDeviceOptionsDefault = {331 address: '11:11:11:11:11:11',332 name: 'Blocklist Device',333 knownServiceUUIDs: ['generic_access', blocklist_test_service_uuid],334 connectable: true,335 serviceDiscoveryComplete: true336};337/** @type {RequestDeviceOptions} */338const blocklistRequestDeviceOptionsDefault = {339 filters: [{services: [blocklist_test_service_uuid]}]340};341/** @type {SetupOptions} */342const blocklistSetupOptionsDefault = {343 fakeDeviceOptions: blocklistFakeDeviceOptionsDefault,344 requestDeviceOptions: blocklistRequestDeviceOptionsDefault345};346/**347 * Returns an object containing a BluetoothDevice discovered using |options|,348 * its corresponding FakePeripheral and FakeRemoteGATTServices.349 * The simulated device is called 'Blocklist Device' and it has one known350 * service UUID |blocklist_test_service_uuid|. The |blocklist_test_service_uuid|351 * service contains two characteristics:352 * - |blocklist_exclude_reads_characteristic_uuid| (read, write)353 * - 'gap.peripheral_privacy_flag' (read, write)354 * The 'gap.peripheral_privacy_flag' characteristic contains three descriptors:355 * - |blocklist_test_descriptor_uuid|356 * - |blocklist_exclude_reads_descriptor_uuid|357 * - 'gatt.client_characteristic_configuration'358 * These are special UUIDs that have been added to the blocklist found at359 * https://github.com/WebBluetoothCG/registries/blob/master/gatt_blocklist.txt360 * There are also test UUIDs that have been added to the test environment which361 * other implementations should add as test UUIDs as well.362 * The device has been connected to and its attributes are ready to be363 * discovered.364 * @returns {Promise<{device: BluetoothDevice, fake_peripheral: FakePeripheral,365 * fake_blocklist_test_service: FakeRemoteGATTService,366 * fake_blocklist_exclude_reads_characteristic:367 * FakeRemoteGATTCharacteristic,368 * fake_blocklist_exclude_writes_characteristic:369 * FakeRemoteGATTCharacteristic,370 * fake_blocklist_descriptor: FakeRemoteGATTDescriptor,371 * fake_blocklist_exclude_reads_descriptor: FakeRemoteGATTDescriptor,372 * fake_blocklist_exclude_writes_descriptor: FakeRemoteGATTDescriptor}>} An373 * object containing the BluetoothDevice object and its corresponding374 * GATT fake objects.375 */376async function getBlocklistDevice(setupOptionsOverride = {}) {377 let setupOptions =378 createSetupOptions(blocklistSetupOptionsDefault, setupOptionsOverride);379 let fakeDevice = await setUpPreconnectedFakeDevice(setupOptions);380 await fakeDevice.device.gatt.connect();381 let fake_blocklist_test_service =382 fakeDevice.fake_services.get(blocklist_test_service_uuid);383 let fake_blocklist_exclude_reads_characteristic =384 await fake_blocklist_test_service.addFakeCharacteristic({385 uuid: blocklist_exclude_reads_characteristic_uuid,386 properties: ['read', 'write'],387 });388 let fake_blocklist_exclude_writes_characteristic =389 await fake_blocklist_test_service.addFakeCharacteristic({390 uuid: 'gap.peripheral_privacy_flag',391 properties: ['read', 'write'],392 });393 let fake_blocklist_descriptor =394 await fake_blocklist_exclude_writes_characteristic.addFakeDescriptor(395 {uuid: blocklist_test_descriptor_uuid});396 let fake_blocklist_exclude_reads_descriptor =397 await fake_blocklist_exclude_writes_characteristic.addFakeDescriptor(398 {uuid: blocklist_exclude_reads_descriptor_uuid});399 let fake_blocklist_exclude_writes_descriptor =400 await fake_blocklist_exclude_writes_characteristic.addFakeDescriptor(401 {uuid: 'gatt.client_characteristic_configuration'});402 return {403 device: fakeDevice.device,404 fake_peripheral: fakeDevice.fake_peripheral,405 fake_blocklist_test_service,406 fake_blocklist_exclude_reads_characteristic,407 fake_blocklist_exclude_writes_characteristic,408 fake_blocklist_descriptor,409 fake_blocklist_exclude_reads_descriptor,410 fake_blocklist_exclude_writes_descriptor,411 };412}413/**414 * Returns an object containing a Blocklist Test BluetoothRemoteGattService and415 * its corresponding FakeRemoteGATTService.416 * @returns {Promise<{device: BluetoothDevice, fake_peripheral: FakePeripheral,417 * fake_blocklist_test_service: FakeRemoteGATTService,418 * fake_blocklist_exclude_reads_characteristic:419 * FakeRemoteGATTCharacteristic,420 * fake_blocklist_exclude_writes_characteristic:421 * FakeRemoteGATTCharacteristic,422 * fake_blocklist_descriptor: FakeRemoteGATTDescriptor,423 * fake_blocklist_exclude_reads_descriptor: FakeRemoteGATTDescriptor,424 * fake_blocklist_exclude_writes_descriptor: FakeRemoteGATTDescriptor,425 * service: BluetoothRemoteGATTService,426 * fake_service: FakeBluetoothRemoteGATTService}>} An object containing the427 * BluetoothDevice object and its corresponding GATT fake objects.428 */429async function getBlocklistTestService() {430 let result = await getBlocklistDevice();431 let service =432 await result.device.gatt.getPrimaryService(blocklist_test_service_uuid);433 return Object.assign(result, {434 service,435 fake_service: result.fake_blocklist_test_service,436 });437}438/**439 * Returns an object containing a blocklisted BluetoothRemoteGATTCharacteristic440 * that excludes reads and its corresponding FakeRemoteGATTCharacteristic.441 * @returns {Promise<{device: BluetoothDevice, fake_peripheral: FakePeripheral,442 * fake_blocklist_test_service: FakeRemoteGATTService,443 * fake_blocklist_exclude_reads_characteristic:444 * FakeRemoteGATTCharacteristic,445 * fake_blocklist_exclude_writes_characteristic:446 * FakeRemoteGATTCharacteristic,447 * fake_blocklist_descriptor: FakeRemoteGATTDescriptor,448 * fake_blocklist_exclude_reads_descriptor: FakeRemoteGATTDescriptor,449 * fake_blocklist_exclude_writes_descriptor: FakeRemoteGATTDescriptor,450 * service: BluetoothRemoteGATTService,451 * fake_service: FakeBluetoothRemoteGATTService,452 * characteristic: BluetoothRemoteGATTCharacteristic,453 * fake_characteristic: FakeBluetoothRemoteGATTCharacteristic}>} An object454 * containing the BluetoothDevice object and its corresponding GATT fake455 * objects.456 */457async function getBlocklistExcludeReadsCharacteristic() {458 let result = await getBlocklistTestService();459 let characteristic = await result.service.getCharacteristic(460 blocklist_exclude_reads_characteristic_uuid);461 return Object.assign(result, {462 characteristic,463 fake_characteristic: result.fake_blocklist_exclude_reads_characteristic464 });465}466/**467 * Returns an object containing a blocklisted BluetoothRemoteGATTCharacteristic468 * that excludes writes and its corresponding FakeRemoteGATTCharacteristic.469 * @returns {Promise<{device: BluetoothDevice, fake_peripheral: FakePeripheral,470 * fake_blocklist_test_service: FakeRemoteGATTService,471 * fake_blocklist_exclude_reads_characteristic:472 * FakeRemoteGATTCharacteristic,473 * fake_blocklist_exclude_writes_characteristic:474 * FakeRemoteGATTCharacteristic,475 * fake_blocklist_descriptor: FakeRemoteGATTDescriptor,476 * fake_blocklist_exclude_reads_descriptor: FakeRemoteGATTDescriptor,477 * fake_blocklist_exclude_writes_descriptor: FakeRemoteGATTDescriptor,478 * service: BluetoothRemoteGATTService,479 * fake_service: FakeBluetoothRemoteGATTService,480 * characteristic: BluetoothRemoteGATTCharacteristic,481 * fake_characteristic: FakeBluetoothRemoteGATTCharacteristic}>} An object482 * containing the BluetoothDevice object and its corresponding GATT fake483 * objects.484 */485async function getBlocklistExcludeWritesCharacteristic() {486 let result = await getBlocklistTestService();487 let characteristic =488 await result.service.getCharacteristic('gap.peripheral_privacy_flag');489 return Object.assign(result, {490 characteristic,491 fake_characteristic: result.fake_blocklist_exclude_writes_characteristic492 });493}494/**495 * Returns an object containing a blocklisted BluetoothRemoteGATTDescriptor that496 * excludes reads and its corresponding FakeRemoteGATTDescriptor.497 * @returns {Promise<{device: BluetoothDevice, fake_peripheral: FakePeripheral,498 * fake_blocklist_test_service: FakeRemoteGATTService,499 * fake_blocklist_exclude_reads_characteristic:500 * FakeRemoteGATTCharacteristic,501 * fake_blocklist_exclude_writes_characteristic:502 * FakeRemoteGATTCharacteristic,503 * fake_blocklist_descriptor: FakeRemoteGATTDescriptor,504 * fake_blocklist_exclude_reads_descriptor: FakeRemoteGATTDescriptor,505 * fake_blocklist_exclude_writes_descriptor: FakeRemoteGATTDescriptor,506 * service: BluetoothRemoteGATTService,507 * fake_service: FakeBluetoothRemoteGATTService,508 * characteristic: BluetoothRemoteGATTCharacteristic,509 * fake_characteristic: FakeBluetoothRemoteGATTCharacteristic,510 * descriptor: BluetoothRemoteGATTDescriptor,511 * fake_descriptor: FakeBluetoothRemoteGATTDescriptor}>} An object512 * containing the BluetoothDevice object and its corresponding GATT fake513 * objects.514 */515async function getBlocklistExcludeReadsDescriptor() {516 let result = await getBlocklistExcludeWritesCharacteristic();517 let descriptor = await result.characteristic.getDescriptor(518 blocklist_exclude_reads_descriptor_uuid);519 return Object.assign(result, {520 descriptor,521 fake_descriptor: result.fake_blocklist_exclude_reads_descriptor522 });523}524/**525 * Returns an object containing a blocklisted BluetoothRemoteGATTDescriptor that526 * excludes writes and its corresponding FakeRemoteGATTDescriptor.527 * @returns {Promise<{device: BluetoothDevice, fake_peripheral: FakePeripheral,528 * fake_blocklist_test_service: FakeRemoteGATTService,529 * fake_blocklist_exclude_reads_characteristic:530 * FakeRemoteGATTCharacteristic,531 * fake_blocklist_exclude_writes_characteristic:532 * FakeRemoteGATTCharacteristic,533 * fake_blocklist_descriptor: FakeRemoteGATTDescriptor,534 * fake_blocklist_exclude_reads_descriptor: FakeRemoteGATTDescriptor,535 * fake_blocklist_exclude_writes_descriptor: FakeRemoteGATTDescriptor,536 * service: BluetoothRemoteGATTService,537 * fake_service: FakeBluetoothRemoteGATTService,538 * characteristic: BluetoothRemoteGATTCharacteristic,539 * fake_characteristic: FakeBluetoothRemoteGATTCharacteristic,540 * descriptor: BluetoothRemoteGATTDescriptor,541 * fake_descriptor: FakeBluetoothRemoteGATTDescriptor}>} An object542 * containing the BluetoothDevice object and its corresponding GATT fake543 * objects.544 */545async function getBlocklistExcludeWritesDescriptor() {546 let result = await getBlocklistExcludeWritesCharacteristic();547 let descriptor = await result.characteristic.getDescriptor(548 'gatt.client_characteristic_configuration');549 return Object.assign(result, {550 descriptor: descriptor,551 fake_descriptor: result.fake_blocklist_exclude_writes_descriptor,552 });553}554/** Bluetooth HID Device Helper Methods */555/** @type {FakeDeviceOptions} */556const connectedHIDFakeDeviceOptionsDefault = {557 address: '10:10:10:10:10:10',558 name: 'HID Device',559 knownServiceUUIDs: [560 'generic_access',561 'device_information',562 'human_interface_device',563 ],564 connectable: true,565 serviceDiscoveryComplete: false566};567/** @type {RequestDeviceOptions} */568const connectedHIDRequestDeviceOptionsDefault = {569 filters: [{services: ['device_information']}],570 optionalServices: ['human_interface_device']571};572/** @type {SetupOptions} */573const connectedHIDSetupOptionsDefault = {574 fakeDeviceOptions: connectedHIDFakeDeviceOptionsDefault,575 requestDeviceOptions: connectedHIDRequestDeviceOptionsDefault576};577/**578 * Similar to getHealthThermometerDevice except the GATT discovery579 * response has not been set yet so more attributes can still be added.580 * TODO(crbug.com/719816): Add descriptors.581 * @param {RequestDeviceOptions} options The options for requesting a Bluetooth582 * Device.583 * @returns {device: BluetoothDevice, fake_peripheral: FakePeripheral} An object584 * containing a requested BluetoothDevice and its fake counter part.585 */586async function getConnectedHIDDevice(587 requestDeviceOptionsOverride, fakeDeviceOptionsOverride) {588 let setupOptions = createSetupOptions(connectedHIDSetupOptionsDefault, {589 fakeDeviceOptions: fakeDeviceOptionsOverride,590 requestDeviceOptions: requestDeviceOptionsOverride591 });592 let fakeDevice = await setUpPreconnectedFakeDevice(setupOptions);593 await fakeDevice.device.gatt.connect();594 // Blocklisted Characteristic:595 // https://github.com/WebBluetoothCG/registries/blob/master/gatt_blocklist.txt596 let dev_info = fakeDevice.fake_services.get('device_information');597 await dev_info.addFakeCharacteristic({598 uuid: 'serial_number_string',599 properties: ['read'],600 });601 return fakeDevice;602}603/**604 * Returns a BluetoothDevice discovered using |options| and its605 * corresponding FakePeripheral.606 * The simulated device is called 'HID Device' it has three known service607 * UUIDs: 'generic_access', 'device_information', 'human_interface_device'.608 * The primary service with 'device_information' UUID has a characteristics609 * with UUID 'serial_number_string'. The device has been connected to and its610 * attributes are ready to be discovered.611 * @param {RequestDeviceOptions} options The options for requesting a Bluetooth612 * Device.613 * @returns {device: BluetoothDevice, fake_peripheral: FakePeripheral} An object614 * containing a requested BluetoothDevice and its fake counter part.615 */616async function getHIDDevice(options) {617 let result =618 await getConnectedHIDDevice(options, {serviceDiscoveryComplete: true});619 return result;620}621/** Health Thermometer Bluetooth Device Helper Methods */622/**623 * Returns a FakePeripheral that corresponds to a simulated pre-connected device624 * called 'Health Thermometer'. The device has two known serviceUUIDs:625 * 'generic_access' and 'health_thermometer'.626 * @returns {Promise<FakePeripheral>} The device fake initialized as a Health627 * Thermometer device.628 */629function setUpHealthThermometerDevice() {630 return setUpPreconnectedDevice({631 address: '09:09:09:09:09:09',632 name: 'Health Thermometer',633 knownServiceUUIDs: ['generic_access', 'health_thermometer'],634 });635}636/**637 * Returns the same fake peripheral as setUpHealthThermometerDevice() except638 * that connecting to the peripheral will succeed.639 * @returns {Promise<FakePeripheral>} The device fake initialized as a640 * connectable Health Thermometer device.641 */642async function setUpConnectableHealthThermometerDevice() {643 let fake_peripheral = await setUpHealthThermometerDevice();644 await fake_peripheral.setNextGATTConnectionResponse({645 code: HCI_SUCCESS,646 });647 return fake_peripheral;648}649/**650 * Populates a fake_peripheral with various fakes appropriate for a health651 * thermometer. This resolves to an associative array composed of the fakes,652 * including the |fake_peripheral|.653 * @param {FakePeripheral} fake_peripheral The Bluetooth fake to populate GATT654 * services, characteristics, and descriptors on.655 * @returns {Promise<{fake_peripheral: FakePeripheral,656 * fake_generic_access: FakeRemoteGATTService,657 * fake_health_thermometer: FakeRemoteGATTService,658 * fake_measurement_interval: FakeRemoteGATTCharacteristic,659 * fake_cccd: FakeRemoteGATTDescriptor,660 * fake_user_description: FakeRemoteGATTDescriptor,661 * fake_temperature_measurement: FakeRemoteGATTCharacteristic,662 * fake_temperature_type: FakeRemoteGATTCharacteristic}>} The FakePeripheral663 * passed into this method along with the fake GATT services, characteristics,664 * and descriptors added to it.665 */666async function populateHealthThermometerFakes(fake_peripheral) {667 let fake_generic_access =668 await fake_peripheral.addFakeService({uuid: 'generic_access'});669 let fake_health_thermometer = await fake_peripheral.addFakeService({670 uuid: 'health_thermometer',671 });672 let fake_measurement_interval =673 await fake_health_thermometer.addFakeCharacteristic({674 uuid: 'measurement_interval',675 properties: ['read', 'write', 'indicate'],676 });677 let fake_user_description =678 await fake_measurement_interval.addFakeDescriptor({679 uuid: 'gatt.characteristic_user_description',680 });681 let fake_cccd = await fake_measurement_interval.addFakeDescriptor({682 uuid: 'gatt.client_characteristic_configuration',683 });684 let fake_temperature_measurement =685 await fake_health_thermometer.addFakeCharacteristic({686 uuid: 'temperature_measurement',687 properties: ['indicate'],688 });689 let fake_temperature_type =690 await fake_health_thermometer.addFakeCharacteristic({691 uuid: 'temperature_type',692 properties: ['read'],693 });694 return {695 fake_peripheral,696 fake_generic_access,697 fake_health_thermometer,698 fake_measurement_interval,699 fake_cccd,700 fake_user_description,701 fake_temperature_measurement,702 fake_temperature_type,703 };704}705/**706 * Returns the same device and fake peripheral as getHealthThermometerDevice()707 * after another frame (an iframe we insert) discovered the device,708 * connected to it and discovered its services.709 * @param {RequestDeviceOptions} options The options for requesting a Bluetooth710 * Device.711 * @returns {Promise<{device: BluetoothDevice, fakes: {712 * fake_peripheral: FakePeripheral,713 * fake_generic_access: FakeRemoteGATTService,714 * fake_health_thermometer: FakeRemoteGATTService,715 * fake_measurement_interval: FakeRemoteGATTCharacteristic,716 * fake_cccd: FakeRemoteGATTDescriptor,717 * fake_user_description: FakeRemoteGATTDescriptor,718 * fake_temperature_measurement: FakeRemoteGATTCharacteristic,719 * fake_temperature_type: FakeRemoteGATTCharacteristic}}>} An object720 * containing a requested BluetoothDevice and all of the GATT fake721 * objects.722 */723async function getHealthThermometerDeviceWithServicesDiscovered(options) {724 let iframe = document.createElement('iframe');725 let fake_peripheral = await setUpConnectableHealthThermometerDevice();726 let fakes = populateHealthThermometerFakes(fake_peripheral);727 await fake_peripheral.setNextGATTDiscoveryResponse({728 code: HCI_SUCCESS,729 });730 await new Promise(resolve => {731 let src = '/bluetooth/resources/health-thermometer-iframe.html';732 // TODO(509038): Can be removed once LayoutTests/bluetooth/* that733 // use health-thermometer-iframe.html have been moved to734 // LayoutTests/external/wpt/bluetooth/*735 if (window.location.pathname.includes('/LayoutTests/')) {736 src =737 '../../../external/wpt/bluetooth/resources/health-thermometer-iframe.html';738 }739 iframe.src = src;740 document.body.appendChild(iframe);741 iframe.addEventListener('load', resolve);742 });743 await new Promise((resolve, reject) => {744 callWithTrustedClick(() => {745 iframe.contentWindow.postMessage(746 {type: 'DiscoverServices', options: options}, '*');747 });748 function messageHandler(messageEvent) {749 if (messageEvent.data == 'DiscoveryComplete') {750 window.removeEventListener('message', messageHandler);751 resolve();752 } else {753 reject(new Error(`Unexpected message: ${messageEvent.data}`));754 }755 }756 window.addEventListener('message', messageHandler);757 });758 let device = await requestDeviceWithTrustedClick(options);759 await device.gatt.connect();760 return Object.assign({device}, fakes);761}762/**763 * Similar to getHealthThermometerDevice() except the device764 * is not connected and thus its services have not been765 * discovered.766 * @param {RequestDeviceOptions} options The options for requesting a Bluetooth767 * Device.768 * @returns {device: BluetoothDevice, fake_peripheral: FakePeripheral} An object769 * containing a requested BluetoothDevice and its fake counter part.770 */771async function getDiscoveredHealthThermometerDevice(options = {772 filters: [{services: ['health_thermometer']}]773}) {774 let fake_peripheral = await setUpHealthThermometerDevice();775 let device = await requestDeviceWithTrustedClick(options);776 return {device: device, fake_peripheral: fake_peripheral};777}778/**779 * Similar to getHealthThermometerDevice() except the device has no services,780 * characteristics, or descriptors.781 * @param {RequestDeviceOptions} options The options for requesting a Bluetooth782 * Device.783 * @returns {device: BluetoothDevice, fake_peripheral: FakePeripheral} An object784 * containing a requested BluetoothDevice and its fake counter part.785 */786async function getEmptyHealthThermometerDevice(options) {787 let result = await getDiscoveredHealthThermometerDevice(options);788 await result.fake_peripheral.setNextGATTConnectionResponse(789 {code: HCI_SUCCESS});790 await result.device.gatt.connect();791 await result.fake_peripheral.setNextGATTDiscoveryResponse(792 {code: HCI_SUCCESS});793 return result;794}795/**796 * Similar to getHealthThermometerService() except the service has no797 * characteristics or included services.798 * @param {RequestDeviceOptions} options The options for requesting a Bluetooth799 * Device.800 * @returns {service: BluetoothRemoteGATTService,801 * fake_health_thermometer: FakeRemoteGATTService} An object containing the802 * health themometer service object and its corresponding fake.803 */804async function getEmptyHealthThermometerService(options) {805 let result = await getDiscoveredHealthThermometerDevice(options);806 await result.fake_peripheral.setNextGATTConnectionResponse(807 {code: HCI_SUCCESS});808 await result.device.gatt.connect();809 let fake_health_thermometer =810 await result.fake_peripheral.addFakeService({uuid: 'health_thermometer'});811 await result.fake_peripheral.setNextGATTDiscoveryResponse(812 {code: HCI_SUCCESS});813 let service =814 await result.device.gatt.getPrimaryService('health_thermometer');815 return {816 service: service,817 fake_health_thermometer: fake_health_thermometer,818 };819}820/**821 * Similar to getHealthThermometerDevice except the GATT discovery822 * response has not been set yet so more attributes can still be added.823 * @param {RequestDeviceOptions} options The options for requesting a Bluetooth824 * Device.825 * @returns {Promise<{device: BluetoothDevice, fakes: {826 * fake_peripheral: FakePeripheral,827 * fake_generic_access: FakeRemoteGATTService,828 * fake_health_thermometer: FakeRemoteGATTService,829 * fake_measurement_interval: FakeRemoteGATTCharacteristic,830 * fake_cccd: FakeRemoteGATTDescriptor,831 * fake_user_description: FakeRemoteGATTDescriptor,832 * fake_temperature_measurement: FakeRemoteGATTCharacteristic,833 * fake_temperature_type: FakeRemoteGATTCharacteristic}}>} An object834 * containing a requested BluetoothDevice and all of the GATT fake835 * objects.836 */837async function getConnectedHealthThermometerDevice(options) {838 let result = await getDiscoveredHealthThermometerDevice(options);839 await result.fake_peripheral.setNextGATTConnectionResponse({840 code: HCI_SUCCESS,841 });842 let fakes = await populateHealthThermometerFakes(result.fake_peripheral);843 await result.device.gatt.connect();844 return Object.assign({device: result.device}, fakes);845}846/**847 * Returns an object containing a BluetoothDevice discovered using |options|,848 * its corresponding FakePeripheral and FakeRemoteGATTServices.849 * The simulated device is called 'Health Thermometer' it has two known service850 * UUIDs: 'generic_access' and 'health_thermometer' which correspond to two851 * services with the same UUIDs. The 'health thermometer' service contains three852 * characteristics:853 * - 'temperature_measurement' (indicate),854 * - 'temperature_type' (read),855 * - 'measurement_interval' (read, write, indicate)856 * The 'measurement_interval' characteristic contains a857 * 'gatt.client_characteristic_configuration' descriptor and a858 * 'characteristic_user_description' descriptor.859 * The device has been connected to and its attributes are ready to be860 * discovered.861 * @param {RequestDeviceOptions} options The options for requesting a Bluetooth862 * Device.863 * @returns {Promise<{device: BluetoothDevice, fakes: {864 * fake_peripheral: FakePeripheral,865 * fake_generic_access: FakeRemoteGATTService,866 * fake_health_thermometer: FakeRemoteGATTService,867 * fake_measurement_interval: FakeRemoteGATTCharacteristic,868 * fake_cccd: FakeRemoteGATTDescriptor,869 * fake_user_description: FakeRemoteGATTDescriptor,870 * fake_temperature_measurement: FakeRemoteGATTCharacteristic,871 * fake_temperature_type: FakeRemoteGATTCharacteristic}}>} An object872 * containing a requested BluetoothDevice and all of the GATT fake873 * objects.874 */875async function getHealthThermometerDevice(options) {876 let result = await getConnectedHealthThermometerDevice(options);877 await result.fake_peripheral.setNextGATTDiscoveryResponse({878 code: HCI_SUCCESS,879 });880 return result;881}882/**883 * Similar to getHealthThermometerDevice except that the peripheral has two884 * 'health_thermometer' services.885 * @param {RequestDeviceOptions} options The options for requesting a Bluetooth886 * Device.887 * @returns {Promise<{device: BluetoothDevice, fake_peripheral: FakePeripheral,888 * fake_generic_access: FakeRemoteGATTService, fake_health_thermometer1:889 * FakeRemoteGATTService, fake_health_thermometer2: FakeRemoteGATTService}>} An890 * object containing a requested Bluetooth device and two fake health891 * thermometer GATT services.892 */893async function getTwoHealthThermometerServicesDevice(options) {894 let result = await getConnectedHealthThermometerDevice(options);895 let fake_health_thermometer2 =896 await result.fake_peripheral.addFakeService({uuid: 'health_thermometer'});897 await result.fake_peripheral.setNextGATTDiscoveryResponse(898 {code: HCI_SUCCESS});899 return {900 device: result.device,901 fake_peripheral: result.fake_peripheral,902 fake_generic_access: result.fake_generic_access,903 fake_health_thermometer1: result.fake_health_thermometer,904 fake_health_thermometer2: fake_health_thermometer2905 };906}907/**908 * Returns an object containing a Health Thermometer BluetoothRemoteGattService909 * and its corresponding FakeRemoteGATTService.910 * @returns {Promise<{device: BluetoothDevice, fakes: {911 * fake_peripheral: FakePeripheral,912 * fake_generic_access: FakeRemoteGATTService,913 * fake_health_thermometer: FakeRemoteGATTService,914 * fake_measurement_interval: FakeRemoteGATTCharacteristic,915 * fake_cccd: FakeRemoteGATTDescriptor,916 * fake_user_description: FakeRemoteGATTDescriptor,917 * fake_temperature_measurement: FakeRemoteGATTCharacteristic,918 * fake_temperature_type: FakeRemoteGATTCharacteristic,919 * service: BluetoothRemoteGATTService,920 * fake_service: FakeRemoteGATTService}}>} An object921 * containing a requested BluetoothDevice and all of the GATT fake922 * objects.923 */924async function getHealthThermometerService() {925 let result = await getHealthThermometerDevice();926 let service =927 await result.device.gatt.getPrimaryService('health_thermometer');928 return Object.assign(result, {929 service,930 fake_service: result.fake_health_thermometer,931 });932}933/**934 * Returns an object containing a Measurement Interval935 * BluetoothRemoteGATTCharacteristic and its corresponding936 * FakeRemoteGATTCharacteristic.937 * @returns {Promise<{device: BluetoothDevice, fakes: {938 * fake_peripheral: FakePeripheral,939 * fake_generic_access: FakeRemoteGATTService,940 * fake_health_thermometer: FakeRemoteGATTService,941 * fake_measurement_interval: FakeRemoteGATTCharacteristic,942 * fake_cccd: FakeRemoteGATTDescriptor,943 * fake_user_description: FakeRemoteGATTDescriptor,944 * fake_temperature_measurement: FakeRemoteGATTCharacteristic,945 * fake_temperature_type: FakeRemoteGATTCharacteristic,946 * service: BluetoothRemoteGATTService,947 * fake_service: FakeRemoteGATTService,948 * characteristic: BluetoothRemoteGATTCharacteristic,949 * fake_characteristic: FakeRemoteGATTCharacteristic}}>} An object950 * containing a requested BluetoothDevice and all of the GATT fake951 * objects.952 */953async function getMeasurementIntervalCharacteristic() {954 let result = await getHealthThermometerService();955 let characteristic =956 await result.service.getCharacteristic('measurement_interval');957 return Object.assign(result, {958 characteristic,959 fake_characteristic: result.fake_measurement_interval,960 });961}962/**963 * Returns an object containing a User Description964 * BluetoothRemoteGATTDescriptor and its corresponding965 * FakeRemoteGATTDescriptor.966 * @returns {Promise<{device: BluetoothDevice, fakes: {967 * fake_peripheral: FakePeripheral,968 * fake_generic_access: FakeRemoteGATTService,969 * fake_health_thermometer: FakeRemoteGATTService,970 * fake_measurement_interval: FakeRemoteGATTCharacteristic,971 * fake_cccd: FakeRemoteGATTDescriptor,972 * fake_user_description: FakeRemoteGATTDescriptor,973 * fake_temperature_measurement: FakeRemoteGATTCharacteristic,974 * fake_temperature_type: FakeRemoteGATTCharacteristic,975 * service: BluetoothRemoteGATTService,976 * fake_service: FakeRemoteGATTService,977 * characteristic: BluetoothRemoteGATTCharacteristic,978 * fake_characteristic: FakeRemoteGATTCharacteristic979 * descriptor: BluetoothRemoteGATTDescriptor,980 * fake_descriptor: FakeRemoteGATTDescriptor}}>} An object981 * containing a requested BluetoothDevice and all of the GATT fake982 * objects.983 */984async function getUserDescriptionDescriptor() {985 let result = await getMeasurementIntervalCharacteristic();986 let descriptor = await result.characteristic.getDescriptor(987 'gatt.characteristic_user_description');988 return Object.assign(result, {989 descriptor,990 fake_descriptor: result.fake_user_description,991 });992}993/** Heart Rate Bluetooth Device Helper Methods */994/** @type {FakeDeviceOptions} */995const heartRateFakeDeviceOptionsDefault = {996 address: '08:08:08:08:08:08',997 name: 'Heart Rate',998 knownServiceUUIDs: ['generic_access', 'heart_rate'],999 connectable: false,1000 serviceDiscoveryComplete: false,1001};1002/** @type {RequestDeviceOptions} */1003const heartRateRequestDeviceOptionsDefault = {1004 filters: [{services: ['heart_rate']}]1005};1006async function getHeartRateDevice(setupOptionsOverride) {1007 let setupOptions = createSetupOptions(1008 {fakeDeviceOptions: heartRateFakeDeviceOptionsDefault},1009 setupOptionsOverride);1010 return await setUpPreconnectedFakeDevice(setupOptions);1011}1012/**1013 * Returns an array containing two FakePeripherals corresponding1014 * to the simulated devices.1015 * @returns {Promise<Array<FakePeripheral>>} The device fakes initialized as1016 * Health Thermometer and Heart Rate devices.1017 */1018async function setUpHealthThermometerAndHeartRateDevices() {1019 await initializeFakeCentral({state: 'powered-on'});1020 return Promise.all([1021 fake_central.simulatePreconnectedPeripheral({1022 address: '09:09:09:09:09:09',1023 name: 'Health Thermometer',1024 knownServiceUUIDs: ['generic_access', 'health_thermometer'],1025 }),1026 fake_central.simulatePreconnectedPeripheral({1027 address: '08:08:08:08:08:08',1028 name: 'Heart Rate',1029 knownServiceUUIDs: ['generic_access', 'heart_rate'],1030 })1031 ]);...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const wpt = require('wpt-api');2wpt.initializeFakeCentral();3const wpt = require('wpt-api');4wpt.initializeFakeCentral();5const wpt = require('wpt-api');6wpt.initializeFakeCentral();7const wpt = require('wpt-api');8wpt.initializeFakeCentral();9const wpt = require('wpt-api');10wpt.initializeFakeCentral();11const wpt = require('wpt-api');12wpt.initializeFakeCentral();13const wpt = require('wpt-api');14wpt.initializeFakeCentral();15const wpt = require('wpt-api');16wpt.initializeFakeCentral();17const wpt = require('wpt-api');18wpt.initializeFakeCentral();19const wpt = require('wpt-api');20wpt.initializeFakeCentral();21const wpt = require('wpt-api');22wpt.initializeFakeCentral();23const wpt = require('wpt-api');24wpt.initializeFakeCentral();25const wpt = require('wpt-api');26wpt.initializeFakeCentral();27const wpt = require('wpt-api');28wpt.initializeFakeCentral();29const wpt = require('w

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptb = require('./wptb.js');2wptb.initializeFakeCentral();3var initializeFakeCentral = function() {4 console.log('initializing fake central');5}6module.exports = {7}8module.exports.initializeFakeCentral = function() {9 console.log('initializing fake central');10}11var wptb = require('./wptb.js');12wptb.initializeFakeCentral();13module.exports = {14 initializeFakeCentral: function() {15 console.log('initializing fake central');16 },17 initializeFakeCentral2: function() {18 console.log('initializing fake central 2');19 }20}21var wptb = require('./wptb.js');22wptb.initializeFakeCentral();23wptb.initializeFakeCentral2();24module.exports = function() {25 console.log('initializing fake central');26}27var wptb = require('./wptb.js');28wptb();29module.exports = function() {30 this.initializeFakeCentral = function() {31 console.log('initializing fake central');32 }33}34var Wptb = require('./wptb.js');35var wptb = new Wptb();36wptb.initializeFakeCentral();37module.exports = function() {38 this.initializeFakeCentral = function() {39 console.log('

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3wpt.initializeFakeCentral('fake-central', 'fake-central', 'fake-central');4var wpt = require('webpagetest');5var wpt = new WebPageTest('www.webpagetest.org');6var options = {7 basicAuth: {8 },

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2wpt.initializeFakeCentral();3var wpt = require('wpt');4wpt.initializeFakeCentral()5.then(function() {6})7.catch(function(err) {8});9var wpt = require('wpt');10wpt.initializeFakeCentral()11.then(function() {12})13.catch(function(err) {14});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful