| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 BluetoothDevice* device = SimulateLowEnergyDevice(1); | 330 BluetoothDevice* device = SimulateLowEnergyDevice(1); |
| 331 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED), | 331 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED), |
| 332 GetErrorCallback(Call::NOT_EXPECTED)); | 332 GetErrorCallback(Call::NOT_EXPECTED)); |
| 333 | 333 |
| 334 // Connect to the device. | 334 // Connect to the device. |
| 335 // - GetUUIDs: Should return no UUIDs because Services have not been | 335 // - GetUUIDs: Should return no UUIDs because Services have not been |
| 336 // discovered. | 336 // discovered. |
| 337 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 337 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 338 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 338 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 339 SimulateGattConnection(device); | 339 SimulateGattConnection(device); |
| 340 base::RunLoop().RunUntilIdle(); |
| 340 ASSERT_TRUE(device->IsConnected()); | 341 ASSERT_TRUE(device->IsConnected()); |
| 341 | 342 |
| 342 EXPECT_TRUE(device->GetUUIDs().empty()); | 343 EXPECT_TRUE(device->GetUUIDs().empty()); |
| 343 | 344 |
| 344 observer.Reset(); | 345 observer.Reset(); |
| 345 | 346 |
| 346 // Discover services, should notify of device changed. | 347 // Discover services, should notify of device changed. |
| 347 // - GetUUIDs: Should return the device's services' UUIDs. | 348 // - GetUUIDs: Should return the device's services' UUIDs. |
| 348 std::vector<std::string> services; | 349 std::vector<std::string> services; |
| 349 services.push_back(kTestUUIDGenericAccess); | 350 services.push_back(kTestUUIDGenericAccess); |
| 350 SimulateGattServicesDiscovered(device, services); | 351 SimulateGattServicesDiscovered(device, services); |
| 352 base::RunLoop().RunUntilIdle(); |
| 351 | 353 |
| 352 EXPECT_EQ(1, observer.device_changed_count()); | 354 EXPECT_EQ(1, observer.device_changed_count()); |
| 353 | 355 |
| 354 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDGenericAccess)}), | 356 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDGenericAccess)}), |
| 355 device->GetUUIDs()); | 357 device->GetUUIDs()); |
| 356 | 358 |
| 357 #if defined(OS_MACOSX) | 359 #if defined(OS_MACOSX) |
| 358 // TODO(ortuno): Enable in Android and Windows. | 360 // TODO(ortuno): Enable in Android and Windows. |
| 359 // Android and Windows don't yet support service changed events. | 361 // Android and Windows don't yet support service changed events. |
| 360 // http://crbug.com/548280 | 362 // http://crbug.com/548280 |
| 361 // http://crbug.com/579202 | 363 // http://crbug.com/579202 |
| 362 | 364 |
| 363 observer.Reset(); | 365 observer.Reset(); |
| 364 | 366 |
| 365 // Notify of services changed, should notify of device changed. | 367 // Notify of services changed, should notify of device changed. |
| 366 // - GetUUIDs: Should return no UUIDs because we no longer know what services | 368 // - GetUUIDs: Should return no UUIDs because we no longer know what services |
| 367 // the device has. | 369 // the device has. |
| 368 SimulateGattServicesChanged(device); | 370 SimulateGattServicesChanged(device); |
| 369 | 371 |
| 370 ASSERT_FALSE(device->IsGattServicesDiscoveryComplete()); | 372 ASSERT_FALSE(device->IsGattServicesDiscoveryComplete()); |
| 371 EXPECT_EQ(1, observer.device_changed_count()); | 373 EXPECT_EQ(1, observer.device_changed_count()); |
| 372 EXPECT_TRUE(device->GetUUIDs().empty()); | 374 EXPECT_TRUE(device->GetUUIDs().empty()); |
| 373 | 375 |
| 374 // Services discovered again, should notify of device changed. | 376 // Services discovered again, should notify of device changed. |
| 375 // - GetUUIDs: Should return Service UUIDs. | 377 // - GetUUIDs: Should return Service UUIDs. |
| 376 SimulateGattServicesDiscovered(device, {} /* services */); | 378 SimulateGattServicesDiscovered(device, {} /* services */); |
| 379 base::RunLoop().RunUntilIdle(); |
| 377 | 380 |
| 378 EXPECT_EQ(2, observer.device_changed_count()); | 381 EXPECT_EQ(2, observer.device_changed_count()); |
| 379 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDGenericAccess)}), | 382 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDGenericAccess)}), |
| 380 device->GetUUIDs()); | 383 device->GetUUIDs()); |
| 381 | 384 |
| 382 #endif // defined(OS_MACOSX) | 385 #endif // defined(OS_MACOSX) |
| 383 | 386 |
| 384 observer.Reset(); | 387 observer.Reset(); |
| 385 | 388 |
| 386 // Disconnect, should notify device changed. | 389 // Disconnect, should notify device changed. |
| 387 // - GetUUIDs: Should return no UUIDs since we no longer know what services | 390 // - GetUUIDs: Should return no UUIDs since we no longer know what services |
| 388 // the device holds and notify of device changed. | 391 // the device holds and notify of device changed. |
| 389 gatt_connections_[0]->Disconnect(); | 392 gatt_connections_[0]->Disconnect(); |
| 390 SimulateGattDisconnection(device); | 393 SimulateGattDisconnection(device); |
| 394 base::RunLoop().RunUntilIdle(); |
| 391 ASSERT_FALSE(device->IsGattConnected()); | 395 ASSERT_FALSE(device->IsGattConnected()); |
| 392 | 396 |
| 393 EXPECT_EQ(1, observer.device_changed_count()); | 397 EXPECT_EQ(1, observer.device_changed_count()); |
| 394 EXPECT_TRUE(device->GetUUIDs().empty()); | 398 EXPECT_TRUE(device->GetUUIDs().empty()); |
| 395 } | 399 } |
| 396 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 400 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| 397 | 401 |
| 398 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 402 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 399 // Tests Advertisement Data is updated correctly when we start discovery | 403 // Tests Advertisement Data is updated correctly when we start discovery |
| 400 // during a connection. | 404 // during a connection. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 417 GetErrorCallback(Call::NOT_EXPECTED)); | 421 GetErrorCallback(Call::NOT_EXPECTED)); |
| 418 ASSERT_FALSE(adapter_->IsDiscovering()); | 422 ASSERT_FALSE(adapter_->IsDiscovering()); |
| 419 ASSERT_FALSE(discovery_sessions_[0]->IsActive()); | 423 ASSERT_FALSE(discovery_sessions_[0]->IsActive()); |
| 420 ASSERT_EQ(0u, device->GetUUIDs().size()); | 424 ASSERT_EQ(0u, device->GetUUIDs().size()); |
| 421 discovery_sessions_.clear(); | 425 discovery_sessions_.clear(); |
| 422 | 426 |
| 423 // Connect. | 427 // Connect. |
| 424 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 428 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 425 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 429 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 426 SimulateGattConnection(device); | 430 SimulateGattConnection(device); |
| 431 base::RunLoop().RunUntilIdle(); |
| 427 ASSERT_TRUE(device->IsConnected()); | 432 ASSERT_TRUE(device->IsConnected()); |
| 428 | 433 |
| 429 observer.Reset(); | 434 observer.Reset(); |
| 430 | 435 |
| 431 // Start Discovery and receive advertisement during connection, | 436 // Start Discovery and receive advertisement during connection, |
| 432 // should notify of device changed. | 437 // should notify of device changed. |
| 433 // - GetInquiryRSSI: Should return the packet's rssi. | 438 // - GetInquiryRSSI: Should return the packet's rssi. |
| 434 // - GetUUIDs: Should return only Advertised UUIDs since services haven't | 439 // - GetUUIDs: Should return only Advertised UUIDs since services haven't |
| 435 // been discovered yet. | 440 // been discovered yet. |
| 436 // - GetServiceData: Should return last advertised Service Data. | 441 // - GetServiceData: Should return last advertised Service Data. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 452 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}), | 457 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}), |
| 453 device->GetServiceData()); | 458 device->GetServiceData()); |
| 454 #endif // defined(OS_MACOSX) | 459 #endif // defined(OS_MACOSX) |
| 455 EXPECT_EQ(ToInt8(TestTxPower::LOWEST), device->GetInquiryTxPower().value()); | 460 EXPECT_EQ(ToInt8(TestTxPower::LOWEST), device->GetInquiryTxPower().value()); |
| 456 | 461 |
| 457 // Discover services, should notify of device changed. | 462 // Discover services, should notify of device changed. |
| 458 // - GetUUIDs: Should return both Advertised UUIDs and Service UUIDs. | 463 // - GetUUIDs: Should return both Advertised UUIDs and Service UUIDs. |
| 459 std::vector<std::string> services; | 464 std::vector<std::string> services; |
| 460 services.push_back(kTestUUIDHeartRate); | 465 services.push_back(kTestUUIDHeartRate); |
| 461 SimulateGattServicesDiscovered(device, services); | 466 SimulateGattServicesDiscovered(device, services); |
| 467 base::RunLoop().RunUntilIdle(); |
| 462 | 468 |
| 463 EXPECT_EQ(2, observer.device_changed_count()); | 469 EXPECT_EQ(2, observer.device_changed_count()); |
| 464 | 470 |
| 465 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDGenericAccess), | 471 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDGenericAccess), |
| 466 BluetoothUUID(kTestUUIDGenericAttribute), | 472 BluetoothUUID(kTestUUIDGenericAttribute), |
| 467 BluetoothUUID(kTestUUIDHeartRate)}), | 473 BluetoothUUID(kTestUUIDHeartRate)}), |
| 468 device->GetUUIDs()); | 474 device->GetUUIDs()); |
| 469 | 475 |
| 470 // Receive advertisement again, notify of device changed. | 476 // Receive advertisement again, notify of device changed. |
| 471 // - GetInquiryRSSI: Should return last packet's rssi. | 477 // - GetInquiryRSSI: Should return last packet's rssi. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 509 // TODO(ortuno): Enable on Android once it supports Service Data. | 515 // TODO(ortuno): Enable on Android once it supports Service Data. |
| 510 // http://crbug.com/639408 | 516 // http://crbug.com/639408 |
| 511 EXPECT_EQ(ServiceDataMap(), device->GetServiceData()); | 517 EXPECT_EQ(ServiceDataMap(), device->GetServiceData()); |
| 512 #endif // defined(OS_MACOSX) | 518 #endif // defined(OS_MACOSX) |
| 513 EXPECT_FALSE(device->GetInquiryTxPower()); | 519 EXPECT_FALSE(device->GetInquiryTxPower()); |
| 514 | 520 |
| 515 // Disconnect device, should notify of device changed. | 521 // Disconnect device, should notify of device changed. |
| 516 // - GetUUIDs: Should return no UUIDs. | 522 // - GetUUIDs: Should return no UUIDs. |
| 517 gatt_connections_[0]->Disconnect(); | 523 gatt_connections_[0]->Disconnect(); |
| 518 SimulateGattDisconnection(device); | 524 SimulateGattDisconnection(device); |
| 525 base::RunLoop().RunUntilIdle(); |
| 519 ASSERT_FALSE(device->IsGattConnected()); | 526 ASSERT_FALSE(device->IsGattConnected()); |
| 520 | 527 |
| 521 EXPECT_EQ(5, observer.device_changed_count()); | 528 EXPECT_EQ(5, observer.device_changed_count()); |
| 522 | 529 |
| 523 EXPECT_TRUE(device->GetUUIDs().empty()); | 530 EXPECT_TRUE(device->GetUUIDs().empty()); |
| 524 } | 531 } |
| 525 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 532 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| 526 | 533 |
| 527 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 534 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 528 TEST_F(BluetoothTest, AdvertisementData_ConnectionDuringDiscovery) { | 535 TEST_F(BluetoothTest, AdvertisementData_ConnectionDuringDiscovery) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 558 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}), | 565 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}), |
| 559 device->GetServiceData()); | 566 device->GetServiceData()); |
| 560 #endif // defined(OS_MACOSX) | 567 #endif // defined(OS_MACOSX) |
| 561 EXPECT_EQ(ToInt8(TestTxPower::LOWEST), device->GetInquiryTxPower().value()); | 568 EXPECT_EQ(ToInt8(TestTxPower::LOWEST), device->GetInquiryTxPower().value()); |
| 562 | 569 |
| 563 // Connect, should notify of device changed. | 570 // Connect, should notify of device changed. |
| 564 // - GetUUIDs: Should return Advertised UUIDs even before GATT Discovery. | 571 // - GetUUIDs: Should return Advertised UUIDs even before GATT Discovery. |
| 565 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 572 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 566 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 573 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 567 SimulateGattConnection(device); | 574 SimulateGattConnection(device); |
| 575 base::RunLoop().RunUntilIdle(); |
| 568 ASSERT_TRUE(device->IsConnected()); | 576 ASSERT_TRUE(device->IsConnected()); |
| 569 | 577 |
| 570 observer.Reset(); | 578 observer.Reset(); |
| 571 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDGenericAccess), | 579 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDGenericAccess), |
| 572 BluetoothUUID(kTestUUIDGenericAttribute)}), | 580 BluetoothUUID(kTestUUIDGenericAttribute)}), |
| 573 device->GetUUIDs()); | 581 device->GetUUIDs()); |
| 574 | 582 |
| 575 // Receive Advertisement with new UUIDs, should notify of device changed. | 583 // Receive Advertisement with new UUIDs, should notify of device changed. |
| 576 // - GetInquiryRSSI: Should return the packet's rssi. | 584 // - GetInquiryRSSI: Should return the packet's rssi. |
| 577 // - GetUUIDs: Should return new Advertised UUIDs. | 585 // - GetUUIDs: Should return new Advertised UUIDs. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 591 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}), | 599 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}), |
| 592 device->GetServiceData()); | 600 device->GetServiceData()); |
| 593 #endif // defined(OS_MACOSX) | 601 #endif // defined(OS_MACOSX) |
| 594 EXPECT_EQ(ToInt8(TestTxPower::LOWER), device->GetInquiryTxPower().value()); | 602 EXPECT_EQ(ToInt8(TestTxPower::LOWER), device->GetInquiryTxPower().value()); |
| 595 | 603 |
| 596 // Discover Services, should notify of device changed. | 604 // Discover Services, should notify of device changed. |
| 597 // - GetUUIDs: Should return Advertised UUIDs and Service UUIDs. | 605 // - GetUUIDs: Should return Advertised UUIDs and Service UUIDs. |
| 598 std::vector<std::string> services; | 606 std::vector<std::string> services; |
| 599 services.push_back(kTestUUIDHeartRate); | 607 services.push_back(kTestUUIDHeartRate); |
| 600 SimulateGattServicesDiscovered(device, services); | 608 SimulateGattServicesDiscovered(device, services); |
| 609 base::RunLoop().RunUntilIdle(); |
| 601 | 610 |
| 602 EXPECT_EQ(2, observer.device_changed_count()); | 611 EXPECT_EQ(2, observer.device_changed_count()); |
| 603 | 612 |
| 604 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDLinkLoss), | 613 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDLinkLoss), |
| 605 BluetoothUUID(kTestUUIDImmediateAlert), | 614 BluetoothUUID(kTestUUIDImmediateAlert), |
| 606 BluetoothUUID(kTestUUIDHeartRate)}), | 615 BluetoothUUID(kTestUUIDHeartRate)}), |
| 607 device->GetUUIDs()); | 616 device->GetUUIDs()); |
| 608 | 617 |
| 609 // Disconnect, should notify of device changed. | 618 // Disconnect, should notify of device changed. |
| 610 // - GetInquiryRSSI: Should return last packet's rssi. | 619 // - GetInquiryRSSI: Should return last packet's rssi. |
| 611 // - GetUUIDs: Should return only Advertised UUIDs. | 620 // - GetUUIDs: Should return only Advertised UUIDs. |
| 612 // - GetServiceData: Should still return same advertised Service Data. | 621 // - GetServiceData: Should still return same advertised Service Data. |
| 613 // - GetInquiryTxPower: Should return the last packet's advertised Tx Power. | 622 // - GetInquiryTxPower: Should return the last packet's advertised Tx Power. |
| 614 gatt_connections_[0]->Disconnect(); | 623 gatt_connections_[0]->Disconnect(); |
| 615 SimulateGattDisconnection(device); | 624 SimulateGattDisconnection(device); |
| 625 base::RunLoop().RunUntilIdle(); |
| 616 ASSERT_FALSE(device->IsGattConnected()); | 626 ASSERT_FALSE(device->IsGattConnected()); |
| 617 | 627 |
| 618 EXPECT_EQ(3, observer.device_changed_count()); | 628 EXPECT_EQ(3, observer.device_changed_count()); |
| 619 EXPECT_EQ(ToInt8(TestRSSI::LOWER), device->GetInquiryRSSI().value()); | 629 EXPECT_EQ(ToInt8(TestRSSI::LOWER), device->GetInquiryRSSI().value()); |
| 620 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDLinkLoss), | 630 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDLinkLoss), |
| 621 BluetoothUUID(kTestUUIDImmediateAlert)}), | 631 BluetoothUUID(kTestUUIDImmediateAlert)}), |
| 622 device->GetUUIDs()); | 632 device->GetUUIDs()); |
| 623 | 633 |
| 624 #if defined(OS_MACOSX) | 634 #if defined(OS_MACOSX) |
| 625 // TODO(ortuno): Enable on Android once it supports Service Data. | 635 // TODO(ortuno): Enable on Android once it supports Service Data. |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 712 return; | 722 return; |
| 713 } | 723 } |
| 714 InitWithFakeAdapter(); | 724 InitWithFakeAdapter(); |
| 715 StartLowEnergyDiscoverySession(); | 725 StartLowEnergyDiscoverySession(); |
| 716 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 726 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 717 | 727 |
| 718 ResetEventCounts(); | 728 ResetEventCounts(); |
| 719 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 729 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 720 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 730 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 721 SimulateGattConnection(device); | 731 SimulateGattConnection(device); |
| 732 base::RunLoop().RunUntilIdle(); |
| 733 |
| 722 ASSERT_EQ(1u, gatt_connections_.size()); | 734 ASSERT_EQ(1u, gatt_connections_.size()); |
| 723 EXPECT_TRUE(device->IsGattConnected()); | 735 EXPECT_TRUE(device->IsGattConnected()); |
| 724 EXPECT_TRUE(gatt_connections_[0]->IsConnected()); | 736 EXPECT_TRUE(gatt_connections_[0]->IsConnected()); |
| 725 } | 737 } |
| 726 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 738 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| 727 | 739 |
| 728 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 740 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 729 TEST_F(BluetoothTest, DisconnectionNotifiesDeviceChanged) { | 741 TEST_F(BluetoothTest, DisconnectionNotifiesDeviceChanged) { |
| 730 if (!PlatformSupportsLowEnergy()) { | 742 if (!PlatformSupportsLowEnergy()) { |
| 731 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 743 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 732 return; | 744 return; |
| 733 } | 745 } |
| 734 InitWithFakeAdapter(); | 746 InitWithFakeAdapter(); |
| 735 TestBluetoothAdapterObserver observer(adapter_); | 747 TestBluetoothAdapterObserver observer(adapter_); |
| 736 StartLowEnergyDiscoverySession(); | 748 StartLowEnergyDiscoverySession(); |
| 737 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 749 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 738 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 750 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 739 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 751 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 740 SimulateGattConnection(device); | 752 SimulateGattConnection(device); |
| 753 base::RunLoop().RunUntilIdle(); |
| 754 |
| 741 EXPECT_EQ(1, observer.device_changed_count()); | 755 EXPECT_EQ(1, observer.device_changed_count()); |
| 742 EXPECT_TRUE(device->IsConnected()); | 756 EXPECT_TRUE(device->IsConnected()); |
| 743 EXPECT_TRUE(device->IsGattConnected()); | 757 EXPECT_TRUE(device->IsGattConnected()); |
| 744 | 758 |
| 745 SimulateGattDisconnection(device); | 759 SimulateGattDisconnection(device); |
| 760 base::RunLoop().RunUntilIdle(); |
| 746 EXPECT_EQ(2, observer.device_changed_count()); | 761 EXPECT_EQ(2, observer.device_changed_count()); |
| 747 EXPECT_FALSE(device->IsConnected()); | 762 EXPECT_FALSE(device->IsConnected()); |
| 748 EXPECT_FALSE(device->IsGattConnected()); | 763 EXPECT_FALSE(device->IsGattConnected()); |
| 749 } | 764 } |
| 750 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 765 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| 751 | 766 |
| 752 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 767 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 753 // Creates BluetoothGattConnection instances and tests that the interface | 768 // Creates BluetoothGattConnection instances and tests that the interface |
| 754 // functions even when some Disconnect and the BluetoothDevice is destroyed. | 769 // functions even when some Disconnect and the BluetoothDevice is destroyed. |
| 755 TEST_F(BluetoothTest, BluetoothGattConnection) { | 770 TEST_F(BluetoothTest, BluetoothGattConnection) { |
| 756 if (!PlatformSupportsLowEnergy()) { | 771 if (!PlatformSupportsLowEnergy()) { |
| 757 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 772 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 758 return; | 773 return; |
| 759 } | 774 } |
| 760 InitWithFakeAdapter(); | 775 InitWithFakeAdapter(); |
| 761 StartLowEnergyDiscoverySession(); | 776 StartLowEnergyDiscoverySession(); |
| 762 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 777 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 763 std::string device_address = device->GetAddress(); | 778 std::string device_address = device->GetAddress(); |
| 764 | 779 |
| 765 // CreateGattConnection | 780 // CreateGattConnection |
| 766 ResetEventCounts(); | 781 ResetEventCounts(); |
| 767 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 782 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 768 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 783 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 769 EXPECT_EQ(1, gatt_connection_attempts_); | 784 EXPECT_EQ(1, gatt_connection_attempts_); |
| 785 |
| 770 SimulateGattConnection(device); | 786 SimulateGattConnection(device); |
| 787 base::RunLoop().RunUntilIdle(); |
| 788 |
| 771 EXPECT_EQ(1, callback_count_); | 789 EXPECT_EQ(1, callback_count_); |
| 772 EXPECT_EQ(0, error_callback_count_); | 790 EXPECT_EQ(0, error_callback_count_); |
| 773 ASSERT_EQ(1u, gatt_connections_.size()); | 791 ASSERT_EQ(1u, gatt_connections_.size()); |
| 774 EXPECT_TRUE(device->IsGattConnected()); | 792 EXPECT_TRUE(device->IsGattConnected()); |
| 775 EXPECT_TRUE(gatt_connections_[0]->IsConnected()); | 793 EXPECT_TRUE(gatt_connections_[0]->IsConnected()); |
| 776 | 794 |
| 777 // Connect again once already connected. | 795 // Connect again once already connected. |
| 778 ResetEventCounts(); | 796 ResetEventCounts(); |
| 779 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 797 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 780 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 798 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 826 StartLowEnergyDiscoverySession(); | 844 StartLowEnergyDiscoverySession(); |
| 827 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 845 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 828 | 846 |
| 829 // CreateGattConnection, & multiple connections from platform only invoke | 847 // CreateGattConnection, & multiple connections from platform only invoke |
| 830 // callbacks once: | 848 // callbacks once: |
| 831 ResetEventCounts(); | 849 ResetEventCounts(); |
| 832 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 850 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 833 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 851 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 834 SimulateGattConnection(device); | 852 SimulateGattConnection(device); |
| 835 SimulateGattConnection(device); | 853 SimulateGattConnection(device); |
| 854 base::RunLoop().RunUntilIdle(); |
| 855 |
| 836 EXPECT_EQ(1, gatt_connection_attempts_); | 856 EXPECT_EQ(1, gatt_connection_attempts_); |
| 837 EXPECT_EQ(1, callback_count_); | 857 EXPECT_EQ(1, callback_count_); |
| 838 EXPECT_EQ(0, error_callback_count_); | 858 EXPECT_EQ(0, error_callback_count_); |
| 839 EXPECT_TRUE(gatt_connections_[0]->IsConnected()); | 859 EXPECT_TRUE(gatt_connections_[0]->IsConnected()); |
| 840 | 860 |
| 841 // Become disconnected: | 861 // Become disconnected: |
| 842 SimulateGattDisconnection(device); | 862 SimulateGattDisconnection(device); |
| 863 base::RunLoop().RunUntilIdle(); |
| 843 EXPECT_FALSE(gatt_connections_[0]->IsConnected()); | 864 EXPECT_FALSE(gatt_connections_[0]->IsConnected()); |
| 844 } | 865 } |
| 845 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 866 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| 846 | 867 |
| 847 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 868 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 848 // Calls CreateGattConnection after already connected. | 869 // Calls CreateGattConnection after already connected. |
| 849 TEST_F(BluetoothTest, BluetoothGattConnection_AlreadyConnected) { | 870 TEST_F(BluetoothTest, BluetoothGattConnection_AlreadyConnected) { |
| 850 if (!PlatformSupportsLowEnergy()) { | 871 if (!PlatformSupportsLowEnergy()) { |
| 851 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 872 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 852 return; | 873 return; |
| 853 } | 874 } |
| 854 InitWithFakeAdapter(); | 875 InitWithFakeAdapter(); |
| 855 StartLowEnergyDiscoverySession(); | 876 StartLowEnergyDiscoverySession(); |
| 856 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 877 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 857 | 878 |
| 858 // Be already connected: | 879 // Be already connected: |
| 859 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 880 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 860 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 881 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 861 SimulateGattConnection(device); | 882 SimulateGattConnection(device); |
| 883 base::RunLoop().RunUntilIdle(); |
| 862 EXPECT_TRUE(gatt_connections_[0]->IsConnected()); | 884 EXPECT_TRUE(gatt_connections_[0]->IsConnected()); |
| 863 | 885 |
| 864 // Then CreateGattConnection: | 886 // Then CreateGattConnection: |
| 865 ResetEventCounts(); | 887 ResetEventCounts(); |
| 866 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 888 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 867 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 889 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 868 EXPECT_EQ(0, gatt_connection_attempts_); | 890 EXPECT_EQ(0, gatt_connection_attempts_); |
| 869 EXPECT_TRUE(gatt_connections_[1]->IsConnected()); | 891 EXPECT_TRUE(gatt_connections_[1]->IsConnected()); |
| 870 } | 892 } |
| 871 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 893 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| 872 | 894 |
| 873 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 895 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 874 // Creates BluetoothGattConnection after one exists that has disconnected. | 896 // Creates BluetoothGattConnection after one exists that has disconnected. |
| 875 TEST_F(BluetoothTest, | 897 TEST_F(BluetoothTest, |
| 876 BluetoothGattConnection_NewConnectionLeavesPreviousDisconnected) { | 898 BluetoothGattConnection_NewConnectionLeavesPreviousDisconnected) { |
| 877 if (!PlatformSupportsLowEnergy()) { | 899 if (!PlatformSupportsLowEnergy()) { |
| 878 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 900 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 879 return; | 901 return; |
| 880 } | 902 } |
| 881 InitWithFakeAdapter(); | 903 InitWithFakeAdapter(); |
| 882 StartLowEnergyDiscoverySession(); | 904 StartLowEnergyDiscoverySession(); |
| 883 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 905 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 884 | 906 |
| 885 // Create connection: | 907 // Create connection: |
| 886 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 908 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 887 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 909 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 888 SimulateGattConnection(device); | 910 SimulateGattConnection(device); |
| 911 base::RunLoop().RunUntilIdle(); |
| 889 | 912 |
| 890 // Disconnect connection: | 913 // Disconnect connection: |
| 891 gatt_connections_[0]->Disconnect(); | 914 gatt_connections_[0]->Disconnect(); |
| 892 SimulateGattDisconnection(device); | 915 SimulateGattDisconnection(device); |
| 916 base::RunLoop().RunUntilIdle(); |
| 893 | 917 |
| 894 // Create 2nd connection: | 918 // Create 2nd connection: |
| 895 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 919 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 896 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 920 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 897 SimulateGattConnection(device); | 921 SimulateGattConnection(device); |
| 922 base::RunLoop().RunUntilIdle(); |
| 898 | 923 |
| 899 EXPECT_FALSE(gatt_connections_[0]->IsConnected()) | 924 EXPECT_FALSE(gatt_connections_[0]->IsConnected()) |
| 900 << "The disconnected connection shouldn't become connected when another " | 925 << "The disconnected connection shouldn't become connected when another " |
| 901 "connection is created."; | 926 "connection is created."; |
| 902 EXPECT_TRUE(gatt_connections_[1]->IsConnected()); | 927 EXPECT_TRUE(gatt_connections_[1]->IsConnected()); |
| 903 } | 928 } |
| 904 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 929 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| 905 | 930 |
| 906 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 931 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 907 // Deletes BluetoothGattConnection causing disconnection. | 932 // Deletes BluetoothGattConnection causing disconnection. |
| 908 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectWhenObjectsDestroyed) { | 933 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectWhenObjectsDestroyed) { |
| 909 if (!PlatformSupportsLowEnergy()) { | 934 if (!PlatformSupportsLowEnergy()) { |
| 910 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 935 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 911 return; | 936 return; |
| 912 } | 937 } |
| 913 InitWithFakeAdapter(); | 938 InitWithFakeAdapter(); |
| 914 StartLowEnergyDiscoverySession(); | 939 StartLowEnergyDiscoverySession(); |
| 915 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 940 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 916 | 941 |
| 917 // Create multiple connections and simulate connection complete: | 942 // Create multiple connections and simulate connection complete: |
| 918 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 943 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 919 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 944 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 920 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 945 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 921 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 946 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 922 SimulateGattConnection(device); | 947 SimulateGattConnection(device); |
| 948 base::RunLoop().RunUntilIdle(); |
| 923 | 949 |
| 924 // Delete all CreateGattConnection objects, observe disconnection: | 950 // Delete all CreateGattConnection objects, observe disconnection: |
| 925 ResetEventCounts(); | 951 ResetEventCounts(); |
| 926 gatt_connections_.clear(); | 952 gatt_connections_.clear(); |
| 927 EXPECT_EQ(1, gatt_disconnection_attempts_); | 953 EXPECT_EQ(1, gatt_disconnection_attempts_); |
| 928 } | 954 } |
| 929 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 955 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| 930 | 956 |
| 931 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 957 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 932 // Starts process of disconnecting and then calls BluetoothGattConnection. | 958 // Starts process of disconnecting and then calls BluetoothGattConnection. |
| 933 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectInProgress) { | 959 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectInProgress) { |
| 934 if (!PlatformSupportsLowEnergy()) { | 960 if (!PlatformSupportsLowEnergy()) { |
| 935 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 961 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 936 return; | 962 return; |
| 937 } | 963 } |
| 938 InitWithFakeAdapter(); | 964 InitWithFakeAdapter(); |
| 939 StartLowEnergyDiscoverySession(); | 965 StartLowEnergyDiscoverySession(); |
| 940 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 966 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 941 | 967 |
| 942 // Create multiple connections and simulate connection complete: | 968 // Create multiple connections and simulate connection complete: |
| 943 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 969 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 944 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 970 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 945 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 971 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 946 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 972 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 947 SimulateGattConnection(device); | 973 SimulateGattConnection(device); |
| 974 base::RunLoop().RunUntilIdle(); |
| 948 | 975 |
| 949 // Disconnect all CreateGattConnection objects & create a new connection. | 976 // Disconnect all CreateGattConnection objects & create a new connection. |
| 950 // But, don't yet simulate the device disconnecting: | 977 // But, don't yet simulate the device disconnecting: |
| 951 ResetEventCounts(); | 978 ResetEventCounts(); |
| 952 for (const auto& connection : gatt_connections_) | 979 for (const auto& connection : gatt_connections_) |
| 953 connection->Disconnect(); | 980 connection->Disconnect(); |
| 954 EXPECT_EQ(1, gatt_disconnection_attempts_); | 981 EXPECT_EQ(1, gatt_disconnection_attempts_); |
| 955 | 982 |
| 956 // Create a connection. | 983 // Create a connection. |
| 957 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 984 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 958 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 985 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 959 EXPECT_EQ(0, gatt_connection_attempts_); // No connection attempt. | 986 EXPECT_EQ(0, gatt_connection_attempts_); // No connection attempt. |
| 960 EXPECT_EQ(1, callback_count_); // Device is assumed still connected. | 987 EXPECT_EQ(1, callback_count_); // Device is assumed still connected. |
| 961 EXPECT_EQ(0, error_callback_count_); | 988 EXPECT_EQ(0, error_callback_count_); |
| 962 EXPECT_FALSE(gatt_connections_.front()->IsConnected()); | 989 EXPECT_FALSE(gatt_connections_.front()->IsConnected()); |
| 963 EXPECT_TRUE(gatt_connections_.back()->IsConnected()); | 990 EXPECT_TRUE(gatt_connections_.back()->IsConnected()); |
| 964 | 991 |
| 965 // Actually disconnect: | 992 // Actually disconnect: |
| 966 SimulateGattDisconnection(device); | 993 SimulateGattDisconnection(device); |
| 994 base::RunLoop().RunUntilIdle(); |
| 967 for (const auto& connection : gatt_connections_) | 995 for (const auto& connection : gatt_connections_) |
| 968 EXPECT_FALSE(connection->IsConnected()); | 996 EXPECT_FALSE(connection->IsConnected()); |
| 969 } | 997 } |
| 970 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 998 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| 971 | 999 |
| 972 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 1000 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 973 // Calls CreateGattConnection but receives notice that the device disconnected | 1001 // Calls CreateGattConnection but receives notice that the device disconnected |
| 974 // before it ever connects. | 1002 // before it ever connects. |
| 975 TEST_F(BluetoothTest, BluetoothGattConnection_SimulateDisconnect) { | 1003 TEST_F(BluetoothTest, BluetoothGattConnection_SimulateDisconnect) { |
| 976 if (!PlatformSupportsLowEnergy()) { | 1004 if (!PlatformSupportsLowEnergy()) { |
| 977 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 1005 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 978 return; | 1006 return; |
| 979 } | 1007 } |
| 980 InitWithFakeAdapter(); | 1008 InitWithFakeAdapter(); |
| 981 StartLowEnergyDiscoverySession(); | 1009 StartLowEnergyDiscoverySession(); |
| 982 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 1010 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 983 | 1011 |
| 984 ResetEventCounts(); | 1012 ResetEventCounts(); |
| 985 device->CreateGattConnection(GetGattConnectionCallback(Call::NOT_EXPECTED), | 1013 device->CreateGattConnection(GetGattConnectionCallback(Call::NOT_EXPECTED), |
| 986 GetConnectErrorCallback(Call::EXPECTED)); | 1014 GetConnectErrorCallback(Call::EXPECTED)); |
| 987 EXPECT_EQ(1, gatt_connection_attempts_); | 1015 EXPECT_EQ(1, gatt_connection_attempts_); |
| 988 SimulateGattDisconnection(device); | 1016 SimulateGattDisconnection(device); |
| 1017 base::RunLoop().RunUntilIdle(); |
| 989 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_); | 1018 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_); |
| 990 for (const auto& connection : gatt_connections_) | 1019 for (const auto& connection : gatt_connections_) |
| 991 EXPECT_FALSE(connection->IsConnected()); | 1020 EXPECT_FALSE(connection->IsConnected()); |
| 992 } | 1021 } |
| 993 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 1022 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| 994 | 1023 |
| 995 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 1024 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 996 // Calls CreateGattConnection & DisconnectGatt, then simulates connection. | 1025 // Calls CreateGattConnection & DisconnectGatt, then simulates connection. |
| 997 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectGatt_SimulateConnect) { | 1026 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectGatt_SimulateConnect) { |
| 998 if (!PlatformSupportsLowEnergy()) { | 1027 if (!PlatformSupportsLowEnergy()) { |
| 999 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 1028 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 1000 return; | 1029 return; |
| 1001 } | 1030 } |
| 1002 InitWithFakeAdapter(); | 1031 InitWithFakeAdapter(); |
| 1003 StartLowEnergyDiscoverySession(); | 1032 StartLowEnergyDiscoverySession(); |
| 1004 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 1033 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 1005 | 1034 |
| 1006 ResetEventCounts(); | 1035 ResetEventCounts(); |
| 1007 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 1036 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 1008 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 1037 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 1009 device->DisconnectGatt(); | 1038 device->DisconnectGatt(); |
| 1010 EXPECT_EQ(1, gatt_connection_attempts_); | 1039 EXPECT_EQ(1, gatt_connection_attempts_); |
| 1011 EXPECT_EQ(1, gatt_disconnection_attempts_); | 1040 EXPECT_EQ(1, gatt_disconnection_attempts_); |
| 1041 |
| 1012 SimulateGattConnection(device); | 1042 SimulateGattConnection(device); |
| 1043 base::RunLoop().RunUntilIdle(); |
| 1044 |
| 1013 EXPECT_EQ(1, callback_count_); | 1045 EXPECT_EQ(1, callback_count_); |
| 1014 EXPECT_EQ(0, error_callback_count_); | 1046 EXPECT_EQ(0, error_callback_count_); |
| 1015 EXPECT_TRUE(gatt_connections_.back()->IsConnected()); | 1047 EXPECT_TRUE(gatt_connections_.back()->IsConnected()); |
| 1016 ResetEventCounts(); | 1048 ResetEventCounts(); |
| 1017 SimulateGattDisconnection(device); | 1049 SimulateGattDisconnection(device); |
| 1050 base::RunLoop().RunUntilIdle(); |
| 1018 EXPECT_EQ(0, callback_count_); | 1051 EXPECT_EQ(0, callback_count_); |
| 1019 EXPECT_EQ(0, error_callback_count_); | 1052 EXPECT_EQ(0, error_callback_count_); |
| 1020 } | 1053 } |
| 1021 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 1054 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| 1022 | 1055 |
| 1023 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 1056 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 1024 // Calls CreateGattConnection & DisconnectGatt, then simulates disconnection. | 1057 // Calls CreateGattConnection & DisconnectGatt, then simulates disconnection. |
| 1025 TEST_F(BluetoothTest, | 1058 TEST_F(BluetoothTest, |
| 1026 BluetoothGattConnection_DisconnectGatt_SimulateDisconnect) { | 1059 BluetoothGattConnection_DisconnectGatt_SimulateDisconnect) { |
| 1027 if (!PlatformSupportsLowEnergy()) { | 1060 if (!PlatformSupportsLowEnergy()) { |
| 1028 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 1061 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 1029 return; | 1062 return; |
| 1030 } | 1063 } |
| 1031 InitWithFakeAdapter(); | 1064 InitWithFakeAdapter(); |
| 1032 StartLowEnergyDiscoverySession(); | 1065 StartLowEnergyDiscoverySession(); |
| 1033 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 1066 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 1034 | 1067 |
| 1035 ResetEventCounts(); | 1068 ResetEventCounts(); |
| 1036 device->CreateGattConnection(GetGattConnectionCallback(Call::NOT_EXPECTED), | 1069 device->CreateGattConnection(GetGattConnectionCallback(Call::NOT_EXPECTED), |
| 1037 GetConnectErrorCallback(Call::EXPECTED)); | 1070 GetConnectErrorCallback(Call::EXPECTED)); |
| 1038 device->DisconnectGatt(); | 1071 device->DisconnectGatt(); |
| 1039 EXPECT_EQ(1, gatt_connection_attempts_); | 1072 EXPECT_EQ(1, gatt_connection_attempts_); |
| 1040 EXPECT_EQ(1, gatt_disconnection_attempts_); | 1073 EXPECT_EQ(1, gatt_disconnection_attempts_); |
| 1041 SimulateGattDisconnection(device); | 1074 SimulateGattDisconnection(device); |
| 1075 base::RunLoop().RunUntilIdle(); |
| 1042 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_); | 1076 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_); |
| 1043 for (const auto& connection : gatt_connections_) | 1077 for (const auto& connection : gatt_connections_) |
| 1044 EXPECT_FALSE(connection->IsConnected()); | 1078 EXPECT_FALSE(connection->IsConnected()); |
| 1045 } | 1079 } |
| 1046 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 1080 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| 1047 | 1081 |
| 1048 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 1082 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 1049 // Calls CreateGattConnection & DisconnectGatt, then checks that gatt services | 1083 // Calls CreateGattConnection & DisconnectGatt, then checks that gatt services |
| 1050 // have been cleaned up. | 1084 // have been cleaned up. |
| 1051 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectGatt_Cleanup) { | 1085 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectGatt_Cleanup) { |
| 1052 if (!PlatformSupportsLowEnergy()) { | 1086 if (!PlatformSupportsLowEnergy()) { |
| 1053 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 1087 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 1054 return; | 1088 return; |
| 1055 } | 1089 } |
| 1056 InitWithFakeAdapter(); | 1090 InitWithFakeAdapter(); |
| 1057 StartLowEnergyDiscoverySession(); | 1091 StartLowEnergyDiscoverySession(); |
| 1058 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 1092 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 1059 EXPECT_FALSE(device->IsConnected()); | 1093 EXPECT_FALSE(device->IsConnected()); |
| 1060 | 1094 |
| 1061 // Connect to the device | 1095 // Connect to the device |
| 1062 ResetEventCounts(); | 1096 ResetEventCounts(); |
| 1063 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 1097 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 1064 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 1098 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 1065 TestBluetoothAdapterObserver observer(adapter_); | 1099 TestBluetoothAdapterObserver observer(adapter_); |
| 1066 SimulateGattConnection(device); | 1100 SimulateGattConnection(device); |
| 1101 base::RunLoop().RunUntilIdle(); |
| 1067 EXPECT_TRUE(device->IsConnected()); | 1102 EXPECT_TRUE(device->IsConnected()); |
| 1068 | 1103 |
| 1069 // Discover services | 1104 // Discover services |
| 1070 std::vector<std::string> services; | 1105 std::vector<std::string> services; |
| 1071 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 1106 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
| 1072 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 1107 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); |
| 1073 SimulateGattServicesDiscovered(device, services); | 1108 SimulateGattServicesDiscovered(device, services); |
| 1109 base::RunLoop().RunUntilIdle(); |
| 1074 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); | 1110 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); |
| 1075 EXPECT_EQ(2u, device->GetGattServices().size()); | 1111 EXPECT_EQ(2u, device->GetGattServices().size()); |
| 1076 EXPECT_EQ(1, observer.gatt_services_discovered_count()); | 1112 EXPECT_EQ(1, observer.gatt_services_discovered_count()); |
| 1077 | 1113 |
| 1078 // Disconnect from the device | 1114 // Disconnect from the device |
| 1079 device->DisconnectGatt(); | 1115 device->DisconnectGatt(); |
| 1080 SimulateGattDisconnection(device); | 1116 SimulateGattDisconnection(device); |
| 1117 base::RunLoop().RunUntilIdle(); |
| 1081 EXPECT_FALSE(device->IsConnected()); | 1118 EXPECT_FALSE(device->IsConnected()); |
| 1082 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); | 1119 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); |
| 1083 EXPECT_EQ(0u, device->GetGattServices().size()); | 1120 EXPECT_EQ(0u, device->GetGattServices().size()); |
| 1084 | 1121 |
| 1085 // Verify that the device can be connected to again | 1122 // Verify that the device can be connected to again |
| 1086 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 1123 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 1087 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 1124 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 1088 SimulateGattConnection(device); | 1125 SimulateGattConnection(device); |
| 1126 base::RunLoop().RunUntilIdle(); |
| 1089 EXPECT_TRUE(device->IsConnected()); | 1127 EXPECT_TRUE(device->IsConnected()); |
| 1090 | 1128 |
| 1091 // Verify that service discovery can be done again | 1129 // Verify that service discovery can be done again |
| 1092 std::vector<std::string> services2; | 1130 std::vector<std::string> services2; |
| 1093 services2.push_back("00000002-0000-1000-8000-00805f9b34fb"); | 1131 services2.push_back("00000002-0000-1000-8000-00805f9b34fb"); |
| 1094 SimulateGattServicesDiscovered(device, services2); | 1132 SimulateGattServicesDiscovered(device, services2); |
| 1133 base::RunLoop().RunUntilIdle(); |
| 1095 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); | 1134 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); |
| 1096 EXPECT_EQ(1u, device->GetGattServices().size()); | 1135 EXPECT_EQ(1u, device->GetGattServices().size()); |
| 1097 EXPECT_EQ(2, observer.gatt_services_discovered_count()); | 1136 EXPECT_EQ(2, observer.gatt_services_discovered_count()); |
| 1098 } | 1137 } |
| 1099 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 1138 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| 1100 | 1139 |
| 1101 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 1140 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 1102 // Calls CreateGattConnection, but simulate errors connecting. Also, verifies | 1141 // Calls CreateGattConnection, but simulate errors connecting. Also, verifies |
| 1103 // multiple errors should only invoke callbacks once. | 1142 // multiple errors should only invoke callbacks once. |
| 1104 TEST_F(BluetoothTest, BluetoothGattConnection_ErrorAfterConnection) { | 1143 TEST_F(BluetoothTest, BluetoothGattConnection_ErrorAfterConnection) { |
| 1105 if (!PlatformSupportsLowEnergy()) { | 1144 if (!PlatformSupportsLowEnergy()) { |
| 1106 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 1145 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 1107 return; | 1146 return; |
| 1108 } | 1147 } |
| 1109 InitWithFakeAdapter(); | 1148 InitWithFakeAdapter(); |
| 1110 StartLowEnergyDiscoverySession(); | 1149 StartLowEnergyDiscoverySession(); |
| 1111 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 1150 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 1112 | 1151 |
| 1113 ResetEventCounts(); | 1152 ResetEventCounts(); |
| 1114 device->CreateGattConnection(GetGattConnectionCallback(Call::NOT_EXPECTED), | 1153 device->CreateGattConnection(GetGattConnectionCallback(Call::NOT_EXPECTED), |
| 1115 GetConnectErrorCallback(Call::EXPECTED)); | 1154 GetConnectErrorCallback(Call::EXPECTED)); |
| 1116 EXPECT_EQ(1, gatt_connection_attempts_); | 1155 EXPECT_EQ(1, gatt_connection_attempts_); |
| 1117 SimulateGattConnectionError(device, BluetoothDevice::ERROR_AUTH_FAILED); | 1156 SimulateGattConnectionError(device, BluetoothDevice::ERROR_AUTH_FAILED); |
| 1118 SimulateGattConnectionError(device, BluetoothDevice::ERROR_FAILED); | 1157 SimulateGattConnectionError(device, BluetoothDevice::ERROR_FAILED); |
| 1158 base::RunLoop().RunUntilIdle(); |
| 1119 #if defined(OS_ANDROID) | 1159 #if defined(OS_ANDROID) |
| 1120 // TODO: Change to ERROR_AUTH_FAILED. We should be getting a callback | 1160 // TODO: Change to ERROR_AUTH_FAILED. We should be getting a callback |
| 1121 // only with the first error, but our android framework doesn't yet | 1161 // only with the first error, but our android framework doesn't yet |
| 1122 // support sending different errors. | 1162 // support sending different errors. |
| 1123 // http://crbug.com/578191 | 1163 // http://crbug.com/578191 |
| 1124 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_); | 1164 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_); |
| 1125 #else | 1165 #else |
| 1126 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_FAILED, last_connect_error_code_); | 1166 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_FAILED, last_connect_error_code_); |
| 1127 #endif | 1167 #endif |
| 1128 for (const auto& connection : gatt_connections_) | 1168 for (const auto& connection : gatt_connections_) |
| 1129 EXPECT_FALSE(connection->IsConnected()); | 1169 EXPECT_FALSE(connection->IsConnected()); |
| 1130 } | 1170 } |
| 1131 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 1171 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| 1132 | 1172 |
| 1133 #if defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_MACOSX) | 1173 #if defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_MACOSX) |
| 1134 TEST_F(BluetoothTest, GattServices_ObserversCalls) { | 1174 TEST_F(BluetoothTest, GattServices_ObserversCalls) { |
| 1135 if (!PlatformSupportsLowEnergy()) { | 1175 if (!PlatformSupportsLowEnergy()) { |
| 1136 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 1176 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 1137 return; | 1177 return; |
| 1138 } | 1178 } |
| 1139 InitWithFakeAdapter(); | 1179 InitWithFakeAdapter(); |
| 1140 StartLowEnergyDiscoverySession(); | 1180 StartLowEnergyDiscoverySession(); |
| 1141 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 1181 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 1142 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 1182 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 1143 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 1183 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 1144 TestBluetoothAdapterObserver observer(adapter_); | 1184 TestBluetoothAdapterObserver observer(adapter_); |
| 1145 ResetEventCounts(); | 1185 ResetEventCounts(); |
| 1146 SimulateGattConnection(device); | 1186 SimulateGattConnection(device); |
| 1187 base::RunLoop().RunUntilIdle(); |
| 1147 EXPECT_EQ(1, gatt_discovery_attempts_); | 1188 EXPECT_EQ(1, gatt_discovery_attempts_); |
| 1148 | 1189 |
| 1149 std::vector<std::string> services; | 1190 std::vector<std::string> services; |
| 1150 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 1191 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
| 1151 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 1192 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); |
| 1152 SimulateGattServicesDiscovered(device, services); | 1193 SimulateGattServicesDiscovered(device, services); |
| 1194 base::RunLoop().RunUntilIdle(); |
| 1153 | 1195 |
| 1154 EXPECT_EQ(1, observer.gatt_services_discovered_count()); | 1196 EXPECT_EQ(1, observer.gatt_services_discovered_count()); |
| 1155 EXPECT_EQ(2, observer.gatt_service_added_count()); | 1197 EXPECT_EQ(2, observer.gatt_service_added_count()); |
| 1156 } | 1198 } |
| 1157 #endif // defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_MACOSX) | 1199 #endif // defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_MACOSX) |
| 1158 | 1200 |
| 1159 #if defined(OS_ANDROID) | 1201 #if defined(OS_ANDROID) |
| 1160 // macOS: Not applicable: This can never happen because when | 1202 // macOS: Not applicable: This can never happen because when |
| 1161 // the device gets destroyed the CBPeripheralDelegate is also destroyed | 1203 // the device gets destroyed the CBPeripheralDelegate is also destroyed |
| 1162 // and no more events are dispatched. | 1204 // and no more events are dispatched. |
| 1163 TEST_F(BluetoothTest, GattServicesDiscovered_AfterDeleted) { | 1205 TEST_F(BluetoothTest, GattServicesDiscovered_AfterDeleted) { |
| 1164 // Tests that we don't crash if services are discovered after | 1206 // Tests that we don't crash if services are discovered after |
| 1165 // the device object is deleted. | 1207 // the device object is deleted. |
| 1166 if (!PlatformSupportsLowEnergy()) { | 1208 if (!PlatformSupportsLowEnergy()) { |
| 1167 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 1209 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 1168 return; | 1210 return; |
| 1169 } | 1211 } |
| 1170 InitWithFakeAdapter(); | 1212 InitWithFakeAdapter(); |
| 1171 StartLowEnergyDiscoverySession(); | 1213 StartLowEnergyDiscoverySession(); |
| 1172 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 1214 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 1173 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 1215 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 1174 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 1216 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 1175 ResetEventCounts(); | 1217 ResetEventCounts(); |
| 1176 SimulateGattConnection(device); | 1218 SimulateGattConnection(device); |
| 1219 base::RunLoop().RunUntilIdle(); |
| 1177 EXPECT_EQ(1, gatt_discovery_attempts_); | 1220 EXPECT_EQ(1, gatt_discovery_attempts_); |
| 1178 | 1221 |
| 1179 RememberDeviceForSubsequentAction(device); | 1222 RememberDeviceForSubsequentAction(device); |
| 1180 DeleteDevice(device); | 1223 DeleteDevice(device); |
| 1181 | 1224 |
| 1182 std::vector<std::string> services; | 1225 std::vector<std::string> services; |
| 1183 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 1226 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
| 1184 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 1227 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); |
| 1185 SimulateGattServicesDiscovered(nullptr /* use remembered device */, services); | 1228 SimulateGattServicesDiscovered(nullptr /* use remembered device */, services); |
| 1229 base::RunLoop().RunUntilIdle(); |
| 1186 } | 1230 } |
| 1187 #endif // defined(OS_ANDROID) | 1231 #endif // defined(OS_ANDROID) |
| 1188 | 1232 |
| 1189 #if defined(OS_ANDROID) | 1233 #if defined(OS_ANDROID) |
| 1190 // macOS: Not applicable: This can never happen because when | 1234 // macOS: Not applicable: This can never happen because when |
| 1191 // the device gets destroyed the CBPeripheralDelegate is also destroyed | 1235 // the device gets destroyed the CBPeripheralDelegate is also destroyed |
| 1192 // and no more events are dispatched. | 1236 // and no more events are dispatched. |
| 1193 TEST_F(BluetoothTest, GattServicesDiscoveredError_AfterDeleted) { | 1237 TEST_F(BluetoothTest, GattServicesDiscoveredError_AfterDeleted) { |
| 1194 // Tests that we don't crash if there was an error discoverying services | 1238 // Tests that we don't crash if there was an error discoverying services |
| 1195 // after the device object is deleted. | 1239 // after the device object is deleted. |
| 1196 if (!PlatformSupportsLowEnergy()) { | 1240 if (!PlatformSupportsLowEnergy()) { |
| 1197 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 1241 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 1198 return; | 1242 return; |
| 1199 } | 1243 } |
| 1200 InitWithFakeAdapter(); | 1244 InitWithFakeAdapter(); |
| 1201 StartLowEnergyDiscoverySession(); | 1245 StartLowEnergyDiscoverySession(); |
| 1202 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 1246 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 1203 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 1247 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 1204 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 1248 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 1205 ResetEventCounts(); | 1249 ResetEventCounts(); |
| 1206 SimulateGattConnection(device); | 1250 SimulateGattConnection(device); |
| 1251 base::RunLoop().RunUntilIdle(); |
| 1207 EXPECT_EQ(1, gatt_discovery_attempts_); | 1252 EXPECT_EQ(1, gatt_discovery_attempts_); |
| 1208 | 1253 |
| 1209 RememberDeviceForSubsequentAction(device); | 1254 RememberDeviceForSubsequentAction(device); |
| 1210 DeleteDevice(device); | 1255 DeleteDevice(device); |
| 1211 | 1256 |
| 1212 SimulateGattServicesDiscoveryError(nullptr /* use remembered device */); | 1257 SimulateGattServicesDiscoveryError(nullptr /* use remembered device */); |
| 1258 base::RunLoop().RunUntilIdle(); |
| 1213 } | 1259 } |
| 1214 #endif // defined(OS_ANDROID) | 1260 #endif // defined(OS_ANDROID) |
| 1215 | 1261 |
| 1216 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 1262 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 1217 TEST_F(BluetoothTest, GattServicesDiscovered_AfterDisconnection) { | 1263 TEST_F(BluetoothTest, GattServicesDiscovered_AfterDisconnection) { |
| 1218 // Tests that we don't crash there was an error discovering services after | 1264 // Tests that we don't crash there was an error discovering services after |
| 1219 // the device disconnects. | 1265 // the device disconnects. |
| 1220 if (!PlatformSupportsLowEnergy()) { | 1266 if (!PlatformSupportsLowEnergy()) { |
| 1221 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 1267 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 1222 return; | 1268 return; |
| 1223 } | 1269 } |
| 1224 InitWithFakeAdapter(); | 1270 InitWithFakeAdapter(); |
| 1225 StartLowEnergyDiscoverySession(); | 1271 StartLowEnergyDiscoverySession(); |
| 1226 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 1272 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 1227 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 1273 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 1228 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 1274 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 1229 ResetEventCounts(); | 1275 ResetEventCounts(); |
| 1230 SimulateGattConnection(device); | 1276 SimulateGattConnection(device); |
| 1277 base::RunLoop().RunUntilIdle(); |
| 1231 EXPECT_EQ(1, gatt_discovery_attempts_); | 1278 EXPECT_EQ(1, gatt_discovery_attempts_); |
| 1232 | 1279 |
| 1233 SimulateGattDisconnection(device); | 1280 SimulateGattDisconnection(device); |
| 1281 base::RunLoop().RunUntilIdle(); |
| 1234 | 1282 |
| 1235 std::vector<std::string> services; | 1283 std::vector<std::string> services; |
| 1236 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 1284 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
| 1237 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 1285 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); |
| 1238 SimulateGattServicesDiscovered(device, services); | 1286 SimulateGattServicesDiscovered(device, services); |
| 1287 base::RunLoop().RunUntilIdle(); |
| 1239 | 1288 |
| 1240 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); | 1289 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); |
| 1241 EXPECT_EQ(0u, device->GetGattServices().size()); | 1290 EXPECT_EQ(0u, device->GetGattServices().size()); |
| 1242 } | 1291 } |
| 1243 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 1292 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| 1244 | 1293 |
| 1245 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 1294 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 1246 TEST_F(BluetoothTest, GattServicesDiscoveredError_AfterDisconnection) { | 1295 TEST_F(BluetoothTest, GattServicesDiscoveredError_AfterDisconnection) { |
| 1247 // Tests that we don't crash if services are discovered after | 1296 // Tests that we don't crash if services are discovered after |
| 1248 // the device disconnects. | 1297 // the device disconnects. |
| 1249 if (!PlatformSupportsLowEnergy()) { | 1298 if (!PlatformSupportsLowEnergy()) { |
| 1250 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 1299 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 1251 return; | 1300 return; |
| 1252 } | 1301 } |
| 1253 InitWithFakeAdapter(); | 1302 InitWithFakeAdapter(); |
| 1254 StartLowEnergyDiscoverySession(); | 1303 StartLowEnergyDiscoverySession(); |
| 1255 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 1304 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 1256 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 1305 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 1257 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 1306 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 1258 ResetEventCounts(); | 1307 ResetEventCounts(); |
| 1259 SimulateGattConnection(device); | 1308 SimulateGattConnection(device); |
| 1309 base::RunLoop().RunUntilIdle(); |
| 1260 EXPECT_EQ(1, gatt_discovery_attempts_); | 1310 EXPECT_EQ(1, gatt_discovery_attempts_); |
| 1261 | 1311 |
| 1262 SimulateGattDisconnection(device); | 1312 SimulateGattDisconnection(device); |
| 1313 base::RunLoop().RunUntilIdle(); |
| 1263 | 1314 |
| 1264 SimulateGattServicesDiscoveryError(device); | 1315 SimulateGattServicesDiscoveryError(device); |
| 1316 base::RunLoop().RunUntilIdle(); |
| 1265 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); | 1317 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); |
| 1266 EXPECT_EQ(0u, device->GetGattServices().size()); | 1318 EXPECT_EQ(0u, device->GetGattServices().size()); |
| 1267 } | 1319 } |
| 1268 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 1320 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| 1269 | 1321 |
| 1270 #if defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_MACOSX) | 1322 #if defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_MACOSX) |
| 1271 TEST_F(BluetoothTest, GetGattServices_and_GetGattService) { | 1323 TEST_F(BluetoothTest, GetGattServices_and_GetGattService) { |
| 1272 if (!PlatformSupportsLowEnergy()) { | 1324 if (!PlatformSupportsLowEnergy()) { |
| 1273 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 1325 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 1274 return; | 1326 return; |
| 1275 } | 1327 } |
| 1276 InitWithFakeAdapter(); | 1328 InitWithFakeAdapter(); |
| 1277 StartLowEnergyDiscoverySession(); | 1329 StartLowEnergyDiscoverySession(); |
| 1278 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 1330 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 1279 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 1331 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 1280 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 1332 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 1281 ResetEventCounts(); | 1333 ResetEventCounts(); |
| 1282 SimulateGattConnection(device); | 1334 SimulateGattConnection(device); |
| 1335 base::RunLoop().RunUntilIdle(); |
| 1283 EXPECT_EQ(1, gatt_discovery_attempts_); | 1336 EXPECT_EQ(1, gatt_discovery_attempts_); |
| 1284 | 1337 |
| 1285 std::vector<std::string> services; | 1338 std::vector<std::string> services; |
| 1286 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 1339 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
| 1287 // 2 duplicate UUIDs creating 2 instances. | 1340 // 2 duplicate UUIDs creating 2 instances. |
| 1288 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 1341 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); |
| 1289 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 1342 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); |
| 1290 SimulateGattServicesDiscovered(device, services); | 1343 SimulateGattServicesDiscovered(device, services); |
| 1344 base::RunLoop().RunUntilIdle(); |
| 1291 EXPECT_EQ(3u, device->GetGattServices().size()); | 1345 EXPECT_EQ(3u, device->GetGattServices().size()); |
| 1292 | 1346 |
| 1293 // Test GetGattService: | 1347 // Test GetGattService: |
| 1294 std::string service_id1 = device->GetGattServices()[0]->GetIdentifier(); | 1348 std::string service_id1 = device->GetGattServices()[0]->GetIdentifier(); |
| 1295 std::string service_id2 = device->GetGattServices()[1]->GetIdentifier(); | 1349 std::string service_id2 = device->GetGattServices()[1]->GetIdentifier(); |
| 1296 std::string service_id3 = device->GetGattServices()[2]->GetIdentifier(); | 1350 std::string service_id3 = device->GetGattServices()[2]->GetIdentifier(); |
| 1297 EXPECT_TRUE(device->GetGattService(service_id1)); | 1351 EXPECT_TRUE(device->GetGattService(service_id1)); |
| 1298 EXPECT_TRUE(device->GetGattService(service_id2)); | 1352 EXPECT_TRUE(device->GetGattService(service_id2)); |
| 1299 EXPECT_TRUE(device->GetGattService(service_id3)); | 1353 EXPECT_TRUE(device->GetGattService(service_id3)); |
| 1300 } | 1354 } |
| 1301 #endif // defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_MACOSX) | 1355 #endif // defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_MACOSX) |
| 1302 | 1356 |
| 1303 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 1357 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 1304 TEST_F(BluetoothTest, GetGattServices_DiscoveryError) { | 1358 TEST_F(BluetoothTest, GetGattServices_DiscoveryError) { |
| 1305 if (!PlatformSupportsLowEnergy()) { | 1359 if (!PlatformSupportsLowEnergy()) { |
| 1306 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 1360 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 1307 return; | 1361 return; |
| 1308 } | 1362 } |
| 1309 InitWithFakeAdapter(); | 1363 InitWithFakeAdapter(); |
| 1310 StartLowEnergyDiscoverySession(); | 1364 StartLowEnergyDiscoverySession(); |
| 1311 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 1365 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 1312 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 1366 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 1313 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 1367 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 1314 ResetEventCounts(); | 1368 ResetEventCounts(); |
| 1315 SimulateGattConnection(device); | 1369 SimulateGattConnection(device); |
| 1370 base::RunLoop().RunUntilIdle(); |
| 1316 EXPECT_EQ(1, gatt_discovery_attempts_); | 1371 EXPECT_EQ(1, gatt_discovery_attempts_); |
| 1317 | 1372 |
| 1318 SimulateGattServicesDiscoveryError(device); | 1373 SimulateGattServicesDiscoveryError(device); |
| 1374 base::RunLoop().RunUntilIdle(); |
| 1319 EXPECT_EQ(0u, device->GetGattServices().size()); | 1375 EXPECT_EQ(0u, device->GetGattServices().size()); |
| 1320 } | 1376 } |
| 1321 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 1377 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| 1322 | 1378 |
| 1323 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | 1379 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 1324 TEST_F(BluetoothTest, GetDeviceTransportType) { | 1380 TEST_F(BluetoothTest, GetDeviceTransportType) { |
| 1325 if (!PlatformSupportsLowEnergy()) { | 1381 if (!PlatformSupportsLowEnergy()) { |
| 1326 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 1382 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 1327 return; | 1383 return; |
| 1328 } | 1384 } |
| 1329 InitWithFakeAdapter(); | 1385 InitWithFakeAdapter(); |
| 1330 BluetoothDevice* device = SimulateLowEnergyDevice(1); | 1386 BluetoothDevice* device = SimulateLowEnergyDevice(1); |
| 1331 EXPECT_EQ(BLUETOOTH_TRANSPORT_LE, device->GetType()); | 1387 EXPECT_EQ(BLUETOOTH_TRANSPORT_LE, device->GetType()); |
| 1332 | 1388 |
| 1333 BluetoothDevice* device2 = SimulateLowEnergyDevice(6); | 1389 BluetoothDevice* device2 = SimulateLowEnergyDevice(6); |
| 1334 EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, device2->GetType()); | 1390 EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, device2->GetType()); |
| 1335 | 1391 |
| 1336 BluetoothDevice* device3 = SimulateClassicDevice(); | 1392 BluetoothDevice* device3 = SimulateClassicDevice(); |
| 1337 EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, device3->GetType()); | 1393 EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, device3->GetType()); |
| 1338 } | 1394 } |
| 1339 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 1395 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 1340 | 1396 |
| 1341 } // namespace device | 1397 } // namespace device |
| OLD | NEW |