OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/run_loop.h" | 5 #include "base/run_loop.h" |
6 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" | 6 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" |
7 #include "device/bluetooth/bluetooth_remote_gatt_service.h" | 7 #include "device/bluetooth/bluetooth_remote_gatt_service.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 #if defined(OS_ANDROID) | 10 #if defined(OS_ANDROID) |
(...skipping 10 matching lines...) Expand all Loading... |
21 // Creates adapter_, device_, service_, characteristic_, | 21 // Creates adapter_, device_, service_, characteristic_, |
22 // descriptor1_, & descriptor2_. | 22 // descriptor1_, & descriptor2_. |
23 void FakeDescriptorBoilerplate() { | 23 void FakeDescriptorBoilerplate() { |
24 InitWithFakeAdapter(); | 24 InitWithFakeAdapter(); |
25 StartLowEnergyDiscoverySession(); | 25 StartLowEnergyDiscoverySession(); |
26 device_ = SimulateLowEnergyDevice(3); | 26 device_ = SimulateLowEnergyDevice(3); |
27 device_->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 27 device_->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
28 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 28 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
29 SimulateGattConnection(device_); | 29 SimulateGattConnection(device_); |
30 base::RunLoop().RunUntilIdle(); | 30 base::RunLoop().RunUntilIdle(); |
31 std::vector<std::string> services; | 31 SimulateGattServicesDiscovered( |
32 std::string uuid("00000000-0000-1000-8000-00805f9b34fb"); | 32 device_, std::vector<std::string>({kTestUUIDGenericAccess})); |
33 services.push_back(uuid); | |
34 SimulateGattServicesDiscovered(device_, services); | |
35 base::RunLoop().RunUntilIdle(); | 33 base::RunLoop().RunUntilIdle(); |
36 ASSERT_EQ(1u, device_->GetGattServices().size()); | 34 ASSERT_EQ(1u, device_->GetGattServices().size()); |
37 service_ = device_->GetGattServices()[0]; | 35 service_ = device_->GetGattServices()[0]; |
38 SimulateGattCharacteristic(service_, uuid, 0); | 36 SimulateGattCharacteristic(service_, kTestUUIDDeviceName, 0); |
39 ASSERT_EQ(1u, service_->GetCharacteristics().size()); | 37 ASSERT_EQ(1u, service_->GetCharacteristics().size()); |
40 characteristic_ = service_->GetCharacteristics()[0]; | 38 characteristic_ = service_->GetCharacteristics()[0]; |
41 SimulateGattDescriptor(characteristic_, | 39 SimulateGattDescriptor(characteristic_, |
42 "00000001-0000-1000-8000-00805f9b34fb"); | 40 kTestUUIDCharacteristicUserDescription); |
43 SimulateGattDescriptor(characteristic_, | 41 SimulateGattDescriptor(characteristic_, |
44 "00000002-0000-1000-8000-00805f9b34fb"); | 42 kTestUUIDClientCharacteristicConfiguration); |
45 ASSERT_EQ(2u, characteristic_->GetDescriptors().size()); | 43 ASSERT_EQ(2u, characteristic_->GetDescriptors().size()); |
46 descriptor1_ = characteristic_->GetDescriptors()[0]; | 44 descriptor1_ = characteristic_->GetDescriptors()[0]; |
47 descriptor2_ = characteristic_->GetDescriptors()[1]; | 45 descriptor2_ = characteristic_->GetDescriptors()[1]; |
48 ResetEventCounts(); | 46 ResetEventCounts(); |
49 } | 47 } |
50 | 48 |
51 BluetoothDevice* device_ = nullptr; | 49 BluetoothDevice* device_ = nullptr; |
52 BluetoothRemoteGattService* service_ = nullptr; | 50 BluetoothRemoteGattService* service_ = nullptr; |
53 BluetoothRemoteGattCharacteristic* characteristic_ = nullptr; | 51 BluetoothRemoteGattCharacteristic* characteristic_ = nullptr; |
54 BluetoothRemoteGattDescriptor* descriptor1_ = nullptr; | 52 BluetoothRemoteGattDescriptor* descriptor1_ = nullptr; |
(...skipping 16 matching lines...) Expand all Loading... |
71 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 69 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
72 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 70 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
73 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 71 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
74 SimulateGattConnection(device1); | 72 SimulateGattConnection(device1); |
75 SimulateGattConnection(device2); | 73 SimulateGattConnection(device2); |
76 base::RunLoop().RunUntilIdle(); | 74 base::RunLoop().RunUntilIdle(); |
77 | 75 |
78 // 3 services (all with same UUID). | 76 // 3 services (all with same UUID). |
79 // 1 on the first device (to test characteristic instances across devices). | 77 // 1 on the first device (to test characteristic instances across devices). |
80 // 2 on the second device (to test same device, multiple service instances). | 78 // 2 on the second device (to test same device, multiple service instances). |
81 std::vector<std::string> services; | 79 SimulateGattServicesDiscovered( |
82 std::string uuid = "00000000-0000-1000-8000-00805f9b34fb"; | 80 device1, std::vector<std::string>({kTestUUIDGenericAccess})); |
83 services.push_back(uuid); | |
84 SimulateGattServicesDiscovered(device1, services); | |
85 base::RunLoop().RunUntilIdle(); | 81 base::RunLoop().RunUntilIdle(); |
86 services.push_back(uuid); | 82 SimulateGattServicesDiscovered( |
87 SimulateGattServicesDiscovered(device2, services); | 83 device2, std::vector<std::string>( |
| 84 {kTestUUIDGenericAccess, kTestUUIDGenericAccess})); |
88 base::RunLoop().RunUntilIdle(); | 85 base::RunLoop().RunUntilIdle(); |
89 BluetoothRemoteGattService* service1 = device1->GetGattServices()[0]; | 86 BluetoothRemoteGattService* service1 = device1->GetGattServices()[0]; |
90 BluetoothRemoteGattService* service2 = device2->GetGattServices()[0]; | 87 BluetoothRemoteGattService* service2 = device2->GetGattServices()[0]; |
91 BluetoothRemoteGattService* service3 = device2->GetGattServices()[1]; | 88 BluetoothRemoteGattService* service3 = device2->GetGattServices()[1]; |
92 // 6 characteristics (same UUID), 2 on each service. | 89 // 6 characteristics (same UUID), 2 on each service. |
93 SimulateGattCharacteristic(service1, uuid, /* properties */ 0); | 90 SimulateGattCharacteristic(service1, kTestUUIDDeviceName, /* properties */ 0); |
94 SimulateGattCharacteristic(service1, uuid, /* properties */ 0); | 91 SimulateGattCharacteristic(service1, kTestUUIDDeviceName, /* properties */ 0); |
95 SimulateGattCharacteristic(service2, uuid, /* properties */ 0); | 92 SimulateGattCharacteristic(service2, kTestUUIDDeviceName, /* properties */ 0); |
96 SimulateGattCharacteristic(service2, uuid, /* properties */ 0); | 93 SimulateGattCharacteristic(service2, kTestUUIDDeviceName, /* properties */ 0); |
97 SimulateGattCharacteristic(service3, uuid, /* properties */ 0); | 94 SimulateGattCharacteristic(service3, kTestUUIDDeviceName, /* properties */ 0); |
98 SimulateGattCharacteristic(service3, uuid, /* properties */ 0); | 95 SimulateGattCharacteristic(service3, kTestUUIDDeviceName, /* properties */ 0); |
99 BluetoothRemoteGattCharacteristic* char1 = service1->GetCharacteristics()[0]; | 96 BluetoothRemoteGattCharacteristic* char1 = service1->GetCharacteristics()[0]; |
100 BluetoothRemoteGattCharacteristic* char2 = service1->GetCharacteristics()[1]; | 97 BluetoothRemoteGattCharacteristic* char2 = service1->GetCharacteristics()[1]; |
101 BluetoothRemoteGattCharacteristic* char3 = service2->GetCharacteristics()[0]; | 98 BluetoothRemoteGattCharacteristic* char3 = service2->GetCharacteristics()[0]; |
102 BluetoothRemoteGattCharacteristic* char4 = service2->GetCharacteristics()[1]; | 99 BluetoothRemoteGattCharacteristic* char4 = service2->GetCharacteristics()[1]; |
103 BluetoothRemoteGattCharacteristic* char5 = service3->GetCharacteristics()[0]; | 100 BluetoothRemoteGattCharacteristic* char5 = service3->GetCharacteristics()[0]; |
104 BluetoothRemoteGattCharacteristic* char6 = service3->GetCharacteristics()[1]; | 101 BluetoothRemoteGattCharacteristic* char6 = service3->GetCharacteristics()[1]; |
105 // 6 descriptors (same UUID), 1 on each characteristic | 102 // 6 descriptors (same UUID), 1 on each characteristic |
106 // TODO(576900) Test multiple descriptors with same UUID on one | 103 // TODO(576900) Test multiple descriptors with same UUID on one |
107 // characteristic. | 104 // characteristic. |
108 SimulateGattDescriptor(char1, uuid); | 105 SimulateGattDescriptor(char1, kTestUUIDCharacteristicUserDescription); |
109 SimulateGattDescriptor(char2, uuid); | 106 SimulateGattDescriptor(char2, kTestUUIDCharacteristicUserDescription); |
110 SimulateGattDescriptor(char3, uuid); | 107 SimulateGattDescriptor(char3, kTestUUIDCharacteristicUserDescription); |
111 SimulateGattDescriptor(char4, uuid); | 108 SimulateGattDescriptor(char4, kTestUUIDCharacteristicUserDescription); |
112 SimulateGattDescriptor(char5, uuid); | 109 SimulateGattDescriptor(char5, kTestUUIDCharacteristicUserDescription); |
113 SimulateGattDescriptor(char6, uuid); | 110 SimulateGattDescriptor(char6, kTestUUIDCharacteristicUserDescription); |
114 BluetoothRemoteGattDescriptor* desc1 = char1->GetDescriptors()[0]; | 111 BluetoothRemoteGattDescriptor* desc1 = char1->GetDescriptors()[0]; |
115 BluetoothRemoteGattDescriptor* desc2 = char2->GetDescriptors()[0]; | 112 BluetoothRemoteGattDescriptor* desc2 = char2->GetDescriptors()[0]; |
116 BluetoothRemoteGattDescriptor* desc3 = char3->GetDescriptors()[0]; | 113 BluetoothRemoteGattDescriptor* desc3 = char3->GetDescriptors()[0]; |
117 BluetoothRemoteGattDescriptor* desc4 = char4->GetDescriptors()[0]; | 114 BluetoothRemoteGattDescriptor* desc4 = char4->GetDescriptors()[0]; |
118 BluetoothRemoteGattDescriptor* desc5 = char5->GetDescriptors()[0]; | 115 BluetoothRemoteGattDescriptor* desc5 = char5->GetDescriptors()[0]; |
119 BluetoothRemoteGattDescriptor* desc6 = char6->GetDescriptors()[0]; | 116 BluetoothRemoteGattDescriptor* desc6 = char6->GetDescriptors()[0]; |
120 | 117 |
121 // All IDs are unique. | 118 // All IDs are unique. |
122 EXPECT_NE(desc1->GetIdentifier(), desc2->GetIdentifier()); | 119 EXPECT_NE(desc1->GetIdentifier(), desc2->GetIdentifier()); |
123 EXPECT_NE(desc1->GetIdentifier(), desc3->GetIdentifier()); | 120 EXPECT_NE(desc1->GetIdentifier(), desc3->GetIdentifier()); |
(...skipping 22 matching lines...) Expand all Loading... |
146 if (!PlatformSupportsLowEnergy()) { | 143 if (!PlatformSupportsLowEnergy()) { |
147 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 144 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
148 return; | 145 return; |
149 } | 146 } |
150 InitWithFakeAdapter(); | 147 InitWithFakeAdapter(); |
151 StartLowEnergyDiscoverySession(); | 148 StartLowEnergyDiscoverySession(); |
152 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 149 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
153 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 150 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
154 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 151 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
155 SimulateGattConnection(device); | 152 SimulateGattConnection(device); |
156 std::vector<std::string> services; | 153 SimulateGattServicesDiscovered( |
157 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 154 device, std::vector<std::string>({kTestUUIDGenericAccess})); |
158 SimulateGattServicesDiscovered(device, services); | |
159 base::RunLoop().RunUntilIdle(); | 155 base::RunLoop().RunUntilIdle(); |
160 ASSERT_EQ(1u, device->GetGattServices().size()); | 156 ASSERT_EQ(1u, device->GetGattServices().size()); |
161 BluetoothRemoteGattService* service = device->GetGattServices()[0]; | 157 BluetoothRemoteGattService* service = device->GetGattServices()[0]; |
162 | 158 |
163 SimulateGattCharacteristic(service, "00000000-0000-1000-8000-00805f9b34fb", | 159 SimulateGattCharacteristic(service, kTestUUIDDeviceName, |
164 /* properties */ 0); | 160 /* properties */ 0); |
165 ASSERT_EQ(1u, service->GetCharacteristics().size()); | 161 ASSERT_EQ(1u, service->GetCharacteristics().size()); |
166 BluetoothRemoteGattCharacteristic* characteristic = | 162 BluetoothRemoteGattCharacteristic* characteristic = |
167 service->GetCharacteristics()[0]; | 163 service->GetCharacteristics()[0]; |
168 | 164 |
169 // Create 2 descriptors. | 165 // Create 2 descriptors. |
170 std::string uuid_str1("11111111-0000-1000-8000-00805f9b34fb"); | 166 BluetoothUUID uuid1(kTestUUIDCharacteristicUserDescription); |
171 std::string uuid_str2("22222222-0000-1000-8000-00805f9b34fb"); | 167 BluetoothUUID uuid2(kTestUUIDClientCharacteristicConfiguration); |
172 BluetoothUUID uuid1(uuid_str1); | 168 SimulateGattDescriptor(characteristic, |
173 BluetoothUUID uuid2(uuid_str2); | 169 kTestUUIDCharacteristicUserDescription); |
174 SimulateGattDescriptor(characteristic, uuid_str1); | 170 SimulateGattDescriptor(characteristic, |
175 SimulateGattDescriptor(characteristic, uuid_str2); | 171 kTestUUIDClientCharacteristicConfiguration); |
176 ASSERT_EQ(2u, characteristic->GetDescriptors().size()); | 172 ASSERT_EQ(2u, characteristic->GetDescriptors().size()); |
177 BluetoothRemoteGattDescriptor* descriptor1 = | 173 BluetoothRemoteGattDescriptor* descriptor1 = |
178 characteristic->GetDescriptors()[0]; | 174 characteristic->GetDescriptors()[0]; |
179 BluetoothRemoteGattDescriptor* descriptor2 = | 175 BluetoothRemoteGattDescriptor* descriptor2 = |
180 characteristic->GetDescriptors()[1]; | 176 characteristic->GetDescriptors()[1]; |
181 | 177 |
182 // Swap as needed to have descriptor1 be the one with uuid1. | 178 // Swap as needed to have descriptor1 be the one with uuid1. |
183 if (descriptor2->GetUUID() == uuid1) | 179 if (descriptor2->GetUUID() == uuid1) |
184 std::swap(descriptor1, descriptor2); | 180 std::swap(descriptor1, descriptor2); |
185 | 181 |
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
683 std::vector<uint8_t>(), GetCallback(Call::NOT_EXPECTED), | 679 std::vector<uint8_t>(), GetCallback(Call::NOT_EXPECTED), |
684 // TODO(crbug.com/621901): Expect an error. | 680 // TODO(crbug.com/621901): Expect an error. |
685 GetGattErrorCallback(Call::NOT_EXPECTED)); | 681 GetGattErrorCallback(Call::NOT_EXPECTED)); |
686 | 682 |
687 base::RunLoop().RunUntilIdle(); | 683 base::RunLoop().RunUntilIdle(); |
688 // TODO(crbug.com/621901): Test that an error was returned. | 684 // TODO(crbug.com/621901): Test that an error was returned. |
689 } | 685 } |
690 #endif // defined(OS_ANDROID) | 686 #endif // defined(OS_ANDROID) |
691 | 687 |
692 } // namespace device | 688 } // namespace device |
OLD | NEW |