Flutter Engine
canvas_spy_unittests.cc
Go to the documentation of this file.
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 
7 #include "gtest/gtest.h"
8 #include "third_party/skia/include/core/SkPictureRecorder.h"
9 #include "third_party/skia/include/core/SkSurface.h"
10 
11 namespace flutter {
12 namespace testing {
13 
14 TEST(CanvasSpyTest, DrawingIsTracked) {
15  SkPictureRecorder picture_recorder;
16  SkCanvas* canvas = picture_recorder.beginRecording(100, 100);
17  CanvasSpy canvas_spy = CanvasSpy(canvas);
18  SkCanvas* spy = canvas_spy.GetSpyingCanvas();
19 
20  ASSERT_FALSE(canvas_spy.DidDrawIntoCanvas());
21 
22  spy->translate(128, 128);
23  spy->clear(SK_ColorTRANSPARENT);
24  ASSERT_FALSE(canvas_spy.DidDrawIntoCanvas());
25 
26  SkPaint paint;
27  spy->drawCircle(0, 0, 60, paint);
28  ASSERT_TRUE(canvas_spy.DidDrawIntoCanvas());
29 }
30 
31 TEST(CanvasSpyTest, SpiedCanvasIsDrawing) {
32  auto actual_surface =
33  SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(100, 100));
34  SkCanvas* actual_canvas = actual_surface->getCanvas();
35 
36  auto expected_surface =
37  SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(100, 100));
38  SkCanvas* expected_canvas = expected_surface->getCanvas();
39 
40  CanvasSpy canvas_spy = CanvasSpy(actual_canvas);
41  SkCanvas* spy = canvas_spy.GetSpyingCanvas();
42 
43  SkNWayCanvas multi_canvas = SkNWayCanvas(100, 100);
44  multi_canvas.addCanvas(spy);
45  multi_canvas.addCanvas(expected_canvas);
46 
47  multi_canvas.clipRect(SkRect::MakeWH(100, 100));
48  multi_canvas.clear(SK_ColorRED);
49  multi_canvas.scale(.5, .5);
50  multi_canvas.clipRect(SkRect::MakeWH(100, 100));
51  multi_canvas.clear(SK_ColorBLUE);
52 
53  ASSERT_TRUE(canvas_spy.DidDrawIntoCanvas());
54 
55  SkPixmap actual;
56  SkPixmap expected;
57 
58  ASSERT_TRUE(actual_surface->peekPixels(&actual));
59  ASSERT_TRUE(expected_surface->peekPixels(&expected));
60 
61  const auto size = actual.rowBytes() * actual.height();
62  ASSERT_NE(size, 0u);
63 
64  ASSERT_EQ(::memcmp(actual.addr(), expected.addr(), size), 0);
65 }
66 } // namespace testing
67 } // namespace flutter
SkCanvas * GetSpyingCanvas()
The returned canvas delegate all operations to the target canvas while spying on them.
Definition: canvas_spy.cc:19
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
bool DidDrawIntoCanvas()
Returns true if any non transparent content has been drawn into the spying canvas. Note that this class does tries to detect empty canvases but in some cases may return true even for empty canvases (e.g when a transparent image is drawn into the canvas).
Definition: canvas_spy.cc:33
TEST(EmbeddedViewParams, GetBoundingRectAfterMutationsWithNoMutations)