Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1548)

Side by Side Diff: device/bluetooth/bluetooth_adapter_unittest.cc

Issue 2339253002: bluetooth: mac: add connected LE devices to chooser (Closed)
Patch Set: Nit for the tests Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 calling
870 // RetrieveGattConnectedDevicesWithDiscoveryFilter() with 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 EXPECT_EQ(BluetoothDevice::UUIDSet({BluetoothUUID(kTestUUIDGenericAccess)}),
892 RetrieveConnectedPeripheralServiceUUIDs());
893 EXPECT_EQ(2, observer.device_added_count());
894 EXPECT_EQ(2u, adapter_->GetDevices().size());
895 }
896
897 // Simulate two devices being connected before calling
898 // RetrieveGattConnectedDevicesWithDiscoveryFilter() with one service filter.
899 TEST_F(BluetoothTest, DiscoverConnectedLowEnergyDeviceWithFilter) {
900 if (!PlatformSupportsLowEnergy()) {
901 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
902 return;
903 }
904 InitWithFakeAdapter();
905 TestBluetoothAdapterObserver observer(adapter_);
906
907 SimulateConnectedLowEnergyDevice(ConnectedDeviceType::GENERIC_DEVICE);
908 SimulateConnectedLowEnergyDevice(ConnectedDeviceType::HEART_RATE_DEVICE);
909 BluetoothDiscoveryFilter discovery_filter(BLUETOOTH_TRANSPORT_LE);
910 device::BluetoothUUID heart_service_uuid =
911 device::BluetoothUUID(kTestUUIDHeartRate);
912 discovery_filter.AddUUID(heart_service_uuid);
913 std::unordered_map<BluetoothDevice*, BluetoothDevice::UUIDSet> result =
914 adapter_->RetrieveGattConnectedDevicesWithDiscoveryFilter(
915 discovery_filter);
916
917 EXPECT_EQ(1u, result.size());
918 for (const auto& pair : result) {
919 EXPECT_EQ(kTestDeviceAddress2, pair.first->GetAddress());
920 EXPECT_TRUE(adapter_->GetDevice(pair.first->GetAddress()));
921 EXPECT_EQ(BluetoothDevice::UUIDSet({heart_service_uuid}), pair.second);
922 }
923 EXPECT_EQ(BluetoothDevice::UUIDSet({heart_service_uuid}),
924 RetrieveConnectedPeripheralServiceUUIDs());
925 EXPECT_EQ(1, observer.device_added_count());
926 EXPECT_EQ(1u, adapter_->GetDevices().size());
927 }
928
929 // Simulate two devices being connected before calling
930 // RetrieveGattConnectedDevicesWithDiscoveryFilter() with one service filter
931 // that doesn't match.
932 TEST_F(BluetoothTest, DiscoverConnectedLowEnergyDeviceWithWrongFilter) {
933 if (!PlatformSupportsLowEnergy()) {
934 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
935 return;
936 }
937 InitWithFakeAdapter();
938 TestBluetoothAdapterObserver observer(adapter_);
939
940 SimulateConnectedLowEnergyDevice(ConnectedDeviceType::GENERIC_DEVICE);
941 SimulateConnectedLowEnergyDevice(ConnectedDeviceType::HEART_RATE_DEVICE);
942 BluetoothDiscoveryFilter discovery_filter(BLUETOOTH_TRANSPORT_LE);
943 discovery_filter.AddUUID(device::BluetoothUUID(kTestUUIDLinkLoss));
944 std::unordered_map<BluetoothDevice*, BluetoothDevice::UUIDSet> result =
945 adapter_->RetrieveGattConnectedDevicesWithDiscoveryFilter(
946 discovery_filter);
947
948 EXPECT_TRUE(result.empty());
949 EXPECT_EQ(
950 BluetoothDevice::UUIDSet({device::BluetoothUUID(kTestUUIDLinkLoss)}),
951 RetrieveConnectedPeripheralServiceUUIDs());
952 EXPECT_EQ(0, observer.device_added_count());
953 EXPECT_EQ(0u, adapter_->GetDevices().size());
954 }
955
956 // Simulate two devices being connected before calling
957 // RetrieveGattConnectedDevicesWithDiscoveryFilter() with two service filters
958 // 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() == kTestDeviceAddress2) {
984 EXPECT_EQ(
985 BluetoothDevice::UUIDSet({heart_service_uuid, generic_service_uuid}),
986 pair.second);
987 } else if (pair.first->GetAddress() == kTestDeviceAddress1) {
988 EXPECT_EQ(BluetoothDevice::UUIDSet({generic_service_uuid}), pair.second);
989 } else {
990 // Unknown device.
991 EXPECT_TRUE(false);
992 }
993 }
994 EXPECT_EQ(
995 BluetoothDevice::UUIDSet({generic_service_uuid, heart_service_uuid}),
996 RetrieveConnectedPeripheralServiceUUIDs());
997 EXPECT_EQ(2, observer.device_added_count());
998 EXPECT_EQ(2u, adapter_->GetDevices().size());
999 }
1000
1001 // Simulate two devices being connected before calling
1002 // RetrieveGattConnectedDevicesWithDiscoveryFilter() with one service filter
1003 // that one match device, and then
1004 // RetrieveGattConnectedDevicesWithDiscoveryFilter() again.
1005 TEST_F(BluetoothTest, DiscoverConnectedLowEnergyDeviceTwice) {
1006 if (!PlatformSupportsLowEnergy()) {
1007 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
1008 return;
1009 }
1010 InitWithFakeAdapter();
1011 TestBluetoothAdapterObserver observer(adapter_);
1012
1013 SimulateConnectedLowEnergyDevice(ConnectedDeviceType::GENERIC_DEVICE);
1014 SimulateConnectedLowEnergyDevice(ConnectedDeviceType::HEART_RATE_DEVICE);
1015 BluetoothDiscoveryFilter discovery_filter(BLUETOOTH_TRANSPORT_LE);
1016 device::BluetoothUUID heart_service_uuid =
1017 device::BluetoothUUID(kTestUUIDHeartRate);
1018 discovery_filter.AddUUID(heart_service_uuid);
1019 std::unordered_map<BluetoothDevice*, BluetoothDevice::UUIDSet> result =
1020 adapter_->RetrieveGattConnectedDevicesWithDiscoveryFilter(
1021 discovery_filter);
1022
1023 EXPECT_EQ(1u, result.size());
1024 for (const auto& pair : result) {
1025 EXPECT_EQ(kTestDeviceAddress2, pair.first->GetAddress());
1026 EXPECT_TRUE(adapter_->GetDevice(pair.first->GetAddress()));
1027 EXPECT_EQ(BluetoothDevice::UUIDSet({heart_service_uuid}), pair.second);
1028 }
1029 EXPECT_EQ(BluetoothDevice::UUIDSet({heart_service_uuid}),
1030 RetrieveConnectedPeripheralServiceUUIDs());
1031 EXPECT_EQ(1, observer.device_added_count());
1032 EXPECT_EQ(1u, adapter_->GetDevices().size());
1033
1034 observer.Reset();
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(kTestDeviceAddress2, pair.first->GetAddress());
1042 EXPECT_TRUE(adapter_->GetDevice(pair.first->GetAddress()));
1043 EXPECT_EQ(BluetoothDevice::UUIDSet({heart_service_uuid}), pair.second);
1044 }
1045 EXPECT_EQ(BluetoothDevice::UUIDSet({heart_service_uuid}),
1046 RetrieveConnectedPeripheralServiceUUIDs());
1047
1048 EXPECT_EQ(0, observer.device_added_count());
1049 EXPECT_EQ(1u, adapter_->GetDevices().size());
1050 }
1051 #endif // defined(OS_MACOSX)
1052
868 } // namespace device 1053 } // namespace device
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_adapter_mac.mm ('k') | device/bluetooth/bluetooth_low_energy_device_mac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698