| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "device/bluetooth/bluetooth_device.h" | 5 #include "device/bluetooth/bluetooth_device.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 476 base::RunLoop().RunUntilIdle(); | 476 base::RunLoop().RunUntilIdle(); |
| 477 ASSERT_FALSE(device->IsGattConnected()); | 477 ASSERT_FALSE(device->IsGattConnected()); |
| 478 | 478 |
| 479 EXPECT_EQ(1, observer.device_changed_count()); | 479 EXPECT_EQ(1, observer.device_changed_count()); |
| 480 EXPECT_TRUE(device->GetUUIDs().empty()); | 480 EXPECT_TRUE(device->GetUUIDs().empty()); |
| 481 } | 481 } |
| 482 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 482 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| 483 | 483 |
| 484 #if defined(OS_MACOSX) | 484 #if defined(OS_MACOSX) |
| 485 // Tests that receiving 2 notifications in a row from macOS that services has | 485 // Tests that receiving 2 notifications in a row from macOS that services has |
| 486 // changed is handled correctly. Each notification should generate a notfication | 486 // changed is handled correctly. Each notification should generate a |
| 487 // that the gatt device has changed, and each notification should ask to macOS | 487 // notification that the gatt device has changed, and each notification should |
| 488 // to scan for services. Only after the second service scan is received, the | 488 // ask to macOS to scan for services. Only after the second service scan is |
| 489 // device changed notification should be sent and the characteristic discovery | 489 // received, the device changed notification should be sent and the |
| 490 // procedure should be started. | 490 // characteristic discovery procedure should be started. |
| 491 // Android: This test doesn't apply to Android because there is no services | 491 // Android: This test doesn't apply to Android because there is no services |
| 492 // changed event that could arrive during a discovery procedure. | 492 // changed event that could arrive during a discovery procedure. |
| 493 TEST_F(BluetoothTest, TwoPendingServiceDiscoveryRequests) { | 493 TEST_F(BluetoothTest, TwoPendingServiceDiscoveryRequests) { |
| 494 if (!PlatformSupportsLowEnergy()) { | 494 if (!PlatformSupportsLowEnergy()) { |
| 495 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 495 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 496 return; | 496 return; |
| 497 } | 497 } |
| 498 | 498 |
| 499 InitWithFakeAdapter(); | 499 InitWithFakeAdapter(); |
| 500 TestBluetoothAdapterObserver observer(adapter_); | 500 TestBluetoothAdapterObserver observer(adapter_); |
| 501 | 501 |
| 502 BluetoothDevice* device = SimulateLowEnergyDevice(1); | 502 BluetoothDevice* device = SimulateLowEnergyDevice(1); |
| 503 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 503 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 504 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 504 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 505 SimulateGattConnection(device); | 505 SimulateGattConnection(device); |
| 506 EXPECT_EQ(1, observer.device_changed_count()); | 506 EXPECT_EQ(1, observer.device_changed_count()); |
| 507 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); | 507 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); |
| 508 | 508 |
| 509 observer.Reset(); | 509 observer.Reset(); |
| 510 SimulateGattServicesChanged(device); | 510 SimulateGattServicesChanged(device); |
| 511 EXPECT_EQ(1, observer.device_changed_count()); | 511 EXPECT_EQ(1, observer.device_changed_count()); |
| 512 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); | 512 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); |
| 513 | 513 |
| 514 // Fist system call to | 514 // First system call to |
| 515 // -[id<CBPeripheralDelegate> peripheral:didDiscoverServices:] | 515 // -[id<CBPeripheralDelegate> peripheral:didDiscoverServices:] using |
| 516 // SimulateDidDiscoverServicesMac(). |
| 516 observer.Reset(); | 517 observer.Reset(); |
| 517 SimulateDidDiscoverServices(device, {kTestUUIDHeartRate}); | 518 AddServicesToDeviceMac(device, {kTestUUIDHeartRate}); |
| 519 SimulateDidDiscoverServicesMac(device); |
| 518 EXPECT_EQ(0, observer.device_changed_count()); | 520 EXPECT_EQ(0, observer.device_changed_count()); |
| 519 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); | 521 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); |
| 520 EXPECT_EQ(gatt_characteristic_discovery_attempts_, 0); | 522 EXPECT_EQ(gatt_characteristic_discovery_attempts_, 0); |
| 521 | 523 |
| 522 // Second system call to | 524 // Second system call to |
| 523 // -[id<CBPeripheralDelegate> peripheral:didDiscoverServices:] | 525 // -[id<CBPeripheralDelegate> peripheral:didDiscoverServices:] using the |
| 526 // generic call to SimulateGattServicesDiscovered(). This method triggers |
| 527 // the full discovery cycles (services, characteristics and descriptors), |
| 528 // which includes -[id<CBPeripheralDelegate> peripheral:didDiscoverServices:]. |
| 524 SimulateGattServicesDiscovered( | 529 SimulateGattServicesDiscovered( |
| 525 device, std::vector<std::string>({kTestUUIDImmediateAlert})); | 530 device, std::vector<std::string>({kTestUUIDImmediateAlert})); |
| 526 EXPECT_EQ(1, observer.device_changed_count()); | 531 EXPECT_EQ(1, observer.device_changed_count()); |
| 527 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); | 532 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); |
| 528 // Characteristics are discovered once for each service. | 533 // Characteristics are discovered once for each service. |
| 529 EXPECT_EQ(gatt_characteristic_discovery_attempts_, 2); | 534 EXPECT_EQ(gatt_characteristic_discovery_attempts_, 2); |
| 530 | 535 |
| 531 EXPECT_EQ(2u, device->GetGattServices().size()); | 536 EXPECT_EQ(2u, device->GetGattServices().size()); |
| 532 } | 537 } |
| 533 | 538 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 544 TestBluetoothAdapterObserver observer(adapter_); | 549 TestBluetoothAdapterObserver observer(adapter_); |
| 545 | 550 |
| 546 BluetoothDevice* device = SimulateLowEnergyDevice(1); | 551 BluetoothDevice* device = SimulateLowEnergyDevice(1); |
| 547 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 552 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 548 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 553 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 549 SimulateGattConnection(device); | 554 SimulateGattConnection(device); |
| 550 EXPECT_EQ(1, observer.device_changed_count()); | 555 EXPECT_EQ(1, observer.device_changed_count()); |
| 551 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); | 556 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); |
| 552 | 557 |
| 553 // Legitimate system call to | 558 // Legitimate system call to |
| 554 // -[id<CBPeripheralDelegate> peripheral:didDiscoverServices:] | 559 // -[id<CBPeripheralDelegate> peripheral:didDiscoverServices:]. |
| 555 observer.Reset(); | 560 observer.Reset(); |
| 556 SimulateGattServicesDiscovered( | 561 SimulateGattServicesDiscovered( |
| 557 device, std::vector<std::string>({kTestUUIDHeartRate})); | 562 device, std::vector<std::string>({kTestUUIDHeartRate})); |
| 558 EXPECT_EQ(1, observer.device_changed_count()); | 563 EXPECT_EQ(1, observer.device_changed_count()); |
| 559 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); | 564 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); |
| 560 EXPECT_EQ(gatt_characteristic_discovery_attempts_, 1); | 565 EXPECT_EQ(gatt_characteristic_discovery_attempts_, 1); |
| 561 EXPECT_EQ(1u, device->GetGattServices().size()); | 566 EXPECT_EQ(1u, device->GetGattServices().size()); |
| 562 | 567 |
| 563 // Unexpected system call to | 568 // Unexpected system call to |
| 564 // -[id<CBPeripheralDelegate> peripheral:didDiscoverServices:] | 569 // -[id<CBPeripheralDelegate> peripheral:didDiscoverServices:]: |
| 565 SimulateDidDiscoverServices(device, {kTestUUIDImmediateAlert}); | 570 // This system call is expected only once after -[CBCentralManager |
| 571 // discoverServices:]. The call to -[CBCentralManager discoverServices:] and |
| 572 // its answer with -[id<CBPeripheralDelegate> peripheral:didDiscoverServices:] |
| 573 // is done with SimulateGattServicesDiscovered(). So a second system call to |
| 574 // -[id<CBPeripheralDelegate> peripheral:didDiscoverServices:] is not expected |
| 575 // and should be ignored. |
| 576 AddServicesToDeviceMac(device, {kTestUUIDImmediateAlert}); |
| 577 SimulateDidDiscoverServicesMac(device); |
| 566 EXPECT_EQ(1, observer.device_changed_count()); | 578 EXPECT_EQ(1, observer.device_changed_count()); |
| 567 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); | 579 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); |
| 568 | 580 |
| 569 EXPECT_EQ(1u, device->GetGattServices().size()); | 581 EXPECT_EQ(1u, device->GetGattServices().size()); |
| 570 } | 582 } |
| 571 #endif // defined(OS_MACOSX) | 583 #endif // defined(OS_MACOSX) |
| 572 | 584 |
| 573 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 585 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 574 // Tests Advertisement Data is updated correctly when we start discovery | 586 // Tests Advertisement Data is updated correctly when we start discovery |
| 575 // during a connection. | 587 // during a connection. |
| (...skipping 1022 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1598 ->GetPrimaryServicesByUUID(BluetoothUUID( | 1610 ->GetPrimaryServicesByUUID(BluetoothUUID( |
| 1599 BluetoothTestBase::kTestUUIDGenericAttribute)) | 1611 BluetoothTestBase::kTestUUIDGenericAttribute)) |
| 1600 .empty()); | 1612 .empty()); |
| 1601 | 1613 |
| 1602 EXPECT_NE(services[0]->GetIdentifier(), services[1]->GetIdentifier()); | 1614 EXPECT_NE(services[0]->GetIdentifier(), services[1]->GetIdentifier()); |
| 1603 } | 1615 } |
| 1604 } | 1616 } |
| 1605 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) | 1617 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) |
| 1606 | 1618 |
| 1607 } // namespace device | 1619 } // namespace device |
| OLD | NEW |