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_device.h" | 5 #include "device/bluetooth/bluetooth_device.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
14 #include "device/bluetooth/bluetooth_remote_gatt_service.h" | 14 #include "device/bluetooth/bluetooth_remote_gatt_service.h" |
15 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" | 15 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
18 #if defined(OS_ANDROID) | 18 #if defined(OS_ANDROID) |
19 #include "device/bluetooth/test/bluetooth_test_android.h" | 19 #include "device/bluetooth/test/bluetooth_test_android.h" |
20 #elif defined(OS_MACOSX) | 20 #elif defined(OS_MACOSX) |
21 #include "device/bluetooth/test/bluetooth_test_mac.h" | 21 #include "device/bluetooth/test/bluetooth_test_mac.h" |
22 #elif defined(OS_WIN) | 22 #elif defined(OS_WIN) |
23 #include "device/bluetooth/test/bluetooth_test_win.h" | 23 #include "device/bluetooth/test/bluetooth_test_win.h" |
24 #elif defined(OS_CHROMEOS) || defined(OS_LINUX) | 24 #elif defined(OS_CHROMEOS) || defined(OS_LINUX) |
25 #include "device/bluetooth/test/bluetooth_test_bluez.h" | 25 #include "device/bluetooth/test/bluetooth_test_bluez.h" |
26 #endif | 26 #endif |
27 | 27 |
28 namespace device { | 28 namespace device { |
29 | 29 |
30 namespace { | |
31 | |
32 int8_t ToInt8(BluetoothTest::TestRSSI rssi) { | |
33 return static_cast<int8_t>(rssi); | |
34 } | |
35 | |
36 int8_t ToInt8(BluetoothTest::TestTxPower tx_power) { | |
37 return static_cast<int8_t>(tx_power); | |
38 } | |
39 | |
40 } // namespace | |
41 | |
30 using UUIDSet = BluetoothDevice::UUIDSet; | 42 using UUIDSet = BluetoothDevice::UUIDSet; |
31 using ServiceDataMap = BluetoothDevice::ServiceDataMap; | 43 using ServiceDataMap = BluetoothDevice::ServiceDataMap; |
32 | 44 |
33 TEST(BluetoothDeviceTest, CanonicalizeAddressFormat_AcceptsAllValidFormats) { | 45 TEST(BluetoothDeviceTest, CanonicalizeAddressFormat_AcceptsAllValidFormats) { |
34 // There are three valid separators (':', '-', and none). | 46 // There are three valid separators (':', '-', and none). |
35 // Case shouldn't matter. | 47 // Case shouldn't matter. |
36 const char* const kValidFormats[] = { | 48 const char* const kValidFormats[] = { |
37 "1A:2B:3C:4D:5E:6F", | 49 "1A:2B:3C:4D:5E:6F", |
38 "1a:2B:3c:4D:5e:6F", | 50 "1a:2B:3c:4D:5e:6F", |
39 "1a:2b:3c:4d:5e:6f", | 51 "1a:2b:3c:4d:5e:6f", |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
115 } | 127 } |
116 InitWithFakeAdapter(); | 128 InitWithFakeAdapter(); |
117 StartLowEnergyDiscoverySession(); | 129 StartLowEnergyDiscoverySession(); |
118 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 130 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
119 ASSERT_TRUE(device); | 131 ASSERT_TRUE(device); |
120 UUIDSet uuids = device->GetUUIDs(); | 132 UUIDSet uuids = device->GetUUIDs(); |
121 EXPECT_EQ(0u, uuids.size()); | 133 EXPECT_EQ(0u, uuids.size()); |
122 } | 134 } |
123 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) | 135 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) |
124 | 136 |
137 #if defined(OS_MACOSX) | |
138 // TODO(ortuno): Enable on Android once it supports Service Data. | |
139 // http://crbug.com/639408 | |
scheib
2016/08/29 02:58:13
BTW I think we should stick with TODO(639408) styl
ortuno
2016/08/29 15:53:58
Acknowledged.
| |
140 TEST_F(BluetoothTest, GetServiceDataUUIDs_GetServiceDataForUUID) { | |
141 if (!PlatformSupportsLowEnergy()) { | |
142 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | |
143 return; | |
144 } | |
145 InitWithFakeAdapter(); | |
146 StartLowEnergyDiscoverySession(); | |
147 | |
148 // Receive Advertisement with service data. | |
149 BluetoothDevice* device = SimulateLowEnergyDevice(1); | |
150 | |
151 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}), | |
152 device->GetServiceData()); | |
153 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate)}), | |
154 device->GetServiceDataUUIDs()); | |
155 EXPECT_EQ(std::vector<uint8_t>({1}), | |
156 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | |
157 | |
158 // Receive Advertisement with no service data. | |
159 SimulateLowEnergyDevice(3); | |
160 | |
161 EXPECT_TRUE(device->GetServiceData().empty()); | |
162 EXPECT_TRUE(device->GetServiceDataUUIDs().empty()); | |
163 EXPECT_EQ(nullptr, | |
164 device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | |
165 | |
166 // Receive Advertisement with new service data. | |
167 SimulateLowEnergyDevice(2); | |
168 | |
169 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}}, | |
170 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}), | |
171 device->GetServiceData()); | |
172 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate), | |
173 BluetoothUUID(kTestUUIDImmediateAlert)}), | |
174 device->GetServiceDataUUIDs()); | |
175 EXPECT_EQ(std::vector<uint8_t>({2}), | |
176 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | |
177 EXPECT_EQ( | |
178 std::vector<uint8_t>({0}), | |
179 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDImmediateAlert))); | |
180 | |
181 // Stop discovery. | |
182 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED), | |
183 GetErrorCallback(Call::NOT_EXPECTED)); | |
184 ASSERT_FALSE(adapter_->IsDiscovering()); | |
185 ASSERT_FALSE(discovery_sessions_[0]->IsActive()); | |
186 | |
187 EXPECT_TRUE(device->GetServiceData().empty()); | |
188 EXPECT_TRUE(device->GetServiceDataUUIDs().empty()); | |
189 EXPECT_EQ(nullptr, | |
190 device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | |
191 EXPECT_EQ(nullptr, device->GetServiceDataForUUID( | |
192 BluetoothUUID(kTestUUIDImmediateAlert))); | |
193 } | |
194 #endif // defined(OS_MACOSX) | |
195 | |
125 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 196 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
126 // Tests that the Advertisement Data fields are correctly updated during | 197 // Tests that the Advertisement Data fields are correctly updated during |
127 // discovery. | 198 // discovery. |
128 TEST_F(BluetoothTest, AdvertisementData_Discovery) { | 199 TEST_F(BluetoothTest, AdvertisementData_Discovery) { |
129 if (!PlatformSupportsLowEnergy()) { | 200 if (!PlatformSupportsLowEnergy()) { |
130 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 201 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
131 return; | 202 return; |
132 } | 203 } |
133 InitWithFakeAdapter(); | 204 InitWithFakeAdapter(); |
134 TestBluetoothAdapterObserver observer(adapter_); | 205 TestBluetoothAdapterObserver observer(adapter_); |
135 | 206 |
136 // Start Discovery Session and receive Advertisement. No DeviceChanged event | 207 // Start Discovery Session and receive Advertisement, should |
137 // because the device is new. | 208 // not notify of device changed because the device is new. |
209 // - GetInquiryRSSI: Should return the packet's rssi. | |
138 // - GetUUIDs: Should return Advertised UUIDs. | 210 // - GetUUIDs: Should return Advertised UUIDs. |
139 // - GetServiceData: Should return advertised Service Data. | 211 // - GetServiceData: Should return advertised Service Data. |
212 // - GetInquiryTxPower: Should return the packet's advertised Tx Power. | |
140 StartLowEnergyDiscoverySession(); | 213 StartLowEnergyDiscoverySession(); |
141 BluetoothDevice* device = SimulateLowEnergyDevice(1); | 214 BluetoothDevice* device = SimulateLowEnergyDevice(1); |
142 | 215 |
143 EXPECT_EQ(0, observer.device_changed_count()); | 216 EXPECT_EQ(0, observer.device_changed_count()); |
144 EXPECT_EQ(2u, device->GetUUIDs().size()); | |
145 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | |
146 BluetoothUUID(kTestUUIDGenericAccess))); | |
147 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | |
148 BluetoothUUID(kTestUUIDGenericAttribute))); | |
149 | 217 |
218 EXPECT_EQ(ToInt8(TestRSSI::LOWEST), device->GetInquiryRSSI().value()); | |
219 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDGenericAccess), | |
220 BluetoothUUID(kTestUUIDGenericAttribute)}), | |
221 device->GetUUIDs()); | |
150 #if defined(OS_MACOSX) | 222 #if defined(OS_MACOSX) |
151 // TODO(ortuno): Enable on Android once it supports Service Data. | 223 // TODO(ortuno): Enable on Android once it supports Service Data. |
152 // http://crbug.com/639408 | 224 // http://crbug.com/639408 |
153 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}), | 225 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}), |
154 device->GetServiceData()); | 226 device->GetServiceData()); |
227 #endif // defined(OS_MACOSX) | |
228 EXPECT_EQ(ToInt8(TestTxPower::LOWEST), device->GetInquiryTxPower().value()); | |
155 | 229 |
156 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate)}), | 230 // Receive Advertisement with no UUIDs, Service Data, or Tx Power, should |
157 device->GetServiceDataUUIDs()); | 231 // notify device changed. |
158 | 232 // - GetInquiryRSSI: Should return packet's rssi. |
159 EXPECT_EQ(std::vector<uint8_t>({1}), | |
160 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | |
161 #endif // defined(OS_MACOSX) | |
162 | |
163 // Receive Advertisement with no UUIDs or Service Data, should notify | |
164 // device changed. | |
165 // - GetUUIDs: Should return no UUIDs. | 233 // - GetUUIDs: Should return no UUIDs. |
166 // - GetServiceData: Should return empty map. | 234 // - GetServiceData: Should return empty map. |
235 // - GetInquiryTxPower: Should return nullopt because of no Tx Power. | |
167 SimulateLowEnergyDevice(3); | 236 SimulateLowEnergyDevice(3); |
168 EXPECT_EQ(1, observer.device_changed_count()); | 237 EXPECT_EQ(1, observer.device_changed_count()); |
169 EXPECT_EQ(0u, device->GetUUIDs().size()); | |
170 | 238 |
239 EXPECT_EQ(ToInt8(TestRSSI::LOW), device->GetInquiryRSSI().value()); | |
240 EXPECT_TRUE(device->GetUUIDs().empty()); | |
171 #if defined(OS_MACOSX) | 241 #if defined(OS_MACOSX) |
172 // TODO(ortuno): Enable on Android once it supports Service Data. | 242 // TODO(ortuno): Enable on Android once it supports Service Data. |
173 // http://crbug.com/639408 | 243 // http://crbug.com/639408 |
174 EXPECT_EQ(ServiceDataMap(), device->GetServiceData()); | 244 EXPECT_TRUE(device->GetServiceData().empty()); |
175 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs()); | |
176 EXPECT_EQ(nullptr, | |
177 device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | |
178 #endif // defined(OS_MACOSX) | 245 #endif // defined(OS_MACOSX) |
246 EXPECT_FALSE(device->GetInquiryTxPower()); | |
179 | 247 |
180 // Receive Advertisement with different UUIDs and Service Data, should notify | 248 // Receive Advertisement with different UUIDs, Service Data, and Tx Power, |
181 // device changed. | 249 // should notify device changed. |
250 // - GetInquiryRSSI: Should return last packet's rssi. | |
182 // - GetUUIDs: Should return latest Advertised UUIDs. | 251 // - GetUUIDs: Should return latest Advertised UUIDs. |
183 // - GetServiceData: Should return last advertised Service Data. | 252 // - GetServiceData: Should return last advertised Service Data. |
253 // - GetInquiryTxPower: Should return last advertised Tx Power. | |
184 SimulateLowEnergyDevice(2); | 254 SimulateLowEnergyDevice(2); |
185 EXPECT_EQ(2, observer.device_changed_count()); | 255 EXPECT_EQ(2, observer.device_changed_count()); |
186 EXPECT_EQ(2u, device->GetUUIDs().size()); | |
187 EXPECT_FALSE(base::ContainsKey(device->GetUUIDs(), | |
188 BluetoothUUID(kTestUUIDGenericAccess))); | |
189 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | |
190 BluetoothUUID(kTestUUIDImmediateAlert))); | |
191 | 256 |
257 EXPECT_EQ(ToInt8(TestRSSI::LOWER), device->GetInquiryRSSI().value()); | |
258 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDImmediateAlert), | |
259 BluetoothUUID(kTestUUIDLinkLoss)}), | |
260 device->GetUUIDs()); | |
192 #if defined(OS_MACOSX) | 261 #if defined(OS_MACOSX) |
193 // TODO(ortuno): Enable on Android once it supports Service Data. | 262 // TODO(ortuno): Enable on Android once it supports Service Data. |
194 // http://crbug.com/639408 | 263 // http://crbug.com/639408 |
195 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}}, | 264 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}}, |
196 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}), | 265 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}), |
197 device->GetServiceData()); | 266 device->GetServiceData()); |
198 | |
199 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate), | |
200 BluetoothUUID(kTestUUIDImmediateAlert)}), | |
201 device->GetServiceDataUUIDs()); | |
202 | |
203 EXPECT_EQ(std::vector<uint8_t>({2}), | |
204 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | |
205 EXPECT_EQ( | |
206 std::vector<uint8_t>({0}), | |
207 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDImmediateAlert))); | |
208 #endif // defined(OS_MACOSX) | 267 #endif // defined(OS_MACOSX) |
268 EXPECT_EQ(ToInt8(TestTxPower::LOWER), device->GetInquiryTxPower().value()); | |
209 | 269 |
210 // Stop discovery session, should notify of device changed. | 270 // Stop discovery session, should notify of device changed. |
271 // - GetInquiryRSSI: Should return nullopt because we are no longer | |
272 // discovering. | |
211 // - GetUUIDs: Should not return any UUIDs. | 273 // - GetUUIDs: Should not return any UUIDs. |
212 // - GetServiceData: Should return empty map. | 274 // - GetServiceData: Should return empty map. |
275 // - GetInquiryTxPower: Should return nullopt because we are no longer | |
276 // discovering. | |
213 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED), | 277 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED), |
214 GetErrorCallback(Call::NOT_EXPECTED)); | 278 GetErrorCallback(Call::NOT_EXPECTED)); |
215 ASSERT_FALSE(adapter_->IsDiscovering()); | 279 ASSERT_FALSE(adapter_->IsDiscovering()); |
216 ASSERT_FALSE(discovery_sessions_[0]->IsActive()); | 280 ASSERT_FALSE(discovery_sessions_[0]->IsActive()); |
217 | 281 |
218 EXPECT_EQ(3, observer.device_changed_count()); | 282 EXPECT_EQ(3, observer.device_changed_count()); |
219 EXPECT_EQ(0u, device->GetUUIDs().size()); | |
220 | 283 |
284 EXPECT_FALSE(device->GetInquiryRSSI()); | |
285 EXPECT_TRUE(device->GetUUIDs().empty()); | |
221 #if defined(OS_MACOSX) | 286 #if defined(OS_MACOSX) |
222 // TODO(ortuno): Enable on Android once it supports Service Data. | 287 // TODO(ortuno): Enable on Android once it supports Service Data. |
223 // http://crbug.com/639408 | 288 // http://crbug.com/639408 |
224 EXPECT_EQ(ServiceDataMap(), device->GetServiceData()); | 289 EXPECT_TRUE(device->GetServiceData().empty()); |
225 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs()); | |
226 EXPECT_EQ(nullptr, | |
227 device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | |
228 EXPECT_EQ(nullptr, device->GetServiceDataForUUID( | |
229 BluetoothUUID(kTestUUIDImmediateAlert))); | |
230 #endif // defined(OS_MACOSX) | 290 #endif // defined(OS_MACOSX) |
291 EXPECT_FALSE(device->GetInquiryTxPower()); | |
231 | 292 |
232 // Discover the device again with different UUIDs, should notify of device | 293 // Discover the device again with different UUIDs, should notify of device |
233 // changed. | 294 // changed. |
295 // - GetInquiryRSSI: Should return last packet's rssi. | |
234 // - GetUUIDs: Should return only the latest Advertised UUIDs. | 296 // - GetUUIDs: Should return only the latest Advertised UUIDs. |
235 // - GetServiceData: Should return last advertise Service Data. | 297 // - GetServiceData: Should return last advertise Service Data. |
298 // - GetInquiryTxPower: Should return last advertised Tx Power. | |
236 StartLowEnergyDiscoverySession(); | 299 StartLowEnergyDiscoverySession(); |
237 device = SimulateLowEnergyDevice(1); | 300 device = SimulateLowEnergyDevice(1); |
238 | 301 |
239 EXPECT_EQ(4, observer.device_changed_count()); | 302 EXPECT_EQ(4, observer.device_changed_count()); |
240 EXPECT_EQ(2u, device->GetUUIDs().size()); | |
241 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | |
242 BluetoothUUID(kTestUUIDGenericAccess))); | |
243 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | |
244 BluetoothUUID(kTestUUIDGenericAttribute))); | |
245 | 303 |
304 EXPECT_EQ(ToInt8(TestRSSI::LOWEST), device->GetInquiryRSSI().value()); | |
305 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDGenericAccess), | |
306 BluetoothUUID(kTestUUIDGenericAttribute)}), | |
307 device->GetUUIDs()); | |
246 #if defined(OS_MACOSX) | 308 #if defined(OS_MACOSX) |
247 // TODO(ortuno): Enable on Android once it supports Service Data. | 309 // TODO(ortuno): Enable on Android once it supports Service Data. |
248 // http://crbug.com/639408 | 310 // http://crbug.com/639408 |
249 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}), | 311 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}), |
250 device->GetServiceData()); | 312 device->GetServiceData()); |
251 | |
252 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate)}), | |
253 device->GetServiceDataUUIDs()); | |
254 | |
255 EXPECT_EQ(std::vector<uint8_t>({1}), | |
256 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | |
257 #endif // defined(OS_MACOSX) | 313 #endif // defined(OS_MACOSX) |
314 EXPECT_EQ(ToInt8(TestTxPower::LOWEST), device->GetInquiryTxPower().value()); | |
258 } | 315 } |
259 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 316 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
260 | 317 |
261 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 318 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
262 // Tests Advertisement Data is updated correctly during a connection. | 319 // Tests Advertisement Data is updated correctly during a connection. |
263 TEST_F(BluetoothTest, GetUUIDs_Connection) { | 320 TEST_F(BluetoothTest, GetUUIDs_Connection) { |
264 if (!PlatformSupportsLowEnergy()) { | 321 if (!PlatformSupportsLowEnergy()) { |
265 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 322 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
266 return; | 323 return; |
267 } | 324 } |
268 | 325 |
269 InitWithFakeAdapter(); | 326 InitWithFakeAdapter(); |
270 TestBluetoothAdapterObserver observer(adapter_); | 327 TestBluetoothAdapterObserver observer(adapter_); |
271 | 328 |
272 StartLowEnergyDiscoverySession(); | 329 StartLowEnergyDiscoverySession(); |
273 BluetoothDevice* device = SimulateLowEnergyDevice(1); | 330 BluetoothDevice* device = SimulateLowEnergyDevice(1); |
274 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED), | 331 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED), |
275 GetErrorCallback(Call::NOT_EXPECTED)); | 332 GetErrorCallback(Call::NOT_EXPECTED)); |
276 | 333 |
277 // Connect to the device. | 334 // Connect to the device. |
278 // - GetUUIDs: Should return no UUIDs because Services have not been | 335 // - GetUUIDs: Should return no UUIDs because Services have not been |
279 // discovered. | 336 // discovered. |
280 // - GetServiceData: Should return empty map because we are no longer | |
281 // discovering. | |
282 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 337 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
283 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 338 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
284 SimulateGattConnection(device); | 339 SimulateGattConnection(device); |
285 ASSERT_TRUE(device->IsConnected()); | 340 ASSERT_TRUE(device->IsConnected()); |
286 | 341 |
287 EXPECT_EQ(0u, device->GetUUIDs().size()); | 342 EXPECT_TRUE(device->GetUUIDs().empty()); |
288 #if defined(OS_MACOSX) | |
289 // TODO(ortuno): Enable on Android once it supports Service Data. | |
290 // http://crbug.com/639408 | |
291 EXPECT_EQ(ServiceDataMap(), device->GetServiceData()); | |
292 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs()); | |
293 #endif // defined(OS_MACOSX) | |
294 | 343 |
295 observer.Reset(); | 344 observer.Reset(); |
296 | 345 |
297 // Discover services, should notify of device changed. | 346 // Discover services, should notify of device changed. |
298 // - GetUUIDs: Should return the device's services' UUIDs. | 347 // - GetUUIDs: Should return the device's services' UUIDs. |
299 // - GetServiceData: Should return empty map. | |
300 std::vector<std::string> services; | 348 std::vector<std::string> services; |
301 services.push_back(BluetoothUUID(kTestUUIDGenericAccess).canonical_value()); | 349 services.push_back(BluetoothUUID(kTestUUIDGenericAccess).canonical_value()); |
302 SimulateGattServicesDiscovered(device, services); | 350 SimulateGattServicesDiscovered(device, services); |
303 | 351 |
304 EXPECT_EQ(1, observer.device_changed_count()); | 352 EXPECT_EQ(1, observer.device_changed_count()); |
305 EXPECT_EQ(1u, device->GetUUIDs().size()); | |
306 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | |
307 BluetoothUUID(kTestUUIDGenericAccess))); | |
308 | 353 |
309 #if defined(OS_MACOSX) | 354 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDGenericAccess)}), |
310 // TODO(ortuno): Enable on Android once it supports Service Data. | 355 device->GetUUIDs()); |
311 // http://crbug.com/639408 | |
312 EXPECT_EQ(ServiceDataMap(), device->GetServiceData()); | |
313 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs()); | |
314 #endif // defined(OS_MACOSX) | |
315 | 356 |
316 #if defined(OS_MACOSX) | 357 #if defined(OS_MACOSX) |
317 // TODO(ortuno): Enable in Android and Windows. | 358 // TODO(ortuno): Enable in Android and Windows. |
318 // Android and Windows don't yet support service changed events. | 359 // Android and Windows don't yet support service changed events. |
319 // http://crbug.com/548280 | 360 // http://crbug.com/548280 |
320 // http://crbug.com/579202 | 361 // http://crbug.com/579202 |
321 | 362 |
322 observer.Reset(); | 363 observer.Reset(); |
323 | 364 |
324 // Notify of services changed, should notify of device changed. | 365 // Notify of services changed, should notify of device changed. |
325 // - GetUUIDs: Should return no UUIDs because we no longer know what services | 366 // - GetUUIDs: Should return no UUIDs because we no longer know what services |
326 // the device has. | 367 // the device has. |
327 // - GetServiceData: Should return empty map. | |
328 | |
329 SimulateGattServicesChanged(device); | 368 SimulateGattServicesChanged(device); |
330 | 369 |
331 ASSERT_FALSE(device->IsGattServicesDiscoveryComplete()); | 370 ASSERT_FALSE(device->IsGattServicesDiscoveryComplete()); |
332 EXPECT_EQ(1, observer.device_changed_count()); | 371 EXPECT_EQ(1, observer.device_changed_count()); |
333 EXPECT_EQ(0u, device->GetUUIDs().size()); | 372 EXPECT_TRUE(device->GetUUIDs().empty()); |
334 | |
335 EXPECT_EQ(ServiceDataMap(), device->GetServiceData()); | |
336 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs()); | |
337 | 373 |
338 // Services discovered again, should notify of device changed. | 374 // Services discovered again, should notify of device changed. |
339 // - GetUUIDs: Should return Service UUIDs. | 375 // - GetUUIDs: Should return Service UUIDs. |
340 // - GetServiceData: Should return empty map. | |
341 SimulateGattServicesDiscovered(device, {} /* services */); | 376 SimulateGattServicesDiscovered(device, {} /* services */); |
342 | 377 |
343 EXPECT_EQ(2, observer.device_changed_count()); | 378 EXPECT_EQ(2, observer.device_changed_count()); |
344 EXPECT_EQ(1u, device->GetUUIDs().size()); | 379 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDGenericAccess)}), |
345 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | 380 device->GetUUIDs()); |
346 BluetoothUUID(kTestUUIDGenericAccess))); | |
347 | 381 |
348 EXPECT_EQ(ServiceDataMap(), device->GetServiceData()); | |
349 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs()); | |
350 #endif // defined(OS_MACOSX) | 382 #endif // defined(OS_MACOSX) |
351 | 383 |
352 observer.Reset(); | 384 observer.Reset(); |
353 | 385 |
354 // Disconnect, should notify device changed. | 386 // Disconnect, should notify device changed. |
355 // - GetUUIDs: Should return no UUIDs since we no longer know what services | 387 // - GetUUIDs: Should return no UUIDs since we no longer know what services |
356 // the device holds and notify of device changed. | 388 // the device holds and notify of device changed. |
357 // - GetServiceData: Should return empty map. | |
358 gatt_connections_[0]->Disconnect(); | 389 gatt_connections_[0]->Disconnect(); |
359 SimulateGattDisconnection(device); | 390 SimulateGattDisconnection(device); |
360 ASSERT_FALSE(device->IsGattConnected()); | 391 ASSERT_FALSE(device->IsGattConnected()); |
361 | 392 |
362 EXPECT_EQ(1, observer.device_changed_count()); | 393 EXPECT_EQ(1, observer.device_changed_count()); |
363 EXPECT_EQ(0u, device->GetUUIDs().size()); | 394 EXPECT_TRUE(device->GetUUIDs().empty()); |
364 | |
365 #if defined(OS_MACOSX) | |
366 // TODO(ortuno): Enable on Android once it supports Service Data. | |
367 // http://crbug.com/639408 | |
368 EXPECT_EQ(ServiceDataMap(), device->GetServiceData()); | |
369 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs()); | |
370 #endif // defined(OS_MACOSX) | |
371 } | 395 } |
372 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 396 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
373 | 397 |
374 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 398 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
375 // Tests Advertisement Data is updated correctly when we start discovery | 399 // Tests Advertisement Data is updated correctly when we start discovery |
376 // during a connection. | 400 // during a connection. |
377 TEST_F(BluetoothTest, GetUUIDs_DiscoveryDuringConnection) { | 401 TEST_F(BluetoothTest, AdvertisementData_DiscoveryDuringConnection) { |
378 if (!PlatformSupportsLowEnergy()) { | 402 if (!PlatformSupportsLowEnergy()) { |
379 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 403 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
380 return; | 404 return; |
381 } | 405 } |
382 | 406 |
383 InitWithFakeAdapter(); | 407 InitWithFakeAdapter(); |
384 TestBluetoothAdapterObserver observer(adapter_); | 408 TestBluetoothAdapterObserver observer(adapter_); |
385 | 409 |
386 StartLowEnergyDiscoverySession(); | 410 StartLowEnergyDiscoverySession(); |
387 BluetoothDevice* device = SimulateLowEnergyDevice(1); | 411 BluetoothDevice* device = SimulateLowEnergyDevice(1); |
388 | 412 |
389 EXPECT_EQ(2u, device->GetUUIDs().size()); | 413 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDGenericAccess), |
390 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | 414 BluetoothUUID(kTestUUIDGenericAttribute)}), |
391 BluetoothUUID(kTestUUIDGenericAccess))); | 415 device->GetUUIDs()); |
392 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | |
393 BluetoothUUID(kTestUUIDGenericAttribute))); | |
394 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED), | 416 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED), |
395 GetErrorCallback(Call::NOT_EXPECTED)); | 417 GetErrorCallback(Call::NOT_EXPECTED)); |
396 ASSERT_FALSE(adapter_->IsDiscovering()); | 418 ASSERT_FALSE(adapter_->IsDiscovering()); |
397 ASSERT_FALSE(discovery_sessions_[0]->IsActive()); | 419 ASSERT_FALSE(discovery_sessions_[0]->IsActive()); |
398 ASSERT_EQ(0u, device->GetUUIDs().size()); | 420 ASSERT_EQ(0u, device->GetUUIDs().size()); |
399 discovery_sessions_.clear(); | 421 discovery_sessions_.clear(); |
400 | 422 |
401 // Connect. | 423 // Connect. |
402 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 424 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
403 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 425 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
404 SimulateGattConnection(device); | 426 SimulateGattConnection(device); |
405 ASSERT_TRUE(device->IsConnected()); | 427 ASSERT_TRUE(device->IsConnected()); |
406 | 428 |
407 observer.Reset(); | 429 observer.Reset(); |
408 | 430 |
409 // Start Discovery and receive advertisement during connection, | 431 // Start Discovery and receive advertisement during connection, |
410 // should notify of device changed. | 432 // should notify of device changed. |
433 // - GetInquiryRSSI: Should return the packet's rssi. | |
411 // - GetUUIDs: Should return only Advertised UUIDs since services haven't | 434 // - GetUUIDs: Should return only Advertised UUIDs since services haven't |
412 // been discovered yet. | 435 // been discovered yet. |
413 // - GetServiceData: Should return last advertised Service Data. | 436 // - GetServiceData: Should return last advertised Service Data. |
437 // - GetInquiryTxPower: Should return the packet's advertised Tx Power. | |
414 StartLowEnergyDiscoverySession(); | 438 StartLowEnergyDiscoverySession(); |
415 ASSERT_TRUE(adapter_->IsDiscovering()); | 439 ASSERT_TRUE(adapter_->IsDiscovering()); |
416 ASSERT_TRUE(discovery_sessions_[0]->IsActive()); | 440 ASSERT_TRUE(discovery_sessions_[0]->IsActive()); |
417 device = SimulateLowEnergyDevice(1); | 441 device = SimulateLowEnergyDevice(1); |
418 | 442 |
419 EXPECT_EQ(1, observer.device_changed_count()); | 443 EXPECT_EQ(1, observer.device_changed_count()); |
420 EXPECT_EQ(2u, device->GetUUIDs().size()); | |
421 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | |
422 BluetoothUUID(kTestUUIDGenericAccess))); | |
423 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | |
424 BluetoothUUID(kTestUUIDGenericAttribute))); | |
425 | 444 |
445 EXPECT_EQ(ToInt8(TestRSSI::LOWEST), device->GetInquiryRSSI().value()); | |
446 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDGenericAccess), | |
447 BluetoothUUID(kTestUUIDGenericAttribute)}), | |
448 device->GetUUIDs()); | |
426 #if defined(OS_MACOSX) | 449 #if defined(OS_MACOSX) |
427 // TODO(ortuno): Enable on Android once it supports Service Data. | 450 // TODO(ortuno): Enable on Android once it supports Service Data. |
428 // http://crbug.com/639408 | 451 // http://crbug.com/639408 |
429 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}), | 452 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}), |
430 device->GetServiceData()); | 453 device->GetServiceData()); |
431 | |
432 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate)}), | |
433 device->GetServiceDataUUIDs()); | |
434 | |
435 EXPECT_EQ(std::vector<uint8_t>({1}), | |
436 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | |
437 #endif // defined(OS_MACOSX) | 454 #endif // defined(OS_MACOSX) |
455 EXPECT_EQ(ToInt8(TestTxPower::LOWEST), device->GetInquiryTxPower().value()); | |
438 | 456 |
439 // Discover services, should notify of device changed. | 457 // Discover services, should notify of device changed. |
440 // - GetUUIDs: Should return both Advertised UUIDs and Service UUIDs. | 458 // - GetUUIDs: Should return both Advertised UUIDs and Service UUIDs. |
441 // - GetServiceData: Should return same data as before since there was | |
442 // no new advertised data. | |
443 std::vector<std::string> services; | 459 std::vector<std::string> services; |
444 services.push_back(BluetoothUUID(kTestUUIDHeartRate).canonical_value()); | 460 services.push_back(BluetoothUUID(kTestUUIDHeartRate).canonical_value()); |
445 SimulateGattServicesDiscovered(device, services); | 461 SimulateGattServicesDiscovered(device, services); |
446 | 462 |
447 EXPECT_EQ(2, observer.device_changed_count()); | 463 EXPECT_EQ(2, observer.device_changed_count()); |
448 EXPECT_EQ(3u, device->GetUUIDs().size()); | |
449 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | |
450 BluetoothUUID(kTestUUIDGenericAccess))); | |
451 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | |
452 BluetoothUUID(kTestUUIDGenericAttribute))); | |
453 EXPECT_TRUE( | |
454 base::ContainsKey(device->GetUUIDs(), BluetoothUUID(kTestUUIDHeartRate))); | |
455 | 464 |
465 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDGenericAccess), | |
466 BluetoothUUID(kTestUUIDGenericAttribute), | |
467 BluetoothUUID(kTestUUIDHeartRate)}), | |
468 device->GetUUIDs()); | |
469 | |
470 // Receive advertisement again, notify of device changed. | |
471 // - GetInquiryRSSI: Should return last packet's rssi. | |
472 // - GetUUIDs: Should return only new Advertised UUIDs and Service UUIDs. | |
473 // - GetServiceData: Should return last advertised Service Data. | |
474 // - GetInquiryTxPower: Should return the last packet's advertised Tx Power. | |
475 device = SimulateLowEnergyDevice(2); | |
476 | |
477 EXPECT_EQ(3, observer.device_changed_count()); | |
478 EXPECT_EQ(ToInt8(TestRSSI::LOWER), device->GetInquiryRSSI().value()); | |
479 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDLinkLoss), | |
480 BluetoothUUID(kTestUUIDImmediateAlert), | |
481 BluetoothUUID(kTestUUIDHeartRate)}), | |
482 device->GetUUIDs()); | |
456 #if defined(OS_MACOSX) | 483 #if defined(OS_MACOSX) |
457 // TODO(ortuno): Enable on Android once it supports Service Data. | 484 // TODO(ortuno): Enable on Android once it supports Service Data. |
458 // http://crbug.com/639408 | 485 // http://crbug.com/639408 |
459 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}), | 486 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}}, |
487 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}), | |
460 device->GetServiceData()); | 488 device->GetServiceData()); |
461 | |
462 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate)}), | |
463 device->GetServiceDataUUIDs()); | |
464 | |
465 EXPECT_EQ(std::vector<uint8_t>({1}), | |
466 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | |
467 #endif // defined(OS_MACOSX) | 489 #endif // defined(OS_MACOSX) |
468 | 490 EXPECT_EQ(ToInt8(TestTxPower::LOWER), device->GetInquiryTxPower().value()); |
469 // Receive advertisement again, notify of device changed. | |
470 // - GetUUIDs: Should return only new Advertised UUIDs and Service UUIDs. | |
471 // - GetServiceData: Should return last advertised Service Data. | |
472 device = SimulateLowEnergyDevice(2); | |
473 | |
474 EXPECT_EQ(3, observer.device_changed_count()); | |
475 EXPECT_EQ(3u, device->GetUUIDs().size()); | |
476 EXPECT_TRUE( | |
477 base::ContainsKey(device->GetUUIDs(), BluetoothUUID(kTestUUIDLinkLoss))); | |
478 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | |
479 BluetoothUUID(kTestUUIDImmediateAlert))); | |
480 EXPECT_TRUE( | |
481 base::ContainsKey(device->GetUUIDs(), BluetoothUUID(kTestUUIDHeartRate))); | |
482 | 491 |
483 // Stop discovery session, should notify of device changed. | 492 // Stop discovery session, should notify of device changed. |
493 // - GetInquiryRSSI: Should return nullopt because we are no longer | |
494 // discovering. | |
484 // - GetUUIDs: Should only return Service UUIDs. | 495 // - GetUUIDs: Should only return Service UUIDs. |
485 // - GetServiceData: Should return an empty map since we are no longer | 496 // - GetServiceData: Should return an empty map since we are no longer |
486 // discovering. | 497 // discovering. |
498 // - GetInquiryTxPower: Should return nullopt because we are no longer | |
499 // discovering. | |
487 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED), | 500 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED), |
488 GetErrorCallback(Call::NOT_EXPECTED)); | 501 GetErrorCallback(Call::NOT_EXPECTED)); |
489 ASSERT_FALSE(adapter_->IsDiscovering()); | 502 ASSERT_FALSE(adapter_->IsDiscovering()); |
490 ASSERT_FALSE(discovery_sessions_[0]->IsActive()); | 503 ASSERT_FALSE(discovery_sessions_[0]->IsActive()); |
491 | 504 |
492 EXPECT_EQ(4, observer.device_changed_count()); | 505 EXPECT_EQ(4, observer.device_changed_count()); |
493 EXPECT_EQ(1u, device->GetUUIDs().size()); | 506 EXPECT_FALSE(device->GetInquiryRSSI()); |
494 EXPECT_TRUE( | 507 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate)}), device->GetUUIDs()); |
495 base::ContainsKey(device->GetUUIDs(), BluetoothUUID(kTestUUIDHeartRate))); | |
496 | |
497 #if defined(OS_MACOSX) | 508 #if defined(OS_MACOSX) |
498 // TODO(ortuno): Enable on Android once it supports Service Data. | 509 // TODO(ortuno): Enable on Android once it supports Service Data. |
499 // http://crbug.com/639408 | 510 // http://crbug.com/639408 |
500 EXPECT_EQ(ServiceDataMap(), device->GetServiceData()); | 511 EXPECT_EQ(ServiceDataMap(), device->GetServiceData()); |
501 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs()); | |
502 #endif // defined(OS_MACOSX) | 512 #endif // defined(OS_MACOSX) |
513 EXPECT_FALSE(device->GetInquiryTxPower()); | |
503 | 514 |
504 // Disconnect device, should notify of device changed. | 515 // Disconnect device, should notify of device changed. |
505 // - GetUUIDs: Should return no UUIDs. | 516 // - GetUUIDs: Should return no UUIDs. |
506 // - GetServiceData: Should still return an empty map. | |
507 gatt_connections_[0]->Disconnect(); | 517 gatt_connections_[0]->Disconnect(); |
508 SimulateGattDisconnection(device); | 518 SimulateGattDisconnection(device); |
509 ASSERT_FALSE(device->IsGattConnected()); | 519 ASSERT_FALSE(device->IsGattConnected()); |
510 | 520 |
511 EXPECT_EQ(5, observer.device_changed_count()); | 521 EXPECT_EQ(5, observer.device_changed_count()); |
512 EXPECT_EQ(0u, device->GetUUIDs().size()); | |
513 | 522 |
514 #if defined(OS_MACOSX) | 523 EXPECT_TRUE(device->GetUUIDs().empty()); |
515 // TODO(ortuno): Enable on Android once it supports Service Data. | |
516 // http://crbug.com/639408 | |
517 EXPECT_EQ(ServiceDataMap(), device->GetServiceData()); | |
518 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs()); | |
519 #endif // defined(OS_MACOSX) | |
520 } | 524 } |
521 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 525 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
522 | 526 |
523 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 527 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
524 TEST_F(BluetoothTest, AdvertisementData_ConnectionDuringDiscovery) { | 528 TEST_F(BluetoothTest, AdvertisementData_ConnectionDuringDiscovery) { |
525 // Tests that the Advertisement Data is correctly updated when | 529 // Tests that the Advertisement Data is correctly updated when |
526 // the device connects during discovery. | 530 // the device connects during discovery. |
527 if (!PlatformSupportsLowEnergy()) { | 531 if (!PlatformSupportsLowEnergy()) { |
528 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 532 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
529 return; | 533 return; |
530 } | 534 } |
531 | 535 |
532 InitWithFakeAdapter(); | 536 InitWithFakeAdapter(); |
533 TestBluetoothAdapterObserver observer(adapter_); | 537 TestBluetoothAdapterObserver observer(adapter_); |
534 | 538 |
535 // Start discovery session and receive and advertisement. No device changed | 539 // Start discovery session and receive and advertisement. No device changed |
536 // notification because it's a new device. | 540 // notification because it's a new device. |
541 // - GetInquiryRSSI: Should return the packet's rssi. | |
537 // - GetUUIDs: Should return Advertised UUIDs. | 542 // - GetUUIDs: Should return Advertised UUIDs. |
538 // - GetServiceData: Should return advertised Service Data. | 543 // - GetServiceData: Should return advertised Service Data. |
544 // - GetInquiryTxPower: Should return the packet's advertised Tx Power. | |
539 StartLowEnergyDiscoverySession(); | 545 StartLowEnergyDiscoverySession(); |
540 ASSERT_TRUE(adapter_->IsDiscovering()); | 546 ASSERT_TRUE(adapter_->IsDiscovering()); |
541 ASSERT_TRUE(discovery_sessions_[0]->IsActive()); | 547 ASSERT_TRUE(discovery_sessions_[0]->IsActive()); |
542 BluetoothDevice* device = SimulateLowEnergyDevice(1); | 548 BluetoothDevice* device = SimulateLowEnergyDevice(1); |
543 | 549 |
544 EXPECT_EQ(0, observer.device_changed_count()); | 550 EXPECT_EQ(0, observer.device_changed_count()); |
545 EXPECT_EQ(2u, device->GetUUIDs().size()); | 551 EXPECT_EQ(ToInt8(TestRSSI::LOWEST), device->GetInquiryRSSI().value()); |
546 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | 552 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDGenericAccess), |
547 BluetoothUUID(kTestUUIDGenericAccess))); | 553 BluetoothUUID(kTestUUIDGenericAttribute)}), |
548 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | 554 device->GetUUIDs()); |
549 BluetoothUUID(kTestUUIDGenericAttribute))); | |
550 | |
551 #if defined(OS_MACOSX) | 555 #if defined(OS_MACOSX) |
552 // TODO(ortuno): Enable on Android once it supports Service Data. | 556 // TODO(ortuno): Enable on Android once it supports Service Data. |
553 // http://crbug.com/639408 | 557 // http://crbug.com/639408 |
554 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}), | 558 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}), |
555 device->GetServiceData()); | 559 device->GetServiceData()); |
556 | |
557 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate)}), | |
558 device->GetServiceDataUUIDs()); | |
559 | |
560 EXPECT_EQ(std::vector<uint8_t>({1}), | |
561 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | |
562 #endif // defined(OS_MACOSX) | 560 #endif // defined(OS_MACOSX) |
561 EXPECT_EQ(ToInt8(TestTxPower::LOWEST), device->GetInquiryTxPower().value()); | |
563 | 562 |
564 // Connect, should notify of device changed. | 563 // Connect, should notify of device changed. |
565 // - GetUUIDs: Should return Advertised UUIDs even before GATT Discovery. | 564 // - GetUUIDs: Should return Advertised UUIDs even before GATT Discovery. |
566 // - GetServiceData: Should still return the same Service Data. | |
567 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 565 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
568 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 566 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
569 SimulateGattConnection(device); | 567 SimulateGattConnection(device); |
570 ASSERT_TRUE(device->IsConnected()); | 568 ASSERT_TRUE(device->IsConnected()); |
571 | 569 |
572 observer.Reset(); | 570 observer.Reset(); |
573 EXPECT_EQ(2u, device->GetUUIDs().size()); | 571 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDGenericAccess), |
574 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | 572 BluetoothUUID(kTestUUIDGenericAttribute)}), |
575 BluetoothUUID(kTestUUIDGenericAccess))); | 573 device->GetUUIDs()); |
576 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | |
577 BluetoothUUID(kTestUUIDGenericAttribute))); | |
578 | |
579 #if defined(OS_MACOSX) | |
580 // TODO(ortuno): Enable on Android once it supports Service Data. | |
581 // http://crbug.com/639408 | |
582 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}), | |
583 device->GetServiceData()); | |
584 | |
585 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate)}), | |
586 device->GetServiceDataUUIDs()); | |
587 | |
588 EXPECT_EQ(std::vector<uint8_t>({1}), | |
589 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | |
590 #endif // defined(OS_MACOSX) | |
591 | 574 |
592 // Receive Advertisement with new UUIDs, should notify of device changed. | 575 // Receive Advertisement with new UUIDs, should notify of device changed. |
576 // - GetInquiryRSSI: Should return the packet's rssi. | |
593 // - GetUUIDs: Should return new Advertised UUIDs. | 577 // - GetUUIDs: Should return new Advertised UUIDs. |
594 // - GetServiceData: Should return new advertised Service Data. | 578 // - GetServiceData: Should return new advertised Service Data. |
579 // - GetInquiryTxPower: Should return the packet's advertised Tx Power. | |
595 device = SimulateLowEnergyDevice(2); | 580 device = SimulateLowEnergyDevice(2); |
596 | 581 |
597 EXPECT_EQ(1, observer.device_changed_count()); | 582 EXPECT_EQ(1, observer.device_changed_count()); |
598 EXPECT_EQ(2u, device->GetUUIDs().size()); | 583 EXPECT_EQ(ToInt8(TestRSSI::LOWER), device->GetInquiryRSSI().value()); |
599 EXPECT_TRUE( | 584 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDLinkLoss), |
600 base::ContainsKey(device->GetUUIDs(), BluetoothUUID(kTestUUIDLinkLoss))); | 585 BluetoothUUID(kTestUUIDImmediateAlert)}), |
601 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | 586 device->GetUUIDs()); |
602 BluetoothUUID(kTestUUIDImmediateAlert))); | |
603 | |
604 #if defined(OS_MACOSX) | 587 #if defined(OS_MACOSX) |
605 // TODO(ortuno): Enable on Android once it supports Service Data. | 588 // TODO(ortuno): Enable on Android once it supports Service Data. |
606 // http://crbug.com/639408 | 589 // http://crbug.com/639408 |
607 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}}, | 590 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}}, |
608 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}), | 591 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}), |
609 device->GetServiceData()); | 592 device->GetServiceData()); |
593 #endif // defined(OS_MACOSX) | |
594 EXPECT_EQ(ToInt8(TestTxPower::LOWER), device->GetInquiryTxPower().value()); | |
610 | 595 |
611 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate), | 596 // Discover Services, should notify of device changed. |
612 BluetoothUUID(kTestUUIDImmediateAlert)}), | |
613 device->GetServiceDataUUIDs()); | |
614 | |
615 EXPECT_EQ(std::vector<uint8_t>({2}), | |
616 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | |
617 EXPECT_EQ( | |
618 std::vector<uint8_t>({0}), | |
619 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDImmediateAlert))); | |
620 #endif // defined(OS_MACOSX) | |
621 | |
622 // Discover Service, should notify of device changed. | |
623 // - GetUUIDs: Should return Advertised UUIDs and Service UUIDs. | 597 // - GetUUIDs: Should return Advertised UUIDs and Service UUIDs. |
624 // - GetServiceData: Should return same advertised Service Data. | |
625 std::vector<std::string> services; | 598 std::vector<std::string> services; |
626 services.push_back(BluetoothUUID(kTestUUIDHeartRate).canonical_value()); | 599 services.push_back(BluetoothUUID(kTestUUIDHeartRate).canonical_value()); |
627 SimulateGattServicesDiscovered(device, services); | 600 SimulateGattServicesDiscovered(device, services); |
628 | 601 |
629 EXPECT_EQ(2, observer.device_changed_count()); | 602 EXPECT_EQ(2, observer.device_changed_count()); |
630 EXPECT_EQ(3u, device->GetUUIDs().size()); | |
631 EXPECT_TRUE( | |
632 base::ContainsKey(device->GetUUIDs(), BluetoothUUID(kTestUUIDLinkLoss))); | |
633 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | |
634 BluetoothUUID(kTestUUIDImmediateAlert))); | |
635 EXPECT_TRUE( | |
636 base::ContainsKey(device->GetUUIDs(), BluetoothUUID(kTestUUIDHeartRate))); | |
637 | 603 |
638 #if defined(OS_MACOSX) | 604 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDLinkLoss), |
639 // TODO(ortuno): Enable on Android once it supports Service Data. | 605 BluetoothUUID(kTestUUIDImmediateAlert), |
640 // http://crbug.com/639408 | 606 BluetoothUUID(kTestUUIDHeartRate)}), |
641 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}}, | 607 device->GetUUIDs()); |
642 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}), | |
643 device->GetServiceData()); | |
644 | |
645 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate), | |
646 BluetoothUUID(kTestUUIDImmediateAlert)}), | |
647 device->GetServiceDataUUIDs()); | |
648 | |
649 EXPECT_EQ(std::vector<uint8_t>({2}), | |
650 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | |
651 EXPECT_EQ( | |
652 std::vector<uint8_t>({0}), | |
653 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDImmediateAlert))); | |
654 #endif // defined(OS_MACOSX) | |
655 | 608 |
656 // Disconnect, should notify of device changed. | 609 // Disconnect, should notify of device changed. |
610 // - GetInquiryRSSI: Should return last packet's rssi. | |
657 // - GetUUIDs: Should return only Advertised UUIDs. | 611 // - GetUUIDs: Should return only Advertised UUIDs. |
658 // - GetServiceData: Should still return same advertised Service Data. | 612 // - GetServiceData: Should still return same advertised Service Data. |
613 // - GetInquiryTxPower: Should return the last packet's advertised Tx Power. | |
659 gatt_connections_[0]->Disconnect(); | 614 gatt_connections_[0]->Disconnect(); |
660 SimulateGattDisconnection(device); | 615 SimulateGattDisconnection(device); |
661 ASSERT_FALSE(device->IsGattConnected()); | 616 ASSERT_FALSE(device->IsGattConnected()); |
662 | 617 |
663 EXPECT_EQ(3, observer.device_changed_count()); | 618 EXPECT_EQ(3, observer.device_changed_count()); |
664 EXPECT_EQ(2u, device->GetUUIDs().size()); | 619 EXPECT_EQ(ToInt8(TestRSSI::LOWER), device->GetInquiryRSSI().value()); |
665 EXPECT_TRUE( | 620 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDLinkLoss), |
666 base::ContainsKey(device->GetUUIDs(), BluetoothUUID(kTestUUIDLinkLoss))); | 621 BluetoothUUID(kTestUUIDImmediateAlert)}), |
667 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | 622 device->GetUUIDs()); |
668 BluetoothUUID(kTestUUIDImmediateAlert))); | |
669 | 623 |
670 #if defined(OS_MACOSX) | 624 #if defined(OS_MACOSX) |
671 // TODO(ortuno): Enable on Android once it supports Service Data. | 625 // TODO(ortuno): Enable on Android once it supports Service Data. |
672 // http://crbug.com/639408 | 626 // http://crbug.com/639408 |
673 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}}, | 627 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}}, |
674 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}), | 628 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}), |
675 device->GetServiceData()); | 629 device->GetServiceData()); |
676 | |
677 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate), | |
678 BluetoothUUID(kTestUUIDImmediateAlert)}), | |
679 device->GetServiceDataUUIDs()); | |
680 | |
681 EXPECT_EQ(std::vector<uint8_t>({2}), | |
682 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | |
683 EXPECT_EQ( | |
684 std::vector<uint8_t>({0}), | |
685 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDImmediateAlert))); | |
686 #endif // defined(OS_MACOSX) | 630 #endif // defined(OS_MACOSX) |
631 EXPECT_EQ(ToInt8(TestTxPower::LOWER), device->GetInquiryTxPower().value()); | |
687 | 632 |
688 // Receive Advertisement with new UUIDs, should notify of device changed. | 633 // Receive Advertisement with new UUIDs, should notify of device changed. |
634 // - GetInquiryRSSI: Should return last packet's rssi. | |
689 // - GetUUIDs: Should return only new Advertised UUIDs. | 635 // - GetUUIDs: Should return only new Advertised UUIDs. |
690 // - GetServiceData: Should return only new advertised Service Data. | 636 // - GetServiceData: Should return only new advertised Service Data. |
637 // - GetInquiryTxPower: Should return the last packet's advertised Tx Power. | |
691 device = SimulateLowEnergyDevice(1); | 638 device = SimulateLowEnergyDevice(1); |
692 | 639 |
693 EXPECT_EQ(4, observer.device_changed_count()); | 640 EXPECT_EQ(4, observer.device_changed_count()); |
694 EXPECT_EQ(2u, device->GetUUIDs().size()); | 641 EXPECT_EQ(ToInt8(TestRSSI::LOWEST), device->GetInquiryRSSI().value()); |
695 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | 642 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDGenericAccess), |
696 BluetoothUUID(kTestUUIDGenericAccess))); | 643 BluetoothUUID(kTestUUIDGenericAttribute)}), |
697 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(), | 644 device->GetUUIDs()); |
698 BluetoothUUID(kTestUUIDGenericAttribute))); | |
699 | |
700 #if defined(OS_MACOSX) | 645 #if defined(OS_MACOSX) |
701 // TODO(ortuno): Enable on Android once it supports Service Data. | 646 // TODO(ortuno): Enable on Android once it supports Service Data. |
702 // http://crbug.com/639408 | 647 // http://crbug.com/639408 |
703 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}), | 648 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}), |
704 device->GetServiceData()); | 649 device->GetServiceData()); |
705 | |
706 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate)}), | |
707 device->GetServiceDataUUIDs()); | |
708 | |
709 EXPECT_EQ(std::vector<uint8_t>({1}), | |
710 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | |
711 #endif // defined(OS_MACOSX) | 650 #endif // defined(OS_MACOSX) |
651 EXPECT_EQ(ToInt8(TestTxPower::LOWEST), device->GetInquiryTxPower().value()); | |
712 | 652 |
713 // Stop discovery session, should notify of device changed. | 653 // Stop discovery session, should notify of device changed. |
654 // - GetInquiryRSSI: Should return nullopt because we are no longer | |
655 // discovering. | |
714 // - GetUUIDs: Should return no UUIDs. | 656 // - GetUUIDs: Should return no UUIDs. |
715 // - GetServiceData: Should return no UUIDs since we are no longer | 657 // - GetServiceData: Should return no UUIDs since we are no longer |
716 // discovering. | 658 // discovering. |
659 // - GetInquiryTxPower: Should return nullopt because we are no longer | |
660 // discovering. | |
717 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED), | 661 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED), |
718 GetErrorCallback(Call::NOT_EXPECTED)); | 662 GetErrorCallback(Call::NOT_EXPECTED)); |
719 EXPECT_EQ(5, observer.device_changed_count()); | 663 EXPECT_EQ(5, observer.device_changed_count()); |
720 EXPECT_EQ(0u, device->GetUUIDs().size()); | |
721 | 664 |
665 EXPECT_FALSE(device->GetInquiryRSSI()); | |
666 EXPECT_TRUE(device->GetUUIDs().empty()); | |
722 #if defined(OS_MACOSX) | 667 #if defined(OS_MACOSX) |
723 // TODO(ortuno): Enable on Android once it supports Service Data. | 668 // TODO(ortuno): Enable on Android once it supports Service Data. |
724 // http://crbug.com/639408 | 669 // http://crbug.com/639408 |
725 EXPECT_EQ(ServiceDataMap(), device->GetServiceData()); | 670 EXPECT_TRUE(device->GetServiceData().empty()); |
726 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs()); | |
727 EXPECT_EQ(nullptr, | |
728 device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | |
729 #endif // defined(OS_MACOSX) | 671 #endif // defined(OS_MACOSX) |
672 EXPECT_FALSE(device->GetInquiryTxPower()); | |
730 } | 673 } |
731 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 674 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
732 | 675 |
733 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 676 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
734 // GetName for Device with no name. | 677 // GetName for Device with no name. |
735 TEST_F(BluetoothTest, GetName_NullName) { | 678 TEST_F(BluetoothTest, GetName_NullName) { |
736 if (!PlatformSupportsLowEnergy()) { | 679 if (!PlatformSupportsLowEnergy()) { |
737 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 680 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
738 return; | 681 return; |
739 } | 682 } |
(...skipping 639 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1379 | 1322 |
1380 BluetoothDevice* device2 = SimulateLowEnergyDevice(6); | 1323 BluetoothDevice* device2 = SimulateLowEnergyDevice(6); |
1381 EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, device2->GetType()); | 1324 EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, device2->GetType()); |
1382 | 1325 |
1383 BluetoothDevice* device3 = SimulateClassicDevice(); | 1326 BluetoothDevice* device3 = SimulateClassicDevice(); |
1384 EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, device3->GetType()); | 1327 EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, device3->GetType()); |
1385 } | 1328 } |
1386 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 1329 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
1387 | 1330 |
1388 } // namespace device | 1331 } // namespace device |
OLD | NEW |