granted-devices-with-services.https.window.js (3063B)
1 // META: script=/resources/testdriver.js?feature=bidi 2 // META: script=/resources/testdriver-vendor.js 3 // META: script=/bluetooth/resources/bluetooth-test.js 4 // META: script=/bluetooth/resources/bluetooth-fake-devices.js 5 // META: timeout=long 6 'use strict'; 7 const test_desc = 'getDevices() resolves with permitted devices that can be ' + 8 'GATT connected to.'; 9 10 bluetooth_bidi_test(async () => { 11 // Set up two connectable Bluetooth devices with their services discovered. 12 // One device is a Health Thermometer device with the 'health_thermometer' 13 // service while the other is a Heart Rate device with the 'heart_rate' 14 // service. Both devices contain the 'generic_access' service. 15 let fake_peripherals = await setUpHealthThermometerAndHeartRateDevices(); 16 for (let fake_peripheral of fake_peripherals) { 17 await fake_peripheral.setNextGATTConnectionResponse({code: HCI_SUCCESS}); 18 await fake_peripheral.addFakeService({uuid: 'generic_access'}); 19 if (fake_peripheral.address === '09:09:09:09:09:09') 20 await fake_peripheral.addFakeService({uuid: 'health_thermometer'}); 21 else 22 await fake_peripheral.addFakeService({uuid: 'heart_rate'}); 23 await fake_peripheral.setNextGATTDiscoveryResponse({code: HCI_SUCCESS}); 24 } 25 26 // Request the Health Thermometer device with access to its 'generic_access' 27 // service. 28 await requestDeviceWithTrustedClick( 29 {filters: [{name: 'Health Thermometer', services: ['generic_access']}]}); 30 let devices = await navigator.bluetooth.getDevices(); 31 assert_equals( 32 devices.length, 1, 33 `getDevices() should return the 'Health Thermometer' device.`); 34 35 // Only the 'generic_access' service can be accessed. 36 try { 37 await devices[0].gatt.connect(); 38 await devices[0].gatt.getPrimaryService('generic_access'); 39 assert_promise_rejects_with_message( 40 devices[0].gatt.getPrimaryService('health_thermometer'), 41 {name: 'SecurityError'}); 42 } catch (err) { 43 assert_unreached(`${err.name}: ${err.message}`); 44 } 45 46 // Request the Heart Rate device with access to both of its services. 47 await requestDeviceWithTrustedClick({ 48 filters: [{name: 'Heart Rate', services: ['generic_access', 'heart_rate']}] 49 }); 50 devices = await navigator.bluetooth.getDevices(); 51 assert_equals( 52 devices.length, 2, 53 `getDevices() should return the 'Health Thermometer' and 'Health ` + 54 `Monitor' devices`); 55 56 // All of Heart Rate device's services can be accessed, while only the 57 // 'generic_access' service can be accessed on Health Thermometer. 58 try { 59 for (let device of devices) { 60 await device.gatt.connect(); 61 await device.gatt.getPrimaryService('generic_access'); 62 if (device.name === 'Heart Rate') { 63 await device.gatt.getPrimaryService('heart_rate'); 64 } else { 65 assert_promise_rejects_with_message( 66 devices[0].gatt.getPrimaryService('health_thermometer'), 67 {name: 'SecurityError'}); 68 } 69 } 70 } catch (err) { 71 assert_unreached(`${err.name}: ${err.message}`); 72 } 73 }, test_desc);