Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/memory/ref_counted.h" | 5 #include "base/memory/ref_counted.h" |
| 6 #include "base/test/test_simple_task_runner.h" | 6 #include "base/test/test_simple_task_runner.h" |
| 7 #include "device/bluetooth/bluetooth_adapter.h" | 7 #include "device/bluetooth/bluetooth_adapter.h" |
| 8 #include "device/bluetooth/bluetooth_adapter_mac.h" | 8 #include "device/bluetooth/bluetooth_adapter_mac.h" |
| 9 #include "device/bluetooth/bluetooth_discovery_session.h" | |
| 10 #include "device/bluetooth/test/mock_bluetooth_central_manager.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 12 |
| 11 namespace device { | 13 namespace device { |
| 12 | 14 |
| 13 class BluetoothAdapterMacTest : public testing::Test { | 15 class BluetoothAdapterMacTest : public testing::Test { |
| 14 public: | 16 public: |
| 15 BluetoothAdapterMacTest() | 17 BluetoothAdapterMacTest() |
| 16 : ui_task_runner_(new base::TestSimpleTaskRunner()), | 18 : ui_task_runner_(new base::TestSimpleTaskRunner()), |
| 17 adapter_(new BluetoothAdapterMac()), | 19 adapter_(new BluetoothAdapterMac()), |
| 18 adapter_mac_(static_cast<BluetoothAdapterMac*>(adapter_.get())) { | 20 adapter_mac_(static_cast<BluetoothAdapterMac*>(adapter_.get())), |
| 21 callback_count_(0), | |
| 22 error_callback_count_(0) { | |
| 19 adapter_mac_->InitForTest(ui_task_runner_); | 23 adapter_mac_->InitForTest(ui_task_runner_); |
| 20 } | 24 } |
| 21 | 25 |
| 26 // Helper methods for access BluetoothAdapterMacTest's members. | |
|
scheib
2015/06/06 05:09:48
access to
krstnmnlsn
2015/06/09 01:08:10
Done.
| |
| 27 void SetMockCentralManager() { | |
| 28 mock_central_manager_ = [[MockCentralManager alloc] init]; | |
| 29 adapter_mac_->low_energy_discovery_manager_->SetManager( | |
| 30 mock_central_manager_); | |
| 31 } | |
| 32 | |
| 33 void AddDiscoverySession(BluetoothDiscoveryFilter* discovery_filter) { | |
| 34 adapter_mac_->AddDiscoverySession( | |
| 35 discovery_filter, | |
| 36 base::Bind(&BluetoothAdapterMacTest::Callback, base::Unretained(this)), | |
| 37 base::Bind(&BluetoothAdapterMacTest::ErrorCallback, | |
| 38 base::Unretained(this))); | |
| 39 } | |
| 40 | |
| 41 void RemoveDiscoverySession(BluetoothDiscoveryFilter* discovery_filter) { | |
| 42 adapter_mac_->RemoveDiscoverySession( | |
| 43 discovery_filter, | |
| 44 base::Bind(&BluetoothAdapterMacTest::Callback, base::Unretained(this)), | |
| 45 base::Bind(&BluetoothAdapterMacTest::ErrorCallback, | |
| 46 base::Unretained(this))); | |
| 47 } | |
| 48 | |
| 49 int NumDiscoverySessions() { return adapter_mac_->num_discovery_sessions_; } | |
| 50 | |
| 51 // Generic callbacks. | |
| 52 void Callback() { ++callback_count_; } | |
| 53 void ErrorCallback() { ++error_callback_count_; } | |
| 54 | |
| 22 protected: | 55 protected: |
| 23 scoped_refptr<base::TestSimpleTaskRunner> ui_task_runner_; | 56 scoped_refptr<base::TestSimpleTaskRunner> ui_task_runner_; |
| 24 scoped_refptr<BluetoothAdapter> adapter_; | 57 scoped_refptr<BluetoothAdapter> adapter_; |
| 25 BluetoothAdapterMac* adapter_mac_; | 58 BluetoothAdapterMac* adapter_mac_; |
| 59 | |
| 60 // Owned by |low_energy_discovery_manager_| on |adapter_mac_|. | |
| 61 MockCentralManager* mock_central_manager_ = NULL; | |
| 62 | |
| 63 int callback_count_; | |
| 64 int error_callback_count_; | |
| 26 }; | 65 }; |
| 27 | 66 |
| 28 TEST_F(BluetoothAdapterMacTest, Poll) { | 67 TEST_F(BluetoothAdapterMacTest, Poll) { |
| 29 EXPECT_FALSE(ui_task_runner_->GetPendingTasks().empty()); | 68 EXPECT_FALSE(ui_task_runner_->GetPendingTasks().empty()); |
| 30 } | 69 } |
| 31 | 70 |
| 71 TEST_F(BluetoothAdapterMacTest, AddDiscoverySessionWithLowEnergyFilter) { | |
| 72 SetMockCentralManager(); | |
| 73 EXPECT_EQ(NO, [mock_central_manager_ scanForPeripheralsWasCalled]); | |
| 74 EXPECT_EQ(0, NumDiscoverySessions()); | |
| 75 | |
| 76 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( | |
| 77 new BluetoothDiscoveryFilter( | |
| 78 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); | |
| 79 AddDiscoverySession(discovery_filter.get()); | |
| 80 EXPECT_EQ(1, callback_count_); | |
| 81 EXPECT_EQ(0, error_callback_count_); | |
| 82 EXPECT_EQ(1, NumDiscoverySessions()); | |
| 83 | |
| 84 // Check that adding a discovery session resulted in | |
| 85 // scanForPeripheralsWithServices being called on the Central Manager. | |
| 86 EXPECT_EQ(YES, [mock_central_manager_ scanForPeripheralsWasCalled]); | |
| 87 } | |
| 88 | |
| 89 // TODO(krstnmnlsn): Test changing the filter when adding the second discovery | |
| 90 // session (once we have that ability). | |
| 91 TEST_F(BluetoothAdapterMacTest, AddSecondDiscoverySessionWithLowEnergyFilter) { | |
| 92 SetMockCentralManager(); | |
| 93 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( | |
| 94 new BluetoothDiscoveryFilter( | |
| 95 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); | |
| 96 AddDiscoverySession(discovery_filter.get()); | |
| 97 EXPECT_EQ(1, callback_count_); | |
| 98 EXPECT_EQ(0, error_callback_count_); | |
| 99 EXPECT_EQ(1, NumDiscoverySessions()); | |
| 100 | |
| 101 // We replaced the success callback handed to AddDiscoverySession, so | |
| 102 // |adapter_mac_| should remain in a discovering state indefinitely. | |
| 103 EXPECT_TRUE(adapter_mac_->IsDiscovering()); | |
| 104 | |
| 105 // Reset |_scanForPeripheralsWasCalled| and check that it is called again. | |
| 106 [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.
| |
| 107 AddDiscoverySession(discovery_filter.get()); | |
| 108 EXPECT_EQ(YES, [mock_central_manager_ scanForPeripheralsWasCalled]); | |
| 109 EXPECT_EQ(2, callback_count_); | |
| 110 EXPECT_EQ(0, error_callback_count_); | |
| 111 EXPECT_EQ(2, NumDiscoverySessions()); | |
| 112 } | |
| 113 | |
| 114 TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySessionWithLowEnergyFilter) { | |
| 115 SetMockCentralManager(); | |
| 116 EXPECT_EQ(NO, [mock_central_manager_ scanForPeripheralsWasCalled]); | |
| 117 | |
| 118 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( | |
| 119 new BluetoothDiscoveryFilter( | |
| 120 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); | |
| 121 AddDiscoverySession(discovery_filter.get()); | |
| 122 EXPECT_EQ(1, callback_count_); | |
| 123 EXPECT_EQ(0, error_callback_count_); | |
| 124 EXPECT_EQ(1, NumDiscoverySessions()); | |
| 125 | |
| 126 EXPECT_EQ(NO, [mock_central_manager_ stopScanWasCalled]); | |
| 127 RemoveDiscoverySession(discovery_filter.get()); | |
| 128 EXPECT_EQ(2, callback_count_); | |
| 129 EXPECT_EQ(0, error_callback_count_); | |
| 130 EXPECT_EQ(0, NumDiscoverySessions()); | |
| 131 | |
| 132 // Check that removing the discovery session resulted in stopScan being called | |
| 133 // on the Central Manager. | |
| 134 EXPECT_EQ(YES, [mock_central_manager_ stopScanWasCalled]); | |
| 135 } | |
| 136 | |
| 137 TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySessionWithLowEnergyFilterFail) { | |
| 138 SetMockCentralManager(); | |
| 139 EXPECT_EQ(NO, [mock_central_manager_ scanForPeripheralsWasCalled]); | |
| 140 EXPECT_EQ(NO, [mock_central_manager_ stopScanWasCalled]); | |
| 141 EXPECT_EQ(0, NumDiscoverySessions()); | |
| 142 | |
| 143 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( | |
| 144 new BluetoothDiscoveryFilter( | |
| 145 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); | |
| 146 RemoveDiscoverySession(discovery_filter.get()); | |
| 147 EXPECT_EQ(0, callback_count_); | |
| 148 EXPECT_EQ(1, error_callback_count_); | |
| 149 EXPECT_EQ(0, NumDiscoverySessions()); | |
| 150 | |
| 151 // Check that stopScan was not called. | |
| 152 EXPECT_EQ(NO, [mock_central_manager_ stopScanWasCalled]); | |
| 153 } | |
| 154 | |
| 32 } // namespace device | 155 } // namespace device |
| OLD | NEW |