border_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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/views/border.h"
  5. #include <algorithm>
  6. #include <memory>
  7. #include <set>
  8. #include <utility>
  9. #include <vector>
  10. #include "base/memory/raw_ptr.h"
  11. #include "cc/paint/paint_recorder.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. #include "third_party/skia/include/core/SkCanvas.h"
  14. #include "third_party/skia/include/core/SkPaint.h"
  15. #include "third_party/skia/include/core/SkRRect.h"
  16. #include "third_party/skia/include/core/SkRect.h"
  17. #include "third_party/skia/include/core/SkRefCnt.h"
  18. #include "ui/gfx/canvas.h"
  19. #include "ui/gfx/geometry/size.h"
  20. #include "ui/gfx/geometry/skia_conversions.h"
  21. #include "ui/views/painter.h"
  22. #include "ui/views/test/views_test_base.h"
  23. #include "ui/views/view.h"
  24. namespace {
  25. class MockCanvas : public SkCanvas {
  26. public:
  27. struct DrawRectCall {
  28. DrawRectCall(const SkRect& rect, const SkPaint& paint)
  29. : rect(rect), paint(paint) {}
  30. bool operator<(const DrawRectCall& other) const {
  31. return std::tie(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom) <
  32. std::tie(other.rect.fLeft, other.rect.fTop, other.rect.fRight,
  33. other.rect.fBottom);
  34. }
  35. SkRect rect;
  36. SkPaint paint;
  37. };
  38. struct DrawRRectCall {
  39. DrawRRectCall(const SkRRect& rrect, const SkPaint& paint)
  40. : rrect(rrect), paint(paint) {}
  41. bool operator<(const DrawRRectCall& other) const {
  42. SkRect rect = rrect.rect();
  43. SkRect other_rect = other.rrect.rect();
  44. return std::tie(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom) <
  45. std::tie(other_rect.fLeft, other_rect.fTop, other_rect.fRight,
  46. other_rect.fBottom);
  47. }
  48. SkRRect rrect;
  49. SkPaint paint;
  50. };
  51. MockCanvas(int width, int height) : SkCanvas(width, height) {}
  52. MockCanvas(const MockCanvas&) = delete;
  53. MockCanvas& operator=(const MockCanvas&) = delete;
  54. // Return calls in sorted order.
  55. std::vector<DrawRectCall> draw_rect_calls() {
  56. return std::vector<DrawRectCall>(draw_rect_calls_.begin(),
  57. draw_rect_calls_.end());
  58. }
  59. // Return calls in sorted order.
  60. std::vector<DrawRRectCall> draw_rrect_calls() {
  61. return std::vector<DrawRRectCall>(draw_rrect_calls_.begin(),
  62. draw_rrect_calls_.end());
  63. }
  64. const std::vector<SkPaint>& draw_paint_calls() const {
  65. return draw_paint_calls_;
  66. }
  67. const SkRect& last_clip_bounds() const { return last_clip_bounds_; }
  68. // SkCanvas overrides:
  69. void onDrawRect(const SkRect& rect, const SkPaint& paint) override {
  70. draw_rect_calls_.insert(DrawRectCall(rect, paint));
  71. }
  72. void onDrawRRect(const SkRRect& rrect, const SkPaint& paint) override {
  73. draw_rrect_calls_.insert(DrawRRectCall(rrect, paint));
  74. }
  75. void onDrawPaint(const SkPaint& paint) override {
  76. draw_paint_calls_.push_back(paint);
  77. }
  78. void onClipRect(const SkRect& rect,
  79. SkClipOp op,
  80. ClipEdgeStyle edge_style) override {
  81. last_clip_bounds_ = rect;
  82. }
  83. private:
  84. // Stores all the calls for querying by the test, in sorted order.
  85. std::set<DrawRectCall> draw_rect_calls_;
  86. std::set<DrawRRectCall> draw_rrect_calls_;
  87. // Stores the onDrawPaint calls in chronological order.
  88. std::vector<SkPaint> draw_paint_calls_;
  89. SkRect last_clip_bounds_;
  90. };
  91. // Simple Painter that will be used to test BorderPainter.
  92. class MockPainter : public views::Painter {
  93. public:
  94. MockPainter() = default;
  95. MockPainter(const MockPainter&) = delete;
  96. MockPainter& operator=(const MockPainter&) = delete;
  97. // Gets the canvas given to the last call to Paint().
  98. gfx::Canvas* given_canvas() const { return given_canvas_; }
  99. // Gets the size given to the last call to Paint().
  100. const gfx::Size& given_size() const { return given_size_; }
  101. // Painter overrides:
  102. gfx::Size GetMinimumSize() const override {
  103. // Just return some arbitrary size.
  104. return gfx::Size(60, 40);
  105. }
  106. void Paint(gfx::Canvas* canvas, const gfx::Size& size) override {
  107. // Just record the arguments.
  108. given_canvas_ = canvas;
  109. given_size_ = size;
  110. }
  111. private:
  112. raw_ptr<gfx::Canvas> given_canvas_ = nullptr;
  113. gfx::Size given_size_;
  114. };
  115. } // namespace
  116. namespace views {
  117. class BorderTest : public ViewsTestBase {
  118. public:
  119. enum {
  120. // The canvas should be much bigger than the view.
  121. kCanvasWidth = 1000,
  122. kCanvasHeight = 500,
  123. };
  124. void SetUp() override {
  125. ViewsTestBase::SetUp();
  126. view_ = std::make_unique<views::View>();
  127. view_->SetSize(gfx::Size(100, 50));
  128. recorder_ = std::make_unique<cc::PaintRecorder>();
  129. canvas_ = std::make_unique<gfx::Canvas>(
  130. recorder_->beginRecording(SkRect::MakeWH(kCanvasWidth, kCanvasHeight)),
  131. 1.0f);
  132. }
  133. void TearDown() override {
  134. ViewsTestBase::TearDown();
  135. canvas_.reset();
  136. recorder_.reset();
  137. view_.reset();
  138. }
  139. std::unique_ptr<MockCanvas> DrawIntoMockCanvas() {
  140. sk_sp<cc::PaintRecord> record = recorder_->finishRecordingAsPicture();
  141. std::unique_ptr<MockCanvas> mock(
  142. new MockCanvas(kCanvasWidth, kCanvasHeight));
  143. record->Playback(mock.get());
  144. return mock;
  145. }
  146. protected:
  147. std::unique_ptr<cc::PaintRecorder> recorder_;
  148. std::unique_ptr<views::View> view_;
  149. std::unique_ptr<gfx::Canvas> canvas_;
  150. };
  151. TEST_F(BorderTest, NullBorder) {
  152. std::unique_ptr<Border> border(NullBorder());
  153. EXPECT_FALSE(border);
  154. }
  155. TEST_F(BorderTest, SolidBorder) {
  156. const SkColor kBorderColor = SK_ColorMAGENTA;
  157. std::unique_ptr<Border> border(CreateSolidBorder(3, kBorderColor));
  158. EXPECT_EQ(gfx::Size(6, 6), border->GetMinimumSize());
  159. EXPECT_EQ(gfx::Insets(3), border->GetInsets());
  160. border->Paint(*view_, canvas_.get());
  161. std::unique_ptr<MockCanvas> mock = DrawIntoMockCanvas();
  162. std::vector<MockCanvas::DrawRectCall> draw_rect_calls =
  163. mock->draw_rect_calls();
  164. gfx::Rect bounds = view_->GetLocalBounds();
  165. bounds.Inset(border->GetInsets());
  166. ASSERT_EQ(1u, mock->draw_paint_calls().size());
  167. EXPECT_EQ(kBorderColor, mock->draw_paint_calls()[0].getColor());
  168. EXPECT_EQ(gfx::RectF(bounds), gfx::SkRectToRectF(mock->last_clip_bounds()));
  169. }
  170. TEST_F(BorderTest, RoundedRectBorder) {
  171. std::unique_ptr<Border> border(CreateRoundedRectBorder(
  172. 3, LayoutProvider::Get()->GetCornerRadiusMetric(Emphasis::kLow),
  173. SK_ColorBLUE));
  174. EXPECT_EQ(gfx::Size(6, 6), border->GetMinimumSize());
  175. EXPECT_EQ(gfx::Insets(3), border->GetInsets());
  176. border->Paint(*view_, canvas_.get());
  177. std::unique_ptr<MockCanvas> mock = DrawIntoMockCanvas();
  178. SkRRect expected_rrect;
  179. expected_rrect.setRectXY(SkRect::MakeLTRB(1.5, 1.5, 98.5, 48.5), 2.5, 2.5);
  180. EXPECT_TRUE(mock->draw_rect_calls().empty());
  181. std::vector<MockCanvas::DrawRRectCall> draw_rrect_calls =
  182. mock->draw_rrect_calls();
  183. ASSERT_EQ(1u, draw_rrect_calls.size());
  184. EXPECT_EQ(expected_rrect, draw_rrect_calls[0].rrect);
  185. EXPECT_EQ(3, draw_rrect_calls[0].paint.getStrokeWidth());
  186. EXPECT_EQ(SK_ColorBLUE, draw_rrect_calls[0].paint.getColor());
  187. EXPECT_EQ(SkPaint::kStroke_Style, draw_rrect_calls[0].paint.getStyle());
  188. EXPECT_TRUE(draw_rrect_calls[0].paint.isAntiAlias());
  189. }
  190. TEST_F(BorderTest, EmptyBorder) {
  191. constexpr auto kInsets = gfx::Insets::TLBR(1, 2, 3, 4);
  192. std::unique_ptr<Border> border(CreateEmptyBorder(kInsets));
  193. // The EmptyBorder has no minimum size despite nonzero insets.
  194. EXPECT_EQ(gfx::Size(), border->GetMinimumSize());
  195. EXPECT_EQ(kInsets, border->GetInsets());
  196. // Should have no effect.
  197. border->Paint(*view_, canvas_.get());
  198. std::unique_ptr<Border> border2(CreateEmptyBorder(kInsets));
  199. EXPECT_EQ(kInsets, border2->GetInsets());
  200. }
  201. TEST_F(BorderTest, SolidSidedBorder) {
  202. constexpr SkColor kBorderColor = SK_ColorMAGENTA;
  203. constexpr auto kInsets = gfx::Insets::TLBR(1, 2, 3, 4);
  204. std::unique_ptr<Border> border(CreateSolidSidedBorder(kInsets, kBorderColor));
  205. EXPECT_EQ(gfx::Size(6, 4), border->GetMinimumSize());
  206. EXPECT_EQ(kInsets, border->GetInsets());
  207. border->Paint(*view_, canvas_.get());
  208. std::unique_ptr<MockCanvas> mock = DrawIntoMockCanvas();
  209. std::vector<MockCanvas::DrawRectCall> draw_rect_calls =
  210. mock->draw_rect_calls();
  211. gfx::Rect bounds = view_->GetLocalBounds();
  212. bounds.Inset(border->GetInsets());
  213. ASSERT_EQ(1u, mock->draw_paint_calls().size());
  214. EXPECT_EQ(kBorderColor, mock->draw_paint_calls().front().getColor());
  215. EXPECT_EQ(gfx::RectF(bounds), gfx::SkRectToRectF(mock->last_clip_bounds()));
  216. }
  217. TEST_F(BorderTest, BorderPainter) {
  218. constexpr auto kInsets = gfx::Insets::TLBR(1, 2, 3, 4);
  219. std::unique_ptr<MockPainter> painter(new MockPainter());
  220. MockPainter* painter_ptr = painter.get();
  221. std::unique_ptr<Border> border(
  222. CreateBorderPainter(std::move(painter), kInsets));
  223. EXPECT_EQ(gfx::Size(60, 40), border->GetMinimumSize());
  224. EXPECT_EQ(kInsets, border->GetInsets());
  225. border->Paint(*view_, canvas_.get());
  226. // Expect that the Painter was called with our canvas and the view's size.
  227. EXPECT_EQ(canvas_.get(), painter_ptr->given_canvas());
  228. EXPECT_EQ(view_->size(), painter_ptr->given_size());
  229. }
  230. TEST_F(BorderTest, ExtraInsetsBorder) {
  231. constexpr SkColor kBorderColor = SK_ColorMAGENTA;
  232. constexpr int kOriginalInset = 3;
  233. std::unique_ptr<Border> border =
  234. CreateSolidBorder(kOriginalInset, kBorderColor);
  235. constexpr gfx::Insets kOriginalInsets(kOriginalInset);
  236. EXPECT_EQ(kOriginalInsets.size(), border->GetMinimumSize());
  237. EXPECT_EQ(kOriginalInsets, border->GetInsets());
  238. EXPECT_EQ(kBorderColor, border->color());
  239. constexpr int kExtraInset = 2;
  240. constexpr gfx::Insets kExtraInsets(kExtraInset);
  241. std::unique_ptr<Border> extra_insets_border =
  242. CreatePaddedBorder(std::move(border), kExtraInsets);
  243. constexpr gfx::Insets kTotalInsets(kOriginalInset + kExtraInset);
  244. EXPECT_EQ(kTotalInsets.size(), extra_insets_border->GetMinimumSize());
  245. EXPECT_EQ(kTotalInsets, extra_insets_border->GetInsets());
  246. EXPECT_EQ(kBorderColor, extra_insets_border->color());
  247. extra_insets_border->Paint(*view_, canvas_.get());
  248. std::unique_ptr<MockCanvas> mock = DrawIntoMockCanvas();
  249. std::vector<MockCanvas::DrawRectCall> draw_rect_calls =
  250. mock->draw_rect_calls();
  251. gfx::Rect bounds = view_->GetLocalBounds();
  252. // We only use the wrapped border's insets for painting the border. The extra
  253. // insets of the ExtraInsetsBorder are applied within the wrapped border.
  254. bounds.Inset(extra_insets_border->GetInsets() - gfx::Insets(kExtraInset));
  255. ASSERT_EQ(1u, mock->draw_paint_calls().size());
  256. EXPECT_EQ(kBorderColor, mock->draw_paint_calls().front().getColor());
  257. EXPECT_EQ(gfx::RectF(bounds), gfx::SkRectToRectF(mock->last_clip_bounds()));
  258. }
  259. } // namespace views