ui_broker_impl_unittest.cc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // Copyright 2022 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 "ash/quick_pair/ui/ui_broker_impl.h"
  5. #include "ash/quick_pair/common/constants.h"
  6. #include "ash/quick_pair/common/device.h"
  7. #include "ash/quick_pair/ui/fast_pair/fast_pair_presenter.h"
  8. #include "ash/quick_pair/ui/fast_pair/fast_pair_presenter_impl.h"
  9. #include "ash/quick_pair/ui/ui_broker.h"
  10. #include "ash/test/ash_test_base.h"
  11. #include "base/memory/scoped_refptr.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. #include "ui/message_center/message_center.h"
  14. namespace {
  15. constexpr char kTestDeviceAddress[] = "11:12:13:14:15:16";
  16. constexpr char kValidModelId[] = "718c17";
  17. const std::string kUserEmail = "test@test.test";
  18. class FakeFastPairPresenter : public ash::quick_pair::FastPairPresenter {
  19. public:
  20. FakeFastPairPresenter(message_center::MessageCenter* message_center) {}
  21. ~FakeFastPairPresenter() override = default;
  22. void ShowDiscovery(scoped_refptr<ash::quick_pair::Device> device,
  23. ash::quick_pair::DiscoveryCallback callback) override {
  24. callback.Run(ash::quick_pair::DiscoveryAction::kPairToDevice);
  25. }
  26. void ShowPairing(scoped_refptr<ash::quick_pair::Device> device) override {
  27. show_pairing_ = true;
  28. }
  29. bool show_pairing() { return show_pairing_; }
  30. void ShowPairingFailed(
  31. scoped_refptr<ash::quick_pair::Device> device,
  32. ash::quick_pair::PairingFailedCallback callback) override {
  33. callback.Run(ash::quick_pair::PairingFailedAction::kNavigateToSettings);
  34. show_pairing_failed_ = true;
  35. }
  36. bool show_pairing_failed() { return show_pairing_failed_; }
  37. void ShowAssociateAccount(
  38. scoped_refptr<ash::quick_pair::Device> device,
  39. ash::quick_pair::AssociateAccountCallback callback) override {
  40. callback.Run(ash::quick_pair::AssociateAccountAction::kLearnMore);
  41. }
  42. void ShowCompanionApp(
  43. scoped_refptr<ash::quick_pair::Device> device,
  44. ash::quick_pair::CompanionAppCallback callback) override {
  45. callback.Run(ash::quick_pair::CompanionAppAction::kDownloadAndLaunchApp);
  46. }
  47. void RemoveNotifications() override { removed_ = true; }
  48. bool removed() { return removed_; }
  49. private:
  50. bool show_pairing_ = false;
  51. bool removed_ = false;
  52. bool show_pairing_failed_ = false;
  53. };
  54. class FakeFastPairPresenterFactory
  55. : public ash::quick_pair::FastPairPresenterImpl::Factory {
  56. public:
  57. std::unique_ptr<ash::quick_pair::FastPairPresenter> CreateInstance(
  58. message_center::MessageCenter* message_center) override {
  59. auto fake_fast_pair_presenter =
  60. std::make_unique<FakeFastPairPresenter>(message_center);
  61. fake_fast_pair_presenter_ = fake_fast_pair_presenter.get();
  62. return fake_fast_pair_presenter;
  63. }
  64. ~FakeFastPairPresenterFactory() override = default;
  65. FakeFastPairPresenter* fake_fast_pair_presenter() {
  66. return fake_fast_pair_presenter_;
  67. }
  68. protected:
  69. FakeFastPairPresenter* fake_fast_pair_presenter_ = nullptr;
  70. };
  71. } // namespace
  72. namespace ash {
  73. namespace quick_pair {
  74. class UIBrokerImplTest : public AshTestBase, public UIBroker::Observer {
  75. public:
  76. void SetUp() override {
  77. AshTestBase::SetUp();
  78. presenter_factory_ = std::make_unique<FakeFastPairPresenterFactory>();
  79. FastPairPresenterImpl::Factory::SetFactoryForTesting(
  80. presenter_factory_.get());
  81. ui_broker_ = std::make_unique<UIBrokerImpl>();
  82. ui_broker_->AddObserver(this);
  83. }
  84. void TearDown() override {
  85. ui_broker_->RemoveObserver(this);
  86. ui_broker_.reset();
  87. ClearLogin();
  88. AshTestBase::TearDown();
  89. }
  90. void OnDiscoveryAction(scoped_refptr<Device> device,
  91. DiscoveryAction action) override {
  92. discovery_action_ = action;
  93. }
  94. void OnCompanionAppAction(scoped_refptr<Device> device,
  95. CompanionAppAction action) override {
  96. companion_app_action_ = action;
  97. }
  98. void OnPairingFailureAction(scoped_refptr<Device> device,
  99. PairingFailedAction action) override {
  100. pairing_failed_action_ = action;
  101. }
  102. void OnAssociateAccountAction(scoped_refptr<Device> device,
  103. AssociateAccountAction action) override {
  104. associate_account_action_ = action;
  105. }
  106. protected:
  107. DiscoveryAction discovery_action_;
  108. PairingFailedAction pairing_failed_action_;
  109. AssociateAccountAction associate_account_action_;
  110. CompanionAppAction companion_app_action_;
  111. std::unique_ptr<FakeFastPairPresenterFactory> presenter_factory_;
  112. std::unique_ptr<UIBroker> ui_broker_;
  113. };
  114. TEST_F(UIBrokerImplTest, ShowDiscovery_Initial) {
  115. auto device = base::MakeRefCounted<Device>(kValidModelId, kTestDeviceAddress,
  116. Protocol::kFastPairInitial);
  117. ui_broker_->ShowDiscovery(device);
  118. base::RunLoop().RunUntilIdle();
  119. EXPECT_EQ(discovery_action_, DiscoveryAction::kPairToDevice);
  120. }
  121. TEST_F(UIBrokerImplTest, ShowDiscovery_Subsequent) {
  122. auto device = base::MakeRefCounted<Device>(kValidModelId, kTestDeviceAddress,
  123. Protocol::kFastPairSubsequent);
  124. ui_broker_->ShowDiscovery(device);
  125. base::RunLoop().RunUntilIdle();
  126. EXPECT_EQ(discovery_action_, DiscoveryAction::kPairToDevice);
  127. }
  128. TEST_F(UIBrokerImplTest, ShowPairing_Initial) {
  129. auto device = base::MakeRefCounted<Device>(kValidModelId, kTestDeviceAddress,
  130. Protocol::kFastPairInitial);
  131. ui_broker_->ShowPairing(device);
  132. base::RunLoop().RunUntilIdle();
  133. EXPECT_TRUE(presenter_factory_->fake_fast_pair_presenter()->show_pairing());
  134. }
  135. TEST_F(UIBrokerImplTest, ShowPairing_Subsequent) {
  136. auto device = base::MakeRefCounted<Device>(kValidModelId, kTestDeviceAddress,
  137. Protocol::kFastPairSubsequent);
  138. ui_broker_->ShowPairing(device);
  139. base::RunLoop().RunUntilIdle();
  140. EXPECT_TRUE(presenter_factory_->fake_fast_pair_presenter()->show_pairing());
  141. }
  142. TEST_F(UIBrokerImplTest, ShowPairingFailed_Initial) {
  143. auto device = base::MakeRefCounted<Device>(kValidModelId, kTestDeviceAddress,
  144. Protocol::kFastPairInitial);
  145. ui_broker_->ShowPairingFailed(device);
  146. base::RunLoop().RunUntilIdle();
  147. EXPECT_EQ(pairing_failed_action_, PairingFailedAction::kNavigateToSettings);
  148. }
  149. TEST_F(UIBrokerImplTest, ShowPairingFailed_Subsequent) {
  150. auto device = base::MakeRefCounted<Device>(kValidModelId, kTestDeviceAddress,
  151. Protocol::kFastPairSubsequent);
  152. ui_broker_->ShowPairingFailed(device);
  153. base::RunLoop().RunUntilIdle();
  154. EXPECT_EQ(pairing_failed_action_, PairingFailedAction::kNavigateToSettings);
  155. }
  156. TEST_F(UIBrokerImplTest, ShowPairingFailed_Retroactive) {
  157. auto device = base::MakeRefCounted<Device>(kValidModelId, kTestDeviceAddress,
  158. Protocol::kFastPairRetroactive);
  159. ui_broker_->ShowPairingFailed(device);
  160. base::RunLoop().RunUntilIdle();
  161. EXPECT_FALSE(
  162. presenter_factory_->fake_fast_pair_presenter()->show_pairing_failed());
  163. }
  164. TEST_F(UIBrokerImplTest, ShowAssociateAccount_Initial) {
  165. auto device = base::MakeRefCounted<Device>(kValidModelId, kTestDeviceAddress,
  166. Protocol::kFastPairInitial);
  167. ui_broker_->ShowAssociateAccount(device);
  168. base::RunLoop().RunUntilIdle();
  169. EXPECT_EQ(associate_account_action_, AssociateAccountAction::kLearnMore);
  170. }
  171. TEST_F(UIBrokerImplTest, ShowAssociateAccount_Retroactive) {
  172. auto device = base::MakeRefCounted<Device>(kValidModelId, kTestDeviceAddress,
  173. Protocol::kFastPairRetroactive);
  174. ui_broker_->ShowAssociateAccount(device);
  175. base::RunLoop().RunUntilIdle();
  176. EXPECT_EQ(associate_account_action_, AssociateAccountAction::kLearnMore);
  177. }
  178. TEST_F(UIBrokerImplTest, ShowCompanionApp_Initial) {
  179. auto device = base::MakeRefCounted<Device>(kValidModelId, kTestDeviceAddress,
  180. Protocol::kFastPairInitial);
  181. ui_broker_->ShowCompanionApp(device);
  182. base::RunLoop().RunUntilIdle();
  183. EXPECT_EQ(companion_app_action_, CompanionAppAction::kDownloadAndLaunchApp);
  184. }
  185. TEST_F(UIBrokerImplTest, ShowCompanionApp_Subsequent) {
  186. auto device = base::MakeRefCounted<Device>(kValidModelId, kTestDeviceAddress,
  187. Protocol::kFastPairSubsequent);
  188. ui_broker_->ShowCompanionApp(device);
  189. base::RunLoop().RunUntilIdle();
  190. EXPECT_EQ(companion_app_action_, CompanionAppAction::kDownloadAndLaunchApp);
  191. }
  192. TEST_F(UIBrokerImplTest, ShowCompanionApp_Retroactive) {
  193. auto device = base::MakeRefCounted<Device>(kValidModelId, kTestDeviceAddress,
  194. Protocol::kFastPairRetroactive);
  195. ui_broker_->ShowCompanionApp(device);
  196. base::RunLoop().RunUntilIdle();
  197. EXPECT_EQ(companion_app_action_, CompanionAppAction::kDownloadAndLaunchApp);
  198. }
  199. TEST_F(UIBrokerImplTest, RemoveNotifications_Initial) {
  200. auto device = base::MakeRefCounted<Device>(kValidModelId, kTestDeviceAddress,
  201. Protocol::kFastPairInitial);
  202. ui_broker_->RemoveNotifications();
  203. base::RunLoop().RunUntilIdle();
  204. EXPECT_TRUE(presenter_factory_->fake_fast_pair_presenter()->removed());
  205. }
  206. TEST_F(UIBrokerImplTest, RemoveNotifications_Subsequent) {
  207. auto device = base::MakeRefCounted<Device>(kValidModelId, kTestDeviceAddress,
  208. Protocol::kFastPairSubsequent);
  209. ui_broker_->RemoveNotifications();
  210. base::RunLoop().RunUntilIdle();
  211. EXPECT_TRUE(presenter_factory_->fake_fast_pair_presenter()->removed());
  212. }
  213. TEST_F(UIBrokerImplTest, RemoveNotifications_Retroactive) {
  214. auto device = base::MakeRefCounted<Device>(kValidModelId, kTestDeviceAddress,
  215. Protocol::kFastPairRetroactive);
  216. ui_broker_->RemoveNotifications();
  217. base::RunLoop().RunUntilIdle();
  218. EXPECT_TRUE(presenter_factory_->fake_fast_pair_presenter()->removed());
  219. }
  220. } // namespace quick_pair
  221. } // namespace ash