Yaroslav Shalivskyy | 80ae54e | 2022-09-12 20:54:17 | [diff] [blame] | 1 | // Copyright 2022 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_fluent.h" |
| 6 | |
| 7 | #include "testing/gtest/include/gtest/gtest.h" |
Yaroslav Shalivskyy | aad7659 | 2022-10-31 19:01:29 | [diff] [blame] | 8 | #include "third_party/skia/include/core/SkTypeface.h" |
| 9 | #include "ui/gfx/geometry/rect_conversions.h" |
| 10 | #include "ui/gfx/geometry/rect_f.h" |
Yaroslav Shalivskyy | 80ae54e | 2022-09-12 20:54:17 | [diff] [blame] | 11 | #include "ui/native_theme/native_theme_constants_fluent.h" |
| 12 | |
| 13 | namespace ui { |
| 14 | |
| 15 | class NativeThemeFluentTest : public ::testing::Test, |
| 16 | public ::testing::WithParamInterface<float> { |
| 17 | protected: |
Yaroslav Shalivskyy | aad7659 | 2022-10-31 19:01:29 | [diff] [blame] | 18 | void VerifyArrowRectCommonDimensions(const gfx::RectF& arrow_rect) const { |
| 19 | EXPECT_FALSE(arrow_rect.IsEmpty()); |
| 20 | EXPECT_EQ(arrow_rect.width(), arrow_rect.height()); |
| 21 | EXPECT_EQ(arrow_rect.width(), std::floor(arrow_rect.width())); |
Yaroslav Shalivskyy | 80ae54e | 2022-09-12 20:54:17 | [diff] [blame] | 22 | } |
| 23 | |
Yaroslav Shalivskyy | aad7659 | 2022-10-31 19:01:29 | [diff] [blame] | 24 | void VerifyArrowRectIsCentered(const gfx::RectF& button_rect, |
| 25 | const gfx::RectF& arrow_rect, |
| 26 | NativeTheme::Part part) const { |
| 27 | if (part == NativeTheme::kScrollbarUpArrow || |
| 28 | part == NativeTheme::kScrollbarDownArrow) { |
| 29 | EXPECT_EQ(button_rect.CenterPoint().x(), arrow_rect.CenterPoint().x()); |
| 30 | // Due to the offset the arrow rect is shifted from the center. |
| 31 | // See NativeThemeFluent::OffsetArrowRect() for more details. Same below. |
| 32 | EXPECT_NEAR(button_rect.CenterPoint().y(), arrow_rect.CenterPoint().y(), |
| 33 | ScaleFromDIP() * 2); |
| 34 | } else { |
| 35 | EXPECT_EQ(button_rect.CenterPoint().y(), arrow_rect.CenterPoint().y()); |
| 36 | EXPECT_NEAR(button_rect.CenterPoint().x(), arrow_rect.CenterPoint().x(), |
| 37 | ScaleFromDIP() * 2); |
| 38 | } |
Yaroslav Shalivskyy | 80ae54e | 2022-09-12 20:54:17 | [diff] [blame] | 39 | } |
| 40 | |
Yaroslav Shalivskyy | aad7659 | 2022-10-31 19:01:29 | [diff] [blame] | 41 | void VerifyArrowRectIsIntRect(const gfx::RectF& arrow_rect) const { |
| 42 | if (theme_.ArrowIconsAvailable()) |
| 43 | return; |
Yaroslav Shalivskyy | 80ae54e | 2022-09-12 20:54:17 | [diff] [blame] | 44 | |
Yaroslav Shalivskyy | aad7659 | 2022-10-31 19:01:29 | [diff] [blame] | 45 | // Verify that an arrow rect with triangular arrows is an integer rect. |
| 46 | EXPECT_TRUE(IsNearestRectWithinDistance(arrow_rect, 0.01f)); |
Yaroslav Shalivskyy | 80ae54e | 2022-09-12 20:54:17 | [diff] [blame] | 47 | } |
| 48 | |
Yaroslav Shalivskyy | aad7659 | 2022-10-31 19:01:29 | [diff] [blame] | 49 | void VerifyArrowRectLengthRatio(const gfx::RectF& button_rect, |
| 50 | const gfx::RectF& arrow_rect, |
| 51 | NativeTheme::State state) const { |
| 52 | const int smaller_button_side = |
| 53 | std::min(button_rect.width(), button_rect.height()); |
| 54 | if (state == NativeTheme::kNormal) { |
| 55 | // Default state arrows are slightly bigger than the half of the button's |
| 56 | // smaller side (track thickness). |
| 57 | EXPECT_GT(arrow_rect.width(), smaller_button_side / 2.0f); |
| 58 | EXPECT_LT(arrow_rect.width(), smaller_button_side); |
| 59 | } else { |
| 60 | EXPECT_GT(arrow_rect.width(), smaller_button_side / 3.0f); |
| 61 | EXPECT_LT(arrow_rect.width(), smaller_button_side / 1.5f); |
| 62 | } |
Yaroslav Shalivskyy | 80ae54e | 2022-09-12 20:54:17 | [diff] [blame] | 63 | } |
| 64 | |
Yaroslav Shalivskyy | aad7659 | 2022-10-31 19:01:29 | [diff] [blame] | 65 | void VerifyArrowRect() const { |
| 66 | for (auto const& part : |
| 67 | {NativeTheme::kScrollbarUpArrow, NativeTheme::kScrollbarLeftArrow}) { |
| 68 | const gfx::RectF button_rect(ButtonRect(part)); |
| 69 | for (auto const& state : {NativeTheme::kNormal, NativeTheme::kPressed}) { |
| 70 | const gfx::RectF arrow_rect = |
| 71 | theme_.GetArrowRect(ToNearestRect(button_rect), part, state); |
| 72 | VerifyArrowRectCommonDimensions(arrow_rect); |
| 73 | VerifyArrowRectIsIntRect(arrow_rect); |
| 74 | VerifyArrowRectIsCentered(button_rect, arrow_rect, part); |
| 75 | VerifyArrowRectLengthRatio(button_rect, arrow_rect, state); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | gfx::RectF ButtonRect(NativeTheme::Part part) const { |
| 81 | const int button_length = |
| 82 | base::ClampFloor(kFluentScrollbarButtonSideLength * ScaleFromDIP()); |
| 83 | const int track_thickness = |
| 84 | base::ClampFloor(kFluentScrollbarThickness * ScaleFromDIP()); |
| 85 | |
| 86 | if (part == NativeTheme::kScrollbarUpArrow || |
| 87 | part == NativeTheme::kScrollbarDownArrow) |
| 88 | return gfx::RectF(0, 0, track_thickness, button_length); |
| 89 | |
| 90 | return gfx::RectF(0, 0, button_length, track_thickness); |
Yaroslav Shalivskyy | 80ae54e | 2022-09-12 20:54:17 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | float ScaleFromDIP() const { return GetParam(); } |
Yaroslav Shalivskyy | aad7659 | 2022-10-31 19:01:29 | [diff] [blame] | 94 | |
| 95 | // Mocks the availability of the font for drawing arrow icons. |
| 96 | void SetArrowIconsAvailable(bool enabled) { |
| 97 | if (enabled) { |
| 98 | theme_.typeface_ = SkTypeface::MakeDefault(); |
| 99 | EXPECT_TRUE(theme_.ArrowIconsAvailable()); |
| 100 | } else { |
| 101 | theme_.typeface_ = nullptr; |
| 102 | EXPECT_FALSE(theme_.ArrowIconsAvailable()); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | NativeThemeFluent theme_{false}; |
Yaroslav Shalivskyy | 80ae54e | 2022-09-12 20:54:17 | [diff] [blame] | 107 | }; |
| 108 | |
Yaroslav Shalivskyy | aad7659 | 2022-10-31 19:01:29 | [diff] [blame] | 109 | // Verify the dimensions of an arrow rect with triangular arrows for a given |
| 110 | // button rect depending on the arrow direction and state. |
| 111 | TEST_P(NativeThemeFluentTest, VerifyArrowRectWithTriangularArrows) { |
| 112 | SetArrowIconsAvailable(false); |
| 113 | VerifyArrowRect(); |
Yaroslav Shalivskyy | 80ae54e | 2022-09-12 20:54:17 | [diff] [blame] | 114 | } |
| 115 | |
Yaroslav Shalivskyy | aad7659 | 2022-10-31 19:01:29 | [diff] [blame] | 116 | // Verify the dimensions of an arrow rect with arrow icons for a given |
| 117 | // button rect depending on the arrow direction and state. |
| 118 | TEST_P(NativeThemeFluentTest, VerifyArrowRectWithArrowIcons) { |
| 119 | SetArrowIconsAvailable(true); |
| 120 | VerifyArrowRect(); |
Yaroslav Shalivskyy | 80ae54e | 2022-09-12 20:54:17 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | INSTANTIATE_TEST_SUITE_P(All, |
| 124 | NativeThemeFluentTest, |
| 125 | ::testing::Values(1.f, 1.25f, 1.5f, 1.75f, 2.f)); |
| 126 | |
| 127 | } // namespace ui |