Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / bluetooth / getCharacteristic.html
blobfdd6762f2be97f1e6a043848cce1a1ea020b2b5a
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 => {
17 testRunner.setBluetoothMockDataSet('EmptyAdapter');
18 return assert_promise_rejects_with_message(
19 service.getCharacteristic('gap.device_name'), {
20 name: 'NetworkError',
21 message: 'Bluetooth Device is no longer in range.'
22 }, 'Device went out of range.');
23 });
24 }, 'Device goes out of range. Reject with NetworkError.');
26 promise_test(() => {
27 testRunner.setBluetoothMockDataSet('GenericAccessAdapter');
28 return requestDeviceWithKeyDown({filters: [{services: ['generic_access']}]})
29 .then(device => device.connectGATT())
30 .then(gattService => gattService.getPrimaryService('generic_access'))
31 .then(service => {
32 testRunner.setBluetoothMockDataSet('MissingServiceGenericAccessAdapter');
33 return assert_promise_rejects_with_message(
34 service.getCharacteristic('gap.device_name'), {
35 name: 'InvalidStateError',
36 message: 'GATT Service no longer exists.'
37 }, 'Service got removed.');
38 });
39 }, 'Service is removed. Reject with InvalidStateError.');
42 promise_test(() => {
43 testRunner.setBluetoothMockDataSet('GenericAccessAdapter');
44 return requestDeviceWithKeyDown({filters: [{services: ['generic_access']}]})
45 .then(device => device.connectGATT())
46 .then(gattServer => gattServer.getPrimaryService('generic_access'))
47 .then(service => Promise.all(
48 [service.getCharacteristic(battery_level.alias),
49 service.getCharacteristic(battery_level.name),
50 service.getCharacteristic(battery_level.uuid)]))
51 .then(characteristics => {
52 characteristics.forEach(characteristic => {
53 assert_equals(characteristic, null,
54 'Non existent characteristic should return null.');
55 });
56 });
57 }, 'Request for wrong characteristic. Should return null.');
59 promise_test(() => {
60 testRunner.setBluetoothMockDataSet('GenericAccessAdapter');
61 return requestDeviceWithKeyDown({filters: [{services: ['generic_access']}]})
62 .then(device => device.connectGATT())
63 .then(gattServer => gattServer.getPrimaryService('generic_access'))
64 .then(service => Promise.all(
65 [service.getCharacteristic(device_name.alias),
66 service.getCharacteristic(device_name.name),
67 service.getCharacteristic(device_name.uuid)]))
68 .then(characteristics => {
69 characteristics.forEach(characteristic => {
70 assert_equals(
71 characteristic.uuid, device_name.uuid,
72 'Characteristic UUID should be the same as requested UUID.');
73 });
74 });
75 }, 'Request for characteristic. Should return right characteristic');
77 promise_test(() => {
78 testRunner.setBluetoothMockDataSet('GenericAccessAdapter');
79 return requestDeviceWithKeyDown({filters: [{services: ['generic_access']}]})
80 .then(device => device.connectGATT())
81 .then(gattServer => gattServer.getPrimaryService('generic_access'))
82 .then(services => Promise.all(
83 [services.getCharacteristic(device_name.alias),
84 services.getCharacteristic(device_name.alias),
85 services.getCharacteristic(device_name.name),
86 services.getCharacteristic(device_name.name),
87 services.getCharacteristic(device_name.uuid),
88 services.getCharacteristic(device_name.uuid)]))
89 .then(characteristics => {
90 // TODO(ortuno): getCharacteristic should return the same object
91 // if it was created earlier.
92 // https://crbug.com/495270
93 for (var i = 1; i < characteristics.length; i++) {
94 assert_not_equals(
95 characteristics[0], characteristics[i],
96 'Should return the same characteristic as the first call.');
98 });
99 }, 'Calls to get the same characteristic should return the same object.');
101 promise_test(() => {
102 testRunner.setBluetoothMockDataSet('GenericAccessAdapter');
103 return requestDeviceWithKeyDown({filters: [{services: ['generic_access']}]})
104 .then(device => device.connectGATT())
105 .then(gattServer => gattServer.getPrimaryService('generic_access'))
106 .then(service => {
107 return assert_promise_rejects_with_message(
108 service.getCharacteristic('wrong_name'), {
109 name: 'SyntaxError',
110 message: 'Failed to execute \'getCharacteristic\' on ' +
111 '\'BluetoothGATTService\': \Invalid Characteristic name: ' +
112 '\'wrong_name\'. ' +
113 'It must be a valid UUID alias (e.g. 0x1234), ' +
114 'UUID (lowercase hex characters e.g. ' +
115 '\'00001234-0000-1000-8000-00805f9b34fb\'), ' +
116 'or recognized standard name from ' +
117 'https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicsHome.aspx' +
118 ' e.g. \'aerobic_heart_rate_lower_limit\'.'
119 }, 'Wrong Characteristic name passed.');
121 }, 'Wrong Characteristic name. Reject with SyntaxError.');
122 </script>