adapter_unittest.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. #include <base/macros.h>
  17. #include <gtest/gtest.h>
  18. #include "service/adapter.h"
  19. #include "service/hal/fake_bluetooth_gatt_interface.h"
  20. #include "service/hal/fake_bluetooth_interface.h"
  21. namespace bluetooth {
  22. namespace {
  23. class AdapterTest : public ::testing::Test {
  24. public:
  25. AdapterTest() = default;
  26. ~AdapterTest() override = default;
  27. void SetUp() override {
  28. fake_hal_manager_ = hal::FakeBluetoothInterface::GetManager();
  29. fake_hal_iface_ = new hal::FakeBluetoothInterface();
  30. hal::BluetoothInterface::InitializeForTesting(fake_hal_iface_);
  31. // Initialize GATT interface with default handlers.
  32. hal::BluetoothGattInterface::InitializeForTesting(
  33. new hal::FakeBluetoothGattInterface(nullptr, nullptr, nullptr,
  34. nullptr));
  35. adapter_ = Adapter::Create();
  36. }
  37. void TearDown() override {
  38. adapter_.reset();
  39. hal::BluetoothGattInterface::CleanUp();
  40. hal::BluetoothInterface::CleanUp();
  41. }
  42. protected:
  43. hal::FakeBluetoothInterface* fake_hal_iface_;
  44. hal::FakeBluetoothInterface::Manager* fake_hal_manager_;
  45. std::unique_ptr<Adapter> adapter_;
  46. private:
  47. DISALLOW_COPY_AND_ASSIGN(AdapterTest);
  48. };
  49. class TestObserver final : public bluetooth::Adapter::Observer {
  50. public:
  51. explicit TestObserver(bluetooth::Adapter* adapter)
  52. : adapter_(adapter),
  53. prev_state_(bluetooth::ADAPTER_STATE_INVALID),
  54. cur_state_(bluetooth::ADAPTER_STATE_INVALID),
  55. last_device_connected_state_(false) {
  56. CHECK(adapter_);
  57. adapter_->AddObserver(this);
  58. }
  59. ~TestObserver() override { adapter_->RemoveObserver(this); }
  60. bluetooth::AdapterState prev_state() const { return prev_state_; }
  61. bluetooth::AdapterState cur_state() const { return cur_state_; }
  62. std::string last_connection_state_address() const {
  63. return last_connection_state_address_;
  64. }
  65. bool last_device_connected_state() const {
  66. return last_device_connected_state_;
  67. }
  68. // bluetooth::Adapter::Observer override:
  69. void OnAdapterStateChanged(bluetooth::Adapter* adapter,
  70. bluetooth::AdapterState prev_state,
  71. bluetooth::AdapterState new_state) override {
  72. ASSERT_EQ(adapter_, adapter);
  73. prev_state_ = prev_state;
  74. cur_state_ = new_state;
  75. }
  76. void OnDeviceConnectionStateChanged(Adapter* adapter,
  77. const std::string& device_address,
  78. bool connected) override {
  79. ASSERT_EQ(adapter_, adapter);
  80. last_connection_state_address_ = device_address;
  81. last_device_connected_state_ = connected;
  82. }
  83. private:
  84. bluetooth::Adapter* adapter_;
  85. bluetooth::AdapterState prev_state_, cur_state_;
  86. std::string last_connection_state_address_;
  87. bool last_device_connected_state_;
  88. DISALLOW_COPY_AND_ASSIGN(TestObserver);
  89. };
  90. TEST_F(AdapterTest, IsEnabled) {
  91. EXPECT_FALSE(adapter_->IsEnabled());
  92. fake_hal_iface_->NotifyAdapterStateChanged(BT_STATE_ON);
  93. EXPECT_TRUE(adapter_->IsEnabled());
  94. fake_hal_iface_->NotifyAdapterStateChanged(BT_STATE_OFF);
  95. EXPECT_FALSE(adapter_->IsEnabled());
  96. }
  97. TEST_F(AdapterTest, Enable) {
  98. TestObserver observer(adapter_.get());
  99. EXPECT_FALSE(adapter_->IsEnabled());
  100. EXPECT_EQ(bluetooth::ADAPTER_STATE_OFF, adapter_->GetState());
  101. // Enable fails at HAL level
  102. EXPECT_FALSE(adapter_->Enable());
  103. EXPECT_EQ(bluetooth::ADAPTER_STATE_OFF, adapter_->GetState());
  104. // Enable success
  105. fake_hal_manager_->enable_succeed = true;
  106. EXPECT_TRUE(adapter_->Enable());
  107. // Should have received a state update.
  108. EXPECT_EQ(bluetooth::ADAPTER_STATE_OFF, observer.prev_state());
  109. EXPECT_EQ(bluetooth::ADAPTER_STATE_TURNING_ON, observer.cur_state());
  110. // Enable fails because not disabled
  111. EXPECT_EQ(bluetooth::ADAPTER_STATE_TURNING_ON, adapter_->GetState());
  112. EXPECT_FALSE(adapter_->Enable());
  113. // Adapter state updates properly
  114. fake_hal_iface_->NotifyAdapterStateChanged(BT_STATE_ON);
  115. EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, adapter_->GetState());
  116. // Should have received a state update.
  117. EXPECT_EQ(bluetooth::ADAPTER_STATE_TURNING_ON, observer.prev_state());
  118. EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, observer.cur_state());
  119. // Enable fails because already enabled
  120. EXPECT_FALSE(adapter_->Enable());
  121. }
  122. TEST_F(AdapterTest, Disable) {
  123. TestObserver observer(adapter_.get());
  124. fake_hal_manager_->disable_succeed = true;
  125. EXPECT_FALSE(adapter_->IsEnabled());
  126. EXPECT_EQ(bluetooth::ADAPTER_STATE_OFF, adapter_->GetState());
  127. // Disable fails because already disabled
  128. EXPECT_FALSE(adapter_->Disable());
  129. EXPECT_EQ(bluetooth::ADAPTER_STATE_OFF, adapter_->GetState());
  130. // Disable success
  131. fake_hal_iface_->NotifyAdapterStateChanged(BT_STATE_ON);
  132. // Should have received a state update.
  133. EXPECT_EQ(bluetooth::ADAPTER_STATE_OFF, observer.prev_state());
  134. EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, observer.cur_state());
  135. EXPECT_TRUE(adapter_->Disable());
  136. // Should have received a state update.
  137. EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, observer.prev_state());
  138. EXPECT_EQ(bluetooth::ADAPTER_STATE_TURNING_OFF, observer.cur_state());
  139. // Disable fails because not enabled
  140. EXPECT_EQ(bluetooth::ADAPTER_STATE_TURNING_OFF, adapter_->GetState());
  141. EXPECT_FALSE(adapter_->Disable());
  142. fake_hal_iface_->NotifyAdapterStateChanged(BT_STATE_ON);
  143. EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, adapter_->GetState());
  144. // Should have received a state update.
  145. EXPECT_EQ(bluetooth::ADAPTER_STATE_TURNING_OFF, observer.prev_state());
  146. EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, observer.cur_state());
  147. // Disable fails at HAL level
  148. fake_hal_manager_->disable_succeed = false;
  149. EXPECT_FALSE(adapter_->Disable());
  150. // Should have received a state update. In this case we will receive two
  151. // updates: one going from OFF to TURNING_OFF, and one going from TURNING_OFF
  152. // back to ON since we failed to initiate the disable operation.
  153. EXPECT_EQ(bluetooth::ADAPTER_STATE_TURNING_OFF, observer.prev_state());
  154. EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, observer.cur_state());
  155. // Update state to OFF. Should receive a state update.
  156. fake_hal_iface_->NotifyAdapterStateChanged(BT_STATE_OFF);
  157. EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, observer.prev_state());
  158. EXPECT_EQ(bluetooth::ADAPTER_STATE_OFF, observer.cur_state());
  159. }
  160. TEST_F(AdapterTest, GetName) {
  161. EXPECT_EQ(bluetooth::Adapter::kDefaultName, adapter_->GetName());
  162. const char kTestAdapterName[] = "Test Adapter Name";
  163. fake_hal_iface_->NotifyAdapterNamePropertyChanged(kTestAdapterName);
  164. EXPECT_EQ(kTestAdapterName, adapter_->GetName());
  165. }
  166. TEST_F(AdapterTest, SetName) {
  167. bt_bdname_t hal_name;
  168. // Name too large.
  169. EXPECT_FALSE(adapter_->SetName(std::string(sizeof(hal_name.name), 'a')));
  170. // Valid length.
  171. EXPECT_FALSE(adapter_->SetName("Test Name"));
  172. fake_hal_manager_->set_property_succeed = true;
  173. EXPECT_TRUE(adapter_->SetName("Test Name"));
  174. }
  175. TEST_F(AdapterTest, GetAddress) {
  176. EXPECT_EQ(bluetooth::Adapter::kDefaultAddress, adapter_->GetAddress());
  177. const RawAddress kTestAdapterInput = {{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc}};
  178. const char kTestAdapterAddressOutput[] = "12:34:56:78:9A:BC";
  179. fake_hal_iface_->NotifyAdapterAddressPropertyChanged(&kTestAdapterInput);
  180. EXPECT_EQ(kTestAdapterAddressOutput, adapter_->GetAddress());
  181. }
  182. TEST_F(AdapterTest, IsMultiAdvertisementSupported) {
  183. EXPECT_FALSE(adapter_->IsMultiAdvertisementSupported());
  184. bt_local_le_features_t features;
  185. memset(&features, 0, sizeof(features));
  186. features.max_adv_instance = 10; // Some high number.
  187. fake_hal_iface_->NotifyAdapterLocalLeFeaturesPropertyChanged(&features);
  188. EXPECT_TRUE(adapter_->IsMultiAdvertisementSupported());
  189. features.max_adv_instance = 0; // Low number.
  190. fake_hal_iface_->NotifyAdapterLocalLeFeaturesPropertyChanged(&features);
  191. EXPECT_FALSE(adapter_->IsMultiAdvertisementSupported());
  192. }
  193. TEST_F(AdapterTest, IsDeviceConnected) {
  194. const char kDeviceAddr[] = "12:34:56:78:9A:BC";
  195. TestObserver observer(adapter_.get());
  196. EXPECT_FALSE(adapter_->IsDeviceConnected(kDeviceAddr));
  197. RawAddress hal_addr;
  198. ASSERT_TRUE(RawAddress::FromString(kDeviceAddr, hal_addr));
  199. // status != BT_STATUS_SUCCESS should be ignored
  200. fake_hal_iface_->NotifyAclStateChangedCallback(BT_STATUS_FAIL, hal_addr,
  201. BT_ACL_STATE_CONNECTED);
  202. EXPECT_FALSE(adapter_->IsDeviceConnected(kDeviceAddr));
  203. EXPECT_TRUE(observer.last_connection_state_address().empty());
  204. EXPECT_FALSE(observer.last_device_connected_state());
  205. // Connected
  206. fake_hal_iface_->NotifyAclStateChangedCallback(BT_STATUS_SUCCESS, hal_addr,
  207. BT_ACL_STATE_CONNECTED);
  208. EXPECT_TRUE(adapter_->IsDeviceConnected(kDeviceAddr));
  209. EXPECT_EQ(kDeviceAddr, observer.last_connection_state_address());
  210. EXPECT_TRUE(observer.last_device_connected_state());
  211. // Disconnected
  212. fake_hal_iface_->NotifyAclStateChangedCallback(BT_STATUS_SUCCESS, hal_addr,
  213. BT_ACL_STATE_DISCONNECTED);
  214. EXPECT_FALSE(adapter_->IsDeviceConnected(kDeviceAddr));
  215. EXPECT_EQ(kDeviceAddr, observer.last_connection_state_address());
  216. EXPECT_FALSE(observer.last_device_connected_state());
  217. }
  218. } // namespace
  219. } // namespace bluetooth