1 //
2 // Copyright 2015 Google, Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at:
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include <base/macros.h>
18 #include <gtest/gtest.h>
19
20 #include "service/adapter.h"
21 #include "service/hal/fake_bluetooth_gatt_interface.h"
22 #include "service/hal/fake_bluetooth_interface.h"
23
24 namespace bluetooth {
25 namespace {
26
27 class AdapterTest : public ::testing::Test {
28 public:
29 AdapterTest() = default;
30 ~AdapterTest() override = default;
31
SetUp()32 void SetUp() override {
33 fake_hal_manager_ = hal::FakeBluetoothInterface::GetManager();
34 fake_hal_iface_ = new hal::FakeBluetoothInterface();
35 hal::BluetoothInterface::InitializeForTesting(fake_hal_iface_);
36
37 // Initialize GATT interface with default handlers.
38 hal::BluetoothGattInterface::InitializeForTesting(
39 new hal::FakeBluetoothGattInterface(nullptr, nullptr, nullptr,
40 nullptr));
41
42 adapter_ = Adapter::Create();
43 }
44
TearDown()45 void TearDown() override {
46 adapter_.reset();
47 hal::BluetoothGattInterface::CleanUp();
48 hal::BluetoothInterface::CleanUp();
49 }
50
51 protected:
52 hal::FakeBluetoothInterface* fake_hal_iface_;
53 hal::FakeBluetoothInterface::Manager* fake_hal_manager_;
54 std::unique_ptr<Adapter> adapter_;
55
56 private:
57 DISALLOW_COPY_AND_ASSIGN(AdapterTest);
58 };
59
60 class TestObserver final : public bluetooth::Adapter::Observer {
61 public:
TestObserver(bluetooth::Adapter * adapter)62 explicit TestObserver(bluetooth::Adapter* adapter)
63 : adapter_(adapter),
64 prev_state_(bluetooth::ADAPTER_STATE_INVALID),
65 cur_state_(bluetooth::ADAPTER_STATE_INVALID),
66 last_device_connected_state_(false) {
67 CHECK(adapter_);
68 adapter_->AddObserver(this);
69 }
70
~TestObserver()71 ~TestObserver() override { adapter_->RemoveObserver(this); }
72
prev_state() const73 bluetooth::AdapterState prev_state() const { return prev_state_; }
cur_state() const74 bluetooth::AdapterState cur_state() const { return cur_state_; }
75
last_connection_state_address() const76 std::string last_connection_state_address() const {
77 return last_connection_state_address_;
78 }
79
last_device_connected_state() const80 bool last_device_connected_state() const {
81 return last_device_connected_state_;
82 }
83
84 // bluetooth::Adapter::Observer override:
OnAdapterStateChanged(bluetooth::Adapter * adapter,bluetooth::AdapterState prev_state,bluetooth::AdapterState new_state)85 void OnAdapterStateChanged(bluetooth::Adapter* adapter,
86 bluetooth::AdapterState prev_state,
87 bluetooth::AdapterState new_state) override {
88 ASSERT_EQ(adapter_, adapter);
89 prev_state_ = prev_state;
90 cur_state_ = new_state;
91 }
92
OnDeviceConnectionStateChanged(Adapter * adapter,const std::string & device_address,bool connected)93 void OnDeviceConnectionStateChanged(Adapter* adapter,
94 const std::string& device_address,
95 bool connected) override {
96 ASSERT_EQ(adapter_, adapter);
97 last_connection_state_address_ = device_address;
98 last_device_connected_state_ = connected;
99 }
100
101 private:
102 bluetooth::Adapter* adapter_;
103 bluetooth::AdapterState prev_state_, cur_state_;
104 std::string last_connection_state_address_;
105 bool last_device_connected_state_;
106
107 DISALLOW_COPY_AND_ASSIGN(TestObserver);
108 };
109
TEST_F(AdapterTest,IsEnabled)110 TEST_F(AdapterTest, IsEnabled) {
111 EXPECT_FALSE(adapter_->IsEnabled());
112
113 fake_hal_iface_->NotifyAdapterStateChanged(BT_STATE_ON);
114 EXPECT_TRUE(adapter_->IsEnabled());
115
116 fake_hal_iface_->NotifyAdapterStateChanged(BT_STATE_OFF);
117 EXPECT_FALSE(adapter_->IsEnabled());
118 }
119
TEST_F(AdapterTest,Enable)120 TEST_F(AdapterTest, Enable) {
121 TestObserver observer(adapter_.get());
122
123 EXPECT_FALSE(adapter_->IsEnabled());
124 EXPECT_EQ(bluetooth::ADAPTER_STATE_OFF, adapter_->GetState());
125
126 // Enable fails at HAL level
127 EXPECT_FALSE(adapter_->Enable());
128 EXPECT_EQ(bluetooth::ADAPTER_STATE_OFF, adapter_->GetState());
129
130 // Enable success
131 fake_hal_manager_->enable_succeed = true;
132 EXPECT_TRUE(adapter_->Enable());
133
134 // Should have received a state update.
135 EXPECT_EQ(bluetooth::ADAPTER_STATE_OFF, observer.prev_state());
136 EXPECT_EQ(bluetooth::ADAPTER_STATE_TURNING_ON, observer.cur_state());
137
138 // Enable fails because not disabled
139 EXPECT_EQ(bluetooth::ADAPTER_STATE_TURNING_ON, adapter_->GetState());
140 EXPECT_FALSE(adapter_->Enable());
141
142 // Adapter state updates properly
143 fake_hal_iface_->NotifyAdapterStateChanged(BT_STATE_ON);
144 EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, adapter_->GetState());
145
146 // Should have received a state update.
147 EXPECT_EQ(bluetooth::ADAPTER_STATE_TURNING_ON, observer.prev_state());
148 EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, observer.cur_state());
149
150 // Enable fails because already enabled
151 EXPECT_FALSE(adapter_->Enable());
152 }
153
TEST_F(AdapterTest,Disable)154 TEST_F(AdapterTest, Disable) {
155 TestObserver observer(adapter_.get());
156
157 fake_hal_manager_->disable_succeed = true;
158 EXPECT_FALSE(adapter_->IsEnabled());
159 EXPECT_EQ(bluetooth::ADAPTER_STATE_OFF, adapter_->GetState());
160
161 // Disable fails because already disabled
162 EXPECT_FALSE(adapter_->Disable());
163 EXPECT_EQ(bluetooth::ADAPTER_STATE_OFF, adapter_->GetState());
164
165 // Disable success
166 fake_hal_iface_->NotifyAdapterStateChanged(BT_STATE_ON);
167
168 // Should have received a state update.
169 EXPECT_EQ(bluetooth::ADAPTER_STATE_OFF, observer.prev_state());
170 EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, observer.cur_state());
171
172 EXPECT_TRUE(adapter_->Disable());
173
174 // Should have received a state update.
175 EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, observer.prev_state());
176 EXPECT_EQ(bluetooth::ADAPTER_STATE_TURNING_OFF, observer.cur_state());
177
178 // Disable fails because not enabled
179 EXPECT_EQ(bluetooth::ADAPTER_STATE_TURNING_OFF, adapter_->GetState());
180 EXPECT_FALSE(adapter_->Disable());
181
182 fake_hal_iface_->NotifyAdapterStateChanged(BT_STATE_ON);
183 EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, adapter_->GetState());
184
185 // Should have received a state update.
186 EXPECT_EQ(bluetooth::ADAPTER_STATE_TURNING_OFF, observer.prev_state());
187 EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, observer.cur_state());
188
189 // Disable fails at HAL level
190 fake_hal_manager_->disable_succeed = false;
191 EXPECT_FALSE(adapter_->Disable());
192
193 // Should have received a state update. In this case we will receive two
194 // updates: one going from OFF to TURNING_OFF, and one going from TURNING_OFF
195 // back to ON since we failed to initiate the disable operation.
196 EXPECT_EQ(bluetooth::ADAPTER_STATE_TURNING_OFF, observer.prev_state());
197 EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, observer.cur_state());
198
199 // Update state to OFF. Should receive a state update.
200 fake_hal_iface_->NotifyAdapterStateChanged(BT_STATE_OFF);
201 EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, observer.prev_state());
202 EXPECT_EQ(bluetooth::ADAPTER_STATE_OFF, observer.cur_state());
203 }
204
TEST_F(AdapterTest,GetName)205 TEST_F(AdapterTest, GetName) {
206 EXPECT_EQ(bluetooth::Adapter::kDefaultName, adapter_->GetName());
207
208 const char kTestAdapterName[] = "Test Adapter Name";
209
210 fake_hal_iface_->NotifyAdapterNamePropertyChanged(kTestAdapterName);
211 EXPECT_EQ(kTestAdapterName, adapter_->GetName());
212 }
213
TEST_F(AdapterTest,SetName)214 TEST_F(AdapterTest, SetName) {
215 bt_bdname_t hal_name;
216
217 // Name too large.
218 EXPECT_FALSE(adapter_->SetName(std::string(sizeof(hal_name.name), 'a')));
219
220 // Valid length.
221 EXPECT_FALSE(adapter_->SetName("Test Name"));
222 fake_hal_manager_->set_property_succeed = true;
223 EXPECT_TRUE(adapter_->SetName("Test Name"));
224 }
225
TEST_F(AdapterTest,GetAddress)226 TEST_F(AdapterTest, GetAddress) {
227 EXPECT_EQ(bluetooth::Adapter::kDefaultAddress, adapter_->GetAddress());
228
229 const RawAddress kTestAdapterInput = {{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc}};
230 const char kTestAdapterAddressOutput[] = "12:34:56:78:9A:BC";
231
232 fake_hal_iface_->NotifyAdapterAddressPropertyChanged(&kTestAdapterInput);
233 EXPECT_EQ(kTestAdapterAddressOutput, adapter_->GetAddress());
234 }
235
TEST_F(AdapterTest,IsMultiAdvertisementSupported)236 TEST_F(AdapterTest, IsMultiAdvertisementSupported) {
237 EXPECT_FALSE(adapter_->IsMultiAdvertisementSupported());
238
239 bt_local_le_features_t features;
240 memset(&features, 0, sizeof(features));
241
242 features.max_adv_instance = 10; // Some high number.
243 fake_hal_iface_->NotifyAdapterLocalLeFeaturesPropertyChanged(&features);
244 EXPECT_TRUE(adapter_->IsMultiAdvertisementSupported());
245
246 features.max_adv_instance = 0; // Low number.
247 fake_hal_iface_->NotifyAdapterLocalLeFeaturesPropertyChanged(&features);
248 EXPECT_FALSE(adapter_->IsMultiAdvertisementSupported());
249 }
250
TEST_F(AdapterTest,IsDeviceConnected)251 TEST_F(AdapterTest, IsDeviceConnected) {
252 const char kDeviceAddr[] = "12:34:56:78:9A:BC";
253 TestObserver observer(adapter_.get());
254
255 EXPECT_FALSE(adapter_->IsDeviceConnected(kDeviceAddr));
256
257 RawAddress hal_addr;
258 ASSERT_TRUE(RawAddress::FromString(kDeviceAddr, hal_addr));
259
260 // status != BT_STATUS_SUCCESS should be ignored
261 fake_hal_iface_->NotifyAclStateChangedCallback(BT_STATUS_FAIL, hal_addr,
262 BT_ACL_STATE_CONNECTED,
263 0xff); // HCI_ERR_UNDEFINED
264 EXPECT_FALSE(adapter_->IsDeviceConnected(kDeviceAddr));
265 EXPECT_TRUE(observer.last_connection_state_address().empty());
266 EXPECT_FALSE(observer.last_device_connected_state());
267
268 // Connected
269 fake_hal_iface_->NotifyAclStateChangedCallback(BT_STATUS_SUCCESS, hal_addr,
270 BT_ACL_STATE_CONNECTED,
271 0x00); // HCI_SUCCESS
272 EXPECT_TRUE(adapter_->IsDeviceConnected(kDeviceAddr));
273 EXPECT_EQ(kDeviceAddr, observer.last_connection_state_address());
274 EXPECT_TRUE(observer.last_device_connected_state());
275
276 // Disconnected
277 fake_hal_iface_->NotifyAclStateChangedCallback(BT_STATUS_SUCCESS, hal_addr,
278 BT_ACL_STATE_DISCONNECTED,
279 0x16); // HCI_ERR_CONN_CAUSE_LOCAL_HOST
280 EXPECT_FALSE(adapter_->IsDeviceConnected(kDeviceAddr));
281 EXPECT_EQ(kDeviceAddr, observer.last_connection_state_address());
282 EXPECT_FALSE(observer.last_device_connected_state());
283 }
284
285 } // namespace
286 } // namespace bluetooth
287