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

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: Fix arc 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) {
Jeffrey Yasskin 2016/08/19 15:09:53 Nice tests!
ortuno 2016/08/19 20:50:33 Thanks :)
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)));
139 149
140 // Connect. 150 #if defined(OS_MACOSX)
Jeffrey Yasskin 2016/08/19 15:09:53 Comment why this only works for Mac?
ortuno 2016/08/19 20:50:33 Done here and in all instances below.
151 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}),
152 *device->GetServiceData());
153
154 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate)}),
155 device->GetServiceDataUUIDs());
156
157 EXPECT_EQ(std::vector<uint8_t>({1}),
158 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate)));
159 #endif // defined(OS_MACOSX)
160
161 // Receive Advertisement with no UUIDs or Service Data, should notify
162 // device changed.
163 // - GetUUIDs: Should return no UUIDs.
164 // - GetServiceData: Should return empty map.
165 SimulateLowEnergyDevice(3);
166 EXPECT_EQ(1, observer.device_changed_count());
167 EXPECT_EQ(0u, device->GetUUIDs().size());
168
169 #if defined(OS_MACOSX)
170 EXPECT_EQ(ServiceDataMap(), *device->GetServiceData());
171 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs());
172 EXPECT_FALSE(
173 !!device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate)));
Jeffrey Yasskin 2016/08/19 15:09:53 Use ==nullptr instead of !! to check for null.
ortuno 2016/08/19 20:50:33 Done.
174 #endif // defined(OS_MACOSX)
175
176 // Receive Advertisement with different UUIDs and Service Data, should notify
177 // device changed.
178 // - GetUUIDs: Should return latest Advertised UUIDs.
179 // - GetServiceData: Should return last advertised Service Data.
180 SimulateLowEnergyDevice(2);
181 EXPECT_EQ(2, observer.device_changed_count());
182 EXPECT_EQ(2u, device->GetUUIDs().size());
183 EXPECT_FALSE(base::ContainsKey(device->GetUUIDs(),
184 BluetoothUUID(kTestUUIDGenericAccess)));
185 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
186 BluetoothUUID(kTestUUIDImmediateAlert)));
187
188 #if defined(OS_MACOSX)
189 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}},
190 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}),
191 *device->GetServiceData());
192
193 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate),
194 BluetoothUUID(kTestUUIDImmediateAlert)}),
195 device->GetServiceDataUUIDs());
196
197 EXPECT_EQ(std::vector<uint8_t>({2}),
198 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate)));
199 EXPECT_EQ(
200 std::vector<uint8_t>({0}),
201 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDImmediateAlert)));
202 #endif // defined(OS_MACOSX)
203
204 // Stop discovery session, should notify of device changed.
205 // - GetUUIDs: Should not return any UUIDs.
206 // - GetServiceData: Should return empty map.
207 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED),
208 GetErrorCallback(Call::NOT_EXPECTED));
209 ASSERT_FALSE(adapter_->IsDiscovering());
210 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
211
212 EXPECT_EQ(3, observer.device_changed_count());
213 EXPECT_EQ(0u, device->GetUUIDs().size());
214
215 #if defined(OS_MACOSX)
216 EXPECT_EQ(ServiceDataMap(), *device->GetServiceData());
217 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs());
218 EXPECT_FALSE(
219 !!device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate)));
220 EXPECT_FALSE(
221 !!device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDImmediateAlert)));
222 #endif // defined(OS_MACOSX)
223
224 // Discover the device again with different UUIDs, should notify of device
225 // changed.
226 // - GetUUIDs: Should return only the latest Advertised UUIDs.
227 // - GetServiceData: Should return last advertise Service Data.
228 StartLowEnergyDiscoverySession();
229 device = SimulateLowEnergyDevice(1);
230
231 EXPECT_EQ(4, observer.device_changed_count());
232 EXPECT_EQ(2u, device->GetUUIDs().size());
233 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
234 BluetoothUUID(kTestUUIDGenericAccess)));
235 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
236 BluetoothUUID(kTestUUIDGenericAttribute)));
237
238 #if defined(OS_MACOSX)
239 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}),
240 *device->GetServiceData());
241
242 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate)}),
243 device->GetServiceDataUUIDs());
244
245 EXPECT_EQ(std::vector<uint8_t>({1}),
246 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate)));
247 #endif // defined(OS_MACOSX)
248 }
249 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
250
251 #if defined(OS_ANDROID) || defined(OS_MACOSX)
252 // Tests Advertisement Data is updated correctly during a connection.
253 TEST_F(BluetoothTest, GetUUIDs_Connection) {
254 if (!PlatformSupportsLowEnergy()) {
255 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
256 return;
257 }
258
259 InitWithFakeAdapter();
260 TestBluetoothAdapterObserver observer(adapter_);
261
262 StartLowEnergyDiscoverySession();
263 BluetoothDevice* device = SimulateLowEnergyDevice(1);
264 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED),
265 GetErrorCallback(Call::NOT_EXPECTED));
266
267 // Connect to the device.
268 // - GetUUIDs: Should return no UUIDs because Services have not been
269 // discovered.
270 // - GetServiceData: Should return empty map because we are no longer
271 // discovering.
141 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 272 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
142 GetConnectErrorCallback(Call::NOT_EXPECTED)); 273 GetConnectErrorCallback(Call::NOT_EXPECTED));
143 TestBluetoothAdapterObserver observer(adapter_);
144 SimulateGattConnection(device); 274 SimulateGattConnection(device);
145 ASSERT_TRUE(device->IsConnected()); 275 ASSERT_TRUE(device->IsConnected());
146 276
147 // No UUIDs until services are completely discovered. 277 EXPECT_EQ(0u, device->GetUUIDs().size());
148 EXPECT_EQ(0u, device->GetUUIDs().size()); 278 #if defined(OS_MACOSX)
149 279 EXPECT_EQ(ServiceDataMap(), *device->GetServiceData());
150 // Discover services. 280 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs());
281 #endif // defined(OS_MACOSX)
282
283 observer.Reset();
284
285 // Discover services, should notify of device changed.
286 // - GetUUIDs: Should return the device's services' UUIDs.
287 // - GetServiceData: Should return empty map.
151 std::vector<std::string> services; 288 std::vector<std::string> services;
152 services.push_back(BluetoothUUID(kTestUUIDGenericAccess).canonical_value()); 289 services.push_back(BluetoothUUID(kTestUUIDGenericAccess).canonical_value());
153 SimulateGattServicesDiscovered(device, services); 290 SimulateGattServicesDiscovered(device, services);
154 291
155 // GetUUIDs should return UUIDs of services in device. 292 EXPECT_EQ(1, observer.device_changed_count());
156 EXPECT_EQ(1u, device->GetUUIDs().size()); 293 EXPECT_EQ(1u, device->GetUUIDs().size());
157 EXPECT_TRUE(base::ContainsValue(device->GetUUIDs(), 294 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
158 BluetoothUUID(kTestUUIDGenericAccess))); 295 BluetoothUUID(kTestUUIDGenericAccess)));
159 296
160 #if defined(OS_MACOSX) 297 #if defined(OS_MACOSX)
298 EXPECT_EQ(ServiceDataMap(), *device->GetServiceData());
299 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs());
300 #endif // defined(OS_MACOSX)
301
302 #if defined(OS_MACOSX)
303
304 observer.Reset();
305
306 // Notify of services changed, should notify of device changed.
307 // - GetUUIDs: Should return no UUIDs because we no longer know what services
308 // the device has.
309 // - GetServiceData: Should return empty map.
310
161 // Android and Windows don't yet support service changed events. 311 // Android and Windows don't yet support service changed events.
162 // http://crbug.com/548280 312 // http://crbug.com/548280
163 // http://crbug.com/579202 313 // http://crbug.com/579202
164 SimulateGattServicesChanged(device); 314 SimulateGattServicesChanged(device);
165 315
166 // After service changed event we don't know what UUIDs the device has.
167 ASSERT_FALSE(device->IsGattServicesDiscoveryComplete()); 316 ASSERT_FALSE(device->IsGattServicesDiscoveryComplete());
168 EXPECT_EQ(0u, device->GetUUIDs().size()); 317 EXPECT_EQ(1, observer.device_changed_count());
169 318 EXPECT_EQ(0u, device->GetUUIDs().size());
319
320 EXPECT_EQ(ServiceDataMap(), *device->GetServiceData());
321 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs());
322
323 // Services discovered again, should notify of device changed.
324 // - GetUUIDs: Should return Service UUIDs.
325 // - GetServiceData: Should return empty map.
170 SimulateGattServicesDiscovered(device, {} /* services */); 326 SimulateGattServicesDiscovered(device, {} /* services */);
171 327
172 // GetUUIDs should return UUIDs of services in device. 328 EXPECT_EQ(2, observer.device_changed_count());
173 EXPECT_EQ(1u, device->GetUUIDs().size()); 329 EXPECT_EQ(1u, device->GetUUIDs().size());
174 EXPECT_TRUE(base::ContainsValue(device->GetUUIDs(), 330 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
175 BluetoothUUID(kTestUUIDGenericAccess))); 331 BluetoothUUID(kTestUUIDGenericAccess)));
176 #endif // defined(OS_MACOSX) 332
177 333 EXPECT_EQ(ServiceDataMap(), *device->GetServiceData());
178 // Disconnect. 334 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs());
179 device->DisconnectGatt(); 335 #endif // defined(OS_MACOSX)
336
337 observer.Reset();
338
339 // Disconnect, should notify device changed.
340 // - GetUUIDs: Should return no UUIDs since we no longer know what services
341 // the device holds and notify of device changed.
342 // - GetServiceData: Should return empty map.
343 gatt_connections_[0]->Disconnect();
180 SimulateGattDisconnection(device); 344 SimulateGattDisconnection(device);
181 345 ASSERT_FALSE(device->IsGattConnected());
182 // After disconnection we don't know what UUIDs the device has. 346
183 EXPECT_EQ(0u, device->GetUUIDs().size()); 347 EXPECT_EQ(1, observer.device_changed_count());
184 348 EXPECT_EQ(0u, device->GetUUIDs().size());
185 // Discover the device again with different UUIDs. 349
186 device = SimulateLowEnergyDevice(2); 350 #if defined(OS_MACOSX)
187 351 EXPECT_EQ(ServiceDataMap(), *device->GetServiceData());
188 // Check advertised UUIDs. 352 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs());
189 EXPECT_EQ(2u, device->GetUUIDs().size()); 353 #endif // defined(OS_MACOSX)
190 EXPECT_TRUE(base::ContainsValue(device->GetUUIDs(),
191 BluetoothUUID(kTestUUIDImmediateAlert)));
192 EXPECT_TRUE(base::ContainsValue(device->GetUUIDs(),
193 BluetoothUUID(kTestUUIDLinkLoss)));
194 } 354 }
195 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) 355 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
196 356
357 #if defined(OS_ANDROID) || defined(OS_MACOSX)
358 // Tests Advertisement Data is updated correctly when we start discovery
359 // during a connection.
360 TEST_F(BluetoothTest, GetUUIDs_DiscoveryDuringConnection) {
361 if (!PlatformSupportsLowEnergy()) {
362 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
363 return;
364 }
365
366 InitWithFakeAdapter();
367 TestBluetoothAdapterObserver observer(adapter_);
368
369 StartLowEnergyDiscoverySession();
370 BluetoothDevice* device = SimulateLowEnergyDevice(1);
371
372 EXPECT_EQ(2u, device->GetUUIDs().size());
373 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
374 BluetoothUUID(kTestUUIDGenericAccess)));
375 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
376 BluetoothUUID(kTestUUIDGenericAttribute)));
377 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED),
378 GetErrorCallback(Call::NOT_EXPECTED));
379 ASSERT_FALSE(adapter_->IsDiscovering());
380 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
381 ASSERT_EQ(0u, device->GetUUIDs().size());
382 discovery_sessions_.clear();
383
384 // Connect.
385 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
386 GetConnectErrorCallback(Call::NOT_EXPECTED));
387 SimulateGattConnection(device);
388 ASSERT_TRUE(device->IsConnected());
389
390 observer.Reset();
391
392 // Start Discovery and receive advertisement during connection,
393 // should notify of device changed.
394 // - GetUUIDs: Should return only Advertised UUIDs since services haven't
395 // been discovered yet.
396 // - GetServiceData: Should return last advertised Service Data.
397 StartLowEnergyDiscoverySession();
398 ASSERT_TRUE(adapter_->IsDiscovering());
399 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
400 device = SimulateLowEnergyDevice(1);
401
402 EXPECT_EQ(1, observer.device_changed_count());
403 EXPECT_EQ(2u, device->GetUUIDs().size());
404 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
405 BluetoothUUID(kTestUUIDGenericAccess)));
406 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
407 BluetoothUUID(kTestUUIDGenericAttribute)));
408
409 #if defined(OS_MACOSX)
410 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}),
411 *device->GetServiceData());
412
413 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate)}),
414 device->GetServiceDataUUIDs());
415
416 EXPECT_EQ(std::vector<uint8_t>({1}),
417 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate)));
418 #endif // defined(OS_MACOSX)
419
420 // Discover services, should notify of device changed.
421 // - GetUUIDs: Should return both Advertised UUIDs and Service UUIDs.
422 // - GetServiceData: Should return same data as before since there was
423 // no new advertised data.
424 std::vector<std::string> services;
425 services.push_back(BluetoothUUID(kTestUUIDHeartRate).canonical_value());
426 SimulateGattServicesDiscovered(device, services);
427
428 EXPECT_EQ(2, observer.device_changed_count());
429 EXPECT_EQ(3u, device->GetUUIDs().size());
430 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
431 BluetoothUUID(kTestUUIDGenericAccess)));
432 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
433 BluetoothUUID(kTestUUIDGenericAttribute)));
434 EXPECT_TRUE(
435 base::ContainsKey(device->GetUUIDs(), BluetoothUUID(kTestUUIDHeartRate)));
436
437 #if defined(OS_MACOSX)
438 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}),
439 *device->GetServiceData());
440
441 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate)}),
442 device->GetServiceDataUUIDs());
443
444 EXPECT_EQ(std::vector<uint8_t>({1}),
445 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate)));
446 #endif // defined(OS_MACOSX)
447
448 // Receive advertisement again, notify of device changed.
449 // - GetUUIDs: Should return only new Advertised UUIDs and Service UUIDs.
450 // - GetServiceData: Should return last advertised Service Data.
451 device = SimulateLowEnergyDevice(2);
452
453 EXPECT_EQ(3, observer.device_changed_count());
454 EXPECT_EQ(3u, device->GetUUIDs().size());
455 EXPECT_TRUE(
456 base::ContainsKey(device->GetUUIDs(), BluetoothUUID(kTestUUIDLinkLoss)));
457 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
458 BluetoothUUID(kTestUUIDImmediateAlert)));
459 EXPECT_TRUE(
460 base::ContainsKey(device->GetUUIDs(), BluetoothUUID(kTestUUIDHeartRate)));
461
462 // Stop discovery session, should notify of device changed.
463 // - GetUUIDs: Should only return Service UUIDs.
464 // - GetServiceData: Should return an empty map since we are no longer
465 // discovering.
466 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED),
467 GetErrorCallback(Call::NOT_EXPECTED));
468 ASSERT_FALSE(adapter_->IsDiscovering());
469 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
470
471 EXPECT_EQ(4, observer.device_changed_count());
472 EXPECT_EQ(1u, device->GetUUIDs().size());
473 EXPECT_TRUE(
474 base::ContainsKey(device->GetUUIDs(), BluetoothUUID(kTestUUIDHeartRate)));
475
476 #if defined(OS_MACOSX)
477 EXPECT_EQ(ServiceDataMap(), *device->GetServiceData());
478 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs());
479 #endif // defined(OS_MACOSX)
480
481 // Disconnect device, should notify of device changed.
482 // - GetUUIDs: Should return no UUIDs.
483 // - GetServiceData: Should still return an empty map.
484 gatt_connections_[0]->Disconnect();
485 SimulateGattDisconnection(device);
486 ASSERT_FALSE(device->IsGattConnected());
487
488 EXPECT_EQ(5, observer.device_changed_count());
489 EXPECT_EQ(0u, device->GetUUIDs().size());
490
491 #if defined(OS_MACOSX)
492 EXPECT_EQ(ServiceDataMap(), *device->GetServiceData());
493 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs());
494 #endif // defined(OS_MACOSX)
495 }
496 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
497
498 #if defined(OS_ANDROID) || defined(OS_MACOSX)
499 TEST_F(BluetoothTest, AdvertisementData_ConnectionDuringDiscovery) {
500 // Tests that the Advertisement Data is correctly updated when
501 // the device connects during discovery.
502 if (!PlatformSupportsLowEnergy()) {
503 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
504 return;
505 }
506
507 InitWithFakeAdapter();
508 TestBluetoothAdapterObserver observer(adapter_);
509
510 // Start discovery session and receive and advertisement. No device changed
511 // notification because it's a new device.
512 // - GetUUIDs: Should return Advertised UUIDs.
513 // - GetServiceData: Should return advertised Service Data.
514 StartLowEnergyDiscoverySession();
515 ASSERT_TRUE(adapter_->IsDiscovering());
516 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
517 BluetoothDevice* device = SimulateLowEnergyDevice(1);
518
519 EXPECT_EQ(0, observer.device_changed_count());
520 EXPECT_EQ(2u, device->GetUUIDs().size());
521 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
522 BluetoothUUID(kTestUUIDGenericAccess)));
523 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
524 BluetoothUUID(kTestUUIDGenericAttribute)));
525
526 #if defined(OS_MACOSX)
527 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}),
528 *device->GetServiceData());
529
530 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate)}),
531 device->GetServiceDataUUIDs());
532
533 EXPECT_EQ(std::vector<uint8_t>({1}),
534 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate)));
535 #endif // defined(OS_MACOSX)
536
537 // Connect, should notify of device changed.
538 // - GetUUIDs: Should return Advertised UUIDs even before GATT Discovery.
539 // - GetServiceData: Should still return the same Service Data.
540 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
541 GetConnectErrorCallback(Call::NOT_EXPECTED));
542 SimulateGattConnection(device);
543 ASSERT_TRUE(device->IsConnected());
544
545 observer.Reset();
546 EXPECT_EQ(2u, device->GetUUIDs().size());
547 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
548 BluetoothUUID(kTestUUIDGenericAccess)));
549 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
550 BluetoothUUID(kTestUUIDGenericAttribute)));
551
552 #if defined(OS_MACOSX)
553 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}),
554 *device->GetServiceData());
555
556 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate)}),
557 device->GetServiceDataUUIDs());
558
559 EXPECT_EQ(std::vector<uint8_t>({1}),
560 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate)));
561 #endif // defined(OS_MACOSX)
562
563 // Receive Advertisement with new UUIDs, should notify of device changed.
564 // - GetUUIDs: Should return new Advertised UUIDs.
565 // - GetServiceData: Should return new advertised Service Data.
566 device = SimulateLowEnergyDevice(2);
567
568 EXPECT_EQ(1, observer.device_changed_count());
569 EXPECT_EQ(2u, device->GetUUIDs().size());
570 EXPECT_TRUE(
571 base::ContainsKey(device->GetUUIDs(), BluetoothUUID(kTestUUIDLinkLoss)));
572 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
573 BluetoothUUID(kTestUUIDImmediateAlert)));
574
575 #if defined(OS_MACOSX)
576 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}},
577 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}),
578 *device->GetServiceData());
579
580 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate),
581 BluetoothUUID(kTestUUIDImmediateAlert)}),
582 device->GetServiceDataUUIDs());
583
584 EXPECT_EQ(std::vector<uint8_t>({2}),
585 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate)));
586 EXPECT_EQ(
587 std::vector<uint8_t>({0}),
588 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDImmediateAlert)));
589 #endif // defined(OS_MACOSX)
590
591 // Discover Service, should notify of device changed.
592 // - GetUUIDs: Should return Advertised UUIDs and Service UUIDs.
593 // - GetServiceData: Should return same advertised Service Data.
594 std::vector<std::string> services;
595 services.push_back(BluetoothUUID(kTestUUIDHeartRate).canonical_value());
596 SimulateGattServicesDiscovered(device, services);
597
598 EXPECT_EQ(2, observer.device_changed_count());
599 EXPECT_EQ(3u, device->GetUUIDs().size());
600 EXPECT_TRUE(
601 base::ContainsKey(device->GetUUIDs(), BluetoothUUID(kTestUUIDLinkLoss)));
602 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
603 BluetoothUUID(kTestUUIDImmediateAlert)));
604 EXPECT_TRUE(
605 base::ContainsKey(device->GetUUIDs(), BluetoothUUID(kTestUUIDHeartRate)));
606
607 #if defined(OS_MACOSX)
608 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}},
609 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}),
610 *device->GetServiceData());
611
612 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate),
613 BluetoothUUID(kTestUUIDImmediateAlert)}),
614 device->GetServiceDataUUIDs());
615
616 EXPECT_EQ(std::vector<uint8_t>({2}),
617 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate)));
618 EXPECT_EQ(
619 std::vector<uint8_t>({0}),
620 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDImmediateAlert)));
621 #endif // defined(OS_MACOSX)
622
623 // Disconnect, should notify of device changed.
624 // - GetUUIDs: Should return only Advertised UUIDs.
625 // - GetServiceData: Should still return same advertised Service Data.
626 gatt_connections_[0]->Disconnect();
627 SimulateGattDisconnection(device);
628 ASSERT_FALSE(device->IsGattConnected());
629
630 EXPECT_EQ(3, observer.device_changed_count());
631 EXPECT_EQ(2u, device->GetUUIDs().size());
632 EXPECT_TRUE(
633 base::ContainsKey(device->GetUUIDs(), BluetoothUUID(kTestUUIDLinkLoss)));
634 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
635 BluetoothUUID(kTestUUIDImmediateAlert)));
636
637 #if defined(OS_MACOSX)
638 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}},
639 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}),
640 *device->GetServiceData());
641
642 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate),
643 BluetoothUUID(kTestUUIDImmediateAlert)}),
644 device->GetServiceDataUUIDs());
645
646 EXPECT_EQ(std::vector<uint8_t>({2}),
647 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate)));
648 EXPECT_EQ(
649 std::vector<uint8_t>({0}),
650 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDImmediateAlert)));
651 #endif // defined(OS_MACOSX)
652
653 // Receive Advertisement with new UUIDs, should notify of device changed.
654 // - GetUUIDs: Should return only new Advertised UUIDs.
655 // - GetServiceData: Should return only new advertised Service Data.
656 device = SimulateLowEnergyDevice(1);
657
658 EXPECT_EQ(4, observer.device_changed_count());
659 EXPECT_EQ(2u, device->GetUUIDs().size());
660 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
661 BluetoothUUID(kTestUUIDGenericAccess)));
662 EXPECT_TRUE(base::ContainsKey(device->GetUUIDs(),
663 BluetoothUUID(kTestUUIDGenericAttribute)));
664
665 #if defined(OS_MACOSX)
666 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}),
667 *device->GetServiceData());
668
669 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate)}),
670 device->GetServiceDataUUIDs());
671
672 EXPECT_EQ(std::vector<uint8_t>({1}),
673 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate)));
674 #endif // defined(OS_MACOSX)
675
676 // Stop discovery session, should notify of device changed.
677 // - GetUUIDs: Should return no UUIDs.
678 // - GetServiceData: Should return no UUIDs since we are no longer
679 // discovering.
680 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED),
681 GetErrorCallback(Call::NOT_EXPECTED));
682 EXPECT_EQ(5, observer.device_changed_count());
683 EXPECT_EQ(0u, device->GetUUIDs().size());
684
685 #if defined(OS_MACOSX)
686 EXPECT_EQ(ServiceDataMap(), *device->GetServiceData());
687 EXPECT_EQ(UUIDSet(), device->GetServiceDataUUIDs());
688 EXPECT_FALSE(
689 !!device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate)));
690 #endif // defined(OS_MACOSX)
691 }
692 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
693
197 #if defined(OS_ANDROID) || defined(OS_MACOSX) 694 #if defined(OS_ANDROID) || defined(OS_MACOSX)
198 // GetName for Device with no name. 695 // GetName for Device with no name.
199 TEST_F(BluetoothTest, GetName_NullName) { 696 TEST_F(BluetoothTest, GetName_NullName) {
200 if (!PlatformSupportsLowEnergy()) { 697 if (!PlatformSupportsLowEnergy()) {
201 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 698 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
202 return; 699 return;
203 } 700 }
204 InitWithFakeAdapter(); 701 InitWithFakeAdapter();
205 StartLowEnergyDiscoverySession(); 702 StartLowEnergyDiscoverySession();
206 BluetoothDevice* device = SimulateLowEnergyDevice(5); 703 BluetoothDevice* device = SimulateLowEnergyDevice(5);
(...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after
843 1340
844 BluetoothDevice* device2 = SimulateLowEnergyDevice(6); 1341 BluetoothDevice* device2 = SimulateLowEnergyDevice(6);
845 EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, device2->GetType()); 1342 EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, device2->GetType());
846 1343
847 BluetoothDevice* device3 = SimulateClassicDevice(); 1344 BluetoothDevice* device3 = SimulateClassicDevice();
848 EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, device3->GetType()); 1345 EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, device3->GetType());
849 } 1346 }
850 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) 1347 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX)
851 1348
852 } // namespace device 1349 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698