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

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

Issue 2715793002: Change mock canvases to paint into SkCanvas (Closed)
Patch Set: Fix deps Created 3 years, 9 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 | « ui/views/BUILD.gn ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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>
11 #include <vector> 11 #include <vector>
12 12
13 #include "cc/paint/paint_recorder.h"
13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/skia/include/core/SkCanvas.h" 15 #include "third_party/skia/include/core/SkCanvas.h"
15 #include "third_party/skia/include/core/SkPaint.h" 16 #include "third_party/skia/include/core/SkPaint.h"
16 #include "third_party/skia/include/core/SkRRect.h" 17 #include "third_party/skia/include/core/SkRRect.h"
17 #include "third_party/skia/include/core/SkRect.h" 18 #include "third_party/skia/include/core/SkRect.h"
18 #include "third_party/skia/include/core/SkRefCnt.h" 19 #include "third_party/skia/include/core/SkRefCnt.h"
19 #include "ui/gfx/canvas.h" 20 #include "ui/gfx/canvas.h"
20 #include "ui/gfx/geometry/size.h" 21 #include "ui/gfx/geometry/size.h"
21 #include "ui/views/painter.h" 22 #include "ui/views/painter.h"
22 #include "ui/views/test/views_test_base.h" 23 #include "ui/views/test/views_test_base.h"
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 139
139 DISALLOW_COPY_AND_ASSIGN(MockPainter); 140 DISALLOW_COPY_AND_ASSIGN(MockPainter);
140 }; 141 };
141 142
142 } // namespace 143 } // namespace
143 144
144 namespace views { 145 namespace views {
145 146
146 class BorderTest : public ViewsTestBase { 147 class BorderTest : public ViewsTestBase {
147 public: 148 public:
149 enum {
150 // The canvas should be much bigger than the view.
151 kCanvasWidth = 1000,
152 kCanvasHeight = 500,
153 };
154
148 void SetUp() override { 155 void SetUp() override {
149 ViewsTestBase::SetUp(); 156 ViewsTestBase::SetUp();
150 157
151 view_.reset(new views::View()); 158 view_.reset(new views::View());
152 view_->SetSize(gfx::Size(100, 50)); 159 view_->SetSize(gfx::Size(100, 50));
153 // The canvas should be much bigger than the view. 160 recorder_.reset(new cc::PaintRecorder());
154 sk_canvas_.reset(new MockCanvas(1000, 500)); 161 canvas_.reset(new gfx::Canvas(
155 canvas_.reset(new gfx::Canvas(sk_canvas_.get(), 1.0f)); 162 recorder_->beginRecording(SkRect::MakeWH(kCanvasWidth, kCanvasHeight)),
163 1.0f));
156 } 164 }
157 165
158 void TearDown() override { 166 void TearDown() override {
159 ViewsTestBase::TearDown(); 167 ViewsTestBase::TearDown();
160 168
161 canvas_.reset(); 169 canvas_.reset();
162 sk_canvas_.reset(); 170 recorder_.reset();
163 view_.reset(); 171 view_.reset();
164 } 172 }
165 173
174 std::unique_ptr<MockCanvas> DrawIntoMockCanvas() {
175 sk_sp<cc::PaintRecord> record = recorder_->finishRecordingAsPicture();
176 std::unique_ptr<MockCanvas> mock(
177 new MockCanvas(kCanvasWidth, kCanvasHeight));
178 record->playback(mock.get());
179 return mock;
180 }
181
166 protected: 182 protected:
183 std::unique_ptr<cc::PaintRecorder> recorder_;
167 std::unique_ptr<views::View> view_; 184 std::unique_ptr<views::View> view_;
168 std::unique_ptr<MockCanvas> sk_canvas_;
169 std::unique_ptr<gfx::Canvas> canvas_; 185 std::unique_ptr<gfx::Canvas> canvas_;
170 }; 186 };
171 187
172 TEST_F(BorderTest, NullBorder) { 188 TEST_F(BorderTest, NullBorder) {
173 std::unique_ptr<Border> border(NullBorder()); 189 std::unique_ptr<Border> border(NullBorder());
174 EXPECT_FALSE(border); 190 EXPECT_FALSE(border);
175 } 191 }
176 192
177 TEST_F(BorderTest, SolidBorder) { 193 TEST_F(BorderTest, SolidBorder) {
178 const SkColor kBorderColor = SK_ColorMAGENTA; 194 const SkColor kBorderColor = SK_ColorMAGENTA;
179 std::unique_ptr<Border> border(CreateSolidBorder(3, kBorderColor)); 195 std::unique_ptr<Border> border(CreateSolidBorder(3, kBorderColor));
180 EXPECT_EQ(gfx::Size(6, 6), border->GetMinimumSize()); 196 EXPECT_EQ(gfx::Size(6, 6), border->GetMinimumSize());
181 EXPECT_EQ(gfx::Insets(3, 3, 3, 3), border->GetInsets()); 197 EXPECT_EQ(gfx::Insets(3, 3, 3, 3), border->GetInsets());
182 border->Paint(*view_, canvas_.get()); 198 border->Paint(*view_, canvas_.get());
183 199
200 std::unique_ptr<MockCanvas> mock = DrawIntoMockCanvas();
201 std::vector<MockCanvas::DrawRectCall> draw_rect_calls =
202 mock->draw_rect_calls();
203
184 gfx::Rect bounds = view_->GetLocalBounds(); 204 gfx::Rect bounds = view_->GetLocalBounds();
185 bounds.Inset(border->GetInsets()); 205 bounds.Inset(border->GetInsets());
186 206
187 ASSERT_EQ(1u, sk_canvas_->draw_paint_calls().size()); 207 ASSERT_EQ(1u, mock->draw_paint_calls().size());
188 EXPECT_EQ(kBorderColor, sk_canvas_->draw_paint_calls()[0].getColor()); 208 EXPECT_EQ(kBorderColor, mock->draw_paint_calls()[0].getColor());
189 EXPECT_EQ(gfx::RectF(bounds), 209 EXPECT_EQ(gfx::RectF(bounds), gfx::SkRectToRectF(mock->last_clip_bounds()));
190 gfx::SkRectToRectF(sk_canvas_->last_clip_bounds()));
191 } 210 }
192 211
193 TEST_F(BorderTest, RoundedRectBorder) { 212 TEST_F(BorderTest, RoundedRectBorder) {
194 std::unique_ptr<Border> border(CreateRoundedRectBorder(3, 4, SK_ColorBLUE)); 213 std::unique_ptr<Border> border(CreateRoundedRectBorder(3, 4, SK_ColorBLUE));
195 EXPECT_EQ(gfx::Size(6, 6), border->GetMinimumSize()); 214 EXPECT_EQ(gfx::Size(6, 6), border->GetMinimumSize());
196 EXPECT_EQ(gfx::Insets(3, 3, 3, 3), border->GetInsets()); 215 EXPECT_EQ(gfx::Insets(3, 3, 3, 3), border->GetInsets());
197 border->Paint(*view_, canvas_.get()); 216 border->Paint(*view_, canvas_.get());
198 217
218 std::unique_ptr<MockCanvas> mock = DrawIntoMockCanvas();
199 SkRRect expected_rrect; 219 SkRRect expected_rrect;
200 expected_rrect.setRectXY(SkRect::MakeLTRB(1.5, 1.5, 98.5, 48.5), 4, 4); 220 expected_rrect.setRectXY(SkRect::MakeLTRB(1.5, 1.5, 98.5, 48.5), 4, 4);
201 EXPECT_TRUE(sk_canvas_->draw_rect_calls().empty()); 221 EXPECT_TRUE(mock->draw_rect_calls().empty());
202 std::vector<MockCanvas::DrawRRectCall> draw_rrect_calls = 222 std::vector<MockCanvas::DrawRRectCall> draw_rrect_calls =
203 sk_canvas_->draw_rrect_calls(); 223 mock->draw_rrect_calls();
204 ASSERT_EQ(1u, draw_rrect_calls.size()); 224 ASSERT_EQ(1u, draw_rrect_calls.size());
205 EXPECT_EQ(expected_rrect, draw_rrect_calls[0].rrect); 225 EXPECT_EQ(expected_rrect, draw_rrect_calls[0].rrect);
206 EXPECT_EQ(3, draw_rrect_calls[0].paint.getStrokeWidth()); 226 EXPECT_EQ(3, draw_rrect_calls[0].paint.getStrokeWidth());
207 EXPECT_EQ(SK_ColorBLUE, draw_rrect_calls[0].paint.getColor()); 227 EXPECT_EQ(SK_ColorBLUE, draw_rrect_calls[0].paint.getColor());
208 EXPECT_EQ(SkPaint::kStroke_Style, draw_rrect_calls[0].paint.getStyle()); 228 EXPECT_EQ(SkPaint::kStroke_Style, draw_rrect_calls[0].paint.getStyle());
209 EXPECT_TRUE(draw_rrect_calls[0].paint.isAntiAlias()); 229 EXPECT_TRUE(draw_rrect_calls[0].paint.isAntiAlias());
210 } 230 }
211 231
212 TEST_F(BorderTest, EmptyBorder) { 232 TEST_F(BorderTest, EmptyBorder) {
213 const gfx::Insets kInsets(1, 2, 3, 4); 233 const gfx::Insets kInsets(1, 2, 3, 4);
(...skipping 14 matching lines...) Expand all
228 const SkColor kBorderColor = SK_ColorMAGENTA; 248 const SkColor kBorderColor = SK_ColorMAGENTA;
229 const gfx::Insets kInsets(1, 2, 3, 4); 249 const gfx::Insets kInsets(1, 2, 3, 4);
230 250
231 std::unique_ptr<Border> border( 251 std::unique_ptr<Border> border(
232 CreateSolidSidedBorder(kInsets.top(), kInsets.left(), kInsets.bottom(), 252 CreateSolidSidedBorder(kInsets.top(), kInsets.left(), kInsets.bottom(),
233 kInsets.right(), kBorderColor)); 253 kInsets.right(), kBorderColor));
234 EXPECT_EQ(gfx::Size(6, 4), border->GetMinimumSize()); 254 EXPECT_EQ(gfx::Size(6, 4), border->GetMinimumSize());
235 EXPECT_EQ(kInsets, border->GetInsets()); 255 EXPECT_EQ(kInsets, border->GetInsets());
236 border->Paint(*view_, canvas_.get()); 256 border->Paint(*view_, canvas_.get());
237 257
258 std::unique_ptr<MockCanvas> mock = DrawIntoMockCanvas();
259 std::vector<MockCanvas::DrawRectCall> draw_rect_calls =
260 mock->draw_rect_calls();
261
238 gfx::Rect bounds = view_->GetLocalBounds(); 262 gfx::Rect bounds = view_->GetLocalBounds();
239 bounds.Inset(border->GetInsets()); 263 bounds.Inset(border->GetInsets());
240 264
241 ASSERT_EQ(1u, sk_canvas_->draw_paint_calls().size()); 265 ASSERT_EQ(1u, mock->draw_paint_calls().size());
242 EXPECT_EQ(kBorderColor, sk_canvas_->draw_paint_calls()[0].getColor()); 266 EXPECT_EQ(kBorderColor, mock->draw_paint_calls()[0].getColor());
243 EXPECT_EQ(gfx::RectF(bounds), 267 EXPECT_EQ(gfx::RectF(bounds), gfx::SkRectToRectF(mock->last_clip_bounds()));
244 gfx::SkRectToRectF(sk_canvas_->last_clip_bounds()));
245 } 268 }
246 269
247 TEST_F(BorderTest, BorderPainter) { 270 TEST_F(BorderTest, BorderPainter) {
248 const gfx::Insets kInsets(1, 2, 3, 4); 271 const gfx::Insets kInsets(1, 2, 3, 4);
249 272
250 std::unique_ptr<MockPainter> painter(new MockPainter()); 273 std::unique_ptr<MockPainter> painter(new MockPainter());
251 MockPainter* painter_ptr = painter.get(); 274 MockPainter* painter_ptr = painter.get();
252 std::unique_ptr<Border> border( 275 std::unique_ptr<Border> border(
253 CreateBorderPainter(std::move(painter), kInsets)); 276 CreateBorderPainter(std::move(painter), kInsets));
254 EXPECT_EQ(gfx::Size(60, 40), border->GetMinimumSize()); 277 EXPECT_EQ(gfx::Size(60, 40), border->GetMinimumSize());
255 EXPECT_EQ(kInsets, border->GetInsets()); 278 EXPECT_EQ(kInsets, border->GetInsets());
256 279
257 border->Paint(*view_, canvas_.get()); 280 border->Paint(*view_, canvas_.get());
258 281
259 // Expect that the Painter was called with our canvas and the view's size. 282 // Expect that the Painter was called with our canvas and the view's size.
260 EXPECT_EQ(canvas_.get(), painter_ptr->given_canvas()); 283 EXPECT_EQ(canvas_.get(), painter_ptr->given_canvas());
261 EXPECT_EQ(view_->size(), painter_ptr->given_size()); 284 EXPECT_EQ(view_->size(), painter_ptr->given_size());
262 } 285 }
263 286
264 } // namespace views 287 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698