OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/run_loop.h" | 5 #include "base/run_loop.h" |
6 #include "device/bluetooth/bluetooth_gatt_characteristic.h" | 6 #include "device/bluetooth/bluetooth_gatt_characteristic.h" |
7 #include "device/bluetooth/bluetooth_gatt_service.h" | 7 #include "device/bluetooth/bluetooth_gatt_service.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 #if defined(OS_ANDROID) | 10 #if defined(OS_ANDROID) |
11 #include "device/bluetooth/test/bluetooth_test_android.h" | 11 #include "device/bluetooth/test/bluetooth_test_android.h" |
12 #elif defined(OS_MACOSX) | 12 #elif defined(OS_MACOSX) |
13 #include "device/bluetooth/test/bluetooth_test_mac.h" | 13 #include "device/bluetooth/test/bluetooth_test_mac.h" |
14 #endif | 14 #endif |
15 | 15 |
16 namespace device { | 16 namespace device { |
17 | 17 |
18 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 18 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
19 class BluetoothGattDescriptorTest : public BluetoothTest {}; | 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 }; |
20 #endif | 55 #endif |
21 | 56 |
22 #if defined(OS_ANDROID) | 57 #if defined(OS_ANDROID) |
23 TEST_F(BluetoothGattDescriptorTest, GetIdentifier) { | 58 TEST_F(BluetoothGattDescriptorTest, GetIdentifier) { |
24 InitWithFakeAdapter(); | 59 InitWithFakeAdapter(); |
25 StartLowEnergyDiscoverySession(); | 60 StartLowEnergyDiscoverySession(); |
26 // 2 devices to verify that descriptors on them have distinct IDs. | 61 // 2 devices to verify that descriptors on them have distinct IDs. |
27 BluetoothDevice* device1 = DiscoverLowEnergyDevice(3); | 62 BluetoothDevice* device1 = DiscoverLowEnergyDevice(3); |
28 BluetoothDevice* device2 = DiscoverLowEnergyDevice(4); | 63 BluetoothDevice* device2 = DiscoverLowEnergyDevice(4); |
29 device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 64 device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 | 165 |
131 // Swap as needed to have descriptor1 be the one with uuid1. | 166 // Swap as needed to have descriptor1 be the one with uuid1. |
132 if (descriptor2->GetUUID() == uuid1) | 167 if (descriptor2->GetUUID() == uuid1) |
133 std::swap(descriptor1, descriptor2); | 168 std::swap(descriptor1, descriptor2); |
134 | 169 |
135 EXPECT_EQ(uuid1, descriptor1->GetUUID()); | 170 EXPECT_EQ(uuid1, descriptor1->GetUUID()); |
136 EXPECT_EQ(uuid2, descriptor2->GetUUID()); | 171 EXPECT_EQ(uuid2, descriptor2->GetUUID()); |
137 } | 172 } |
138 #endif // defined(OS_ANDROID) | 173 #endif // defined(OS_ANDROID) |
139 | 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 EXPECT_EQ(empty_vector, last_write_value_); |
| 206 } |
| 207 #endif // defined(OS_ANDROID) |
| 208 |
| 209 #if defined(OS_ANDROID) |
| 210 // Tests ReadRemoteDescriptor completing after Chrome objects are deleted. |
| 211 TEST_F(BluetoothGattDescriptorTest, ReadRemoteDescriptor_AfterDeleted) { |
| 212 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); |
| 213 |
| 214 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::NOT_EXPECTED), |
| 215 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 216 |
| 217 RememberDescriptorForSubsequentAction(descriptor1_); |
| 218 DeleteDevice(device_); |
| 219 |
| 220 std::vector<uint8_t> empty_vector; |
| 221 SimulateGattDescriptorRead(/* use remembered descriptor */ nullptr, |
| 222 empty_vector); |
| 223 EXPECT_TRUE("Did not crash!"); |
| 224 } |
| 225 #endif // defined(OS_ANDROID) |
| 226 |
| 227 #if defined(OS_ANDROID) |
| 228 // Tests WriteRemoteDescriptor completing after Chrome objects are deleted. |
| 229 TEST_F(BluetoothGattDescriptorTest, WriteRemoteDescriptor_AfterDeleted) { |
| 230 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); |
| 231 |
| 232 std::vector<uint8_t> empty_vector; |
| 233 descriptor1_->WriteRemoteDescriptor(empty_vector, |
| 234 GetCallback(Call::NOT_EXPECTED), |
| 235 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 236 |
| 237 RememberDescriptorForSubsequentAction(descriptor1_); |
| 238 DeleteDevice(device_); |
| 239 |
| 240 SimulateGattDescriptorWrite(/* use remembered descriptor */ nullptr); |
| 241 EXPECT_TRUE("Did not crash!"); |
| 242 } |
| 243 #endif // defined(OS_ANDROID) |
| 244 |
| 245 #if defined(OS_ANDROID) |
| 246 // Tests ReadRemoteDescriptor and GetValue with non-empty value buffer. |
| 247 TEST_F(BluetoothGattDescriptorTest, ReadRemoteDescriptor) { |
| 248 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); |
| 249 |
| 250 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), |
| 251 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 252 EXPECT_EQ(1, gatt_read_descriptor_attempts_); |
| 253 |
| 254 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; |
| 255 std::vector<uint8_t> test_vector(values, values + arraysize(values)); |
| 256 SimulateGattDescriptorRead(descriptor1_, test_vector); |
| 257 |
| 258 // Duplicate read reported from OS shouldn't cause a problem: |
| 259 std::vector<uint8_t> empty_vector; |
| 260 SimulateGattDescriptorRead(descriptor1_, empty_vector); |
| 261 |
| 262 EXPECT_EQ(test_vector, last_read_value_); |
| 263 EXPECT_EQ(test_vector, descriptor1_->GetValue()); |
| 264 } |
| 265 #endif // defined(OS_ANDROID) |
| 266 |
| 267 #if defined(OS_ANDROID) |
| 268 // Tests WriteRemoteDescriptor with non-empty value buffer. |
| 269 TEST_F(BluetoothGattDescriptorTest, WriteRemoteDescriptor) { |
| 270 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); |
| 271 |
| 272 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; |
| 273 std::vector<uint8_t> test_vector(values, values + arraysize(values)); |
| 274 descriptor1_->WriteRemoteDescriptor(test_vector, GetCallback(Call::EXPECTED), |
| 275 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 276 EXPECT_EQ(1, gatt_write_descriptor_attempts_); |
| 277 |
| 278 SimulateGattDescriptorWrite(descriptor1_); |
| 279 |
| 280 EXPECT_EQ(test_vector, last_write_value_); |
| 281 } |
| 282 #endif // defined(OS_ANDROID) |
| 283 |
| 284 #if defined(OS_ANDROID) |
| 285 // Tests ReadRemoteDescriptor and GetValue multiple times. |
| 286 TEST_F(BluetoothGattDescriptorTest, ReadRemoteDescriptor_Twice) { |
| 287 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); |
| 288 |
| 289 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), |
| 290 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 291 EXPECT_EQ(1, gatt_read_descriptor_attempts_); |
| 292 |
| 293 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; |
| 294 std::vector<uint8_t> test_vector(values, values + arraysize(values)); |
| 295 SimulateGattDescriptorRead(descriptor1_, test_vector); |
| 296 EXPECT_EQ(1, callback_count_); |
| 297 EXPECT_EQ(0, error_callback_count_); |
| 298 EXPECT_EQ(test_vector, last_read_value_); |
| 299 EXPECT_EQ(test_vector, descriptor1_->GetValue()); |
| 300 |
| 301 // Read again, with different value: |
| 302 ResetEventCounts(); |
| 303 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), |
| 304 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 305 EXPECT_EQ(1, gatt_read_descriptor_attempts_); |
| 306 std::vector<uint8_t> empty_vector; |
| 307 SimulateGattDescriptorRead(descriptor1_, empty_vector); |
| 308 EXPECT_EQ(1, callback_count_); |
| 309 EXPECT_EQ(0, error_callback_count_); |
| 310 EXPECT_EQ(empty_vector, last_read_value_); |
| 311 EXPECT_EQ(empty_vector, descriptor1_->GetValue()); |
| 312 } |
| 313 #endif // defined(OS_ANDROID) |
| 314 |
| 315 #if defined(OS_ANDROID) |
| 316 // Tests WriteRemoteDescriptor multiple times. |
| 317 TEST_F(BluetoothGattDescriptorTest, WriteRemoteDescriptor_Twice) { |
| 318 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); |
| 319 |
| 320 uint8_t values[] = {0, 1, 2, 3, 4, 0xf, 0xf0, 0xff}; |
| 321 std::vector<uint8_t> test_vector(values, values + arraysize(values)); |
| 322 descriptor1_->WriteRemoteDescriptor(test_vector, GetCallback(Call::EXPECTED), |
| 323 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 324 EXPECT_EQ(1, gatt_write_descriptor_attempts_); |
| 325 |
| 326 SimulateGattDescriptorWrite(descriptor1_); |
| 327 EXPECT_EQ(1, callback_count_); |
| 328 EXPECT_EQ(0, error_callback_count_); |
| 329 EXPECT_EQ(test_vector, last_write_value_); |
| 330 |
| 331 // Write again, with different value: |
| 332 ResetEventCounts(); |
| 333 std::vector<uint8_t> empty_vector; |
| 334 descriptor1_->WriteRemoteDescriptor(empty_vector, GetCallback(Call::EXPECTED), |
| 335 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 336 EXPECT_EQ(1, gatt_write_descriptor_attempts_); |
| 337 SimulateGattDescriptorWrite(descriptor1_); |
| 338 EXPECT_EQ(1, callback_count_); |
| 339 EXPECT_EQ(0, error_callback_count_); |
| 340 EXPECT_EQ(empty_vector, last_write_value_); |
| 341 } |
| 342 #endif // defined(OS_ANDROID) |
| 343 |
| 344 #if defined(OS_ANDROID) |
| 345 // Tests ReadRemoteDescriptor on two descriptors. |
| 346 TEST_F(BluetoothGattDescriptorTest, ReadRemoteDescriptor_MultipleDescriptors) { |
| 347 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); |
| 348 |
| 349 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), |
| 350 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 351 descriptor2_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), |
| 352 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 353 EXPECT_EQ(2, gatt_read_descriptor_attempts_); |
| 354 EXPECT_EQ(0, callback_count_); |
| 355 EXPECT_EQ(0, error_callback_count_); |
| 356 |
| 357 std::vector<uint8_t> test_vector1; |
| 358 test_vector1.push_back(111); |
| 359 SimulateGattDescriptorRead(descriptor1_, test_vector1); |
| 360 EXPECT_EQ(test_vector1, last_read_value_); |
| 361 |
| 362 std::vector<uint8_t> test_vector2; |
| 363 test_vector2.push_back(222); |
| 364 SimulateGattDescriptorRead(descriptor2_, test_vector2); |
| 365 EXPECT_EQ(test_vector2, last_read_value_); |
| 366 |
| 367 EXPECT_EQ(2, callback_count_); |
| 368 EXPECT_EQ(0, error_callback_count_); |
| 369 EXPECT_EQ(test_vector1, descriptor1_->GetValue()); |
| 370 EXPECT_EQ(test_vector2, descriptor2_->GetValue()); |
| 371 } |
| 372 #endif // defined(OS_ANDROID) |
| 373 |
| 374 #if defined(OS_ANDROID) |
| 375 // Tests WriteRemoteDescriptor on two descriptors. |
| 376 TEST_F(BluetoothGattDescriptorTest, WriteRemoteDescriptor_MultipleDescriptors) { |
| 377 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); |
| 378 |
| 379 std::vector<uint8_t> test_vector1; |
| 380 test_vector1.push_back(111); |
| 381 descriptor1_->WriteRemoteDescriptor(test_vector1, GetCallback(Call::EXPECTED), |
| 382 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 383 EXPECT_EQ(test_vector1, last_write_value_); |
| 384 |
| 385 std::vector<uint8_t> test_vector2; |
| 386 test_vector2.push_back(222); |
| 387 descriptor2_->WriteRemoteDescriptor(test_vector2, GetCallback(Call::EXPECTED), |
| 388 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 389 EXPECT_EQ(test_vector2, last_write_value_); |
| 390 |
| 391 EXPECT_EQ(2, gatt_write_descriptor_attempts_); |
| 392 EXPECT_EQ(0, callback_count_); |
| 393 EXPECT_EQ(0, error_callback_count_); |
| 394 |
| 395 SimulateGattDescriptorWrite(descriptor1_); |
| 396 SimulateGattDescriptorWrite(descriptor2_); |
| 397 |
| 398 EXPECT_EQ(2, callback_count_); |
| 399 EXPECT_EQ(0, error_callback_count_); |
| 400 } |
| 401 #endif // defined(OS_ANDROID) |
| 402 |
| 403 #if defined(OS_ANDROID) |
| 404 // Tests ReadRemoteDescriptor asynchronous error. |
| 405 TEST_F(BluetoothGattDescriptorTest, ReadError) { |
| 406 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); |
| 407 |
| 408 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::NOT_EXPECTED), |
| 409 GetGattErrorCallback(Call::EXPECTED)); |
| 410 SimulateGattDescriptorReadError( |
| 411 descriptor1_, BluetoothGattService::GATT_ERROR_INVALID_LENGTH); |
| 412 SimulateGattDescriptorReadError(descriptor1_, |
| 413 BluetoothGattService::GATT_ERROR_FAILED); |
| 414 EXPECT_EQ(BluetoothGattService::GATT_ERROR_INVALID_LENGTH, |
| 415 last_gatt_error_code_); |
| 416 } |
| 417 #endif // defined(OS_ANDROID) |
| 418 |
| 419 #if defined(OS_ANDROID) |
| 420 // Tests WriteRemoteDescriptor asynchronous error. |
| 421 TEST_F(BluetoothGattDescriptorTest, WriteError) { |
| 422 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); |
| 423 |
| 424 std::vector<uint8_t> empty_vector; |
| 425 descriptor1_->WriteRemoteDescriptor(empty_vector, |
| 426 GetCallback(Call::NOT_EXPECTED), |
| 427 GetGattErrorCallback(Call::EXPECTED)); |
| 428 SimulateGattDescriptorWriteError( |
| 429 descriptor1_, BluetoothGattService::GATT_ERROR_INVALID_LENGTH); |
| 430 SimulateGattDescriptorWriteError(descriptor1_, |
| 431 BluetoothGattService::GATT_ERROR_FAILED); |
| 432 |
| 433 EXPECT_EQ(BluetoothGattService::GATT_ERROR_INVALID_LENGTH, |
| 434 last_gatt_error_code_); |
| 435 } |
| 436 #endif // defined(OS_ANDROID) |
| 437 |
| 438 #if defined(OS_ANDROID) |
| 439 // Tests ReadRemoteDescriptor synchronous error. |
| 440 TEST_F(BluetoothGattDescriptorTest, ReadSynchronousError) { |
| 441 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); |
| 442 |
| 443 SimulateGattDescriptorReadWillFailSynchronouslyOnce(descriptor1_); |
| 444 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::NOT_EXPECTED), |
| 445 GetGattErrorCallback(Call::EXPECTED)); |
| 446 EXPECT_EQ(0, gatt_read_descriptor_attempts_); |
| 447 base::RunLoop().RunUntilIdle(); |
| 448 EXPECT_EQ(0, callback_count_); |
| 449 EXPECT_EQ(1, error_callback_count_); |
| 450 EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_); |
| 451 |
| 452 // After failing once, can succeed: |
| 453 ResetEventCounts(); |
| 454 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), |
| 455 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 456 EXPECT_EQ(1, gatt_read_descriptor_attempts_); |
| 457 std::vector<uint8_t> empty_vector; |
| 458 SimulateGattDescriptorRead(descriptor1_, empty_vector); |
| 459 EXPECT_EQ(1, callback_count_); |
| 460 EXPECT_EQ(0, error_callback_count_); |
| 461 } |
| 462 #endif // defined(OS_ANDROID) |
| 463 |
| 464 #if defined(OS_ANDROID) |
| 465 // Tests WriteRemoteDescriptor synchronous error. |
| 466 TEST_F(BluetoothGattDescriptorTest, WriteSynchronousError) { |
| 467 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); |
| 468 |
| 469 SimulateGattDescriptorWriteWillFailSynchronouslyOnce(descriptor1_); |
| 470 std::vector<uint8_t> empty_vector; |
| 471 descriptor1_->WriteRemoteDescriptor(empty_vector, |
| 472 GetCallback(Call::NOT_EXPECTED), |
| 473 GetGattErrorCallback(Call::EXPECTED)); |
| 474 EXPECT_EQ(0, gatt_write_descriptor_attempts_); |
| 475 base::RunLoop().RunUntilIdle(); |
| 476 EXPECT_EQ(0, callback_count_); |
| 477 EXPECT_EQ(1, error_callback_count_); |
| 478 EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_gatt_error_code_); |
| 479 |
| 480 // After failing once, can succeed: |
| 481 ResetEventCounts(); |
| 482 descriptor1_->WriteRemoteDescriptor(empty_vector, GetCallback(Call::EXPECTED), |
| 483 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 484 EXPECT_EQ(1, gatt_write_descriptor_attempts_); |
| 485 SimulateGattDescriptorWrite(descriptor1_); |
| 486 EXPECT_EQ(1, callback_count_); |
| 487 EXPECT_EQ(0, error_callback_count_); |
| 488 } |
| 489 #endif // defined(OS_ANDROID) |
| 490 |
| 491 #if defined(OS_ANDROID) |
| 492 // Tests ReadRemoteDescriptor error with a pending read operation. |
| 493 TEST_F(BluetoothGattDescriptorTest, ReadRemoteDescriptor_ReadPending) { |
| 494 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); |
| 495 |
| 496 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), |
| 497 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 498 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::NOT_EXPECTED), |
| 499 GetGattErrorCallback(Call::EXPECTED)); |
| 500 |
| 501 base::RunLoop().RunUntilIdle(); |
| 502 |
| 503 EXPECT_EQ(0, callback_count_); |
| 504 EXPECT_EQ(1, error_callback_count_); |
| 505 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, |
| 506 last_gatt_error_code_); |
| 507 |
| 508 // Initial read should still succeed: |
| 509 ResetEventCounts(); |
| 510 std::vector<uint8_t> empty_vector; |
| 511 SimulateGattDescriptorRead(descriptor1_, empty_vector); |
| 512 EXPECT_EQ(1, callback_count_); |
| 513 EXPECT_EQ(0, error_callback_count_); |
| 514 } |
| 515 #endif // defined(OS_ANDROID) |
| 516 |
| 517 #if defined(OS_ANDROID) |
| 518 // Tests WriteRemoteDescriptor error with a pending write operation. |
| 519 TEST_F(BluetoothGattDescriptorTest, WriteRemoteDescriptor_WritePending) { |
| 520 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); |
| 521 |
| 522 std::vector<uint8_t> empty_vector; |
| 523 descriptor1_->WriteRemoteDescriptor(empty_vector, GetCallback(Call::EXPECTED), |
| 524 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 525 descriptor1_->WriteRemoteDescriptor(empty_vector, |
| 526 GetCallback(Call::NOT_EXPECTED), |
| 527 GetGattErrorCallback(Call::EXPECTED)); |
| 528 |
| 529 base::RunLoop().RunUntilIdle(); |
| 530 |
| 531 EXPECT_EQ(0, callback_count_); |
| 532 EXPECT_EQ(1, error_callback_count_); |
| 533 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, |
| 534 last_gatt_error_code_); |
| 535 |
| 536 // Initial write should still succeed: |
| 537 ResetEventCounts(); |
| 538 SimulateGattDescriptorWrite(descriptor1_); |
| 539 EXPECT_EQ(1, callback_count_); |
| 540 EXPECT_EQ(0, error_callback_count_); |
| 541 } |
| 542 #endif // defined(OS_ANDROID) |
| 543 |
| 544 #if defined(OS_ANDROID) |
| 545 // Tests ReadRemoteDescriptor error with a pending write operation. |
| 546 TEST_F(BluetoothGattDescriptorTest, ReadRemoteDescriptor_WritePending) { |
| 547 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); |
| 548 |
| 549 std::vector<uint8_t> empty_vector; |
| 550 descriptor1_->WriteRemoteDescriptor(empty_vector, GetCallback(Call::EXPECTED), |
| 551 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 552 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::NOT_EXPECTED), |
| 553 GetGattErrorCallback(Call::EXPECTED)); |
| 554 |
| 555 base::RunLoop().RunUntilIdle(); |
| 556 |
| 557 EXPECT_EQ(0, callback_count_); |
| 558 EXPECT_EQ(1, error_callback_count_); |
| 559 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, |
| 560 last_gatt_error_code_); |
| 561 |
| 562 // Initial write should still succeed: |
| 563 ResetEventCounts(); |
| 564 SimulateGattDescriptorWrite(descriptor1_); |
| 565 EXPECT_EQ(1, callback_count_); |
| 566 EXPECT_EQ(0, error_callback_count_); |
| 567 } |
| 568 #endif // defined(OS_ANDROID) |
| 569 |
| 570 #if defined(OS_ANDROID) |
| 571 // Tests WriteRemoteDescriptor error with a pending Read operation. |
| 572 TEST_F(BluetoothGattDescriptorTest, WriteRemoteDescriptor_ReadPending) { |
| 573 ASSERT_NO_FATAL_FAILURE(FakeDescriptorBoilerplate()); |
| 574 |
| 575 std::vector<uint8_t> empty_vector; |
| 576 descriptor1_->ReadRemoteDescriptor(GetReadValueCallback(Call::EXPECTED), |
| 577 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 578 descriptor1_->WriteRemoteDescriptor(empty_vector, |
| 579 GetCallback(Call::NOT_EXPECTED), |
| 580 GetGattErrorCallback(Call::EXPECTED)); |
| 581 base::RunLoop().RunUntilIdle(); |
| 582 |
| 583 EXPECT_EQ(0, callback_count_); |
| 584 EXPECT_EQ(1, error_callback_count_); |
| 585 EXPECT_EQ(BluetoothGattService::GATT_ERROR_IN_PROGRESS, |
| 586 last_gatt_error_code_); |
| 587 |
| 588 // Initial read should still succeed: |
| 589 ResetEventCounts(); |
| 590 SimulateGattDescriptorRead(descriptor1_, empty_vector); |
| 591 EXPECT_EQ(1, callback_count_); |
| 592 EXPECT_EQ(0, error_callback_count_); |
| 593 } |
| 594 #endif // defined(OS_ANDROID) |
| 595 |
140 } // namespace device | 596 } // namespace device |
OLD | NEW |