diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /testing/web-platform/tests/bluetooth/getDevices | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/bluetooth/getDevices')
6 files changed, 151 insertions, 0 deletions
diff --git a/testing/web-platform/tests/bluetooth/getDevices/granted-devices-with-services.https.window.js b/testing/web-platform/tests/bluetooth/getDevices/granted-devices-with-services.https.window.js new file mode 100644 index 0000000000..3228543617 --- /dev/null +++ b/testing/web-platform/tests/bluetooth/getDevices/granted-devices-with-services.https.window.js @@ -0,0 +1,72 @@ +// META: script=/resources/testdriver.js +// META: script=/resources/testdriver-vendor.js +// META: script=/bluetooth/resources/bluetooth-test.js +// META: script=/bluetooth/resources/bluetooth-fake-devices.js +'use strict'; +const test_desc = 'getDevices() resolves with permitted devices that can be ' + + 'GATT connected to.'; + +bluetooth_test(async () => { + // Set up two connectable Bluetooth devices with their services discovered. + // One device is a Health Thermometer device with the 'health_thermometer' + // service while the other is a Heart Rate device with the 'heart_rate' + // service. Both devices contain the 'generic_access' service. + let fake_peripherals = await setUpHealthThermometerAndHeartRateDevices(); + for (let fake_peripheral of fake_peripherals) { + await fake_peripheral.setNextGATTConnectionResponse({code: HCI_SUCCESS}); + await fake_peripheral.addFakeService({uuid: 'generic_access'}); + if (fake_peripheral.address === '09:09:09:09:09:09') + await fake_peripheral.addFakeService({uuid: 'health_thermometer'}); + else + await fake_peripheral.addFakeService({uuid: 'heart_rate'}); + await fake_peripheral.setNextGATTDiscoveryResponse({code: HCI_SUCCESS}); + } + + // Request the Health Thermometer device with access to its 'generic_access' + // service. + await requestDeviceWithTrustedClick( + {filters: [{name: 'Health Thermometer', services: ['generic_access']}]}); + let devices = await navigator.bluetooth.getDevices(); + assert_equals( + devices.length, 1, + `getDevices() should return the 'Health Thermometer' device.`); + + // Only the 'generic_access' service can be accessed. + try { + await devices[0].gatt.connect(); + await devices[0].gatt.getPrimaryService('generic_access'); + assert_promise_rejects_with_message( + devices[0].gatt.getPrimaryService('health_thermometer'), + {name: 'SecurityError'}); + } catch (err) { + assert_unreached(`${err.name}: ${err.message}`); + } + + // Request the Heart Rate device with access to both of its services. + await requestDeviceWithTrustedClick({ + filters: [{name: 'Heart Rate', services: ['generic_access', 'heart_rate']}] + }); + devices = await navigator.bluetooth.getDevices(); + assert_equals( + devices.length, 2, + `getDevices() should return the 'Health Thermometer' and 'Health ` + + `Monitor' devices`); + + // All of Heart Rate device's services can be accessed, while only the + // 'generic_access' service can be accessed on Health Thermometer. + try { + for (let device of devices) { + await device.gatt.connect(); + await device.gatt.getPrimaryService('generic_access'); + if (device.name === 'Heart Rate') { + await device.gatt.getPrimaryService('heart_rate'); + } else { + assert_promise_rejects_with_message( + devices[0].gatt.getPrimaryService('health_thermometer'), + {name: 'SecurityError'}); + } + } + } catch (err) { + assert_unreached(`${err.name}: ${err.message}`); + } +}, test_desc); diff --git a/testing/web-platform/tests/bluetooth/getDevices/no-granted-devices.https.window.js b/testing/web-platform/tests/bluetooth/getDevices/no-granted-devices.https.window.js new file mode 100644 index 0000000000..304aa3820d --- /dev/null +++ b/testing/web-platform/tests/bluetooth/getDevices/no-granted-devices.https.window.js @@ -0,0 +1,15 @@ +// META: script=/resources/testdriver.js +// META: script=/resources/testdriver-vendor.js +// META: script=/bluetooth/resources/bluetooth-test.js +// META: script=/bluetooth/resources/bluetooth-fake-devices.js +'use strict'; +const test_desc = 'getDevices() resolves with empty array if no device ' + + 'permissions have been granted.'; + +bluetooth_test(async () => { + await navigator.bluetooth.test.simulateCentral({state: 'powered-on'}); + let devices = await navigator.bluetooth.getDevices(); + + assert_equals( + 0, devices.length, 'getDevices() should resolve with an empty array'); +}, test_desc); diff --git a/testing/web-platform/tests/bluetooth/getDevices/reject_opaque_origin.https.html b/testing/web-platform/tests/bluetooth/getDevices/reject_opaque_origin.https.html new file mode 100644 index 0000000000..64b2808fbc --- /dev/null +++ b/testing/web-platform/tests/bluetooth/getDevices/reject_opaque_origin.https.html @@ -0,0 +1,13 @@ +<!DOCTYPE html> + +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<script> + 'use strict'; + + promise_test(async (t) => { + await promise_rejects_dom( + t, 'SecurityError', navigator.bluetooth.getDevices(), + 'getDevices() should throw a SecurityError DOMException when called from a context where the top-level document has an opaque origin.'); + }, 'Calls to Bluetooth APIs from an origin with opaque top origin get blocked.'); +</script>
\ No newline at end of file diff --git a/testing/web-platform/tests/bluetooth/getDevices/reject_opaque_origin.https.html.headers b/testing/web-platform/tests/bluetooth/getDevices/reject_opaque_origin.https.html.headers new file mode 100644 index 0000000000..c7e4e7cc5b --- /dev/null +++ b/testing/web-platform/tests/bluetooth/getDevices/reject_opaque_origin.https.html.headers @@ -0,0 +1 @@ +Content-Security-Policy: sandbox allow-scripts
\ No newline at end of file diff --git a/testing/web-platform/tests/bluetooth/getDevices/returns-same-bluetooth-device-object.https.window.js b/testing/web-platform/tests/bluetooth/getDevices/returns-same-bluetooth-device-object.https.window.js new file mode 100644 index 0000000000..81c0f6a97e --- /dev/null +++ b/testing/web-platform/tests/bluetooth/getDevices/returns-same-bluetooth-device-object.https.window.js @@ -0,0 +1,23 @@ +// META: script=/resources/testdriver.js +// META: script=/resources/testdriver-vendor.js +// META: script=/bluetooth/resources/bluetooth-test.js +// META: script=/bluetooth/resources/bluetooth-fake-devices.js +'use strict'; +const test_desc = 'multiple calls to getDevices() resolves with the same' + + 'BluetoothDevice objects for each granted Bluetooth device.'; + +bluetooth_test(async () => { + await getConnectedHealthThermometerDevice(); + let firstDevices = await navigator.bluetooth.getDevices(); + assert_equals( + firstDevices.length, 1, 'getDevices() should return the granted device.'); + + let secondDevices = await navigator.bluetooth.getDevices(); + assert_equals( + secondDevices.length, 1, + 'getDevices() should return the granted device.'); + assert_equals( + firstDevices[0], secondDevices[0], + 'getDevices() should produce the same BluetoothDevice objects for a ' + + 'given Bluetooth device.'); +}, test_desc); diff --git a/testing/web-platform/tests/bluetooth/getDevices/sandboxed_iframe.https.window.js b/testing/web-platform/tests/bluetooth/getDevices/sandboxed_iframe.https.window.js new file mode 100644 index 0000000000..22cfd17d46 --- /dev/null +++ b/testing/web-platform/tests/bluetooth/getDevices/sandboxed_iframe.https.window.js @@ -0,0 +1,27 @@ +// META: script=/resources/testdriver.js +// META: script=/resources/testdriver-vendor.js +// META: script=/bluetooth/resources/bluetooth-test.js +// META: script=/bluetooth/resources/bluetooth-fake-devices.js + +'use strict'; + +let iframe = document.createElement('iframe'); + +bluetooth_test(async () => { + await getConnectedHealthThermometerDevice(); + await new Promise(resolve => { + iframe.src = '/bluetooth/resources/health-thermometer-iframe.html'; + iframe.sandbox.add('allow-scripts'); + iframe.allow = 'bluetooth'; + document.body.appendChild(iframe); + iframe.addEventListener('load', resolve); + }); + await new Promise(resolve => { + iframe.contentWindow.postMessage({type: 'GetDevices'}, '*'); + + window.addEventListener('message', (messageEvent) => { + assert_false(/^FAIL: .*/.test(messageEvent.data)); + resolve(); + }); + }); +}, 'Calls to Bluetooth APIs from a sandboxed iframe are valid.');
\ No newline at end of file |