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

Unified Diff: device/bluetooth/bluetooth_device_unittest.cc

Issue 2744243008: Add unit tests for BluetoothDevice::GetPrimaryServices() etc. (Closed)
Patch Set: move test fixture to the top of the file Created 3 years, 9 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..ab478fc7386df4a49d9bf91a0645488354e48239 100644
--- a/device/bluetooth/bluetooth_device_unittest.cc
+++ b/device/bluetooth/bluetooth_device_unittest.cc
@@ -46,6 +46,39 @@ int8_t ToInt8(BluetoothTest::TestTxPower tx_power) {
using UUIDSet = BluetoothDevice::UUIDSet;
using ServiceDataMap = BluetoothDevice::ServiceDataMap;
+class BluetoothGetServiceOrCharacteristicTest : public BluetoothTest {
+ public:
+ // Creates |device_|, |services_|.
+ void FakeServiceBoilerplate() {
+ InitWithFakeAdapter();
+ StartLowEnergyDiscoverySession();
+ 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.
+ 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());
+ }
+
+ protected:
+ BluetoothDevice* device_ = nullptr;
+ std::vector<std::string> services_;
+};
+
TEST(BluetoothDeviceTest, CanonicalizeAddressFormat_AcceptsAllValidFormats) {
// There are three valid separators (':', '-', and none).
// Case shouldn't matter.
@@ -1483,4 +1516,107 @@ TEST_F(BluetoothTest, GetDeviceTransportType) {
}
#endif // defined(OS_CHROMEOS) || defined(OS_LINUX)
+#if defined(OS_ANDROID) || defined(OS_MACOSX)
ortuno 2017/03/28 03:07:49 We usually add a note about why other platforms ar
juncai 2017/03/29 17:49:52 reopen the issue: https://bugs.chromium.org/p/chro
+TEST_F(BluetoothGetServiceOrCharacteristicTest, GetPrimaryServices) {
+ if (!PlatformSupportsLowEnergy()) {
+ LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
+ return;
+ }
+ ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate());
+
+ EXPECT_EQ(3u, device_->GetPrimaryServices().size());
+}
+
+TEST_F(BluetoothGetServiceOrCharacteristicTest, GetPrimaryServicesByUUID) {
ortuno 2017/03/28 03:07:49 This test could be improved by either checking the
juncai 2017/03/29 17:49:53 ditto.
+ if (!PlatformSupportsLowEnergy()) {
+ LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
+ return;
+ }
+ ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate());
+
+ 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 =
ortuno 2017/03/28 03:07:49 nit: use existing test UUIDs: https://cs.chromium.
juncai 2017/03/29 17:49:53 ditto.
+ "00000002-0000-1000-8000-00805f9b34fb";
+ EXPECT_TRUE(device_
+ ->GetPrimaryServicesByUUID(
+ BluetoothUUID(service_uuid_not_exist_in_setup))
+ .empty());
+}
+
+TEST_F(BluetoothGetServiceOrCharacteristicTest, GetCharacteristicsByUUID) {
ortuno 2017/03/28 03:07:49 Similarly here we should make sure the right chara
juncai 2017/03/29 17:49:53 ditto.
+ if (!PlatformSupportsLowEnergy()) {
+ LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
+ return;
+ }
+ ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate());
+
+ std::vector<BluetoothRemoteGattService*> primary_services =
+ device_->GetPrimaryServices();
+ std::string service_instance_id0 = primary_services[0]->GetIdentifier();
+ std::string service_instance_id1 = primary_services[1]->GetIdentifier();
+ std::string service_instance_id2 = primary_services[2]->GetIdentifier();
+
+ 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 */);
+
+ EXPECT_EQ(1u,
+ device_
+ ->GetCharacteristicsByUUID(service_instance_id0,
ortuno 2017/03/28 03:07:49 Unrelated questions about these methods: 1. Why i
juncai 2017/03/29 17:49:53 For 1: it is mentioned in the TODO at: https://cod
ortuno 2017/03/29 22:45:42 I see. Let's move them to the appropriate attribut
+ BluetoothUUID(characteristic_uuid0))
+ .size());
+ EXPECT_EQ(1u,
+ device_
+ ->GetCharacteristicsByUUID(service_instance_id1,
+ BluetoothUUID(characteristic_uuid1))
+ .size());
+ EXPECT_EQ(1u,
+ device_
+ ->GetCharacteristicsByUUID(service_instance_id2,
+ BluetoothUUID(characteristic_uuid1))
+ .size());
+
+ std::string service_instance_id_not_exist_in_setup =
+ "non-existent platform specific service instance id";
+ 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)
ortuno 2017/03/28 03:07:49 Do we already have tests for GetDescriptorsByUUID?
juncai 2017/03/29 17:49:53 I don't think we have tests for GetDescriptorsByUU
+
} // namespace device
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698