Gastón Rodríguez | 5ca1f81 | 2025-01-22 18:13:43 | [diff] [blame] | 1 | // Copyright 2025 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ui/native_theme/native_theme_base.h" |
| 6 | |
| 7 | #include "base/test/scoped_feature_list.h" |
| 8 | #include "testing/gtest/include/gtest/gtest.h" |
| 9 | #include "ui/gfx/color_utils.h" |
Zoraiz Naeem | badbeda | 2025-03-03 22:15:03 | [diff] [blame] | 10 | #include "ui/native_theme/features/native_theme_features.h" |
Gastón Rodríguez | 5ca1f81 | 2025-01-22 18:13:43 | [diff] [blame] | 11 | |
| 12 | namespace ui { |
| 13 | |
| 14 | class NativeThemeBaseTest : public NativeThemeBase, public testing::Test { |
| 15 | public: |
| 16 | SkColor GetContrastingPressedOrHoveredColor( |
| 17 | SkColor fg, |
| 18 | std::optional<SkColor> bg = std::nullopt) { |
| 19 | return NativeThemeBase::GetContrastingPressedOrHoveredColor( |
| 20 | fg, bg, /*state=*/NativeTheme::State::kHovered, |
| 21 | /*part=*/Part::kScrollbarVerticalThumb) |
| 22 | .value(); |
| 23 | } |
| 24 | float GetBaseContrastRatio() { |
| 25 | return NativeThemeBase::GetContrastRatioForState( |
| 26 | /*state=*/NativeTheme::State::kHovered, |
| 27 | /*part=*/Part::kScrollbarVerticalThumb); |
| 28 | } |
| 29 | }; |
| 30 | |
| 31 | // Check that `GetContrastingPressedOrHoveredColor` doesn't modify fully |
| 32 | // transparent colors. |
| 33 | TEST_F(NativeThemeBaseTest, GetContrastingPressedOrHoveredTransparent) { |
| 34 | const SkColor transparent_color = SkColorSetARGB(0x00, 0xBA, 0x74, 0x74); |
| 35 | EXPECT_EQ(transparent_color, |
| 36 | GetContrastingPressedOrHoveredColor(transparent_color)); |
| 37 | } |
| 38 | |
| 39 | // Tests that the `GetContrastingPressedOrHoveredColor` can adapt to a whole |
| 40 | // range of luminosity for the colors to modify. |
| 41 | TEST_F(NativeThemeBaseTest, GetContrastingPressedOrHoveredColor) { |
| 42 | for (unsigned i = 0; i < 255; i++) { |
| 43 | const SkColor color = SkColorSetRGB(i, i, i); |
| 44 | const float luminance = color_utils::GetRelativeLuminance(color); |
| 45 | const float adjusted_luminance = color_utils::GetRelativeLuminance( |
| 46 | GetContrastingPressedOrHoveredColor(color)); |
| 47 | if (color_utils::IsDark(color)) { |
| 48 | EXPECT_GT(adjusted_luminance, luminance); |
| 49 | } else { |
| 50 | EXPECT_LT(adjusted_luminance, luminance); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // Tests that the returned color never loses contrast against the background |
| 56 | // color. |
| 57 | TEST_F(NativeThemeBaseTest, |
| 58 | GetContrastingPressedOrHoveredColorBackgroundContrast) { |
| 59 | for (int c = 0; c < 24; c += 8) { |
| 60 | // Test with Green, Blue and Red. |
| 61 | const SkColor bg_color = SkColorSetA(SkColor(0x80 << (c)), SK_AlphaOPAQUE); |
| 62 | for (U8CPU i = 0; i < 255; i++) { |
| 63 | const SkColor adjusted_color = GetContrastingPressedOrHoveredColor( |
| 64 | SkColorSetA(SkColor(i << (c)), SK_AlphaOPAQUE), bg_color); |
| 65 | EXPECT_GT(color_utils::GetContrastRatio(bg_color, adjusted_color), |
| 66 | GetBaseContrastRatio()); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Checks that grayscale colors never lose contrast with the background when it |
| 72 | // is different shades of gray. |
| 73 | TEST_F(NativeThemeBaseTest, GetContrastingPressedOrHoveredColorGrayScales) { |
| 74 | static constexpr auto kColors = |
| 75 | std::to_array({SK_ColorWHITE, SK_ColorLTGRAY, SK_ColorGRAY, |
| 76 | SK_ColorDKGRAY, SK_ColorBLACK}); |
| 77 | for (const auto foreground : kColors) { |
| 78 | for (const auto background : kColors) { |
| 79 | const float background_contrast_ratio = color_utils::GetContrastRatio( |
| 80 | background, |
| 81 | GetContrastingPressedOrHoveredColor(foreground, background)); |
| 82 | const float base_contrast_ratio = GetBaseContrastRatio(); |
| 83 | EXPECT_GE(background_contrast_ratio, base_contrast_ratio); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Checks that transparent foreground colors don't lose contrast against the |
| 89 | // background when altered. |
| 90 | TEST_F(NativeThemeBaseTest, GetContrastingPressedOrHoveredTransparencies) { |
| 91 | for (const auto background : {SK_ColorBLACK, SK_ColorWHITE}) { |
| 92 | for (auto foreground : {SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE}) { |
| 93 | for (int i = 1; i < 4; i++) { |
| 94 | foreground = SkColorSetA(foreground, SK_AlphaOPAQUE * i / 4); |
| 95 | const float background_contrast_ratio = color_utils::GetContrastRatio( |
| 96 | background, |
| 97 | GetContrastingPressedOrHoveredColor(foreground, background)); |
| 98 | const float base_contrast_ratio = GetBaseContrastRatio(); |
| 99 | EXPECT_GE(background_contrast_ratio, base_contrast_ratio); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Tests that colors are not modified if the feature flag is disabled. |
| 106 | TEST_F(NativeThemeBaseTest, GetContrastingPressedOrHoveredDisableFeature) { |
| 107 | base::test::ScopedFeatureList feature_list; |
| 108 | feature_list.InitAndDisableFeature( |
| 109 | ::features::kModifyScrollbarCssColorOnHoverOrPress); |
| 110 | EXPECT_EQ(GetContrastingPressedOrHoveredColor(SK_ColorRED, SK_ColorBLACK), |
| 111 | SK_ColorRED); |
| 112 | } |
| 113 | |
| 114 | } // namespace ui |