Chromium Code Reviews| 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_adapter.h" | 5 #include "device/bluetooth/bluetooth_adapter.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 847 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 858 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 858 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 859 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 859 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 860 SimulateGattConnection(device); | 860 SimulateGattConnection(device); |
| 861 EXPECT_EQ(1u, adapter_->GetDevices().size()); | 861 EXPECT_EQ(1u, adapter_->GetDevices().size()); |
| 862 RemoveTimedOutDevices(); | 862 RemoveTimedOutDevices(); |
| 863 EXPECT_EQ(0, observer.device_removed_count()); | 863 EXPECT_EQ(0, observer.device_removed_count()); |
| 864 EXPECT_EQ(1u, adapter_->GetDevices().size()); | 864 EXPECT_EQ(1u, adapter_->GetDevices().size()); |
| 865 } | 865 } |
| 866 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 866 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| 867 | 867 |
| 868 #if defined(OS_MACOSX) | |
| 869 // Simulate two devices being connected before starting discovery session with | |
| 870 // no service filter. | |
| 871 TEST_F(BluetoothTest, DiscoverConnectedLowEnergyDeviceWithNoFilter) { | |
| 872 if (!PlatformSupportsLowEnergy()) { | |
| 873 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | |
| 874 return; | |
| 875 } | |
| 876 InitWithFakeAdapter(); | |
| 877 TestBluetoothAdapterObserver observer(adapter_); | |
| 878 | |
| 879 SimulateConnectedLowEnergyDevice(ConnectedDeviceType::GENERIC_DEVICE); | |
| 880 SimulateConnectedLowEnergyDevice(ConnectedDeviceType::HEART_RATE_DEVICE); | |
| 881 BluetoothDiscoveryFilter discovery_filter(BLUETOOTH_TRANSPORT_LE); | |
| 882 std::unordered_map<BluetoothDevice*, BluetoothDevice::UUIDSet> result = | |
| 883 adapter_->RetrieveGattConnectedDevicesWithDiscoveryFilter( | |
| 884 discovery_filter); | |
| 885 | |
| 886 EXPECT_EQ(2u, result.size()); | |
| 887 for (const auto& pair : result) { | |
| 888 EXPECT_TRUE(adapter_->GetDevice(pair.first->GetAddress())); | |
| 889 EXPECT_TRUE(pair.second.empty()); | |
| 890 } | |
| 891 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.
| |
| 892 service_uuids.insert(device::BluetoothUUID(kTestUUIDGenericAccess)); | |
| 893 EXPECT_EQ(RetrieveConnectedPeripheralServiceUUIDs(), service_uuids); | |
| 894 EXPECT_EQ(2, observer.device_added_count()); | |
| 895 EXPECT_EQ(2u, adapter_->GetDevices().size()); | |
| 896 } | |
| 897 | |
| 898 // Simulate two devices being connected before starting discovery session with | |
| 899 // one service filter. | |
| 900 TEST_F(BluetoothTest, DiscoverConnectedLowEnergyDeviceWithFilter) { | |
| 901 if (!PlatformSupportsLowEnergy()) { | |
| 902 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | |
| 903 return; | |
| 904 } | |
| 905 InitWithFakeAdapter(); | |
| 906 TestBluetoothAdapterObserver observer(adapter_); | |
| 907 | |
| 908 SimulateConnectedLowEnergyDevice(ConnectedDeviceType::GENERIC_DEVICE); | |
| 909 SimulateConnectedLowEnergyDevice(ConnectedDeviceType::HEART_RATE_DEVICE); | |
| 910 BluetoothDiscoveryFilter discovery_filter(BLUETOOTH_TRANSPORT_LE); | |
| 911 device::BluetoothUUID heart_service_uuid = | |
| 912 device::BluetoothUUID(kTestUUIDHeartRate); | |
| 913 discovery_filter.AddUUID(heart_service_uuid); | |
| 914 std::unordered_map<BluetoothDevice*, BluetoothDevice::UUIDSet> result = | |
| 915 adapter_->RetrieveGattConnectedDevicesWithDiscoveryFilter( | |
| 916 discovery_filter); | |
| 917 | |
| 918 EXPECT_EQ(1u, result.size()); | |
| 919 for (const auto& pair : result) { | |
| 920 EXPECT_EQ("02:00:00:8B:74:63", pair.first->GetAddress()); | |
| 921 EXPECT_TRUE(adapter_->GetDevice(pair.first->GetAddress())); | |
| 922 EXPECT_EQ(BluetoothDevice::UUIDSet({heart_service_uuid}), pair.second); | |
| 923 } | |
| 924 BluetoothDevice::UUIDSet service_uuids; | |
| 925 service_uuids.insert(heart_service_uuid); | |
| 926 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.
| |
| 927 EXPECT_EQ(1, observer.device_added_count()); | |
| 928 EXPECT_EQ(1u, adapter_->GetDevices().size()); | |
| 929 } | |
| 930 | |
| 931 // Simulate two devices being connected before starting discovery session with | |
| 932 // one service filter that doesn't match. | |
| 933 TEST_F(BluetoothTest, DiscoverConnectedLowEnergyDeviceWithWrongFilter) { | |
| 934 if (!PlatformSupportsLowEnergy()) { | |
| 935 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | |
| 936 return; | |
| 937 } | |
| 938 InitWithFakeAdapter(); | |
| 939 TestBluetoothAdapterObserver observer(adapter_); | |
| 940 | |
| 941 SimulateConnectedLowEnergyDevice(ConnectedDeviceType::GENERIC_DEVICE); | |
| 942 SimulateConnectedLowEnergyDevice(ConnectedDeviceType::HEART_RATE_DEVICE); | |
| 943 BluetoothDiscoveryFilter discovery_filter(BLUETOOTH_TRANSPORT_LE); | |
| 944 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.
| |
| 945 std::unordered_map<BluetoothDevice*, BluetoothDevice::UUIDSet> result = | |
| 946 adapter_->RetrieveGattConnectedDevicesWithDiscoveryFilter( | |
| 947 discovery_filter); | |
| 948 | |
| 949 EXPECT_TRUE(result.empty()); | |
| 950 BluetoothDevice::UUIDSet service_uuids; | |
| 951 service_uuids.insert(device::BluetoothUUID("1002")); | |
| 952 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.
| |
| 953 EXPECT_EQ(0, observer.device_added_count()); | |
| 954 EXPECT_EQ(0u, adapter_->GetDevices().size()); | |
| 955 } | |
| 956 | |
| 957 // Simulate two devices being connected before starting discovery session with | |
| 958 // two service filters that both match. | |
| 959 TEST_F(BluetoothTest, DiscoverConnectedLowEnergyDeviceWithTwoFilters) { | |
| 960 if (!PlatformSupportsLowEnergy()) { | |
| 961 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | |
| 962 return; | |
| 963 } | |
| 964 InitWithFakeAdapter(); | |
| 965 TestBluetoothAdapterObserver observer(adapter_); | |
| 966 | |
| 967 SimulateConnectedLowEnergyDevice(ConnectedDeviceType::GENERIC_DEVICE); | |
| 968 SimulateConnectedLowEnergyDevice(ConnectedDeviceType::HEART_RATE_DEVICE); | |
| 969 BluetoothDiscoveryFilter discovery_filter(BLUETOOTH_TRANSPORT_LE); | |
| 970 device::BluetoothUUID heart_service_uuid = | |
| 971 device::BluetoothUUID(kTestUUIDHeartRate); | |
| 972 discovery_filter.AddUUID(heart_service_uuid); | |
| 973 device::BluetoothUUID generic_service_uuid = | |
| 974 device::BluetoothUUID(kTestUUIDGenericAccess); | |
| 975 discovery_filter.AddUUID(generic_service_uuid); | |
| 976 std::unordered_map<BluetoothDevice*, BluetoothDevice::UUIDSet> result = | |
| 977 adapter_->RetrieveGattConnectedDevicesWithDiscoveryFilter( | |
| 978 discovery_filter); | |
| 979 | |
| 980 EXPECT_EQ(2u, result.size()); | |
| 981 for (const auto& pair : result) { | |
| 982 EXPECT_TRUE(adapter_->GetDevice(pair.first->GetAddress())); | |
| 983 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.
| |
| 984 BluetoothDevice::UUIDSet service_uuids; | |
| 985 service_uuids.insert(heart_service_uuid); | |
| 986 service_uuids.insert(generic_service_uuid); | |
| 987 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.
| |
| 988 } 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.
| |
| 989 BluetoothDevice::UUIDSet service_uuids; | |
| 990 service_uuids.insert(generic_service_uuid); | |
| 991 EXPECT_EQ(service_uuids, pair.second); | |
| 992 } else { | |
| 993 // Unknown device. | |
| 994 EXPECT_TRUE(false); | |
| 995 } | |
| 996 } | |
| 997 BluetoothDevice::UUIDSet service_uuids; | |
| 998 service_uuids.insert(heart_service_uuid); | |
| 999 service_uuids.insert(generic_service_uuid); | |
| 1000 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.
| |
| 1001 EXPECT_EQ(2, observer.device_added_count()); | |
| 1002 EXPECT_EQ(2u, adapter_->GetDevices().size()); | |
| 1003 } | |
| 1004 | |
| 1005 // 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.
| |
| 1006 // two service filters that both match. | |
| 1007 TEST_F(BluetoothTest, DiscoverConnectedLowEnergyDeviceTwice) { | |
| 1008 if (!PlatformSupportsLowEnergy()) { | |
| 1009 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | |
| 1010 return; | |
| 1011 } | |
| 1012 InitWithFakeAdapter(); | |
| 1013 TestBluetoothAdapterObserver observer(adapter_); | |
| 1014 | |
| 1015 SimulateConnectedLowEnergyDevice(ConnectedDeviceType::GENERIC_DEVICE); | |
| 1016 SimulateConnectedLowEnergyDevice(ConnectedDeviceType::HEART_RATE_DEVICE); | |
| 1017 BluetoothDiscoveryFilter discovery_filter(BLUETOOTH_TRANSPORT_LE); | |
| 1018 device::BluetoothUUID heart_service_uuid = | |
| 1019 device::BluetoothUUID(kTestUUIDHeartRate); | |
| 1020 discovery_filter.AddUUID(heart_service_uuid); | |
| 1021 std::unordered_map<BluetoothDevice*, BluetoothDevice::UUIDSet> result = | |
| 1022 adapter_->RetrieveGattConnectedDevicesWithDiscoveryFilter( | |
| 1023 discovery_filter); | |
| 1024 | |
| 1025 EXPECT_EQ(1u, result.size()); | |
| 1026 for (const auto& pair : result) { | |
| 1027 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.
| |
| 1028 EXPECT_TRUE(adapter_->GetDevice(pair.first->GetAddress())); | |
| 1029 EXPECT_EQ(BluetoothDevice::UUIDSet({heart_service_uuid}), pair.second); | |
| 1030 } | |
| 1031 BluetoothDevice::UUIDSet service_uuids; | |
| 1032 service_uuids.insert(heart_service_uuid); | |
| 1033 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.
| |
| 1034 | |
| 1035 ResetRetrieveConnectedPeripheralServiceUUIDs(); | |
| 1036 result = adapter_->RetrieveGattConnectedDevicesWithDiscoveryFilter( | |
| 1037 discovery_filter); | |
| 1038 | |
| 1039 EXPECT_EQ(1u, result.size()); | |
| 1040 for (const auto& pair : result) { | |
| 1041 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.
| |
| 1042 EXPECT_TRUE(adapter_->GetDevice(pair.first->GetAddress())); | |
| 1043 EXPECT_EQ(BluetoothDevice::UUIDSet({heart_service_uuid}), pair.second); | |
| 1044 } | |
| 1045 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.
| |
| 1046 | |
| 1047 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.
| |
| 1048 EXPECT_EQ(1u, adapter_->GetDevices().size()); | |
| 1049 } | |
| 1050 #endif // defined(OS_MACOSX) | |
| 1051 | |
| 868 } // namespace device | 1052 } // namespace device |
| OLD | NEW |