Chromium Code Reviews| Index: device/bluetooth/bluetooth_adapter_mac_unittest.mm |
| diff --git a/device/bluetooth/bluetooth_adapter_mac_unittest.mm b/device/bluetooth/bluetooth_adapter_mac_unittest.mm |
| index 6ef4c9b3b7e0c0878e12cfd210f2cdb7665e4f67..bfb1c946e622ea4a45630aa149275cdcda7ca6ff 100644 |
| --- a/device/bluetooth/bluetooth_adapter_mac_unittest.mm |
| +++ b/device/bluetooth/bluetooth_adapter_mac_unittest.mm |
| @@ -6,6 +6,8 @@ |
| #include "base/test/test_simple_task_runner.h" |
| #include "device/bluetooth/bluetooth_adapter.h" |
| #include "device/bluetooth/bluetooth_adapter_mac.h" |
| +#include "device/bluetooth/bluetooth_discovery_session.h" |
| +#include "device/bluetooth/test/mock_bluetooth_central_manager.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| namespace device { |
| @@ -15,18 +17,139 @@ class BluetoothAdapterMacTest : public testing::Test { |
| BluetoothAdapterMacTest() |
| : ui_task_runner_(new base::TestSimpleTaskRunner()), |
| adapter_(new BluetoothAdapterMac()), |
| - adapter_mac_(static_cast<BluetoothAdapterMac*>(adapter_.get())) { |
| + adapter_mac_(static_cast<BluetoothAdapterMac*>(adapter_.get())), |
| + callback_count_(0), |
| + error_callback_count_(0) { |
| adapter_mac_->InitForTest(ui_task_runner_); |
| } |
| + // Helper methods for access BluetoothAdapterMacTest's members. |
|
scheib
2015/06/06 05:09:48
access to
krstnmnlsn
2015/06/09 01:08:10
Done.
|
| + void SetMockCentralManager() { |
| + mock_central_manager_ = [[MockCentralManager alloc] init]; |
| + adapter_mac_->low_energy_discovery_manager_->SetManager( |
| + mock_central_manager_); |
| + } |
| + |
| + void AddDiscoverySession(BluetoothDiscoveryFilter* discovery_filter) { |
| + adapter_mac_->AddDiscoverySession( |
| + discovery_filter, |
| + base::Bind(&BluetoothAdapterMacTest::Callback, base::Unretained(this)), |
| + base::Bind(&BluetoothAdapterMacTest::ErrorCallback, |
| + base::Unretained(this))); |
| + } |
| + |
| + void RemoveDiscoverySession(BluetoothDiscoveryFilter* discovery_filter) { |
| + adapter_mac_->RemoveDiscoverySession( |
| + discovery_filter, |
| + base::Bind(&BluetoothAdapterMacTest::Callback, base::Unretained(this)), |
| + base::Bind(&BluetoothAdapterMacTest::ErrorCallback, |
| + base::Unretained(this))); |
| + } |
| + |
| + int NumDiscoverySessions() { return adapter_mac_->num_discovery_sessions_; } |
| + |
| + // Generic callbacks. |
| + void Callback() { ++callback_count_; } |
| + void ErrorCallback() { ++error_callback_count_; } |
| + |
| protected: |
| scoped_refptr<base::TestSimpleTaskRunner> ui_task_runner_; |
| scoped_refptr<BluetoothAdapter> adapter_; |
| BluetoothAdapterMac* adapter_mac_; |
| + |
| + // Owned by |low_energy_discovery_manager_| on |adapter_mac_|. |
| + MockCentralManager* mock_central_manager_ = NULL; |
| + |
| + int callback_count_; |
| + int error_callback_count_; |
| }; |
| TEST_F(BluetoothAdapterMacTest, Poll) { |
| EXPECT_FALSE(ui_task_runner_->GetPendingTasks().empty()); |
| } |
| +TEST_F(BluetoothAdapterMacTest, AddDiscoverySessionWithLowEnergyFilter) { |
| + SetMockCentralManager(); |
| + EXPECT_EQ(NO, [mock_central_manager_ scanForPeripheralsWasCalled]); |
| + EXPECT_EQ(0, NumDiscoverySessions()); |
| + |
| + scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( |
| + new BluetoothDiscoveryFilter( |
| + BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); |
| + AddDiscoverySession(discovery_filter.get()); |
| + EXPECT_EQ(1, callback_count_); |
| + EXPECT_EQ(0, error_callback_count_); |
| + EXPECT_EQ(1, NumDiscoverySessions()); |
| + |
| + // Check that adding a discovery session resulted in |
| + // scanForPeripheralsWithServices being called on the Central Manager. |
| + EXPECT_EQ(YES, [mock_central_manager_ scanForPeripheralsWasCalled]); |
| +} |
| + |
| +// TODO(krstnmnlsn): Test changing the filter when adding the second discovery |
| +// session (once we have that ability). |
| +TEST_F(BluetoothAdapterMacTest, AddSecondDiscoverySessionWithLowEnergyFilter) { |
| + SetMockCentralManager(); |
| + scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( |
| + new BluetoothDiscoveryFilter( |
| + BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); |
| + AddDiscoverySession(discovery_filter.get()); |
| + EXPECT_EQ(1, callback_count_); |
| + EXPECT_EQ(0, error_callback_count_); |
| + EXPECT_EQ(1, NumDiscoverySessions()); |
| + |
| + // We replaced the success callback handed to AddDiscoverySession, so |
| + // |adapter_mac_| should remain in a discovering state indefinitely. |
| + EXPECT_TRUE(adapter_mac_->IsDiscovering()); |
| + |
| + // Reset |_scanForPeripheralsWasCalled| and check that it is called again. |
| + [mock_central_manager_ setScanForPeripheralsWasCalled:NO]; |
|
scheib
2015/06/06 05:09:48
Would it be easier and perhaps measure behavior mo
krstnmnlsn
2015/06/09 01:08:10
Sure, I was already debating switching it.
|
| + AddDiscoverySession(discovery_filter.get()); |
| + EXPECT_EQ(YES, [mock_central_manager_ scanForPeripheralsWasCalled]); |
| + EXPECT_EQ(2, callback_count_); |
| + EXPECT_EQ(0, error_callback_count_); |
| + EXPECT_EQ(2, NumDiscoverySessions()); |
| +} |
| + |
| +TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySessionWithLowEnergyFilter) { |
| + SetMockCentralManager(); |
| + EXPECT_EQ(NO, [mock_central_manager_ scanForPeripheralsWasCalled]); |
| + |
| + scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( |
| + new BluetoothDiscoveryFilter( |
| + BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); |
| + AddDiscoverySession(discovery_filter.get()); |
| + EXPECT_EQ(1, callback_count_); |
| + EXPECT_EQ(0, error_callback_count_); |
| + EXPECT_EQ(1, NumDiscoverySessions()); |
| + |
| + EXPECT_EQ(NO, [mock_central_manager_ stopScanWasCalled]); |
| + RemoveDiscoverySession(discovery_filter.get()); |
| + EXPECT_EQ(2, callback_count_); |
| + EXPECT_EQ(0, error_callback_count_); |
| + EXPECT_EQ(0, NumDiscoverySessions()); |
| + |
| + // Check that removing the discovery session resulted in stopScan being called |
| + // on the Central Manager. |
| + EXPECT_EQ(YES, [mock_central_manager_ stopScanWasCalled]); |
| +} |
| + |
| +TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySessionWithLowEnergyFilterFail) { |
| + SetMockCentralManager(); |
| + EXPECT_EQ(NO, [mock_central_manager_ scanForPeripheralsWasCalled]); |
| + EXPECT_EQ(NO, [mock_central_manager_ stopScanWasCalled]); |
| + EXPECT_EQ(0, NumDiscoverySessions()); |
| + |
| + scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( |
| + new BluetoothDiscoveryFilter( |
| + BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); |
| + RemoveDiscoverySession(discovery_filter.get()); |
| + EXPECT_EQ(0, callback_count_); |
| + EXPECT_EQ(1, error_callback_count_); |
| + EXPECT_EQ(0, NumDiscoverySessions()); |
| + |
| + // Check that stopScan was not called. |
| + EXPECT_EQ(NO, [mock_central_manager_ stopScanWasCalled]); |
| +} |
| + |
| } // namespace device |