• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1---
2title: 'Writing Skia Tests'
3linkTitle: 'Writing Skia Tests'
4---
5
6- [Unit Tests](#test)
7- [Rendering Tests](#gm)
8- [Benchmark Tests](#bench)
9
10We assume you have already synced Skia's dependencies and set up Skia's build
11system.
12
13<!--?prettify lang=sh?-->
14
15    python2 tools/git-sync-deps
16    bin/gn gen out/Debug
17    bin/gn gen out/Release --args='is_debug=false'
18
19<span id="test"></span>
20
21## Writing a Unit Test
22
231.  Add a file `tests/NewUnitTest.cpp`:
24
25    <!--?prettify lang=cc?-->
26
27        /*
28         * Copyright ........
29         *
30         * Use of this source code is governed by a BSD-style license
31         * that can be found in the LICENSE file.
32         */
33        #include "Test.h"
34        DEF_TEST(NewUnitTest, reporter) {
35            if (1 + 1 != 2) {
36                ERRORF(reporter, "%d + %d != %d", 1, 1, 2);
37            }
38            bool lifeIsGood = true;
39            REPORTER_ASSERT(reporter, lifeIsGood);
40        }
41
422.  Add `NewUnitTest.cpp` to `gn/tests.gni`.
43
443.  Recompile and run test:
45
46    <!--?prettify lang=sh?-->
47
48        ninja -C out/Debug dm
49        out/Debug/dm --match NewUnitTest
50
51<span id="gm"></span>
52
53## Writing a Rendering Test
54
551.  Add a file `gm/newgmtest.cpp`:
56
57    <!--?prettify lang=cc?-->
58
59        /*
60         * Copyright ........
61         *
62         * Use of this source code is governed by a BSD-style license
63         * that can be found in the LICENSE file.
64         */
65        #include "gm.h"
66        DEF_SIMPLE_GM(newgmtest, canvas, 128, 128) {
67            canvas->clear(SK_ColorWHITE);
68            SkPaint p;
69            p.setStrokeWidth(2);
70            canvas->drawLine(16, 16, 112, 112, p);
71        }
72
732.  Add `newgmtest.cpp` to `gn/gm.gni`.
74
753.  Recompile and run test:
76
77    <!--?prettify lang=sh?-->
78
79        ninja -C out/Debug dm
80        out/Debug/dm --match newgmtest
81
824.  Run the GM inside Viewer:
83
84    <!--?prettify lang=sh?-->
85
86        ninja -C out/Debug viewer
87        out/Debug/viewer --slide GM_newgmtest
88
89<span id="bench"></span>
90
91## Writing a Benchmark Test
92
931.  Add a file `bench/FooBench.cpp`:
94
95    <!--?prettify lang=cc?-->
96
97        /*
98         * Copyright ........
99         *
100         * Use of this source code is governed by a BSD-style license
101         * that can be found in the LICENSE file.
102         */
103        #include "Benchmark.h"
104        #include "SkCanvas.h"
105        namespace {
106        class FooBench : public Benchmark {
107        public:
108            FooBench() {}
109            virtual ~FooBench() {}
110        protected:
111            const char* onGetName() override { return "Foo"; }
112            SkIPoint onGetSize() override { return SkIPoint{100, 100}; }
113            void onDraw(int loops, SkCanvas* canvas) override {
114                while (loops-- > 0) {
115                    canvas->drawLine(0.0f, 0.0f, 100.0f, 100.0f, SkPaint());
116                }
117            }
118        };
119        }  // namespace
120        DEF_BENCH(return new FooBench;)
121
1222.  Add `FooBench.cpp` to `gn/bench.gni`.
123
1243.  Recompile and run nanobench:
125
126    <!--?prettify lang=sh?-->
127
128        ninja -C out/Release nanobench
129        out/Release/nanobench --match Foo
130