OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/border.h" | 5 #include "ui/views/border.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <memory> | 8 #include <memory> |
9 #include <set> | 9 #include <set> |
10 #include <utility> | 10 #include <utility> |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 return std::vector<DrawRectCall>(draw_rect_calls_.begin(), | 65 return std::vector<DrawRectCall>(draw_rect_calls_.begin(), |
66 draw_rect_calls_.end()); | 66 draw_rect_calls_.end()); |
67 } | 67 } |
68 | 68 |
69 // Return calls in sorted order. | 69 // Return calls in sorted order. |
70 std::vector<DrawRRectCall> draw_rrect_calls() { | 70 std::vector<DrawRRectCall> draw_rrect_calls() { |
71 return std::vector<DrawRRectCall>(draw_rrect_calls_.begin(), | 71 return std::vector<DrawRRectCall>(draw_rrect_calls_.begin(), |
72 draw_rrect_calls_.end()); | 72 draw_rrect_calls_.end()); |
73 } | 73 } |
74 | 74 |
75 const std::vector<SkPaint>& draw_paint_calls() const { | |
76 return draw_paint_calls_; | |
77 } | |
78 | |
79 const SkRect& last_clip_bounds() const { return last_clip_bounds_; } | |
80 | |
75 // SkCanvas overrides: | 81 // SkCanvas overrides: |
76 void onDrawRect(const SkRect& rect, const SkPaint& paint) override { | 82 void onDrawRect(const SkRect& rect, const SkPaint& paint) override { |
77 draw_rect_calls_.insert(DrawRectCall(rect, paint)); | 83 draw_rect_calls_.insert(DrawRectCall(rect, paint)); |
78 } | 84 } |
79 | 85 |
80 void onDrawRRect(const SkRRect& rrect, const SkPaint& paint) override { | 86 void onDrawRRect(const SkRRect& rrect, const SkPaint& paint) override { |
81 draw_rrect_calls_.insert(DrawRRectCall(rrect, paint)); | 87 draw_rrect_calls_.insert(DrawRRectCall(rrect, paint)); |
82 } | 88 } |
83 | 89 |
90 void onDrawPaint(const SkPaint& paint) override { | |
91 draw_paint_calls_.push_back(paint); | |
92 } | |
93 | |
94 void onClipRect(const SkRect& rect, | |
95 SkClipOp op, | |
96 ClipEdgeStyle edge_style) override { | |
97 last_clip_bounds_ = rect; | |
98 } | |
99 | |
84 private: | 100 private: |
85 // Stores all the calls for querying by the test, in sorted order. | 101 // Stores all the calls for querying by the test, in sorted order. |
86 std::set<DrawRectCall> draw_rect_calls_; | 102 std::set<DrawRectCall> draw_rect_calls_; |
87 std::set<DrawRRectCall> draw_rrect_calls_; | 103 std::set<DrawRRectCall> draw_rrect_calls_; |
88 | 104 |
105 // Stores the onDrawPaint calls in chronological order. | |
Matt Giuca
2017/02/22 03:29:09
I suppose the reason for this being a vector and n
Evan Stade
2017/02/22 16:59:23
It's hard to hypothesize about what we would want
| |
106 std::vector<SkPaint> draw_paint_calls_; | |
107 SkRect last_clip_bounds_; | |
108 | |
89 DISALLOW_COPY_AND_ASSIGN(MockCanvas); | 109 DISALLOW_COPY_AND_ASSIGN(MockCanvas); |
90 }; | 110 }; |
91 | 111 |
92 // Simple Painter that will be used to test BorderPainter. | 112 // Simple Painter that will be used to test BorderPainter. |
93 class MockPainter : public views::Painter { | 113 class MockPainter : public views::Painter { |
94 public: | 114 public: |
95 MockPainter() {} | 115 MockPainter() {} |
96 | 116 |
97 // Gets the canvas given to the last call to Paint(). | 117 // Gets the canvas given to the last call to Paint(). |
98 gfx::Canvas* given_canvas() const { return given_canvas_; } | 118 gfx::Canvas* given_canvas() const { return given_canvas_; } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
148 std::unique_ptr<MockCanvas> sk_canvas_; | 168 std::unique_ptr<MockCanvas> sk_canvas_; |
149 std::unique_ptr<gfx::Canvas> canvas_; | 169 std::unique_ptr<gfx::Canvas> canvas_; |
150 }; | 170 }; |
151 | 171 |
152 TEST_F(BorderTest, NullBorder) { | 172 TEST_F(BorderTest, NullBorder) { |
153 std::unique_ptr<Border> border(NullBorder()); | 173 std::unique_ptr<Border> border(NullBorder()); |
154 EXPECT_FALSE(border); | 174 EXPECT_FALSE(border); |
155 } | 175 } |
156 | 176 |
157 TEST_F(BorderTest, SolidBorder) { | 177 TEST_F(BorderTest, SolidBorder) { |
158 std::unique_ptr<Border> border(CreateSolidBorder(3, SK_ColorBLUE)); | 178 const SkColor kBorderColor = SK_ColorMAGENTA; |
Matt Giuca
2017/02/22 03:29:09
Any reason to change this to magenta?
(Perhaps ju
Evan Stade
2017/02/22 16:59:23
no real reason. I guess I thought it was less like
Peter Kasting
2017/02/22 19:07:12
Choose border color using random number generator?
Matt Giuca
2017/02/22 22:36:13
And choose the expected border color with a 98% ch
| |
179 std::unique_ptr<Border> border(CreateSolidBorder(3, kBorderColor)); | |
159 EXPECT_EQ(gfx::Size(6, 6), border->GetMinimumSize()); | 180 EXPECT_EQ(gfx::Size(6, 6), border->GetMinimumSize()); |
160 EXPECT_EQ(gfx::Insets(3, 3, 3, 3), border->GetInsets()); | 181 EXPECT_EQ(gfx::Insets(3, 3, 3, 3), border->GetInsets()); |
161 border->Paint(*view_, canvas_.get()); | 182 border->Paint(*view_, canvas_.get()); |
162 | 183 |
163 std::vector<MockCanvas::DrawRectCall> draw_rect_calls = | 184 gfx::Rect bounds = view_->GetLocalBounds(); |
164 sk_canvas_->draw_rect_calls(); | 185 bounds.Inset(border->GetInsets()); |
165 ASSERT_EQ(4u, draw_rect_calls.size()); | |
166 EXPECT_EQ(SkRect::MakeLTRB(0, 0, 100, 3), draw_rect_calls[0].rect); | |
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 | 186 |
175 EXPECT_TRUE(sk_canvas_->draw_rrect_calls().empty()); | 187 ASSERT_EQ(1u, sk_canvas_->draw_paint_calls().size()); |
188 EXPECT_EQ(kBorderColor, sk_canvas_->draw_paint_calls()[0].getColor()); | |
189 EXPECT_EQ(gfx::RectF(bounds), | |
190 gfx::SkRectToRectF(sk_canvas_->last_clip_bounds())); | |
176 } | 191 } |
177 | 192 |
178 TEST_F(BorderTest, RoundedRectBorder) { | 193 TEST_F(BorderTest, RoundedRectBorder) { |
179 std::unique_ptr<Border> border(CreateRoundedRectBorder(3, 4, SK_ColorBLUE)); | 194 std::unique_ptr<Border> border(CreateRoundedRectBorder(3, 4, SK_ColorBLUE)); |
180 EXPECT_EQ(gfx::Size(6, 6), border->GetMinimumSize()); | 195 EXPECT_EQ(gfx::Size(6, 6), border->GetMinimumSize()); |
181 EXPECT_EQ(gfx::Insets(3, 3, 3, 3), border->GetInsets()); | 196 EXPECT_EQ(gfx::Insets(3, 3, 3, 3), border->GetInsets()); |
182 border->Paint(*view_, canvas_.get()); | 197 border->Paint(*view_, canvas_.get()); |
183 | 198 |
184 SkRRect expected_rrect; | 199 SkRRect expected_rrect; |
185 expected_rrect.setRectXY(SkRect::MakeLTRB(1.5, 1.5, 98.5, 48.5), 4, 4); | 200 expected_rrect.setRectXY(SkRect::MakeLTRB(1.5, 1.5, 98.5, 48.5), 4, 4); |
(...skipping 17 matching lines...) Expand all Loading... | |
203 EXPECT_EQ(gfx::Size(), border->GetMinimumSize()); | 218 EXPECT_EQ(gfx::Size(), border->GetMinimumSize()); |
204 EXPECT_EQ(kInsets, border->GetInsets()); | 219 EXPECT_EQ(kInsets, border->GetInsets()); |
205 // Should have no effect. | 220 // Should have no effect. |
206 border->Paint(*view_, canvas_.get()); | 221 border->Paint(*view_, canvas_.get()); |
207 | 222 |
208 std::unique_ptr<Border> border2(CreateEmptyBorder(kInsets)); | 223 std::unique_ptr<Border> border2(CreateEmptyBorder(kInsets)); |
209 EXPECT_EQ(kInsets, border2->GetInsets()); | 224 EXPECT_EQ(kInsets, border2->GetInsets()); |
210 } | 225 } |
211 | 226 |
212 TEST_F(BorderTest, SolidSidedBorder) { | 227 TEST_F(BorderTest, SolidSidedBorder) { |
228 const SkColor kBorderColor = SK_ColorMAGENTA; | |
213 const gfx::Insets kInsets(1, 2, 3, 4); | 229 const gfx::Insets kInsets(1, 2, 3, 4); |
214 | 230 |
215 std::unique_ptr<Border> border( | 231 std::unique_ptr<Border> border( |
216 CreateSolidSidedBorder(kInsets.top(), kInsets.left(), kInsets.bottom(), | 232 CreateSolidSidedBorder(kInsets.top(), kInsets.left(), kInsets.bottom(), |
217 kInsets.right(), SK_ColorBLUE)); | 233 kInsets.right(), kBorderColor)); |
218 EXPECT_EQ(gfx::Size(6, 4), border->GetMinimumSize()); | 234 EXPECT_EQ(gfx::Size(6, 4), border->GetMinimumSize()); |
219 EXPECT_EQ(kInsets, border->GetInsets()); | 235 EXPECT_EQ(kInsets, border->GetInsets()); |
220 border->Paint(*view_, canvas_.get()); | 236 border->Paint(*view_, canvas_.get()); |
221 | 237 |
222 std::vector<MockCanvas::DrawRectCall> draw_rect_calls = | 238 gfx::Rect bounds = view_->GetLocalBounds(); |
223 sk_canvas_->draw_rect_calls(); | 239 bounds.Inset(border->GetInsets()); |
224 ASSERT_EQ(4u, draw_rect_calls.size()); | |
225 EXPECT_EQ(SkRect::MakeLTRB(0, 0, 100, 1), draw_rect_calls[0].rect); | |
226 EXPECT_EQ(SK_ColorBLUE, draw_rect_calls[0].paint.getColor()); | |
227 EXPECT_EQ(SkRect::MakeLTRB(0, 1, 2, 47), draw_rect_calls[1].rect); | |
228 EXPECT_EQ(SK_ColorBLUE, draw_rect_calls[1].paint.getColor()); | |
229 EXPECT_EQ(SkRect::MakeLTRB(0, 47, 100, 50), draw_rect_calls[2].rect); | |
230 EXPECT_EQ(SK_ColorBLUE, draw_rect_calls[2].paint.getColor()); | |
231 EXPECT_EQ(SkRect::MakeLTRB(96, 1, 100, 47), draw_rect_calls[3].rect); | |
232 EXPECT_EQ(SK_ColorBLUE, draw_rect_calls[3].paint.getColor()); | |
233 | 240 |
234 EXPECT_TRUE(sk_canvas_->draw_rrect_calls().empty()); | 241 ASSERT_EQ(1u, sk_canvas_->draw_paint_calls().size()); |
242 EXPECT_EQ(kBorderColor, sk_canvas_->draw_paint_calls()[0].getColor()); | |
243 EXPECT_EQ(gfx::RectF(bounds), | |
244 gfx::SkRectToRectF(sk_canvas_->last_clip_bounds())); | |
235 } | 245 } |
236 | 246 |
237 TEST_F(BorderTest, BorderPainter) { | 247 TEST_F(BorderTest, BorderPainter) { |
238 const gfx::Insets kInsets(1, 2, 3, 4); | 248 const gfx::Insets kInsets(1, 2, 3, 4); |
239 | 249 |
240 std::unique_ptr<MockPainter> painter(new MockPainter()); | 250 std::unique_ptr<MockPainter> painter(new MockPainter()); |
241 MockPainter* painter_ptr = painter.get(); | 251 MockPainter* painter_ptr = painter.get(); |
242 std::unique_ptr<Border> border( | 252 std::unique_ptr<Border> border( |
243 CreateBorderPainter(std::move(painter), kInsets)); | 253 CreateBorderPainter(std::move(painter), kInsets)); |
244 EXPECT_EQ(gfx::Size(60, 40), border->GetMinimumSize()); | 254 EXPECT_EQ(gfx::Size(60, 40), border->GetMinimumSize()); |
245 EXPECT_EQ(kInsets, border->GetInsets()); | 255 EXPECT_EQ(kInsets, border->GetInsets()); |
246 | 256 |
247 border->Paint(*view_, canvas_.get()); | 257 border->Paint(*view_, canvas_.get()); |
248 | 258 |
249 // Expect that the Painter was called with our canvas and the view's size. | 259 // Expect that the Painter was called with our canvas and the view's size. |
250 EXPECT_EQ(canvas_.get(), painter_ptr->given_canvas()); | 260 EXPECT_EQ(canvas_.get(), painter_ptr->given_canvas()); |
251 EXPECT_EQ(view_->size(), painter_ptr->given_size()); | 261 EXPECT_EQ(view_->size(), painter_ptr->given_size()); |
252 } | 262 } |
253 | 263 |
254 } // namespace views | 264 } // namespace views |
OLD | NEW |