Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: device/bluetooth/bluetooth_adapter_mac_unittest.mm

Issue 1165053003: Adding support for Low Energy device discovery to BluetoothAdapterMac (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comment nit on SetManagerForTesting. Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 mock_central_manager_ = [[MockCentralManager alloc] init];
34 adapter_mac_->low_energy_discovery_manager_->SetManagerForTesting(
35 mock_central_manager_);
36 return true;
37 }
38
39 void AddDiscoverySession(BluetoothDiscoveryFilter* discovery_filter) {
40 adapter_mac_->AddDiscoverySession(
41 discovery_filter,
42 base::Bind(&BluetoothAdapterMacTest::Callback, base::Unretained(this)),
43 base::Bind(&BluetoothAdapterMacTest::ErrorCallback,
44 base::Unretained(this)));
45 }
46
47 void RemoveDiscoverySession(BluetoothDiscoveryFilter* discovery_filter) {
48 adapter_mac_->RemoveDiscoverySession(
49 discovery_filter,
50 base::Bind(&BluetoothAdapterMacTest::Callback, base::Unretained(this)),
51 base::Bind(&BluetoothAdapterMacTest::ErrorCallback,
52 base::Unretained(this)));
53 }
54
55 int NumDiscoverySessions() { return adapter_mac_->num_discovery_sessions_; }
56
57 // Generic callbacks.
58 void Callback() { ++callback_count_; }
59 void ErrorCallback() { ++error_callback_count_; }
60
22 protected: 61 protected:
23 scoped_refptr<base::TestSimpleTaskRunner> ui_task_runner_; 62 scoped_refptr<base::TestSimpleTaskRunner> ui_task_runner_;
24 scoped_refptr<BluetoothAdapter> adapter_; 63 scoped_refptr<BluetoothAdapter> adapter_;
25 BluetoothAdapterMac* adapter_mac_; 64 BluetoothAdapterMac* adapter_mac_;
65
66 // Owned by |low_energy_discovery_manager_| on |adapter_mac_|.
67 id mock_central_manager_ = NULL;
68
69 int callback_count_;
70 int error_callback_count_;
26 }; 71 };
27 72
28 TEST_F(BluetoothAdapterMacTest, Poll) { 73 TEST_F(BluetoothAdapterMacTest, Poll) {
29 EXPECT_FALSE(ui_task_runner_->GetPendingTasks().empty()); 74 EXPECT_FALSE(ui_task_runner_->GetPendingTasks().empty());
30 } 75 }
31 76
77 TEST_F(BluetoothAdapterMacTest, AddDiscoverySessionWithLowEnergyFilter) {
78 if (!SetMockCentralManager())
79 return;
80 EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]);
81 EXPECT_EQ(0, NumDiscoverySessions());
82
83 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(
84 new BluetoothDiscoveryFilter(
85 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE));
86 AddDiscoverySession(discovery_filter.get());
87 EXPECT_EQ(1, callback_count_);
88 EXPECT_EQ(0, error_callback_count_);
89 EXPECT_EQ(1, NumDiscoverySessions());
90
91 // Check that adding a discovery session resulted in
92 // scanForPeripheralsWithServices being called on the Central Manager.
93 EXPECT_EQ(1, [mock_central_manager_ scanForPeripheralsCallCount]);
94 }
95
96 // TODO(krstnmnlsn): Test changing the filter when adding the second discovery
97 // session (once we have that ability).
98 TEST_F(BluetoothAdapterMacTest, AddSecondDiscoverySessionWithLowEnergyFilter) {
99 if (!SetMockCentralManager())
100 return;
101 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(
102 new BluetoothDiscoveryFilter(
103 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE));
104 AddDiscoverySession(discovery_filter.get());
105 EXPECT_EQ(1, callback_count_);
106 EXPECT_EQ(0, error_callback_count_);
107 EXPECT_EQ(1, NumDiscoverySessions());
108
109 // We replaced the success callback handed to AddDiscoverySession, so
110 // |adapter_mac_| should remain in a discovering state indefinitely.
111 EXPECT_TRUE(adapter_mac_->IsDiscovering());
112
113 AddDiscoverySession(discovery_filter.get());
114 EXPECT_EQ(2, [mock_central_manager_ scanForPeripheralsCallCount]);
115 EXPECT_EQ(2, callback_count_);
116 EXPECT_EQ(0, error_callback_count_);
117 EXPECT_EQ(2, NumDiscoverySessions());
118 }
119
120 TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySessionWithLowEnergyFilter) {
121 if (!SetMockCentralManager())
122 return;
123 EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]);
124
125 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(
126 new BluetoothDiscoveryFilter(
127 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE));
128 AddDiscoverySession(discovery_filter.get());
129 EXPECT_EQ(1, callback_count_);
130 EXPECT_EQ(0, error_callback_count_);
131 EXPECT_EQ(1, NumDiscoverySessions());
132
133 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]);
134 RemoveDiscoverySession(discovery_filter.get());
135 EXPECT_EQ(2, callback_count_);
136 EXPECT_EQ(0, error_callback_count_);
137 EXPECT_EQ(0, NumDiscoverySessions());
138
139 // Check that removing the discovery session resulted in stopScan being called
140 // on the Central Manager.
141 EXPECT_EQ(1, [mock_central_manager_ stopScanCallCount]);
142 }
143
144 TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySessionWithLowEnergyFilterFail) {
145 if (!SetMockCentralManager())
146 return;
147 EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]);
148 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]);
149 EXPECT_EQ(0, NumDiscoverySessions());
150
151 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(
152 new BluetoothDiscoveryFilter(
153 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE));
154 RemoveDiscoverySession(discovery_filter.get());
155 EXPECT_EQ(0, callback_count_);
156 EXPECT_EQ(1, error_callback_count_);
157 EXPECT_EQ(0, NumDiscoverySessions());
158
159 // Check that stopScan was not called.
160 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]);
161 }
162
32 } // namespace device 163 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698