OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "device/bluetooth/bluetooth_gatt_characteristic.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include "base/macros.h" | |
10 #include "base/run_loop.h" | |
11 #include "build/build_config.h" | |
12 #include "device/bluetooth/bluetooth_gatt_service.h" | |
13 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 #if defined(OS_ANDROID) | |
17 #include "device/bluetooth/test/bluetooth_test_android.h" | |
18 #elif defined(OS_MACOSX) | |
19 #include "device/bluetooth/test/bluetooth_test_mac.h" | |
20 #elif defined(OS_WIN) | |
21 #include "device/bluetooth/test/bluetooth_test_win.h" | |
22 #endif | |
23 | |
24 namespace device { | |
25 | |
26 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) | |
27 class BluetoothGattCharacteristicTest : public BluetoothTest { | |
28 public: | |
29 // Creates adapter_, device_, service_, characteristic1_, & characteristic2_. | |
30 // |properties| will be used for each characteristic. | |
31 void FakeCharacteristicBoilerplate(int properties = 0) { | |
32 InitWithFakeAdapter(); | |
33 StartLowEnergyDiscoverySession(); | |
34 device_ = DiscoverLowEnergyDevice(3); | |
35 device_->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | |
36 GetConnectErrorCallback(Call::NOT_EXPECTED)); | |
37 SimulateGattConnection(device_); | |
38 std::vector<std::string> services; | |
39 std::string uuid("00000000-0000-1000-8000-00805f9b34fb"); | |
40 services.push_back(uuid); | |
41 SimulateGattServicesDiscovered(device_, services); | |
42 ASSERT_EQ(1u, device_->GetGattServices().size()); | |
43 service_ = device_->GetGattServices()[0]; | |
44 SimulateGattCharacteristic(service_, uuid, properties); | |
45 SimulateGattCharacteristic(service_, uuid, properties); | |
46 ASSERT_EQ(2u, service_->GetCharacteristics().size()); | |
47 characteristic1_ = service_->GetCharacteristics()[0]; | |
48 characteristic2_ = service_->GetCharacteristics()[1]; | |
49 ResetEventCounts(); | |
50 } | |
51 | |
52 enum class StartNotifySetupError { | |
53 CHARACTERISTIC_PROPERTIES, | |
54 CONFIG_DESCRIPTOR_MISSING, | |
55 CONFIG_DESCRIPTOR_DUPLICATE, | |
56 SET_NOTIFY, | |
57 WRITE_DESCRIPTOR, | |
58 NONE | |
59 }; | |
60 // Constructs characteristics with |properties|, calls StartNotifySession, | |
61 // and verifies the appropriate |expected_config_descriptor_value| is written. | |
62 // Error scenarios in this boilerplate are tested by setting |error| to the | |
63 // setup stage to test. | |
64 void StartNotifyBoilerplate( | |
65 int properties, | |
66 uint16_t expected_config_descriptor_value, | |
67 StartNotifySetupError error = StartNotifySetupError::NONE) { | |
68 if (error == StartNotifySetupError::CHARACTERISTIC_PROPERTIES) { | |
69 properties = 0; | |
70 } | |
71 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(properties)); | |
72 | |
73 size_t expected_descriptors_count = 0; | |
74 if (error != StartNotifySetupError::CONFIG_DESCRIPTOR_MISSING) { | |
75 SimulateGattDescriptor( | |
76 characteristic1_, | |
77 BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid() | |
78 .canonical_value()); | |
79 expected_descriptors_count++; | |
80 } | |
81 if (error == StartNotifySetupError::CONFIG_DESCRIPTOR_DUPLICATE) { | |
82 SimulateGattDescriptor( | |
83 characteristic1_, | |
84 BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid() | |
85 .canonical_value()); | |
86 expected_descriptors_count++; | |
87 } | |
88 ASSERT_EQ(expected_descriptors_count, | |
89 characteristic1_->GetDescriptors().size()); | |
90 | |
91 if (error == StartNotifySetupError::SET_NOTIFY) { | |
92 SimulateGattCharacteristicSetNotifyWillFailSynchronouslyOnce( | |
93 characteristic1_); | |
94 } | |
95 | |
96 if (error == StartNotifySetupError::WRITE_DESCRIPTOR) { | |
97 SimulateGattDescriptorWriteWillFailSynchronouslyOnce( | |
98 characteristic1_->GetDescriptors()[0]); | |
99 } | |
100 | |
101 if (error != StartNotifySetupError::NONE) { | |
102 characteristic1_->StartNotifySession( | |
103 GetNotifyCallback(Call::NOT_EXPECTED), | |
104 GetGattErrorCallback(Call::EXPECTED)); | |
105 return; | |
106 } | |
107 | |
108 characteristic1_->StartNotifySession( | |
109 GetNotifyCallback(Call::EXPECTED), | |
110 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
111 | |
112 EXPECT_EQ(0, callback_count_); | |
113 SimulateGattNotifySessionStarted(characteristic1_); | |
114 EXPECT_EQ(1, gatt_notify_characteristic_attempts_); | |
115 EXPECT_EQ(1, callback_count_); | |
116 EXPECT_EQ(0, error_callback_count_); | |
117 ASSERT_EQ(1u, notify_sessions_.size()); | |
118 ASSERT_TRUE(notify_sessions_[0]); | |
119 EXPECT_EQ(characteristic1_->GetIdentifier(), | |
120 notify_sessions_[0]->GetCharacteristicIdentifier()); | |
121 EXPECT_TRUE(notify_sessions_[0]->IsActive()); | |
122 | |
123 // Verify the Client Characteristic Configuration descriptor was written to. | |
124 EXPECT_EQ(1, gatt_write_descriptor_attempts_); | |
125 EXPECT_EQ(2u, last_write_value_.size()); | |
126 uint8_t expected_byte0 = expected_config_descriptor_value & 0xFF; | |
127 uint8_t expected_byte1 = (expected_config_descriptor_value >> 8) & 0xFF; | |
128 EXPECT_EQ(expected_byte0, last_write_value_[0]); | |
129 EXPECT_EQ(expected_byte1, last_write_value_[1]); | |
130 } | |
131 | |
132 BluetoothDevice* device_ = nullptr; | |
133 BluetoothGattService* service_ = nullptr; | |
134 BluetoothGattCharacteristic* characteristic1_ = nullptr; | |
135 BluetoothGattCharacteristic* characteristic2_ = nullptr; | |
136 }; | |
137 #endif | |
138 | |
139 #if defined(OS_ANDROID) || defined(OS_WIN) | |
140 TEST_F(BluetoothGattCharacteristicTest, GetIdentifier) { | |
141 InitWithFakeAdapter(); | |
142 StartLowEnergyDiscoverySession(); | |
143 // 2 devices to verify unique IDs across them. | |
144 BluetoothDevice* device1 = DiscoverLowEnergyDevice(3); | |
145 BluetoothDevice* device2 = DiscoverLowEnergyDevice(4); | |
146 device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | |
147 GetConnectErrorCallback(Call::NOT_EXPECTED)); | |
148 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | |
149 GetConnectErrorCallback(Call::NOT_EXPECTED)); | |
150 SimulateGattConnection(device1); | |
151 SimulateGattConnection(device2); | |
152 | |
153 // 3 services (all with same UUID). | |
154 // 1 on the first device (to test characteristic instances across devices). | |
155 // 2 on the second device (to test same device, multiple service instances). | |
156 std::vector<std::string> services; | |
157 std::string uuid = "00000000-0000-1000-8000-00805f9b34fb"; | |
158 services.push_back(uuid); | |
159 SimulateGattServicesDiscovered(device1, services); | |
160 services.push_back(uuid); | |
161 SimulateGattServicesDiscovered(device2, services); | |
162 BluetoothGattService* service1 = device1->GetGattServices()[0]; | |
163 BluetoothGattService* service2 = device2->GetGattServices()[0]; | |
164 BluetoothGattService* service3 = device2->GetGattServices()[1]; | |
165 // 6 characteristics (same UUID), 2 on each service. | |
166 SimulateGattCharacteristic(service1, uuid, /* properties */ 0); | |
167 SimulateGattCharacteristic(service1, uuid, /* properties */ 0); | |
168 SimulateGattCharacteristic(service2, uuid, /* properties */ 0); | |
169 SimulateGattCharacteristic(service2, uuid, /* properties */ 0); | |
170 SimulateGattCharacteristic(service3, uuid, /* properties */ 0); | |
171 SimulateGattCharacteristic(service3, uuid, /* properties */ 0); | |
172 BluetoothGattCharacteristic* char1 = service1->GetCharacteristics()[0]; | |
173 BluetoothGattCharacteristic* char2 = service1->GetCharacteristics()[1]; | |
174 BluetoothGattCharacteristic* char3 = service2->GetCharacteristics()[0]; | |
175 BluetoothGattCharacteristic* char4 = service2->GetCharacteristics()[1]; | |
176 BluetoothGattCharacteristic* char5 = service3->GetCharacteristics()[0]; | |
177 BluetoothGattCharacteristic* char6 = service3->GetCharacteristics()[1]; | |
178 | |
179 // All IDs are unique, even though they have the same UUID. | |
180 EXPECT_NE(char1->GetIdentifier(), char2->GetIdentifier()); | |
181 EXPECT_NE(char1->GetIdentifier(), char3->GetIdentifier()); | |
182 EXPECT_NE(char1->GetIdentifier(), char4->GetIdentifier()); | |
183 EXPECT_NE(char1->GetIdentifier(), char5->GetIdentifier()); | |
184 EXPECT_NE(char1->GetIdentifier(), char6->GetIdentifier()); | |
185 | |
186 EXPECT_NE(char2->GetIdentifier(), char3->GetIdentifier()); | |
187 EXPECT_NE(char2->GetIdentifier(), char4->GetIdentifier()); | |
188 EXPECT_NE(char2->GetIdentifier(), char5->GetIdentifier()); | |
189 EXPECT_NE(char2->GetIdentifier(), char6->GetIdentifier()); | |
190 | |
191 EXPECT_NE(char3->GetIdentifier(), char4->GetIdentifier()); | |
192 EXPECT_NE(char3->GetIdentifier(), char5->GetIdentifier()); | |
193 EXPECT_NE(char3->GetIdentifier(), char6->GetIdentifier()); | |
194 | |
195 EXPECT_NE(char4->GetIdentifier(), char5->GetIdentifier()); | |
196 EXPECT_NE(char4->GetIdentifier(), char6->GetIdentifier()); | |
197 | |
198 EXPECT_NE(char5->GetIdentifier(), char6->GetIdentifier()); | |
199 } | |
200 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
201 | |
202 #if defined(OS_ANDROID) || defined(OS_WIN) | |
203 TEST_F(BluetoothGattCharacteristicTest, GetUUID) { | |
204 InitWithFakeAdapter(); | |
205 StartLowEnergyDiscoverySession(); | |
206 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | |
207 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | |
208 GetConnectErrorCallback(Call::NOT_EXPECTED)); | |
209 SimulateGattConnection(device); | |
210 std::vector<std::string> services; | |
211 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | |
212 SimulateGattServicesDiscovered(device, services); | |
213 BluetoothGattService* service = device->GetGattServices()[0]; | |
214 | |
215 // Create 3 characteristics. Two of them are duplicates. | |
216 std::string uuid_str1("11111111-0000-1000-8000-00805f9b34fb"); | |
217 std::string uuid_str2("22222222-0000-1000-8000-00805f9b34fb"); | |
218 BluetoothUUID uuid1(uuid_str1); | |
219 BluetoothUUID uuid2(uuid_str2); | |
220 SimulateGattCharacteristic(service, uuid_str1, /* properties */ 0); | |
221 SimulateGattCharacteristic(service, uuid_str2, /* properties */ 0); | |
222 SimulateGattCharacteristic(service, uuid_str2, /* properties */ 0); | |
223 BluetoothGattCharacteristic* char1 = service->GetCharacteristics()[0]; | |
224 BluetoothGattCharacteristic* char2 = service->GetCharacteristics()[1]; | |
225 BluetoothGattCharacteristic* char3 = service->GetCharacteristics()[2]; | |
226 | |
227 // Swap as needed to have char1 point to the the characteristic with uuid1. | |
228 if (char2->GetUUID() == uuid1) { | |
229 std::swap(char1, char2); | |
230 } else if (char3->GetUUID() == uuid1) { | |
231 std::swap(char1, char3); | |
232 } | |
233 | |
234 EXPECT_EQ(uuid1, char1->GetUUID()); | |
235 EXPECT_EQ(uuid2, char2->GetUUID()); | |
236 EXPECT_EQ(uuid2, char3->GetUUID()); | |
237 } | |
238 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
239 | |
240 #if defined(OS_ANDROID) || defined(OS_WIN) | |
241 TEST_F(BluetoothGattCharacteristicTest, GetProperties) { | |
242 InitWithFakeAdapter(); | |
243 StartLowEnergyDiscoverySession(); | |
244 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | |
245 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | |
246 GetConnectErrorCallback(Call::NOT_EXPECTED)); | |
247 SimulateGattConnection(device); | |
248 std::vector<std::string> services; | |
249 std::string uuid("00000000-0000-1000-8000-00805f9b34fb"); | |
250 services.push_back(uuid); | |
251 SimulateGattServicesDiscovered(device, services); | |
252 BluetoothGattService* service = device->GetGattServices()[0]; | |
253 | |
254 // Create two characteristics with different properties: | |
255 SimulateGattCharacteristic(service, uuid, /* properties */ 0); | |
256 SimulateGattCharacteristic(service, uuid, /* properties */ 7); | |
257 | |
258 // Read the properties. Because ordering is unknown swap as necessary. | |
259 int properties1 = service->GetCharacteristics()[0]->GetProperties(); | |
260 int properties2 = service->GetCharacteristics()[1]->GetProperties(); | |
261 if (properties2 == 0) | |
262 std::swap(properties1, properties2); | |
263 EXPECT_EQ(0, properties1); | |
264 EXPECT_EQ(7, properties2); | |
265 } | |
266 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
267 | |
268 #if defined(OS_ANDROID) || defined(OS_WIN) | |
269 // Tests GetService. | |
270 TEST_F(BluetoothGattCharacteristicTest, GetService) { | |
271 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | |
272 | |
273 EXPECT_EQ(service_, characteristic1_->GetService()); | |
274 EXPECT_EQ(service_, characteristic2_->GetService()); | |
275 } | |
276 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
277 | |
278 #if defined(OS_ANDROID) || defined(OS_WIN) | |
279 // Tests ReadRemoteCharacteristic and GetValue with empty value buffer. | |
280 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_Empty) { | |
281 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( | |
282 BluetoothGattCharacteristic::PROPERTY_READ)); | |
283 | |
284 characteristic1_->ReadRemoteCharacteristic( | |
285 GetReadValueCallback(Call::EXPECTED), | |
286 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
287 std::vector<uint8_t> empty_vector; | |
288 SimulateGattCharacteristicRead(characteristic1_, empty_vector); | |
289 | |
290 // Duplicate read reported from OS shouldn't cause a problem: | |
291 SimulateGattCharacteristicRead(characteristic1_, empty_vector); | |
292 | |
293 EXPECT_EQ(1, gatt_read_characteristic_attempts_); | |
294 EXPECT_EQ(empty_vector, last_read_value_); | |
295 EXPECT_EQ(empty_vector, characteristic1_->GetValue()); | |
296 } | |
297 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
298 | |
299 #if defined(OS_ANDROID) || defined(OS_WIN) | |
300 // Tests WriteRemoteCharacteristic with empty value buffer. | |
301 TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic_Empty) { | |
302 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( | |
303 BluetoothGattCharacteristic::PROPERTY_WRITE)); | |
304 | |
305 std::vector<uint8_t> empty_vector; | |
306 characteristic1_->WriteRemoteCharacteristic( | |
307 empty_vector, GetCallback(Call::EXPECTED), | |
308 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
309 SimulateGattCharacteristicWrite(characteristic1_); | |
310 EXPECT_EQ(1, gatt_write_characteristic_attempts_); | |
311 | |
312 // Duplicate write reported from OS shouldn't cause a problem: | |
313 SimulateGattCharacteristicWrite(characteristic1_); | |
314 | |
315 EXPECT_EQ(empty_vector, last_write_value_); | |
316 } | |
317 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
318 | |
319 #if defined(OS_ANDROID) || defined(OS_WIN) | |
320 // Tests ReadRemoteCharacteristic completing after Chrome objects are deleted. | |
321 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_AfterDeleted) { | |
322 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( | |
323 BluetoothGattCharacteristic::PROPERTY_READ)); | |
324 | |
325 characteristic1_->ReadRemoteCharacteristic( | |
326 GetReadValueCallback(Call::NOT_EXPECTED), | |
327 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
328 | |
329 RememberCharacteristicForSubsequentAction(characteristic1_); | |
330 DeleteDevice(device_); // TODO(576906) delete only the characteristic. | |
331 | |
332 std::vector<uint8_t> empty_vector; | |
333 SimulateGattCharacteristicRead(/* use remembered characteristic */ nullptr, | |
334 empty_vector); | |
335 EXPECT_TRUE("Did not crash!"); | |
336 } | |
337 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
338 | |
339 #if defined(OS_ANDROID) || defined(OS_WIN) | |
340 // Tests WriteRemoteCharacteristic completing after Chrome objects are deleted. | |
341 TEST_F(BluetoothGattCharacteristicTest, | |
342 WriteRemoteCharacteristic_AfterDeleted) { | |
343 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( | |
344 BluetoothGattCharacteristic::PROPERTY_WRITE)); | |
345 | |
346 std::vector<uint8_t> empty_vector; | |
347 characteristic1_->WriteRemoteCharacteristic( | |
348 empty_vector, GetCallback(Call::NOT_EXPECTED), | |
349 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
350 | |
351 RememberCharacteristicForSubsequentAction(characteristic1_); | |
352 DeleteDevice(device_); // TODO(576906) delete only the characteristic. | |
353 | |
354 SimulateGattCharacteristicWrite(/* use remembered characteristic */ nullptr); | |
355 EXPECT_TRUE("Did not crash!"); | |
356 } | |
357 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
358 | |
359 #if defined(OS_ANDROID) || defined(OS_WIN) | |
360 // Tests ReadRemoteCharacteristic and GetValue with non-empty value buffer. | |
361 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic) { | |
362 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( | |
363 BluetoothGattCharacteristic::PROPERTY_READ)); | |
364 | |
365 characteristic1_->ReadRemoteCharacteristic( | |
366 GetReadValueCallback(Call::EXPECTED), | |
367 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
368 | |
369 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; | |
370 std::vector<uint8_t> test_vector(values, values + arraysize(values)); | |
371 SimulateGattCharacteristicRead(characteristic1_, test_vector); | |
372 | |
373 // Duplicate read reported from OS shouldn't cause a problem: | |
374 std::vector<uint8_t> empty_vector; | |
375 SimulateGattCharacteristicRead(characteristic1_, empty_vector); | |
376 | |
377 EXPECT_EQ(1, gatt_read_characteristic_attempts_); | |
378 EXPECT_EQ(test_vector, last_read_value_); | |
379 EXPECT_EQ(test_vector, characteristic1_->GetValue()); | |
380 } | |
381 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
382 | |
383 #if defined(OS_ANDROID) || defined(OS_WIN) | |
384 // Tests WriteRemoteCharacteristic with non-empty value buffer. | |
385 TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic) { | |
386 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( | |
387 BluetoothGattCharacteristic::PROPERTY_WRITE)); | |
388 | |
389 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; | |
390 std::vector<uint8_t> test_vector(values, values + arraysize(values)); | |
391 characteristic1_->WriteRemoteCharacteristic( | |
392 test_vector, GetCallback(Call::EXPECTED), | |
393 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
394 | |
395 SimulateGattCharacteristicWrite(characteristic1_); | |
396 | |
397 EXPECT_EQ(1, gatt_write_characteristic_attempts_); | |
398 EXPECT_EQ(test_vector, last_write_value_); | |
399 } | |
400 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
401 | |
402 #if defined(OS_ANDROID) || defined(OS_WIN) | |
403 // Tests ReadRemoteCharacteristic and GetValue multiple times. | |
404 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_Twice) { | |
405 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( | |
406 BluetoothGattCharacteristic::PROPERTY_READ)); | |
407 | |
408 characteristic1_->ReadRemoteCharacteristic( | |
409 GetReadValueCallback(Call::EXPECTED), | |
410 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
411 | |
412 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; | |
413 std::vector<uint8_t> test_vector(values, values + arraysize(values)); | |
414 SimulateGattCharacteristicRead(characteristic1_, test_vector); | |
415 EXPECT_EQ(1, gatt_read_characteristic_attempts_); | |
416 EXPECT_EQ(1, callback_count_); | |
417 EXPECT_EQ(0, error_callback_count_); | |
418 EXPECT_EQ(test_vector, last_read_value_); | |
419 EXPECT_EQ(test_vector, characteristic1_->GetValue()); | |
420 | |
421 // Read again, with different value: | |
422 ResetEventCounts(); | |
423 characteristic1_->ReadRemoteCharacteristic( | |
424 GetReadValueCallback(Call::EXPECTED), | |
425 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
426 std::vector<uint8_t> empty_vector; | |
427 SimulateGattCharacteristicRead(characteristic1_, empty_vector); | |
428 EXPECT_EQ(1, gatt_read_characteristic_attempts_); | |
429 EXPECT_EQ(1, callback_count_); | |
430 EXPECT_EQ(0, error_callback_count_); | |
431 EXPECT_EQ(empty_vector, last_read_value_); | |
432 EXPECT_EQ(empty_vector, characteristic1_->GetValue()); | |
433 } | |
434 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
435 | |
436 #if defined(OS_ANDROID) || defined(OS_WIN) | |
437 // Tests WriteRemoteCharacteristic multiple times. | |
438 TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic_Twice) { | |
439 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( | |
440 BluetoothGattCharacteristic::PROPERTY_WRITE)); | |
441 | |
442 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; | |
443 std::vector<uint8_t> test_vector(values, values + arraysize(values)); | |
444 characteristic1_->WriteRemoteCharacteristic( | |
445 test_vector, GetCallback(Call::EXPECTED), | |
446 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
447 | |
448 SimulateGattCharacteristicWrite(characteristic1_); | |
449 EXPECT_EQ(1, gatt_write_characteristic_attempts_); | |
450 EXPECT_EQ(1, callback_count_); | |
451 EXPECT_EQ(0, error_callback_count_); | |
452 EXPECT_EQ(test_vector, last_write_value_); | |
453 | |
454 // Write again, with different value: | |
455 ResetEventCounts(); | |
456 std::vector<uint8_t> empty_vector; | |
457 characteristic1_->WriteRemoteCharacteristic( | |
458 empty_vector, GetCallback(Call::EXPECTED), | |
459 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
460 | |
461 SimulateGattCharacteristicWrite(characteristic1_); | |
462 EXPECT_EQ(1, gatt_write_characteristic_attempts_); | |
463 EXPECT_EQ(1, callback_count_); | |
464 EXPECT_EQ(0, error_callback_count_); | |
465 EXPECT_EQ(empty_vector, last_write_value_); | |
466 } | |
467 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
468 | |
469 #if defined(OS_ANDROID) || defined(OS_WIN) | |
470 // Tests ReadRemoteCharacteristic on two characteristics. | |
471 TEST_F(BluetoothGattCharacteristicTest, | |
472 ReadRemoteCharacteristic_MultipleCharacteristics) { | |
473 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( | |
474 BluetoothGattCharacteristic::PROPERTY_READ)); | |
475 | |
476 characteristic1_->ReadRemoteCharacteristic( | |
477 GetReadValueCallback(Call::EXPECTED), | |
478 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
479 characteristic2_->ReadRemoteCharacteristic( | |
480 GetReadValueCallback(Call::EXPECTED), | |
481 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
482 EXPECT_EQ(0, callback_count_); | |
483 EXPECT_EQ(0, error_callback_count_); | |
484 | |
485 std::vector<uint8_t> test_vector1; | |
486 test_vector1.push_back(111); | |
487 SimulateGattCharacteristicRead(characteristic1_, test_vector1); | |
488 EXPECT_EQ(test_vector1, last_read_value_); | |
489 | |
490 std::vector<uint8_t> test_vector2; | |
491 test_vector2.push_back(222); | |
492 SimulateGattCharacteristicRead(characteristic2_, test_vector2); | |
493 EXPECT_EQ(test_vector2, last_read_value_); | |
494 | |
495 EXPECT_EQ(2, gatt_read_characteristic_attempts_); | |
496 EXPECT_EQ(2, callback_count_); | |
497 EXPECT_EQ(0, error_callback_count_); | |
498 EXPECT_EQ(test_vector1, characteristic1_->GetValue()); | |
499 EXPECT_EQ(test_vector2, characteristic2_->GetValue()); | |
500 } | |
501 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
502 | |
503 #if defined(OS_ANDROID) || defined(OS_WIN) | |
504 // Tests WriteRemoteCharacteristic on two characteristics. | |
505 TEST_F(BluetoothGattCharacteristicTest, | |
506 WriteRemoteCharacteristic_MultipleCharacteristics) { | |
507 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( | |
508 BluetoothGattCharacteristic::PROPERTY_WRITE)); | |
509 | |
510 std::vector<uint8_t> test_vector1; | |
511 test_vector1.push_back(111); | |
512 characteristic1_->WriteRemoteCharacteristic( | |
513 test_vector1, GetCallback(Call::EXPECTED), | |
514 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
515 #ifdef OS_ANDROID | |
516 EXPECT_EQ(test_vector1, last_write_value_); | |
517 #endif | |
518 | |
519 std::vector<uint8_t> test_vector2; | |
520 test_vector2.push_back(222); | |
521 characteristic2_->WriteRemoteCharacteristic( | |
522 test_vector2, GetCallback(Call::EXPECTED), | |
523 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
524 #ifdef OS_ANDROID | |
525 EXPECT_EQ(test_vector2, last_write_value_); | |
526 #endif | |
527 | |
528 EXPECT_EQ(0, callback_count_); | |
529 EXPECT_EQ(0, error_callback_count_); | |
530 | |
531 SimulateGattCharacteristicWrite(characteristic1_); | |
532 #ifndef OS_ANDROID | |
533 EXPECT_EQ(test_vector1, last_write_value_); | |
534 #endif | |
535 | |
536 SimulateGattCharacteristicWrite(characteristic2_); | |
537 #ifndef OS_ANDROID | |
538 EXPECT_EQ(test_vector2, last_write_value_); | |
539 #endif | |
540 | |
541 EXPECT_EQ(2, gatt_write_characteristic_attempts_); | |
542 EXPECT_EQ(2, callback_count_); | |
543 EXPECT_EQ(0, error_callback_count_); | |
544 | |
545 // TODO(591740): Remove if define for OS_ANDROID in this test. | |
546 } | |
547 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
548 | |
549 #if defined(OS_ANDROID) || defined(OS_WIN) | |
550 // Tests ReadRemoteCharacteristic asynchronous error. | |
551 TEST_F(BluetoothGattCharacteristicTest, ReadError) { | |
552 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( | |
553 BluetoothGattCharacteristic::PROPERTY_READ)); | |
554 | |
555 characteristic1_->ReadRemoteCharacteristic( | |
556 GetReadValueCallback(Call::NOT_EXPECTED), | |
557 GetGattErrorCallback(Call::EXPECTED)); | |
558 SimulateGattCharacteristicReadError( | |
559 characteristic1_, BluetoothGattService::GATT_ERROR_INVALID_LENGTH); | |
560 SimulateGattCharacteristicReadError(characteristic1_, | |
561 BluetoothGattService::GATT_ERROR_FAILED); | |
562 EXPECT_EQ(BluetoothGattService::GATT_ERROR_INVALID_LENGTH, | |
563 last_gatt_error_code_); | |
564 } | |
565 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
566 | |
567 #if defined(OS_ANDROID) || defined(OS_WIN) | |
568 // Tests WriteRemoteCharacteristic asynchronous error. | |
569 TEST_F(BluetoothGattCharacteristicTest, WriteError) { | |
570 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( | |
571 BluetoothGattCharacteristic::PROPERTY_WRITE)); | |
572 | |
573 std::vector<uint8_t> empty_vector; | |
574 characteristic1_->WriteRemoteCharacteristic( | |
575 empty_vector, GetCallback(Call::NOT_EXPECTED), | |
576 GetGattErrorCallback(Call::EXPECTED)); | |
577 SimulateGattCharacteristicWriteError( | |
578 characteristic1_, BluetoothGattService::GATT_ERROR_INVALID_LENGTH); | |
579 SimulateGattCharacteristicWriteError(characteristic1_, | |
580 BluetoothGattService::GATT_ERROR_FAILED); | |
581 | |
582 EXPECT_EQ(BluetoothGattService::GATT_ERROR_INVALID_LENGTH, | |
583 last_gatt_error_code_); | |
584 } | |
585 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
586 | |
587 #if defined(OS_ANDROID) | |
588 // Tests ReadRemoteCharacteristic synchronous error. | |
589 TEST_F(BluetoothGattCharacteristicTest, ReadSynchronousError) { | |
590 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | |
591 | |
592 SimulateGattCharacteristicReadWillFailSynchronouslyOnce(characteristic1_); | |
593 characteristic1_->ReadRemoteCharacteristic( | |
594 GetReadValueCallback(Call::NOT_EXPECTED), | |
595 GetGattErrorCallback(Call::EXPECTED)); | |
596 EXPECT_EQ(0, gatt_read_characteristic_attempts_); | |
597 base::RunLoop().RunUntilIdle(); | |
598 EXPECT_EQ(0, callback_count_); | |
599 EXPECT_EQ(1, error_callback_count_); | |
600 EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_); | |
601 | |
602 // After failing once, can succeed: | |
603 ResetEventCounts(); | |
604 characteristic1_->ReadRemoteCharacteristic( | |
605 GetReadValueCallback(Call::EXPECTED), | |
606 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
607 EXPECT_EQ(1, gatt_read_characteristic_attempts_); | |
608 std::vector<uint8_t> empty_vector; | |
609 SimulateGattCharacteristicRead(characteristic1_, empty_vector); | |
610 EXPECT_EQ(1, callback_count_); | |
611 EXPECT_EQ(0, error_callback_count_); | |
612 } | |
613 #endif // defined(OS_ANDROID) | |
614 | |
615 #if defined(OS_ANDROID) | |
616 // Tests WriteRemoteCharacteristic synchronous error. | |
617 TEST_F(BluetoothGattCharacteristicTest, WriteSynchronousError) { | |
618 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | |
619 | |
620 SimulateGattCharacteristicWriteWillFailSynchronouslyOnce(characteristic1_); | |
621 std::vector<uint8_t> empty_vector; | |
622 characteristic1_->WriteRemoteCharacteristic( | |
623 empty_vector, GetCallback(Call::NOT_EXPECTED), | |
624 GetGattErrorCallback(Call::EXPECTED)); | |
625 EXPECT_EQ(0, gatt_write_characteristic_attempts_); | |
626 base::RunLoop().RunUntilIdle(); | |
627 EXPECT_EQ(0, callback_count_); | |
628 EXPECT_EQ(1, error_callback_count_); | |
629 EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_); | |
630 | |
631 // After failing once, can succeed: | |
632 ResetEventCounts(); | |
633 characteristic1_->WriteRemoteCharacteristic( | |
634 empty_vector, GetCallback(Call::EXPECTED), | |
635 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
636 EXPECT_EQ(1, gatt_write_characteristic_attempts_); | |
637 SimulateGattCharacteristicWrite(characteristic1_); | |
638 EXPECT_EQ(1, callback_count_); | |
639 EXPECT_EQ(0, error_callback_count_); | |
640 } | |
641 #endif // defined(OS_ANDROID) | |
642 | |
643 #if defined(OS_ANDROID) || defined(OS_WIN) | |
644 // Tests ReadRemoteCharacteristic error with a pending read operation. | |
645 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_ReadPending) { | |
646 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( | |
647 BluetoothGattCharacteristic::PROPERTY_READ)); | |
648 | |
649 characteristic1_->ReadRemoteCharacteristic( | |
650 GetReadValueCallback(Call::EXPECTED), | |
651 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
652 characteristic1_->ReadRemoteCharacteristic( | |
653 GetReadValueCallback(Call::NOT_EXPECTED), | |
654 GetGattErrorCallback(Call::EXPECTED)); | |
655 | |
656 base::RunLoop().RunUntilIdle(); | |
657 | |
658 EXPECT_EQ(0, callback_count_); | |
659 EXPECT_EQ(1, error_callback_count_); | |
660 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, | |
661 last_gatt_error_code_); | |
662 | |
663 // Initial read should still succeed: | |
664 ResetEventCounts(); | |
665 std::vector<uint8_t> empty_vector; | |
666 SimulateGattCharacteristicRead(characteristic1_, empty_vector); | |
667 EXPECT_EQ(1, callback_count_); | |
668 EXPECT_EQ(0, error_callback_count_); | |
669 } | |
670 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
671 | |
672 #if defined(OS_ANDROID) || defined(OS_WIN) | |
673 // Tests WriteRemoteCharacteristic error with a pending write operation. | |
674 TEST_F(BluetoothGattCharacteristicTest, | |
675 WriteRemoteCharacteristic_WritePending) { | |
676 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( | |
677 BluetoothGattCharacteristic::PROPERTY_WRITE)); | |
678 | |
679 std::vector<uint8_t> empty_vector; | |
680 characteristic1_->WriteRemoteCharacteristic( | |
681 empty_vector, GetCallback(Call::EXPECTED), | |
682 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
683 characteristic1_->WriteRemoteCharacteristic( | |
684 empty_vector, GetCallback(Call::NOT_EXPECTED), | |
685 GetGattErrorCallback(Call::EXPECTED)); | |
686 | |
687 base::RunLoop().RunUntilIdle(); | |
688 | |
689 EXPECT_EQ(0, callback_count_); | |
690 EXPECT_EQ(1, error_callback_count_); | |
691 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, | |
692 last_gatt_error_code_); | |
693 | |
694 // Initial write should still succeed: | |
695 ResetEventCounts(); | |
696 SimulateGattCharacteristicWrite(characteristic1_); | |
697 EXPECT_EQ(1, callback_count_); | |
698 EXPECT_EQ(0, error_callback_count_); | |
699 } | |
700 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
701 | |
702 #if defined(OS_ANDROID) || defined(OS_WIN) | |
703 // Tests ReadRemoteCharacteristic error with a pending write operation. | |
704 TEST_F(BluetoothGattCharacteristicTest, ReadRemoteCharacteristic_WritePending) { | |
705 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( | |
706 BluetoothGattCharacteristic::PROPERTY_READ | | |
707 BluetoothGattCharacteristic::PROPERTY_WRITE)); | |
708 | |
709 std::vector<uint8_t> empty_vector; | |
710 characteristic1_->WriteRemoteCharacteristic( | |
711 empty_vector, GetCallback(Call::EXPECTED), | |
712 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
713 characteristic1_->ReadRemoteCharacteristic( | |
714 GetReadValueCallback(Call::NOT_EXPECTED), | |
715 GetGattErrorCallback(Call::EXPECTED)); | |
716 | |
717 base::RunLoop().RunUntilIdle(); | |
718 | |
719 EXPECT_EQ(0, callback_count_); | |
720 EXPECT_EQ(1, error_callback_count_); | |
721 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, | |
722 last_gatt_error_code_); | |
723 | |
724 // Initial write should still succeed: | |
725 ResetEventCounts(); | |
726 SimulateGattCharacteristicWrite(characteristic1_); | |
727 EXPECT_EQ(1, callback_count_); | |
728 EXPECT_EQ(0, error_callback_count_); | |
729 } | |
730 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
731 | |
732 #if defined(OS_ANDROID) || defined(OS_WIN) | |
733 // Tests WriteRemoteCharacteristic error with a pending Read operation. | |
734 TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic_ReadPending) { | |
735 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( | |
736 BluetoothGattCharacteristic::PROPERTY_READ | | |
737 BluetoothGattCharacteristic::PROPERTY_WRITE)); | |
738 | |
739 std::vector<uint8_t> empty_vector; | |
740 characteristic1_->ReadRemoteCharacteristic( | |
741 GetReadValueCallback(Call::EXPECTED), | |
742 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
743 characteristic1_->WriteRemoteCharacteristic( | |
744 empty_vector, GetCallback(Call::NOT_EXPECTED), | |
745 GetGattErrorCallback(Call::EXPECTED)); | |
746 base::RunLoop().RunUntilIdle(); | |
747 | |
748 EXPECT_EQ(0, callback_count_); | |
749 EXPECT_EQ(1, error_callback_count_); | |
750 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, | |
751 last_gatt_error_code_); | |
752 | |
753 // Initial read should still succeed: | |
754 ResetEventCounts(); | |
755 SimulateGattCharacteristicRead(characteristic1_, empty_vector); | |
756 EXPECT_EQ(1, callback_count_); | |
757 EXPECT_EQ(0, error_callback_count_); | |
758 } | |
759 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
760 | |
761 #if defined(OS_ANDROID) || defined(OS_WIN) | |
762 // StartNotifySession fails if characteristic doesn't have Notify or Indicate | |
763 // property. | |
764 TEST_F(BluetoothGattCharacteristicTest, StartNotifySession_NoNotifyOrIndicate) { | |
765 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( | |
766 /* properties: NOTIFY */ 0x10, | |
767 /* expected_config_descriptor_value: NOTIFY */ 1, | |
768 StartNotifySetupError::CHARACTERISTIC_PROPERTIES)); | |
769 | |
770 EXPECT_EQ(0, gatt_notify_characteristic_attempts_); | |
771 | |
772 // The expected error callback is asynchronous: | |
773 EXPECT_EQ(0, error_callback_count_); | |
774 base::RunLoop().RunUntilIdle(); | |
775 EXPECT_EQ(1, error_callback_count_); | |
776 EXPECT_EQ(BluetoothGattService::GATT_ERROR_NOT_SUPPORTED, | |
777 last_gatt_error_code_); | |
778 } | |
779 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
780 | |
781 #if defined(OS_ANDROID) || defined(OS_WIN) | |
782 // StartNotifySession fails if the characteristic is missing the Client | |
783 // Characteristic Configuration descriptor. | |
784 TEST_F(BluetoothGattCharacteristicTest, StartNotifySession_NoConfigDescriptor) { | |
785 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( | |
786 /* properties: NOTIFY */ 0x10, | |
787 /* expected_config_descriptor_value: NOTIFY */ 1, | |
788 StartNotifySetupError::CONFIG_DESCRIPTOR_MISSING)); | |
789 | |
790 EXPECT_EQ(0, gatt_notify_characteristic_attempts_); | |
791 | |
792 // The expected error callback is asynchronous: | |
793 EXPECT_EQ(0, error_callback_count_); | |
794 base::RunLoop().RunUntilIdle(); | |
795 EXPECT_EQ(1, error_callback_count_); | |
796 EXPECT_EQ(BluetoothGattService::GATT_ERROR_NOT_SUPPORTED, | |
797 last_gatt_error_code_); | |
798 } | |
799 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
800 | |
801 #if defined(OS_ANDROID) || defined(OS_WIN) | |
802 // StartNotifySession fails if the characteristic has multiple Client | |
803 // Characteristic Configuration descriptors. | |
804 TEST_F(BluetoothGattCharacteristicTest, | |
805 StartNotifySession_MultipleConfigDescriptor) { | |
806 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( | |
807 /* properties: NOTIFY */ 0x10, | |
808 /* expected_config_descriptor_value: NOTIFY */ 1, | |
809 StartNotifySetupError::CONFIG_DESCRIPTOR_DUPLICATE)); | |
810 | |
811 EXPECT_EQ(0, gatt_notify_characteristic_attempts_); | |
812 | |
813 // The expected error callback is asynchronous: | |
814 EXPECT_EQ(0, error_callback_count_); | |
815 base::RunLoop().RunUntilIdle(); | |
816 EXPECT_EQ(1, error_callback_count_); | |
817 EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_); | |
818 } | |
819 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
820 | |
821 #if defined(OS_ANDROID) | |
822 // StartNotifySession fails synchronously when failing to set a characteristic | |
823 // to enable notifications. | |
824 // Android: This is mBluetoothGatt.setCharacteristicNotification failing. | |
825 // Windows: Synchronous Test Not Applicable: OS calls are all made | |
826 // asynchronously from BluetoothTaskManagerWin. | |
827 TEST_F(BluetoothGattCharacteristicTest, | |
828 StartNotifySession_FailToSetCharacteristicNotification) { | |
829 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( | |
830 /* properties: NOTIFY */ 0x10, | |
831 /* expected_config_descriptor_value: NOTIFY */ 1, | |
832 StartNotifySetupError::SET_NOTIFY)); | |
833 | |
834 // The expected error callback is asynchronous: | |
835 EXPECT_EQ(0, error_callback_count_); | |
836 base::RunLoop().RunUntilIdle(); | |
837 EXPECT_EQ(1, error_callback_count_); | |
838 | |
839 EXPECT_EQ(0, gatt_notify_characteristic_attempts_); | |
840 ASSERT_EQ(0u, notify_sessions_.size()); | |
841 } | |
842 #endif // defined(OS_ANDROID) | |
843 | |
844 #if defined(OS_ANDROID) | |
845 // Tests StartNotifySession descriptor write synchronous failure. | |
846 // Windows: Synchronous Test Not Applicable: OS calls are all made | |
847 // asynchronously from BluetoothTaskManagerWin. | |
848 TEST_F(BluetoothGattCharacteristicTest, | |
849 StartNotifySession_WriteDescriptorSynchronousError) { | |
850 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( | |
851 /* properties: NOTIFY */ 0x10, | |
852 /* expected_config_descriptor_value: NOTIFY */ 1, | |
853 StartNotifySetupError::WRITE_DESCRIPTOR)); | |
854 | |
855 // The expected error callback is asynchronous: | |
856 EXPECT_EQ(0, error_callback_count_); | |
857 base::RunLoop().RunUntilIdle(); | |
858 EXPECT_EQ(1, error_callback_count_); | |
859 | |
860 EXPECT_EQ(1, gatt_notify_characteristic_attempts_); | |
861 ASSERT_EQ(0u, notify_sessions_.size()); | |
862 } | |
863 #endif // defined(OS_ANDROID) | |
864 | |
865 #if defined(OS_ANDROID) || defined(OS_WIN) | |
866 // Tests StartNotifySession success on a characteristic enabling Notify. | |
867 TEST_F(BluetoothGattCharacteristicTest, StartNotifySession) { | |
868 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( | |
869 /* properties: NOTIFY */ 0x10, | |
870 /* expected_config_descriptor_value: NOTIFY */ 1)); | |
871 } | |
872 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
873 | |
874 #if defined(OS_ANDROID) || defined(OS_WIN) | |
875 // Tests StartNotifySession success on a characteristic enabling Indicate. | |
876 TEST_F(BluetoothGattCharacteristicTest, StartNotifySession_OnIndicate) { | |
877 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( | |
878 /* properties: INDICATE */ 0x20, | |
879 /* expected_config_descriptor_value: INDICATE */ 2)); | |
880 } | |
881 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
882 | |
883 #if defined(OS_ANDROID) || defined(OS_WIN) | |
884 // Tests StartNotifySession success on a characteristic enabling Notify & | |
885 // Indicate. | |
886 TEST_F(BluetoothGattCharacteristicTest, | |
887 StartNotifySession_OnNotifyAndIndicate) { | |
888 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( | |
889 /* properties: NOTIFY and INDICATE bits set */ 0x30, | |
890 /* expected_config_descriptor_value: NOTIFY */ 1)); | |
891 } | |
892 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
893 | |
894 #if defined(OS_ANDROID) || defined(OS_WIN) | |
895 // Tests multiple StartNotifySession success. | |
896 TEST_F(BluetoothGattCharacteristicTest, StartNotifySession_Multiple) { | |
897 ASSERT_NO_FATAL_FAILURE( | |
898 FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10)); | |
899 SimulateGattDescriptor( | |
900 characteristic1_, | |
901 BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid() | |
902 .canonical_value()); | |
903 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size()); | |
904 | |
905 characteristic1_->StartNotifySession( | |
906 GetNotifyCallback(Call::EXPECTED), | |
907 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
908 characteristic1_->StartNotifySession( | |
909 GetNotifyCallback(Call::EXPECTED), | |
910 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
911 EXPECT_EQ(0, callback_count_); | |
912 SimulateGattNotifySessionStarted(characteristic1_); | |
913 EXPECT_EQ(1, gatt_notify_characteristic_attempts_); | |
914 EXPECT_EQ(2, callback_count_); | |
915 EXPECT_EQ(0, error_callback_count_); | |
916 ASSERT_EQ(2u, notify_sessions_.size()); | |
917 ASSERT_TRUE(notify_sessions_[0]); | |
918 ASSERT_TRUE(notify_sessions_[1]); | |
919 EXPECT_EQ(characteristic1_->GetIdentifier(), | |
920 notify_sessions_[0]->GetCharacteristicIdentifier()); | |
921 EXPECT_EQ(characteristic1_->GetIdentifier(), | |
922 notify_sessions_[1]->GetCharacteristicIdentifier()); | |
923 EXPECT_TRUE(notify_sessions_[0]->IsActive()); | |
924 EXPECT_TRUE(notify_sessions_[1]->IsActive()); | |
925 } | |
926 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
927 | |
928 #if defined(OS_ANDROID) | |
929 // Tests multiple StartNotifySessions pending and then an error. | |
930 TEST_F(BluetoothGattCharacteristicTest, StartNotifySessionError_Multiple) { | |
931 ASSERT_NO_FATAL_FAILURE( | |
932 FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10)); | |
933 SimulateGattDescriptor( | |
934 characteristic1_, | |
935 BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid() | |
936 .canonical_value()); | |
937 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size()); | |
938 | |
939 characteristic1_->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED), | |
940 GetGattErrorCallback(Call::EXPECTED)); | |
941 characteristic1_->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED), | |
942 GetGattErrorCallback(Call::EXPECTED)); | |
943 EXPECT_EQ(1, gatt_notify_characteristic_attempts_); | |
944 EXPECT_EQ(0, callback_count_); | |
945 SimulateGattNotifySessionStartError(characteristic1_, | |
946 BluetoothGattService::GATT_ERROR_FAILED); | |
947 EXPECT_EQ(0, callback_count_); | |
948 EXPECT_EQ(2, error_callback_count_); | |
949 ASSERT_EQ(0u, notify_sessions_.size()); | |
950 EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_); | |
951 } | |
952 #endif // defined(OS_ANDROID) | |
953 | |
954 #if defined(OS_ANDROID) | |
955 // Tests StartNotifySession completing after chrome objects are deleted. | |
956 TEST_F(BluetoothGattCharacteristicTest, StartNotifySession_AfterDeleted) { | |
957 ASSERT_NO_FATAL_FAILURE( | |
958 FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10)); | |
959 SimulateGattDescriptor( | |
960 characteristic1_, | |
961 BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid() | |
962 .canonical_value()); | |
963 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size()); | |
964 | |
965 characteristic1_->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED), | |
966 GetGattErrorCallback(Call::EXPECTED)); | |
967 EXPECT_EQ(1, gatt_notify_characteristic_attempts_); | |
968 EXPECT_EQ(0, callback_count_); | |
969 | |
970 RememberCharacteristicForSubsequentAction(characteristic1_); | |
971 RememberCCCDescriptorForSubsequentAction(characteristic1_); | |
972 DeleteDevice(device_); // TODO(576906) delete only the characteristic. | |
973 | |
974 SimulateGattNotifySessionStarted(/* use remembered characteristic */ nullptr); | |
975 EXPECT_EQ(0, callback_count_); | |
976 EXPECT_EQ(1, error_callback_count_); | |
977 ASSERT_EQ(0u, notify_sessions_.size()); | |
978 EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_); | |
979 } | |
980 #endif // defined(OS_ANDROID) | |
981 | |
982 #if defined(OS_ANDROID) || defined(OS_WIN) | |
983 // Tests Characteristic Value changes during a Notify Session. | |
984 TEST_F(BluetoothGattCharacteristicTest, GattCharacteristicValueChanged) { | |
985 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( | |
986 /* properties: NOTIFY */ 0x10, | |
987 /* expected_config_descriptor_value: NOTIFY */ 1)); | |
988 | |
989 TestBluetoothAdapterObserver observer(adapter_); | |
990 | |
991 std::vector<uint8_t> test_vector1, test_vector2; | |
992 test_vector1.push_back(111); | |
993 test_vector2.push_back(222); | |
994 | |
995 SimulateGattCharacteristicChanged(characteristic1_, test_vector1); | |
996 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); | |
997 EXPECT_EQ(test_vector1, characteristic1_->GetValue()); | |
998 | |
999 SimulateGattCharacteristicChanged(characteristic1_, test_vector2); | |
1000 EXPECT_EQ(2, observer.gatt_characteristic_value_changed_count()); | |
1001 EXPECT_EQ(test_vector2, characteristic1_->GetValue()); | |
1002 } | |
1003 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
1004 | |
1005 #if defined(OS_ANDROID) || defined(OS_WIN) | |
1006 // Tests Characteristic Value changing after a Notify Session and objects being | |
1007 // destroyed. | |
1008 TEST_F(BluetoothGattCharacteristicTest, | |
1009 GattCharacteristicValueChanged_AfterDeleted) { | |
1010 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( | |
1011 /* properties: NOTIFY */ 0x10, | |
1012 /* expected_config_descriptor_value: NOTIFY */ 1)); | |
1013 TestBluetoothAdapterObserver observer(adapter_); | |
1014 | |
1015 RememberCharacteristicForSubsequentAction(characteristic1_); | |
1016 DeleteDevice(device_); // TODO(576906) delete only the characteristic. | |
1017 | |
1018 std::vector<uint8_t> empty_vector; | |
1019 SimulateGattCharacteristicChanged(/* use remembered characteristic */ nullptr, | |
1020 empty_vector); | |
1021 EXPECT_TRUE("Did not crash!"); | |
1022 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count()); | |
1023 } | |
1024 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
1025 | |
1026 #if defined(OS_ANDROID) || defined(OS_WIN) | |
1027 TEST_F(BluetoothGattCharacteristicTest, GetDescriptors_FindNone) { | |
1028 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | |
1029 | |
1030 EXPECT_EQ(0u, characteristic1_->GetDescriptors().size()); | |
1031 } | |
1032 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
1033 | |
1034 #if defined(OS_ANDROID) || defined(OS_WIN) | |
1035 TEST_F(BluetoothGattCharacteristicTest, GetDescriptors_and_GetDescriptor) { | |
1036 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | |
1037 | |
1038 // Add several Descriptors: | |
1039 BluetoothUUID uuid1("11111111-0000-1000-8000-00805f9b34fb"); | |
1040 BluetoothUUID uuid2("22222222-0000-1000-8000-00805f9b34fb"); | |
1041 BluetoothUUID uuid3("33333333-0000-1000-8000-00805f9b34fb"); | |
1042 BluetoothUUID uuid4("44444444-0000-1000-8000-00805f9b34fb"); | |
1043 SimulateGattDescriptor(characteristic1_, uuid1.canonical_value()); | |
1044 SimulateGattDescriptor(characteristic1_, uuid2.canonical_value()); | |
1045 SimulateGattDescriptor(characteristic2_, uuid3.canonical_value()); | |
1046 SimulateGattDescriptor(characteristic2_, uuid4.canonical_value()); | |
1047 | |
1048 // Verify that GetDescriptor can retrieve descriptors again by ID, | |
1049 // and that the same Descriptor is returned when searched by ID. | |
1050 EXPECT_EQ(2u, characteristic1_->GetDescriptors().size()); | |
1051 EXPECT_EQ(2u, characteristic2_->GetDescriptors().size()); | |
1052 std::string c1_id1 = characteristic1_->GetDescriptors()[0]->GetIdentifier(); | |
1053 std::string c1_id2 = characteristic1_->GetDescriptors()[1]->GetIdentifier(); | |
1054 std::string c2_id1 = characteristic2_->GetDescriptors()[0]->GetIdentifier(); | |
1055 std::string c2_id2 = characteristic2_->GetDescriptors()[1]->GetIdentifier(); | |
1056 BluetoothUUID c1_uuid1 = characteristic1_->GetDescriptors()[0]->GetUUID(); | |
1057 BluetoothUUID c1_uuid2 = characteristic1_->GetDescriptors()[1]->GetUUID(); | |
1058 BluetoothUUID c2_uuid1 = characteristic2_->GetDescriptors()[0]->GetUUID(); | |
1059 BluetoothUUID c2_uuid2 = characteristic2_->GetDescriptors()[1]->GetUUID(); | |
1060 EXPECT_EQ(c1_uuid1, characteristic1_->GetDescriptor(c1_id1)->GetUUID()); | |
1061 EXPECT_EQ(c1_uuid2, characteristic1_->GetDescriptor(c1_id2)->GetUUID()); | |
1062 EXPECT_EQ(c2_uuid1, characteristic2_->GetDescriptor(c2_id1)->GetUUID()); | |
1063 EXPECT_EQ(c2_uuid2, characteristic2_->GetDescriptor(c2_id2)->GetUUID()); | |
1064 | |
1065 // GetDescriptors & GetDescriptor return the same object for the same ID: | |
1066 EXPECT_EQ(characteristic1_->GetDescriptors()[0], | |
1067 characteristic1_->GetDescriptor(c1_id1)); | |
1068 EXPECT_EQ(characteristic1_->GetDescriptor(c1_id1), | |
1069 characteristic1_->GetDescriptor(c1_id1)); | |
1070 | |
1071 // Characteristic 1 has descriptor uuids 1 and 2 (we don't know the order). | |
1072 EXPECT_TRUE(c1_uuid1 == uuid1 || c1_uuid2 == uuid1); | |
1073 EXPECT_TRUE(c1_uuid1 == uuid2 || c1_uuid2 == uuid2); | |
1074 // ... but not uuid 3 | |
1075 EXPECT_FALSE(c1_uuid1 == uuid3 || c1_uuid2 == uuid3); | |
1076 } | |
1077 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
1078 | |
1079 #if defined(OS_ANDROID) || defined(OS_WIN) | |
1080 TEST_F(BluetoothGattCharacteristicTest, GetDescriptorsByUUID) { | |
1081 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | |
1082 | |
1083 // Add several Descriptors: | |
1084 BluetoothUUID id1("11111111-0000-1000-8000-00805f9b34fb"); | |
1085 BluetoothUUID id2("22222222-0000-1000-8000-00805f9b34fb"); | |
1086 BluetoothUUID id3("33333333-0000-1000-8000-00805f9b34fb"); | |
1087 SimulateGattDescriptor(characteristic1_, id1.canonical_value()); | |
1088 SimulateGattDescriptor(characteristic1_, id2.canonical_value()); | |
1089 SimulateGattDescriptor(characteristic2_, id3.canonical_value()); | |
1090 SimulateGattDescriptor(characteristic2_, id3.canonical_value()); | |
1091 | |
1092 EXPECT_NE(characteristic2_->GetDescriptorsByUUID(id3).at(0)->GetIdentifier(), | |
1093 characteristic2_->GetDescriptorsByUUID(id3).at(1)->GetIdentifier()); | |
1094 | |
1095 EXPECT_EQ(id1, characteristic1_->GetDescriptorsByUUID(id1).at(0)->GetUUID()); | |
1096 EXPECT_EQ(id2, characteristic1_->GetDescriptorsByUUID(id2).at(0)->GetUUID()); | |
1097 EXPECT_EQ(id3, characteristic2_->GetDescriptorsByUUID(id3).at(0)->GetUUID()); | |
1098 EXPECT_EQ(id3, characteristic2_->GetDescriptorsByUUID(id3).at(1)->GetUUID()); | |
1099 EXPECT_EQ(1u, characteristic1_->GetDescriptorsByUUID(id1).size()); | |
1100 EXPECT_EQ(1u, characteristic1_->GetDescriptorsByUUID(id2).size()); | |
1101 EXPECT_EQ(2u, characteristic2_->GetDescriptorsByUUID(id3).size()); | |
1102 | |
1103 EXPECT_EQ(0u, characteristic2_->GetDescriptorsByUUID(id1).size()); | |
1104 EXPECT_EQ(0u, characteristic2_->GetDescriptorsByUUID(id2).size()); | |
1105 EXPECT_EQ(0u, characteristic1_->GetDescriptorsByUUID(id3).size()); | |
1106 } | |
1107 #endif // defined(OS_ANDROID) || defined(OS_WIN) | |
1108 | |
1109 } // namespace device | |
OLD | NEW |