Chromium Code Reviews| Index: device/bluetooth/bluetooth_adapter_unittest.cc |
| diff --git a/device/bluetooth/bluetooth_adapter_unittest.cc b/device/bluetooth/bluetooth_adapter_unittest.cc |
| index 06829a88dd6d1887fcf14d537b923cb42bd52a56..0ecb0f6ebf80f5168bd750ecf7c3326abfd7fe1c 100644 |
| --- a/device/bluetooth/bluetooth_adapter_unittest.cc |
| +++ b/device/bluetooth/bluetooth_adapter_unittest.cc |
| @@ -865,4 +865,188 @@ TEST_F(BluetoothTest, RemoveOutdatedDeviceGattConnect) { |
| } |
| #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| +#if defined(OS_MACOSX) |
| +// Simulate two devices being connected before starting discovery session with |
| +// no service filter. |
| +TEST_F(BluetoothTest, DiscoverConnectedLowEnergyDeviceWithNoFilter) { |
| + if (!PlatformSupportsLowEnergy()) { |
| + LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| + return; |
| + } |
| + InitWithFakeAdapter(); |
| + TestBluetoothAdapterObserver observer(adapter_); |
| + |
| + SimulateConnectedLowEnergyDevice(ConnectedDeviceType::GENERIC_DEVICE); |
| + SimulateConnectedLowEnergyDevice(ConnectedDeviceType::HEART_RATE_DEVICE); |
| + BluetoothDiscoveryFilter discovery_filter(BLUETOOTH_TRANSPORT_LE); |
| + std::unordered_map<BluetoothDevice*, BluetoothDevice::UUIDSet> result = |
| + adapter_->RetrieveGattConnectedDevicesWithDiscoveryFilter( |
| + discovery_filter); |
| + |
| + EXPECT_EQ(2u, result.size()); |
| + for (const auto& pair : result) { |
| + EXPECT_TRUE(adapter_->GetDevice(pair.first->GetAddress())); |
| + EXPECT_TRUE(pair.second.empty()); |
| + } |
| + BluetoothDevice::UUIDSet service_uuids; |
|
ortuno
2016/11/07 03:12:33
nit: you don't need insert anymore. You can do:
B
jlebel
2016/11/07 05:56:39
Done.
|
| + service_uuids.insert(device::BluetoothUUID(kTestUUIDGenericAccess)); |
| + EXPECT_EQ(RetrieveConnectedPeripheralServiceUUIDs(), service_uuids); |
| + EXPECT_EQ(2, observer.device_added_count()); |
| + EXPECT_EQ(2u, adapter_->GetDevices().size()); |
| +} |
| + |
| +// Simulate two devices being connected before starting discovery session with |
| +// one service filter. |
| +TEST_F(BluetoothTest, DiscoverConnectedLowEnergyDeviceWithFilter) { |
| + if (!PlatformSupportsLowEnergy()) { |
| + LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| + return; |
| + } |
| + InitWithFakeAdapter(); |
| + TestBluetoothAdapterObserver observer(adapter_); |
| + |
| + SimulateConnectedLowEnergyDevice(ConnectedDeviceType::GENERIC_DEVICE); |
| + SimulateConnectedLowEnergyDevice(ConnectedDeviceType::HEART_RATE_DEVICE); |
| + BluetoothDiscoveryFilter discovery_filter(BLUETOOTH_TRANSPORT_LE); |
| + device::BluetoothUUID heart_service_uuid = |
| + device::BluetoothUUID(kTestUUIDHeartRate); |
| + discovery_filter.AddUUID(heart_service_uuid); |
| + std::unordered_map<BluetoothDevice*, BluetoothDevice::UUIDSet> result = |
| + adapter_->RetrieveGattConnectedDevicesWithDiscoveryFilter( |
| + discovery_filter); |
| + |
| + EXPECT_EQ(1u, result.size()); |
| + for (const auto& pair : result) { |
| + EXPECT_EQ("02:00:00:8B:74:63", pair.first->GetAddress()); |
| + EXPECT_TRUE(adapter_->GetDevice(pair.first->GetAddress())); |
| + EXPECT_EQ(BluetoothDevice::UUIDSet({heart_service_uuid}), pair.second); |
| + } |
| + BluetoothDevice::UUIDSet service_uuids; |
| + service_uuids.insert(heart_service_uuid); |
| + EXPECT_EQ(RetrieveConnectedPeripheralServiceUUIDs(), service_uuids); |
|
ortuno
2016/11/07 03:12:34
nit: Same as above
BluetoothDevice::UUIDSet servi
jlebel
2016/11/07 05:56:39
Done.
|
| + EXPECT_EQ(1, observer.device_added_count()); |
| + EXPECT_EQ(1u, adapter_->GetDevices().size()); |
| +} |
| + |
| +// Simulate two devices being connected before starting discovery session with |
| +// one service filter that doesn't match. |
| +TEST_F(BluetoothTest, DiscoverConnectedLowEnergyDeviceWithWrongFilter) { |
| + if (!PlatformSupportsLowEnergy()) { |
| + LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| + return; |
| + } |
| + InitWithFakeAdapter(); |
| + TestBluetoothAdapterObserver observer(adapter_); |
| + |
| + SimulateConnectedLowEnergyDevice(ConnectedDeviceType::GENERIC_DEVICE); |
| + SimulateConnectedLowEnergyDevice(ConnectedDeviceType::HEART_RATE_DEVICE); |
| + BluetoothDiscoveryFilter discovery_filter(BLUETOOTH_TRANSPORT_LE); |
| + discovery_filter.AddUUID(device::BluetoothUUID("1002")); |
|
ortuno
2016/11/07 03:12:33
nit: let's use one of the existing test uuids like
jlebel
2016/11/07 05:56:39
Done.
|
| + std::unordered_map<BluetoothDevice*, BluetoothDevice::UUIDSet> result = |
| + adapter_->RetrieveGattConnectedDevicesWithDiscoveryFilter( |
| + discovery_filter); |
| + |
| + EXPECT_TRUE(result.empty()); |
| + BluetoothDevice::UUIDSet service_uuids; |
| + service_uuids.insert(device::BluetoothUUID("1002")); |
| + EXPECT_EQ(RetrieveConnectedPeripheralServiceUUIDs(), service_uuids); |
|
ortuno
2016/11/07 03:12:33
nit: same as above: Use initializer lists
jlebel
2016/11/07 05:56:39
Done.
|
| + EXPECT_EQ(0, observer.device_added_count()); |
| + EXPECT_EQ(0u, adapter_->GetDevices().size()); |
| +} |
| + |
| +// Simulate two devices being connected before starting discovery session with |
| +// two service filters that both match. |
| +TEST_F(BluetoothTest, DiscoverConnectedLowEnergyDeviceWithTwoFilters) { |
| + if (!PlatformSupportsLowEnergy()) { |
| + LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| + return; |
| + } |
| + InitWithFakeAdapter(); |
| + TestBluetoothAdapterObserver observer(adapter_); |
| + |
| + SimulateConnectedLowEnergyDevice(ConnectedDeviceType::GENERIC_DEVICE); |
| + SimulateConnectedLowEnergyDevice(ConnectedDeviceType::HEART_RATE_DEVICE); |
| + BluetoothDiscoveryFilter discovery_filter(BLUETOOTH_TRANSPORT_LE); |
| + device::BluetoothUUID heart_service_uuid = |
| + device::BluetoothUUID(kTestUUIDHeartRate); |
| + discovery_filter.AddUUID(heart_service_uuid); |
| + device::BluetoothUUID generic_service_uuid = |
| + device::BluetoothUUID(kTestUUIDGenericAccess); |
| + discovery_filter.AddUUID(generic_service_uuid); |
| + std::unordered_map<BluetoothDevice*, BluetoothDevice::UUIDSet> result = |
| + adapter_->RetrieveGattConnectedDevicesWithDiscoveryFilter( |
| + discovery_filter); |
| + |
| + EXPECT_EQ(2u, result.size()); |
| + for (const auto& pair : result) { |
| + EXPECT_TRUE(adapter_->GetDevice(pair.first->GetAddress())); |
| + if (pair.first->GetAddress() == "02:00:00:8B:74:63") { |
|
ortuno
2016/11/07 03:12:34
Use the constants in: https://cs.chromium.org/chro
jlebel
2016/11/07 05:56:39
Done.
|
| + BluetoothDevice::UUIDSet service_uuids; |
| + service_uuids.insert(heart_service_uuid); |
| + service_uuids.insert(generic_service_uuid); |
| + EXPECT_EQ(service_uuids, pair.second); |
|
ortuno
2016/11/07 03:12:33
nit: Either:
BluetoothDevice::UUIDSet = service_u
jlebel
2016/11/07 05:56:39
Done.
|
| + } else if (pair.first->GetAddress() == "01:00:00:90:1E:BE") { |
|
ortuno
2016/11/07 03:12:33
Same here, use constants in: https://cs.chromium.o
jlebel
2016/11/07 05:56:39
Done.
|
| + BluetoothDevice::UUIDSet service_uuids; |
| + service_uuids.insert(generic_service_uuid); |
| + EXPECT_EQ(service_uuids, pair.second); |
| + } else { |
| + // Unknown device. |
| + EXPECT_TRUE(false); |
| + } |
| + } |
| + BluetoothDevice::UUIDSet service_uuids; |
| + service_uuids.insert(heart_service_uuid); |
| + service_uuids.insert(generic_service_uuid); |
| + EXPECT_EQ(RetrieveConnectedPeripheralServiceUUIDs(), service_uuids); |
|
ortuno
2016/11/07 03:12:33
Same here, use initializer lists.
jlebel
2016/11/07 05:56:39
Done.
|
| + EXPECT_EQ(2, observer.device_added_count()); |
| + EXPECT_EQ(2u, adapter_->GetDevices().size()); |
| +} |
| + |
| +// Simulate two devices being connected before starting discovery session with |
|
ortuno
2016/11/07 03:12:34
nit: fix description.
jlebel
2016/11/07 05:56:39
Done.
|
| +// two service filters that both match. |
| +TEST_F(BluetoothTest, DiscoverConnectedLowEnergyDeviceTwice) { |
| + if (!PlatformSupportsLowEnergy()) { |
| + LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| + return; |
| + } |
| + InitWithFakeAdapter(); |
| + TestBluetoothAdapterObserver observer(adapter_); |
| + |
| + SimulateConnectedLowEnergyDevice(ConnectedDeviceType::GENERIC_DEVICE); |
| + SimulateConnectedLowEnergyDevice(ConnectedDeviceType::HEART_RATE_DEVICE); |
| + BluetoothDiscoveryFilter discovery_filter(BLUETOOTH_TRANSPORT_LE); |
| + device::BluetoothUUID heart_service_uuid = |
| + device::BluetoothUUID(kTestUUIDHeartRate); |
| + discovery_filter.AddUUID(heart_service_uuid); |
| + std::unordered_map<BluetoothDevice*, BluetoothDevice::UUIDSet> result = |
| + adapter_->RetrieveGattConnectedDevicesWithDiscoveryFilter( |
| + discovery_filter); |
| + |
| + EXPECT_EQ(1u, result.size()); |
| + for (const auto& pair : result) { |
| + EXPECT_EQ("02:00:00:8B:74:63", pair.first->GetAddress()); |
|
ortuno
2016/11/07 03:12:34
nit: use constants https://cs.chromium.org/chromiu
jlebel
2016/11/07 05:56:39
Done.
|
| + EXPECT_TRUE(adapter_->GetDevice(pair.first->GetAddress())); |
| + EXPECT_EQ(BluetoothDevice::UUIDSet({heart_service_uuid}), pair.second); |
| + } |
| + BluetoothDevice::UUIDSet service_uuids; |
| + service_uuids.insert(heart_service_uuid); |
| + EXPECT_EQ(RetrieveConnectedPeripheralServiceUUIDs(), service_uuids); |
|
ortuno
2016/11/07 03:12:34
nit: expected value comes first and use initialize
jlebel
2016/11/07 05:56:39
Done.
|
| + |
| + ResetRetrieveConnectedPeripheralServiceUUIDs(); |
| + result = adapter_->RetrieveGattConnectedDevicesWithDiscoveryFilter( |
| + discovery_filter); |
| + |
| + EXPECT_EQ(1u, result.size()); |
| + for (const auto& pair : result) { |
| + EXPECT_EQ("02:00:00:8B:74:63", pair.first->GetAddress()); |
|
ortuno
2016/11/07 03:12:34
Same here, use already existing constants.
jlebel
2016/11/07 05:56:39
Done.
|
| + EXPECT_TRUE(adapter_->GetDevice(pair.first->GetAddress())); |
| + EXPECT_EQ(BluetoothDevice::UUIDSet({heart_service_uuid}), pair.second); |
| + } |
| + EXPECT_EQ(RetrieveConnectedPeripheralServiceUUIDs(), service_uuids); |
|
ortuno
2016/11/07 03:12:34
nit: expected value comes first.
jlebel
2016/11/07 05:56:39
Done.
|
| + |
| + EXPECT_EQ(1, observer.device_added_count()); |
|
ortuno
2016/11/07 03:12:33
Test this after the first call to RetrieveConnecte
jlebel
2016/11/07 05:56:39
Done.
|
| + EXPECT_EQ(1u, adapter_->GetDevices().size()); |
| +} |
| +#endif // defined(OS_MACOSX) |
| + |
| } // namespace device |