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

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

Issue 1465863003: bluetooth: Fix crash when trying to read or write when operation pending (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@my-origin
Patch Set: Add override Created 5 years, 1 month 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 "device/bluetooth/bluetooth_gatt_service.h" 10 #include "device/bluetooth/bluetooth_gatt_service.h"
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 // file. 115 // file.
116 116
117 #if defined(OS_ANDROID) 117 #if defined(OS_ANDROID)
118 // Basic CreateGattConnection test. 118 // Basic CreateGattConnection test.
119 TEST_F(BluetoothTest, CreateGattConnection) { 119 TEST_F(BluetoothTest, CreateGattConnection) {
120 InitWithFakeAdapter(); 120 InitWithFakeAdapter();
121 StartLowEnergyDiscoverySession(); 121 StartLowEnergyDiscoverySession();
122 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 122 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
123 123
124 ResetEventCounts(); 124 ResetEventCounts();
125 device->CreateGattConnection(GetGattConnectionCallback(), 125 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
126 GetConnectErrorCallback()); 126 GetConnectErrorCallback(Call::NOT_EXPECTED));
127 SimulateGattConnection(device); 127 SimulateGattConnection(device);
128 EXPECT_EQ(1, callback_count_); 128 EXPECT_EQ(1, callback_count_);
129 EXPECT_EQ(0, error_callback_count_); 129 EXPECT_EQ(0, error_callback_count_);
130 ASSERT_EQ(1u, gatt_connections_.size()); 130 ASSERT_EQ(1u, gatt_connections_.size());
131 EXPECT_TRUE(device->IsGattConnected()); 131 EXPECT_TRUE(device->IsGattConnected());
132 EXPECT_TRUE(gatt_connections_[0]->IsConnected()); 132 EXPECT_TRUE(gatt_connections_[0]->IsConnected());
133 } 133 }
134 #endif // defined(OS_ANDROID) 134 #endif // defined(OS_ANDROID)
135 135
136 #if defined(OS_ANDROID) 136 #if defined(OS_ANDROID)
137 // Creates BluetoothGattConnection instances and tests that the interface 137 // Creates BluetoothGattConnection instances and tests that the interface
138 // functions even when some Disconnect and the BluetoothDevice is destroyed. 138 // functions even when some Disconnect and the BluetoothDevice is destroyed.
139 TEST_F(BluetoothTest, BluetoothGattConnection) { 139 TEST_F(BluetoothTest, BluetoothGattConnection) {
140 InitWithFakeAdapter(); 140 InitWithFakeAdapter();
141 StartLowEnergyDiscoverySession(); 141 StartLowEnergyDiscoverySession();
142 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 142 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
143 std::string device_address = device->GetAddress(); 143 std::string device_address = device->GetAddress();
144 144
145 // CreateGattConnection 145 // CreateGattConnection
146 ResetEventCounts(); 146 ResetEventCounts();
147 device->CreateGattConnection(GetGattConnectionCallback(), 147 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
148 GetConnectErrorCallback()); 148 GetConnectErrorCallback(Call::NOT_EXPECTED));
149 EXPECT_EQ(1, gatt_connection_attempts_); 149 EXPECT_EQ(1, gatt_connection_attempts_);
150 SimulateGattConnection(device); 150 SimulateGattConnection(device);
151 EXPECT_EQ(1, callback_count_); 151 EXPECT_EQ(1, callback_count_);
152 EXPECT_EQ(0, error_callback_count_); 152 EXPECT_EQ(0, error_callback_count_);
153 ASSERT_EQ(1u, gatt_connections_.size()); 153 ASSERT_EQ(1u, gatt_connections_.size());
154 EXPECT_TRUE(device->IsGattConnected()); 154 EXPECT_TRUE(device->IsGattConnected());
155 EXPECT_TRUE(gatt_connections_[0]->IsConnected()); 155 EXPECT_TRUE(gatt_connections_[0]->IsConnected());
156 156
157 // Connect again once already connected. 157 // Connect again once already connected.
158 ResetEventCounts(); 158 ResetEventCounts();
159 device->CreateGattConnection(GetGattConnectionCallback(), 159 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
160 GetConnectErrorCallback()); 160 GetConnectErrorCallback(Call::NOT_EXPECTED));
161 device->CreateGattConnection(GetGattConnectionCallback(), 161 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
162 GetConnectErrorCallback()); 162 GetConnectErrorCallback(Call::NOT_EXPECTED));
163 EXPECT_EQ(0, gatt_connection_attempts_); 163 EXPECT_EQ(0, gatt_connection_attempts_);
164 EXPECT_EQ(2, callback_count_); 164 EXPECT_EQ(2, callback_count_);
165 EXPECT_EQ(0, error_callback_count_); 165 EXPECT_EQ(0, error_callback_count_);
166 ASSERT_EQ(3u, gatt_connections_.size()); 166 ASSERT_EQ(3u, gatt_connections_.size());
167 167
168 // Test GetDeviceAddress 168 // Test GetDeviceAddress
169 EXPECT_EQ(device_address, gatt_connections_[0]->GetDeviceAddress()); 169 EXPECT_EQ(device_address, gatt_connections_[0]->GetDeviceAddress());
170 170
171 // Test IsConnected 171 // Test IsConnected
172 EXPECT_TRUE(gatt_connections_[0]->IsConnected()); 172 EXPECT_TRUE(gatt_connections_[0]->IsConnected());
(...skipping 25 matching lines...) Expand all
198 // Calls CreateGattConnection then simulates multiple connections from platform. 198 // Calls CreateGattConnection then simulates multiple connections from platform.
199 TEST_F(BluetoothTest, 199 TEST_F(BluetoothTest,
200 BluetoothGattConnection_ConnectWithMultipleOSConnections) { 200 BluetoothGattConnection_ConnectWithMultipleOSConnections) {
201 InitWithFakeAdapter(); 201 InitWithFakeAdapter();
202 StartLowEnergyDiscoverySession(); 202 StartLowEnergyDiscoverySession();
203 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 203 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
204 204
205 // CreateGattConnection, & multiple connections from platform only invoke 205 // CreateGattConnection, & multiple connections from platform only invoke
206 // callbacks once: 206 // callbacks once:
207 ResetEventCounts(); 207 ResetEventCounts();
208 device->CreateGattConnection(GetGattConnectionCallback(), 208 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
209 GetConnectErrorCallback()); 209 GetConnectErrorCallback(Call::NOT_EXPECTED));
210 SimulateGattConnection(device); 210 SimulateGattConnection(device);
211 SimulateGattConnection(device); 211 SimulateGattConnection(device);
212 EXPECT_EQ(1, gatt_connection_attempts_); 212 EXPECT_EQ(1, gatt_connection_attempts_);
213 EXPECT_EQ(1, callback_count_); 213 EXPECT_EQ(1, callback_count_);
214 EXPECT_EQ(0, error_callback_count_); 214 EXPECT_EQ(0, error_callback_count_);
215 EXPECT_TRUE(gatt_connections_[0]->IsConnected()); 215 EXPECT_TRUE(gatt_connections_[0]->IsConnected());
216 216
217 // Become disconnected: 217 // Become disconnected:
218 ResetEventCounts(); 218 ResetEventCounts();
219 SimulateGattDisconnection(device); 219 SimulateGattDisconnection(device);
220 EXPECT_EQ(0, callback_count_); 220 EXPECT_EQ(0, callback_count_);
221 EXPECT_EQ(0, error_callback_count_); 221 EXPECT_EQ(0, error_callback_count_);
222 EXPECT_FALSE(gatt_connections_[0]->IsConnected()); 222 EXPECT_FALSE(gatt_connections_[0]->IsConnected());
223 } 223 }
224 #endif // defined(OS_ANDROID) 224 #endif // defined(OS_ANDROID)
225 225
226 #if defined(OS_ANDROID) 226 #if defined(OS_ANDROID)
227 // Calls CreateGattConnection after already connected. 227 // Calls CreateGattConnection after already connected.
228 TEST_F(BluetoothTest, BluetoothGattConnection_AlreadyConnected) { 228 TEST_F(BluetoothTest, BluetoothGattConnection_AlreadyConnected) {
229 InitWithFakeAdapter(); 229 InitWithFakeAdapter();
230 StartLowEnergyDiscoverySession(); 230 StartLowEnergyDiscoverySession();
231 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 231 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
232 232
233 // Be already connected: 233 // Be already connected:
234 device->CreateGattConnection(GetGattConnectionCallback(), 234 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
235 GetConnectErrorCallback()); 235 GetConnectErrorCallback(Call::NOT_EXPECTED));
236 SimulateGattConnection(device); 236 SimulateGattConnection(device);
237 EXPECT_TRUE(gatt_connections_[0]->IsConnected()); 237 EXPECT_TRUE(gatt_connections_[0]->IsConnected());
238 238
239 // Then CreateGattConnection: 239 // Then CreateGattConnection:
240 ResetEventCounts(); 240 ResetEventCounts();
241 device->CreateGattConnection(GetGattConnectionCallback(), 241 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
242 GetConnectErrorCallback()); 242 GetConnectErrorCallback(Call::NOT_EXPECTED));
243 EXPECT_EQ(0, gatt_connection_attempts_); 243 EXPECT_EQ(0, gatt_connection_attempts_);
244 EXPECT_EQ(1, callback_count_); 244 EXPECT_EQ(1, callback_count_);
245 EXPECT_EQ(0, error_callback_count_); 245 EXPECT_EQ(0, error_callback_count_);
246 EXPECT_TRUE(gatt_connections_[1]->IsConnected()); 246 EXPECT_TRUE(gatt_connections_[1]->IsConnected());
247 } 247 }
248 #endif // defined(OS_ANDROID) 248 #endif // defined(OS_ANDROID)
249 249
250 #if defined(OS_ANDROID) 250 #if defined(OS_ANDROID)
251 // Creates BluetoothGattConnection after one exists that has disconnected. 251 // Creates BluetoothGattConnection after one exists that has disconnected.
252 TEST_F(BluetoothTest, 252 TEST_F(BluetoothTest,
253 BluetoothGattConnection_NewConnectionLeavesPreviousDisconnected) { 253 BluetoothGattConnection_NewConnectionLeavesPreviousDisconnected) {
254 InitWithFakeAdapter(); 254 InitWithFakeAdapter();
255 StartLowEnergyDiscoverySession(); 255 StartLowEnergyDiscoverySession();
256 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 256 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
257 257
258 // Create connection: 258 // Create connection:
259 device->CreateGattConnection(GetGattConnectionCallback(), 259 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
260 GetConnectErrorCallback()); 260 GetConnectErrorCallback(Call::NOT_EXPECTED));
261 SimulateGattConnection(device); 261 SimulateGattConnection(device);
262 262
263 // Disconnect connection: 263 // Disconnect connection:
264 gatt_connections_[0]->Disconnect(); 264 gatt_connections_[0]->Disconnect();
265 SimulateGattDisconnection(device); 265 SimulateGattDisconnection(device);
266 266
267 // Create 2nd connection: 267 // Create 2nd connection:
268 device->CreateGattConnection(GetGattConnectionCallback(), 268 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
269 GetConnectErrorCallback()); 269 GetConnectErrorCallback(Call::NOT_EXPECTED));
270 SimulateGattConnection(device); 270 SimulateGattConnection(device);
271 271
272 EXPECT_FALSE(gatt_connections_[0]->IsConnected()) 272 EXPECT_FALSE(gatt_connections_[0]->IsConnected())
273 << "The disconnected connection shouldn't become connected when another " 273 << "The disconnected connection shouldn't become connected when another "
274 "connection is created."; 274 "connection is created.";
275 EXPECT_TRUE(gatt_connections_[1]->IsConnected()); 275 EXPECT_TRUE(gatt_connections_[1]->IsConnected());
276 } 276 }
277 #endif // defined(OS_ANDROID) 277 #endif // defined(OS_ANDROID)
278 278
279 #if defined(OS_ANDROID) 279 #if defined(OS_ANDROID)
280 // Deletes BluetoothGattConnection causing disconnection. 280 // Deletes BluetoothGattConnection causing disconnection.
281 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectWhenObjectsDestroyed) { 281 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectWhenObjectsDestroyed) {
282 InitWithFakeAdapter(); 282 InitWithFakeAdapter();
283 StartLowEnergyDiscoverySession(); 283 StartLowEnergyDiscoverySession();
284 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 284 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
285 285
286 // Create multiple connections and simulate connection complete: 286 // Create multiple connections and simulate connection complete:
287 device->CreateGattConnection(GetGattConnectionCallback(), 287 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
288 GetConnectErrorCallback()); 288 GetConnectErrorCallback(Call::NOT_EXPECTED));
289 device->CreateGattConnection(GetGattConnectionCallback(), 289 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
290 GetConnectErrorCallback()); 290 GetConnectErrorCallback(Call::NOT_EXPECTED));
291 SimulateGattConnection(device); 291 SimulateGattConnection(device);
292 292
293 // Delete all CreateGattConnection objects, observe disconnection: 293 // Delete all CreateGattConnection objects, observe disconnection:
294 ResetEventCounts(); 294 ResetEventCounts();
295 gatt_connections_.clear(); 295 gatt_connections_.clear();
296 EXPECT_EQ(1, gatt_disconnection_attempts_); 296 EXPECT_EQ(1, gatt_disconnection_attempts_);
297 } 297 }
298 #endif // defined(OS_ANDROID) 298 #endif // defined(OS_ANDROID)
299 299
300 #if defined(OS_ANDROID) 300 #if defined(OS_ANDROID)
301 // Starts process of disconnecting and then calls BluetoothGattConnection. 301 // Starts process of disconnecting and then calls BluetoothGattConnection.
302 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectInProgress) { 302 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectInProgress) {
303 InitWithFakeAdapter(); 303 InitWithFakeAdapter();
304 StartLowEnergyDiscoverySession(); 304 StartLowEnergyDiscoverySession();
305 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 305 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
306 306
307 // Create multiple connections and simulate connection complete: 307 // Create multiple connections and simulate connection complete:
308 device->CreateGattConnection(GetGattConnectionCallback(), 308 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
309 GetConnectErrorCallback()); 309 GetConnectErrorCallback(Call::NOT_EXPECTED));
310 device->CreateGattConnection(GetGattConnectionCallback(), 310 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
311 GetConnectErrorCallback()); 311 GetConnectErrorCallback(Call::NOT_EXPECTED));
312 SimulateGattConnection(device); 312 SimulateGattConnection(device);
313 313
314 // Disconnect all CreateGattConnection objects & create a new connection. 314 // Disconnect all CreateGattConnection objects & create a new connection.
315 // But, don't yet simulate the device disconnecting: 315 // But, don't yet simulate the device disconnecting:
316 ResetEventCounts(); 316 ResetEventCounts();
317 for (BluetoothGattConnection* connection : gatt_connections_) 317 for (BluetoothGattConnection* connection : gatt_connections_)
318 connection->Disconnect(); 318 connection->Disconnect();
319 EXPECT_EQ(1, gatt_disconnection_attempts_); 319 EXPECT_EQ(1, gatt_disconnection_attempts_);
320 320
321 // Create a connection. 321 // Create a connection.
322 device->CreateGattConnection(GetGattConnectionCallback(), 322 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
323 GetConnectErrorCallback()); 323 GetConnectErrorCallback(Call::NOT_EXPECTED));
324 EXPECT_EQ(0, gatt_connection_attempts_); // No connection attempt. 324 EXPECT_EQ(0, gatt_connection_attempts_); // No connection attempt.
325 EXPECT_EQ(1, callback_count_); // Device is assumed still connected. 325 EXPECT_EQ(1, callback_count_); // Device is assumed still connected.
326 EXPECT_EQ(0, error_callback_count_); 326 EXPECT_EQ(0, error_callback_count_);
327 EXPECT_FALSE(gatt_connections_.front()->IsConnected()); 327 EXPECT_FALSE(gatt_connections_.front()->IsConnected());
328 EXPECT_TRUE(gatt_connections_.back()->IsConnected()); 328 EXPECT_TRUE(gatt_connections_.back()->IsConnected());
329 329
330 // Actually disconnect: 330 // Actually disconnect:
331 ResetEventCounts(); 331 ResetEventCounts();
332 SimulateGattDisconnection(device); 332 SimulateGattDisconnection(device);
333 EXPECT_EQ(0, callback_count_); 333 EXPECT_EQ(0, callback_count_);
334 EXPECT_EQ(0, error_callback_count_); 334 EXPECT_EQ(0, error_callback_count_);
335 for (BluetoothGattConnection* connection : gatt_connections_) 335 for (BluetoothGattConnection* connection : gatt_connections_)
336 EXPECT_FALSE(connection->IsConnected()); 336 EXPECT_FALSE(connection->IsConnected());
337 } 337 }
338 #endif // defined(OS_ANDROID) 338 #endif // defined(OS_ANDROID)
339 339
340 #if defined(OS_ANDROID) 340 #if defined(OS_ANDROID)
341 // Calls CreateGattConnection but receives notice that the device disconnected 341 // Calls CreateGattConnection but receives notice that the device disconnected
342 // before it ever connects. 342 // before it ever connects.
343 TEST_F(BluetoothTest, BluetoothGattConnection_SimulateDisconnect) { 343 TEST_F(BluetoothTest, BluetoothGattConnection_SimulateDisconnect) {
344 InitWithFakeAdapter(); 344 InitWithFakeAdapter();
345 StartLowEnergyDiscoverySession(); 345 StartLowEnergyDiscoverySession();
346 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 346 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
347 347
348 ResetEventCounts(); 348 ResetEventCounts();
349 device->CreateGattConnection(GetGattConnectionCallback(), 349 device->CreateGattConnection(GetGattConnectionCallback(Call::NOT_EXPECTED),
350 GetConnectErrorCallback()); 350 GetConnectErrorCallback(Call::EXPECTED));
351 EXPECT_EQ(1, gatt_connection_attempts_); 351 EXPECT_EQ(1, gatt_connection_attempts_);
352 SimulateGattDisconnection(device); 352 SimulateGattDisconnection(device);
353 EXPECT_EQ(0, callback_count_); 353 EXPECT_EQ(0, callback_count_);
354 EXPECT_EQ(1, error_callback_count_); 354 EXPECT_EQ(1, error_callback_count_);
355 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_); 355 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_);
356 for (BluetoothGattConnection* connection : gatt_connections_) 356 for (BluetoothGattConnection* connection : gatt_connections_)
357 EXPECT_FALSE(connection->IsConnected()); 357 EXPECT_FALSE(connection->IsConnected());
358 } 358 }
359 #endif // defined(OS_ANDROID) 359 #endif // defined(OS_ANDROID)
360 360
361 #if defined(OS_ANDROID) 361 #if defined(OS_ANDROID)
362 // Calls CreateGattConnection & DisconnectGatt, then simulates connection. 362 // Calls CreateGattConnection & DisconnectGatt, then simulates connection.
363 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectGatt_SimulateConnect) { 363 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectGatt_SimulateConnect) {
364 InitWithFakeAdapter(); 364 InitWithFakeAdapter();
365 StartLowEnergyDiscoverySession(); 365 StartLowEnergyDiscoverySession();
366 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 366 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
367 367
368 ResetEventCounts(); 368 ResetEventCounts();
369 device->CreateGattConnection(GetGattConnectionCallback(), 369 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
370 GetConnectErrorCallback()); 370 GetConnectErrorCallback(Call::NOT_EXPECTED));
371 device->DisconnectGatt(); 371 device->DisconnectGatt();
372 EXPECT_EQ(1, gatt_connection_attempts_); 372 EXPECT_EQ(1, gatt_connection_attempts_);
373 EXPECT_EQ(1, gatt_disconnection_attempts_); 373 EXPECT_EQ(1, gatt_disconnection_attempts_);
374 SimulateGattConnection(device); 374 SimulateGattConnection(device);
375 EXPECT_EQ(1, callback_count_); 375 EXPECT_EQ(1, callback_count_);
376 EXPECT_EQ(0, error_callback_count_); 376 EXPECT_EQ(0, error_callback_count_);
377 EXPECT_TRUE(gatt_connections_.back()->IsConnected()); 377 EXPECT_TRUE(gatt_connections_.back()->IsConnected());
378 ResetEventCounts(); 378 ResetEventCounts();
379 SimulateGattDisconnection(device); 379 SimulateGattDisconnection(device);
380 EXPECT_EQ(0, callback_count_); 380 EXPECT_EQ(0, callback_count_);
381 EXPECT_EQ(0, error_callback_count_); 381 EXPECT_EQ(0, error_callback_count_);
382 } 382 }
383 #endif // defined(OS_ANDROID) 383 #endif // defined(OS_ANDROID)
384 384
385 #if defined(OS_ANDROID) 385 #if defined(OS_ANDROID)
386 // Calls CreateGattConnection & DisconnectGatt, then simulates disconnection. 386 // Calls CreateGattConnection & DisconnectGatt, then simulates disconnection.
387 TEST_F(BluetoothTest, 387 TEST_F(BluetoothTest,
388 BluetoothGattConnection_DisconnectGatt_SimulateDisconnect) { 388 BluetoothGattConnection_DisconnectGatt_SimulateDisconnect) {
389 InitWithFakeAdapter(); 389 InitWithFakeAdapter();
390 StartLowEnergyDiscoverySession(); 390 StartLowEnergyDiscoverySession();
391 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 391 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
392 392
393 ResetEventCounts(); 393 ResetEventCounts();
394 device->CreateGattConnection(GetGattConnectionCallback(), 394 device->CreateGattConnection(GetGattConnectionCallback(Call::NOT_EXPECTED),
395 GetConnectErrorCallback()); 395 GetConnectErrorCallback(Call::EXPECTED));
396 device->DisconnectGatt(); 396 device->DisconnectGatt();
397 EXPECT_EQ(1, gatt_connection_attempts_); 397 EXPECT_EQ(1, gatt_connection_attempts_);
398 EXPECT_EQ(1, gatt_disconnection_attempts_); 398 EXPECT_EQ(1, gatt_disconnection_attempts_);
399 SimulateGattDisconnection(device); 399 SimulateGattDisconnection(device);
400 EXPECT_EQ(0, callback_count_); 400 EXPECT_EQ(0, callback_count_);
401 EXPECT_EQ(1, error_callback_count_); 401 EXPECT_EQ(1, error_callback_count_);
402 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_); 402 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_);
403 for (BluetoothGattConnection* connection : gatt_connections_) 403 for (BluetoothGattConnection* connection : gatt_connections_)
404 EXPECT_FALSE(connection->IsConnected()); 404 EXPECT_FALSE(connection->IsConnected());
405 } 405 }
406 #endif // defined(OS_ANDROID) 406 #endif // defined(OS_ANDROID)
407 407
408 #if defined(OS_ANDROID) 408 #if defined(OS_ANDROID)
409 // Calls CreateGattConnection, but simulate errors connecting. Also, verifies 409 // Calls CreateGattConnection, but simulate errors connecting. Also, verifies
410 // multiple errors should only invoke callbacks once. 410 // multiple errors should only invoke callbacks once.
411 TEST_F(BluetoothTest, BluetoothGattConnection_ErrorAfterConnection) { 411 TEST_F(BluetoothTest, BluetoothGattConnection_ErrorAfterConnection) {
412 InitWithFakeAdapter(); 412 InitWithFakeAdapter();
413 StartLowEnergyDiscoverySession(); 413 StartLowEnergyDiscoverySession();
414 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 414 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
415 415
416 ResetEventCounts(); 416 ResetEventCounts();
417 device->CreateGattConnection(GetGattConnectionCallback(), 417 device->CreateGattConnection(GetGattConnectionCallback(Call::NOT_EXPECTED),
418 GetConnectErrorCallback()); 418 GetConnectErrorCallback(Call::EXPECTED));
419 EXPECT_EQ(1, gatt_connection_attempts_); 419 EXPECT_EQ(1, gatt_connection_attempts_);
420 SimulateGattConnectionError(device, BluetoothDevice::ERROR_AUTH_FAILED); 420 SimulateGattConnectionError(device, BluetoothDevice::ERROR_AUTH_FAILED);
421 SimulateGattConnectionError(device, BluetoothDevice::ERROR_FAILED); 421 SimulateGattConnectionError(device, BluetoothDevice::ERROR_FAILED);
422 EXPECT_EQ(0, callback_count_); 422 EXPECT_EQ(0, callback_count_);
423 EXPECT_EQ(1, error_callback_count_); 423 EXPECT_EQ(1, error_callback_count_);
424 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_FAILED, last_connect_error_code_); 424 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_FAILED, last_connect_error_code_);
425 for (BluetoothGattConnection* connection : gatt_connections_) 425 for (BluetoothGattConnection* connection : gatt_connections_)
426 EXPECT_FALSE(connection->IsConnected()); 426 EXPECT_FALSE(connection->IsConnected());
427 } 427 }
428 #endif // defined(OS_ANDROID) 428 #endif // defined(OS_ANDROID)
429 429
430 #if defined(OS_ANDROID) 430 #if defined(OS_ANDROID)
431 TEST_F(BluetoothTest, GattServices_ObserversCalls) { 431 TEST_F(BluetoothTest, GattServices_ObserversCalls) {
432 InitWithFakeAdapter(); 432 InitWithFakeAdapter();
433 StartLowEnergyDiscoverySession(); 433 StartLowEnergyDiscoverySession();
434 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 434 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
435 device->CreateGattConnection(GetGattConnectionCallback(), 435 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
436 GetConnectErrorCallback()); 436 GetConnectErrorCallback(Call::NOT_EXPECTED));
437 TestBluetoothAdapterObserver observer(adapter_); 437 TestBluetoothAdapterObserver observer(adapter_);
438 ResetEventCounts(); 438 ResetEventCounts();
439 SimulateGattConnection(device); 439 SimulateGattConnection(device);
440 EXPECT_EQ(1, gatt_discovery_attempts_); 440 EXPECT_EQ(1, gatt_discovery_attempts_);
441 441
442 std::vector<std::string> services; 442 std::vector<std::string> services;
443 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); 443 services.push_back("00000000-0000-1000-8000-00805f9b34fb");
444 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); 444 services.push_back("00000001-0000-1000-8000-00805f9b34fb");
445 SimulateGattServicesDiscovered(device, services); 445 SimulateGattServicesDiscovered(device, services);
446 446
447 EXPECT_EQ(1, observer.gatt_services_discovered_count()); 447 EXPECT_EQ(1, observer.gatt_services_discovered_count());
448 EXPECT_EQ(2, observer.gatt_service_added_count()); 448 EXPECT_EQ(2, observer.gatt_service_added_count());
449 } 449 }
450 #endif // defined(OS_ANDROID) 450 #endif // defined(OS_ANDROID)
451 451
452 #if defined(OS_ANDROID) 452 #if defined(OS_ANDROID)
453 TEST_F(BluetoothTest, GetGattServices_and_GetGattService) { 453 TEST_F(BluetoothTest, GetGattServices_and_GetGattService) {
454 InitWithFakeAdapter(); 454 InitWithFakeAdapter();
455 StartLowEnergyDiscoverySession(); 455 StartLowEnergyDiscoverySession();
456 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 456 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
457 device->CreateGattConnection(GetGattConnectionCallback(), 457 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
458 GetConnectErrorCallback()); 458 GetConnectErrorCallback(Call::NOT_EXPECTED));
459 ResetEventCounts(); 459 ResetEventCounts();
460 SimulateGattConnection(device); 460 SimulateGattConnection(device);
461 EXPECT_EQ(1, gatt_discovery_attempts_); 461 EXPECT_EQ(1, gatt_discovery_attempts_);
462 462
463 std::vector<std::string> services; 463 std::vector<std::string> services;
464 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); 464 services.push_back("00000000-0000-1000-8000-00805f9b34fb");
465 // 2 duplicate UUIDs creating 2 instances. 465 // 2 duplicate UUIDs creating 2 instances.
466 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); 466 services.push_back("00000001-0000-1000-8000-00805f9b34fb");
467 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); 467 services.push_back("00000001-0000-1000-8000-00805f9b34fb");
468 SimulateGattServicesDiscovered(device, services); 468 SimulateGattServicesDiscovered(device, services);
469 EXPECT_EQ(3u, device->GetGattServices().size()); 469 EXPECT_EQ(3u, device->GetGattServices().size());
470 470
471 // Test GetGattService: 471 // Test GetGattService:
472 std::string service_id1 = device->GetGattServices()[0]->GetIdentifier(); 472 std::string service_id1 = device->GetGattServices()[0]->GetIdentifier();
473 std::string service_id2 = device->GetGattServices()[1]->GetIdentifier(); 473 std::string service_id2 = device->GetGattServices()[1]->GetIdentifier();
474 std::string service_id3 = device->GetGattServices()[2]->GetIdentifier(); 474 std::string service_id3 = device->GetGattServices()[2]->GetIdentifier();
475 EXPECT_TRUE(device->GetGattService(service_id1)); 475 EXPECT_TRUE(device->GetGattService(service_id1));
476 EXPECT_TRUE(device->GetGattService(service_id2)); 476 EXPECT_TRUE(device->GetGattService(service_id2));
477 EXPECT_TRUE(device->GetGattService(service_id3)); 477 EXPECT_TRUE(device->GetGattService(service_id3));
478 } 478 }
479 #endif // defined(OS_ANDROID) 479 #endif // defined(OS_ANDROID)
480 480
481 #if defined(OS_ANDROID) 481 #if defined(OS_ANDROID)
482 TEST_F(BluetoothTest, GetGattServices_DiscoveryError) { 482 TEST_F(BluetoothTest, GetGattServices_DiscoveryError) {
483 InitWithFakeAdapter(); 483 InitWithFakeAdapter();
484 StartLowEnergyDiscoverySession(); 484 StartLowEnergyDiscoverySession();
485 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 485 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
486 device->CreateGattConnection(GetGattConnectionCallback(), 486 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
487 GetConnectErrorCallback()); 487 GetConnectErrorCallback(Call::NOT_EXPECTED));
488 ResetEventCounts(); 488 ResetEventCounts();
489 SimulateGattConnection(device); 489 SimulateGattConnection(device);
490 EXPECT_EQ(1, gatt_discovery_attempts_); 490 EXPECT_EQ(1, gatt_discovery_attempts_);
491 491
492 SimulateGattServicesDiscoveryError(device); 492 SimulateGattServicesDiscoveryError(device);
493 EXPECT_EQ(0u, device->GetGattServices().size()); 493 EXPECT_EQ(0u, device->GetGattServices().size());
494 } 494 }
495 #endif // defined(OS_ANDROID) 495 #endif // defined(OS_ANDROID)
496 496
497 } // namespace device 497 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698