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