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_mac.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 setup and access to BluetoothAdapterMacTest's members. | |
| 27 bool SetMockCentralManager() { | |
| 28 Class aClass = NSClassFromString(@"CBCentralManager"); | |
| 29 if (aClass == nil) { | |
| 30 LOG(WARNING) << "CoreBluetooth not available, skipping unit test."; | |
| 31 return false; | |
| 32 } | |
| 33 // Cannot use a OCMockObject because mocking 'state' property gives a | |
|
scheib
2015/06/17 16:55:08
I haven't seen OCMockObject use yet, is this comme
krstnmnlsn
2015/06/17 20:18:41
Done.
| |
| 34 // compiler warning when mock_central_manager of type id (multiple methods | |
|
scheib
2015/06/17 16:55:08
"when mock_central_manager of type id" confuses me
krstnmnlsn
2015/06/17 20:18:41
Done.
| |
| 35 // named 'state' found), and a compiler warning when mock_central_manager is | |
| 36 // of type CBCentralManager (CBCentralManager may not respond to 'stub'). | |
| 37 mock_central_manager_ = [[MockCentralManager alloc] init]; | |
| 38 adapter_mac_->low_energy_discovery_manager_->SetManager( | |
|
scheib
2015/06/17 16:55:08
I think the logic should clearly skip the test if
krstnmnlsn
2015/06/17 20:18:41
The check in SetManagerForTesting() is redundant w
scheib
2015/06/18 18:11:07
CHECK handles it. Wouldn't hurt to mention in the
| |
| 39 mock_central_manager_); | |
| 40 return true; | |
| 41 } | |
| 42 | |
| 43 void AddDiscoverySession(BluetoothDiscoveryFilter* discovery_filter) { | |
| 44 adapter_mac_->AddDiscoverySession( | |
| 45 discovery_filter, | |
| 46 base::Bind(&BluetoothAdapterMacTest::Callback, base::Unretained(this)), | |
| 47 base::Bind(&BluetoothAdapterMacTest::ErrorCallback, | |
| 48 base::Unretained(this))); | |
| 49 } | |
| 50 | |
| 51 void RemoveDiscoverySession(BluetoothDiscoveryFilter* discovery_filter) { | |
| 52 adapter_mac_->RemoveDiscoverySession( | |
| 53 discovery_filter, | |
| 54 base::Bind(&BluetoothAdapterMacTest::Callback, base::Unretained(this)), | |
| 55 base::Bind(&BluetoothAdapterMacTest::ErrorCallback, | |
| 56 base::Unretained(this))); | |
| 57 } | |
| 58 | |
| 59 int NumDiscoverySessions() { return adapter_mac_->num_discovery_sessions_; } | |
| 60 | |
| 61 // Generic callbacks. | |
| 62 void Callback() { ++callback_count_; } | |
| 63 void ErrorCallback() { ++error_callback_count_; } | |
| 64 | |
| 22 protected: | 65 protected: |
| 23 scoped_refptr<base::TestSimpleTaskRunner> ui_task_runner_; | 66 scoped_refptr<base::TestSimpleTaskRunner> ui_task_runner_; |
| 24 scoped_refptr<BluetoothAdapter> adapter_; | 67 scoped_refptr<BluetoothAdapter> adapter_; |
| 25 BluetoothAdapterMac* adapter_mac_; | 68 BluetoothAdapterMac* adapter_mac_; |
| 69 | |
| 70 // Owned by |low_energy_discovery_manager_| on |adapter_mac_|. | |
| 71 id mock_central_manager_ = NULL; | |
| 72 | |
| 73 int callback_count_; | |
| 74 int error_callback_count_; | |
| 26 }; | 75 }; |
| 27 | 76 |
| 28 TEST_F(BluetoothAdapterMacTest, Poll) { | 77 TEST_F(BluetoothAdapterMacTest, Poll) { |
| 29 EXPECT_FALSE(ui_task_runner_->GetPendingTasks().empty()); | 78 EXPECT_FALSE(ui_task_runner_->GetPendingTasks().empty()); |
| 30 } | 79 } |
| 31 | 80 |
| 81 TEST_F(BluetoothAdapterMacTest, AddDiscoverySessionWithLowEnergyFilter) { | |
| 82 if (!SetMockCentralManager()) | |
| 83 return; | |
| 84 EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); | |
| 85 EXPECT_EQ(0, NumDiscoverySessions()); | |
| 86 | |
| 87 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( | |
| 88 new BluetoothDiscoveryFilter( | |
| 89 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); | |
| 90 AddDiscoverySession(discovery_filter.get()); | |
| 91 EXPECT_EQ(1, callback_count_); | |
| 92 EXPECT_EQ(0, error_callback_count_); | |
| 93 EXPECT_EQ(1, NumDiscoverySessions()); | |
| 94 | |
| 95 // Check that adding a discovery session resulted in | |
| 96 // scanForPeripheralsWithServices being called on the Central Manager. | |
| 97 EXPECT_EQ(1, [mock_central_manager_ scanForPeripheralsCallCount]); | |
| 98 } | |
| 99 | |
| 100 // TODO(krstnmnlsn): Test changing the filter when adding the second discovery | |
| 101 // session (once we have that ability). | |
| 102 TEST_F(BluetoothAdapterMacTest, AddSecondDiscoverySessionWithLowEnergyFilter) { | |
| 103 if (!SetMockCentralManager()) | |
| 104 return; | |
| 105 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( | |
| 106 new BluetoothDiscoveryFilter( | |
| 107 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); | |
| 108 AddDiscoverySession(discovery_filter.get()); | |
| 109 EXPECT_EQ(1, callback_count_); | |
| 110 EXPECT_EQ(0, error_callback_count_); | |
| 111 EXPECT_EQ(1, NumDiscoverySessions()); | |
| 112 | |
| 113 // We replaced the success callback handed to AddDiscoverySession, so | |
| 114 // |adapter_mac_| should remain in a discovering state indefinitely. | |
| 115 EXPECT_TRUE(adapter_mac_->IsDiscovering()); | |
| 116 | |
| 117 AddDiscoverySession(discovery_filter.get()); | |
| 118 EXPECT_EQ(2, [mock_central_manager_ scanForPeripheralsCallCount]); | |
| 119 EXPECT_EQ(2, callback_count_); | |
| 120 EXPECT_EQ(0, error_callback_count_); | |
| 121 EXPECT_EQ(2, NumDiscoverySessions()); | |
| 122 } | |
| 123 | |
| 124 TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySessionWithLowEnergyFilter) { | |
| 125 if (!SetMockCentralManager()) | |
| 126 return; | |
| 127 EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); | |
| 128 | |
| 129 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( | |
| 130 new BluetoothDiscoveryFilter( | |
| 131 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); | |
| 132 AddDiscoverySession(discovery_filter.get()); | |
| 133 EXPECT_EQ(1, callback_count_); | |
| 134 EXPECT_EQ(0, error_callback_count_); | |
| 135 EXPECT_EQ(1, NumDiscoverySessions()); | |
| 136 | |
| 137 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]); | |
| 138 RemoveDiscoverySession(discovery_filter.get()); | |
| 139 EXPECT_EQ(2, callback_count_); | |
| 140 EXPECT_EQ(0, error_callback_count_); | |
| 141 EXPECT_EQ(0, NumDiscoverySessions()); | |
| 142 | |
| 143 // Check that removing the discovery session resulted in stopScan being called | |
| 144 // on the Central Manager. | |
| 145 EXPECT_EQ(1, [mock_central_manager_ stopScanCallCount]); | |
| 146 } | |
| 147 | |
| 148 TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySessionWithLowEnergyFilterFail) { | |
| 149 if (!SetMockCentralManager()) | |
| 150 return; | |
| 151 EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); | |
| 152 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]); | |
| 153 EXPECT_EQ(0, NumDiscoverySessions()); | |
| 154 | |
| 155 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( | |
| 156 new BluetoothDiscoveryFilter( | |
| 157 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); | |
| 158 RemoveDiscoverySession(discovery_filter.get()); | |
| 159 EXPECT_EQ(0, callback_count_); | |
| 160 EXPECT_EQ(1, error_callback_count_); | |
| 161 EXPECT_EQ(0, NumDiscoverySessions()); | |
| 162 | |
| 163 // Check that stopScan was not called. | |
| 164 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]); | |
| 165 } | |
| 166 | |
| 32 } // namespace device | 167 } // namespace device |
| OLD | NEW |