Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: ui/views/border_unittest.cc

Issue 1517463003: Added comprehensive tests for views::Border. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | ui/views/views.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
5 #include "ui/views/border.h"
6
7 #include <algorithm>
8 #include <memory>
9 #include <set>
10 #include <utility>
11 #include <vector>
12
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/skia/include/core/SkCanvas.h"
15 #include "third_party/skia/include/core/SkPaint.h"
16 #include "third_party/skia/include/core/SkRRect.h"
17 #include "third_party/skia/include/core/SkRect.h"
18 #include "third_party/skia/include/core/SkRefCnt.h"
19 #include "ui/gfx/canvas.h"
20 #include "ui/gfx/geometry/size.h"
21 #include "ui/views/painter.h"
22 #include "ui/views/test/views_test_base.h"
23 #include "ui/views/view.h"
24
25 using namespace testing;
26
27 namespace {
28
29 class MockCanvas : public SkCanvas {
30 public:
31 struct DrawRectCall {
32 DrawRectCall(const SkRect& rect, const SkPaint& paint)
33 : rect(rect), paint(paint) {}
34
35 bool operator<(const DrawRectCall& other) const {
36 return std::tie(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom) <
37 std::tie(other.rect.fLeft, other.rect.fTop, other.rect.fRight,
38 other.rect.fBottom);
39 }
40
41 SkRect rect;
42 SkPaint paint;
43 };
44
45 struct DrawRRectCall {
46 DrawRRectCall(const SkRRect& rrect, const SkPaint& paint)
47 : rrect(rrect), paint(paint) {}
48
49 bool operator<(const DrawRRectCall& other) const {
50 SkRect rect = rrect.rect();
51 SkRect other_rect = other.rrect.rect();
52 return std::tie(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom) <
53 std::tie(other_rect.fLeft, other_rect.fTop, other_rect.fRight,
54 other_rect.fBottom);
55 }
56
57 SkRRect rrect;
58 SkPaint paint;
59 };
60
61 MockCanvas(int width, int height) : SkCanvas(width, height) {}
62
63 // Return calls in sorted order.
64 std::vector<DrawRectCall> draw_rect_calls() {
65 return std::vector<DrawRectCall>(draw_rect_calls_.begin(),
66 draw_rect_calls_.end());
67 }
68
69 // Return calls in sorted order.
70 std::vector<DrawRRectCall> draw_rrect_calls() {
71 return std::vector<DrawRRectCall>(draw_rrect_calls_.begin(),
sadrul 2016/06/08 16:02:55 Does this automatically sort the vector?
Matt Giuca 2016/06/09 01:49:38 Yes, because the std::set iterator intrinsically r
72 draw_rrect_calls_.end());
73 }
74
75 // SkCanvas overrides:
76 void onDrawRect(const SkRect& rect, const SkPaint& paint) override {
77 draw_rect_calls_.insert(DrawRectCall(rect, paint));
78 }
79
80 void onDrawRRect(const SkRRect& rrect, const SkPaint& paint) override {
81 draw_rrect_calls_.insert(DrawRRectCall(rrect, paint));
82 }
83
84 private:
85 // Stores all the calls for querying by the test.
86 std::set<DrawRectCall> draw_rect_calls_;
87 std::set<DrawRRectCall> draw_rrect_calls_;
88
89 DISALLOW_COPY_AND_ASSIGN(MockCanvas);
90 };
91
92 // Simple Painter that will be used to test BorderPainter.
93 class MockPainter : public views::Painter {
94 public:
95 MockPainter() {}
96
97 // Gets the canvas given to the last call to Paint().
98 gfx::Canvas* given_canvas() const { return given_canvas_; }
99
100 // Gets the size given to the last call to Paint().
101 const gfx::Size& given_size() const { return given_size_; }
102
103 // Painter overrides:
104 gfx::Size GetMinimumSize() const override {
105 // Just return some arbitrary size.
106 return gfx::Size(60, 40);
107 }
108
109 void Paint(gfx::Canvas* canvas, const gfx::Size& size) override {
110 // Just record the arguments.
111 given_canvas_ = canvas;
112 given_size_ = size;
113 }
114
115 private:
116 gfx::Canvas* given_canvas_ = nullptr;
117 gfx::Size given_size_;
118
119 DISALLOW_COPY_AND_ASSIGN(MockPainter);
120 };
121
122 } // namespace
123
124 namespace views {
125
126 class BorderTest : public ViewsTestBase {
127 public:
128 void SetUp() override {
129 ViewsTestBase::SetUp();
130
131 view_.reset(new views::View());
132 view_->SetSize(gfx::Size(100, 50));
133 // The canvas should be much bigger than the view.
134 sk_canvas_.reset(new MockCanvas(1000, 500));
135 canvas_.reset(new gfx::Canvas(sk_canvas_, 1.0f));
136 }
137
138 void TearDown() override {
139 ViewsTestBase::TearDown();
140
141 canvas_.reset();
142 sk_canvas_.reset();
143 view_.reset();
144 }
145
146 protected:
147 std::unique_ptr<views::View> view_;
148 sk_sp<MockCanvas> sk_canvas_;
149 std::unique_ptr<gfx::Canvas> canvas_;
150 };
151
152 TEST_F(BorderTest, NullBorder) {
153 std::unique_ptr<Border> border(Border::NullBorder());
154 EXPECT_FALSE(border);
155 }
156
157 TEST_F(BorderTest, SolidBorder) {
158 std::unique_ptr<Border> border(Border::CreateSolidBorder(3, SK_ColorBLUE));
159 EXPECT_EQ(gfx::Size(6, 6), border->GetMinimumSize());
160 EXPECT_EQ(gfx::Insets(3, 3, 3, 3), border->GetInsets());
161 border->Paint(*view_, canvas_.get());
162
163 std::vector<MockCanvas::DrawRectCall> draw_rect_calls =
164 sk_canvas_->draw_rect_calls();
165 ASSERT_EQ(4u, draw_rect_calls.size());
166 EXPECT_EQ(SkRect::MakeLTRB(0, 0, 100, 3), draw_rect_calls[0].rect);
sadrul 2016/06/08 16:02:55 Can't you just use set::find() instead?
Matt Giuca 2016/06/09 01:49:38 I could but then I'd have to construct an entire S
sadrul 2016/06/13 00:47:38 OK. I would've preferred if you made DrawRectCall
Matt Giuca 2016/06/14 04:39:02 That means we have to specify exactly which attrib
167 EXPECT_EQ(SK_ColorBLUE, draw_rect_calls[0].paint.getColor());
168 EXPECT_EQ(SkRect::MakeLTRB(0, 3, 3, 47), draw_rect_calls[1].rect);
169 EXPECT_EQ(SK_ColorBLUE, draw_rect_calls[1].paint.getColor());
170 EXPECT_EQ(SkRect::MakeLTRB(0, 47, 100, 50), draw_rect_calls[2].rect);
171 EXPECT_EQ(SK_ColorBLUE, draw_rect_calls[2].paint.getColor());
172 EXPECT_EQ(SkRect::MakeLTRB(97, 3, 100, 47), draw_rect_calls[3].rect);
173 EXPECT_EQ(SK_ColorBLUE, draw_rect_calls[3].paint.getColor());
174
175 EXPECT_TRUE(sk_canvas_->draw_rrect_calls().empty());
176 }
177
178 TEST_F(BorderTest, RoundedRectBorder) {
179 std::unique_ptr<Border> border(
180 Border::CreateRoundedRectBorder(3, 4, SK_ColorBLUE));
181 EXPECT_EQ(gfx::Size(6, 6), border->GetMinimumSize());
182 EXPECT_EQ(gfx::Insets(3, 3, 3, 3), border->GetInsets());
183 border->Paint(*view_, canvas_.get());
184
185 SkRRect expected_rrect;
186 expected_rrect.setRectXY(SkRect::MakeLTRB(1.5, 1.5, 98.5, 48.5), 4, 4);
187 EXPECT_TRUE(sk_canvas_->draw_rect_calls().empty());
188 std::vector<MockCanvas::DrawRRectCall> draw_rrect_calls =
189 sk_canvas_->draw_rrect_calls();
190 ASSERT_EQ(1u, draw_rrect_calls.size());
191 EXPECT_EQ(expected_rrect, draw_rrect_calls[0].rrect);
192 EXPECT_EQ(3, draw_rrect_calls[0].paint.getStrokeWidth());
193 EXPECT_EQ(SK_ColorBLUE, draw_rrect_calls[0].paint.getColor());
194 EXPECT_EQ(SkPaint::kStroke_Style, draw_rrect_calls[0].paint.getStyle());
195 EXPECT_TRUE(draw_rrect_calls[0].paint.isAntiAlias());
196 }
197
198 TEST_F(BorderTest, EmptyBorder) {
199 const gfx::Insets kInsets(1, 2, 3, 4);
200
201 std::unique_ptr<Border> border(Border::CreateEmptyBorder(
202 kInsets.top(), kInsets.left(), kInsets.bottom(), kInsets.right()));
203 // The EmptyBorder has no minimum size despite nonzero insets.
204 EXPECT_EQ(gfx::Size(), border->GetMinimumSize());
205 EXPECT_EQ(kInsets, border->GetInsets());
206 // Should have no effect.
207 border->Paint(*view_, canvas_.get());
208
209 std::unique_ptr<Border> border2(Border::CreateEmptyBorder(kInsets));
210 EXPECT_EQ(kInsets, border2->GetInsets());
211 }
212
213 TEST_F(BorderTest, SolidSidedBorder) {
214 const gfx::Insets kInsets(1, 2, 3, 4);
215
216 std::unique_ptr<Border> border(Border::CreateSolidSidedBorder(
217 kInsets.top(), kInsets.left(), kInsets.bottom(), kInsets.right(),
218 SK_ColorBLUE));
219 EXPECT_EQ(gfx::Size(6, 4), border->GetMinimumSize());
220 EXPECT_EQ(kInsets, border->GetInsets());
221 border->Paint(*view_, canvas_.get());
222
223 std::vector<MockCanvas::DrawRectCall> draw_rect_calls =
224 sk_canvas_->draw_rect_calls();
225 ASSERT_EQ(4u, draw_rect_calls.size());
226 EXPECT_EQ(SkRect::MakeLTRB(0, 0, 100, 1), draw_rect_calls[0].rect);
227 EXPECT_EQ(SK_ColorBLUE, draw_rect_calls[0].paint.getColor());
228 EXPECT_EQ(SkRect::MakeLTRB(0, 1, 2, 47), draw_rect_calls[1].rect);
229 EXPECT_EQ(SK_ColorBLUE, draw_rect_calls[1].paint.getColor());
230 EXPECT_EQ(SkRect::MakeLTRB(0, 47, 100, 50), draw_rect_calls[2].rect);
231 EXPECT_EQ(SK_ColorBLUE, draw_rect_calls[2].paint.getColor());
232 EXPECT_EQ(SkRect::MakeLTRB(96, 1, 100, 47), draw_rect_calls[3].rect);
233 EXPECT_EQ(SK_ColorBLUE, draw_rect_calls[3].paint.getColor());
234
235 EXPECT_TRUE(sk_canvas_->draw_rrect_calls().empty());
236 }
237
238 TEST_F(BorderTest, BorderPainter) {
239 const gfx::Insets kInsets(1, 2, 3, 4);
240
241 std::unique_ptr<MockPainter> painter(new MockPainter());
242 MockPainter* painter_ptr = painter.get();
243 std::unique_ptr<Border> border(
244 Border::CreateBorderPainter(std::move(painter), kInsets));
245 EXPECT_EQ(gfx::Size(60, 40), border->GetMinimumSize());
246 EXPECT_EQ(kInsets, border->GetInsets());
247
248 border->Paint(*view_, canvas_.get());
249
250 // Expect that the Painter was called with our canvas and the view's size.
251 EXPECT_EQ(canvas_.get(), painter_ptr->given_canvas());
252 EXPECT_EQ(view_->size(), painter_ptr->given_size());
253 }
254
255 } // namespace views
OLDNEW
« no previous file with comments | « no previous file | ui/views/views.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698