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

ash / wallpaper / wallpaper_controller_test_api.cc [blame]

// Copyright 2017 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/wallpaper_controller_test_api.h"

#include <memory>

#include "ash/wallpaper/wallpaper_controller_impl.h"
#include "ash/wallpaper/wallpaper_utils/wallpaper_calculated_colors.h"
#include "ash/wallpaper/wallpaper_utils/wallpaper_color_calculator.h"
#include "base/functional/bind.h"
#include "base/time/time.h"
#include "components/account_id/account_id.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image_skia.h"

namespace ash {

namespace {

const WallpaperInfo kTestWallpaperInfo = {"", WALLPAPER_LAYOUT_CENTER,
                                          WallpaperType::kDefault,
                                          base::Time::Now().LocalMidnight()};

gfx::ImageSkia CreateImageWithColor(const SkColor color) {
  gfx::Canvas canvas(gfx::Size(5, 5), 1.0f, true);
  canvas.DrawColor(color);
  return gfx::ImageSkia::CreateFrom1xBitmap(canvas.GetBitmap());
}

}  // namespace

WallpaperControllerTestApi::WallpaperControllerTestApi(
    WallpaperControllerImpl* controller)
    : controller_(controller) {}

WallpaperControllerTestApi::~WallpaperControllerTestApi() = default;

void WallpaperControllerTestApi::StartWallpaperPreview() {
  // Preview mode is considered active when the two callbacks have non-empty
  // values. Their specific values don't matter for testing purpose.
  controller_->confirm_preview_wallpaper_callback_ = base::BindOnce(
      &WallpaperControllerImpl::SetWallpaperFromInfo,
      controller_->weak_factory_.GetWeakPtr(),
      AccountId::FromUserEmail("user@test.com"), kTestWallpaperInfo);
  controller_->reload_preview_wallpaper_callback_ = base::BindRepeating(
      &WallpaperControllerImpl::ShowWallpaperImage,
      controller_->weak_factory_.GetWeakPtr(),
      CreateImageWithColor(SK_ColorBLUE), kTestWallpaperInfo,
      /*preview_mode=*/true, /*always_on_top=*/false);
  // Show the preview wallpaper.
  controller_->reload_preview_wallpaper_callback_.Run();
}

void WallpaperControllerTestApi::EndWallpaperPreview(
    bool confirm_preview_wallpaper) {
  if (confirm_preview_wallpaper)
    controller_->ConfirmPreviewWallpaper();
  else
    controller_->CancelPreviewWallpaper();
}

void WallpaperControllerTestApi::SetCalculatedColors(
    const WallpaperCalculatedColors& calculated_colors) {
  if (controller_->color_calculator_) {
    controller_->color_calculator_.reset();
  }
  controller_->SetCalculatedColors(calculated_colors);
}

void WallpaperControllerTestApi::ResetCalculatedColors() {
  if (controller_->color_calculator_) {
    controller_->color_calculator_.reset();
  }
  controller_->ResetCalculatedColors();
}

void WallpaperControllerTestApi::SetDefaultWallpaper(
    const AccountId& account_id) {
  static constexpr base::Time::Exploded kTime = {
      .year = 2023, .month = 2, .day_of_month = 13, .hour = 4};
  base::Time time;
  CHECK(base::Time::FromUTCExploded(kTime, &time));
  controller_->SetDefaultWallpaperInfo(account_id, time);
}

void WallpaperControllerTestApi::ShowWallpaperImage(
    const WallpaperInfo& wallpaper_info,
    bool preview_mode,
    bool is_override) {
  controller_->ShowWallpaperImage(CreateImageWithColor(SK_ColorBLUE),
                                  wallpaper_info, preview_mode, is_override);
}

}  // namespace ash