Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / bluetooth / readValue.html
blob683e68ca5f42037f9bd2b98fceedc2ad8a8ea8e3
1 <!DOCTYPE html>
2 <script src="../resources/testharness.js"></script>
3 <script src="../resources/testharnessreport.js"></script>
4 <script src="resources/bluetooth-helpers.js"></script>
5 <script>
6 'use strict';
8 test(t => { assert_true(window.testRunner instanceof Object); t.done(); },
9 'window.testRunner is required for the following tests.');
11 promise_test(() => {
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(), {
21 name: 'NetworkError',
22 message: 'Bluetooth Device is no longer in range.'
23 }, 'Device went out of range');
24 });
25 }, 'Device goes out of range. Reject with NetworkError.');
27 promise_test(() => {
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.');
40 });
41 }, 'Service gets removed. Reject with InvalidStateError.');
43 promise_test(() => {
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.');
57 });
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),
71 error: {
72 name: 'NotSupportedError',
73 message: 'GATT Error Unknown.'
75 }, {
76 testName: 'GATT Error: Failed.',
77 uuid: errorUUID(0xA2),
78 error: {
79 name: 'NotSupportedError',
80 message: 'GATT operation failed for unknown reason.'
82 }, {
83 testName: 'GATT Error: In Progress.',
84 uuid: errorUUID(0xA3),
85 error: {
86 name: 'NetworkError',
87 message: 'GATT operation already in progress.'
89 }, {
90 testName: 'GATT Error: Invalid Length.',
91 uuid: errorUUID(0xA4),
92 error: {
93 name: 'InvalidModificationError',
94 message: 'GATT Error: invalid attribute length.'
96 }, {
97 testName: 'GATT Error: Not Permitted.',
98 uuid: errorUUID(0xA5),
99 error: {
100 name: 'NotSupportedError',
101 message: 'GATT operation not permitted.'
103 }, {
104 testName: 'GATT Error: Not Authorized.',
105 uuid: errorUUID(0xA6),
106 error: {
107 name: 'SecurityError',
108 message: 'GATT operation not authorized.'
110 }, {
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
115 // resolved.
116 error: {
117 name: 'NetworkError',
118 message: 'GATT Error: Not paired.'
120 }, {
121 testName: 'GATT Error: Not Supported.',
122 uuid: errorUUID(0xA8),
123 error: {
124 name: 'NotSupportedError',
125 message: 'GATT Error: Not supported.'
127 }].forEach(testSpec => {
128 promise_test(() => {
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(),
137 testSpec.error,
138 'Trying to read the characteristic failed');
140 }, testSpec.testName);
143 promise_test(() => {
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())
150 .then(value => {
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');
156 </script>