wallpaper_controller_test_api.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2017 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_controller_test_api.h"
  5. #include "ash/wallpaper/wallpaper_controller_impl.h"
  6. #include "base/bind.h"
  7. #include "ui/gfx/canvas.h"
  8. #include "ui/gfx/color_utils.h"
  9. #include "ui/gfx/geometry/size.h"
  10. #include "ui/gfx/image/image_skia.h"
  11. namespace ash {
  12. namespace {
  13. const WallpaperInfo kTestWallpaperInfo = {"", WALLPAPER_LAYOUT_CENTER,
  14. WallpaperType::kDefault,
  15. base::Time::Now().LocalMidnight()};
  16. gfx::ImageSkia CreateImageWithColor(const SkColor color) {
  17. gfx::Canvas canvas(gfx::Size(5, 5), 1.0f, true);
  18. canvas.DrawColor(color);
  19. return gfx::ImageSkia::CreateFrom1xBitmap(canvas.GetBitmap());
  20. }
  21. } // namespace
  22. WallpaperControllerTestApi::WallpaperControllerTestApi(
  23. WallpaperControllerImpl* controller)
  24. : controller_(controller) {}
  25. WallpaperControllerTestApi::~WallpaperControllerTestApi() = default;
  26. SkColor WallpaperControllerTestApi::ApplyColorProducingWallpaper() {
  27. // TODO(manucornet): Figure out where all those "magic numbers" come from
  28. // and document/compute them instead of just hard-coding them.
  29. controller_->ShowWallpaperImage(
  30. CreateImageWithColor(SkColorSetRGB(60, 40, 40)), kTestWallpaperInfo,
  31. /*preview_mode=*/false, /*always_on_top=*/false);
  32. return SkColorSetRGB(40, 35, 37);
  33. }
  34. void WallpaperControllerTestApi::StartWallpaperPreview() {
  35. // Preview mode is considered active when the two callbacks have non-empty
  36. // values. Their specific values don't matter for testing purpose.
  37. controller_->confirm_preview_wallpaper_callback_ =
  38. base::BindOnce(&WallpaperControllerImpl::SetWallpaperFromInfo,
  39. controller_->weak_factory_.GetWeakPtr(),
  40. AccountId::FromUserEmail("user@test.com"),
  41. kTestWallpaperInfo, /*show_wallpaper=*/true);
  42. controller_->reload_preview_wallpaper_callback_ = base::BindRepeating(
  43. &WallpaperControllerImpl::ShowWallpaperImage,
  44. controller_->weak_factory_.GetWeakPtr(),
  45. CreateImageWithColor(SK_ColorBLUE), kTestWallpaperInfo,
  46. /*preview_mode=*/true, /*always_on_top=*/false);
  47. // Show the preview wallpaper.
  48. controller_->reload_preview_wallpaper_callback_.Run();
  49. }
  50. void WallpaperControllerTestApi::EndWallpaperPreview(
  51. bool confirm_preview_wallpaper) {
  52. if (confirm_preview_wallpaper)
  53. controller_->ConfirmPreviewWallpaper();
  54. else
  55. controller_->CancelPreviewWallpaper();
  56. }
  57. } // namespace ash