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..f4b41bbc79418f32ca81e9d64c3b4be16bda1100 100644 |
| --- a/device/bluetooth/bluetooth_device_unittest.cc |
| +++ b/device/bluetooth/bluetooth_device_unittest.cc |
| @@ -1483,4 +1483,173 @@ 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(); |
| + 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"); |
| + // 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); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); |
| + |
| + EXPECT_EQ(3u, device->GetPrimaryServices().size()); |
| +} |
| + |
| +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"); |
| + // 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); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); |
| + |
| + 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(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"); |
| + // 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); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); |
| + |
| + std::vector<BluetoothRemoteGattService*> primary_services = |
| + device->GetPrimaryServices(); |
| + |
| + 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 */); |
| + |
| + std::string service_instance_id0 = primary_services[0]->GetIdentifier(); |
| + EXPECT_EQ(1u, |
| + device |
| + ->GetCharacteristicsByUUID(service_instance_id0, |
| + BluetoothUUID(characteristic_uuid0)) |
| + .size()); |
| + |
| + std::string service_instance_id1 = primary_services[1]->GetIdentifier(); |
| + EXPECT_EQ(1u, |
| + device |
| + ->GetCharacteristicsByUUID(service_instance_id1, |
| + BluetoothUUID(characteristic_uuid1)) |
| + .size()); |
| + |
| + std::string service_instance_id2 = primary_services[2]->GetIdentifier(); |
| + EXPECT_EQ(1u, |
| + device |
| + ->GetCharacteristicsByUUID(service_instance_id2, |
| + BluetoothUUID(characteristic_uuid1)) |
| + .size()); |
| + |
| + std::string service_instance_id_not_exist_in_setup = |
| + "01:00:00:90:1E:BE/00000004-0000-1000-8000-00805f9b34fb,0"; |
|
scheib
2017/03/17 22:10:47
This is a platform specific service identifier (fr
juncai
2017/03/17 23:33:54
Done.
|
| + 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()); |
| +} |
| +#endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| + |
| } // namespace device |