 Chromium Code Reviews
 Chromium Code Reviews Issue 2785383002:
  Update unit tests for BluetoothDevice::GetPrimaryServicesByUUID() etc.  (Closed)
    
  
    Issue 2785383002:
  Update unit tests for BluetoothDevice::GetPrimaryServicesByUUID() etc.  (Closed) 
  | 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 28 matching lines...) Expand all Loading... | |
| 39 return static_cast<int8_t>(tx_power); | 39 return static_cast<int8_t>(tx_power); | 
| 40 } | 40 } | 
| 41 | 41 | 
| 42 } // namespace | 42 } // namespace | 
| 43 | 43 | 
| 44 #endif | 44 #endif | 
| 45 | 45 | 
| 46 using UUIDSet = BluetoothDevice::UUIDSet; | 46 using UUIDSet = BluetoothDevice::UUIDSet; | 
| 47 using ServiceDataMap = BluetoothDevice::ServiceDataMap; | 47 using ServiceDataMap = BluetoothDevice::ServiceDataMap; | 
| 48 | 48 | 
| 49 class BluetoothGetServiceOrCharacteristicTest : public BluetoothTest { | 49 class BluetoothGetServiceTest : public BluetoothTest { | 
| 50 public: | 50 public: | 
| 51 // Creates |device_|, |services_|. | 51 BluetoothGetServiceTest() | 
| 52 : unique_service_uuid_(kTestUUIDGenericAccess), | |
| 53 duplicate_service_uuid_(kTestUUIDHeartRate) {} | |
| 54 | |
| 55 // Creates |device_|, |service_uuids_|. | |
| 52 void FakeServiceBoilerplate() { | 56 void FakeServiceBoilerplate() { | 
| 53 InitWithFakeAdapter(); | 57 InitWithFakeAdapter(); | 
| 54 StartLowEnergyDiscoverySession(); | 58 StartLowEnergyDiscoverySession(); | 
| 55 device_ = SimulateLowEnergyDevice(3); | 59 device_ = SimulateLowEnergyDevice(3); | 
| 56 EXPECT_FALSE(device_->IsConnected()); | 60 EXPECT_FALSE(device_->IsConnected()); | 
| 57 | 61 | 
| 58 // Connect to the device. | 62 // Connect to the device. | 
| 59 ResetEventCounts(); | 63 ResetEventCounts(); | 
| 60 device_->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 64 device_->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 
| 61 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 65 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 
| 62 TestBluetoothAdapterObserver observer(adapter_); | 66 TestBluetoothAdapterObserver observer(adapter_); | 
| 63 SimulateGattConnection(device_); | 67 SimulateGattConnection(device_); | 
| 64 base::RunLoop().RunUntilIdle(); | 68 base::RunLoop().RunUntilIdle(); | 
| 69 #if defined(OS_WIN) | |
| 70 // TODO(crbug.com/507419): Check connection once CreateGattConnection is | |
| 71 // implemented on Windows. | |
| 72 EXPECT_FALSE(device_->IsConnected()); | |
| 73 #else | |
| 65 EXPECT_TRUE(device_->IsConnected()); | 74 EXPECT_TRUE(device_->IsConnected()); | 
| 75 #endif // defined(OS_WIN) | |
| 66 | 76 | 
| 67 // Discover services. | 77 // Discover services. | 
| 68 services_.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 78 service_uuids_.push_back(unique_service_uuid_.canonical_value()); | 
| 69 // 2 duplicate UUIDs creating 2 instances. | 79 // 2 duplicate UUIDs creating 2 instances. | 
| 70 services_.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 80 service_uuids_.push_back(duplicate_service_uuid_.canonical_value()); | 
| 71 services_.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 81 service_uuids_.push_back(duplicate_service_uuid_.canonical_value()); | 
| 72 SimulateGattServicesDiscovered(device_, services_); | 82 SimulateGattServicesDiscovered(device_, service_uuids_); | 
| 
ortuno
2017/04/05 00:53:10
service_uuids_ seems unused outside of the constru
 
juncai
2017/04/05 19:01:23
Done.
 | |
| 73 base::RunLoop().RunUntilIdle(); | 83 base::RunLoop().RunUntilIdle(); | 
| 84 #if defined(OS_WIN) | |
| 
ortuno
2017/04/05 00:53:10
Also add the TODO here.
 
juncai
2017/04/05 19:01:23
Done.
 | |
| 85 EXPECT_FALSE(device_->IsGattServicesDiscoveryComplete()); | |
| 86 #else | |
| 74 EXPECT_TRUE(device_->IsGattServicesDiscoveryComplete()); | 87 EXPECT_TRUE(device_->IsGattServicesDiscoveryComplete()); | 
| 88 #endif // defined(OS_WIN) | |
| 75 } | 89 } | 
| 76 | 90 | 
| 77 protected: | 91 protected: | 
| 92 BluetoothUUID unique_service_uuid_; | |
| 93 BluetoothUUID duplicate_service_uuid_; | |
| 94 | |
| 78 BluetoothDevice* device_ = nullptr; | 95 BluetoothDevice* device_ = nullptr; | 
| 79 std::vector<std::string> services_; | 96 std::vector<std::string> service_uuids_; | 
| 80 }; | 97 }; | 
| 81 | 98 | 
| 82 TEST(BluetoothDeviceTest, CanonicalizeAddressFormat_AcceptsAllValidFormats) { | 99 TEST(BluetoothDeviceTest, CanonicalizeAddressFormat_AcceptsAllValidFormats) { | 
| 83 // There are three valid separators (':', '-', and none). | 100 // There are three valid separators (':', '-', and none). | 
| 84 // Case shouldn't matter. | 101 // Case shouldn't matter. | 
| 85 const char* const kValidFormats[] = { | 102 const char* const kValidFormats[] = { | 
| 86 "1A:2B:3C:4D:5E:6F", | 103 "1A:2B:3C:4D:5E:6F", | 
| 87 "1a:2B:3c:4D:5e:6F", | 104 "1a:2B:3c:4D:5e:6F", | 
| 88 "1a:2b:3c:4d:5e:6f", | 105 "1a:2b:3c:4d:5e:6f", | 
| 89 "1A-2B-3C-4D-5E-6F", | 106 "1A-2B-3C-4D-5E-6F", | 
| (...skipping 1419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1509 EXPECT_EQ(BLUETOOTH_TRANSPORT_LE, device->GetType()); | 1526 EXPECT_EQ(BLUETOOTH_TRANSPORT_LE, device->GetType()); | 
| 1510 | 1527 | 
| 1511 BluetoothDevice* device2 = SimulateLowEnergyDevice(6); | 1528 BluetoothDevice* device2 = SimulateLowEnergyDevice(6); | 
| 1512 EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, device2->GetType()); | 1529 EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, device2->GetType()); | 
| 1513 | 1530 | 
| 1514 BluetoothDevice* device3 = SimulateClassicDevice(); | 1531 BluetoothDevice* device3 = SimulateClassicDevice(); | 
| 1515 EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, device3->GetType()); | 1532 EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, device3->GetType()); | 
| 1516 } | 1533 } | 
| 1517 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 1534 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 
| 1518 | 1535 | 
| 1519 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 1536 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) | 
| 1520 TEST_F(BluetoothGetServiceOrCharacteristicTest, GetPrimaryServices) { | 1537 TEST_F(BluetoothGetServiceTest, GetPrimaryServices) { | 
| 1521 if (!PlatformSupportsLowEnergy()) { | 1538 if (!PlatformSupportsLowEnergy()) { | 
| 1522 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 1539 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 
| 1523 return; | 1540 return; | 
| 1524 } | 1541 } | 
| 1525 ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate()); | 1542 ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate()); | 
| 1526 | 1543 | 
| 1527 EXPECT_EQ(3u, device_->GetPrimaryServices().size()); | 1544 EXPECT_EQ(3u, device_->GetPrimaryServices().size()); | 
| 1528 } | 1545 } | 
| 1529 | 1546 | 
| 1530 TEST_F(BluetoothGetServiceOrCharacteristicTest, GetPrimaryServicesByUUID) { | 1547 TEST_F(BluetoothGetServiceTest, GetPrimaryServicesByUUID) { | 
| 1531 if (!PlatformSupportsLowEnergy()) { | 1548 if (!PlatformSupportsLowEnergy()) { | 
| 1532 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 1549 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 
| 1533 return; | 1550 return; | 
| 1534 } | 1551 } | 
| 1535 ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate()); | 1552 ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate()); | 
| 1536 | 1553 | 
| 1537 EXPECT_EQ( | 1554 { | 
| 1538 1u, | 1555 std::vector<BluetoothRemoteGattService*> services = | 
| 1539 device_->GetPrimaryServicesByUUID(BluetoothUUID(services_[0])).size()); | 1556 device_->GetPrimaryServicesByUUID(unique_service_uuid_); | 
| 1540 EXPECT_EQ( | 1557 EXPECT_EQ(1u, services.size()); | 
| 1541 2u, | 1558 EXPECT_EQ(unique_service_uuid_, services[0]->GetUUID()); | 
| 1542 device_->GetPrimaryServicesByUUID(BluetoothUUID(services_[1])).size()); | 1559 } | 
| 1543 std::string service_uuid_not_exist_in_setup = | 1560 | 
| 1544 "00000002-0000-1000-8000-00805f9b34fb"; | 1561 { | 
| 1545 EXPECT_TRUE(device_ | 1562 std::vector<BluetoothRemoteGattService*> services = | 
| 1546 ->GetPrimaryServicesByUUID( | 1563 device_->GetPrimaryServicesByUUID(duplicate_service_uuid_); | 
| 1547 BluetoothUUID(service_uuid_not_exist_in_setup)) | 1564 EXPECT_EQ(2u, services.size()); | 
| 1548 .empty()); | 1565 EXPECT_EQ(duplicate_service_uuid_, services[0]->GetUUID()); | 
| 1566 EXPECT_EQ(duplicate_service_uuid_, services[1]->GetUUID()); | |
| 1567 | |
| 1568 EXPECT_TRUE(device_ | |
| 1569 ->GetPrimaryServicesByUUID(BluetoothUUID( | |
| 1570 BluetoothTestBase::kTestUUIDGenericAttribute)) | |
| 1571 .empty()); | |
| 1572 | |
| 1573 EXPECT_NE(services[0]->GetIdentifier(), services[1]->GetIdentifier()); | |
| 1574 } | |
| 1549 } | 1575 } | 
| 1550 | 1576 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) | 
| 1551 TEST_F(BluetoothGetServiceOrCharacteristicTest, GetCharacteristicsByUUID) { | |
| 1552 if (!PlatformSupportsLowEnergy()) { | |
| 1553 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | |
| 1554 return; | |
| 1555 } | |
| 1556 ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate()); | |
| 1557 | |
| 1558 std::vector<BluetoothRemoteGattService*> primary_services = | |
| 1559 device_->GetPrimaryServices(); | |
| 1560 std::string service_instance_id0 = primary_services[0]->GetIdentifier(); | |
| 1561 std::string service_instance_id1 = primary_services[1]->GetIdentifier(); | |
| 1562 std::string service_instance_id2 = primary_services[2]->GetIdentifier(); | |
| 1563 | |
| 1564 std::string characteristic_uuid0 = "00000002-0000-1000-8000-00805f9b34fb"; | |
| 1565 std::string characteristic_uuid1 = "00000003-0000-1000-8000-00805f9b34fb"; | |
| 1566 SimulateGattCharacteristic(primary_services[0], characteristic_uuid0, | |
| 1567 0 /* properties */); | |
| 1568 SimulateGattCharacteristic(primary_services[1], characteristic_uuid1, | |
| 1569 0 /* properties */); | |
| 1570 SimulateGattCharacteristic(primary_services[2], characteristic_uuid1, | |
| 1571 0 /* properties */); | |
| 1572 | |
| 1573 EXPECT_EQ(1u, | |
| 1574 device_ | |
| 1575 ->GetCharacteristicsByUUID(service_instance_id0, | |
| 1576 BluetoothUUID(characteristic_uuid0)) | |
| 1577 .size()); | |
| 1578 EXPECT_EQ(1u, | |
| 1579 device_ | |
| 1580 ->GetCharacteristicsByUUID(service_instance_id1, | |
| 1581 BluetoothUUID(characteristic_uuid1)) | |
| 1582 .size()); | |
| 1583 EXPECT_EQ(1u, | |
| 1584 device_ | |
| 1585 ->GetCharacteristicsByUUID(service_instance_id2, | |
| 1586 BluetoothUUID(characteristic_uuid1)) | |
| 1587 .size()); | |
| 1588 | |
| 1589 std::string service_instance_id_not_exist_in_setup = | |
| 1590 "non-existent platform specific service instance id"; | |
| 1591 EXPECT_TRUE( | |
| 1592 device_ | |
| 1593 ->GetCharacteristicsByUUID(service_instance_id_not_exist_in_setup, | |
| 1594 BluetoothUUID(characteristic_uuid0)) | |
| 1595 .empty()); | |
| 1596 EXPECT_TRUE( | |
| 1597 device_ | |
| 1598 ->GetCharacteristicsByUUID(service_instance_id_not_exist_in_setup, | |
| 1599 BluetoothUUID(characteristic_uuid1)) | |
| 1600 .empty()); | |
| 1601 | |
| 1602 std::string characteristic_uuid_not_exist_in_setup = | |
| 1603 "00000005-0000-1000-8000-00805f9b34fb"; | |
| 1604 EXPECT_TRUE(device_ | |
| 1605 ->GetCharacteristicsByUUID( | |
| 1606 service_instance_id0, | |
| 1607 BluetoothUUID(characteristic_uuid_not_exist_in_setup)) | |
| 1608 .empty()); | |
| 1609 EXPECT_TRUE(device_ | |
| 1610 ->GetCharacteristicsByUUID( | |
| 1611 service_instance_id1, | |
| 1612 BluetoothUUID(characteristic_uuid_not_exist_in_setup)) | |
| 1613 .empty()); | |
| 1614 EXPECT_TRUE(device_ | |
| 1615 ->GetCharacteristicsByUUID( | |
| 1616 service_instance_id2, | |
| 1617 BluetoothUUID(characteristic_uuid_not_exist_in_setup)) | |
| 1618 .empty()); | |
| 1619 } | |
| 1620 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | |
| 1621 | 1577 | 
| 1622 } // namespace device | 1578 } // namespace device | 
| OLD | NEW |