Chromium Code Reviews| Index: device/bluetooth/bluetooth_device_unittest.cc |
| diff --git a/device/bluetooth/bluetooth_device_unittest.cc b/device/bluetooth/bluetooth_device_unittest.cc |
| index 9dcb4a1a7be0f094445f25151c8061088db20c55..b0193943559f0df509d6b407c8def2c00286055b 100644 |
| --- a/device/bluetooth/bluetooth_device_unittest.cc |
| +++ b/device/bluetooth/bluetooth_device_unittest.cc |
| @@ -1483,4 +1483,158 @@ TEST_F(BluetoothTest, GetDeviceTransportType) { |
| } |
| #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
| +#if defined(OS_ANDROID) || defined(OS_MACOSX) |
| +TEST_F(BluetoothTest, GetPrimaryServices) { |
| + if (!PlatformSupportsLowEnergy()) { |
| + LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| + return; |
| + } |
| + InitWithFakeAdapter(); |
|
scheib
2017/03/17 17:38:40
There's a lot of boilerplate for these tests. Cons
juncai
2017/03/17 21:25:47
I think the above 5 lines are also repeated in oth
scheib
2017/03/17 22:10:47
Look at e.g. the test fixture setup done for the c
juncai
2017/03/17 23:33:54
Done.
|
| + StartLowEnergyDiscoverySession(); |
| + BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| + EXPECT_FALSE(device->IsConnected()); |
| + |
| + // Connect to the device |
| + ResetEventCounts(); |
| + device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| + GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| + TestBluetoothAdapterObserver observer(adapter_); |
| + SimulateGattConnection(device); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_TRUE(device->IsConnected()); |
| + |
| + // Discover services |
| + std::vector<std::string> services; |
| + services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
| + services.push_back("00000001-0000-1000-8000-00805f9b34fb"); |
|
scheib
2017/03/17 17:38:40
multiple services, same ID. So, probably use 1 uni
juncai
2017/03/17 21:25:47
Done.
|
| + SimulateGattServicesDiscovered(device, services); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); |
| + |
| + EXPECT_EQ(2u, device->GetPrimaryServices().size()); |
| + |
| + // Disconnect from the device |
|
scheib
2017/03/17 17:38:40
Everything after GetPrimaryServices is not needed.
juncai
2017/03/17 21:25:47
Done.
|
| + device->DisconnectGatt(); |
| + SimulateGattDisconnection(device); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_TRUE(device->GetPrimaryServices().empty()); |
| +} |
| + |
| +TEST_F(BluetoothTest, GetPrimaryServicesByUUID) { |
| + if (!PlatformSupportsLowEnergy()) { |
| + LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| + return; |
| + } |
| + InitWithFakeAdapter(); |
| + StartLowEnergyDiscoverySession(); |
| + BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| + EXPECT_FALSE(device->IsConnected()); |
| + |
| + // Connect to the device |
| + ResetEventCounts(); |
| + device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| + GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| + TestBluetoothAdapterObserver observer(adapter_); |
| + SimulateGattConnection(device); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_TRUE(device->IsConnected()); |
| + |
| + // Discover services |
| + std::vector<std::string> services; |
| + services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
| + services.push_back("00000001-0000-1000-8000-00805f9b34fb"); |
| + SimulateGattServicesDiscovered(device, services); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); |
| + |
| + EXPECT_EQ( |
| + 1u, device->GetPrimaryServicesByUUID(BluetoothUUID(services[0])).size()); |
| + EXPECT_EQ( |
| + 1u, device->GetPrimaryServicesByUUID(BluetoothUUID(services[1])).size()); |
| + EXPECT_TRUE( |
| + device->GetPrimaryServicesByUUID(BluetoothUUID("non-existent")).empty()); |
|
scheib
2017/03/17 17:38:40
Let's use a valid UUID that doesn't exist in the f
juncai
2017/03/17 21:25:48
Done.
|
| + |
| + // Disconnect from the device |
| + device->DisconnectGatt(); |
| + SimulateGattDisconnection(device); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_TRUE( |
| + device->GetPrimaryServicesByUUID(BluetoothUUID(services[0])).empty()); |
| + EXPECT_TRUE( |
| + device->GetPrimaryServicesByUUID(BluetoothUUID(services[1])).empty()); |
| +} |
| + |
| +TEST_F(BluetoothTest, GetCharacteristicsByUUID) { |
| + if (!PlatformSupportsLowEnergy()) { |
| + LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| + return; |
| + } |
| + InitWithFakeAdapter(); |
| + StartLowEnergyDiscoverySession(); |
| + BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| + EXPECT_FALSE(device->IsConnected()); |
| + |
| + // Connect to the device |
| + ResetEventCounts(); |
| + device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| + GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| + TestBluetoothAdapterObserver observer(adapter_); |
| + SimulateGattConnection(device); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_TRUE(device->IsConnected()); |
| + |
| + // Discover services |
| + std::vector<std::string> services; |
| + services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
| + services.push_back("00000001-0000-1000-8000-00805f9b34fb"); |
| + SimulateGattServicesDiscovered(device, services); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); |
| + |
| + std::vector<BluetoothRemoteGattService*> primary_services = |
| + device->GetPrimaryServices(); |
| + |
| + // 2 characteristics, 1 on each service. |
| + SimulateGattCharacteristic(primary_services[0], services[0], |
|
scheib
2017/03/17 17:38:41
Let's use a unique ID for the characteristic inste
juncai
2017/03/17 21:25:48
Done.
|
| + 0 /* properties */); |
| + SimulateGattCharacteristic(primary_services[1], services[1], |
| + 0 /* properties */); |
| + |
| + std::string service_instance_id_0 = primary_services[0]->GetIdentifier(); |
| + BluetoothUUID characteristic_uuid_0(services[0]); |
| + EXPECT_EQ(1u, device |
| + ->GetCharacteristicsByUUID(service_instance_id_0, |
| + characteristic_uuid_0) |
| + .size()); |
| + |
| + std::string service_instance_id_1 = primary_services[1]->GetIdentifier(); |
| + BluetoothUUID characteristic_uuid_1(services[1]); |
| + EXPECT_EQ(1u, device |
| + ->GetCharacteristicsByUUID(service_instance_id_1, |
| + characteristic_uuid_1) |
| + .size()); |
| + |
| + EXPECT_TRUE( |
| + device->GetCharacteristicsByUUID("non-existent", characteristic_uuid_0) |
|
scheib
2017/03/17 17:38:40
Let's use a valid UUID that doesn't exist in the f
juncai
2017/03/17 21:25:47
Done.
|
| + .empty()); |
| + EXPECT_TRUE(device |
| + ->GetCharacteristicsByUUID(service_instance_id_0, |
| + BluetoothUUID("non-existent")) |
| + .empty()); |
| + |
| + // Disconnect from the device |
| + device->DisconnectGatt(); |
| + SimulateGattDisconnection(device); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_TRUE(device |
| + ->GetCharacteristicsByUUID(service_instance_id_0, |
| + characteristic_uuid_0) |
| + .empty()); |
| + EXPECT_TRUE(device |
| + ->GetCharacteristicsByUUID(service_instance_id_1, |
| + characteristic_uuid_1) |
| + .empty()); |
| +} |
| +#endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| + |
| } // namespace device |