bluetooth_chooser_controller_unittest.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <string>
  5. #include "base/bind.h"
  6. #include "components/permissions/bluetooth_chooser_controller.h"
  7. #include "components/permissions/mock_chooser_controller_view.h"
  8. #include "testing/gmock/include/gmock/gmock.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. namespace permissions {
  11. class TestBluetoothChooserController : public BluetoothChooserController {
  12. public:
  13. TestBluetoothChooserController(
  14. content::RenderFrameHost* owner,
  15. const content::BluetoothChooser::EventHandler& event_handler,
  16. std::u16string title)
  17. : BluetoothChooserController(owner, event_handler, title) {}
  18. TestBluetoothChooserController(const TestBluetoothChooserController&) =
  19. delete;
  20. TestBluetoothChooserController& operator=(
  21. const TestBluetoothChooserController&) = delete;
  22. void OpenAdapterOffHelpUrl() const override {}
  23. void OpenPermissionPreferences() const override {}
  24. void OpenHelpCenterUrl() const override {}
  25. };
  26. using testing::NiceMock;
  27. class BluetoothChooserControllerTest : public testing::Test {
  28. public:
  29. BluetoothChooserControllerTest()
  30. : bluetooth_chooser_controller_(
  31. nullptr,
  32. base::BindRepeating(
  33. &BluetoothChooserControllerTest::OnBluetoothChooserEvent,
  34. base::Unretained(this)),
  35. u"title") {
  36. bluetooth_chooser_controller_.set_view(&mock_bluetooth_chooser_view_);
  37. }
  38. BluetoothChooserControllerTest(const BluetoothChooserControllerTest&) =
  39. delete;
  40. BluetoothChooserControllerTest& operator=(
  41. const BluetoothChooserControllerTest&) = delete;
  42. protected:
  43. void OnBluetoothChooserEvent(content::BluetoothChooserEvent event,
  44. const std::string& device_id) {
  45. last_event_ = event;
  46. last_device_id_ = device_id;
  47. }
  48. TestBluetoothChooserController bluetooth_chooser_controller_;
  49. NiceMock<MockChooserControllerView> mock_bluetooth_chooser_view_;
  50. content::BluetoothChooserEvent last_event_;
  51. std::string last_device_id_;
  52. };
  53. class BluetoothChooserControllerWithDevicesAddedTest
  54. : public BluetoothChooserControllerTest {
  55. public:
  56. BluetoothChooserControllerWithDevicesAddedTest() {
  57. bluetooth_chooser_controller_.AddOrUpdateDevice(
  58. "id_a", false /* should_update_name */, u"a",
  59. true /* is_gatt_connected */, true /* is_paired */,
  60. -1 /* signal_strength_level */);
  61. bluetooth_chooser_controller_.AddOrUpdateDevice(
  62. "id_b", false /* should_update_name */, u"b",
  63. true /* is_gatt_connected */, true /* is_paired */,
  64. 0 /* signal_strength_level */);
  65. bluetooth_chooser_controller_.AddOrUpdateDevice(
  66. "id_c", false /* should_update_name */, u"c",
  67. true /* is_gatt_connected */, true /* is_paired */,
  68. 1 /* signal_strength_level */);
  69. }
  70. };
  71. TEST_F(BluetoothChooserControllerTest, AddDevice) {
  72. EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionAdded(0)).Times(1);
  73. bluetooth_chooser_controller_.AddOrUpdateDevice(
  74. "id_a", false /* should_update_name */, u"a",
  75. true /* is_gatt_connected */, true /* is_paired */,
  76. -1 /* signal_strength_level */);
  77. EXPECT_EQ(1u, bluetooth_chooser_controller_.NumOptions());
  78. EXPECT_EQ(u"a", bluetooth_chooser_controller_.GetOption(0));
  79. EXPECT_EQ(-1, bluetooth_chooser_controller_.GetSignalStrengthLevel(0));
  80. EXPECT_TRUE(bluetooth_chooser_controller_.IsConnected(0));
  81. EXPECT_TRUE(bluetooth_chooser_controller_.IsPaired(0));
  82. testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_);
  83. EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionAdded(1)).Times(1);
  84. bluetooth_chooser_controller_.AddOrUpdateDevice(
  85. "id_b", false /* should_update_name */, u"b",
  86. true /* is_gatt_connected */, true /* is_paired */,
  87. 0 /* signal_strength_level */);
  88. EXPECT_EQ(2u, bluetooth_chooser_controller_.NumOptions());
  89. EXPECT_EQ(u"b", bluetooth_chooser_controller_.GetOption(1));
  90. EXPECT_EQ(0, bluetooth_chooser_controller_.GetSignalStrengthLevel(1));
  91. testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_);
  92. EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionAdded(2)).Times(1);
  93. bluetooth_chooser_controller_.AddOrUpdateDevice(
  94. "id_c", false /* should_update_name */, u"c",
  95. true /* is_gatt_connected */, true /* is_paired */,
  96. 1 /* signal_strength_level */);
  97. EXPECT_EQ(3u, bluetooth_chooser_controller_.NumOptions());
  98. EXPECT_EQ(u"c", bluetooth_chooser_controller_.GetOption(2));
  99. EXPECT_EQ(1, bluetooth_chooser_controller_.GetSignalStrengthLevel(2));
  100. }
  101. TEST_F(BluetoothChooserControllerTest, RemoveDevice) {
  102. bluetooth_chooser_controller_.AddOrUpdateDevice(
  103. "id_a", false /* should_update_name */, u"a",
  104. true /* is_gatt_connected */, true /* is_paired */,
  105. -1 /* signal_strength_level */);
  106. bluetooth_chooser_controller_.AddOrUpdateDevice(
  107. "id_b", false /* should_update_name */, u"b",
  108. true /* is_gatt_connected */, true /* is_paired */,
  109. 0 /* signal_strength_level */);
  110. bluetooth_chooser_controller_.AddOrUpdateDevice(
  111. "id_c", false /* should_update_name */, u"c",
  112. true /* is_gatt_connected */, true /* is_paired */,
  113. 1 /* signal_strength_level */);
  114. EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionRemoved(1)).Times(1);
  115. bluetooth_chooser_controller_.RemoveDevice("id_b");
  116. EXPECT_EQ(2u, bluetooth_chooser_controller_.NumOptions());
  117. EXPECT_EQ(u"a", bluetooth_chooser_controller_.GetOption(0));
  118. EXPECT_EQ(u"c", bluetooth_chooser_controller_.GetOption(1));
  119. testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_);
  120. // Remove a non-existent device, the number of devices should not change.
  121. bluetooth_chooser_controller_.RemoveDevice("non-existent");
  122. EXPECT_EQ(2u, bluetooth_chooser_controller_.NumOptions());
  123. EXPECT_EQ(u"a", bluetooth_chooser_controller_.GetOption(0));
  124. EXPECT_EQ(u"c", bluetooth_chooser_controller_.GetOption(1));
  125. EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionRemoved(0)).Times(1);
  126. bluetooth_chooser_controller_.RemoveDevice("id_a");
  127. EXPECT_EQ(1u, bluetooth_chooser_controller_.NumOptions());
  128. EXPECT_EQ(u"c", bluetooth_chooser_controller_.GetOption(0));
  129. testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_);
  130. EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionRemoved(0)).Times(1);
  131. bluetooth_chooser_controller_.RemoveDevice("id_c");
  132. EXPECT_EQ(0u, bluetooth_chooser_controller_.NumOptions());
  133. }
  134. TEST_F(BluetoothChooserControllerTest, MultipleDevicesWithSameNameShowIds) {
  135. bluetooth_chooser_controller_.AddOrUpdateDevice(
  136. "id_a_1", false /* should_update_name */, u"a",
  137. true /* is_gatt_connected */, true /* is_paired */,
  138. -1 /* signal_strength_level */);
  139. EXPECT_EQ(u"a", bluetooth_chooser_controller_.GetOption(0));
  140. bluetooth_chooser_controller_.AddOrUpdateDevice(
  141. "id_b", false /* should_update_name */, u"b",
  142. true /* is_gatt_connected */, true /* is_paired */,
  143. 0 /* signal_strength_level */);
  144. bluetooth_chooser_controller_.AddOrUpdateDevice(
  145. "id_a_2", false /* should_update_name */, u"a",
  146. true /* is_gatt_connected */, true /* is_paired */,
  147. 1 /* signal_strength_level */);
  148. EXPECT_EQ(u"a (id_a_1)", bluetooth_chooser_controller_.GetOption(0));
  149. EXPECT_EQ(u"b", bluetooth_chooser_controller_.GetOption(1));
  150. EXPECT_EQ(u"a (id_a_2)", bluetooth_chooser_controller_.GetOption(2));
  151. bluetooth_chooser_controller_.RemoveDevice("id_a_1");
  152. EXPECT_EQ(u"b", bluetooth_chooser_controller_.GetOption(0));
  153. EXPECT_EQ(u"a", bluetooth_chooser_controller_.GetOption(1));
  154. }
  155. TEST_F(BluetoothChooserControllerTest, UpdateDeviceName) {
  156. bluetooth_chooser_controller_.AddOrUpdateDevice(
  157. "id_a", false /* should_update_name */, u"a",
  158. true /* is_gatt_connected */, true /* is_paired */,
  159. -1 /* signal_strength_level */);
  160. EXPECT_EQ(u"a", bluetooth_chooser_controller_.GetOption(0));
  161. EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionUpdated(0)).Times(1);
  162. bluetooth_chooser_controller_.AddOrUpdateDevice(
  163. "id_a", false /* should_update_name */, u"aa",
  164. true /* is_gatt_connected */, true /* is_paired */,
  165. -1 /* signal_strength_level */);
  166. // The name is still "a" since |should_update_name| is false.
  167. EXPECT_EQ(u"a", bluetooth_chooser_controller_.GetOption(0));
  168. testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_);
  169. EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionUpdated(0)).Times(1);
  170. bluetooth_chooser_controller_.AddOrUpdateDevice(
  171. "id_a", true /* should_update_name */, u"aa",
  172. true /* is_gatt_connected */, true /* is_paired */,
  173. -1 /* signal_strength_level */);
  174. EXPECT_EQ(1u, bluetooth_chooser_controller_.NumOptions());
  175. EXPECT_EQ(u"aa", bluetooth_chooser_controller_.GetOption(0));
  176. bluetooth_chooser_controller_.RemoveDevice("id_a");
  177. EXPECT_EQ(0u, bluetooth_chooser_controller_.NumOptions());
  178. }
  179. TEST_F(BluetoothChooserControllerTest, UpdateDeviceSignalStrengthLevel) {
  180. bluetooth_chooser_controller_.AddOrUpdateDevice(
  181. "id_a", false /* should_update_name */, u"a",
  182. true /* is_gatt_connected */, true /* is_paired */,
  183. -1 /* signal_strength_level */);
  184. EXPECT_EQ(-1, bluetooth_chooser_controller_.GetSignalStrengthLevel(0));
  185. EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionUpdated(0)).Times(1);
  186. bluetooth_chooser_controller_.AddOrUpdateDevice(
  187. "id_a", false /* should_update_name */, u"a",
  188. true /* is_gatt_connected */, true /* is_paired */,
  189. 1 /* signal_strength_level */);
  190. EXPECT_EQ(1, bluetooth_chooser_controller_.GetSignalStrengthLevel(0));
  191. testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_);
  192. EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionUpdated(0)).Times(1);
  193. // When Bluetooth device scanning stops, an update is sent and the signal
  194. // strength level is -1, and in this case, should still use the previously
  195. // stored signal strength level. So here the signal strength level is
  196. // still 1.
  197. bluetooth_chooser_controller_.AddOrUpdateDevice(
  198. "id_a", false /* should_update_name */, u"a",
  199. true /* is_gatt_connected */, true /* is_paired */,
  200. -1 /* signal_strength_level */);
  201. EXPECT_EQ(1, bluetooth_chooser_controller_.GetSignalStrengthLevel(0));
  202. }
  203. TEST_F(BluetoothChooserControllerTest, UpdateConnectedStatus) {
  204. bluetooth_chooser_controller_.AddOrUpdateDevice(
  205. "id_a", false /* should_update_name */, u"a",
  206. false /* is_gatt_connected */, false /* is_paired */,
  207. 1 /* signal_strength_level */);
  208. EXPECT_FALSE(bluetooth_chooser_controller_.IsConnected(0));
  209. EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionUpdated(0)).Times(1);
  210. bluetooth_chooser_controller_.AddOrUpdateDevice(
  211. "id_a", false /* should_update_name */, u"a",
  212. true /* is_gatt_connected */, false /* is_paired */,
  213. -1 /* signal_strength_level */);
  214. EXPECT_TRUE(bluetooth_chooser_controller_.IsConnected(0));
  215. }
  216. TEST_F(BluetoothChooserControllerTest, UpdatePairedStatus) {
  217. bluetooth_chooser_controller_.AddOrUpdateDevice(
  218. "id_a", false /* should_update_name */, u"a",
  219. true /* is_gatt_connected */, false /* is_paired */,
  220. -1 /* signal_strength_level */);
  221. EXPECT_FALSE(bluetooth_chooser_controller_.IsPaired(0));
  222. EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionUpdated(0)).Times(1);
  223. bluetooth_chooser_controller_.AddOrUpdateDevice(
  224. "id_a", false /* should_update_name */, u"a",
  225. true /* is_gatt_connected */, true /* is_paired */,
  226. -1 /* signal_strength_level */);
  227. EXPECT_TRUE(bluetooth_chooser_controller_.IsPaired(0));
  228. }
  229. TEST_F(BluetoothChooserControllerWithDevicesAddedTest,
  230. BluetoothAdapterTurnedOff) {
  231. EXPECT_CALL(mock_bluetooth_chooser_view_,
  232. OnAdapterEnabledChanged(/*enabled=*/false))
  233. .Times(1);
  234. bluetooth_chooser_controller_.OnAdapterPresenceChanged(
  235. content::BluetoothChooser::AdapterPresence::POWERED_OFF);
  236. EXPECT_EQ(0u, bluetooth_chooser_controller_.NumOptions());
  237. }
  238. TEST_F(BluetoothChooserControllerWithDevicesAddedTest,
  239. BluetoothAdapterTurnedOn) {
  240. EXPECT_CALL(mock_bluetooth_chooser_view_,
  241. OnAdapterEnabledChanged(/*enabled=*/true))
  242. .Times(1);
  243. bluetooth_chooser_controller_.OnAdapterPresenceChanged(
  244. content::BluetoothChooser::AdapterPresence::POWERED_ON);
  245. EXPECT_EQ(0u, bluetooth_chooser_controller_.NumOptions());
  246. }
  247. TEST_F(BluetoothChooserControllerWithDevicesAddedTest, DiscoveringState) {
  248. EXPECT_CALL(mock_bluetooth_chooser_view_,
  249. OnRefreshStateChanged(/*refreshing=*/true))
  250. .Times(1);
  251. bluetooth_chooser_controller_.OnDiscoveryStateChanged(
  252. content::BluetoothChooser::DiscoveryState::DISCOVERING);
  253. }
  254. TEST_F(BluetoothChooserControllerWithDevicesAddedTest, IdleState) {
  255. EXPECT_CALL(mock_bluetooth_chooser_view_,
  256. OnRefreshStateChanged(/*refreshing=*/false))
  257. .Times(1);
  258. bluetooth_chooser_controller_.OnDiscoveryStateChanged(
  259. content::BluetoothChooser::DiscoveryState::IDLE);
  260. }
  261. TEST_F(BluetoothChooserControllerWithDevicesAddedTest, FailedToStartState) {
  262. EXPECT_CALL(mock_bluetooth_chooser_view_,
  263. OnRefreshStateChanged(/*refreshing=*/false))
  264. .Times(1);
  265. bluetooth_chooser_controller_.OnDiscoveryStateChanged(
  266. content::BluetoothChooser::DiscoveryState::FAILED_TO_START);
  267. }
  268. TEST_F(BluetoothChooserControllerWithDevicesAddedTest, RefreshOptions) {
  269. bluetooth_chooser_controller_.RefreshOptions();
  270. EXPECT_EQ(0u, bluetooth_chooser_controller_.NumOptions());
  271. EXPECT_EQ(content::BluetoothChooserEvent::RESCAN, last_event_);
  272. EXPECT_EQ(std::string(), last_device_id_);
  273. }
  274. TEST_F(BluetoothChooserControllerWithDevicesAddedTest,
  275. SelectingOneDeviceShouldCallEventHandler) {
  276. std::vector<size_t> indices{0};
  277. bluetooth_chooser_controller_.Select(indices);
  278. EXPECT_EQ(content::BluetoothChooserEvent::SELECTED, last_event_);
  279. EXPECT_EQ("id_a", last_device_id_);
  280. }
  281. TEST_F(BluetoothChooserControllerWithDevicesAddedTest,
  282. CancelShouldCallEventHandler) {
  283. bluetooth_chooser_controller_.Cancel();
  284. EXPECT_EQ(content::BluetoothChooserEvent::CANCELLED, last_event_);
  285. EXPECT_EQ(std::string(), last_device_id_);
  286. }
  287. TEST_F(BluetoothChooserControllerWithDevicesAddedTest,
  288. CloseShouldCallEventHandler) {
  289. bluetooth_chooser_controller_.Close();
  290. EXPECT_EQ(content::BluetoothChooserEvent::CANCELLED, last_event_);
  291. EXPECT_EQ(std::string(), last_device_id_);
  292. }
  293. } // namespace permissions