1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149

ash / wallpaper / test_wallpaper_controller_client.cc [blame]

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/wallpaper/test_wallpaper_controller_client.h"

#include "ash/webui/personalization_app/mojom/personalization_app.mojom.h"
#include "ash/webui/personalization_app/proto/backdrop_wallpaper.pb.h"
#include "base/i18n/time_formatting.h"
#include "base/logging.h"
#include "base/time/time.h"

namespace ash {

// static
const std::string TestWallpaperControllerClient::kDummyCollectionId =
    "testCollectionId";

TestWallpaperControllerClient::TestWallpaperControllerClient() {
  std::vector<backdrop::Image>& images = variations_[kDummyCollectionId];
  backdrop::Image image1;
  image1.set_asset_id(1);
  image1.set_image_url("https://best_wallpaper/1");
  image1.add_attribution()->set_text("test");
  image1.set_unit_id(1);
  image1.set_image_type(backdrop::Image::IMAGE_TYPE_DARK_MODE);
  images.push_back(image1);

  backdrop::Image image2;
  image2.set_asset_id(2);
  image2.set_image_url("https://best_wallpaper/2");
  image2.add_attribution()->set_text("test");
  image2.set_unit_id(1);
  image2.set_image_type(backdrop::Image::IMAGE_TYPE_LIGHT_MODE);
  images.push_back(image2);
}

TestWallpaperControllerClient::~TestWallpaperControllerClient() = default;

void TestWallpaperControllerClient::AddCollection(
    const std::string& collection_id,
    const std::vector<backdrop::Image>& images) {
  variations_[collection_id] = images;
}

void TestWallpaperControllerClient::ResetCounts() {
  open_count_ = 0;
  fetch_daily_refresh_wallpaper_param_ = std::string();
  fetch_daily_refresh_info_fails_ = false;
  fake_files_ids_.clear();
  wallpaper_sync_enabled_ = true;
}

// WallpaperControllerClient:
void TestWallpaperControllerClient::OpenWallpaperPicker() {
  open_count_++;
}

void TestWallpaperControllerClient::FetchDailyRefreshWallpaper(
    const std::string& collection_id,
    DailyWallpaperUrlFetchedCallback callback) {
  auto iter = variations_.find(collection_id);
  fetch_daily_refresh_wallpaper_param_ = collection_id;
  if (fetch_daily_refresh_info_fails_ || iter == variations_.end()) {
    std::move(callback).Run(/*success=*/false, std::move(backdrop::Image()));
    return;
  }

  image_index_ = ++image_index_ % iter->second.size();
  backdrop::Image image(iter->second.at(image_index_));
  std::move(callback).Run(/*success=*/true, std::move(image));
}

void TestWallpaperControllerClient::FetchImagesForCollection(
    const std::string& collection_id,
    FetchImagesForCollectionCallback callback) {
  fetch_images_for_collection_count_++;
  auto iter = variations_.find(collection_id);
  if (fetch_images_for_collection_fails_ || iter == variations_.end()) {
    std::move(callback).Run(/*success=*/false, std::vector<backdrop::Image>());
    return;
  }

  std::vector<backdrop::Image> images = iter->second;
  std::move(callback).Run(/*success=*/true, std::move(images));
}

void TestWallpaperControllerClient::FetchGooglePhotosPhoto(
    const AccountId& account_id,
    const std::string& id,
    FetchGooglePhotosPhotoCallback callback) {
  fetch_google_photos_photo_id_ = id;
  auto iter = wallpaper_google_photos_integration_enabled_.find(account_id);
  if (iter != wallpaper_google_photos_integration_enabled_.end() &&
      !iter->second) {
    std::move(callback).Run(/*photo=*/nullptr, /*success=*/true);
    return;
  }
  base::Time time;
  static constexpr base::Time::Exploded kTime = {.year = 2011,
                                                 .month = 6,
                                                 .day_of_week = 3,
                                                 .day_of_month = 15,
                                                 .hour = 12};
  CHECK(base::Time::FromUTCExploded(kTime, &time));
  if (fetch_google_photos_photo_fails_ || google_photo_has_been_deleted_) {
    std::move(callback).Run(nullptr,
                            /*success=*/google_photo_has_been_deleted_);
  } else {
    std::move(callback).Run(
        personalization_app::mojom::GooglePhotosPhoto::New(
            id, "dedup_key", "test_name", base::TimeFormatFriendlyDate(time),
            GURL("https://google.com/picture.png"), "home"),
        /*success=*/true);
  }
}

void TestWallpaperControllerClient::FetchDailyGooglePhotosPhoto(
    const AccountId& account_id,
    const std::string& album_id,
    FetchGooglePhotosPhotoCallback callback) {
  std::string photo_id = album_id;
  std::reverse(photo_id.begin(), photo_id.end());
  FetchGooglePhotosPhoto(account_id, photo_id, std::move(callback));
}

void TestWallpaperControllerClient::FetchGooglePhotosAccessToken(
    const AccountId& account_id,
    FetchGooglePhotosAccessTokenCallback callback) {
  std::move(callback).Run(std::nullopt);
}

void TestWallpaperControllerClient::GetFilesId(
    const AccountId& account_id,
    base::OnceCallback<void(const std::string&)> files_id_callback) const {
  auto iter = fake_files_ids_.find(account_id);
  if (iter == fake_files_ids_.end()) {
    LOG(ERROR) << "No fake files id for account id: " << account_id;
    return;
  }
  std::move(files_id_callback).Run(iter->second);
}

bool TestWallpaperControllerClient::IsWallpaperSyncEnabled(
    const AccountId& account_id) const {
  return wallpaper_sync_enabled_;
}

}  // namespace ash