| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/memory/ref_counted.h" | 6 #include "base/memory/ref_counted.h" |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "device/bluetooth/bluetooth_adapter.h" | 8 #include "device/bluetooth/bluetooth_adapter.h" |
| 9 #include "device/bluetooth/bluetooth_device.h" | 9 #include "device/bluetooth/bluetooth_device.h" |
| 10 #include "device/bluetooth/bluetooth_discovery_session.h" | 10 #include "device/bluetooth/bluetooth_discovery_session.h" |
| (...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 | 465 |
| 466 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback()); | 466 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback()); |
| 467 base::RunLoop().RunUntilIdle(); | 467 base::RunLoop().RunUntilIdle(); |
| 468 EXPECT_EQ(1, callback_count_--); | 468 EXPECT_EQ(1, callback_count_--); |
| 469 EXPECT_EQ(0, error_callback_count_); | 469 EXPECT_EQ(0, error_callback_count_); |
| 470 EXPECT_FALSE(adapter_->IsDiscovering()); | 470 EXPECT_FALSE(adapter_->IsDiscovering()); |
| 471 EXPECT_FALSE(discovery_sessions_[0]->IsActive()); | 471 EXPECT_FALSE(discovery_sessions_[0]->IsActive()); |
| 472 } | 472 } |
| 473 #endif // defined(OS_ANDROID) | 473 #endif // defined(OS_ANDROID) |
| 474 | 474 |
| 475 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 475 #if defined(OS_ANDROID) |
| 476 // Discovers a device. | 476 // Discovers a device. |
| 477 TEST_F(BluetoothTest, DiscoverLowEnergyDevice) { | 477 TEST_F(BluetoothTest, DiscoverDevice) { |
| 478 if (!PlatformSupportsLowEnergy()) { | |
| 479 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | |
| 480 return; | |
| 481 } | |
| 482 InitWithFakeAdapter(); | 478 InitWithFakeAdapter(); |
| 483 TestBluetoothAdapterObserver observer(adapter_); | 479 TestBluetoothAdapterObserver observer(adapter_); |
| 484 | 480 |
| 485 // Start discovery and find a device. | 481 // Start discovery and find a device. |
| 486 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( | 482 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(), |
| 487 new BluetoothDiscoveryFilter( | 483 GetErrorCallback()); |
| 488 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); | |
| 489 adapter_->StartDiscoverySessionWithFilter(discovery_filter.Pass(), | |
| 490 GetDiscoverySessionCallback(), | |
| 491 GetErrorCallback()); | |
| 492 base::RunLoop().RunUntilIdle(); | 484 base::RunLoop().RunUntilIdle(); |
| 493 DiscoverLowEnergyDevice(1); | 485 DiscoverLowEnergyDevice(1); |
| 494 base::RunLoop().RunUntilIdle(); | 486 base::RunLoop().RunUntilIdle(); |
| 495 EXPECT_EQ(1, observer.device_added_count()); | 487 EXPECT_EQ(1, observer.device_added_count()); |
| 496 BluetoothDevice* device = adapter_->GetDevice(observer.last_device_address()); | 488 BluetoothDevice* device = adapter_->GetDevice(observer.last_device_address()); |
| 497 EXPECT_TRUE(device); | 489 EXPECT_TRUE(device); |
| 498 } | 490 } |
| 499 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 491 #endif // defined(OS_ANDROID) |
| 500 | 492 |
| 501 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 493 #if defined(OS_ANDROID) |
| 502 // Discovers the same device multiple times. | 494 // Discovers the same device multiple times. |
| 503 TEST_F(BluetoothTest, DiscoverLowEnergyDeviceTwice) { | 495 TEST_F(BluetoothTest, DiscoverDeviceTwice) { |
| 504 if (!PlatformSupportsLowEnergy()) { | |
| 505 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | |
| 506 return; | |
| 507 } | |
| 508 InitWithFakeAdapter(); | 496 InitWithFakeAdapter(); |
| 509 TestBluetoothAdapterObserver observer(adapter_); | 497 TestBluetoothAdapterObserver observer(adapter_); |
| 510 | 498 |
| 511 // Start discovery and find a device. | 499 // Start discovery and find a device. |
| 512 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(), | 500 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(), |
| 513 GetErrorCallback()); | 501 GetErrorCallback()); |
| 514 base::RunLoop().RunUntilIdle(); | 502 base::RunLoop().RunUntilIdle(); |
| 515 DiscoverLowEnergyDevice(1); | 503 DiscoverLowEnergyDevice(1); |
| 516 base::RunLoop().RunUntilIdle(); | 504 base::RunLoop().RunUntilIdle(); |
| 517 EXPECT_EQ(1, observer.device_added_count()); | 505 EXPECT_EQ(1, observer.device_added_count()); |
| 518 BluetoothDevice* device = adapter_->GetDevice(observer.last_device_address()); | 506 BluetoothDevice* device = adapter_->GetDevice(observer.last_device_address()); |
| 519 EXPECT_TRUE(device); | 507 EXPECT_TRUE(device); |
| 520 | 508 |
| 521 // Find the same device again. This should not create a new device object. | 509 // Find the same device again. This should not create a new device object. |
| 522 observer.Reset(); | 510 observer.Reset(); |
| 523 DiscoverLowEnergyDevice(1); | 511 DiscoverLowEnergyDevice(1); |
| 524 base::RunLoop().RunUntilIdle(); | 512 base::RunLoop().RunUntilIdle(); |
| 525 EXPECT_EQ(0, observer.device_added_count()); | 513 EXPECT_EQ(0, observer.device_added_count()); |
| 514 EXPECT_EQ(0, observer.device_changed_count()); |
| 526 EXPECT_EQ(1u, adapter_->GetDevices().size()); | 515 EXPECT_EQ(1u, adapter_->GetDevices().size()); |
| 527 } | 516 } |
| 528 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 517 #endif // defined(OS_ANDROID) |
| 529 | 518 |
| 530 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 519 #if defined(OS_ANDROID) |
| 531 // Discovers a device, and then again with new Service UUIDs. | 520 // Discovers a device, and then again with new Service UUIDs. |
| 532 TEST_F(BluetoothTest, DiscoverLowEnergyDeviceWithUpdatedUUIDs) { | 521 TEST_F(BluetoothTest, DiscoverDeviceWithUpdatedUUIDs) { |
| 533 if (!PlatformSupportsLowEnergy()) { | |
| 534 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | |
| 535 return; | |
| 536 } | |
| 537 InitWithFakeAdapter(); | 522 InitWithFakeAdapter(); |
| 538 TestBluetoothAdapterObserver observer(adapter_); | 523 TestBluetoothAdapterObserver observer(adapter_); |
| 539 | 524 |
| 540 // Start discovery and find a device. | 525 // Start discovery and find a device. |
| 541 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(), | 526 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(), |
| 542 GetErrorCallback()); | 527 GetErrorCallback()); |
| 543 base::RunLoop().RunUntilIdle(); | 528 base::RunLoop().RunUntilIdle(); |
| 544 DiscoverLowEnergyDevice(1); | 529 DiscoverLowEnergyDevice(1); |
| 545 base::RunLoop().RunUntilIdle(); | 530 base::RunLoop().RunUntilIdle(); |
| 546 BluetoothDevice* device = observer.last_device(); | 531 BluetoothDevice* device = observer.last_device(); |
| 547 | 532 |
| 548 // Check the initial UUIDs: | 533 // Check the initial UUIDs: |
| 549 EXPECT_TRUE( | 534 EXPECT_TRUE(ContainsValue(device->GetUUIDs(), BluetoothUUID("1800"))); |
| 550 ContainsValue(device->GetUUIDs(), BluetoothUUID(kTestUUIDGenericAccess))); | 535 EXPECT_FALSE(ContainsValue(device->GetUUIDs(), BluetoothUUID("1802"))); |
| 551 EXPECT_FALSE(ContainsValue(device->GetUUIDs(), | |
| 552 BluetoothUUID(kTestUUIDImmediateAlert))); | |
| 553 | 536 |
| 554 // Discover same device again with updated UUIDs: | 537 // Discover same device again with updated UUIDs: |
| 555 observer.Reset(); | 538 observer.Reset(); |
| 556 DiscoverLowEnergyDevice(2); | 539 DiscoverLowEnergyDevice(2); |
| 557 base::RunLoop().RunUntilIdle(); | 540 base::RunLoop().RunUntilIdle(); |
| 558 EXPECT_EQ(0, observer.device_added_count()); | 541 EXPECT_EQ(0, observer.device_added_count()); |
| 559 EXPECT_EQ(1, observer.device_changed_count()); | 542 EXPECT_EQ(1, observer.device_changed_count()); |
| 560 EXPECT_EQ(1u, adapter_->GetDevices().size()); | 543 EXPECT_EQ(1u, adapter_->GetDevices().size()); |
| 561 EXPECT_EQ(device, observer.last_device()); | 544 EXPECT_EQ(device, observer.last_device()); |
| 562 | 545 |
| 563 // Expect new UUIDs: | 546 // Expect new UUIDs: |
| 564 EXPECT_FALSE( | 547 EXPECT_FALSE(ContainsValue(device->GetUUIDs(), BluetoothUUID("1800"))); |
| 565 ContainsValue(device->GetUUIDs(), BluetoothUUID(kTestUUIDGenericAccess))); | 548 EXPECT_TRUE(ContainsValue(device->GetUUIDs(), BluetoothUUID("1802"))); |
| 566 EXPECT_TRUE(ContainsValue(device->GetUUIDs(), | |
| 567 BluetoothUUID(kTestUUIDImmediateAlert))); | |
| 568 | 549 |
| 569 // Discover same device again with empty UUIDs: | 550 // Discover same device again with empty UUIDs: |
| 570 observer.Reset(); | 551 observer.Reset(); |
| 571 DiscoverLowEnergyDevice(3); | 552 DiscoverLowEnergyDevice(3); |
| 572 base::RunLoop().RunUntilIdle(); | 553 base::RunLoop().RunUntilIdle(); |
| 573 EXPECT_EQ(0, observer.device_added_count()); | 554 EXPECT_EQ(0, observer.device_added_count()); |
| 574 EXPECT_EQ(1, observer.device_changed_count()); | 555 EXPECT_EQ(1, observer.device_changed_count()); |
| 575 EXPECT_EQ(1u, adapter_->GetDevices().size()); | 556 EXPECT_EQ(1u, adapter_->GetDevices().size()); |
| 576 EXPECT_EQ(device, observer.last_device()); | 557 EXPECT_EQ(device, observer.last_device()); |
| 577 | 558 |
| 578 // Expect empty UUIDs: | 559 // Expect empty UUIDs: |
| 579 EXPECT_EQ(0u, device->GetUUIDs().size()); | 560 EXPECT_EQ(0u, device->GetUUIDs().size()); |
| 580 } | 561 } |
| 581 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 562 #endif // defined(OS_ANDROID) |
| 582 | 563 |
| 583 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 564 #if defined(OS_ANDROID) |
| 584 // Discovers multiple devices when addresses vary. | 565 // Discovers multiple devices when addresses vary. |
| 585 TEST_F(BluetoothTest, DiscoverMultipleLowEnergyDevices) { | 566 TEST_F(BluetoothTest, DiscoverMultipleDevices) { |
| 586 if (!PlatformSupportsLowEnergy()) { | |
| 587 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | |
| 588 return; | |
| 589 } | |
| 590 InitWithFakeAdapter(); | 567 InitWithFakeAdapter(); |
| 591 TestBluetoothAdapterObserver observer(adapter_); | 568 TestBluetoothAdapterObserver observer(adapter_); |
| 592 | 569 |
| 593 // Start discovery and find a device. | 570 // Start discovery and find a device. |
| 594 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(), | 571 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(), |
| 595 GetErrorCallback()); | 572 GetErrorCallback()); |
| 596 base::RunLoop().RunUntilIdle(); | 573 base::RunLoop().RunUntilIdle(); |
| 597 DiscoverLowEnergyDevice(1); | 574 DiscoverLowEnergyDevice(1); |
| 598 DiscoverLowEnergyDevice(4); | 575 DiscoverLowEnergyDevice(4); |
| 599 base::RunLoop().RunUntilIdle(); | 576 base::RunLoop().RunUntilIdle(); |
| 600 EXPECT_EQ(2, observer.device_added_count()); | 577 EXPECT_EQ(2, observer.device_added_count()); |
| 601 EXPECT_EQ(2u, adapter_->GetDevices().size()); | 578 EXPECT_EQ(2u, adapter_->GetDevices().size()); |
| 602 } | 579 } |
| 603 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 580 #endif // defined(OS_ANDROID) |
| 604 | 581 |
| 605 } // namespace device | 582 } // namespace device |
| OLD | NEW |