OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 | |
5 #include "ui/views/border.h" | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "testing/gmock/include/gmock/gmock.h" | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 #include "third_party/skia/include/core/SkCanvas.h" | |
11 #include "third_party/skia/include/core/SkPaint.h" | |
12 #include "third_party/skia/include/core/SkRRect.h" | |
13 #include "third_party/skia/include/core/SkRect.h" | |
14 #include "ui/gfx/canvas.h" | |
15 #include "ui/gfx/geometry/size.h" | |
16 #include "ui/views/painter.h" | |
17 #include "ui/views/test/views_test_base.h" | |
18 #include "ui/views/view.h" | |
19 | |
20 using namespace testing; | |
21 | |
22 namespace { | |
23 | |
24 class MockCanvas : public SkCanvas { | |
25 public: | |
26 MockCanvas(int width, int height) : SkCanvas(width, height) {} | |
27 | |
28 MOCK_METHOD2(onDrawRect, void(const SkRect&, const SkPaint&)); | |
29 MOCK_METHOD2(onDrawRRect, void(const SkRRect&, const SkPaint&)); | |
30 }; | |
31 | |
32 class MockPainter : public views::Painter { | |
33 public: | |
34 MOCK_CONST_METHOD0(GetMinimumSize, gfx::Size()); | |
35 MOCK_METHOD2(Paint, void(gfx::Canvas*, const gfx::Size&)); | |
36 }; | |
37 | |
38 } // namespace | |
39 | |
40 namespace views { | |
41 | |
42 class BorderTest : public ViewsTestBase { | |
43 public: | |
44 void SetUp() override { | |
45 ViewsTestBase::SetUp(); | |
46 | |
47 view_.reset(new views::View()); | |
48 view_->SetSize(gfx::Size(100, 50)); | |
49 // The canvas should be much bigger than the view. | |
50 sk_canvas_.reset(new MockCanvas(1000, 500)); | |
51 canvas_.reset(new gfx::Canvas(sk_canvas_.get(), 1.0f)); | |
danakj
2015/12/10 20:01:27
Can you make a TearDown() method that cleans this
Matt Giuca
2015/12/11 00:03:21
Done.
Thanks, I forgot that these would not be au
| |
52 } | |
53 | |
54 protected: | |
55 scoped_ptr<views::View> view_; | |
56 scoped_ptr<MockCanvas> sk_canvas_; | |
57 scoped_ptr<gfx::Canvas> canvas_; | |
58 }; | |
59 | |
60 TEST_F(BorderTest, NullBorder) { | |
61 scoped_ptr<Border> border(Border::NullBorder()); | |
62 EXPECT_FALSE(border); | |
63 } | |
64 | |
65 TEST_F(BorderTest, SolidBorder) { | |
66 scoped_ptr<Border> border(Border::CreateSolidBorder(3, SK_ColorBLUE)); | |
67 EXPECT_EQ(gfx::Size(6, 6), border->GetMinimumSize()); | |
68 EXPECT_EQ(gfx::Insets(3, 3, 3, 3), border->GetInsets()); | |
69 | |
70 // Order doesn't matter. | |
71 EXPECT_CALL(*sk_canvas_, | |
72 onDrawRect(SkRect::MakeLTRB(0, 0, 100, 3), | |
73 Property(&SkPaint::getColor, SK_ColorBLUE))); | |
74 EXPECT_CALL(*sk_canvas_, | |
75 onDrawRect(SkRect::MakeLTRB(0, 47, 100, 50), | |
76 Property(&SkPaint::getColor, SK_ColorBLUE))); | |
77 EXPECT_CALL(*sk_canvas_, | |
78 onDrawRect(SkRect::MakeLTRB(0, 3, 3, 47), | |
79 Property(&SkPaint::getColor, SK_ColorBLUE))); | |
80 EXPECT_CALL(*sk_canvas_, | |
81 onDrawRect(SkRect::MakeLTRB(97, 3, 100, 47), | |
82 Property(&SkPaint::getColor, SK_ColorBLUE))); | |
83 border->Paint(*view_, canvas_.get()); | |
84 } | |
85 | |
86 // TODO(mgiuca): Fails due to http://crbug.com/565801. | |
87 TEST_F(BorderTest, DISABLED_RoundedRectBorder) { | |
88 scoped_ptr<Border> border( | |
89 Border::CreateRoundedRectBorder(3, 4, SK_ColorBLUE)); | |
90 EXPECT_EQ(gfx::Size(6, 6), border->GetMinimumSize()); | |
91 EXPECT_EQ(gfx::Insets(3, 3, 3, 3), border->GetInsets()); | |
92 | |
93 SkRRect expected_rrect; | |
94 expected_rrect.setRectXY(SkRect::MakeLTRB(1.5, 1.5, 98.5, 48.5), 4, 4); | |
95 EXPECT_CALL( | |
96 *sk_canvas_, | |
97 onDrawRRect(expected_rrect, | |
98 AllOf(Property(&SkPaint::getStrokeWidth, 3), | |
99 Property(&SkPaint::getColor, SK_ColorBLUE), | |
100 Property(&SkPaint::getStyle, SkPaint::kStroke_Style), | |
101 Property(&SkPaint::isAntiAlias, true)))); | |
102 border->Paint(*view_, canvas_.get()); | |
103 } | |
104 | |
105 TEST_F(BorderTest, EmptyBorder) { | |
106 const gfx::Insets kInsets(1, 2, 3, 4); | |
107 | |
108 scoped_ptr<Border> border(Border::CreateEmptyBorder( | |
109 kInsets.top(), kInsets.left(), kInsets.bottom(), kInsets.right())); | |
110 // The EmptyBorder has no minimum size despite nonzero insets. | |
111 EXPECT_EQ(gfx::Size(), border->GetMinimumSize()); | |
112 EXPECT_EQ(kInsets, border->GetInsets()); | |
113 // Should have no effect. | |
114 border->Paint(*view_, canvas_.get()); | |
115 | |
116 scoped_ptr<Border> border2(Border::CreateEmptyBorder(kInsets)); | |
117 EXPECT_EQ(kInsets, border2->GetInsets()); | |
118 } | |
119 | |
120 TEST_F(BorderTest, SolidSidedBorder) { | |
121 const gfx::Insets kInsets(1, 2, 3, 4); | |
122 | |
123 scoped_ptr<Border> border(Border::CreateSolidSidedBorder( | |
124 kInsets.top(), kInsets.left(), kInsets.bottom(), kInsets.right(), | |
125 SK_ColorBLUE)); | |
126 EXPECT_EQ(gfx::Size(6, 4), border->GetMinimumSize()); | |
127 EXPECT_EQ(kInsets, border->GetInsets()); | |
128 | |
129 // Order doesn't matter. | |
130 EXPECT_CALL(*sk_canvas_, | |
131 onDrawRect(SkRect::MakeLTRB(0, 0, 100, 1), | |
132 Property(&SkPaint::getColor, SK_ColorBLUE))); | |
133 EXPECT_CALL(*sk_canvas_, | |
134 onDrawRect(SkRect::MakeLTRB(0, 47, 100, 50), | |
135 Property(&SkPaint::getColor, SK_ColorBLUE))); | |
136 EXPECT_CALL(*sk_canvas_, | |
137 onDrawRect(SkRect::MakeLTRB(0, 1, 2, 47), | |
138 Property(&SkPaint::getColor, SK_ColorBLUE))); | |
139 EXPECT_CALL(*sk_canvas_, | |
140 onDrawRect(SkRect::MakeLTRB(96, 1, 100, 47), | |
141 Property(&SkPaint::getColor, SK_ColorBLUE))); | |
142 border->Paint(*view_, canvas_.get()); | |
143 } | |
144 | |
145 TEST_F(BorderTest, BorderPainter) { | |
146 const gfx::Insets kInsets(1, 2, 3, 4); | |
147 | |
148 MockPainter* painter = new MockPainter(); | |
149 scoped_ptr<Border> border(Border::CreateBorderPainter(painter, kInsets)); | |
150 EXPECT_CALL(*painter, GetMinimumSize()).WillOnce(Return(gfx::Size(60, 40))); | |
151 EXPECT_EQ(gfx::Size(60, 40), border->GetMinimumSize()); | |
152 | |
153 EXPECT_EQ(kInsets, border->GetInsets()); | |
154 | |
155 // Expect that the Painter is called with our canvas and the view's size. | |
156 EXPECT_CALL(*painter, Paint(canvas_.get(), view_->size())); | |
157 border->Paint(*view_, canvas_.get()); | |
158 } | |
159 | |
160 } // namespace views | |
OLD | NEW |