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

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

Issue 2244693002: bluetooth: Refactor how we update based on Advertising Data (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 4 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 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_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/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
12 #include "build/build_config.h" 13 #include "build/build_config.h"
13 #include "device/bluetooth/bluetooth_remote_gatt_service.h" 14 #include "device/bluetooth/bluetooth_remote_gatt_service.h"
14 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" 15 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h"
15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
16 17
17 #if defined(OS_ANDROID) 18 #if defined(OS_ANDROID)
18 #include "device/bluetooth/test/bluetooth_test_android.h" 19 #include "device/bluetooth/test/bluetooth_test_android.h"
19 #elif defined(OS_MACOSX) 20 #elif defined(OS_MACOSX)
20 #include "device/bluetooth/test/bluetooth_test_mac.h" 21 #include "device/bluetooth/test/bluetooth_test_mac.h"
21 #elif defined(OS_WIN) 22 #elif defined(OS_WIN)
22 #include "device/bluetooth/test/bluetooth_test_win.h" 23 #include "device/bluetooth/test/bluetooth_test_win.h"
23 #elif defined(OS_CHROMEOS) || defined(OS_LINUX) 24 #elif defined(OS_CHROMEOS) || defined(OS_LINUX)
24 #include "device/bluetooth/test/bluetooth_test_bluez.h" 25 #include "device/bluetooth/test/bluetooth_test_bluez.h"
25 #endif 26 #endif
26 27
27 namespace device { 28 namespace device {
28 29
30 using UUIDSet = BluetoothDevice::UUIDSet;
31 using ServiceDataMap = BluetoothDevice::ServiceDataMap;
32
29 TEST(BluetoothDeviceTest, CanonicalizeAddressFormat_AcceptsAllValidFormats) { 33 TEST(BluetoothDeviceTest, CanonicalizeAddressFormat_AcceptsAllValidFormats) {
30 // There are three valid separators (':', '-', and none). 34 // There are three valid separators (':', '-', and none).
31 // Case shouldn't matter. 35 // Case shouldn't matter.
32 const char* const kValidFormats[] = { 36 const char* const kValidFormats[] = {
33 "1A:2B:3C:4D:5E:6F", 37 "1A:2B:3C:4D:5E:6F",
34 "1a:2B:3c:4D:5e:6F", 38 "1a:2B:3c:4D:5e:6F",
35 "1a:2b:3c:4d:5e:6f", 39 "1a:2b:3c:4d:5e:6f",
36 "1A-2B-3C-4D-5E-6F", 40 "1A-2B-3C-4D-5E-6F",
37 "1a-2B-3c-4D-5e-6F", 41 "1a-2B-3c-4D-5e-6F",
38 "1a-2b-3c-4d-5e-6f", 42 "1a-2b-3c-4d-5e-6f",
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 #ifndef OS_WIN 92 #ifndef OS_WIN
89 EXPECT_EQ(0x1F00u, device->GetBluetoothClass()); 93 EXPECT_EQ(0x1F00u, device->GetBluetoothClass());
90 #endif 94 #endif
91 EXPECT_EQ(kTestDeviceAddress1, device->GetAddress()); 95 EXPECT_EQ(kTestDeviceAddress1, device->GetAddress());
92 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource()); 96 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
93 EXPECT_EQ(0, device->GetVendorID()); 97 EXPECT_EQ(0, device->GetVendorID());
94 EXPECT_EQ(0, device->GetProductID()); 98 EXPECT_EQ(0, device->GetProductID());
95 EXPECT_EQ(0, device->GetDeviceID()); 99 EXPECT_EQ(0, device->GetDeviceID());
96 EXPECT_EQ(base::UTF8ToUTF16(kTestDeviceName), device->GetNameForDisplay()); 100 EXPECT_EQ(base::UTF8ToUTF16(kTestDeviceName), device->GetNameForDisplay());
97 EXPECT_FALSE(device->IsPaired()); 101 EXPECT_FALSE(device->IsPaired());
98 BluetoothDevice::UUIDList uuids = device->GetUUIDs(); 102 UUIDSet uuids = device->GetUUIDs();
103 EXPECT_TRUE(base::ContainsKey(uuids, BluetoothUUID(kTestUUIDGenericAccess)));
99 EXPECT_TRUE( 104 EXPECT_TRUE(
100 base::ContainsValue(uuids, BluetoothUUID(kTestUUIDGenericAccess))); 105 base::ContainsKey(uuids, BluetoothUUID(kTestUUIDGenericAttribute)));
101 EXPECT_TRUE(
102 base::ContainsValue(uuids, BluetoothUUID(kTestUUIDGenericAttribute)));
103 } 106 }
104 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 107 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
105 108
106 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 109 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
107 // Device with no advertised Service UUIDs. 110 // Device with no advertised Service UUIDs.
108 TEST_F(BluetoothTest, LowEnergyDeviceNoUUIDs) { 111 TEST_F(BluetoothTest, LowEnergyDeviceNoUUIDs) {
109 if (!PlatformSupportsLowEnergy()) { 112 if (!PlatformSupportsLowEnergy()) {
110 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 113 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
111 return; 114 return;
112 } 115 }
113 InitWithFakeAdapter(); 116 InitWithFakeAdapter();
114 StartLowEnergyDiscoverySession(); 117 StartLowEnergyDiscoverySession();
115 BluetoothDevice* device = SimulateLowEnergyDevice(3); 118 BluetoothDevice* device = SimulateLowEnergyDevice(3);
116 ASSERT_TRUE(device); 119 ASSERT_TRUE(device);
117 BluetoothDevice::UUIDList uuids = device->GetUUIDs(); 120 UUIDSet uuids = device->GetUUIDs();
118 EXPECT_EQ(0u, uuids.size()); 121 EXPECT_EQ(0u, uuids.size());
119 } 122 }
120 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 123 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
121 124
122 #if defined(OS_ANDROID) || defined(OS_MACOSX) 125 #if defined(OS_ANDROID) || defined(OS_MACOSX)
123 // Tests GetUUIDs returns the right UUIDs when device connects and disconnects. 126 // Tests that the Advertisement Data fields are correctly updated during
124 TEST_F(BluetoothTest, GetUUIDs) { 127 // discovery.
128 TEST_F(BluetoothTest, AdvertisementData_Discovery) {
125 if (!PlatformSupportsLowEnergy()) { 129 if (!PlatformSupportsLowEnergy()) {
126 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 130 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
127 return; 131 return;
128 } 132 }
129 InitWithFakeAdapter(); 133 InitWithFakeAdapter();
134 TestBluetoothAdapterObserver observer(adapter_);
135
136 // Start Discovery Session and receive Advertisement. No DeviceChanged event
137 // because the device is new.
138 // - GetUUIDs: Should return Advertised UUIDs.
139 // - GetServiceData: Should return advertised Service Data.
130 StartLowEnergyDiscoverySession(); 140 StartLowEnergyDiscoverySession();
131 BluetoothDevice* device = SimulateLowEnergyDevice(1); 141 BluetoothDevice* device = SimulateLowEnergyDevice(1);
132 142
133 // Check advertised UUIDs: 143 EXPECT_EQ(0, observer.device_changed_count());
134 EXPECT_EQ(2u, device->GetUUIDs().size()); 144 EXPECT_EQ(2u, device->GetUUIDs().size());
135 EXPECT_TRUE(base::ContainsValue(device->GetUUIDs(), 145 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
136 BluetoothUUID(kTestUUIDGenericAccess))); 146 BluetoothUUID(kTestUUIDGenericAccess)));
137 EXPECT_TRUE(base::ContainsValue(device->GetUUIDs(), 147 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
138 BluetoothUUID(kTestUUIDGenericAttribute))); 148 BluetoothUUID(kTestUUIDGenericAttribute)));
149
150 #if defined(OS_MACOSX)
151 // TODO(ortuno): Enable on Android once it supports Service Data.
152 // http://crbug.com/639408
153 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}),
154 device->GetServiceData());
155
156 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate)}),
157 device->GetServiceDataUUIDs());
158
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.
166 // - GetServiceData: Should return empty map.
167 SimulateLowEnergyDevice(3);
168 EXPECT_EQ(1, observer.device_changed_count());
169 EXPECT_EQ(0u, device->GetUUIDs().size());
170
171 #if defined(OS_MACOSX)
172 // TODO(ortuno): Enable on Android once it supports Service Data.
173 // http://crbug.com/639408
174 EXPECT_EQ(ServiceDataMap(), device->GetServiceData());
175 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs());
176 EXPECT_EQ(nullptr,
177 device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate)));
178 #endif // defined(OS_MACOSX)
179
180 // Receive Advertisement with different UUIDs and Service Data, should notify
181 // device changed.
182 // - GetUUIDs: Should return latest Advertised UUIDs.
183 // - GetServiceData: Should return last advertised Service Data.
184 SimulateLowEnergyDevice(2);
185 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
192 #if defined(OS_MACOSX)
193 // TODO(ortuno): Enable on Android once it supports Service Data.
194 // http://crbug.com/639408
195 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}},
196 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}),
197 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)
209
210 // Stop discovery session, should notify of device changed.
211 // - GetUUIDs: Should not return any UUIDs.
212 // - GetServiceData: Should return empty map.
213 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED),
214 GetErrorCallback(Call::NOT_EXPECTED));
215 ASSERT_FALSE(adapter_->IsDiscovering());
216 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
217
218 EXPECT_EQ(3, observer.device_changed_count());
219 EXPECT_EQ(0u, device->GetUUIDs().size());
220
221 #if defined(OS_MACOSX)
222 // TODO(ortuno): Enable on Android once it supports Service Data.
223 // http://crbug.com/639408
224 EXPECT_EQ(ServiceDataMap(), device->GetServiceData());
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)
231
232 // Discover the device again with different UUIDs, should notify of device
233 // changed.
234 // - GetUUIDs: Should return only the latest Advertised UUIDs.
235 // - GetServiceData: Should return last advertise Service Data.
236 StartLowEnergyDiscoverySession();
237 device = SimulateLowEnergyDevice(1);
238
239 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
246 #if defined(OS_MACOSX)
247 // TODO(ortuno): Enable on Android once it supports Service Data.
248 // http://crbug.com/639408
249 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}),
250 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)
258 }
259 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
260
261 #if defined(OS_ANDROID) || defined(OS_MACOSX)
262 // Tests Advertisement Data is updated correctly during a connection.
263 TEST_F(BluetoothTest, GetUUIDs_Connection) {
264 if (!PlatformSupportsLowEnergy()) {
265 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
266 return;
267 }
268
269 InitWithFakeAdapter();
270 TestBluetoothAdapterObserver observer(adapter_);
271
272 StartLowEnergyDiscoverySession();
273 BluetoothDevice* device = SimulateLowEnergyDevice(1);
274 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED),
275 GetErrorCallback(Call::NOT_EXPECTED));
276
277 // Connect to the device.
278 // - GetUUIDs: Should return no UUIDs because Services have not been
279 // discovered.
280 // - GetServiceData: Should return empty map because we are no longer
281 // discovering.
282 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
283 GetConnectErrorCallback(Call::NOT_EXPECTED));
284 SimulateGattConnection(device);
285 ASSERT_TRUE(device->IsConnected());
286
287 EXPECT_EQ(0u, device->GetUUIDs().size());
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
295 observer.Reset();
296
297 // Discover services, should notify of device changed.
298 // - GetUUIDs: Should return the device's services' UUIDs.
299 // - GetServiceData: Should return empty map.
300 std::vector<std::string> services;
301 services.push_back(BluetoothUUID(kTestUUIDGenericAccess).canonical_value());
302 SimulateGattServicesDiscovered(device, services);
303
304 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
309 #if defined(OS_MACOSX)
310 // TODO(ortuno): Enable on Android once it supports Service Data.
311 // http://crbug.com/639408
312 EXPECT_EQ(ServiceDataMap(), device->GetServiceData());
313 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs());
314 #endif // defined(OS_MACOSX)
315
316 #if defined(OS_MACOSX)
317 // TODO(ortuno): Enable in Android and Windows.
318 // Android and Windows don't yet support service changed events.
319 // http://crbug.com/548280
320 // http://crbug.com/579202
321
322 observer.Reset();
323
324 // Notify of services changed, should notify of device changed.
325 // - GetUUIDs: Should return no UUIDs because we no longer know what services
326 // the device has.
327 // - GetServiceData: Should return empty map.
328
329 SimulateGattServicesChanged(device);
330
331 ASSERT_FALSE(device->IsGattServicesDiscoveryComplete());
332 EXPECT_EQ(1, observer.device_changed_count());
333 EXPECT_EQ(0u, device->GetUUIDs().size());
334
335 EXPECT_EQ(ServiceDataMap(), device->GetServiceData());
336 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs());
337
338 // Services discovered again, should notify of device changed.
339 // - GetUUIDs: Should return Service UUIDs.
340 // - GetServiceData: Should return empty map.
341 SimulateGattServicesDiscovered(device, {} /* services */);
342
343 EXPECT_EQ(2, observer.device_changed_count());
344 EXPECT_EQ(1u, device->GetUUIDs().size());
345 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
346 BluetoothUUID(kTestUUIDGenericAccess)));
347
348 EXPECT_EQ(ServiceDataMap(), device->GetServiceData());
349 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs());
350 #endif // defined(OS_MACOSX)
351
352 observer.Reset();
353
354 // Disconnect, should notify device changed.
355 // - GetUUIDs: Should return no UUIDs since we no longer know what services
356 // the device holds and notify of device changed.
357 // - GetServiceData: Should return empty map.
358 gatt_connections_[0]->Disconnect();
359 SimulateGattDisconnection(device);
360 ASSERT_FALSE(device->IsGattConnected());
361
362 EXPECT_EQ(1, observer.device_changed_count());
363 EXPECT_EQ(0u, device->GetUUIDs().size());
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 }
372 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
373
374 #if defined(OS_ANDROID) || defined(OS_MACOSX)
375 // Tests Advertisement Data is updated correctly when we start discovery
376 // during a connection.
377 TEST_F(BluetoothTest, GetUUIDs_DiscoveryDuringConnection) {
378 if (!PlatformSupportsLowEnergy()) {
379 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
380 return;
381 }
382
383 InitWithFakeAdapter();
384 TestBluetoothAdapterObserver observer(adapter_);
385
386 StartLowEnergyDiscoverySession();
387 BluetoothDevice* device = SimulateLowEnergyDevice(1);
388
389 EXPECT_EQ(2u, device->GetUUIDs().size());
390 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
391 BluetoothUUID(kTestUUIDGenericAccess)));
392 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
393 BluetoothUUID(kTestUUIDGenericAttribute)));
394 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED),
395 GetErrorCallback(Call::NOT_EXPECTED));
396 ASSERT_FALSE(adapter_->IsDiscovering());
397 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
398 ASSERT_EQ(0u, device->GetUUIDs().size());
399 discovery_sessions_.clear();
139 400
140 // Connect. 401 // Connect.
141 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 402 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
142 GetConnectErrorCallback(Call::NOT_EXPECTED)); 403 GetConnectErrorCallback(Call::NOT_EXPECTED));
143 TestBluetoothAdapterObserver observer(adapter_);
144 SimulateGattConnection(device); 404 SimulateGattConnection(device);
145 ASSERT_TRUE(device->IsConnected()); 405 ASSERT_TRUE(device->IsConnected());
146 406
147 // No UUIDs until services are completely discovered. 407 observer.Reset();
408
409 // Start Discovery and receive advertisement during connection,
410 // should notify of device changed.
411 // - GetUUIDs: Should return only Advertised UUIDs since services haven't
412 // been discovered yet.
413 // - GetServiceData: Should return last advertised Service Data.
414 StartLowEnergyDiscoverySession();
415 ASSERT_TRUE(adapter_->IsDiscovering());
416 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
417 device = SimulateLowEnergyDevice(1);
418
419 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
426 #if defined(OS_MACOSX)
427 // TODO(ortuno): Enable on Android once it supports Service Data.
428 // http://crbug.com/639408
429 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}),
430 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)
438
439 // Discover services, should notify of device changed.
440 // - 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;
444 services.push_back(BluetoothUUID(kTestUUIDHeartRate).canonical_value());
445 SimulateGattServicesDiscovered(device, services);
446
447 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
456 #if defined(OS_MACOSX)
457 // TODO(ortuno): Enable on Android once it supports Service Data.
458 // http://crbug.com/639408
459 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}),
460 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)
468
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
483 // Stop discovery session, should notify of device changed.
484 // - GetUUIDs: Should only return Service UUIDs.
485 // - GetServiceData: Should return an empty map since we are no longer
486 // discovering.
487 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED),
488 GetErrorCallback(Call::NOT_EXPECTED));
489 ASSERT_FALSE(adapter_->IsDiscovering());
490 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
491
492 EXPECT_EQ(4, observer.device_changed_count());
493 EXPECT_EQ(1u, device->GetUUIDs().size());
494 EXPECT_TRUE(
495 base::ContainsKey(device->GetUUIDs(), BluetoothUUID(kTestUUIDHeartRate)));
496
497 #if defined(OS_MACOSX)
498 // TODO(ortuno): Enable on Android once it supports Service Data.
499 // http://crbug.com/639408
500 EXPECT_EQ(ServiceDataMap(), device->GetServiceData());
501 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs());
502 #endif // defined(OS_MACOSX)
503
504 // Disconnect device, should notify of device changed.
505 // - GetUUIDs: Should return no UUIDs.
506 // - GetServiceData: Should still return an empty map.
507 gatt_connections_[0]->Disconnect();
508 SimulateGattDisconnection(device);
509 ASSERT_FALSE(device->IsGattConnected());
510
511 EXPECT_EQ(5, observer.device_changed_count());
148 EXPECT_EQ(0u, device->GetUUIDs().size()); 512 EXPECT_EQ(0u, device->GetUUIDs().size());
149 513
150 // Discover services. 514 #if defined(OS_MACOSX)
151 std::vector<std::string> services; 515 // TODO(ortuno): Enable on Android once it supports Service Data.
152 services.push_back(BluetoothUUID(kTestUUIDGenericAccess).canonical_value()); 516 // http://crbug.com/639408
153 SimulateGattServicesDiscovered(device, services); 517 EXPECT_EQ(ServiceDataMap(), device->GetServiceData());
154 518 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs());
155 // GetUUIDs should return UUIDs of services in device. 519 #endif // defined(OS_MACOSX)
156 EXPECT_EQ(1u, device->GetUUIDs().size());
157 EXPECT_TRUE(base::ContainsValue(device->GetUUIDs(),
158 BluetoothUUID(kTestUUIDGenericAccess)));
159
160 #if defined(OS_MACOSX)
161 // Android and Windows don't yet support service changed events.
162 // http://crbug.com/548280
163 // http://crbug.com/579202
164 SimulateGattServicesChanged(device);
165
166 // After service changed event we don't know what UUIDs the device has.
167 ASSERT_FALSE(device->IsGattServicesDiscoveryComplete());
168 EXPECT_EQ(0u, device->GetUUIDs().size());
169
170 SimulateGattServicesDiscovered(device, {} /* services */);
171
172 // GetUUIDs should return UUIDs of services in device.
173 EXPECT_EQ(1u, device->GetUUIDs().size());
174 EXPECT_TRUE(base::ContainsValue(device->GetUUIDs(),
175 BluetoothUUID(kTestUUIDGenericAccess)));
176 #endif // defined(OS_MACOSX)
177
178 // Disconnect.
179 device->DisconnectGatt();
180 SimulateGattDisconnection(device);
181
182 // After disconnection we don't know what UUIDs the device has.
183 EXPECT_EQ(0u, device->GetUUIDs().size());
184
185 // Discover the device again with different UUIDs.
186 device = SimulateLowEnergyDevice(2);
187
188 // Check advertised UUIDs.
189 EXPECT_EQ(2u, device->GetUUIDs().size());
190 EXPECT_TRUE(base::ContainsValue(device->GetUUIDs(),
191 BluetoothUUID(kTestUUIDImmediateAlert)));
192 EXPECT_TRUE(base::ContainsValue(device->GetUUIDs(),
193 BluetoothUUID(kTestUUIDLinkLoss)));
194 } 520 }
195 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) 521 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
196 522
523 #if defined(OS_ANDROID) || defined(OS_MACOSX)
524 TEST_F(BluetoothTest, AdvertisementData_ConnectionDuringDiscovery) {
525 // Tests that the Advertisement Data is correctly updated when
526 // the device connects during discovery.
527 if (!PlatformSupportsLowEnergy()) {
528 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
529 return;
530 }
531
532 InitWithFakeAdapter();
533 TestBluetoothAdapterObserver observer(adapter_);
534
535 // Start discovery session and receive and advertisement. No device changed
536 // notification because it's a new device.
537 // - GetUUIDs: Should return Advertised UUIDs.
538 // - GetServiceData: Should return advertised Service Data.
539 StartLowEnergyDiscoverySession();
540 ASSERT_TRUE(adapter_->IsDiscovering());
541 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
542 BluetoothDevice* device = SimulateLowEnergyDevice(1);
543
544 EXPECT_EQ(0, observer.device_changed_count());
545 EXPECT_EQ(2u, device->GetUUIDs().size());
546 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
547 BluetoothUUID(kTestUUIDGenericAccess)));
548 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
549 BluetoothUUID(kTestUUIDGenericAttribute)));
550
551 #if defined(OS_MACOSX)
552 // TODO(ortuno): Enable on Android once it supports Service Data.
553 // http://crbug.com/639408
554 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}),
555 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)
563
564 // Connect, should notify of device changed.
565 // - GetUUIDs: Should return Advertised UUIDs even before GATT Discovery.
566 // - GetServiceData: Should still return the same Service Data.
567 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
568 GetConnectErrorCallback(Call::NOT_EXPECTED));
569 SimulateGattConnection(device);
570 ASSERT_TRUE(device->IsConnected());
571
572 observer.Reset();
573 EXPECT_EQ(2u, device->GetUUIDs().size());
574 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
575 BluetoothUUID(kTestUUIDGenericAccess)));
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
592 // Receive Advertisement with new UUIDs, should notify of device changed.
593 // - GetUUIDs: Should return new Advertised UUIDs.
594 // - GetServiceData: Should return new advertised Service Data.
595 device = SimulateLowEnergyDevice(2);
596
597 EXPECT_EQ(1, observer.device_changed_count());
598 EXPECT_EQ(2u, device->GetUUIDs().size());
599 EXPECT_TRUE(
600 base::ContainsKey(device->GetUUIDs(), BluetoothUUID(kTestUUIDLinkLoss)));
601 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
602 BluetoothUUID(kTestUUIDImmediateAlert)));
603
604 #if defined(OS_MACOSX)
605 // TODO(ortuno): Enable on Android once it supports Service Data.
606 // http://crbug.com/639408
607 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}},
608 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}),
609 device->GetServiceData());
610
611 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate),
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.
624 // - GetServiceData: Should return same advertised Service Data.
625 std::vector<std::string> services;
626 services.push_back(BluetoothUUID(kTestUUIDHeartRate).canonical_value());
627 SimulateGattServicesDiscovered(device, services);
628
629 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
638 #if defined(OS_MACOSX)
639 // TODO(ortuno): Enable on Android once it supports Service Data.
640 // http://crbug.com/639408
641 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}},
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
656 // Disconnect, should notify of device changed.
657 // - GetUUIDs: Should return only Advertised UUIDs.
658 // - GetServiceData: Should still return same advertised Service Data.
659 gatt_connections_[0]->Disconnect();
660 SimulateGattDisconnection(device);
661 ASSERT_FALSE(device->IsGattConnected());
662
663 EXPECT_EQ(3, observer.device_changed_count());
664 EXPECT_EQ(2u, device->GetUUIDs().size());
665 EXPECT_TRUE(
666 base::ContainsKey(device->GetUUIDs(), BluetoothUUID(kTestUUIDLinkLoss)));
667 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
668 BluetoothUUID(kTestUUIDImmediateAlert)));
669
670 #if defined(OS_MACOSX)
671 // TODO(ortuno): Enable on Android once it supports Service Data.
672 // http://crbug.com/639408
673 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}},
674 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}),
675 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)
687
688 // Receive Advertisement with new UUIDs, should notify of device changed.
689 // - GetUUIDs: Should return only new Advertised UUIDs.
690 // - GetServiceData: Should return only new advertised Service Data.
691 device = SimulateLowEnergyDevice(1);
692
693 EXPECT_EQ(4, observer.device_changed_count());
694 EXPECT_EQ(2u, device->GetUUIDs().size());
695 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
696 BluetoothUUID(kTestUUIDGenericAccess)));
697 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
698 BluetoothUUID(kTestUUIDGenericAttribute)));
699
700 #if defined(OS_MACOSX)
701 // TODO(ortuno): Enable on Android once it supports Service Data.
702 // http://crbug.com/639408
703 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}),
704 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)
712
713 // Stop discovery session, should notify of device changed.
714 // - GetUUIDs: Should return no UUIDs.
715 // - GetServiceData: Should return no UUIDs since we are no longer
716 // discovering.
717 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED),
718 GetErrorCallback(Call::NOT_EXPECTED));
719 EXPECT_EQ(5, observer.device_changed_count());
720 EXPECT_EQ(0u, device->GetUUIDs().size());
721
722 #if defined(OS_MACOSX)
723 // TODO(ortuno): Enable on Android once it supports Service Data.
724 // http://crbug.com/639408
725 EXPECT_EQ(ServiceDataMap(), device->GetServiceData());
726 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs());
727 EXPECT_EQ(nullptr,
728 device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate)));
729 #endif // defined(OS_MACOSX)
730 }
731 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
732
197 #if defined(OS_ANDROID) || defined(OS_MACOSX) 733 #if defined(OS_ANDROID) || defined(OS_MACOSX)
198 // GetName for Device with no name. 734 // GetName for Device with no name.
199 TEST_F(BluetoothTest, GetName_NullName) { 735 TEST_F(BluetoothTest, GetName_NullName) {
200 if (!PlatformSupportsLowEnergy()) { 736 if (!PlatformSupportsLowEnergy()) {
201 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 737 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
202 return; 738 return;
203 } 739 }
204 InitWithFakeAdapter(); 740 InitWithFakeAdapter();
205 StartLowEnergyDiscoverySession(); 741 StartLowEnergyDiscoverySession();
206 BluetoothDevice* device = SimulateLowEnergyDevice(5); 742 BluetoothDevice* device = SimulateLowEnergyDevice(5);
(...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after
843 1379
844 BluetoothDevice* device2 = SimulateLowEnergyDevice(6); 1380 BluetoothDevice* device2 = SimulateLowEnergyDevice(6);
845 EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, device2->GetType()); 1381 EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, device2->GetType());
846 1382
847 BluetoothDevice* device3 = SimulateClassicDevice(); 1383 BluetoothDevice* device3 = SimulateClassicDevice();
848 EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, device3->GetType()); 1384 EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, device3->GetType());
849 } 1385 }
850 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) 1386 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX)
851 1387
852 } // namespace device 1388 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698