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
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196

ash / style / system_shadow_unittest.cc [blame]

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

#include <memory>

#include "ash/public/cpp/style/dark_light_mode_controller.h"
#include "ash/style/system_shadow.h"
#include "ash/test/ash_test_base.h"
#include "ui/aura/window.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/compositor/layer.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"

namespace ash {

namespace {

// Different types of SystemShadow extensions.
enum class SystemShadowType {
  kShadowOnNinePatchLayer,        // Instance of `SystemShadowOnNinePatchLayer`.
  kViewShadowOnNinePatchLayer,    // Instance of
                                  // `SystemViewShadowOnNinePatchLayer`.
  kWindowShadowOnNinePatchLayer,  // Instance of
                                  // `SystemWindowShadowOnNinePatchLayer`.
  kShadowOnTextureLayer,          // Instance of `SystemShadowOnTextureLayer`.
};

// Gets the key and ambient shadow colors from a shadow.
std::pair<SkColor, SkColor> GetShadowColors(SystemShadow* shadow) {
  gfx::ShadowValues values = shadow->GetShadowValuesForTesting();
  return std::make_pair(values[0].color(), values[1].color());
}

// Add a shadow to the bottom of the native window of a widget and make the
// shadow observe the theme change of the widget.
void AddShadowToWidget(SystemShadow* shadow, views::Widget* widget) {
  auto* window_layer = widget->GetNativeWindow()->layer();
  auto* shadow_layer = shadow->GetLayer();
  window_layer->Add(shadow_layer);
  window_layer->StackAtBottom(shadow_layer);
  shadow->SetContentBounds(gfx::Rect(window_layer->bounds().size()));
  shadow->ObserveColorProviderSource(widget);
}

}  // namespace

// The parameterized test class for dynamic system shadow colors.
class SystemShadowColorTest
    : public AshTestBase,
      public testing::WithParamInterface<SystemShadowType> {
 public:
  SystemShadowColorTest() = default;
  SystemShadowColorTest(const SystemShadowColorTest&) = delete;
  SystemShadowColorTest& operator=(const SystemShadowColorTest&) = delete;
  ~SystemShadowColorTest() override = default;

  // AshTestBase:
  void SetUp() override {
    AshTestBase::SetUp();
    // Create a test widget as the owner of the shadow instances.
    widget_ = CreateTestWidget(
        views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET,
        /*delegate=*/nullptr,
        /*container_id=*/desks_util::GetActiveDeskContainerId(),
        /*bounds=*/gfx::Rect(100, 100, 320, 200));
  }

 protected:
  // Gets the dynamic color resolved by the color provider according to the
  // theme of the `widget_`.
  SkColor GetColor(ui::ColorId color_id) {
    return widget_->GetColorProvider()->GetColor(color_id);
  }

  // Creates a certain type of SystemShadow instance according to the test
  // parameter.
  std::unique_ptr<SystemShadow> MakeShadowAccordingToParam(
      SystemShadow::Type type) {
    switch (GetParam()) {
      case SystemShadowType::kShadowOnNinePatchLayer:
        return MakeShadowOnNinePatchLayer(type);
      case SystemShadowType::kViewShadowOnNinePatchLayer:
        return MakeViewShadowOnNinePatchLayer(type);
      case SystemShadowType::kWindowShadowOnNinePatchLayer:
        return MakeWindowShadowOnNinePatchLayer(type);
      case SystemShadowType::kShadowOnTextureLayer:
        return MakeShadowOnTextureLayer(type);
    }
  }

 private:
  // Creates an instance of `SystemShadowOnNinePatchLayer`.
  std::unique_ptr<SystemShadow> MakeShadowOnNinePatchLayer(
      SystemShadow::Type type) {
    auto shadow = SystemShadow::CreateShadowOnNinePatchLayer(
        type, SystemShadow::LayerRecreatedCallback());
    AddShadowToWidget(shadow.get(), widget_.get());
    return shadow;
  }

  // Creates an instance of `SystemViewShadowOnNinePatchLayer`.
  std::unique_ptr<SystemShadow> MakeViewShadowOnNinePatchLayer(
      SystemShadow::Type type) {
    auto shadow = SystemShadow::CreateShadowOnNinePatchLayerForView(
        widget_->GetContentsView(), type);
    return shadow;
  }

  // Creates an instance of `SystemWindowShadowOnNinePatchLayer`.
  std::unique_ptr<SystemShadow> MakeWindowShadowOnNinePatchLayer(
      SystemShadow::Type type) {
    auto shadow = SystemShadow::CreateShadowOnNinePatchLayerForWindow(
        widget_->GetNativeWindow(), type);
    return shadow;
  }

  // Creates an instance of `SystemShadowOnTextureLayer`.
  std::unique_ptr<SystemShadow> MakeShadowOnTextureLayer(
      SystemShadow::Type type) {
    auto shadow = SystemShadow::CreateShadowOnTextureLayer(type);
    AddShadowToWidget(shadow.get(), widget_.get());
    return shadow;
  }

  // The test widget used as a shadow owner.
  std::unique_ptr<views::Widget> widget_;
};

INSTANTIATE_TEST_SUITE_P(
    MaterialNext,
    SystemShadowColorTest,
    testing::Values(SystemShadowType::kShadowOnNinePatchLayer,
                    SystemShadowType::kViewShadowOnNinePatchLayer,
                    SystemShadowType::kWindowShadowOnNinePatchLayer,
                    SystemShadowType::kShadowOnTextureLayer),
    [](const testing::TestParamInfo<SystemShadowColorTest::ParamType>& info) {
      switch (info.param) {
        case SystemShadowType::kShadowOnNinePatchLayer:
          return "ShadowOnNinePatchLayer";
        case SystemShadowType::kViewShadowOnNinePatchLayer:
          return "ViewShadowOnNinePatchLayer";
        case SystemShadowType::kWindowShadowOnNinePatchLayer:
          return "WindowShadowOnNinePatcherLayer";
        case SystemShadowType::kShadowOnTextureLayer:
          return "ShadowOnTextureLayer";
      }
    });

// Tests if the colors of system shadow change with shadow types.
TEST_P(SystemShadowColorTest, UpdateShadowColorsWithType) {
  auto shadow = MakeShadowAccordingToParam(SystemShadow::Type::kElevation4);
  EXPECT_EQ(GetShadowColors(shadow.get()),
            std::make_pair(
                GetColor(ui::kColorShadowValueKeyShadowElevationFour),
                GetColor(ui::kColorShadowValueAmbientShadowElevationFour)));

  // Change type to kElevation12.
  shadow->SetType(SystemShadow::Type::kElevation12);
  EXPECT_EQ(GetShadowColors(shadow.get()),
            std::make_pair(
                GetColor(ui::kColorShadowValueKeyShadowElevationTwelve),
                GetColor(ui::kColorShadowValueAmbientShadowElevationTwelve)));

  // Change type to kElevation24;
  shadow->SetType(SystemShadow::Type::kElevation24);
  EXPECT_EQ(
      GetShadowColors(shadow.get()),
      std::make_pair(
          GetColor(ui::kColorShadowValueKeyShadowElevationTwentyFour),
          GetColor(ui::kColorShadowValueAmbientShadowElevationTwentyFour)));
}

// Tests if the colors of system shadow change with the themes.
TEST_P(SystemShadowColorTest, UpdateShadowColorsWithTheme) {
  auto shadow = MakeShadowAccordingToParam(SystemShadow::Type::kElevation4);

  // Set light mode.
  DarkLightModeController::Get()->SetDarkModeEnabledForTest(false);
  EXPECT_EQ(GetShadowColors(shadow.get()),
            std::make_pair(
                GetColor(ui::kColorShadowValueKeyShadowElevationFour),
                GetColor(ui::kColorShadowValueAmbientShadowElevationFour)));

  // Set dark mode.
  DarkLightModeController::Get()->SetDarkModeEnabledForTest(true);
  EXPECT_EQ(GetShadowColors(shadow.get()),
            std::make_pair(
                GetColor(ui::kColorShadowValueKeyShadowElevationFour),
                GetColor(ui::kColorShadowValueAmbientShadowElevationFour)));
}

}  // namespace ash