1 | // Copyright 2013 The Flutter 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 "canvas_spy.h" |
6 | #include "gtest/gtest.h" |
7 | #include "third_party/skia/include/core/SkPictureRecorder.h" |
8 | #include "third_party/skia/include/core/SkSurface.h" |
9 | |
10 | namespace flutter { |
11 | namespace testing { |
12 | |
13 | TEST(CanvasSpyTest, DrawingIsTracked) { |
14 | SkPictureRecorder picture_recorder; |
15 | SkCanvas* canvas = picture_recorder.beginRecording(100, 100); |
16 | CanvasSpy canvas_spy = CanvasSpy(canvas); |
17 | SkCanvas* spy = canvas_spy.GetSpyingCanvas(); |
18 | |
19 | ASSERT_FALSE(canvas_spy.DidDrawIntoCanvas()); |
20 | |
21 | spy->translate(128, 128); |
22 | spy->clear(SK_ColorTRANSPARENT); |
23 | ASSERT_FALSE(canvas_spy.DidDrawIntoCanvas()); |
24 | |
25 | SkPaint paint; |
26 | spy->drawCircle(0, 0, 60, paint); |
27 | ASSERT_TRUE(canvas_spy.DidDrawIntoCanvas()); |
28 | } |
29 | |
30 | TEST(CanvasSpyTest, SpiedCanvasIsDrawing) { |
31 | auto actual_surface = |
32 | SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(100, 100)); |
33 | SkCanvas* actual_canvas = actual_surface->getCanvas(); |
34 | |
35 | auto expected_surface = |
36 | SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(100, 100)); |
37 | SkCanvas* expected_canvas = expected_surface->getCanvas(); |
38 | |
39 | CanvasSpy canvas_spy = CanvasSpy(actual_canvas); |
40 | SkCanvas* spy = canvas_spy.GetSpyingCanvas(); |
41 | |
42 | SkNWayCanvas multi_canvas = SkNWayCanvas(100, 100); |
43 | multi_canvas.addCanvas(spy); |
44 | multi_canvas.addCanvas(expected_canvas); |
45 | |
46 | multi_canvas.clipRect(SkRect::MakeWH(100, 100)); |
47 | multi_canvas.clear(SK_ColorRED); |
48 | multi_canvas.scale(.5, .5); |
49 | multi_canvas.clipRect(SkRect::MakeWH(100, 100)); |
50 | multi_canvas.clear(SK_ColorBLUE); |
51 | |
52 | ASSERT_TRUE(canvas_spy.DidDrawIntoCanvas()); |
53 | |
54 | SkPixmap actual; |
55 | SkPixmap expected; |
56 | |
57 | ASSERT_TRUE(actual_surface->peekPixels(&actual)); |
58 | ASSERT_TRUE(expected_surface->peekPixels(&expected)); |
59 | |
60 | const auto size = actual.rowBytes() * actual.height(); |
61 | ASSERT_NE(size, 0u); |
62 | |
63 | ASSERT_EQ(::memcmp(actual.addr(), expected.addr(), size), 0); |
64 | } |
65 | } // namespace testing |
66 | } // namespace flutter |
67 | |