wallpaper_pref_manager_unittest.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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/wallpaper/wallpaper_pref_manager.h"
  5. #include <string>
  6. #include <utility>
  7. #include "ash/constants/ash_features.h"
  8. #include "ash/constants/ash_pref_names.h"
  9. #include "ash/public/cpp/wallpaper/wallpaper_info.h"
  10. #include "ash/session/session_controller_impl.h"
  11. #include "ash/session/test_pref_service_provider.h"
  12. #include "ash/session/test_session_controller_client.h"
  13. #include "ash/wallpaper/test_wallpaper_controller_client.h"
  14. #include "base/callback_forward.h"
  15. #include "base/run_loop.h"
  16. #include "base/test/scoped_feature_list.h"
  17. #include "base/test/task_environment.h"
  18. #include "components/prefs/scoped_user_pref_update.h"
  19. #include "components/prefs/testing_pref_service.h"
  20. #include "components/user_manager/user_type.h"
  21. #include "testing/gmock/include/gmock/gmock-matchers.h"
  22. #include "testing/gtest/include/gtest/gtest.h"
  23. namespace ash {
  24. namespace {
  25. using testing::AllOf;
  26. using testing::Gt;
  27. using testing::Lt;
  28. constexpr char kUser1[] = "user1@test.com";
  29. const AccountId account_id_1 = AccountId::FromUserEmailGaiaId(kUser1, kUser1);
  30. constexpr char kFakeGooglePhotosPhotoId[] = "fake_photo";
  31. WallpaperInfo InfoWithType(WallpaperType type) {
  32. return WallpaperInfo(std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED, type,
  33. base::Time::Now());
  34. }
  35. base::Value CreateWallpaperInfoDict(WallpaperInfo info) {
  36. base::Value::Dict wallpaper_info_dict;
  37. if (info.asset_id.has_value()) {
  38. wallpaper_info_dict.Set(WallpaperPrefManager::kNewWallpaperAssetIdNodeName,
  39. base::NumberToString(info.asset_id.value()));
  40. }
  41. if (info.dedup_key.has_value()) {
  42. wallpaper_info_dict.Set(WallpaperPrefManager::kNewWallpaperDedupKeyNodeName,
  43. info.dedup_key.value());
  44. }
  45. if (info.unit_id.has_value()) {
  46. wallpaper_info_dict.Set(WallpaperPrefManager::kNewWallpaperUnitIdNodeName,
  47. base::NumberToString(info.unit_id.value()));
  48. }
  49. base::Value::List online_wallpaper_variant_list;
  50. for (const auto& variant : info.variants) {
  51. base::Value::Dict online_wallpaper_variant_dict;
  52. online_wallpaper_variant_dict.Set(
  53. WallpaperPrefManager::kNewWallpaperAssetIdNodeName,
  54. base::NumberToString(variant.asset_id));
  55. online_wallpaper_variant_dict.Set(
  56. WallpaperPrefManager::kOnlineWallpaperUrlNodeName,
  57. variant.raw_url.spec());
  58. online_wallpaper_variant_dict.Set(
  59. WallpaperPrefManager::kOnlineWallpaperTypeNodeName,
  60. static_cast<int>(variant.type));
  61. online_wallpaper_variant_list.Append(
  62. std::move(online_wallpaper_variant_dict));
  63. }
  64. wallpaper_info_dict.Set(
  65. WallpaperPrefManager::kNewWallpaperVariantListNodeName,
  66. std::move(online_wallpaper_variant_list));
  67. wallpaper_info_dict.Set(
  68. WallpaperPrefManager::kNewWallpaperCollectionIdNodeName,
  69. info.collection_id);
  70. wallpaper_info_dict.Set(WallpaperPrefManager::kNewWallpaperDateNodeName,
  71. base::NumberToString(info.date.ToInternalValue()));
  72. wallpaper_info_dict.Set(WallpaperPrefManager::kNewWallpaperLocationNodeName,
  73. info.location);
  74. wallpaper_info_dict.Set(
  75. WallpaperPrefManager::kNewWallpaperUserFilePathNodeName,
  76. info.user_file_path);
  77. wallpaper_info_dict.Set(WallpaperPrefManager::kNewWallpaperLayoutNodeName,
  78. info.layout);
  79. wallpaper_info_dict.Set(WallpaperPrefManager::kNewWallpaperTypeNodeName,
  80. static_cast<int>(info.type));
  81. return base::Value(std::move(wallpaper_info_dict));
  82. }
  83. void PutWallpaperInfoInPrefs(AccountId account_id,
  84. WallpaperInfo info,
  85. PrefService* pref_service,
  86. const std::string& pref_name) {
  87. DCHECK(pref_service);
  88. DictionaryPrefUpdate wallpaper_update(pref_service, pref_name);
  89. base::Value wallpaper_info_dict = CreateWallpaperInfoDict(info);
  90. wallpaper_update->SetKey(account_id.GetUserEmail(),
  91. std::move(wallpaper_info_dict));
  92. }
  93. void AssertWallpaperInfoInPrefs(const PrefService* pref_service,
  94. const char pref_name[],
  95. AccountId account_id,
  96. const WallpaperInfo& info) {
  97. const base::Value::Dict* stored_info_dict =
  98. pref_service->GetValueDict(pref_name).FindDict(account_id.GetUserEmail());
  99. DCHECK(stored_info_dict);
  100. base::Value expected_info_dict = CreateWallpaperInfoDict(info);
  101. EXPECT_EQ(expected_info_dict, *stored_info_dict);
  102. }
  103. std::string GetDummyFileName(const AccountId& account_id) {
  104. return account_id.GetUserEmail() + "-file";
  105. }
  106. class TestProfileHelper : public WallpaperProfileHelper {
  107. public:
  108. TestProfileHelper() = default;
  109. // Create a PrefService for |account_id| if it doesn't exist and register the
  110. // preference keys.
  111. void RegisterPrefsForAccount(const AccountId& account_id) {
  112. TestingPrefServiceSimple* service = &synced_prefs_[account_id];
  113. WallpaperPrefManager::RegisterProfilePrefs(service->registry());
  114. }
  115. void SetClient(WallpaperControllerClient*) override {}
  116. PrefService* GetUserPrefServiceSyncable(const AccountId& id) override {
  117. if (!is_sync_enabled)
  118. return nullptr;
  119. const auto& pref = synced_prefs_.find(id);
  120. return pref == synced_prefs_.end() ? nullptr : &(pref->second);
  121. }
  122. bool IsActiveUserSessionStarted() const override {
  123. return is_session_started;
  124. }
  125. AccountId GetActiveAccountId() const override { return active_account; }
  126. bool IsWallpaperSyncEnabled(const AccountId&) const override {
  127. return is_sync_enabled;
  128. }
  129. bool IsEphemeral(const AccountId&) const override { return is_ephemeral; }
  130. bool is_ephemeral = false;
  131. bool is_session_started = true;
  132. bool is_sync_enabled = true;
  133. AccountId active_account;
  134. private:
  135. std::map<AccountId, TestingPrefServiceSimple> synced_prefs_;
  136. };
  137. class WallpaperPrefManagerTest : public testing::Test {
  138. public:
  139. WallpaperPrefManagerTest()
  140. : task_environment_(base::test::TaskEnvironment::MainThreadType::UI) {}
  141. void SetUp() override {
  142. local_state_ = std::make_unique<TestingPrefServiceSimple>();
  143. WallpaperPrefManager::RegisterLocalStatePrefs(local_state_->registry());
  144. auto profile_helper = std::make_unique<TestProfileHelper>();
  145. profile_helper_ = profile_helper.get();
  146. pref_manager_ = WallpaperPrefManager::CreateForTesting(
  147. local_state_.get(), std::move(profile_helper));
  148. }
  149. void TearDown() override {}
  150. PrefService* GetLocalPrefService() { return local_state_.get(); }
  151. void SimulateUserLogin(const AccountId& id) {
  152. profile_helper_->RegisterPrefsForAccount(id);
  153. }
  154. protected:
  155. base::test::SingleThreadTaskEnvironment task_environment_;
  156. TestProfileHelper* profile_helper_;
  157. TestWallpaperControllerClient client_;
  158. std::unique_ptr<TestingPrefServiceSimple> local_state_;
  159. std::unique_ptr<WallpaperPrefManager> pref_manager_;
  160. };
  161. TEST_F(WallpaperPrefManagerTest, GetWallpaperInfo_Normal) {
  162. WallpaperInfo expected_info = InfoWithType(WallpaperType::kDaily);
  163. pref_manager_->SetUserWallpaperInfo(account_id_1, expected_info);
  164. WallpaperInfo actual_info;
  165. EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(account_id_1, &actual_info));
  166. EXPECT_EQ(expected_info, actual_info);
  167. }
  168. TEST_F(WallpaperPrefManagerTest, GetWallpaperInfo_Ephemeral) {
  169. profile_helper_->is_ephemeral = true;
  170. WallpaperInfo expected_info = InfoWithType(WallpaperType::kDaily);
  171. pref_manager_->SetUserWallpaperInfo(account_id_1, expected_info);
  172. WallpaperInfo actual_info;
  173. EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(account_id_1, &actual_info));
  174. EXPECT_EQ(expected_info, actual_info);
  175. }
  176. TEST_F(WallpaperPrefManagerTest, GetWallpaperInfoNothingToGet_Normal) {
  177. WallpaperInfo info;
  178. EXPECT_FALSE(pref_manager_->GetUserWallpaperInfo(account_id_1, &info));
  179. }
  180. TEST_F(WallpaperPrefManagerTest, GetWallpaperInfoNothingToGet_Ephemeral) {
  181. profile_helper_->is_ephemeral = true;
  182. WallpaperInfo info;
  183. EXPECT_FALSE(pref_manager_->GetUserWallpaperInfo(account_id_1, &info));
  184. }
  185. TEST_F(WallpaperPrefManagerTest,
  186. GetWallpaperInfo_FromEphemeralForManagedGuestSessions) {
  187. WallpaperInfo expected_info = InfoWithType(WallpaperType::kPolicy);
  188. pref_manager_->SetUserWallpaperInfo(account_id_1, /*is_ephemeral=*/true,
  189. expected_info);
  190. WallpaperInfo actual_info;
  191. EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(
  192. account_id_1, /*is_ephemeral=*/true, &actual_info));
  193. EXPECT_EQ(expected_info, actual_info);
  194. }
  195. TEST_F(WallpaperPrefManagerTest, SetWallpaperInfo_EphemeralDoesNotChangeLocal) {
  196. profile_helper_->is_ephemeral = true;
  197. WallpaperInfo expected_info = InfoWithType(WallpaperType::kDaily);
  198. pref_manager_->SetUserWallpaperInfo(account_id_1, expected_info);
  199. // Local state is expected to be untouched for ephemeral users.
  200. EXPECT_EQ(nullptr, local_state_->GetUserPrefValue(prefs::kUserWallpaperInfo));
  201. }
  202. TEST_F(WallpaperPrefManagerTest, SetWallpaperInfoLocal) {
  203. WallpaperInfo info(
  204. GetDummyFileName(account_id_1), WALLPAPER_LAYOUT_CENTER_CROPPED,
  205. WallpaperType::kThirdParty, base::Time::Now().LocalMidnight());
  206. EXPECT_TRUE(pref_manager_->SetUserWallpaperInfo(account_id_1, info));
  207. AssertWallpaperInfoInPrefs(local_state_.get(), prefs::kUserWallpaperInfo,
  208. account_id_1, info);
  209. }
  210. TEST_F(WallpaperPrefManagerTest, SetWallpaperInfoLocalFromGooglePhotos) {
  211. WallpaperInfo info(
  212. GooglePhotosWallpaperParams{account_id_1, kFakeGooglePhotosPhotoId,
  213. /*daily_refresh_enabled=*/false,
  214. WallpaperLayout::WALLPAPER_LAYOUT_STRETCH,
  215. /*preview_mode=*/false, "dedup_key"});
  216. EXPECT_TRUE(pref_manager_->SetUserWallpaperInfo(account_id_1, info));
  217. AssertWallpaperInfoInPrefs(GetLocalPrefService(), prefs::kUserWallpaperInfo,
  218. account_id_1, info);
  219. }
  220. TEST_F(WallpaperPrefManagerTest, SetWallpaperInfoSynced) {
  221. profile_helper_->RegisterPrefsForAccount(account_id_1);
  222. WallpaperInfo info = InfoWithType(WallpaperType::kOnline);
  223. EXPECT_TRUE(pref_manager_->SetUserWallpaperInfo(account_id_1, info));
  224. AssertWallpaperInfoInPrefs(
  225. profile_helper_->GetUserPrefServiceSyncable(account_id_1),
  226. prefs::kSyncableWallpaperInfo, account_id_1, info);
  227. }
  228. TEST_F(WallpaperPrefManagerTest, SetWallpaperInfoSyncedFromGooglePhotos) {
  229. profile_helper_->RegisterPrefsForAccount(account_id_1);
  230. WallpaperInfo info = InfoWithType(WallpaperType::kOnceGooglePhotos);
  231. EXPECT_TRUE(pref_manager_->SetUserWallpaperInfo(account_id_1, info));
  232. AssertWallpaperInfoInPrefs(
  233. profile_helper_->GetUserPrefServiceSyncable(account_id_1),
  234. prefs::kSyncableWallpaperInfo, account_id_1, info);
  235. }
  236. TEST_F(WallpaperPrefManagerTest, SetWallpaperInfoSyncDisabled) {
  237. profile_helper_->RegisterPrefsForAccount(account_id_1);
  238. // This needs to be saved before sync is disabled or we can't get a pref
  239. // service.
  240. PrefService* syncable_prefs =
  241. profile_helper_->GetUserPrefServiceSyncable(account_id_1);
  242. profile_helper_->is_sync_enabled = false;
  243. WallpaperInfo expected_info = InfoWithType(WallpaperType::kCustomized);
  244. PutWallpaperInfoInPrefs(account_id_1, expected_info, syncable_prefs,
  245. prefs::kSyncableWallpaperInfo);
  246. WallpaperInfo info = InfoWithType(WallpaperType::kOnline);
  247. EXPECT_TRUE(pref_manager_->SetUserWallpaperInfo(account_id_1, info));
  248. // Verify that calling SetUserWallpaperInfo does NOT change what is in synced
  249. // prefs when sync is disabled.
  250. AssertWallpaperInfoInPrefs(syncable_prefs, prefs::kSyncableWallpaperInfo,
  251. account_id_1, expected_info);
  252. }
  253. TEST_F(WallpaperPrefManagerTest, SetWallpaperInfoCustom) {
  254. profile_helper_->RegisterPrefsForAccount(account_id_1);
  255. WallpaperInfo synced_info = InfoWithType(WallpaperType::kOnline);
  256. PutWallpaperInfoInPrefs(
  257. account_id_1, synced_info,
  258. profile_helper_->GetUserPrefServiceSyncable(account_id_1),
  259. prefs::kSyncableWallpaperInfo);
  260. WallpaperInfo info = InfoWithType(WallpaperType::kCustomized);
  261. EXPECT_TRUE(pref_manager_->SetUserWallpaperInfo(account_id_1, info));
  262. // Custom wallpaper infos should not be propagated to synced preferences until
  263. // the image is uploaded to drivefs. That is not done in
  264. // |SetUserWallpaperInfo|.
  265. AssertWallpaperInfoInPrefs(
  266. profile_helper_->GetUserPrefServiceSyncable(account_id_1),
  267. prefs::kSyncableWallpaperInfo, account_id_1, synced_info);
  268. }
  269. TEST_F(WallpaperPrefManagerTest, GetNextDailyRefreshUpdate_Future) {
  270. profile_helper_->RegisterPrefsForAccount(account_id_1);
  271. base::Time time = base::Time::Now();
  272. WallpaperInfo info = InfoWithType(WallpaperType::kDaily);
  273. info.date = time + base::Days(2);
  274. ASSERT_TRUE(pref_manager_->SetUserWallpaperInfo(account_id_1, info));
  275. // Next update should be date + 1 day.
  276. EXPECT_THAT(pref_manager_->GetTimeToNextDailyRefreshUpdate(account_id_1),
  277. AllOf(Gt(base::Days(3) - base::Minutes(1)),
  278. Lt(base::Days(3) + base::Minutes(1))));
  279. }
  280. TEST_F(WallpaperPrefManagerTest, GetNextDailyRefreshUpdate_Past) {
  281. profile_helper_->RegisterPrefsForAccount(account_id_1);
  282. base::Time time = base::Time::Now();
  283. WallpaperInfo info = InfoWithType(WallpaperType::kDaily);
  284. info.date = time - base::Days(2);
  285. ASSERT_TRUE(pref_manager_->SetUserWallpaperInfo(account_id_1, info));
  286. // Next update should be immediate if it would be negative.
  287. EXPECT_EQ(pref_manager_->GetTimeToNextDailyRefreshUpdate(account_id_1),
  288. base::TimeDelta());
  289. }
  290. TEST_F(WallpaperPrefManagerTest, GetNextDailyRefreshUpdate_Recent) {
  291. profile_helper_->RegisterPrefsForAccount(account_id_1);
  292. base::Time time = base::Time::Now();
  293. WallpaperInfo info = InfoWithType(WallpaperType::kDaily);
  294. info.date = time - base::Hours(2);
  295. ASSERT_TRUE(pref_manager_->SetUserWallpaperInfo(account_id_1, info));
  296. // Next update should be 24 hours +- 1 minute after the date on WallpaperInfo.
  297. EXPECT_THAT(pref_manager_->GetTimeToNextDailyRefreshUpdate(account_id_1),
  298. AllOf(Gt(base::Hours(22) - base::Minutes(1)),
  299. Lt(base::Hours(22) + base::Minutes(1))));
  300. }
  301. TEST_F(WallpaperPrefManagerTest, CacheProminentColors) {
  302. profile_helper_->RegisterPrefsForAccount(account_id_1);
  303. WallpaperInfo info = InfoWithType(WallpaperType::kCustomized);
  304. const char location[] = "/test/location";
  305. info.location = location;
  306. EXPECT_TRUE(pref_manager_->SetUserWallpaperInfo(account_id_1, info));
  307. const std::vector<SkColor> expected_colors = {
  308. SK_ColorGREEN, SK_ColorGREEN, SK_ColorGREEN,
  309. SkColorSetRGB(0xAB, 0xBC, 0xEF)};
  310. pref_manager_->CacheProminentColors(account_id_1, expected_colors);
  311. EXPECT_EQ(expected_colors,
  312. *pref_manager_->GetCachedProminentColors(location));
  313. }
  314. TEST_F(WallpaperPrefManagerTest, CacheKMeansColor) {
  315. profile_helper_->RegisterPrefsForAccount(account_id_1);
  316. WallpaperInfo info = InfoWithType(WallpaperType::kCustomized);
  317. const char location[] = "/test/location";
  318. info.location = location;
  319. EXPECT_TRUE(pref_manager_->SetUserWallpaperInfo(account_id_1, info));
  320. const SkColor expected_color = SkColorSetRGB(0xAB, 0xBC, 0xEF);
  321. pref_manager_->CacheKMeanColor(account_id_1, expected_color);
  322. EXPECT_EQ(expected_color, *pref_manager_->GetCachedKMeanColor(location));
  323. }
  324. } // namespace
  325. } // namespace ash