| 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 access to BluetoothAdapterMacTest's members. |
| 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(0, [mock_central_manager_ scanForPeripheralsCallCount]); |
| 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(1, [mock_central_manager_ scanForPeripheralsCallCount]); |
| 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 AddDiscoverySession(discovery_filter.get()); |
| 106 EXPECT_EQ(2, [mock_central_manager_ scanForPeripheralsCallCount]); |
| 107 EXPECT_EQ(2, callback_count_); |
| 108 EXPECT_EQ(0, error_callback_count_); |
| 109 EXPECT_EQ(2, NumDiscoverySessions()); |
| 110 } |
| 111 |
| 112 TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySessionWithLowEnergyFilter) { |
| 113 SetMockCentralManager(); |
| 114 EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); |
| 115 |
| 116 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( |
| 117 new BluetoothDiscoveryFilter( |
| 118 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); |
| 119 AddDiscoverySession(discovery_filter.get()); |
| 120 EXPECT_EQ(1, callback_count_); |
| 121 EXPECT_EQ(0, error_callback_count_); |
| 122 EXPECT_EQ(1, NumDiscoverySessions()); |
| 123 |
| 124 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]); |
| 125 RemoveDiscoverySession(discovery_filter.get()); |
| 126 EXPECT_EQ(2, callback_count_); |
| 127 EXPECT_EQ(0, error_callback_count_); |
| 128 EXPECT_EQ(0, NumDiscoverySessions()); |
| 129 |
| 130 // Check that removing the discovery session resulted in stopScan being called |
| 131 // on the Central Manager. |
| 132 EXPECT_EQ(1, [mock_central_manager_ stopScanCallCount]); |
| 133 } |
| 134 |
| 135 TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySessionWithLowEnergyFilterFail) { |
| 136 SetMockCentralManager(); |
| 137 EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); |
| 138 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]); |
| 139 EXPECT_EQ(0, NumDiscoverySessions()); |
| 140 |
| 141 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( |
| 142 new BluetoothDiscoveryFilter( |
| 143 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); |
| 144 RemoveDiscoverySession(discovery_filter.get()); |
| 145 EXPECT_EQ(0, callback_count_); |
| 146 EXPECT_EQ(1, error_callback_count_); |
| 147 EXPECT_EQ(0, NumDiscoverySessions()); |
| 148 |
| 149 // Check that stopScan was not called. |
| 150 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]); |
| 151 } |
| 152 |
| 32 } // namespace device | 153 } // namespace device |
| OLD | NEW |