wifi_sync_notification_controller_unittest.cc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Copyright 2021 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/services/multidevice_setup/wifi_sync_notification_controller.h"
  5. #include <memory>
  6. #include "ash/components/multidevice/remote_device_test_util.h"
  7. #include "ash/components/multidevice/software_feature.h"
  8. #include "ash/components/multidevice/software_feature_state.h"
  9. #include "ash/constants/ash_features.h"
  10. #include "ash/services/device_sync/public/cpp/fake_device_sync_client.h"
  11. #include "ash/services/multidevice_setup/fake_account_status_change_delegate.h"
  12. #include "ash/services/multidevice_setup/fake_account_status_change_delegate_notifier.h"
  13. #include "ash/services/multidevice_setup/fake_global_state_feature_manager.h"
  14. #include "ash/services/multidevice_setup/fake_host_status_provider.h"
  15. #include "ash/services/multidevice_setup/public/cpp/prefs.h"
  16. #include "ash/services/multidevice_setup/public/mojom/multidevice_setup.mojom.h"
  17. #include "base/test/scoped_feature_list.h"
  18. #include "base/test/task_environment.h"
  19. #include "components/session_manager/core/session_manager.h"
  20. #include "components/sync_preferences/testing_pref_service_syncable.h"
  21. #include "testing/gtest/include/gtest/gtest.h"
  22. #include "third_party/abseil-cpp/absl/types/optional.h"
  23. namespace ash {
  24. namespace multidevice_setup {
  25. namespace {
  26. const size_t kNumTestDevices = 4;
  27. } // namespace
  28. class MultiDeviceSetupWifiSyncNotificationControllerTest
  29. : public testing::Test {
  30. public:
  31. MultiDeviceSetupWifiSyncNotificationControllerTest(
  32. const MultiDeviceSetupWifiSyncNotificationControllerTest&) = delete;
  33. MultiDeviceSetupWifiSyncNotificationControllerTest& operator=(
  34. const MultiDeviceSetupWifiSyncNotificationControllerTest&) = delete;
  35. protected:
  36. MultiDeviceSetupWifiSyncNotificationControllerTest()
  37. : test_devices_(
  38. multidevice::CreateRemoteDeviceRefListForTest(kNumTestDevices)) {}
  39. ~MultiDeviceSetupWifiSyncNotificationControllerTest() override = default;
  40. // testing::Test:
  41. void SetUp() override {
  42. scoped_feature_list_.InitAndEnableFeature(
  43. chromeos::features::kWifiSyncAndroid);
  44. SetWifiSyncSupportedInDeviceSyncClient();
  45. fake_host_status_provider_ = std::make_unique<FakeHostStatusProvider>();
  46. test_pref_service_ =
  47. std::make_unique<sync_preferences::TestingPrefServiceSyncable>();
  48. WifiSyncNotificationController::RegisterPrefs(
  49. test_pref_service_->registry());
  50. // Allow Wifi Sync by policy
  51. test_pref_service_->registry()->RegisterBooleanPref(
  52. kWifiSyncAllowedPrefName, true);
  53. session_manager_ = std::make_unique<session_manager::SessionManager>();
  54. fake_device_sync_client_ =
  55. std::make_unique<device_sync::FakeDeviceSyncClient>();
  56. fake_device_sync_client_->set_synced_devices(test_devices_);
  57. fake_account_status_change_delegate_ =
  58. std::make_unique<FakeAccountStatusChangeDelegate>();
  59. fake_account_status_change_delegate_notifier_ =
  60. std::make_unique<FakeAccountStatusChangeDelegateNotifier>();
  61. fake_account_status_change_delegate_notifier_
  62. ->SetAccountStatusChangeDelegateRemote(
  63. fake_account_status_change_delegate_->GenerateRemote());
  64. fake_account_status_change_delegate_notifier_->FlushForTesting();
  65. multidevice::RemoteDeviceRef local_device =
  66. multidevice::CreateRemoteDeviceRefForTest();
  67. GetMutableRemoteDevice(local_device)
  68. ->software_features[multidevice::SoftwareFeature::kWifiSyncClient] =
  69. multidevice::SoftwareFeatureState::kSupported;
  70. fake_device_sync_client_->set_local_device_metadata(local_device);
  71. }
  72. void TearDown() override {}
  73. void SetHostInDeviceSyncClient(
  74. const absl::optional<multidevice::RemoteDeviceRef>& host_device) {
  75. for (const auto& remote_device : test_devices_) {
  76. bool should_be_host =
  77. host_device != absl::nullopt &&
  78. ((!remote_device.instance_id().empty() &&
  79. host_device->instance_id() == remote_device.instance_id()) ||
  80. (!remote_device.GetDeviceId().empty() &&
  81. host_device->GetDeviceId() == remote_device.GetDeviceId()));
  82. GetMutableRemoteDevice(remote_device)
  83. ->software_features
  84. [multidevice::SoftwareFeature::kBetterTogetherHost] =
  85. should_be_host ? multidevice::SoftwareFeatureState::kEnabled
  86. : multidevice::SoftwareFeatureState::kSupported;
  87. }
  88. fake_device_sync_client_->NotifyNewDevicesSynced();
  89. }
  90. void SetWifiSyncSupportedInDeviceSyncClient() {
  91. for (const auto& remote_device : test_devices_) {
  92. GetMutableRemoteDevice(remote_device)
  93. ->software_features[multidevice::SoftwareFeature::kWifiSyncHost] =
  94. multidevice::SoftwareFeatureState::kSupported;
  95. }
  96. }
  97. void CreateDelegate(
  98. const absl::optional<multidevice::RemoteDeviceRef>& initial_host) {
  99. SetHostInDeviceSyncClient(initial_host);
  100. SetHostWithStatus(initial_host);
  101. feature_manager_ = std::make_unique<FakeGlobalStateFeatureManager>();
  102. notification_controller_ = WifiSyncNotificationController::Factory::Create(
  103. feature_manager_.get(), fake_host_status_provider_.get(),
  104. test_pref_service_.get(), fake_device_sync_client_.get(),
  105. fake_account_status_change_delegate_notifier_.get());
  106. }
  107. void SetHostWithStatus(
  108. const absl::optional<multidevice::RemoteDeviceRef>& host_device) {
  109. mojom::HostStatus host_status =
  110. (host_device == absl::nullopt ? mojom::HostStatus::kNoEligibleHosts
  111. : mojom::HostStatus::kHostVerified);
  112. fake_host_status_provider_->SetHostWithStatus(host_status, host_device);
  113. }
  114. void SetIsFeatureEnabled(bool enabled) {
  115. feature_manager_->SetIsFeatureEnabled(enabled);
  116. HostStatusProvider::HostStatusWithDevice host_with_status =
  117. fake_host_status_provider_->GetHostWithStatus();
  118. if (host_with_status.host_status() != mojom::HostStatus::kHostVerified) {
  119. return;
  120. }
  121. multidevice::RemoteDeviceRef host_device = *host_with_status.host_device();
  122. bool enabled_on_backend =
  123. (host_device.GetSoftwareFeatureState(
  124. multidevice::SoftwareFeature::kWifiSyncHost) ==
  125. multidevice::SoftwareFeatureState::kEnabled);
  126. bool pending_request_state_same_as_backend =
  127. (enabled == enabled_on_backend);
  128. if (pending_request_state_same_as_backend) {
  129. return;
  130. }
  131. }
  132. FakeHostStatusProvider* fake_host_status_provider() {
  133. return fake_host_status_provider_.get();
  134. }
  135. FakeAccountStatusChangeDelegate* fake_account_status_change_delegate() {
  136. return fake_account_status_change_delegate_.get();
  137. }
  138. void FlushDelegateNotifier() {
  139. fake_account_status_change_delegate_notifier_->FlushForTesting();
  140. }
  141. const multidevice::RemoteDeviceRefList& test_devices() const {
  142. return test_devices_;
  143. }
  144. session_manager::SessionManager* session_manager() {
  145. return session_manager_.get();
  146. }
  147. private:
  148. base::test::TaskEnvironment task_environment_;
  149. multidevice::RemoteDeviceRefList test_devices_;
  150. std::unique_ptr<session_manager::SessionManager> session_manager_;
  151. std::unique_ptr<FakeHostStatusProvider> fake_host_status_provider_;
  152. std::unique_ptr<sync_preferences::TestingPrefServiceSyncable>
  153. test_pref_service_;
  154. std::unique_ptr<device_sync::FakeDeviceSyncClient> fake_device_sync_client_;
  155. std::unique_ptr<FakeAccountStatusChangeDelegateNotifier>
  156. fake_account_status_change_delegate_notifier_;
  157. std::unique_ptr<FakeAccountStatusChangeDelegate>
  158. fake_account_status_change_delegate_;
  159. std::unique_ptr<GlobalStateFeatureManager> feature_manager_;
  160. std::unique_ptr<WifiSyncNotificationController> notification_controller_;
  161. base::test::ScopedFeatureList scoped_feature_list_;
  162. };
  163. TEST_F(MultiDeviceSetupWifiSyncNotificationControllerTest,
  164. Notification_ShownOnFirstUnlockAfterPhoneEnabled) {
  165. fake_host_status_provider()->SetHostWithStatus(
  166. mojom::HostStatus::kHostVerified, test_devices()[0]);
  167. CreateDelegate(test_devices()[0] /* initial_host */);
  168. EXPECT_EQ(test_devices()[0].GetSoftwareFeatureState(
  169. multidevice::SoftwareFeature::kWifiSyncHost),
  170. multidevice::SoftwareFeatureState::kSupported);
  171. // Simulate lock/unlock
  172. session_manager()->SetSessionState(session_manager::SessionState::LOCKED);
  173. session_manager()->SetSessionState(session_manager::SessionState::ACTIVE);
  174. FlushDelegateNotifier();
  175. // Shown on first unlock.
  176. EXPECT_EQ(1u, fake_account_status_change_delegate()
  177. ->num_eligible_for_wifi_sync_events_handled());
  178. session_manager()->SetSessionState(session_manager::SessionState::LOCKED);
  179. session_manager()->SetSessionState(session_manager::SessionState::ACTIVE);
  180. FlushDelegateNotifier();
  181. // Not shown on second unlock.
  182. EXPECT_EQ(1u, fake_account_status_change_delegate()
  183. ->num_eligible_for_wifi_sync_events_handled());
  184. }
  185. TEST_F(MultiDeviceSetupWifiSyncNotificationControllerTest,
  186. Notification_NotShownIfAlreadyEnabled) {
  187. fake_host_status_provider()->SetHostWithStatus(
  188. mojom::HostStatus::kHostVerified, test_devices()[0]);
  189. CreateDelegate(test_devices()[0] /* initial_host */);
  190. SetIsFeatureEnabled(true);
  191. EXPECT_EQ(test_devices()[0].GetSoftwareFeatureState(
  192. multidevice::SoftwareFeature::kWifiSyncHost),
  193. multidevice::SoftwareFeatureState::kSupported);
  194. // Simulate lock/unlock
  195. session_manager()->SetSessionState(session_manager::SessionState::LOCKED);
  196. session_manager()->SetSessionState(session_manager::SessionState::ACTIVE);
  197. FlushDelegateNotifier();
  198. EXPECT_EQ(0u, fake_account_status_change_delegate()
  199. ->num_eligible_for_wifi_sync_events_handled());
  200. }
  201. } // namespace multidevice_setup
  202. } // namespace ash