test_wallpaper_controller_client.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright (c) 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/wallpaper/test_wallpaper_controller_client.h"
  5. #include "ash/webui/personalization_app/mojom/personalization_app.mojom.h"
  6. #include "ash/webui/personalization_app/proto/backdrop_wallpaper.pb.h"
  7. #include "base/i18n/time_formatting.h"
  8. #include "base/logging.h"
  9. #include "base/time/time.h"
  10. namespace ash {
  11. // static
  12. const std::string TestWallpaperControllerClient::kDummyCollectionId =
  13. "testCollectionId";
  14. TestWallpaperControllerClient::TestWallpaperControllerClient() {
  15. std::vector<backdrop::Image>& images = variations_[kDummyCollectionId];
  16. backdrop::Image image1;
  17. image1.set_asset_id(1);
  18. image1.set_image_url("https://best_wallpaper/1");
  19. image1.add_attribution()->set_text("test");
  20. image1.set_unit_id(1);
  21. image1.set_image_type(backdrop::Image::IMAGE_TYPE_DARK_MODE);
  22. images.push_back(image1);
  23. backdrop::Image image2;
  24. image2.set_asset_id(2);
  25. image2.set_image_url("https://best_wallpaper/2");
  26. image2.add_attribution()->set_text("test");
  27. image2.set_unit_id(1);
  28. image2.set_image_type(backdrop::Image::IMAGE_TYPE_LIGHT_MODE);
  29. images.push_back(image2);
  30. }
  31. TestWallpaperControllerClient::~TestWallpaperControllerClient() = default;
  32. void TestWallpaperControllerClient::AddCollection(
  33. const std::string& collection_id,
  34. const std::vector<backdrop::Image>& images) {
  35. variations_[collection_id] = images;
  36. }
  37. void TestWallpaperControllerClient::ResetCounts() {
  38. open_count_ = 0;
  39. set_default_wallpaper_count_ = 0;
  40. migrate_collection_id_from_chrome_app_count_ = 0;
  41. fetch_daily_refresh_wallpaper_param_ = std::string();
  42. fetch_daily_refresh_info_fails_ = false;
  43. get_wallpaper_path_from_drive_fs_account_id_.clear();
  44. save_wallpaper_to_drive_fs_account_id_.clear();
  45. fake_files_ids_.clear();
  46. wallpaper_sync_enabled_ = true;
  47. }
  48. // WallpaperControllerClient:
  49. void TestWallpaperControllerClient::OpenWallpaperPicker() {
  50. open_count_++;
  51. }
  52. void TestWallpaperControllerClient::SetDefaultWallpaper(
  53. const AccountId& account_id,
  54. bool show_wallpaper,
  55. base::OnceCallback<void(bool success)> callback) {
  56. set_default_wallpaper_count_++;
  57. std::move(callback).Run(/*success=*/true);
  58. }
  59. void TestWallpaperControllerClient::MigrateCollectionIdFromChromeApp(
  60. const AccountId& account_id,
  61. base::OnceCallback<void(const std::string&)>) {
  62. migrate_collection_id_from_chrome_app_count_++;
  63. }
  64. void TestWallpaperControllerClient::FetchDailyRefreshWallpaper(
  65. const std::string& collection_id,
  66. DailyWallpaperUrlFetchedCallback callback) {
  67. auto iter = variations_.find(collection_id);
  68. fetch_daily_refresh_wallpaper_param_ = collection_id;
  69. if (fetch_daily_refresh_info_fails_ || iter == variations_.end()) {
  70. std::move(callback).Run(/*success=*/false, std::move(backdrop::Image()));
  71. return;
  72. }
  73. image_index_ = ++image_index_ % iter->second.size();
  74. backdrop::Image image(iter->second.at(image_index_));
  75. std::move(callback).Run(/*success=*/true, std::move(image));
  76. }
  77. void TestWallpaperControllerClient::FetchImagesForCollection(
  78. const std::string& collection_id,
  79. FetchImagesForCollectionCallback callback) {
  80. fetch_images_for_collection_count_++;
  81. auto iter = variations_.find(collection_id);
  82. if (fetch_images_for_collection_fails_ || iter == variations_.end()) {
  83. std::move(callback).Run(/*success=*/false, std::vector<backdrop::Image>());
  84. return;
  85. }
  86. std::vector<backdrop::Image> images = iter->second;
  87. std::move(callback).Run(/*success=*/true, std::move(images));
  88. }
  89. void TestWallpaperControllerClient::FetchGooglePhotosPhoto(
  90. const AccountId& account_id,
  91. const std::string& id,
  92. FetchGooglePhotosPhotoCallback callback) {
  93. base::Time time;
  94. base::Time::Exploded exploded_time{2011, 6, 3, 15, 12, 0, 0, 0};
  95. if (!base::Time::FromUTCExploded(exploded_time, &time))
  96. NOTREACHED();
  97. if (fetch_google_photos_photo_fails_ || google_photo_has_been_deleted_) {
  98. std::move(callback).Run(nullptr,
  99. /*success=*/google_photo_has_been_deleted_);
  100. } else {
  101. std::move(callback).Run(
  102. personalization_app::mojom::GooglePhotosPhoto::New(
  103. id, "dedup_key", "test_name", base::TimeFormatFriendlyDate(time),
  104. GURL("https://google.com/picture.png"), "home"),
  105. /*success=*/true);
  106. }
  107. }
  108. void TestWallpaperControllerClient::FetchDailyGooglePhotosPhoto(
  109. const AccountId& account_id,
  110. const std::string& album_id,
  111. FetchGooglePhotosPhotoCallback callback) {
  112. std::string photo_id = album_id;
  113. std::reverse(photo_id.begin(), photo_id.end());
  114. FetchGooglePhotosPhoto(account_id, photo_id, std::move(callback));
  115. }
  116. void TestWallpaperControllerClient::FetchGooglePhotosAccessToken(
  117. const AccountId& account_id,
  118. FetchGooglePhotosAccessTokenCallback callback) {
  119. std::move(callback).Run(absl::nullopt);
  120. }
  121. void TestWallpaperControllerClient::SaveWallpaperToDriveFs(
  122. const AccountId& account_id,
  123. const base::FilePath& origin,
  124. base::OnceCallback<void(bool)> wallpaper_saved_callback) {
  125. save_wallpaper_to_drive_fs_account_id_ = account_id;
  126. std::move(wallpaper_saved_callback).Run(true);
  127. }
  128. base::FilePath TestWallpaperControllerClient::GetWallpaperPathFromDriveFs(
  129. const AccountId& account_id) {
  130. get_wallpaper_path_from_drive_fs_account_id_ = account_id;
  131. return base::FilePath();
  132. }
  133. void TestWallpaperControllerClient::GetFilesId(
  134. const AccountId& account_id,
  135. base::OnceCallback<void(const std::string&)> files_id_callback) const {
  136. auto iter = fake_files_ids_.find(account_id);
  137. if (iter == fake_files_ids_.end()) {
  138. LOG(ERROR) << "No fake files id for account id: " << account_id;
  139. return;
  140. }
  141. std::move(files_id_callback).Run(iter->second);
  142. }
  143. bool TestWallpaperControllerClient::IsWallpaperSyncEnabled(
  144. const AccountId& account_id) const {
  145. return wallpaper_sync_enabled_;
  146. }
  147. } // namespace ash