blob: 8ec992e1a346e0b298d993c4d5f4213e7037e5c6 [file] [log] [blame]
<!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_true(window.testRunner instanceof Object); 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 => {
testRunner.setBluetoothMockDataSet('EmptyAdapter');
return assert_promise_rejects_with_message(
service.getCharacteristic('gap.device_name'),
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(gattService => gattService.getPrimaryService('generic_access'))
.then(service => {
testRunner.setBluetoothMockDataSet('MissingServiceHeartRateAdapter');
return assert_promise_rejects_with_message(
service.getCharacteristic('gap.device_name'),
new DOMException('GATT Service no longer exists.',
'InvalidStateError'),
'Service got removed.');
});
}, 'Service is removed. Reject with InvalidStateError.');
promise_test(() => {
testRunner.setBluetoothMockDataSet('HeartRateAdapter');
let expected = new DOMException('Characteristic not found in device.',
'NotFoundError');
return requestDeviceWithKeyDown({filters: [{services: ['heart_rate']}]})
.then(device => device.connectGATT())
.then(gattServer => gattServer.getPrimaryService('generic_access'))
.then(service => Promise.all(
[assert_promise_rejects_with_message(
service.getCharacteristic(battery_level.alias), expected),
assert_promise_rejects_with_message(
service.getCharacteristic(battery_level.name), expected),
assert_promise_rejects_with_message(
service.getCharacteristic(battery_level.uuid), expected)]));
}, 'Request for wrong characteristic. Reject with NotFoundError.');
promise_test(() => {
testRunner.setBluetoothMockDataSet('HeartRateAdapter');
return requestDeviceWithKeyDown({filters: [{services: ['heart_rate']}]})
.then(device => device.connectGATT())
.then(gattServer => gattServer.getPrimaryService('generic_access'))
.then(service => Promise.all(
[service.getCharacteristic(device_name.alias),
service.getCharacteristic(device_name.name),
service.getCharacteristic(device_name.uuid)]))
.then(characteristics => {
characteristics.forEach(characteristic => {
assert_equals(
characteristic.uuid, device_name.uuid,
'Characteristic UUID should be the same as requested UUID.');
});
});
}, 'Request for characteristic. Should return right characteristic');
promise_test(() => {
testRunner.setBluetoothMockDataSet('HeartRateAdapter');
return requestDeviceWithKeyDown({filters: [{services: ['heart_rate']}]})
.then(device => device.connectGATT())
.then(gattServer => gattServer.getPrimaryService('generic_access'))
.then(services => Promise.all(
[services.getCharacteristic(device_name.alias),
services.getCharacteristic(device_name.alias),
services.getCharacteristic(device_name.name),
services.getCharacteristic(device_name.name),
services.getCharacteristic(device_name.uuid),
services.getCharacteristic(device_name.uuid)]))
.then(characteristics => {
// TODO(ortuno): getCharacteristic should return the same object
// if it was created earlier.
// https://crbug.com/495270
for (var i = 1; i < characteristics.length; i++) {
assert_not_equals(
characteristics[0], characteristics[i],
'Should return the same characteristic as the first call.');
}
});
}, 'Calls to get the same characteristic should return the same object.');
promise_test(() => {
testRunner.setBluetoothMockDataSet('HeartRateAdapter');
return requestDeviceWithKeyDown({filters: [{services: ['heart_rate']}]})
.then(device => device.connectGATT())
.then(gattServer => gattServer.getPrimaryService('generic_access'))
.then(service => {
return assert_promise_rejects_with_message(
service.getCharacteristic('wrong_name'), new DOMException(
'Failed to execute \'getCharacteristic\' on ' +
'\'BluetoothGATTService\': Invalid Characteristic name: ' +
'\'wrong_name\'. ' +
'It must be a valid UUID alias (e.g. 0x1234), ' +
'UUID (lowercase hex characters e.g. ' +
'\'00001234-0000-1000-8000-00805f9b34fb\'), ' +
'or recognized standard name from ' +
'https://developer.bluetooth.org/gatt/characteristics/' +
'Pages/CharacteristicsHome.aspx' +
' e.g. \'aerobic_heart_rate_lower_limit\'.',
'SyntaxError'),
'Wrong Characteristic name passed.');
});
}, 'Wrong Characteristic name. Reject with SyntaxError.');
</script>