| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/device.h" | 5 #include "device/bluetooth/device.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 Device::GetServicesCallback CheckGetServicesCount(Call expected) { | 172 Device::GetServicesCallback CheckGetServicesCount(Call expected) { |
| 173 if (expected == Call::EXPECTED) | 173 if (expected == Call::EXPECTED) |
| 174 ++expected_success_callback_calls_; | 174 ++expected_success_callback_calls_; |
| 175 | 175 |
| 176 return base::Bind(&BluetoothInterfaceDeviceTest::CheckGetServicesCountImpl, | 176 return base::Bind(&BluetoothInterfaceDeviceTest::CheckGetServicesCountImpl, |
| 177 weak_factory_.GetWeakPtr(), expected, | 177 weak_factory_.GetWeakPtr(), expected, |
| 178 2 /* expected_service_count */, | 178 2 /* expected_service_count */, |
| 179 expected_callback_count_++); | 179 expected_callback_count_++); |
| 180 } | 180 } |
| 181 | 181 |
| 182 void CheckGetCharacteristicsCountImpl( | |
| 183 Call expected, | |
| 184 size_t expected_count, | |
| 185 int num_of_preceding_calls, | |
| 186 std::vector<mojom::CharacteristicInfoPtr> characteristics) { | |
| 187 EXPECT_EQ(num_of_preceding_calls, actual_callback_count_); | |
| 188 ++actual_callback_count_; | |
| 189 | |
| 190 if (expected == Call::EXPECTED) | |
| 191 ++actual_success_callback_calls_; | |
| 192 | |
| 193 EXPECT_EQ(expected_count, characteristics.size()); | |
| 194 } | |
| 195 | |
| 196 Device::GetCharacteristicsCallback CheckGetCharacteristicsCount( | |
| 197 Call expected, | |
| 198 int expected_count) { | |
| 199 if (expected == Call::EXPECTED) | |
| 200 ++expected_success_callback_calls_; | |
| 201 | |
| 202 return base::Bind( | |
| 203 &BluetoothInterfaceDeviceTest::CheckGetCharacteristicsCountImpl, | |
| 204 weak_factory_.GetWeakPtr(), expected, expected_count, | |
| 205 expected_callback_count_++); | |
| 206 } | |
| 207 | |
| 208 scoped_refptr<NiceMockBluetoothAdapter> adapter_; | 182 scoped_refptr<NiceMockBluetoothAdapter> adapter_; |
| 209 NiceMockBluetoothDevice device_; | 183 NiceMockBluetoothDevice device_; |
| 210 base::MessageLoop message_loop_; | 184 base::MessageLoop message_loop_; |
| 211 mojom::DevicePtr proxy_; | 185 mojom::DevicePtr proxy_; |
| 212 mojo::StrongBindingPtr<mojom::Device> binding_ptr_; | 186 mojo::StrongBindingPtr<mojom::Device> binding_ptr_; |
| 213 | 187 |
| 214 bool message_pipe_closed_ = false; | 188 bool message_pipe_closed_ = false; |
| 215 bool expect_device_service_deleted_ = false; | 189 bool expect_device_service_deleted_ = false; |
| 216 int expected_success_callback_calls_ = 0; | 190 int expected_success_callback_calls_ = 0; |
| 217 int actual_success_callback_calls_ = 0; | 191 int actual_success_callback_calls_ = 0; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 proxy_->GetServices(CheckGetServicesCount(Call::NOT_EXPECTED)); | 265 proxy_->GetServices(CheckGetServicesCount(Call::NOT_EXPECTED)); |
| 292 EXPECT_EQ(0, actual_callback_count_); | 266 EXPECT_EQ(0, actual_callback_count_); |
| 293 | 267 |
| 294 // Simulate connection loss. | 268 // Simulate connection loss. |
| 295 proxy_->Disconnect(); | 269 proxy_->Disconnect(); |
| 296 expect_device_service_deleted_ = true; | 270 expect_device_service_deleted_ = true; |
| 297 | 271 |
| 298 // Wait for message pipe to process error. | 272 // Wait for message pipe to process error. |
| 299 base::RunLoop().RunUntilIdle(); | 273 base::RunLoop().RunUntilIdle(); |
| 300 } | 274 } |
| 301 | |
| 302 TEST_F(BluetoothInterfaceDeviceTest, GetCharacteristics) { | |
| 303 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | |
| 304 .WillRepeatedly(Return(true)); | |
| 305 | |
| 306 proxy_->GetCharacteristics(kTestServiceId0, | |
| 307 CheckGetCharacteristicsCount(Call::EXPECTED, 2)); | |
| 308 | |
| 309 base::RunLoop().RunUntilIdle(); | |
| 310 } | |
| 311 | |
| 312 TEST_F(BluetoothInterfaceDeviceTest, GetCharacteristicsNotDiscovered) { | |
| 313 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | |
| 314 .WillOnce(Return(false)) | |
| 315 .WillOnce(Return(false)) | |
| 316 .WillRepeatedly(Return(true)); | |
| 317 | |
| 318 // Client: Sends multiple requests for characteristics. | |
| 319 proxy_->GetCharacteristics(kTestServiceId0, | |
| 320 CheckGetCharacteristicsCount(Call::EXPECTED, 2)); | |
| 321 proxy_->GetCharacteristics(kTestServiceId1, | |
| 322 CheckGetCharacteristicsCount(Call::EXPECTED, 1)); | |
| 323 | |
| 324 base::RunLoop().RunUntilIdle(); | |
| 325 | |
| 326 SimulateGattServicesDiscovered(); | |
| 327 | |
| 328 // No more GetCharacteristics calls will complete. | |
| 329 SimulateGattServicesDiscovered(); | |
| 330 | |
| 331 base::RunLoop().RunUntilIdle(); | |
| 332 | |
| 333 // Client: Sends more requests which run immediately. | |
| 334 proxy_->GetCharacteristics(kTestServiceId0, | |
| 335 CheckGetCharacteristicsCount(Call::EXPECTED, 2)); | |
| 336 proxy_->GetCharacteristics(kTestServiceId1, | |
| 337 CheckGetCharacteristicsCount(Call::EXPECTED, 1)); | |
| 338 | |
| 339 base::RunLoop().RunUntilIdle(); | |
| 340 | |
| 341 // No more GetCharacteristics calls will complete. | |
| 342 SimulateGattServicesDiscovered(); | |
| 343 | |
| 344 // Wait for message pipe to process error. | |
| 345 base::RunLoop().RunUntilIdle(); | |
| 346 } | |
| 347 | |
| 348 TEST_F(BluetoothInterfaceDeviceTest, | |
| 349 GetCharacteristicsLostConnectionWithPendingRequests) { | |
| 350 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | |
| 351 .WillRepeatedly(Return(false)); | |
| 352 // Client: Sends multiple requests for characteristics. | |
| 353 proxy_->GetCharacteristics( | |
| 354 kTestServiceId0, CheckGetCharacteristicsCount(Call::NOT_EXPECTED, 2)); | |
| 355 proxy_->GetCharacteristics( | |
| 356 kTestServiceId1, CheckGetCharacteristicsCount(Call::NOT_EXPECTED, 1)); | |
| 357 EXPECT_EQ(0, actual_callback_count_); | |
| 358 | |
| 359 // Simulate connection loss. | |
| 360 device_.SetConnected(false); | |
| 361 SimulateDeviceChanged(); | |
| 362 expect_device_service_deleted_ = true; | |
| 363 | |
| 364 // Wait for message pipe to process error. | |
| 365 base::RunLoop().RunUntilIdle(); | |
| 366 } | |
| 367 | |
| 368 TEST_F(BluetoothInterfaceDeviceTest, | |
| 369 GetCharacteristicsForcedDisconnectionWithPendingRequests) { | |
| 370 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | |
| 371 .WillRepeatedly(Return(false)); | |
| 372 | |
| 373 // Client: Sends multiple requests for characteristics. | |
| 374 proxy_->GetCharacteristics( | |
| 375 kTestServiceId0, CheckGetCharacteristicsCount(Call::NOT_EXPECTED, 2)); | |
| 376 proxy_->GetCharacteristics( | |
| 377 kTestServiceId1, CheckGetCharacteristicsCount(Call::NOT_EXPECTED, 1)); | |
| 378 EXPECT_EQ(0, actual_callback_count_); | |
| 379 | |
| 380 // Simulate connection loss. | |
| 381 proxy_->Disconnect(); | |
| 382 expect_device_service_deleted_ = true; | |
| 383 | |
| 384 // Wait for message pipe to process error. | |
| 385 base::RunLoop().RunUntilIdle(); | |
| 386 } | |
| 387 | |
| 388 } // namespace bluetooth | 275 } // namespace bluetooth |
| OLD | NEW |