 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) 
  | Index: device/bluetooth/bluetooth_device_unittest.cc | 
| diff --git a/device/bluetooth/bluetooth_device_unittest.cc b/device/bluetooth/bluetooth_device_unittest.cc | 
| index ab478fc7386df4a49d9bf91a0645488354e48239..538d491fb8b2a094b3fb74d9734ca1ef5976f591 100644 | 
| --- a/device/bluetooth/bluetooth_device_unittest.cc | 
| +++ b/device/bluetooth/bluetooth_device_unittest.cc | 
| @@ -46,9 +46,13 @@ int8_t ToInt8(BluetoothTest::TestTxPower tx_power) { | 
| using UUIDSet = BluetoothDevice::UUIDSet; | 
| using ServiceDataMap = BluetoothDevice::ServiceDataMap; | 
| -class BluetoothGetServiceOrCharacteristicTest : public BluetoothTest { | 
| +class BluetoothGetServiceTest : public BluetoothTest { | 
| public: | 
| - // Creates |device_|, |services_|. | 
| + BluetoothGetServiceTest() | 
| + : unique_service_uuid_(kTestUUIDGenericAccess), | 
| + duplicate_service_uuid_(kTestUUIDHeartRate) {} | 
| + | 
| + // Creates |device_|, |service_uuids_|. | 
| void FakeServiceBoilerplate() { | 
| InitWithFakeAdapter(); | 
| StartLowEnergyDiscoverySession(); | 
| @@ -62,21 +66,34 @@ class BluetoothGetServiceOrCharacteristicTest : public BluetoothTest { | 
| TestBluetoothAdapterObserver observer(adapter_); | 
| SimulateGattConnection(device_); | 
| base::RunLoop().RunUntilIdle(); | 
| +#if defined(OS_WIN) | 
| + // TODO(crbug.com/507419): Check connection once CreateGattConnection is | 
| + // implemented on Windows. | 
| + EXPECT_FALSE(device_->IsConnected()); | 
| +#else | 
| EXPECT_TRUE(device_->IsConnected()); | 
| +#endif // defined(OS_WIN) | 
| // Discover services. | 
| - services_.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 
| + service_uuids_.push_back(unique_service_uuid_.canonical_value()); | 
| // 2 duplicate UUIDs creating 2 instances. | 
| - services_.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 
| - services_.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 
| - SimulateGattServicesDiscovered(device_, services_); | 
| + service_uuids_.push_back(duplicate_service_uuid_.canonical_value()); | 
| + service_uuids_.push_back(duplicate_service_uuid_.canonical_value()); | 
| + 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.
 | 
| base::RunLoop().RunUntilIdle(); | 
| +#if defined(OS_WIN) | 
| 
ortuno
2017/04/05 00:53:10
Also add the TODO here.
 
juncai
2017/04/05 19:01:23
Done.
 | 
| + EXPECT_FALSE(device_->IsGattServicesDiscoveryComplete()); | 
| +#else | 
| EXPECT_TRUE(device_->IsGattServicesDiscoveryComplete()); | 
| +#endif // defined(OS_WIN) | 
| } | 
| protected: | 
| + BluetoothUUID unique_service_uuid_; | 
| + BluetoothUUID duplicate_service_uuid_; | 
| + | 
| BluetoothDevice* device_ = nullptr; | 
| - std::vector<std::string> services_; | 
| + std::vector<std::string> service_uuids_; | 
| }; | 
| TEST(BluetoothDeviceTest, CanonicalizeAddressFormat_AcceptsAllValidFormats) { | 
| @@ -1516,8 +1533,8 @@ TEST_F(BluetoothTest, GetDeviceTransportType) { | 
| } | 
| #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 
| -#if defined(OS_ANDROID) || defined(OS_MACOSX) | 
| -TEST_F(BluetoothGetServiceOrCharacteristicTest, GetPrimaryServices) { | 
| +#if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) | 
| +TEST_F(BluetoothGetServiceTest, GetPrimaryServices) { | 
| if (!PlatformSupportsLowEnergy()) { | 
| LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 
| return; | 
| @@ -1527,96 +1544,35 @@ TEST_F(BluetoothGetServiceOrCharacteristicTest, GetPrimaryServices) { | 
| EXPECT_EQ(3u, device_->GetPrimaryServices().size()); | 
| } | 
| -TEST_F(BluetoothGetServiceOrCharacteristicTest, GetPrimaryServicesByUUID) { | 
| +TEST_F(BluetoothGetServiceTest, GetPrimaryServicesByUUID) { | 
| if (!PlatformSupportsLowEnergy()) { | 
| LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 
| return; | 
| } | 
| ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate()); | 
| - EXPECT_EQ( | 
| - 1u, | 
| - device_->GetPrimaryServicesByUUID(BluetoothUUID(services_[0])).size()); | 
| - EXPECT_EQ( | 
| - 2u, | 
| - device_->GetPrimaryServicesByUUID(BluetoothUUID(services_[1])).size()); | 
| - std::string service_uuid_not_exist_in_setup = | 
| - "00000002-0000-1000-8000-00805f9b34fb"; | 
| - EXPECT_TRUE(device_ | 
| - ->GetPrimaryServicesByUUID( | 
| - BluetoothUUID(service_uuid_not_exist_in_setup)) | 
| - .empty()); | 
| -} | 
| - | 
| -TEST_F(BluetoothGetServiceOrCharacteristicTest, GetCharacteristicsByUUID) { | 
| - if (!PlatformSupportsLowEnergy()) { | 
| - LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 
| - return; | 
| + { | 
| + std::vector<BluetoothRemoteGattService*> services = | 
| + device_->GetPrimaryServicesByUUID(unique_service_uuid_); | 
| + EXPECT_EQ(1u, services.size()); | 
| + EXPECT_EQ(unique_service_uuid_, services[0]->GetUUID()); | 
| } | 
| - ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate()); | 
| - std::vector<BluetoothRemoteGattService*> primary_services = | 
| - device_->GetPrimaryServices(); | 
| - std::string service_instance_id0 = primary_services[0]->GetIdentifier(); | 
| - std::string service_instance_id1 = primary_services[1]->GetIdentifier(); | 
| - std::string service_instance_id2 = primary_services[2]->GetIdentifier(); | 
| - | 
| - std::string characteristic_uuid0 = "00000002-0000-1000-8000-00805f9b34fb"; | 
| - std::string characteristic_uuid1 = "00000003-0000-1000-8000-00805f9b34fb"; | 
| - SimulateGattCharacteristic(primary_services[0], characteristic_uuid0, | 
| - 0 /* properties */); | 
| - SimulateGattCharacteristic(primary_services[1], characteristic_uuid1, | 
| - 0 /* properties */); | 
| - SimulateGattCharacteristic(primary_services[2], characteristic_uuid1, | 
| - 0 /* properties */); | 
| - | 
| - EXPECT_EQ(1u, | 
| - device_ | 
| - ->GetCharacteristicsByUUID(service_instance_id0, | 
| - BluetoothUUID(characteristic_uuid0)) | 
| - .size()); | 
| - EXPECT_EQ(1u, | 
| - device_ | 
| - ->GetCharacteristicsByUUID(service_instance_id1, | 
| - BluetoothUUID(characteristic_uuid1)) | 
| - .size()); | 
| - EXPECT_EQ(1u, | 
| - device_ | 
| - ->GetCharacteristicsByUUID(service_instance_id2, | 
| - BluetoothUUID(characteristic_uuid1)) | 
| - .size()); | 
| - | 
| - std::string service_instance_id_not_exist_in_setup = | 
| - "non-existent platform specific service instance id"; | 
| - EXPECT_TRUE( | 
| - device_ | 
| - ->GetCharacteristicsByUUID(service_instance_id_not_exist_in_setup, | 
| - BluetoothUUID(characteristic_uuid0)) | 
| - .empty()); | 
| - EXPECT_TRUE( | 
| - device_ | 
| - ->GetCharacteristicsByUUID(service_instance_id_not_exist_in_setup, | 
| - BluetoothUUID(characteristic_uuid1)) | 
| - .empty()); | 
| - | 
| - std::string characteristic_uuid_not_exist_in_setup = | 
| - "00000005-0000-1000-8000-00805f9b34fb"; | 
| - EXPECT_TRUE(device_ | 
| - ->GetCharacteristicsByUUID( | 
| - service_instance_id0, | 
| - BluetoothUUID(characteristic_uuid_not_exist_in_setup)) | 
| - .empty()); | 
| - EXPECT_TRUE(device_ | 
| - ->GetCharacteristicsByUUID( | 
| - service_instance_id1, | 
| - BluetoothUUID(characteristic_uuid_not_exist_in_setup)) | 
| - .empty()); | 
| - EXPECT_TRUE(device_ | 
| - ->GetCharacteristicsByUUID( | 
| - service_instance_id2, | 
| - BluetoothUUID(characteristic_uuid_not_exist_in_setup)) | 
| - .empty()); | 
| + { | 
| + std::vector<BluetoothRemoteGattService*> services = | 
| + device_->GetPrimaryServicesByUUID(duplicate_service_uuid_); | 
| + EXPECT_EQ(2u, services.size()); | 
| + EXPECT_EQ(duplicate_service_uuid_, services[0]->GetUUID()); | 
| + EXPECT_EQ(duplicate_service_uuid_, services[1]->GetUUID()); | 
| + | 
| + EXPECT_TRUE(device_ | 
| + ->GetPrimaryServicesByUUID(BluetoothUUID( | 
| + BluetoothTestBase::kTestUUIDGenericAttribute)) | 
| + .empty()); | 
| + | 
| + EXPECT_NE(services[0]->GetIdentifier(), services[1]->GetIdentifier()); | 
| + } | 
| } | 
| -#endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 
| +#endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) | 
| } // namespace device |