OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "build/build_config.h" | 6 #include "build/build_config.h" |
7 #include "device/bluetooth/bluetooth_gatt_service.h" | 7 #include "device/bluetooth/bluetooth_gatt_service.h" |
8 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" | 8 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" |
9 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" | 9 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 // Simulate remove of a primary service. | 329 // Simulate remove of a primary service. |
330 BluetoothRemoteGattService* service1 = device->GetGattServices()[0]; | 330 BluetoothRemoteGattService* service1 = device->GetGattServices()[0]; |
331 BluetoothRemoteGattService* service2 = device->GetGattServices()[1]; | 331 BluetoothRemoteGattService* service2 = device->GetGattServices()[1]; |
332 std::string removed_service = service1->GetIdentifier(); | 332 std::string removed_service = service1->GetIdentifier(); |
333 SimulateGattServiceRemoved(device->GetGattService(removed_service)); | 333 SimulateGattServiceRemoved(device->GetGattService(removed_service)); |
334 EXPECT_EQ(1, observer.gatt_service_removed_count()); | 334 EXPECT_EQ(1, observer.gatt_service_removed_count()); |
335 EXPECT_EQ(1u, device->GetGattServices().size()); | 335 EXPECT_EQ(1u, device->GetGattServices().size()); |
336 EXPECT_FALSE(device->GetGattService(removed_service)); | 336 EXPECT_FALSE(device->GetGattService(removed_service)); |
337 EXPECT_EQ(device->GetGattServices()[0], service2); | 337 EXPECT_EQ(device->GetGattServices()[0], service2); |
338 } | 338 } |
339 #endif // defined(OS_WIN) || defined(OS_MACOSX) | 339 #endif // defined(OS_MACOSX) || defined(OS_WIN) |
| 340 |
| 341 #if defined(OS_MACOSX) |
| 342 // Tests to receive a services changed notification from macOS, while |
| 343 // discovering characteristics. The gatt device should scan again for services |
| 344 // and characteristics, before scanning for descriptors. Only after the gatt |
| 345 // service changed notification should be sent. |
| 346 // Android: This test doesn't apply to Android because there is no services |
| 347 // changed event that could arrive during a discovery procedure. |
| 348 TEST_F(BluetoothRemoteGattServiceTest, |
| 349 SimulateDeviceModificationWhileDiscoveringCharacteristics) { |
| 350 if (!PlatformSupportsLowEnergy()) { |
| 351 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 352 return; |
| 353 } |
| 354 InitWithFakeAdapter(); |
| 355 StartLowEnergyDiscoverySession(); |
| 356 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 357 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 358 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 359 |
| 360 TestBluetoothAdapterObserver observer(adapter_); |
| 361 |
| 362 // Starts first discovery process. |
| 363 SimulateGattConnection(device); |
| 364 AddServicesToDevice(device, {kTestUUIDHeartRate}); |
| 365 SimulateDidDiscoverServicesMac(device); |
| 366 EXPECT_EQ(1u, device->GetGattServices().size()); |
| 367 BluetoothRemoteGattService* service = device->GetGattServices()[0]; |
| 368 std::string characteristic_uuid1 = "11111111-0000-1000-8000-00805f9b34fb"; |
| 369 AddCharacteristicToService(service, characteristic_uuid1, /* properties */ 0); |
| 370 // Now waiting for characteristic discovery. |
| 371 |
| 372 // Starts second discovery process. |
| 373 SimulateGattServicesChanged(device); |
| 374 SimulateDidDiscoverServicesMac(device); |
| 375 // Now waiting for the second characteristic discovery. |
| 376 |
| 377 // First system call to -[id<CBPeripheralDelegate> |
| 378 // peripheral:didDiscoverCharacteristicsForService:error:] |
| 379 SimulateDidDiscoverCharacteristicsMac(service); |
| 380 EXPECT_EQ(0, observer.gatt_service_changed_count()); |
| 381 EXPECT_EQ(1u, service->GetCharacteristics().size()); |
| 382 |
| 383 // Finish discovery process. |
| 384 std::string characteristic_uuid2 = "22222222-0000-1000-8000-00805f9b34fb"; |
| 385 AddCharacteristicToService(service, characteristic_uuid2, /* properties */ 0); |
| 386 // Second system call to -[id<CBPeripheralDelegate> |
| 387 // peripheral:didDiscoverCharacteristicsForService:error:] |
| 388 SimulateDidDiscoverCharacteristicsMac(service); |
| 389 EXPECT_EQ(2u, service->GetCharacteristics().size()); |
| 390 EXPECT_EQ(0, observer.gatt_service_changed_count()); |
| 391 BluetoothRemoteGattCharacteristic* characteristic1 = |
| 392 service->GetCharacteristics()[0]; |
| 393 BluetoothRemoteGattCharacteristic* characteristic2 = |
| 394 service->GetCharacteristics()[1]; |
| 395 if (characteristic1->GetUUID().canonical_value() == characteristic_uuid2) { |
| 396 BluetoothRemoteGattCharacteristic* tmp = characteristic1; |
| 397 characteristic1 = characteristic2; |
| 398 characteristic2 = tmp; |
| 399 } |
| 400 EXPECT_EQ(characteristic_uuid1, characteristic1->GetUUID().canonical_value()); |
| 401 EXPECT_EQ(characteristic_uuid2, characteristic2->GetUUID().canonical_value()); |
| 402 SimulateDidDiscoverDescriptorsMac(characteristic1); |
| 403 SimulateDidDiscoverDescriptorsMac(characteristic2); |
| 404 EXPECT_EQ(1, observer.gatt_service_changed_count()); |
| 405 } |
| 406 #endif // defined(OS_MACOSX) |
| 407 |
| 408 #if defined(OS_MACOSX) |
| 409 // Simulates to receive an extra discovery characteristic notifications from |
| 410 // macOS. Those notifications should be ignored. |
| 411 // Android: This test doesn't apply to Android because there is no services |
| 412 // changed event that could arrive during a discovery procedure. |
| 413 TEST_F(BluetoothRemoteGattServiceTest, ExtraDidDiscoverServicesCall) { |
| 414 if (!PlatformSupportsLowEnergy()) { |
| 415 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 416 return; |
| 417 } |
| 418 InitWithFakeAdapter(); |
| 419 StartLowEnergyDiscoverySession(); |
| 420 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 421 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 422 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 423 |
| 424 TestBluetoothAdapterObserver observer(adapter_); |
| 425 |
| 426 // Starts first discovery process. |
| 427 SimulateGattConnection(device); |
| 428 AddServicesToDevice(device, {kTestUUIDHeartRate}); |
| 429 SimulateDidDiscoverServicesMac(device); |
| 430 EXPECT_EQ(1u, device->GetGattServices().size()); |
| 431 BluetoothRemoteGattService* service = device->GetGattServices()[0]; |
| 432 std::string characteristic_uuid = "11111111-0000-1000-8000-00805f9b34fb"; |
| 433 AddCharacteristicToService(service, characteristic_uuid, /* properties */ 0); |
| 434 SimulateDidDiscoverCharacteristicsMac(service); |
| 435 EXPECT_EQ(1u, service->GetCharacteristics().size()); |
| 436 BluetoothRemoteGattCharacteristic* characteristic = |
| 437 service->GetCharacteristics()[0]; |
| 438 std::string descriptor_uuid = "22222222-0000-1000-8000-00805f9b34fb"; |
| 439 AddDescriptorToCharacteristic(characteristic, descriptor_uuid); |
| 440 SimulateDidDiscoverDescriptorsMac(characteristic); |
| 441 EXPECT_EQ(1, observer.gatt_service_changed_count()); |
| 442 EXPECT_EQ(1u, service->GetCharacteristics().size()); |
| 443 EXPECT_EQ(1u, characteristic->GetDescriptors().size()); |
| 444 |
| 445 observer.Reset(); |
| 446 SimulateDidDiscoverServicesMac(device); // Extra system call. |
| 447 SimulateGattServicesChanged(device); |
| 448 SimulateDidDiscoverServicesMac(device); |
| 449 SimulateDidDiscoverServicesMac(device); // Extra system call. |
| 450 SimulateDidDiscoverCharacteristicsMac(service); |
| 451 SimulateDidDiscoverServicesMac(device); // Extra system call. |
| 452 SimulateDidDiscoverDescriptorsMac(characteristic); |
| 453 SimulateDidDiscoverServicesMac(device); // Extra system call. |
| 454 EXPECT_EQ(2, observer.device_changed_count()); |
| 455 } |
| 456 #endif // defined(OS_MACOSX) |
| 457 |
| 458 #if defined(OS_MACOSX) |
| 459 // Simulates to receive an extra discovery characteristic notifications from |
| 460 // macOS. Those notifications should be ignored. |
| 461 // Android: This test doesn't apply to Android because there is no services |
| 462 // changed event that could arrive during a discovery procedure. |
| 463 TEST_F(BluetoothRemoteGattServiceTest, ExtraDidDiscoverCharacteristicsCall) { |
| 464 if (!PlatformSupportsLowEnergy()) { |
| 465 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 466 return; |
| 467 } |
| 468 InitWithFakeAdapter(); |
| 469 StartLowEnergyDiscoverySession(); |
| 470 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 471 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 472 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 473 |
| 474 TestBluetoothAdapterObserver observer(adapter_); |
| 475 |
| 476 // Starts first discovery process. |
| 477 SimulateGattConnection(device); |
| 478 AddServicesToDevice(device, {kTestUUIDHeartRate}); |
| 479 SimulateDidDiscoverServicesMac(device); |
| 480 EXPECT_EQ(1u, device->GetGattServices().size()); |
| 481 BluetoothRemoteGattService* service = device->GetGattServices()[0]; |
| 482 std::string characteristic_uuid = "11111111-0000-1000-8000-00805f9b34fb"; |
| 483 AddCharacteristicToService(service, characteristic_uuid, /* properties */ 0); |
| 484 SimulateDidDiscoverCharacteristicsMac(service); |
| 485 EXPECT_EQ(1u, service->GetCharacteristics().size()); |
| 486 BluetoothRemoteGattCharacteristic* characteristic = |
| 487 service->GetCharacteristics()[0]; |
| 488 std::string descriptor_uuid = "22222222-0000-1000-8000-00805f9b34fb"; |
| 489 AddDescriptorToCharacteristic(characteristic, descriptor_uuid); |
| 490 SimulateDidDiscoverDescriptorsMac(characteristic); |
| 491 EXPECT_EQ(1, observer.gatt_service_changed_count()); |
| 492 EXPECT_EQ(1u, service->GetCharacteristics().size()); |
| 493 EXPECT_EQ(1u, characteristic->GetDescriptors().size()); |
| 494 |
| 495 observer.Reset(); |
| 496 SimulateDidDiscoverCharacteristicsMac(service); // Extra system call. |
| 497 SimulateGattServicesChanged(device); |
| 498 SimulateDidDiscoverCharacteristicsMac(service); // Extra system call. |
| 499 SimulateDidDiscoverServicesMac(device); |
| 500 SimulateDidDiscoverCharacteristicsMac(service); |
| 501 SimulateDidDiscoverCharacteristicsMac(service); // Extra system call. |
| 502 SimulateDidDiscoverDescriptorsMac(characteristic); |
| 503 SimulateDidDiscoverCharacteristicsMac(service); // Extra system call. |
| 504 EXPECT_EQ(2, observer.device_changed_count()); |
| 505 } |
| 506 #endif // defined(OS_MACOSX) |
340 | 507 |
341 } // namespace device | 508 } // namespace device |
OLD | NEW |