Index: device/bluetooth/bluetooth_device_unittest.cc |
diff --git a/device/bluetooth/bluetooth_device_unittest.cc b/device/bluetooth/bluetooth_device_unittest.cc |
index ab478fc7386df4a49d9bf91a0645488354e48239..d1a040051b483445a22e797a430133309af6173e 100644 |
--- a/device/bluetooth/bluetooth_device_unittest.cc |
+++ b/device/bluetooth/bluetooth_device_unittest.cc |
@@ -46,9 +46,10 @@ int8_t ToInt8(BluetoothTest::TestTxPower tx_power) { |
using UUIDSet = BluetoothDevice::UUIDSet; |
using ServiceDataMap = BluetoothDevice::ServiceDataMap; |
-class BluetoothGetServiceOrCharacteristicTest : public BluetoothTest { |
+class BluetoothGetServiceOrCharacteristicOrDescriptorTest |
+ : public BluetoothTest { |
public: |
- // Creates |device_|, |services_|. |
+ // Creates |device_|, |service_uuids_|. |
void FakeServiceBoilerplate() { |
InitWithFakeAdapter(); |
StartLowEnergyDiscoverySession(); |
@@ -65,18 +66,18 @@ class BluetoothGetServiceOrCharacteristicTest : public BluetoothTest { |
EXPECT_TRUE(device_->IsConnected()); |
// Discover services. |
- services_.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
+ service_uuids_.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
ortuno
2017/04/03 03:36:37
Please use existing service uuids:
https://cs.chr
juncai
2017/04/04 03:03:42
Done.
|
// 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_); |
+ service_uuids_.push_back("00000001-0000-1000-8000-00805f9b34fb"); |
+ service_uuids_.push_back("00000001-0000-1000-8000-00805f9b34fb"); |
+ SimulateGattServicesDiscovered(device_, service_uuids_); |
base::RunLoop().RunUntilIdle(); |
EXPECT_TRUE(device_->IsGattServicesDiscoveryComplete()); |
} |
protected: |
BluetoothDevice* device_ = nullptr; |
- std::vector<std::string> services_; |
+ std::vector<std::string> service_uuids_; |
ortuno
2017/04/03 03:36:38
It might be better to name the UUIDs like you did
juncai
2017/04/04 03:03:42
It seems redundant to do this since there are alre
ortuno
2017/04/04 03:32:11
If you read the test its hard to understand why
d
juncai
2017/04/04 20:33:40
Done.
|
}; |
TEST(BluetoothDeviceTest, CanonicalizeAddressFormat_AcceptsAllValidFormats) { |
@@ -1517,7 +1518,10 @@ TEST_F(BluetoothTest, GetDeviceTransportType) { |
#endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
#if defined(OS_ANDROID) || defined(OS_MACOSX) |
-TEST_F(BluetoothGetServiceOrCharacteristicTest, GetPrimaryServices) { |
+// TODO(https://crbug.com/507419): Run test on Windows once Bluetooth |
ortuno
2017/04/03 03:36:38
It's strange to have this since we already have ma
juncai
2017/04/04 03:03:42
device_->IsConnected() returns false on Windows so
ortuno
2017/04/04 03:32:11
We already run many Service tests[1] on Windows so
juncai
2017/04/04 20:33:40
Done.
|
+// connection is implemented. |
+TEST_F(BluetoothGetServiceOrCharacteristicOrDescriptorTest, |
+ GetPrimaryServices) { |
if (!PlatformSupportsLowEnergy()) { |
LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
return; |
@@ -1527,28 +1531,33 @@ TEST_F(BluetoothGetServiceOrCharacteristicTest, GetPrimaryServices) { |
EXPECT_EQ(3u, device_->GetPrimaryServices().size()); |
} |
-TEST_F(BluetoothGetServiceOrCharacteristicTest, GetPrimaryServicesByUUID) { |
+TEST_F(BluetoothGetServiceOrCharacteristicOrDescriptorTest, |
+ GetPrimaryServicesByUUID) { |
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 = |
- "00000002-0000-1000-8000-00805f9b34fb"; |
+ std::vector<BluetoothRemoteGattService*> services0 = |
ortuno
2017/04/03 03:36:38
To avoid services0, services1, etc. you could add
juncai
2017/04/04 03:03:42
Done.
|
+ device_->GetPrimaryServicesByUUID(BluetoothUUID(service_uuids_[0])); |
+ EXPECT_EQ(1u, services0.size()); |
+ EXPECT_EQ(BluetoothUUID(service_uuids_[0]), services0[0]->GetUUID()); |
+ |
+ std::vector<BluetoothRemoteGattService*> services1 = |
+ device_->GetPrimaryServicesByUUID(BluetoothUUID(service_uuids_[1])); |
+ EXPECT_EQ(2u, services1.size()); |
+ EXPECT_EQ(BluetoothUUID(service_uuids_[1]), services1[0]->GetUUID()); |
+ EXPECT_EQ(BluetoothUUID(service_uuids_[1]), services1[1]->GetUUID()); |
ortuno
2017/04/03 03:36:38
Add:
EXPECT_NEQ(services1[0]->GetIdentifier(), se
juncai
2017/04/04 03:03:41
Done.
|
+ |
EXPECT_TRUE(device_ |
->GetPrimaryServicesByUUID( |
- BluetoothUUID(service_uuid_not_exist_in_setup)) |
+ BluetoothUUID(BluetoothTestBase::kTestUUIDGenericAccess)) |
.empty()); |
} |
-TEST_F(BluetoothGetServiceOrCharacteristicTest, GetCharacteristicsByUUID) { |
+TEST_F(BluetoothGetServiceOrCharacteristicOrDescriptorTest, |
+ GetCharacteristicsByUUID) { |
if (!PlatformSupportsLowEnergy()) { |
LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
return; |
@@ -1570,21 +1579,26 @@ TEST_F(BluetoothGetServiceOrCharacteristicTest, GetCharacteristicsByUUID) { |
SimulateGattCharacteristic(primary_services[2], characteristic_uuid1, |
0 /* properties */); |
- EXPECT_EQ(1u, |
- device_ |
- ->GetCharacteristicsByUUID(service_instance_id0, |
- 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::vector<BluetoothRemoteGattCharacteristic*> characteristics0 = |
ortuno
2017/04/03 03:36:38
Also consider using brackets to avoid characterist
juncai
2017/04/04 03:03:41
Done.
|
+ device_->GetCharacteristicsByUUID(service_instance_id0, |
+ BluetoothUUID(characteristic_uuid0)); |
+ EXPECT_EQ(1u, characteristics0.size()); |
+ EXPECT_EQ(BluetoothUUID(characteristic_uuid0), |
+ characteristics0[0]->GetUUID()); |
+ |
+ std::vector<BluetoothRemoteGattCharacteristic*> characteristics1 = |
+ device_->GetCharacteristicsByUUID(service_instance_id1, |
+ BluetoothUUID(characteristic_uuid1)); |
+ EXPECT_EQ(1u, characteristics1.size()); |
+ EXPECT_EQ(BluetoothUUID(characteristic_uuid1), |
+ characteristics1[0]->GetUUID()); |
+ |
+ std::vector<BluetoothRemoteGattCharacteristic*> characteristics2 = |
+ device_->GetCharacteristicsByUUID(service_instance_id2, |
+ BluetoothUUID(characteristic_uuid1)); |
+ EXPECT_EQ(1u, characteristics2.size()); |
+ EXPECT_EQ(BluetoothUUID(characteristic_uuid1), |
+ characteristics2[0]->GetUUID()); |
std::string service_instance_id_not_exist_in_setup = |
ortuno
2017/04/03 03:36:38
Can we refactor this method to be in the service t
juncai
2017/04/04 03:03:42
https://cs.chromium.org/chromium/src/device/blueto
ortuno
2017/04/04 03:32:11
Can you open an issue explaining what needs to be
juncai
2017/04/04 20:33:40
Moved it to BluetoothRemoteGattService.
Done.
|
"non-existent platform specific service instance id"; |
@@ -1617,6 +1631,92 @@ TEST_F(BluetoothGetServiceOrCharacteristicTest, GetCharacteristicsByUUID) { |
BluetoothUUID(characteristic_uuid_not_exist_in_setup)) |
.empty()); |
} |
+ |
+TEST_F(BluetoothGetServiceOrCharacteristicOrDescriptorTest, |
+ GetDescriptorsByUUID) { |
+ 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 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 */); |
+ |
+ BluetoothRemoteGattCharacteristic* characteristic0 = |
+ primary_services[0]->GetCharacteristics()[0]; |
+ BluetoothRemoteGattCharacteristic* characteristic1 = |
+ primary_services[1]->GetCharacteristics()[0]; |
+ BluetoothRemoteGattCharacteristic* characteristic2 = |
+ primary_services[2]->GetCharacteristics()[0]; |
+ |
+ // Add several descriptors. No descriptors are added to |characteristic2|. |
ortuno
2017/04/03 03:36:38
We should just put all these setup code in the con
juncai
2017/04/04 03:03:42
The same function already exists at BluetoothRemot
|
+ BluetoothUUID descriptor_uuid0("11111111-0000-1000-8000-00805f9b34fb"); |
+ BluetoothUUID descriptor_uuid1("22222222-0000-1000-8000-00805f9b34fb"); |
+ BluetoothUUID descriptor_uuid2("33333333-0000-1000-8000-00805f9b34fb"); |
ortuno
2017/04/03 03:36:38
Also add these to the list of test UUIDs. I would
juncai
2017/04/04 03:03:42
ditto.
|
+ SimulateGattDescriptor(characteristic0, descriptor_uuid0.canonical_value()); |
+ SimulateGattDescriptor(characteristic0, descriptor_uuid1.canonical_value()); |
+ SimulateGattDescriptor(characteristic1, descriptor_uuid2.canonical_value()); |
+ SimulateGattDescriptor(characteristic1, descriptor_uuid2.canonical_value()); |
+ |
+ BluetoothUUID descriptor_uuid_not_exist_in_setup( |
+ "12345678-0000-1000-8000-00805f9b34fb"); |
+ |
+ std::vector<device::BluetoothRemoteGattDescriptor*> descriptors0 = |
+ device_->GetDescriptorsByUUID(characteristic0, descriptor_uuid0); |
+ EXPECT_EQ(1u, descriptors0.size()); |
+ EXPECT_EQ(descriptor_uuid0, descriptors0[0]->GetUUID()); |
+ std::vector<device::BluetoothRemoteGattDescriptor*> descriptors1 = |
ortuno
2017/04/03 03:36:38
nit: add new line
juncai
2017/04/04 03:03:42
ditto.
|
+ device_->GetDescriptorsByUUID(characteristic0, descriptor_uuid1); |
+ EXPECT_EQ(1u, descriptors1.size()); |
+ EXPECT_EQ(descriptor_uuid1, descriptors1[0]->GetUUID()); |
+ EXPECT_TRUE( |
+ device_->GetDescriptorsByUUID(characteristic0, descriptor_uuid2).empty()); |
+ EXPECT_TRUE(device_ |
+ ->GetDescriptorsByUUID(characteristic0, |
+ descriptor_uuid_not_exist_in_setup) |
+ .empty()); |
+ |
+ std::vector<device::BluetoothRemoteGattDescriptor*> descriptors2 = |
+ device_->GetDescriptorsByUUID(characteristic1, descriptor_uuid2); |
+ EXPECT_EQ(2u, descriptors2.size()); |
+ EXPECT_EQ(descriptor_uuid2, descriptors2[0]->GetUUID()); |
+ EXPECT_EQ(descriptor_uuid2, descriptors2[1]->GetUUID()); |
+ EXPECT_TRUE( |
+ device_->GetDescriptorsByUUID(characteristic1, descriptor_uuid0).empty()); |
+ EXPECT_TRUE( |
+ device_->GetDescriptorsByUUID(characteristic1, descriptor_uuid1).empty()); |
+ EXPECT_TRUE(device_ |
+ ->GetDescriptorsByUUID(characteristic1, |
+ descriptor_uuid_not_exist_in_setup) |
+ .empty()); |
+ |
+ EXPECT_TRUE( |
+ device_->GetDescriptorsByUUID(characteristic2, descriptor_uuid0).empty()); |
+ EXPECT_TRUE( |
+ device_->GetDescriptorsByUUID(characteristic2, descriptor_uuid1).empty()); |
+ EXPECT_TRUE( |
+ device_->GetDescriptorsByUUID(characteristic2, descriptor_uuid2).empty()); |
+ EXPECT_TRUE(device_ |
+ ->GetDescriptorsByUUID(characteristic2, |
+ descriptor_uuid_not_exist_in_setup) |
+ .empty()); |
+ |
+ EXPECT_TRUE( |
ortuno
2017/04/03 03:36:38
Can we refactor this method to be in the character
juncai
2017/04/04 03:03:42
ditto.
|
+ device_ |
+ ->GetDescriptorsByUUID(nullptr /* characteristic */, descriptor_uuid0) |
+ .empty()); |
+} |
+ |
#endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
} // namespace device |