ash_test_ui_stabilizer.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (c) 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/test/ash_test_ui_stabilizer.h"
  5. #include "ash/constants/ash_features.h"
  6. #include "ash/shell.h"
  7. #include "ash/style/dark_light_mode_controller_impl.h"
  8. #include "ash/wallpaper/wallpaper_controller_impl.h"
  9. #include "base/command_line.h"
  10. #include "base/i18n/base_i18n_switches.h"
  11. #include "chromeos/dbus/power/fake_power_manager_client.h"
  12. #include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
  13. #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
  14. #include "ui/gfx/image/image_skia.h"
  15. namespace ash {
  16. namespace {
  17. // The color of the default wallpaper in pixel tests.
  18. constexpr SkColor kWallPaperColor = SK_ColorMAGENTA;
  19. // Specify the locale and the time zone used in pixel tests.
  20. constexpr char kLocale[] = "en_US";
  21. constexpr char kTimeZone[] = "America/Chicago";
  22. // Creates a pure color image of the specified size.
  23. gfx::ImageSkia CreateImage(const gfx::Size& image_size, SkColor color) {
  24. SkBitmap bitmap;
  25. bitmap.allocN32Pixels(image_size.width(), image_size.height());
  26. bitmap.eraseColor(color);
  27. gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
  28. return image;
  29. }
  30. } // namespace
  31. AshTestUiStabilizer::AshTestUiStabilizer(const pixel_test::InitParams& params)
  32. : params_(params),
  33. scoped_locale_(base::test::ScopedRestoreICUDefaultLocale(kLocale)),
  34. time_zone_(base::test::ScopedRestoreDefaultTimezone(kTimeZone)) {
  35. if (params.under_rtl) {
  36. base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
  37. ::switches::kForceUIDirection, ::switches::kForceDirectionRTL);
  38. }
  39. }
  40. AshTestUiStabilizer::~AshTestUiStabilizer() = default;
  41. void AshTestUiStabilizer::StabilizeUi(const gfx::Size& wallpaper_size) {
  42. MaybeSetDarkMode();
  43. SetWallPaper(wallpaper_size);
  44. SetBatteryState();
  45. }
  46. void AshTestUiStabilizer::MaybeSetDarkMode() {
  47. // If the dark/light mode feature is not enabled, the dark mode is used as
  48. // default so return early.
  49. if (!features::IsDarkLightModeEnabled())
  50. return;
  51. auto* dark_light_mode_controller = DarkLightModeControllerImpl::Get();
  52. if (!dark_light_mode_controller->IsDarkModeEnabled())
  53. dark_light_mode_controller->ToggleColorMode();
  54. }
  55. void AshTestUiStabilizer::SetWallPaper(const gfx::Size& wallpaper_size) {
  56. auto* controller = Shell::Get()->wallpaper_controller();
  57. controller->set_wallpaper_reload_no_delay_for_test();
  58. switch (params_.wallpaper_init_type) {
  59. case pixel_test::WallpaperInitType::kRegular: {
  60. gfx::ImageSkia wallpaper_image =
  61. CreateImage(wallpaper_size, kWallPaperColor);
  62. controller->ShowWallpaperImage(
  63. wallpaper_image,
  64. WallpaperInfo{/*in_location=*/std::string(),
  65. /*in_layout=*/WALLPAPER_LAYOUT_STRETCH,
  66. /*in_type=*/WallpaperType::kDefault,
  67. /*in_date=*/base::Time::Now().LocalMidnight()},
  68. /*preview_mode=*/false, /*always_on_top=*/false);
  69. break;
  70. }
  71. case pixel_test::WallpaperInitType::kPolicy:
  72. controller->set_bypass_decode_for_testing();
  73. // A dummy file path is sufficient for setting a default policy wallpaper.
  74. controller->SetDevicePolicyWallpaperPath(base::FilePath("tmp.png"));
  75. break;
  76. }
  77. }
  78. void AshTestUiStabilizer::SetBatteryState() {
  79. power_manager::PowerSupplyProperties proto;
  80. proto.set_external_power(
  81. power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED);
  82. proto.set_battery_state(
  83. power_manager::PowerSupplyProperties_BatteryState_DISCHARGING);
  84. proto.set_battery_percent(50.0);
  85. chromeos::FakePowerManagerClient::Get()->UpdatePowerProperties(proto);
  86. }
  87. } // namespace ash