native_theme_aura_unittest.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2016 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 "ui/native_theme/native_theme_aura.h"
  5. #include "testing/gtest/include/gtest/gtest.h"
  6. #include "third_party/skia/include/core/SkPath.h"
  7. #include "ui/gfx/geometry/rect.h"
  8. #include "ui/native_theme/native_theme.h"
  9. namespace ui {
  10. namespace {
  11. void VerifyPoint(SkPoint a, SkPoint b) {
  12. EXPECT_EQ(a.x(), b.x());
  13. EXPECT_EQ(a.y(), b.y());
  14. }
  15. void VerifyTriangle(SkPath actualPath, SkPoint p0, SkPoint p1, SkPoint p2) {
  16. EXPECT_EQ(3, actualPath.countPoints());
  17. VerifyPoint(p0, actualPath.getPoint(0));
  18. VerifyPoint(p1, actualPath.getPoint(1));
  19. VerifyPoint(p2, actualPath.getPoint(2));
  20. }
  21. } // namespace
  22. class NativeThemeAuraTest : public testing::Test {
  23. protected:
  24. NativeThemeAuraTest() = default;
  25. SkPath PathForArrow(const gfx::Rect& rect,
  26. NativeTheme::Part direction) const {
  27. return theme_.PathForArrow(rect, direction);
  28. }
  29. gfx::Rect BoundingRectForArrow(const gfx::Rect& rect) const {
  30. return theme_.BoundingRectForArrow(rect);
  31. }
  32. private:
  33. NativeThemeAura theme_{false, false};
  34. };
  35. TEST_F(NativeThemeAuraTest, VerticalArrows) {
  36. SkPath path;
  37. // Up arrow, sized for 1x.
  38. path =
  39. PathForArrow(gfx::Rect(100, 200, 17, 17), NativeTheme::kScrollbarUpArrow);
  40. VerifyTriangle(path, SkPoint::Make(105, 211), SkPoint::Make(112, 211),
  41. SkPoint::Make(108.5, 207));
  42. // 1.25x, should be larger.
  43. path =
  44. PathForArrow(gfx::Rect(50, 70, 21, 21), NativeTheme::kScrollbarUpArrow);
  45. VerifyTriangle(path, SkPoint::Make(56, 84), SkPoint::Make(65, 84),
  46. SkPoint::Make(60.5, 79));
  47. // Down arrow is just a flipped up arrow.
  48. path =
  49. PathForArrow(gfx::Rect(20, 80, 17, 17), NativeTheme::kScrollbarDownArrow);
  50. VerifyTriangle(path, SkPoint::Make(25, 86), SkPoint::Make(32, 86),
  51. SkPoint::Make(28.5, 90));
  52. }
  53. TEST_F(NativeThemeAuraTest, HorizontalArrows) {
  54. SkPath path;
  55. // Right arrow, sized for 1x.
  56. path = PathForArrow(gfx::Rect(100, 200, 17, 17),
  57. NativeTheme::kScrollbarRightArrow);
  58. VerifyTriangle(path, SkPoint::Make(107, 205), SkPoint::Make(107, 212),
  59. SkPoint::Make(111, 208.5));
  60. // Button size for 1.25x, should be larger.
  61. path = PathForArrow(gfx::Rect(50, 70, 21, 21),
  62. NativeTheme::kScrollbarRightArrow);
  63. VerifyTriangle(path, SkPoint::Make(58, 76), SkPoint::Make(58, 85),
  64. SkPoint::Make(63, 80.5));
  65. // Left arrow is just a flipped right arrow.
  66. path =
  67. PathForArrow(gfx::Rect(20, 80, 17, 17), NativeTheme::kScrollbarLeftArrow);
  68. VerifyTriangle(path, SkPoint::Make(30, 85), SkPoint::Make(30, 92),
  69. SkPoint::Make(26, 88.5));
  70. }
  71. TEST_F(NativeThemeAuraTest, ArrowForNonSquareButton) {
  72. SkPath path =
  73. PathForArrow(gfx::Rect(90, 80, 42, 37), NativeTheme::kScrollbarLeftArrow);
  74. VerifyTriangle(path, SkPoint::Make(116, 89), SkPoint::Make(116, 109),
  75. SkPoint::Make(105, 99));
  76. }
  77. TEST_F(NativeThemeAuraTest, BoundingRectSquare) {
  78. gfx::Rect bounding_rect = BoundingRectForArrow(gfx::Rect(42, 61, 21, 21));
  79. EXPECT_EQ(48.f, bounding_rect.x());
  80. EXPECT_EQ(67.f, bounding_rect.y());
  81. EXPECT_EQ(9.f, bounding_rect.width());
  82. EXPECT_EQ(bounding_rect.width(), bounding_rect.height());
  83. }
  84. TEST_F(NativeThemeAuraTest, BoundingRectSlightlyRectangular) {
  85. // Stretched horzontally.
  86. gfx::Rect bounding_rect = BoundingRectForArrow(gfx::Rect(42, 61, 25, 20));
  87. EXPECT_EQ(49.f, bounding_rect.x());
  88. EXPECT_EQ(66.f, bounding_rect.y());
  89. EXPECT_EQ(11.f, bounding_rect.width());
  90. EXPECT_EQ(bounding_rect.width(), bounding_rect.height());
  91. // Stretched vertically.
  92. bounding_rect = BoundingRectForArrow(gfx::Rect(42, 61, 14, 10));
  93. EXPECT_EQ(46.f, bounding_rect.x());
  94. EXPECT_EQ(63.f, bounding_rect.y());
  95. EXPECT_EQ(6.f, bounding_rect.width());
  96. EXPECT_EQ(bounding_rect.width(), bounding_rect.height());
  97. }
  98. TEST_F(NativeThemeAuraTest, BoundingRectVeryRectangular) {
  99. // Stretched horzontally.
  100. gfx::Rect bounding_rect = BoundingRectForArrow(gfx::Rect(42, 61, 30, 8));
  101. EXPECT_EQ(53.f, bounding_rect.x());
  102. EXPECT_EQ(61.f, bounding_rect.y());
  103. EXPECT_EQ(8.f, bounding_rect.width());
  104. EXPECT_EQ(bounding_rect.width(), bounding_rect.height());
  105. // Stretched vertically.
  106. bounding_rect = BoundingRectForArrow(gfx::Rect(42, 61, 6, 44));
  107. EXPECT_EQ(42.f, bounding_rect.x());
  108. EXPECT_EQ(80.f, bounding_rect.y());
  109. EXPECT_EQ(6.f, bounding_rect.width());
  110. EXPECT_EQ(bounding_rect.width(), bounding_rect.height());
  111. }
  112. TEST_F(NativeThemeAuraTest, BoundingRectSnappedToWholePixels) {
  113. gfx::Rect bounding_rect = BoundingRectForArrow(gfx::Rect(0, 0, 9, 10));
  114. EXPECT_EQ(3.f, bounding_rect.x());
  115. bounding_rect = BoundingRectForArrow(gfx::Rect(0, 0, 10, 9));
  116. EXPECT_EQ(3.f, bounding_rect.y());
  117. }
  118. } // namespace ui