OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 "base/run_loop.h" | |
6 #include "device/bluetooth/bluetooth_gatt_characteristic.h" | |
7 #include "device/bluetooth/bluetooth_gatt_service.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | |
9 | |
10 #if defined(OS_ANDROID) | |
11 #include "device/bluetooth/test/bluetooth_test_android.h" | |
12 #elif defined(OS_MACOSX) | |
13 #include "device/bluetooth/test/bluetooth_test_mac.h" | |
14 #endif | |
15 | |
16 namespace device { | |
17 | |
18 #if defined(OS_ANDROID) || defined(OS_MACOSX) | |
19 class BluetoothGattDescriptorTest : public BluetoothTest { | |
20 public: | |
21 // Creates adapter_, device_, service_, characteristic_, | |
22 // descriptor1_, & descriptor2_. | |
23 void FakeDescriptorBoilerplate() { | |
24 InitWithFakeAdapter(); | |
25 StartLowEnergyDiscoverySession(); | |
26 device_ = DiscoverLowEnergyDevice(3); | |
27 device_->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | |
28 GetConnectErrorCallback(Call::NOT_EXPECTED)); | |
29 SimulateGattConnection(device_); | |
30 std::vector<std::string> services; | |
31 std::string uuid("00000000-0000-1000-8000-00805f9b34fb"); | |
32 services.push_back(uuid); | |
33 SimulateGattServicesDiscovered(device_, services); | |
34 ASSERT_EQ(1u, device_->GetGattServices().size()); | |
35 service_ = device_->GetGattServices()[0]; | |
36 SimulateGattCharacteristic(service_, uuid, 0); | |
37 ASSERT_EQ(1u, service_->GetCharacteristics().size()); | |
38 characteristic_ = service_->GetCharacteristics()[0]; | |
39 SimulateGattDescriptor(characteristic_, | |
40 "00000001-0000-1000-8000-00805f9b34fb"); | |
41 SimulateGattDescriptor(characteristic_, | |
42 "00000002-0000-1000-8000-00805f9b34fb"); | |
43 ASSERT_EQ(2u, characteristic_->GetDescriptors().size()); | |
44 descriptor1_ = characteristic_->GetDescriptors()[0]; | |
45 descriptor2_ = characteristic_->GetDescriptors()[1]; | |
46 ResetEventCounts(); | |
47 } | |
48 | |
49 BluetoothDevice* device_ = nullptr; | |
50 BluetoothGattService* service_ = nullptr; | |
51 BluetoothGattCharacteristic* characteristic_ = nullptr; | |
52 BluetoothGattDescriptor* descriptor1_ = nullptr; | |
53 BluetoothGattDescriptor* descriptor2_ = nullptr; | |
54 }; | |
55 #endif | |
56 | |
57 #if defined(OS_ANDROID) | |
58 TEST_F(BluetoothGattDescriptorTest, GetIdentifier) { | |
59 InitWithFakeAdapter(); | |
60 StartLowEnergyDiscoverySession(); | |
61 // 2 devices to verify that descriptors on them have distinct IDs. | |
62 BluetoothDevice* device1 = DiscoverLowEnergyDevice(3); | |
63 BluetoothDevice* device2 = DiscoverLowEnergyDevice(4); | |
64 device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | |
65 GetConnectErrorCallback(Call::NOT_EXPECTED)); | |
66 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | |
67 GetConnectErrorCallback(Call::NOT_EXPECTED)); | |
68 SimulateGattConnection(device1); | |
69 SimulateGattConnection(device2); | |
70 | |
71 // 3 services (all with same UUID). | |
72 // 1 on the first device (to test characteristic instances across devices). | |
73 // 2 on the second device (to test same device, multiple service instances). | |
74 std::vector<std::string> services; | |
75 std::string uuid = "00000000-0000-1000-8000-00805f9b34fb"; | |
76 services.push_back(uuid); | |
77 SimulateGattServicesDiscovered(device1, services); | |
78 services.push_back(uuid); | |
79 SimulateGattServicesDiscovered(device2, services); | |
80 BluetoothGattService* service1 = device1->GetGattServices()[0]; | |
81 BluetoothGattService* service2 = device2->GetGattServices()[0]; | |
82 BluetoothGattService* service3 = device2->GetGattServices()[1]; | |
83 // 6 characteristics (same UUID), 2 on each service. | |
84 SimulateGattCharacteristic(service1, uuid, /* properties */ 0); | |
85 SimulateGattCharacteristic(service1, uuid, /* properties */ 0); | |
86 SimulateGattCharacteristic(service2, uuid, /* properties */ 0); | |
87 SimulateGattCharacteristic(service2, uuid, /* properties */ 0); | |
88 SimulateGattCharacteristic(service3, uuid, /* properties */ 0); | |
89 SimulateGattCharacteristic(service3, uuid, /* properties */ 0); | |
90 BluetoothGattCharacteristic* char1 = service1->GetCharacteristics()[0]; | |
91 BluetoothGattCharacteristic* char2 = service1->GetCharacteristics()[1]; | |
92 BluetoothGattCharacteristic* char3 = service2->GetCharacteristics()[0]; | |
93 BluetoothGattCharacteristic* char4 = service2->GetCharacteristics()[1]; | |
94 BluetoothGattCharacteristic* char5 = service3->GetCharacteristics()[0]; | |
95 BluetoothGattCharacteristic* char6 = service3->GetCharacteristics()[1]; | |
96 // 6 descriptors (same UUID), 1 on each characteristic | |
97 // TODO(576900) Test multiple descriptors with same UUID on one | |
98 // characteristic. | |
99 SimulateGattDescriptor(char1, uuid); | |
100 SimulateGattDescriptor(char2, uuid); | |
101 SimulateGattDescriptor(char3, uuid); | |
102 SimulateGattDescriptor(char4, uuid); | |
103 SimulateGattDescriptor(char5, uuid); | |
104 SimulateGattDescriptor(char6, uuid); | |
105 BluetoothGattDescriptor* desc1 = char1->GetDescriptors()[0]; | |
106 BluetoothGattDescriptor* desc2 = char2->GetDescriptors()[0]; | |
107 BluetoothGattDescriptor* desc3 = char3->GetDescriptors()[0]; | |
108 BluetoothGattDescriptor* desc4 = char4->GetDescriptors()[0]; | |
109 BluetoothGattDescriptor* desc5 = char5->GetDescriptors()[0]; | |
110 BluetoothGattDescriptor* desc6 = char6->GetDescriptors()[0]; | |
111 | |
112 // All IDs are unique. | |
113 EXPECT_NE(desc1->GetIdentifier(), desc2->GetIdentifier()); | |
114 EXPECT_NE(desc1->GetIdentifier(), desc3->GetIdentifier()); | |
115 EXPECT_NE(desc1->GetIdentifier(), desc4->GetIdentifier()); | |
116 EXPECT_NE(desc1->GetIdentifier(), desc5->GetIdentifier()); | |
117 EXPECT_NE(desc1->GetIdentifier(), desc6->GetIdentifier()); | |
118 | |
119 EXPECT_NE(desc2->GetIdentifier(), desc3->GetIdentifier()); | |
120 EXPECT_NE(desc2->GetIdentifier(), desc4->GetIdentifier()); | |
121 EXPECT_NE(desc2->GetIdentifier(), desc5->GetIdentifier()); | |
122 EXPECT_NE(desc2->GetIdentifier(), desc6->GetIdentifier()); | |
123 | |
124 EXPECT_NE(desc3->GetIdentifier(), desc4->GetIdentifier()); | |
125 EXPECT_NE(desc3->GetIdentifier(), desc5->GetIdentifier()); | |
126 EXPECT_NE(desc3->GetIdentifier(), desc6->GetIdentifier()); | |
127 | |
128 EXPECT_NE(desc4->GetIdentifier(), desc5->GetIdentifier()); | |
129 EXPECT_NE(desc4->GetIdentifier(), desc6->GetIdentifier()); | |
130 | |
131 EXPECT_NE(desc5->GetIdentifier(), desc6->GetIdentifier()); | |
132 } | |
133 #endif // defined(OS_ANDROID) | |
134 | |
135 #if defined(OS_ANDROID) | |
136 TEST_F(BluetoothGattDescriptorTest, GetUUID) { | |
137 InitWithFakeAdapter(); | |
138 StartLowEnergyDiscoverySession(); | |
139 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | |
140 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | |
141 GetConnectErrorCallback(Call::NOT_EXPECTED)); | |
142 SimulateGattConnection(device); | |
143 std::vector<std::string> services; | |
144 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | |
145 SimulateGattServicesDiscovered(device, services); | |
146 ASSERT_EQ(1u, device->GetGattServices().size()); | |
147 BluetoothGattService* service = device->GetGattServices()[0]; | |
148 | |
149 SimulateGattCharacteristic(service, "00000000-0000-1000-8000-00805f9b34fb", | |
150 /* properties */ 0); | |
151 ASSERT_EQ(1u, service->GetCharacteristics().size()); | |
152 BluetoothGattCharacteristic* characteristic = | |
153 service->GetCharacteristics()[0]; | |
154 | |
155 // Create 2 descriptors. | |
156 std::string uuid_str1("11111111-0000-1000-8000-00805f9b34fb"); | |
157 std::string uuid_str2("22222222-0000-1000-8000-00805f9b34fb"); | |
158 BluetoothUUID uuid1(uuid_str1); | |
159 BluetoothUUID uuid2(uuid_str2); | |
160 SimulateGattDescriptor(characteristic, uuid_str1); | |
161 SimulateGattDescriptor(characteristic, uuid_str2); | |
162 ASSERT_EQ(2u, characteristic->GetDescriptors().size()); | |
163 BluetoothGattDescriptor* descriptor1 = characteristic->GetDescriptors()[0]; | |
164 BluetoothGattDescriptor* descriptor2 = characteristic->GetDescriptors()[1]; | |
165 | |
166 // Swap as needed to have descriptor1 be the one with uuid1. | |
167 if (descriptor2->GetUUID() == uuid1) | |
168 std::swap(descriptor1, descriptor2); | |
169 | |
170 EXPECT_EQ(uuid1, descriptor1->GetUUID()); | |
171 EXPECT_EQ(uuid2, descriptor2->GetUUID()); | |
172 } | |
173 #endif // defined(OS_ANDROID) | |
174 | |
175 #if defined(OS_ANDROID) | |
176 // Tests ReadRemoteDescriptor and GetValue with empty value buffer. | |
177 TEST_F(BluetoothGattDescriptorTest, ReadRemoteDescriptor_Empty) { | |
178 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); | |
179 | |
180 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), | |
181 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
182 EXPECT_EQ(1, gatt_read_descriptor_attempts_); | |
183 std::vector<uint8_t> empty_vector; | |
184 SimulateGattDescriptorRead(descriptor1_, empty_vector); | |
185 | |
186 // Duplicate read reported from OS shouldn't cause a problem: | |
187 SimulateGattDescriptorRead(descriptor1_, empty_vector); | |
188 | |
189 EXPECT_EQ(empty_vector, last_read_value_); | |
190 EXPECT_EQ(empty_vector, descriptor1_->GetValue()); | |
191 } | |
192 #endif // defined(OS_ANDROID) | |
193 | |
194 #if defined(OS_ANDROID) | |
195 // Tests WriteRemoteDescriptor with empty value buffer. | |
196 TEST_F(BluetoothGattDescriptorTest, WriteRemoteDescriptor_Empty) { | |
197 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); | |
198 | |
199 std::vector<uint8_t> empty_vector; | |
200 descriptor1_->WriteRemoteDescriptor(empty_vector, GetCallback(Call::EXPECTED), | |
201 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
202 EXPECT_EQ(1, gatt_write_descriptor_attempts_); | |
203 SimulateGattDescriptorWrite(descriptor1_); | |
204 | |
205 // Duplicate write reported from OS shouldn't cause a problem: | |
206 SimulateGattDescriptorWrite(descriptor1_); | |
207 | |
208 EXPECT_EQ(empty_vector, last_write_value_); | |
209 } | |
210 #endif // defined(OS_ANDROID) | |
211 | |
212 #if defined(OS_ANDROID) | |
213 // Tests ReadRemoteDescriptor completing after Chrome objects are deleted. | |
214 TEST_F(BluetoothGattDescriptorTest, ReadRemoteDescriptor_AfterDeleted) { | |
215 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); | |
216 | |
217 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::NOT_EXPECTED), | |
218 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
219 | |
220 RememberDescriptorForSubsequentAction(descriptor1_); | |
221 DeleteDevice(device_); // TODO(576906) delete only the descriptor. | |
222 | |
223 std::vector<uint8_t> empty_vector; | |
224 SimulateGattDescriptorRead(/* use remembered descriptor */ nullptr, | |
225 empty_vector); | |
226 EXPECT_TRUE("Did not crash!"); | |
227 } | |
228 #endif // defined(OS_ANDROID) | |
229 | |
230 #if defined(OS_ANDROID) | |
231 // Tests WriteRemoteDescriptor completing after Chrome objects are deleted. | |
232 TEST_F(BluetoothGattDescriptorTest, WriteRemoteDescriptor_AfterDeleted) { | |
233 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); | |
234 | |
235 std::vector<uint8_t> empty_vector; | |
236 descriptor1_->WriteRemoteDescriptor(empty_vector, | |
237 GetCallback(Call::NOT_EXPECTED), | |
238 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
239 | |
240 RememberDescriptorForSubsequentAction(descriptor1_); | |
241 DeleteDevice(device_); // TODO(576906) delete only the descriptor. | |
242 | |
243 SimulateGattDescriptorWrite(/* use remembered descriptor */ nullptr); | |
244 EXPECT_TRUE("Did not crash!"); | |
245 } | |
246 #endif // defined(OS_ANDROID) | |
247 | |
248 #if defined(OS_ANDROID) | |
249 // Tests ReadRemoteDescriptor and GetValue with non-empty value buffer. | |
250 TEST_F(BluetoothGattDescriptorTest, ReadRemoteDescriptor) { | |
251 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); | |
252 | |
253 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), | |
254 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
255 EXPECT_EQ(1, gatt_read_descriptor_attempts_); | |
256 | |
257 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; | |
258 std::vector<uint8_t> test_vector(values, values + arraysize(values)); | |
259 SimulateGattDescriptorRead(descriptor1_, test_vector); | |
260 | |
261 // Duplicate read reported from OS shouldn't cause a problem: | |
262 std::vector<uint8_t> empty_vector; | |
263 SimulateGattDescriptorRead(descriptor1_, empty_vector); | |
264 | |
265 EXPECT_EQ(test_vector, last_read_value_); | |
266 EXPECT_EQ(test_vector, descriptor1_->GetValue()); | |
267 } | |
268 #endif // defined(OS_ANDROID) | |
269 | |
270 #if defined(OS_ANDROID) | |
271 // Tests WriteRemoteDescriptor with non-empty value buffer. | |
272 TEST_F(BluetoothGattDescriptorTest, WriteRemoteDescriptor) { | |
273 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); | |
274 | |
275 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; | |
276 std::vector<uint8_t> test_vector(values, values + arraysize(values)); | |
277 descriptor1_->WriteRemoteDescriptor(test_vector, GetCallback(Call::EXPECTED), | |
278 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
279 EXPECT_EQ(1, gatt_write_descriptor_attempts_); | |
280 | |
281 SimulateGattDescriptorWrite(descriptor1_); | |
282 | |
283 EXPECT_EQ(test_vector, last_write_value_); | |
284 } | |
285 #endif // defined(OS_ANDROID) | |
286 | |
287 #if defined(OS_ANDROID) | |
288 // Tests ReadRemoteDescriptor and GetValue multiple times. | |
289 TEST_F(BluetoothGattDescriptorTest, ReadRemoteDescriptor_Twice) { | |
290 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); | |
291 | |
292 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), | |
293 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
294 EXPECT_EQ(1, gatt_read_descriptor_attempts_); | |
295 | |
296 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; | |
297 std::vector<uint8_t> test_vector(values, values + arraysize(values)); | |
298 SimulateGattDescriptorRead(descriptor1_, test_vector); | |
299 EXPECT_EQ(1, callback_count_); | |
300 EXPECT_EQ(0, error_callback_count_); | |
301 EXPECT_EQ(test_vector, last_read_value_); | |
302 EXPECT_EQ(test_vector, descriptor1_->GetValue()); | |
303 | |
304 // Read again, with different value: | |
305 ResetEventCounts(); | |
306 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), | |
307 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
308 EXPECT_EQ(1, gatt_read_descriptor_attempts_); | |
309 std::vector<uint8_t> empty_vector; | |
310 SimulateGattDescriptorRead(descriptor1_, empty_vector); | |
311 EXPECT_EQ(1, callback_count_); | |
312 EXPECT_EQ(0, error_callback_count_); | |
313 EXPECT_EQ(empty_vector, last_read_value_); | |
314 EXPECT_EQ(empty_vector, descriptor1_->GetValue()); | |
315 } | |
316 #endif // defined(OS_ANDROID) | |
317 | |
318 #if defined(OS_ANDROID) | |
319 // Tests WriteRemoteDescriptor multiple times. | |
320 TEST_F(BluetoothGattDescriptorTest, WriteRemoteDescriptor_Twice) { | |
321 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); | |
322 | |
323 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; | |
324 std::vector<uint8_t> test_vector(values, values + arraysize(values)); | |
325 descriptor1_->WriteRemoteDescriptor(test_vector, GetCallback(Call::EXPECTED), | |
326 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
327 EXPECT_EQ(1, gatt_write_descriptor_attempts_); | |
328 | |
329 SimulateGattDescriptorWrite(descriptor1_); | |
330 EXPECT_EQ(1, callback_count_); | |
331 EXPECT_EQ(0, error_callback_count_); | |
332 EXPECT_EQ(test_vector, last_write_value_); | |
333 | |
334 // Write again, with different value: | |
335 ResetEventCounts(); | |
336 std::vector<uint8_t> empty_vector; | |
337 descriptor1_->WriteRemoteDescriptor(empty_vector, GetCallback(Call::EXPECTED), | |
338 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
339 EXPECT_EQ(1, gatt_write_descriptor_attempts_); | |
340 SimulateGattDescriptorWrite(descriptor1_); | |
341 EXPECT_EQ(1, callback_count_); | |
342 EXPECT_EQ(0, error_callback_count_); | |
343 EXPECT_EQ(empty_vector, last_write_value_); | |
344 } | |
345 #endif // defined(OS_ANDROID) | |
346 | |
347 #if defined(OS_ANDROID) | |
348 // Tests ReadRemoteDescriptor on two descriptors. | |
349 TEST_F(BluetoothGattDescriptorTest, ReadRemoteDescriptor_MultipleDescriptors) { | |
350 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); | |
351 | |
352 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), | |
353 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
354 descriptor2_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), | |
355 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
356 EXPECT_EQ(2, gatt_read_descriptor_attempts_); | |
357 EXPECT_EQ(0, callback_count_); | |
358 EXPECT_EQ(0, error_callback_count_); | |
359 | |
360 std::vector<uint8_t> test_vector1; | |
361 test_vector1.push_back(111); | |
362 SimulateGattDescriptorRead(descriptor1_, test_vector1); | |
363 EXPECT_EQ(test_vector1, last_read_value_); | |
364 | |
365 std::vector<uint8_t> test_vector2; | |
366 test_vector2.push_back(222); | |
367 SimulateGattDescriptorRead(descriptor2_, test_vector2); | |
368 EXPECT_EQ(test_vector2, last_read_value_); | |
369 | |
370 EXPECT_EQ(2, callback_count_); | |
371 EXPECT_EQ(0, error_callback_count_); | |
372 EXPECT_EQ(test_vector1, descriptor1_->GetValue()); | |
373 EXPECT_EQ(test_vector2, descriptor2_->GetValue()); | |
374 } | |
375 #endif // defined(OS_ANDROID) | |
376 | |
377 #if defined(OS_ANDROID) | |
378 // Tests WriteRemoteDescriptor on two descriptors. | |
379 TEST_F(BluetoothGattDescriptorTest, WriteRemoteDescriptor_MultipleDescriptors) { | |
380 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); | |
381 | |
382 std::vector<uint8_t> test_vector1; | |
383 test_vector1.push_back(111); | |
384 descriptor1_->WriteRemoteDescriptor(test_vector1, GetCallback(Call::EXPECTED), | |
385 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
386 EXPECT_EQ(test_vector1, last_write_value_); | |
387 | |
388 std::vector<uint8_t> test_vector2; | |
389 test_vector2.push_back(222); | |
390 descriptor2_->WriteRemoteDescriptor(test_vector2, GetCallback(Call::EXPECTED), | |
391 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
392 EXPECT_EQ(test_vector2, last_write_value_); | |
393 | |
394 EXPECT_EQ(2, gatt_write_descriptor_attempts_); | |
395 EXPECT_EQ(0, callback_count_); | |
396 EXPECT_EQ(0, error_callback_count_); | |
397 | |
398 SimulateGattDescriptorWrite(descriptor1_); | |
399 SimulateGattDescriptorWrite(descriptor2_); | |
400 | |
401 EXPECT_EQ(2, callback_count_); | |
402 EXPECT_EQ(0, error_callback_count_); | |
403 } | |
404 #endif // defined(OS_ANDROID) | |
405 | |
406 #if defined(OS_ANDROID) | |
407 // Tests ReadRemoteDescriptor asynchronous error. | |
408 TEST_F(BluetoothGattDescriptorTest, ReadError) { | |
409 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); | |
410 | |
411 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::NOT_EXPECTED), | |
412 GetGattErrorCallback(Call::EXPECTED)); | |
413 SimulateGattDescriptorReadError( | |
414 descriptor1_, BluetoothGattService::GATT_ERROR_INVALID_LENGTH); | |
415 SimulateGattDescriptorReadError(descriptor1_, | |
416 BluetoothGattService::GATT_ERROR_FAILED); | |
417 EXPECT_EQ(BluetoothGattService::GATT_ERROR_INVALID_LENGTH, | |
418 last_gatt_error_code_); | |
419 } | |
420 #endif // defined(OS_ANDROID) | |
421 | |
422 #if defined(OS_ANDROID) | |
423 // Tests WriteRemoteDescriptor asynchronous error. | |
424 TEST_F(BluetoothGattDescriptorTest, WriteError) { | |
425 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); | |
426 | |
427 std::vector<uint8_t> empty_vector; | |
428 descriptor1_->WriteRemoteDescriptor(empty_vector, | |
429 GetCallback(Call::NOT_EXPECTED), | |
430 GetGattErrorCallback(Call::EXPECTED)); | |
431 SimulateGattDescriptorWriteError( | |
432 descriptor1_, BluetoothGattService::GATT_ERROR_INVALID_LENGTH); | |
433 SimulateGattDescriptorWriteError(descriptor1_, | |
434 BluetoothGattService::GATT_ERROR_FAILED); | |
435 | |
436 EXPECT_EQ(BluetoothGattService::GATT_ERROR_INVALID_LENGTH, | |
437 last_gatt_error_code_); | |
438 } | |
439 #endif // defined(OS_ANDROID) | |
440 | |
441 #if defined(OS_ANDROID) | |
442 // Tests ReadRemoteDescriptor synchronous error. | |
443 TEST_F(BluetoothGattDescriptorTest, ReadSynchronousError) { | |
444 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); | |
445 | |
446 SimulateGattDescriptorReadWillFailSynchronouslyOnce(descriptor1_); | |
447 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::NOT_EXPECTED), | |
448 GetGattErrorCallback(Call::EXPECTED)); | |
449 EXPECT_EQ(0, gatt_read_descriptor_attempts_); | |
450 base::RunLoop().RunUntilIdle(); | |
451 EXPECT_EQ(0, callback_count_); | |
452 EXPECT_EQ(1, error_callback_count_); | |
453 EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_); | |
454 | |
455 // After failing once, can succeed: | |
456 ResetEventCounts(); | |
457 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), | |
458 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
459 EXPECT_EQ(1, gatt_read_descriptor_attempts_); | |
460 std::vector<uint8_t> empty_vector; | |
461 SimulateGattDescriptorRead(descriptor1_, empty_vector); | |
462 EXPECT_EQ(1, callback_count_); | |
463 EXPECT_EQ(0, error_callback_count_); | |
464 } | |
465 #endif // defined(OS_ANDROID) | |
466 | |
467 #if defined(OS_ANDROID) | |
468 // Tests WriteRemoteDescriptor synchronous error. | |
469 TEST_F(BluetoothGattDescriptorTest, WriteSynchronousError) { | |
470 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); | |
471 | |
472 SimulateGattDescriptorWriteWillFailSynchronouslyOnce(descriptor1_); | |
473 std::vector<uint8_t> empty_vector; | |
474 descriptor1_->WriteRemoteDescriptor(empty_vector, | |
475 GetCallback(Call::NOT_EXPECTED), | |
476 GetGattErrorCallback(Call::EXPECTED)); | |
477 EXPECT_EQ(0, gatt_write_descriptor_attempts_); | |
478 base::RunLoop().RunUntilIdle(); | |
479 EXPECT_EQ(0, callback_count_); | |
480 EXPECT_EQ(1, error_callback_count_); | |
481 EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_); | |
482 | |
483 // After failing once, can succeed: | |
484 ResetEventCounts(); | |
485 descriptor1_->WriteRemoteDescriptor(empty_vector, GetCallback(Call::EXPECTED), | |
486 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
487 EXPECT_EQ(1, gatt_write_descriptor_attempts_); | |
488 SimulateGattDescriptorWrite(descriptor1_); | |
489 EXPECT_EQ(1, callback_count_); | |
490 EXPECT_EQ(0, error_callback_count_); | |
491 } | |
492 #endif // defined(OS_ANDROID) | |
493 | |
494 #if defined(OS_ANDROID) | |
495 // Tests ReadRemoteDescriptor error with a pending read operation. | |
496 TEST_F(BluetoothGattDescriptorTest, ReadRemoteDescriptor_ReadPending) { | |
497 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); | |
498 | |
499 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), | |
500 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
501 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::NOT_EXPECTED), | |
502 GetGattErrorCallback(Call::EXPECTED)); | |
503 | |
504 base::RunLoop().RunUntilIdle(); | |
505 | |
506 EXPECT_EQ(0, callback_count_); | |
507 EXPECT_EQ(1, error_callback_count_); | |
508 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, | |
509 last_gatt_error_code_); | |
510 | |
511 // Initial read should still succeed: | |
512 ResetEventCounts(); | |
513 std::vector<uint8_t> empty_vector; | |
514 SimulateGattDescriptorRead(descriptor1_, empty_vector); | |
515 EXPECT_EQ(1, callback_count_); | |
516 EXPECT_EQ(0, error_callback_count_); | |
517 } | |
518 #endif // defined(OS_ANDROID) | |
519 | |
520 #if defined(OS_ANDROID) | |
521 // Tests WriteRemoteDescriptor error with a pending write operation. | |
522 TEST_F(BluetoothGattDescriptorTest, WriteRemoteDescriptor_WritePending) { | |
523 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); | |
524 | |
525 std::vector<uint8_t> empty_vector; | |
526 descriptor1_->WriteRemoteDescriptor(empty_vector, GetCallback(Call::EXPECTED), | |
527 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
528 descriptor1_->WriteRemoteDescriptor(empty_vector, | |
529 GetCallback(Call::NOT_EXPECTED), | |
530 GetGattErrorCallback(Call::EXPECTED)); | |
531 | |
532 base::RunLoop().RunUntilIdle(); | |
533 | |
534 EXPECT_EQ(0, callback_count_); | |
535 EXPECT_EQ(1, error_callback_count_); | |
536 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, | |
537 last_gatt_error_code_); | |
538 | |
539 // Initial write should still succeed: | |
540 ResetEventCounts(); | |
541 SimulateGattDescriptorWrite(descriptor1_); | |
542 EXPECT_EQ(1, callback_count_); | |
543 EXPECT_EQ(0, error_callback_count_); | |
544 } | |
545 #endif // defined(OS_ANDROID) | |
546 | |
547 #if defined(OS_ANDROID) | |
548 // Tests ReadRemoteDescriptor error with a pending write operation. | |
549 TEST_F(BluetoothGattDescriptorTest, ReadRemoteDescriptor_WritePending) { | |
550 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); | |
551 | |
552 std::vector<uint8_t> empty_vector; | |
553 descriptor1_->WriteRemoteDescriptor(empty_vector, GetCallback(Call::EXPECTED), | |
554 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
555 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::NOT_EXPECTED), | |
556 GetGattErrorCallback(Call::EXPECTED)); | |
557 | |
558 base::RunLoop().RunUntilIdle(); | |
559 | |
560 EXPECT_EQ(0, callback_count_); | |
561 EXPECT_EQ(1, error_callback_count_); | |
562 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, | |
563 last_gatt_error_code_); | |
564 | |
565 // Initial write should still succeed: | |
566 ResetEventCounts(); | |
567 SimulateGattDescriptorWrite(descriptor1_); | |
568 EXPECT_EQ(1, callback_count_); | |
569 EXPECT_EQ(0, error_callback_count_); | |
570 } | |
571 #endif // defined(OS_ANDROID) | |
572 | |
573 #if defined(OS_ANDROID) | |
574 // Tests WriteRemoteDescriptor error with a pending Read operation. | |
575 TEST_F(BluetoothGattDescriptorTest, WriteRemoteDescriptor_ReadPending) { | |
576 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); | |
577 | |
578 std::vector<uint8_t> empty_vector; | |
579 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), | |
580 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
581 descriptor1_->WriteRemoteDescriptor(empty_vector, | |
582 GetCallback(Call::NOT_EXPECTED), | |
583 GetGattErrorCallback(Call::EXPECTED)); | |
584 base::RunLoop().RunUntilIdle(); | |
585 | |
586 EXPECT_EQ(0, callback_count_); | |
587 EXPECT_EQ(1, error_callback_count_); | |
588 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, | |
589 last_gatt_error_code_); | |
590 | |
591 // Initial read should still succeed: | |
592 ResetEventCounts(); | |
593 SimulateGattDescriptorRead(descriptor1_, empty_vector); | |
594 EXPECT_EQ(1, callback_count_); | |
595 EXPECT_EQ(0, error_callback_count_); | |
596 } | |
597 #endif // defined(OS_ANDROID) | |
598 | |
599 } // namespace device | |
OLD | NEW |