| <!DOCTYPE html> |
| <script src="../resources/testharness.js"></script> |
| <script src="../resources/testharnessreport.js"></script> |
| <script src="resources/bluetooth-helpers.js"></script> |
| <script> |
| 'use strict'; |
| |
| test(t => { assert_exists(window, 'testRunner'); t.done(); }, |
| 'window.testRunner is required for the following tests.'); |
| |
| promise_test(() => { |
| testRunner.setBluetoothMockDataSet('HeartRateAdapter'); |
| return requestDeviceWithKeyDown({filters: [{services: ['heart_rate']}]}) |
| .then(device => device.connectGATT()) |
| .then(gattServer => gattServer.getPrimaryService('generic_access')) |
| .then(service => service.getCharacteristic('gap.device_name')) |
| .then(characteristic => { |
| testRunner.setBluetoothMockDataSet('EmptyAdapter'); |
| return assert_promise_rejects_with_message( |
| characteristic.writeValue(new ArrayBuffer(1 /* length */)), |
| new DOMException( |
| 'Bluetooth Device is no longer in range.', 'NetworkError'), |
| 'Device went out of range.'); |
| }); |
| }, 'Device goes out of range. Reject with NetworkError.'); |
| |
| promise_test(() => { |
| testRunner.setBluetoothMockDataSet('HeartRateAdapter'); |
| return requestDeviceWithKeyDown({filters: [{services: ['heart_rate']}]}) |
| .then(device => device.connectGATT()) |
| .then(gattServer => gattServer.getPrimaryService('generic_access')) |
| .then(service => service.getCharacteristic('gap.device_name')) |
| .then(characteristic => { |
| testRunner.setBluetoothMockDataSet('MissingServiceHeartRateAdapter'); |
| return assert_promise_rejects_with_message( |
| characteristic.writeValue(new ArrayBuffer(1 /* length */)), |
| new DOMException('GATT Service no longer exists.', |
| 'InvalidStateError'), |
| 'Service got removed.'); |
| }); |
| }, 'Service gets removed. Reject with InvalidStateError.'); |
| |
| promise_test(() => { |
| testRunner.setBluetoothMockDataSet('HeartRateAdapter'); |
| return requestDeviceWithKeyDown({filters: [{services: ['heart_rate']}]}) |
| .then(device => device.connectGATT()) |
| .then(gattServer => gattServer.getPrimaryService('generic_access')) |
| .then(service => service.getCharacteristic('gap.device_name')) |
| .then(characteristic => { |
| testRunner.setBluetoothMockDataSet('MissingCharacteristicHeartRateAdapter'); |
| return assert_promise_rejects_with_message( |
| characteristic.writeValue(new ArrayBuffer(1 /* length */)), |
| new DOMException( |
| 'GATT Characteristic no longer exists.', 'InvalidStateError'), |
| 'Characteristic got removed.'); |
| }); |
| }, 'Characteristic gets removed. Reject with InvalidStateError.'); |
| |
| gatt_errors_tests.forEach(testSpec => { |
| promise_test(() => { |
| testRunner.setBluetoothMockDataSet('FailingGATTOperationsAdapter'); |
| return requestDeviceWithKeyDown({filters: [{services: [errorUUID(0xA0)]}]}) |
| .then(device => device.connectGATT()) |
| .then(gattServer => gattServer.getPrimaryService(errorUUID(0xA0))) |
| .then(service => service.getCharacteristic(testSpec.uuid)) |
| .then(characteristic => { |
| return assert_promise_rejects_with_message( |
| characteristic.writeValue(new Uint8Array([1])), |
| testSpec.error, |
| 'Trying to write to a characteristic failed.'); |
| }); |
| }, testSpec.testName); |
| }); |
| |
| promise_test(() => { |
| testRunner.setBluetoothMockDataSet('HeartRateAdapter'); |
| return requestDeviceWithKeyDown({filters: [{services: ['heart_rate']}]}) |
| .then(device => device.connectGATT()) |
| .then(gattServer => gattServer.getPrimaryService('generic_access')) |
| .then(service => service.getCharacteristic('gap.device_name')) |
| .then(characteristic => { |
| return assert_promise_rejects_with_message( |
| characteristic.writeValue(new Uint8Array(513 /* length */)), |
| new DOMException( |
| 'Value can\'t exceed 512 bytes.', 'InvalidModificationError'), |
| 'Value passed was too long.'); |
| }); |
| }, 'Trying to write more than 512 bytes should return an error.'); |
| |
| promise_test(() => { |
| testRunner.setBluetoothMockDataSet('HeartRateAdapter'); |
| return requestDeviceWithKeyDown({filters: [{services: ['heart_rate']}]}) |
| .then(device => device.connectGATT()) |
| .then(gattServer => gattServer.getPrimaryService('generic_access')) |
| .then(service => service.getCharacteristic('gap.device_name')) |
| .then(characteristic => Promise.all( |
| [characteristic.writeValue(new Uint8Array(1 /* length */)), |
| characteristic.writeValue(new ArrayBuffer(1 /* length */)), |
| characteristic.writeValue(new DataView(new ArrayBuffer(1 /* length */)))])); |
| }, 'A regular write request to a writable characteristic should succeed.'); |
| </script> |