Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / skia / ext / analysis_canvas_unittest.cc
blob5bb18eb84c9d0f7a257deffb714c671760539d66
1 // Copyright 2013 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.
5 #include "base/compiler_specific.h"
6 #include "skia/ext/analysis_canvas.h"
7 #include "skia/ext/refptr.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "third_party/skia/include/core/SkPicture.h"
10 #include "third_party/skia/include/core/SkPictureRecorder.h"
11 #include "third_party/skia/include/core/SkShader.h"
12 #include "third_party/skia/include/effects/SkOffsetImageFilter.h"
14 namespace {
16 void SolidColorFill(skia::AnalysisCanvas& canvas) {
17 canvas.clear(SkColorSetARGB(255, 255, 255, 255));
20 void TransparentFill(skia::AnalysisCanvas& canvas) {
21 canvas.clear(SkColorSetARGB(0, 0, 0, 0));
24 } // namespace
25 namespace skia {
27 TEST(AnalysisCanvasTest, EmptyCanvas) {
28 skia::AnalysisCanvas canvas(255, 255);
30 SkColor color;
31 EXPECT_TRUE(canvas.GetColorIfSolid(&color));
32 EXPECT_EQ(color, SkColorSetARGB(0, 0, 0, 0));
35 TEST(AnalysisCanvasTest, ClearCanvas) {
36 skia::AnalysisCanvas canvas(255, 255);
38 // Transparent color
39 SkColor color = SkColorSetARGB(0, 12, 34, 56);
40 canvas.clear(color);
42 SkColor outputColor;
43 EXPECT_TRUE(canvas.GetColorIfSolid(&outputColor));
44 EXPECT_EQ(static_cast<SkColor>(SK_ColorTRANSPARENT), outputColor);
46 // Solid color
47 color = SkColorSetARGB(255, 65, 43, 21);
48 canvas.clear(color);
50 EXPECT_TRUE(canvas.GetColorIfSolid(&outputColor));
51 EXPECT_NE(static_cast<SkColor>(SK_ColorTRANSPARENT), outputColor);
52 EXPECT_EQ(outputColor, color);
54 // Translucent color
55 color = SkColorSetARGB(128, 11, 22, 33);
56 canvas.clear(color);
58 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
60 // Test helper methods
61 SolidColorFill(canvas);
62 EXPECT_TRUE(canvas.GetColorIfSolid(&outputColor));
63 EXPECT_NE(static_cast<SkColor>(SK_ColorTRANSPARENT), outputColor);
65 TransparentFill(canvas);
66 EXPECT_TRUE(canvas.GetColorIfSolid(&outputColor));
67 EXPECT_EQ(static_cast<SkColor>(SK_ColorTRANSPARENT), outputColor);
70 TEST(AnalysisCanvasTest, ComplexActions) {
71 skia::AnalysisCanvas canvas(255, 255);
73 // Draw paint test.
74 SkColor color = SkColorSetARGB(255, 11, 22, 33);
75 SkPaint paint;
76 paint.setColor(color);
78 canvas.drawPaint(paint);
80 SkColor outputColor;
81 EXPECT_TRUE(canvas.GetColorIfSolid(&outputColor));
83 // Draw points test.
84 SkPoint points[4] = {
85 SkPoint::Make(0, 0),
86 SkPoint::Make(255, 0),
87 SkPoint::Make(255, 255),
88 SkPoint::Make(0, 255)
91 SolidColorFill(canvas);
92 canvas.drawPoints(SkCanvas::kLines_PointMode, 4, points, paint);
94 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
96 // Draw oval test.
97 SolidColorFill(canvas);
98 canvas.drawOval(SkRect::MakeWH(255, 255), paint);
100 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
102 // Draw bitmap test.
103 SolidColorFill(canvas);
104 SkBitmap secondBitmap;
105 secondBitmap.allocN32Pixels(255, 255);
106 canvas.drawBitmap(secondBitmap, 0, 0);
108 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
111 TEST(AnalysisCanvasTest, SimpleDrawRect) {
112 skia::AnalysisCanvas canvas(255, 255);
114 SkColor color = SkColorSetARGB(255, 11, 22, 33);
115 SkPaint paint;
116 paint.setColor(color);
117 canvas.clipRect(SkRect::MakeWH(255, 255));
118 canvas.drawRect(SkRect::MakeWH(255, 255), paint);
120 SkColor outputColor;
121 EXPECT_TRUE(canvas.GetColorIfSolid(&outputColor));
122 EXPECT_NE(static_cast<SkColor>(SK_ColorTRANSPARENT), outputColor);
123 EXPECT_EQ(color, outputColor);
125 color = SkColorSetARGB(255, 22, 33, 44);
126 paint.setColor(color);
127 canvas.translate(-128, -128);
128 canvas.drawRect(SkRect::MakeWH(382, 382), paint);
130 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
132 color = SkColorSetARGB(255, 33, 44, 55);
133 paint.setColor(color);
134 canvas.drawRect(SkRect::MakeWH(383, 383), paint);
136 EXPECT_TRUE(canvas.GetColorIfSolid(&outputColor));
137 EXPECT_NE(static_cast<SkColor>(SK_ColorTRANSPARENT), outputColor);
138 EXPECT_EQ(color, outputColor);
140 color = SkColorSetARGB(0, 0, 0, 0);
141 paint.setColor(color);
142 canvas.drawRect(SkRect::MakeWH(383, 383), paint);
144 // This test relies on canvas treating a paint with 0-color as a no-op
145 // thus not changing its "is_solid" status.
146 EXPECT_TRUE(canvas.GetColorIfSolid(&outputColor));
147 EXPECT_NE(static_cast<SkColor>(SK_ColorTRANSPARENT), outputColor);
148 EXPECT_EQ(outputColor, SkColorSetARGB(255, 33, 44, 55));
150 color = SkColorSetARGB(128, 128, 128, 128);
151 paint.setColor(color);
152 canvas.drawRect(SkRect::MakeWH(383, 383), paint);
154 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
156 paint.setXfermodeMode(SkXfermode::kClear_Mode);
157 canvas.drawRect(SkRect::MakeWH(382, 382), paint);
159 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
161 canvas.drawRect(SkRect::MakeWH(383, 383), paint);
163 EXPECT_TRUE(canvas.GetColorIfSolid(&outputColor));
164 EXPECT_EQ(static_cast<SkColor>(SK_ColorTRANSPARENT), outputColor);
166 canvas.translate(128, 128);
167 color = SkColorSetARGB(255, 11, 22, 33);
168 paint.setColor(color);
169 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
170 canvas.drawRect(SkRect::MakeWH(255, 255), paint);
172 EXPECT_TRUE(canvas.GetColorIfSolid(&outputColor));
173 EXPECT_NE(static_cast<SkColor>(SK_ColorTRANSPARENT), outputColor);
174 EXPECT_EQ(color, outputColor);
176 canvas.rotate(50);
177 canvas.drawRect(SkRect::MakeWH(255, 255), paint);
179 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
182 TEST(AnalysisCanvasTest, FilterPaint) {
183 skia::AnalysisCanvas canvas(255, 255);
184 SkPaint paint;
186 skia::RefPtr<SkImageFilter> filter = skia::AdoptRef(SkOffsetImageFilter::Create(10, 10));
187 paint.setImageFilter(filter.get());
188 canvas.drawRect(SkRect::MakeWH(255, 255), paint);
190 SkColor outputColor;
191 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
194 TEST(AnalysisCanvasTest, ClipPath) {
195 skia::AnalysisCanvas canvas(255, 255);
197 // Skia will look for paths that are actually rects and treat
198 // them as such. We add a divot to the following path to prevent
199 // this optimization and truly test clipPath's behavior.
200 SkPath path;
201 path.moveTo(0, 0);
202 path.lineTo(128, 50);
203 path.lineTo(255, 0);
204 path.lineTo(255, 255);
205 path.lineTo(0, 255);
207 SkColor outputColor;
208 SolidColorFill(canvas);
209 canvas.clipPath(path);
210 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
212 canvas.save();
213 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
215 canvas.clipPath(path);
216 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
218 canvas.restore();
219 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
221 SolidColorFill(canvas);
222 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
225 TEST(AnalysisCanvasTest, SaveLayerRestore) {
226 skia::AnalysisCanvas canvas(255, 255);
228 SkColor outputColor;
229 SolidColorFill(canvas);
230 EXPECT_TRUE(canvas.GetColorIfSolid(&outputColor));
232 SkRect bounds = SkRect::MakeWH(255, 255);
233 SkPaint paint;
234 paint.setColor(SkColorSetARGB(255, 255, 255, 255));
235 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
237 // This should force non-transparency
238 canvas.saveLayer(&bounds, &paint);
239 EXPECT_TRUE(canvas.GetColorIfSolid(&outputColor));
240 EXPECT_NE(static_cast<SkColor>(SK_ColorTRANSPARENT), outputColor);
242 TransparentFill(canvas);
243 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
245 SolidColorFill(canvas);
246 EXPECT_TRUE(canvas.GetColorIfSolid(&outputColor));
247 EXPECT_NE(static_cast<SkColor>(SK_ColorTRANSPARENT), outputColor);
249 paint.setXfermodeMode(SkXfermode::kDst_Mode);
251 // This should force non-solid color
252 canvas.saveLayer(&bounds, &paint);
253 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
255 TransparentFill(canvas);
256 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
258 SolidColorFill(canvas);
259 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
261 canvas.restore();
262 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
264 TransparentFill(canvas);
265 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
267 SolidColorFill(canvas);
268 EXPECT_TRUE(canvas.GetColorIfSolid(&outputColor));
269 EXPECT_NE(static_cast<SkColor>(SK_ColorTRANSPARENT), outputColor);
271 canvas.restore();
272 EXPECT_TRUE(canvas.GetColorIfSolid(&outputColor));
273 EXPECT_NE(static_cast<SkColor>(SK_ColorTRANSPARENT), outputColor);
275 TransparentFill(canvas);
276 EXPECT_TRUE(canvas.GetColorIfSolid(&outputColor));
277 EXPECT_EQ(static_cast<SkColor>(SK_ColorTRANSPARENT), outputColor);
279 SolidColorFill(canvas);
280 EXPECT_TRUE(canvas.GetColorIfSolid(&outputColor));
281 EXPECT_NE(static_cast<SkColor>(SK_ColorTRANSPARENT), outputColor);
284 TEST(AnalysisCanvasTest, EarlyOutNotSolid) {
285 SkRTreeFactory factory;
286 SkPictureRecorder recorder;
288 // Create a picture with 3 commands, last of which is non-solid.
289 skia::RefPtr<SkCanvas> record_canvas =
290 skia::SharePtr(recorder.beginRecording(256, 256, &factory));
292 std::string text = "text";
293 SkPoint point = SkPoint::Make(SkIntToScalar(25), SkIntToScalar(25));
295 SkPaint paint;
296 paint.setColor(SkColorSetARGB(255, 255, 255, 255));
297 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
299 record_canvas->drawRect(SkRect::MakeWH(256, 256), paint);
300 record_canvas->drawRect(SkRect::MakeWH(256, 256), paint);
301 record_canvas->drawText(
302 text.c_str(), text.length(), point.fX, point.fY, paint);
304 skia::RefPtr<SkPicture> picture = skia::AdoptRef(recorder.endRecording());
306 // Draw the picture into the analysis canvas, using the canvas as a callback
307 // as well.
308 skia::AnalysisCanvas canvas(256, 256);
309 picture->playback(&canvas, &canvas);
311 // Ensure that canvas is not solid.
312 SkColor output_color;
313 EXPECT_FALSE(canvas.GetColorIfSolid(&output_color));
315 // Verify that we aborted drawing.
316 EXPECT_TRUE(canvas.abortDrawing());
320 TEST(AnalysisCanvasTest, ClipComplexRegion) {
321 skia::AnalysisCanvas canvas(255, 255);
323 SkPath path;
324 path.moveTo(0, 0);
325 path.lineTo(128, 50);
326 path.lineTo(255, 0);
327 path.lineTo(255, 255);
328 path.lineTo(0, 255);
329 SkIRect pathBounds = path.getBounds().round();
330 SkRegion region;
331 region.setPath(path, SkRegion(pathBounds));
333 SkColor outputColor;
334 SolidColorFill(canvas);
335 canvas.clipRegion(region);
336 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
338 canvas.save();
339 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
341 canvas.clipRegion(region);
342 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
344 canvas.restore();
345 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
347 SolidColorFill(canvas);
348 EXPECT_FALSE(canvas.GetColorIfSolid(&outputColor));
351 } // namespace skia