2 <script src=
"../resources/testharness.js"></script>
3 <script src=
"../resources/testharnessreport.js"></script>
4 <script src=
"resources/bluetooth-helpers.js"></script>
8 test(t
=> { assert_true(window
.testRunner
instanceof Object
); t
.done(); },
9 'window.testRunner is required for the following tests.');
12 testRunner
.setBluetoothMockDataSet('GenericAccessAdapter');
13 return requestDeviceWithKeyDown({filters
: [{services
: ['generic_access']}]})
14 .then(device
=> device
.connectGATT())
15 .then(gattServer
=> gattServer
.getPrimaryService('generic_access'))
16 .then(service
=> service
.getCharacteristic('gap.device_name'))
17 .then(characteristic
=> {
18 testRunner
.setBluetoothMockDataSet('EmptyAdapter');
19 return assert_promise_rejects_with_message(
20 characteristic
.readValue(), {
22 message
: 'Bluetooth Device is no longer in range.'
23 }, 'Device went out of range');
25 }, 'Device goes out of range. Reject with NetworkError.');
28 testRunner
.setBluetoothMockDataSet('GenericAccessAdapter');
29 return requestDeviceWithKeyDown({filters
: [{services
: ['generic_access']}]})
30 .then(device
=> device
.connectGATT())
31 .then(gattServer
=> gattServer
.getPrimaryService('generic_access'))
32 .then(service
=> service
.getCharacteristic('gap.device_name'))
33 .then(characteristic
=> {
34 testRunner
.setBluetoothMockDataSet('MissingServiceGenericAccessAdapter');
35 return assert_promise_rejects_with_message(
36 characteristic
.readValue(), {
37 name
: 'InvalidStateError',
38 message
: 'GATT Service no longer exists.'
39 }, 'Service got removed.');
41 }, 'Service gets removed. Reject with InvalidStateError.');
44 testRunner
.setBluetoothMockDataSet('GenericAccessAdapter');
45 return requestDeviceWithKeyDown({filters
: [{services
: ['generic_access']}]})
46 .then(device
=> device
.connectGATT())
47 .then(gattService
=> gattService
.getPrimaryService('generic_access'))
48 .then(service
=> service
.getCharacteristic('gap.device_name'))
49 .then(characteristic
=> {
50 testRunner
.setBluetoothMockDataSet(
51 'MissingCharacteristicGenericAccessAdapter');
52 return assert_promise_rejects_with_message(
53 characteristic
.readValue(), {
54 name
: 'InvalidStateError',
55 message
: 'GATT Characteristic no longer exists.'
56 }, 'Characteristic got removed.');
58 }, 'Characteristic gets removed. Reject with InvalidStateError.');
60 // The following tests make sure the Web Bluetooth implementation
61 // responds correctly to the different types of errors the
62 // underlying platform might return for GATT operations.
64 // Each implementation maps these characteristics to specific code paths
65 // that result in different errors thus increasing code coverage
66 // when testing. Therefore some of these characteristics might not be useful
67 // for all implementations.
69 testName
: 'GATT Error: Unknown.',
70 uuid
: errorUUID(0xA1),
72 name
: 'NotSupportedError',
73 message
: 'GATT Error Unknown.'
76 testName
: 'GATT Error: Failed.',
77 uuid
: errorUUID(0xA2),
79 name
: 'NotSupportedError',
80 message
: 'GATT operation failed for unknown reason.'
83 testName
: 'GATT Error: In Progress.',
84 uuid
: errorUUID(0xA3),
87 message
: 'GATT operation already in progress.'
90 testName
: 'GATT Error: Invalid Length.',
91 uuid
: errorUUID(0xA4),
93 name
: 'InvalidModificationError',
94 message
: 'GATT Error: invalid attribute length.'
97 testName
: 'GATT Error: Not Permitted.',
98 uuid
: errorUUID(0xA5),
100 name
: 'NotSupportedError',
101 message
: 'GATT operation not permitted.'
104 testName
: 'GATT Error: Not Authorized.',
105 uuid
: errorUUID(0xA6),
107 name
: 'SecurityError',
108 message
: 'GATT operation not authorized.'
111 testName
: 'GATT Error: Not Paired.',
112 uuid
: errorUUID(0xA7),
113 // TODO(ortuno): Change to InsufficientAuthenticationError or similiar
114 // once https://github.com/WebBluetoothCG/web-bluetooth/issues/137 is
117 name
: 'NetworkError',
118 message
: 'GATT Error: Not paired.'
121 testName
: 'GATT Error: Not Supported.',
122 uuid
: errorUUID(0xA8),
124 name
: 'NotSupportedError',
125 message
: 'GATT Error: Not supported.'
127 }].forEach(testSpec
=> {
129 testRunner
.setBluetoothMockDataSet('FailingGATTOperationsAdapter');
130 return requestDeviceWithKeyDown({filters
: [{services
: [errorUUID(0xA0)]}]})
131 .then(device
=> device
.connectGATT())
132 .then(gattServer
=> gattServer
.getPrimaryService(errorUUID(0xA0)))
133 .then(service
=> service
.getCharacteristic(testSpec
.uuid
))
134 .then(characteristic
=> {
135 return assert_promise_rejects_with_message(
136 characteristic
.readValue(),
138 'Trying to read the characteristic failed');
140 }, testSpec
.testName
);
144 testRunner
.setBluetoothMockDataSet('GenericAccessAdapter');
145 return requestDeviceWithKeyDown({filters
: [{services
: ['generic_access']}]})
146 .then(device
=> device
.connectGATT())
147 .then(gattServer
=> gattServer
.getPrimaryService('generic_access'))
148 .then(service
=> service
.getCharacteristic('gap.device_name'))
149 .then(characteristic
=> characteristic
.readValue())
151 let decoder
= new TextDecoder('utf-8');
152 let value_str
= decoder
.decode(value
);
153 assert_equals(value_str
, 'Generic Access Device');
155 }, 'Request for characteristic. Should return right characteristic');