blob: d1f0abd19f0c6cdd1280244f7ec481461c96743c [file] [log] [blame]
reed@google.com37f3ae02011-11-28 16:06:04 +00001/*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +00002 * Copyright 2012 Google Inc.
reed@google.com37f3ae02011-11-28 16:06:04 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +00007
8/* Description:
9 * This test defines a series of elementatry test steps that perform
10 * a single or a small group of canvas API calls. Each test step is
11 * used in several test cases that verify that different types of SkCanvas
12 * flavors and derivatives pass it and yield consistent behavior. The
13 * test cases analyse results that are queryable through the API. They do
14 * not look at rendering results.
15 *
16 * Adding test stepss:
17 * The general pattern for creating a new test step is to write a test
18 * function of the form:
19 *
rmistry@google.comd6176b02012-08-23 18:14:13 +000020 * static void MyTestStepFunction(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000021 * skiatest::Reporter* reporter,
22 * CanvasTestStep* testStep)
23 * {
24 * canvas->someCanvasAPImethod();
25 * (...)
26 * REPORTER_ASSERT_MESSAGE(reporter, (...), \
27 * testStep->assertMessage());
28 * }
29 *
30 * The definition of the test step function should be followed by an
31 * invocation of the TEST_STEP macro, which generates a class and
32 * instance for the test step:
33 *
34 * TEST_STEP(MyTestStep, MyTestStepFunction)
35 *
36 * There are also short hand macros for defining simple test steps
37 * in a single line of code. A simple test step is a one that is made
38 * of a single canvas API call.
39 *
40 * SIMPLE_TEST_STEP(MytestStep, someCanvasAPIMethod());
41 *
42 * There is another macro called SIMPLE_TEST_STEP_WITH_ASSERT that
43 * works the same way as SIMPLE_TEST_STEP, and additionally verifies
44 * that the invoked method returns a non-zero value.
45 */
reed@google.com37f3ae02011-11-28 16:06:04 +000046#include "SkBitmap.h"
47#include "SkCanvas.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000048#include "SkDeferredCanvas.h"
49#include "SkDevice.h"
50#include "SkMatrix.h"
51#include "SkNWayCanvas.h"
edisonn@google.com77909122012-10-18 15:58:23 +000052#include "SkPDFDevice.h"
53#include "SkPDFDocument.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000054#include "SkPaint.h"
55#include "SkPath.h"
56#include "SkPicture.h"
57#include "SkPictureRecord.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000058#include "SkPictureRecorder.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000059#include "SkRect.h"
60#include "SkRegion.h"
61#include "SkShader.h"
62#include "SkStream.h"
reed@google.com28183b42014-02-04 15:34:10 +000063#include "SkSurface.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000064#include "SkTDArray.h"
65#include "Test.h"
reed@google.com37f3ae02011-11-28 16:06:04 +000066
piotaixrf05f5a72014-10-03 13:26:55 -070067static const int kWidth = 2, kHeight = 2;
68
69static void createBitmap(SkBitmap* bm, SkColor color) {
70 bm->allocN32Pixels(kWidth, kHeight);
71 bm->eraseColor(color);
72}
73
74static SkSurface* createSurface(SkColor color) {
reed3054be12014-12-10 07:24:28 -080075 SkSurface* surface = SkSurface::NewRasterN32Premul(kWidth, kHeight);
piotaixrf05f5a72014-10-03 13:26:55 -070076 surface->getCanvas()->clear(color);
77 return surface;
78}
79
80///////////////////////////////////////////////////////////////////////////////
81// Constants used by test steps
82const SkPoint kTestPoints[] = {
83 {SkIntToScalar(0), SkIntToScalar(0)},
84 {SkIntToScalar(2), SkIntToScalar(1)},
85 {SkIntToScalar(0), SkIntToScalar(2)}
86};
87const SkPoint kTestPoints2[] = {
88 { SkIntToScalar(0), SkIntToScalar(1) },
89 { SkIntToScalar(1), SkIntToScalar(1) },
90 { SkIntToScalar(2), SkIntToScalar(1) },
91 { SkIntToScalar(3), SkIntToScalar(1) },
92 { SkIntToScalar(4), SkIntToScalar(1) },
93 { SkIntToScalar(5), SkIntToScalar(1) },
94 { SkIntToScalar(6), SkIntToScalar(1) },
95 { SkIntToScalar(7), SkIntToScalar(1) },
96 { SkIntToScalar(8), SkIntToScalar(1) },
97 { SkIntToScalar(9), SkIntToScalar(1) },
98 { SkIntToScalar(10), SkIntToScalar(1) }
99};
100
101struct TestData {
102public:
103 TestData()
104 : fRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
105 SkIntToScalar(2), SkIntToScalar(1)))
106 , fMatrix(TestMatrix())
107 , fPath(TestPath())
108 , fNearlyZeroLengthPath(TestNearlyZeroLengthPath())
109 , fIRect(SkIRect::MakeXYWH(0, 0, 2, 1))
110 , fRegion(TestRegion())
111 , fColor(0x01020304)
112 , fPoints(kTestPoints)
113 , fPointCount(3)
114 , fWidth(2)
115 , fHeight(2)
116 , fText("Hello World")
117 , fPoints2(kTestPoints2)
118 , fBitmap(TestBitmap())
119 { }
120
121 SkRect fRect;
122 SkMatrix fMatrix;;
123 SkPath fPath;
124 SkPath fNearlyZeroLengthPath;
125 SkIRect fIRect;
126 SkRegion fRegion;
127 SkColor fColor;
128 SkPaint fPaint;
129 const SkPoint* fPoints;
130 size_t fPointCount;
131 int fWidth;
132 int fHeight;
133 SkString fText;
134 const SkPoint* fPoints2;
135 SkBitmap fBitmap;
136
137private:
138 static SkMatrix TestMatrix() {
139 SkMatrix matrix;
140 matrix.reset();
141 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
142
143 return matrix;
144 }
145 static SkPath TestPath() {
146 SkPath path;
147 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
148 SkIntToScalar(2), SkIntToScalar(1)));
149 return path;
150 }
151 static SkPath TestNearlyZeroLengthPath() {
152 SkPath path;
153 SkPoint pt1 = { 0, 0 };
154 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
155 SkPoint pt3 = { SkIntToScalar(1), 0 };
156 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
157 path.moveTo(pt1);
158 path.lineTo(pt2);
159 path.lineTo(pt3);
160 path.lineTo(pt4);
161 return path;
162 }
163 static SkRegion TestRegion() {
164 SkRegion region;
165 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
166 region.setRect(rect);
167 return region;
168 }
169 static SkBitmap TestBitmap() {
170 SkBitmap bitmap;
171 createBitmap(&bitmap, 0x05060708);
172 return bitmap;
173 }
174};
175
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000176static bool equal_clips(const SkCanvas& a, const SkCanvas& b) {
177 if (a.isClipEmpty()) {
178 return b.isClipEmpty();
179 }
180 if (!a.isClipRect()) {
181 // this is liberally true, since we don't expose a way to know this exactly (for non-rects)
182 return !b.isClipRect();
183 }
184 SkIRect ar, br;
185 a.getClipDeviceBounds(&ar);
186 b.getClipDeviceBounds(&br);
187 return ar == br;
188}
189
reed@google.com90c07ea2012-04-13 13:50:27 +0000190class Canvas2CanvasClipVisitor : public SkCanvas::ClipVisitor {
191public:
192 Canvas2CanvasClipVisitor(SkCanvas* target) : fTarget(target) {}
193
mtklein72c9faa2015-01-09 10:06:39 -0800194 void clipRect(const SkRect& r, SkRegion::Op op, bool aa) SK_OVERRIDE {
reed@google.com90c07ea2012-04-13 13:50:27 +0000195 fTarget->clipRect(r, op, aa);
196 }
mtklein72c9faa2015-01-09 10:06:39 -0800197 void clipRRect(const SkRRect& r, SkRegion::Op op, bool aa) SK_OVERRIDE {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000198 fTarget->clipRRect(r, op, aa);
199 }
mtklein72c9faa2015-01-09 10:06:39 -0800200 void clipPath(const SkPath& p, SkRegion::Op op, bool aa) SK_OVERRIDE {
reed@google.com90c07ea2012-04-13 13:50:27 +0000201 fTarget->clipPath(p, op, aa);
202 }
203
204private:
205 SkCanvas* fTarget;
206};
207
208static void test_clipVisitor(skiatest::Reporter* reporter, SkCanvas* canvas) {
209 SkISize size = canvas->getDeviceSize();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000210
reed@google.com90c07ea2012-04-13 13:50:27 +0000211 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000212 bm.setInfo(SkImageInfo::MakeN32Premul(size.width(), size.height()));
reed@google.com90c07ea2012-04-13 13:50:27 +0000213 SkCanvas c(bm);
214
215 Canvas2CanvasClipVisitor visitor(&c);
216 canvas->replayClips(&visitor);
217
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000218 REPORTER_ASSERT(reporter, equal_clips(c, *canvas));
reed@google.com90c07ea2012-04-13 13:50:27 +0000219}
220
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000221// Format strings that describe the test context. The %s token is where
222// the name of the test step is inserted. The context is required for
223// disambiguating the error in the case of failures that are reported in
224// functions that are called multiple times in different contexts (test
225// cases and test steps).
226static const char* const kDefaultAssertMessageFormat = "%s";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000227static const char* const kCanvasDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000228 "Drawing test step %s with SkCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000229static const char* const kDeferredDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000230 "Drawing test step %s with SkDeferredCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000231static const char* const kNWayDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000232 "Drawing test step %s with SkNWayCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000233static const char* const kDeferredPreFlushAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000234 "test step %s, SkDeferredCanvas state consistency before flush";
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000235static const char* const kDeferredPostFlushPlaybackAssertMessageFormat =
236 "test step %s, SkDeferredCanvas playback canvas state consistency after flush";
junov@chromium.orgfb103892012-09-20 19:35:43 +0000237static const char* const kDeferredPostSilentFlushPlaybackAssertMessageFormat =
238 "test step %s, SkDeferredCanvas playback canvas state consistency after silent flush";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000239static const char* const kNWayStateAssertMessageFormat =
240 "test step %s, SkNWayCanvas state consistency";
241static const char* const kNWayIndirect1StateAssertMessageFormat =
242 "test step %s, SkNWayCanvas indirect canvas 1 state consistency";
243static const char* const kNWayIndirect2StateAssertMessageFormat =
244 "test step %s, SkNWayCanvas indirect canvas 2 state consistency";
edisonn@google.com77909122012-10-18 15:58:23 +0000245static const char* const kPdfAssertMessageFormat =
246 "PDF sanity check failed %s";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000247
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000248class CanvasTestStep;
249static SkTDArray<CanvasTestStep*>& testStepArray() {
250 static SkTDArray<CanvasTestStep*> theTests;
251 return theTests;
252}
253
254class CanvasTestStep {
255public:
edisonn@google.com77909122012-10-18 15:58:23 +0000256 CanvasTestStep(bool fEnablePdfTesting = true) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000257 *testStepArray().append() = this;
258 fAssertMessageFormat = kDefaultAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000259 this->fEnablePdfTesting = fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000260 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000261 virtual ~CanvasTestStep() { }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000262
piotaixrf05f5a72014-10-03 13:26:55 -0700263 virtual void draw(SkCanvas*, const TestData&, skiatest::Reporter*) = 0;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000264 virtual const char* name() const = 0;
265
266 const char* assertMessage() {
267 fAssertMessage.printf(fAssertMessageFormat, name());
268 return fAssertMessage.c_str();
269 }
270
271 void setAssertMessageFormat(const char* format) {
272 fAssertMessageFormat = format;
273 }
274
edisonn@google.com77909122012-10-18 15:58:23 +0000275 bool enablePdfTesting() { return fEnablePdfTesting; }
276
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000277private:
278 SkString fAssertMessage;
279 const char* fAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000280 bool fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000281};
282
283///////////////////////////////////////////////////////////////////////////////
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000284// Macros for defining test steps
285
286#define TEST_STEP(NAME, FUNCTION) \
287class NAME##_TestStep : public CanvasTestStep{ \
288public: \
piotaixrf05f5a72014-10-03 13:26:55 -0700289 virtual void draw(SkCanvas* canvas, const TestData& d, \
290 skiatest::Reporter* reporter) { \
291 FUNCTION (canvas, d, reporter, this); \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000292 } \
293 virtual const char* name() const {return #NAME ;} \
294}; \
295static NAME##_TestStep NAME##_TestStepInstance;
296
piotaixrf05f5a72014-10-03 13:26:55 -0700297#define TEST_STEP_NO_PDF(NAME, FUNCTION) \
edisonn@google.com77909122012-10-18 15:58:23 +0000298class NAME##_TestStep : public CanvasTestStep{ \
299public: \
300 NAME##_TestStep() : CanvasTestStep(false) {} \
piotaixrf05f5a72014-10-03 13:26:55 -0700301 virtual void draw(SkCanvas* canvas, const TestData& d, \
302 skiatest::Reporter* reporter) { \
303 FUNCTION (canvas, d, reporter, this); \
edisonn@google.com77909122012-10-18 15:58:23 +0000304 } \
305 virtual const char* name() const {return #NAME ;} \
306}; \
307static NAME##_TestStep NAME##_TestStepInstance;
308
piotaixrf05f5a72014-10-03 13:26:55 -0700309#define SIMPLE_TEST_STEP(NAME, CALL) \
310static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
311 skiatest::Reporter*, CanvasTestStep*) { \
312 canvas-> CALL ; \
313} \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000314TEST_STEP(NAME, NAME##TestStep )
315
316#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
piotaixrf05f5a72014-10-03 13:26:55 -0700317static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
318 skiatest::Reporter*, CanvasTestStep* testStep) { \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000319 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
320 testStep->assertMessage()); \
321} \
322TEST_STEP(NAME, NAME##TestStep )
323
324
325///////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000326// Basic test steps for most virtual methods in SkCanvas that draw or affect
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000327// the state of the canvas.
328
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000329SIMPLE_TEST_STEP(Translate, translate(SkIntToScalar(1), SkIntToScalar(2)));
330SIMPLE_TEST_STEP(Scale, scale(SkIntToScalar(1), SkIntToScalar(2)));
331SIMPLE_TEST_STEP(Rotate, rotate(SkIntToScalar(1)));
332SIMPLE_TEST_STEP(Skew, skew(SkIntToScalar(1), SkIntToScalar(2)));
piotaixrf05f5a72014-10-03 13:26:55 -0700333SIMPLE_TEST_STEP(Concat, concat(d.fMatrix));
334SIMPLE_TEST_STEP(SetMatrix, setMatrix(d.fMatrix));
335SIMPLE_TEST_STEP(ClipRect, clipRect(d.fRect));
336SIMPLE_TEST_STEP(ClipPath, clipPath(d.fPath));
337SIMPLE_TEST_STEP(ClipRegion, clipRegion(d.fRegion, SkRegion::kReplace_Op));
338SIMPLE_TEST_STEP(Clear, clear(d.fColor));
piotaixrf05f5a72014-10-03 13:26:55 -0700339SIMPLE_TEST_STEP(BeginGroup, beginCommentGroup(d.fText.c_str()));
340SIMPLE_TEST_STEP(AddComment, addComment(d.fText.c_str(), d.fText.c_str()));
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000341SIMPLE_TEST_STEP(EndGroup, endCommentGroup());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000342
343///////////////////////////////////////////////////////////////////////////////
344// Complex test steps
345
piotaixrf05f5a72014-10-03 13:26:55 -0700346static void SaveMatrixClipStep(SkCanvas* canvas, const TestData& d,
347 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000348 int saveCount = canvas->getSaveCount();
Florin Malita5f6102d2014-06-30 10:13:28 -0400349 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000350 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
piotaixrf05f5a72014-10-03 13:26:55 -0700351 canvas->clipRegion(d.fRegion);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000352 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000353 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000354 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000355 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000356 testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000357// REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion, testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000358}
359TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
360
piotaixrf05f5a72014-10-03 13:26:55 -0700361static void SaveLayerStep(SkCanvas* canvas, const TestData& d,
362 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000363 int saveCount = canvas->getSaveCount();
364 canvas->saveLayer(NULL, NULL);
365 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000366 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000367 testStep->assertMessage());
368}
369TEST_STEP(SaveLayer, SaveLayerStep);
370
piotaixrf05f5a72014-10-03 13:26:55 -0700371static void BoundedSaveLayerStep(SkCanvas* canvas, const TestData& d,
372 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000373 int saveCount = canvas->getSaveCount();
piotaixrf05f5a72014-10-03 13:26:55 -0700374 canvas->saveLayer(&d.fRect, NULL);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000375 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000376 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000377 testStep->assertMessage());
378}
379TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
380
piotaixrf05f5a72014-10-03 13:26:55 -0700381static void PaintSaveLayerStep(SkCanvas* canvas, const TestData& d,
382 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000383 int saveCount = canvas->getSaveCount();
piotaixrf05f5a72014-10-03 13:26:55 -0700384 canvas->saveLayer(NULL, &d.fPaint);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000385 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000386 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000387 testStep->assertMessage());
388}
389TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
390
piotaixrf05f5a72014-10-03 13:26:55 -0700391static void TwoClipOpsStep(SkCanvas* canvas, const TestData& d,
392 skiatest::Reporter*, CanvasTestStep*) {
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000393 // This test exercises a functionality in SkPicture that leads to the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000394 // recording of restore offset placeholders. This test will trigger an
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000395 // assertion at playback time if the placeholders are not properly
396 // filled when the recording ends.
piotaixrf05f5a72014-10-03 13:26:55 -0700397 canvas->clipRect(d.fRect);
398 canvas->clipRegion(d.fRegion);
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000399}
400TEST_STEP(TwoClipOps, TwoClipOpsStep);
401
epoger@google.com94fa43c2012-04-11 17:51:01 +0000402// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
403// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
piotaixrf05f5a72014-10-03 13:26:55 -0700404static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas, const TestData& d,
405 skiatest::Reporter*, CanvasTestStep*) {
epoger@google.com94fa43c2012-04-11 17:51:01 +0000406 SkPaint paint;
407 paint.setStrokeWidth(SkIntToScalar(1));
408 paint.setStyle(SkPaint::kStroke_Style);
409
piotaixrf05f5a72014-10-03 13:26:55 -0700410 canvas->drawPath(d.fNearlyZeroLengthPath, paint);
epoger@google.com94fa43c2012-04-11 17:51:01 +0000411}
412TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
413
piotaixrf05f5a72014-10-03 13:26:55 -0700414static void DrawVerticesShaderTestStep(SkCanvas* canvas, const TestData& d,
415 skiatest::Reporter*, CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000416 SkPoint pts[4];
417 pts[0].set(0, 0);
piotaixrf05f5a72014-10-03 13:26:55 -0700418 pts[1].set(SkIntToScalar(d.fWidth), 0);
419 pts[2].set(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight));
420 pts[3].set(0, SkIntToScalar(d.fHeight));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000421 SkPaint paint;
piotaixrf05f5a72014-10-03 13:26:55 -0700422 SkShader* shader = SkShader::CreateBitmapShader(d.fBitmap,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000423 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
424 paint.setShader(shader)->unref();
425 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
426 NULL, NULL, NULL, 0, paint);
427}
edisonn@google.com77909122012-10-18 15:58:23 +0000428// NYI: issue 240.
429TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000430
piotaixrf05f5a72014-10-03 13:26:55 -0700431static void DrawPictureTestStep(SkCanvas* canvas, const TestData& d,
432 skiatest::Reporter*, CanvasTestStep*) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000433 SkPictureRecorder recorder;
piotaixrf05f5a72014-10-03 13:26:55 -0700434 SkCanvas* testCanvas = recorder.beginRecording(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700435 NULL, 0);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000436 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
piotaixrf05f5a72014-10-03 13:26:55 -0700437 testCanvas->clipRect(d.fRect);
438 testCanvas->drawRect(d.fRect, d.fPaint);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000439 SkAutoTUnref<SkPicture> testPicture(recorder.endRecording());
440
robertphillips9b14f262014-06-04 05:40:44 -0700441 canvas->drawPicture(testPicture);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000442}
443TEST_STEP(DrawPicture, DrawPictureTestStep);
444
piotaixrf05f5a72014-10-03 13:26:55 -0700445static void SaveRestoreTestStep(SkCanvas* canvas, const TestData& d,
446 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000447 int baseSaveCount = canvas->getSaveCount();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000448 int n = canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000449 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
450 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000451 testStep->assertMessage());
452 canvas->save();
453 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000454 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000455 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000456 canvas->restoreToCount(baseSaveCount + 1);
457 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000458 testStep->assertMessage());
459
460 // should this pin to 1, or be a no-op, or crash?
461 canvas->restoreToCount(0);
462 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
463 testStep->assertMessage());
464}
465TEST_STEP(SaveRestore, SaveRestoreTestStep);
466
piotaixrf05f5a72014-10-03 13:26:55 -0700467static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas, const TestData& d,
468 skiatest::Reporter*, CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000469 // This test step challenges the TestDeferredCanvasStateConsistency
470 // test cases because the opaque paint can trigger an optimization
471 // that discards previously recorded commands. The challenge is to maintain
472 // correct clip and matrix stack state.
473 canvas->resetMatrix();
474 canvas->rotate(SkIntToScalar(30));
475 canvas->save();
476 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
477 canvas->save();
478 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
479 SkPaint paint;
480 paint.setColor(0xFFFFFFFF);
481 canvas->drawPaint(paint);
482 canvas->restore();
483 canvas->restore();
484}
485TEST_STEP(NestedSaveRestoreWithSolidPaint, \
486 NestedSaveRestoreWithSolidPaintTestStep);
487
piotaixrf05f5a72014-10-03 13:26:55 -0700488static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas, const TestData& d,
489 skiatest::Reporter*, CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000490 // This test step challenges the TestDeferredCanvasStateConsistency
491 // test case because the canvas flush on a deferred canvas will
492 // reset the recording session. The challenge is to maintain correct
493 // clip and matrix stack state on the playback canvas.
494 canvas->resetMatrix();
495 canvas->rotate(SkIntToScalar(30));
496 canvas->save();
497 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
498 canvas->save();
499 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
piotaixrf05f5a72014-10-03 13:26:55 -0700500 canvas->drawRect(d.fRect,d.fPaint);
reed@google.com3b3e8952012-08-16 20:53:31 +0000501 canvas->flush();
502 canvas->restore();
503 canvas->restore();
504}
piotaixrf05f5a72014-10-03 13:26:55 -0700505TEST_STEP(NestedSaveRestoreWithFlush, NestedSaveRestoreWithFlushTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000506
piotaixrf05f5a72014-10-03 13:26:55 -0700507static void AssertCanvasStatesEqual(skiatest::Reporter* reporter, const TestData& d,
508 const SkCanvas* canvas1, const SkCanvas* canvas2,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000509 CanvasTestStep* testStep) {
510 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
511 canvas2->getDeviceSize(), testStep->assertMessage());
512 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
513 canvas2->getSaveCount(), testStep->assertMessage());
reed@google.com3b3e8952012-08-16 20:53:31 +0000514
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000515 SkRect bounds1, bounds2;
516 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.com3b3e8952012-08-16 20:53:31 +0000517 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000518 testStep->assertMessage());
519 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
reed@google.com3b3e8952012-08-16 20:53:31 +0000520 testStep->assertMessage());
521
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000522 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
523 canvas2->getDrawFilter(), testStep->assertMessage());
524 SkIRect deviceBounds1, deviceBounds2;
525 REPORTER_ASSERT_MESSAGE(reporter,
526 canvas1->getClipDeviceBounds(&deviceBounds1) ==
527 canvas2->getClipDeviceBounds(&deviceBounds2),
528 testStep->assertMessage());
reed868074b2014-06-03 10:53:59 -0700529 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2, testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000530 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
531 canvas2->getTotalMatrix(), testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000532 REPORTER_ASSERT_MESSAGE(reporter, equal_clips(*canvas1, *canvas2), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000533
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000534 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
535 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
536 while (!layerIter1.done() && !layerIter2.done()) {
537 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
538 layerIter2.matrix(), testStep->assertMessage());
539 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
540 layerIter2.clip(), testStep->assertMessage());
541 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
542 layerIter2.paint(), testStep->assertMessage());
543 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
544 layerIter2.x(), testStep->assertMessage());
545 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
546 layerIter2.y(), testStep->assertMessage());
547 layerIter1.next();
548 layerIter2.next();
549 }
550 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
551 testStep->assertMessage());
552 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
553 testStep->assertMessage());
piotaixr76993ed2014-10-27 15:31:34 -0700554
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000555}
556
edisonn@google.com77909122012-10-18 15:58:23 +0000557static void TestPdfDevice(skiatest::Reporter* reporter,
piotaixrf05f5a72014-10-03 13:26:55 -0700558 const TestData& d,
edisonn@google.com77909122012-10-18 15:58:23 +0000559 CanvasTestStep* testStep) {
piotaixrf05f5a72014-10-03 13:26:55 -0700560 SkISize pageSize = SkISize::Make(d.fWidth, d.fHeight);
edisonn@google.com77909122012-10-18 15:58:23 +0000561 SkPDFDevice device(pageSize, pageSize, SkMatrix::I());
562 SkCanvas canvas(&device);
563 testStep->setAssertMessageFormat(kPdfAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700564 testStep->draw(&canvas, d, reporter);
edisonn@google.com77909122012-10-18 15:58:23 +0000565 SkPDFDocument doc;
566 doc.appendPage(&device);
567 SkDynamicMemoryWStream stream;
568 doc.emitPDF(&stream);
569}
570
junov@chromium.org88e29142012-08-07 16:48:22 +0000571// The following class groups static functions that need to access
572// the privates members of SkDeferredCanvas
573class SkDeferredCanvasTester {
574public:
575 static void TestDeferredCanvasStateConsistency(
576 skiatest::Reporter* reporter,
piotaixrf05f5a72014-10-03 13:26:55 -0700577 const TestData& d,
junov@chromium.org88e29142012-08-07 16:48:22 +0000578 CanvasTestStep* testStep,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000579 const SkCanvas& referenceCanvas, bool silent) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000580
reed@google.com28183b42014-02-04 15:34:10 +0000581 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
582 SkAutoTUnref<SkDeferredCanvas> deferredCanvas(SkDeferredCanvas::Create(surface.get()));
583
junov@chromium.org88e29142012-08-07 16:48:22 +0000584 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700585 testStep->draw(deferredCanvas, d, reporter);
junov@chromium.org88e29142012-08-07 16:48:22 +0000586 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700587 AssertCanvasStatesEqual(reporter, d, deferredCanvas, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000588
junov@chromium.orgfb103892012-09-20 19:35:43 +0000589 if (silent) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000590 deferredCanvas->silentFlush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000591 } else {
junov@chromium.org66070a52013-05-28 17:39:08 +0000592 deferredCanvas->flush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000593 }
594
skia.committer@gmail.com4c5ea442012-09-21 02:01:01 +0000595 testStep->setAssertMessageFormat(
junov@chromium.orgfb103892012-09-20 19:35:43 +0000596 silent ? kDeferredPostSilentFlushPlaybackAssertMessageFormat :
junov@chromium.org88e29142012-08-07 16:48:22 +0000597 kDeferredPostFlushPlaybackAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700598 AssertCanvasStatesEqual(reporter, d, deferredCanvas->immediateCanvas(),
599 &referenceCanvas, testStep);
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000600
junov@chromium.org88e29142012-08-07 16:48:22 +0000601 // Verified that deferred canvas state is not affected by flushing
602 // pending draw operations
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000603
junov@chromium.org88e29142012-08-07 16:48:22 +0000604 // The following test code is commented out because it currently fails.
605 // Issue: http://code.google.com/p/skia/issues/detail?id=496
606 /*
607 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
608 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
609 testStep);
610 */
611 }
612};
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000613
caryclark@google.com42639cd2012-06-06 12:03:39 +0000614// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000615static void TestNWayCanvasStateConsistency(
616 skiatest::Reporter* reporter,
piotaixrf05f5a72014-10-03 13:26:55 -0700617 const TestData& d,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000618 CanvasTestStep* testStep,
619 const SkCanvas& referenceCanvas) {
620
621 SkBitmap indirectStore1;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000622 createBitmap(&indirectStore1, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700623 SkCanvas indirectCanvas1(indirectStore1);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000624
625 SkBitmap indirectStore2;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000626 createBitmap(&indirectStore2, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700627 SkCanvas indirectCanvas2(indirectStore2);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000628
djsollen@google.comf0a062b2012-05-01 16:50:25 +0000629 SkISize canvasSize = referenceCanvas.getDeviceSize();
630 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000631 nWayCanvas.addCanvas(&indirectCanvas1);
632 nWayCanvas.addCanvas(&indirectCanvas2);
633
634 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700635 testStep->draw(&nWayCanvas, d, reporter);
scroggo648238c2015-01-29 11:58:51 -0800636 // Verify that the SkNWayCanvas reports consitent state
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000637 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700638 AssertCanvasStatesEqual(reporter, d, &nWayCanvas, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000639 // Verify that the indirect canvases report consitent state
640 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700641 AssertCanvasStatesEqual(reporter, d, &indirectCanvas1, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000642 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700643 AssertCanvasStatesEqual(reporter, d, &indirectCanvas2, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000644}
645
646/*
647 * This sub-test verifies that the test step passes when executed
648 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
649 * that the all canvas derivatives report the same state as an SkCanvas
650 * after having executed the test step.
651 */
piotaixrf05f5a72014-10-03 13:26:55 -0700652static void TestOverrideStateConsistency(skiatest::Reporter* reporter, const TestData& d,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000653 CanvasTestStep* testStep) {
654 SkBitmap referenceStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000655 createBitmap(&referenceStore, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700656 SkCanvas referenceCanvas(referenceStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000657 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700658 testStep->draw(&referenceCanvas, d, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000659
piotaixrf05f5a72014-10-03 13:26:55 -0700660 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, d, testStep, referenceCanvas, false);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000661
piotaixrf05f5a72014-10-03 13:26:55 -0700662 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, d, testStep, referenceCanvas, true);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000663
caryclark@google.com42639cd2012-06-06 12:03:39 +0000664 // The following test code is disabled because SkNWayCanvas does not
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000665 // report correct clipping and device bounds information
666 // Issue: http://code.google.com/p/skia/issues/detail?id=501
caryclark@google.com42639cd2012-06-06 12:03:39 +0000667
668 if (false) { // avoid bit rot, suppress warning
piotaixrf05f5a72014-10-03 13:26:55 -0700669 TestNWayCanvasStateConsistency(reporter, d, testStep, referenceCanvas);
caryclark@google.com42639cd2012-06-06 12:03:39 +0000670 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000671
caryclark@google.com42639cd2012-06-06 12:03:39 +0000672 if (false) { // avoid bit rot, suppress warning
673 test_clipVisitor(reporter, &referenceCanvas);
674 }
reed@google.com7c202932011-12-14 18:48:05 +0000675}
reed@google.com37f3ae02011-11-28 16:06:04 +0000676
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000677static void test_newraster(skiatest::Reporter* reporter) {
678 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
reed3054be12014-12-10 07:24:28 -0800679 const size_t minRowBytes = info.minRowBytes();
680 const size_t size = info.getSafeSize(minRowBytes);
681 SkAutoMalloc storage(size);
682 SkPMColor* baseAddr = static_cast<SkPMColor*>(storage.get());
683 sk_bzero(baseAddr, size);
684
685 SkCanvas* canvas = SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000686 REPORTER_ASSERT(reporter, canvas);
687
688 SkImageInfo info2;
689 size_t rowBytes;
690 const SkPMColor* addr = (const SkPMColor*)canvas->peekPixels(&info2, &rowBytes);
691 REPORTER_ASSERT(reporter, addr);
692 REPORTER_ASSERT(reporter, info == info2);
reed3054be12014-12-10 07:24:28 -0800693 REPORTER_ASSERT(reporter, minRowBytes == rowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000694 for (int y = 0; y < info.height(); ++y) {
695 for (int x = 0; x < info.width(); ++x) {
696 REPORTER_ASSERT(reporter, 0 == addr[x]);
697 }
698 addr = (const SkPMColor*)((const char*)addr + rowBytes);
699 }
700 SkDELETE(canvas);
701
702 // now try a deliberately bad info
reede5ea5002014-09-03 11:54:58 -0700703 info = info.makeWH(-1, info.height());
reed3054be12014-12-10 07:24:28 -0800704 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000705
706 // too big
reede5ea5002014-09-03 11:54:58 -0700707 info = info.makeWH(1 << 30, 1 << 30);
reed3054be12014-12-10 07:24:28 -0800708 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
skia.committer@gmail.com0e530752014-02-28 03:02:05 +0000709
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000710 // not a valid pixel type
reede5ea5002014-09-03 11:54:58 -0700711 info = SkImageInfo::Make(10, 10, kUnknown_SkColorType, info.alphaType());
reed3054be12014-12-10 07:24:28 -0800712 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000713
714 // We should succeed with a zero-sized valid info
715 info = SkImageInfo::MakeN32Premul(0, 0);
reed3054be12014-12-10 07:24:28 -0800716 canvas = SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000717 REPORTER_ASSERT(reporter, canvas);
718 SkDELETE(canvas);
719}
720
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000721DEF_TEST(Canvas, reporter) {
piotaixrf05f5a72014-10-03 13:26:55 -0700722 TestData d;
reed@google.com37f3ae02011-11-28 16:06:04 +0000723
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000724 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
piotaixrf05f5a72014-10-03 13:26:55 -0700725 TestOverrideStateConsistency(reporter, d, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000726 if (testStepArray()[testStep]->enablePdfTesting()) {
piotaixrf05f5a72014-10-03 13:26:55 -0700727 TestPdfDevice(reporter, d, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000728 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000729 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000730
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000731 test_newraster(reporter);
reed@google.com37f3ae02011-11-28 16:06:04 +0000732}
reedf0090cb2014-11-26 08:55:51 -0800733
734DEF_TEST(Canvas_SaveState, reporter) {
735 SkCanvas canvas(10, 10);
736 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
737
738 int n = canvas.save();
739 REPORTER_ASSERT(reporter, 1 == n);
740 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
741
742 n = canvas.saveLayer(NULL, NULL);
743 REPORTER_ASSERT(reporter, 2 == n);
744 REPORTER_ASSERT(reporter, 3 == canvas.getSaveCount());
745
746 canvas.restore();
747 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
748 canvas.restore();
749 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
750}