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

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

Issue 1412023002: bluetooth: mac: Only test Low Energy discovery sessions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@btm-key-strings-
Patch Set: Created 5 years, 2 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 "base/macros.h" 7 #include "base/macros.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 } 65 }
66 66
67 #if defined(OS_ANDROID) || defined(OS_MACOSX) 67 #if defined(OS_ANDROID) || defined(OS_MACOSX)
68 // Verifies basic device properties, e.g. GetAddress, GetName, ... 68 // Verifies basic device properties, e.g. GetAddress, GetName, ...
69 TEST_F(BluetoothTest, LowEnergyDeviceProperties) { 69 TEST_F(BluetoothTest, LowEnergyDeviceProperties) {
70 if (!PlatformSupportsLowEnergy()) { 70 if (!PlatformSupportsLowEnergy()) {
71 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 71 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
72 return; 72 return;
73 } 73 }
74 InitWithFakeAdapter(); 74 InitWithFakeAdapter();
75 StartDiscoverySession(); 75 StartLowEnergyDiscoverySession();
76 BluetoothDevice* device = DiscoverLowEnergyDevice(1); 76 BluetoothDevice* device = DiscoverLowEnergyDevice(1);
77 ASSERT_TRUE(device); 77 ASSERT_TRUE(device);
78 EXPECT_EQ(0x1F00u, device->GetBluetoothClass()); 78 EXPECT_EQ(0x1F00u, device->GetBluetoothClass());
79 EXPECT_EQ(kTestDeviceAddress1, device->GetAddress()); 79 EXPECT_EQ(kTestDeviceAddress1, device->GetAddress());
80 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource()); 80 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
81 EXPECT_EQ(0, device->GetVendorID()); 81 EXPECT_EQ(0, device->GetVendorID());
82 EXPECT_EQ(0, device->GetProductID()); 82 EXPECT_EQ(0, device->GetProductID());
83 EXPECT_EQ(0, device->GetDeviceID()); 83 EXPECT_EQ(0, device->GetDeviceID());
84 EXPECT_EQ(base::UTF8ToUTF16(kTestDeviceName), device->GetName()); 84 EXPECT_EQ(base::UTF8ToUTF16(kTestDeviceName), device->GetName());
85 EXPECT_FALSE(device->IsPaired()); 85 EXPECT_FALSE(device->IsPaired());
86 BluetoothDevice::UUIDList uuids = device->GetUUIDs(); 86 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
87 EXPECT_TRUE(ContainsValue(uuids, BluetoothUUID(kTestUUIDGenericAccess))); 87 EXPECT_TRUE(ContainsValue(uuids, BluetoothUUID(kTestUUIDGenericAccess)));
88 EXPECT_TRUE(ContainsValue(uuids, BluetoothUUID(kTestUUIDGenericAttribute))); 88 EXPECT_TRUE(ContainsValue(uuids, BluetoothUUID(kTestUUIDGenericAttribute)));
89 } 89 }
90 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) 90 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
91 91
92 #if defined(OS_ANDROID) || defined(OS_MACOSX) 92 #if defined(OS_ANDROID) || defined(OS_MACOSX)
93 // Device with no advertised Service UUIDs. 93 // Device with no advertised Service UUIDs.
94 TEST_F(BluetoothTest, LowEnergyDeviceNoUUIDs) { 94 TEST_F(BluetoothTest, LowEnergyDeviceNoUUIDs) {
95 if (!PlatformSupportsLowEnergy()) { 95 if (!PlatformSupportsLowEnergy()) {
96 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 96 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
97 return; 97 return;
98 } 98 }
99 InitWithFakeAdapter(); 99 InitWithFakeAdapter();
100 StartDiscoverySession(); 100 StartLowEnergyDiscoverySession();
101 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 101 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
102 ASSERT_TRUE(device); 102 ASSERT_TRUE(device);
103 BluetoothDevice::UUIDList uuids = device->GetUUIDs(); 103 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
104 EXPECT_EQ(0u, uuids.size()); 104 EXPECT_EQ(0u, uuids.size());
105 } 105 }
106 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) 106 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
107 107
108 // TODO(scheib): Test with a device with no name. http://crbug.com/506415 108 // TODO(scheib): Test with a device with no name. http://crbug.com/506415
109 // BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() will run, which 109 // BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() will run, which
110 // requires string resources to be loaded. For that, something like 110 // requires string resources to be loaded. For that, something like
111 // InitSharedInstance must be run. See unittest files that call that. It will 111 // InitSharedInstance must be run. See unittest files that call that. It will
112 // also require build configuration to generate string resources into a .pak 112 // also require build configuration to generate string resources into a .pak
113 // file. 113 // file.
114 114
115 #if defined(OS_ANDROID) 115 #if defined(OS_ANDROID)
116 // Basic CreateGattConnection test. 116 // Basic CreateGattConnection test.
117 TEST_F(BluetoothTest, CreateGattConnection) { 117 TEST_F(BluetoothTest, CreateGattConnection) {
118 InitWithFakeAdapter(); 118 InitWithFakeAdapter();
119 StartDiscoverySession(); 119 StartLowEnergyDiscoverySession();
120 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 120 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
121 121
122 ResetEventCounts(); 122 ResetEventCounts();
123 device->CreateGattConnection(GetGattConnectionCallback(), 123 device->CreateGattConnection(GetGattConnectionCallback(),
124 GetConnectErrorCallback()); 124 GetConnectErrorCallback());
125 SimulateGattConnection(device); 125 SimulateGattConnection(device);
126 EXPECT_EQ(1, callback_count_); 126 EXPECT_EQ(1, callback_count_);
127 EXPECT_EQ(0, error_callback_count_); 127 EXPECT_EQ(0, error_callback_count_);
128 ASSERT_EQ(1u, gatt_connections_.size()); 128 ASSERT_EQ(1u, gatt_connections_.size());
129 EXPECT_TRUE(device->IsGattConnected()); 129 EXPECT_TRUE(device->IsGattConnected());
130 EXPECT_TRUE(gatt_connections_[0]->IsConnected()); 130 EXPECT_TRUE(gatt_connections_[0]->IsConnected());
131 } 131 }
132 #endif // defined(OS_ANDROID) 132 #endif // defined(OS_ANDROID)
133 133
134 #if defined(OS_ANDROID) 134 #if defined(OS_ANDROID)
135 // Creates BluetoothGattConnection instances and tests that the interface 135 // Creates BluetoothGattConnection instances and tests that the interface
136 // functions even when some Disconnect and the BluetoothDevice is destroyed. 136 // functions even when some Disconnect and the BluetoothDevice is destroyed.
137 TEST_F(BluetoothTest, BluetoothGattConnection) { 137 TEST_F(BluetoothTest, BluetoothGattConnection) {
138 InitWithFakeAdapter(); 138 InitWithFakeAdapter();
139 StartDiscoverySession(); 139 StartLowEnergyDiscoverySession();
140 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 140 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
141 std::string device_address = device->GetAddress(); 141 std::string device_address = device->GetAddress();
142 142
143 // CreateGattConnection 143 // CreateGattConnection
144 ResetEventCounts(); 144 ResetEventCounts();
145 device->CreateGattConnection(GetGattConnectionCallback(), 145 device->CreateGattConnection(GetGattConnectionCallback(),
146 GetConnectErrorCallback()); 146 GetConnectErrorCallback());
147 EXPECT_EQ(1, gatt_connection_attempts_); 147 EXPECT_EQ(1, gatt_connection_attempts_);
148 SimulateGattConnection(device); 148 SimulateGattConnection(device);
149 EXPECT_EQ(1, callback_count_); 149 EXPECT_EQ(1, callback_count_);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 EXPECT_EQ(device_address, gatt_connections_[0]->GetDeviceAddress()); 190 EXPECT_EQ(device_address, gatt_connections_[0]->GetDeviceAddress());
191 EXPECT_EQ(device_address, gatt_connections_[1]->GetDeviceAddress()); 191 EXPECT_EQ(device_address, gatt_connections_[1]->GetDeviceAddress());
192 } 192 }
193 #endif // defined(OS_ANDROID) 193 #endif // defined(OS_ANDROID)
194 194
195 #if defined(OS_ANDROID) 195 #if defined(OS_ANDROID)
196 // Calls CreateGattConnection then simulates multiple connections from platform. 196 // Calls CreateGattConnection then simulates multiple connections from platform.
197 TEST_F(BluetoothTest, 197 TEST_F(BluetoothTest,
198 BluetoothGattConnection_ConnectWithMultipleOSConnections) { 198 BluetoothGattConnection_ConnectWithMultipleOSConnections) {
199 InitWithFakeAdapter(); 199 InitWithFakeAdapter();
200 StartDiscoverySession(); 200 StartLowEnergyDiscoverySession();
201 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 201 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
202 202
203 // CreateGattConnection, & multiple connections from platform only invoke 203 // CreateGattConnection, & multiple connections from platform only invoke
204 // callbacks once: 204 // callbacks once:
205 ResetEventCounts(); 205 ResetEventCounts();
206 device->CreateGattConnection(GetGattConnectionCallback(), 206 device->CreateGattConnection(GetGattConnectionCallback(),
207 GetConnectErrorCallback()); 207 GetConnectErrorCallback());
208 SimulateGattConnection(device); 208 SimulateGattConnection(device);
209 SimulateGattConnection(device); 209 SimulateGattConnection(device);
210 EXPECT_EQ(1, gatt_connection_attempts_); 210 EXPECT_EQ(1, gatt_connection_attempts_);
211 EXPECT_EQ(1, callback_count_); 211 EXPECT_EQ(1, callback_count_);
212 EXPECT_EQ(0, error_callback_count_); 212 EXPECT_EQ(0, error_callback_count_);
213 EXPECT_TRUE(gatt_connections_[0]->IsConnected()); 213 EXPECT_TRUE(gatt_connections_[0]->IsConnected());
214 214
215 // Become disconnected: 215 // Become disconnected:
216 ResetEventCounts(); 216 ResetEventCounts();
217 SimulateGattDisconnection(device); 217 SimulateGattDisconnection(device);
218 EXPECT_EQ(0, callback_count_); 218 EXPECT_EQ(0, callback_count_);
219 EXPECT_EQ(0, error_callback_count_); 219 EXPECT_EQ(0, error_callback_count_);
220 EXPECT_FALSE(gatt_connections_[0]->IsConnected()); 220 EXPECT_FALSE(gatt_connections_[0]->IsConnected());
221 } 221 }
222 #endif // defined(OS_ANDROID) 222 #endif // defined(OS_ANDROID)
223 223
224 #if defined(OS_ANDROID) 224 #if defined(OS_ANDROID)
225 // Calls CreateGattConnection after already connected. 225 // Calls CreateGattConnection after already connected.
226 TEST_F(BluetoothTest, BluetoothGattConnection_AlreadyConnected) { 226 TEST_F(BluetoothTest, BluetoothGattConnection_AlreadyConnected) {
227 InitWithFakeAdapter(); 227 InitWithFakeAdapter();
228 StartDiscoverySession(); 228 StartLowEnergyDiscoverySession();
229 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 229 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
230 230
231 // Be already connected: 231 // Be already connected:
232 device->CreateGattConnection(GetGattConnectionCallback(), 232 device->CreateGattConnection(GetGattConnectionCallback(),
233 GetConnectErrorCallback()); 233 GetConnectErrorCallback());
234 SimulateGattConnection(device); 234 SimulateGattConnection(device);
235 EXPECT_TRUE(gatt_connections_[0]->IsConnected()); 235 EXPECT_TRUE(gatt_connections_[0]->IsConnected());
236 236
237 // Then CreateGattConnection: 237 // Then CreateGattConnection:
238 ResetEventCounts(); 238 ResetEventCounts();
239 device->CreateGattConnection(GetGattConnectionCallback(), 239 device->CreateGattConnection(GetGattConnectionCallback(),
240 GetConnectErrorCallback()); 240 GetConnectErrorCallback());
241 EXPECT_EQ(0, gatt_connection_attempts_); 241 EXPECT_EQ(0, gatt_connection_attempts_);
242 EXPECT_EQ(1, callback_count_); 242 EXPECT_EQ(1, callback_count_);
243 EXPECT_EQ(0, error_callback_count_); 243 EXPECT_EQ(0, error_callback_count_);
244 EXPECT_TRUE(gatt_connections_[1]->IsConnected()); 244 EXPECT_TRUE(gatt_connections_[1]->IsConnected());
245 } 245 }
246 #endif // defined(OS_ANDROID) 246 #endif // defined(OS_ANDROID)
247 247
248 #if defined(OS_ANDROID) 248 #if defined(OS_ANDROID)
249 // Creates BluetoothGattConnection after one exists that has disconnected. 249 // Creates BluetoothGattConnection after one exists that has disconnected.
250 TEST_F(BluetoothTest, 250 TEST_F(BluetoothTest,
251 BluetoothGattConnection_NewConnectionLeavesPreviousDisconnected) { 251 BluetoothGattConnection_NewConnectionLeavesPreviousDisconnected) {
252 InitWithFakeAdapter(); 252 InitWithFakeAdapter();
253 StartDiscoverySession(); 253 StartLowEnergyDiscoverySession();
254 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 254 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
255 255
256 // Create connection: 256 // Create connection:
257 device->CreateGattConnection(GetGattConnectionCallback(), 257 device->CreateGattConnection(GetGattConnectionCallback(),
258 GetConnectErrorCallback()); 258 GetConnectErrorCallback());
259 SimulateGattConnection(device); 259 SimulateGattConnection(device);
260 260
261 // Disconnect connection: 261 // Disconnect connection:
262 gatt_connections_[0]->Disconnect(); 262 gatt_connections_[0]->Disconnect();
263 SimulateGattDisconnection(device); 263 SimulateGattDisconnection(device);
264 264
265 // Create 2nd connection: 265 // Create 2nd connection:
266 device->CreateGattConnection(GetGattConnectionCallback(), 266 device->CreateGattConnection(GetGattConnectionCallback(),
267 GetConnectErrorCallback()); 267 GetConnectErrorCallback());
268 SimulateGattConnection(device); 268 SimulateGattConnection(device);
269 269
270 EXPECT_FALSE(gatt_connections_[0]->IsConnected()) 270 EXPECT_FALSE(gatt_connections_[0]->IsConnected())
271 << "The disconnected connection shouldn't become connected when another " 271 << "The disconnected connection shouldn't become connected when another "
272 "connection is created."; 272 "connection is created.";
273 EXPECT_TRUE(gatt_connections_[1]->IsConnected()); 273 EXPECT_TRUE(gatt_connections_[1]->IsConnected());
274 } 274 }
275 #endif // defined(OS_ANDROID) 275 #endif // defined(OS_ANDROID)
276 276
277 #if defined(OS_ANDROID) 277 #if defined(OS_ANDROID)
278 // Deletes BluetoothGattConnection causing disconnection. 278 // Deletes BluetoothGattConnection causing disconnection.
279 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectWhenObjectsDestroyed) { 279 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectWhenObjectsDestroyed) {
280 InitWithFakeAdapter(); 280 InitWithFakeAdapter();
281 StartDiscoverySession(); 281 StartLowEnergyDiscoverySession();
282 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 282 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
283 283
284 // Create multiple connections and simulate connection complete: 284 // Create multiple connections and simulate connection complete:
285 device->CreateGattConnection(GetGattConnectionCallback(), 285 device->CreateGattConnection(GetGattConnectionCallback(),
286 GetConnectErrorCallback()); 286 GetConnectErrorCallback());
287 device->CreateGattConnection(GetGattConnectionCallback(), 287 device->CreateGattConnection(GetGattConnectionCallback(),
288 GetConnectErrorCallback()); 288 GetConnectErrorCallback());
289 SimulateGattConnection(device); 289 SimulateGattConnection(device);
290 290
291 // Delete all CreateGattConnection objects, observe disconnection: 291 // Delete all CreateGattConnection objects, observe disconnection:
292 ResetEventCounts(); 292 ResetEventCounts();
293 gatt_connections_.clear(); 293 gatt_connections_.clear();
294 EXPECT_EQ(1, gatt_disconnection_attempts_); 294 EXPECT_EQ(1, gatt_disconnection_attempts_);
295 } 295 }
296 #endif // defined(OS_ANDROID) 296 #endif // defined(OS_ANDROID)
297 297
298 #if defined(OS_ANDROID) 298 #if defined(OS_ANDROID)
299 // Starts process of disconnecting and then calls BluetoothGattConnection. 299 // Starts process of disconnecting and then calls BluetoothGattConnection.
300 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectInProgress) { 300 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectInProgress) {
301 InitWithFakeAdapter(); 301 InitWithFakeAdapter();
302 StartDiscoverySession(); 302 StartLowEnergyDiscoverySession();
303 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 303 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
304 304
305 // Create multiple connections and simulate connection complete: 305 // Create multiple connections and simulate connection complete:
306 device->CreateGattConnection(GetGattConnectionCallback(), 306 device->CreateGattConnection(GetGattConnectionCallback(),
307 GetConnectErrorCallback()); 307 GetConnectErrorCallback());
308 device->CreateGattConnection(GetGattConnectionCallback(), 308 device->CreateGattConnection(GetGattConnectionCallback(),
309 GetConnectErrorCallback()); 309 GetConnectErrorCallback());
310 SimulateGattConnection(device); 310 SimulateGattConnection(device);
311 311
312 // Disconnect all CreateGattConnection objects & create a new connection. 312 // Disconnect all CreateGattConnection objects & create a new connection.
(...skipping 20 matching lines...) Expand all
333 for (BluetoothGattConnection* connection : gatt_connections_) 333 for (BluetoothGattConnection* connection : gatt_connections_)
334 EXPECT_FALSE(connection->IsConnected()); 334 EXPECT_FALSE(connection->IsConnected());
335 } 335 }
336 #endif // defined(OS_ANDROID) 336 #endif // defined(OS_ANDROID)
337 337
338 #if defined(OS_ANDROID) 338 #if defined(OS_ANDROID)
339 // Calls CreateGattConnection but receives notice that the device disconnected 339 // Calls CreateGattConnection but receives notice that the device disconnected
340 // before it ever connects. 340 // before it ever connects.
341 TEST_F(BluetoothTest, BluetoothGattConnection_SimulateDisconnect) { 341 TEST_F(BluetoothTest, BluetoothGattConnection_SimulateDisconnect) {
342 InitWithFakeAdapter(); 342 InitWithFakeAdapter();
343 StartDiscoverySession(); 343 StartLowEnergyDiscoverySession();
344 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 344 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
345 345
346 ResetEventCounts(); 346 ResetEventCounts();
347 device->CreateGattConnection(GetGattConnectionCallback(), 347 device->CreateGattConnection(GetGattConnectionCallback(),
348 GetConnectErrorCallback()); 348 GetConnectErrorCallback());
349 EXPECT_EQ(1, gatt_connection_attempts_); 349 EXPECT_EQ(1, gatt_connection_attempts_);
350 SimulateGattDisconnection(device); 350 SimulateGattDisconnection(device);
351 EXPECT_EQ(0, callback_count_); 351 EXPECT_EQ(0, callback_count_);
352 EXPECT_EQ(1, error_callback_count_); 352 EXPECT_EQ(1, error_callback_count_);
353 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_); 353 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_);
354 for (BluetoothGattConnection* connection : gatt_connections_) 354 for (BluetoothGattConnection* connection : gatt_connections_)
355 EXPECT_FALSE(connection->IsConnected()); 355 EXPECT_FALSE(connection->IsConnected());
356 } 356 }
357 #endif // defined(OS_ANDROID) 357 #endif // defined(OS_ANDROID)
358 358
359 #if defined(OS_ANDROID) 359 #if defined(OS_ANDROID)
360 // Calls CreateGattConnection & DisconnectGatt, then simulates connection. 360 // Calls CreateGattConnection & DisconnectGatt, then simulates connection.
361 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectGatt_SimulateConnect) { 361 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectGatt_SimulateConnect) {
362 InitWithFakeAdapter(); 362 InitWithFakeAdapter();
363 StartDiscoverySession(); 363 StartLowEnergyDiscoverySession();
364 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 364 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
365 365
366 ResetEventCounts(); 366 ResetEventCounts();
367 device->CreateGattConnection(GetGattConnectionCallback(), 367 device->CreateGattConnection(GetGattConnectionCallback(),
368 GetConnectErrorCallback()); 368 GetConnectErrorCallback());
369 device->DisconnectGatt(); 369 device->DisconnectGatt();
370 EXPECT_EQ(1, gatt_connection_attempts_); 370 EXPECT_EQ(1, gatt_connection_attempts_);
371 EXPECT_EQ(1, gatt_disconnection_attempts_); 371 EXPECT_EQ(1, gatt_disconnection_attempts_);
372 SimulateGattConnection(device); 372 SimulateGattConnection(device);
373 EXPECT_EQ(1, callback_count_); 373 EXPECT_EQ(1, callback_count_);
374 EXPECT_EQ(0, error_callback_count_); 374 EXPECT_EQ(0, error_callback_count_);
375 EXPECT_TRUE(gatt_connections_.back()->IsConnected()); 375 EXPECT_TRUE(gatt_connections_.back()->IsConnected());
376 ResetEventCounts(); 376 ResetEventCounts();
377 SimulateGattDisconnection(device); 377 SimulateGattDisconnection(device);
378 EXPECT_EQ(0, callback_count_); 378 EXPECT_EQ(0, callback_count_);
379 EXPECT_EQ(0, error_callback_count_); 379 EXPECT_EQ(0, error_callback_count_);
380 } 380 }
381 #endif // defined(OS_ANDROID) 381 #endif // defined(OS_ANDROID)
382 382
383 #if defined(OS_ANDROID) 383 #if defined(OS_ANDROID)
384 // Calls CreateGattConnection & DisconnectGatt, then simulates disconnection. 384 // Calls CreateGattConnection & DisconnectGatt, then simulates disconnection.
385 TEST_F(BluetoothTest, 385 TEST_F(BluetoothTest,
386 BluetoothGattConnection_DisconnectGatt_SimulateDisconnect) { 386 BluetoothGattConnection_DisconnectGatt_SimulateDisconnect) {
387 InitWithFakeAdapter(); 387 InitWithFakeAdapter();
388 StartDiscoverySession(); 388 StartLowEnergyDiscoverySession();
389 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 389 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
390 390
391 ResetEventCounts(); 391 ResetEventCounts();
392 device->CreateGattConnection(GetGattConnectionCallback(), 392 device->CreateGattConnection(GetGattConnectionCallback(),
393 GetConnectErrorCallback()); 393 GetConnectErrorCallback());
394 device->DisconnectGatt(); 394 device->DisconnectGatt();
395 EXPECT_EQ(1, gatt_connection_attempts_); 395 EXPECT_EQ(1, gatt_connection_attempts_);
396 EXPECT_EQ(1, gatt_disconnection_attempts_); 396 EXPECT_EQ(1, gatt_disconnection_attempts_);
397 SimulateGattDisconnection(device); 397 SimulateGattDisconnection(device);
398 EXPECT_EQ(0, callback_count_); 398 EXPECT_EQ(0, callback_count_);
399 EXPECT_EQ(1, error_callback_count_); 399 EXPECT_EQ(1, error_callback_count_);
400 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_); 400 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_);
401 for (BluetoothGattConnection* connection : gatt_connections_) 401 for (BluetoothGattConnection* connection : gatt_connections_)
402 EXPECT_FALSE(connection->IsConnected()); 402 EXPECT_FALSE(connection->IsConnected());
403 } 403 }
404 #endif // defined(OS_ANDROID) 404 #endif // defined(OS_ANDROID)
405 405
406 #if defined(OS_ANDROID) 406 #if defined(OS_ANDROID)
407 // Calls CreateGattConnection, but simulate errors connecting. Also, verifies 407 // Calls CreateGattConnection, but simulate errors connecting. Also, verifies
408 // multiple errors should only invoke callbacks once. 408 // multiple errors should only invoke callbacks once.
409 TEST_F(BluetoothTest, BluetoothGattConnection_ErrorAfterConnection) { 409 TEST_F(BluetoothTest, BluetoothGattConnection_ErrorAfterConnection) {
410 InitWithFakeAdapter(); 410 InitWithFakeAdapter();
411 StartDiscoverySession(); 411 StartLowEnergyDiscoverySession();
412 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 412 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
413 413
414 ResetEventCounts(); 414 ResetEventCounts();
415 device->CreateGattConnection(GetGattConnectionCallback(), 415 device->CreateGattConnection(GetGattConnectionCallback(),
416 GetConnectErrorCallback()); 416 GetConnectErrorCallback());
417 EXPECT_EQ(1, gatt_connection_attempts_); 417 EXPECT_EQ(1, gatt_connection_attempts_);
418 SimulateGattConnectionError(device, BluetoothDevice::ERROR_AUTH_FAILED); 418 SimulateGattConnectionError(device, BluetoothDevice::ERROR_AUTH_FAILED);
419 SimulateGattConnectionError(device, BluetoothDevice::ERROR_FAILED); 419 SimulateGattConnectionError(device, BluetoothDevice::ERROR_FAILED);
420 EXPECT_EQ(0, callback_count_); 420 EXPECT_EQ(0, callback_count_);
421 EXPECT_EQ(1, error_callback_count_); 421 EXPECT_EQ(1, error_callback_count_);
422 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_FAILED, last_connect_error_code_); 422 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_FAILED, last_connect_error_code_);
423 for (BluetoothGattConnection* connection : gatt_connections_) 423 for (BluetoothGattConnection* connection : gatt_connections_)
424 EXPECT_FALSE(connection->IsConnected()); 424 EXPECT_FALSE(connection->IsConnected());
425 } 425 }
426 #endif // defined(OS_ANDROID) 426 #endif // defined(OS_ANDROID)
427 427
428 #if defined(OS_ANDROID) 428 #if defined(OS_ANDROID)
429 TEST_F(BluetoothTest, SimulateGattServicesDiscovered) { 429 TEST_F(BluetoothTest, SimulateGattServicesDiscovered) {
430 InitWithFakeAdapter(); 430 InitWithFakeAdapter();
431 StartDiscoverySession(); 431 StartLowEnergyDiscoverySession();
432 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 432 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
433 device->CreateGattConnection(GetGattConnectionCallback(), 433 device->CreateGattConnection(GetGattConnectionCallback(),
434 GetConnectErrorCallback()); 434 GetConnectErrorCallback());
435 ResetEventCounts(); 435 ResetEventCounts();
436 SimulateGattConnection(device); 436 SimulateGattConnection(device);
437 EXPECT_EQ(1, gatt_discovery_attempts_); 437 EXPECT_EQ(1, gatt_discovery_attempts_);
438 438
439 std::vector<std::string> services; 439 std::vector<std::string> services;
440 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); 440 services.push_back("00000000-0000-1000-8000-00805f9b34fb");
441 // 2 duplicate UUIDs creating 2 instances. 441 // 2 duplicate UUIDs creating 2 instances.
442 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); 442 services.push_back("00000001-0000-1000-8000-00805f9b34fb");
443 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); 443 services.push_back("00000001-0000-1000-8000-00805f9b34fb");
444 SimulateGattServicesDiscovered(device, services); 444 SimulateGattServicesDiscovered(device, services);
445 EXPECT_EQ(3u, device->GetGattServices().size()); 445 EXPECT_EQ(3u, device->GetGattServices().size());
446 } 446 }
447 #endif // defined(OS_ANDROID) 447 #endif // defined(OS_ANDROID)
448 448
449 #if defined(OS_ANDROID) 449 #if defined(OS_ANDROID)
450 TEST_F(BluetoothTest, SimulateGattServicesDiscoveryError) { 450 TEST_F(BluetoothTest, SimulateGattServicesDiscoveryError) {
451 InitWithFakeAdapter(); 451 InitWithFakeAdapter();
452 StartDiscoverySession(); 452 StartLowEnergyDiscoverySession();
453 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 453 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
454 device->CreateGattConnection(GetGattConnectionCallback(), 454 device->CreateGattConnection(GetGattConnectionCallback(),
455 GetConnectErrorCallback()); 455 GetConnectErrorCallback());
456 ResetEventCounts(); 456 ResetEventCounts();
457 SimulateGattConnection(device); 457 SimulateGattConnection(device);
458 EXPECT_EQ(1, gatt_discovery_attempts_); 458 EXPECT_EQ(1, gatt_discovery_attempts_);
459 459
460 SimulateGattServicesDiscoveryError(device); 460 SimulateGattServicesDiscoveryError(device);
461 EXPECT_EQ(0u, device->GetGattServices().size()); 461 EXPECT_EQ(0u, device->GetGattServices().size());
462 } 462 }
463 #endif // defined(OS_ANDROID) 463 #endif // defined(OS_ANDROID)
464 464
465 } // namespace device 465 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698