aboutsummaryrefslogtreecommitdiffhomepage
path: root/site/dev/testing/tests.md
blob: c24a1731e07c7aba8adec96987e155f0256d987d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
Writing Skia Tests
==================

+   [Unit Tests](#test)
+   [Rendering Tests](#gm)
+   [Benchmark Tests](#bench)

We assume you have already synced Skia's dependecies and set up Skia's build system.

<!--?prettify lang=sh?-->

    python tools/git-sync-deps
    bin/gn gen out/Debug
    bin/gn gen out/Release --args='is_debug=false'

<span id="test"></span>

Writing a Unit Test
-------------------

1.  Add a file `tests/NewUnitTest.cpp`:

    <!--?prettify lang=cc?-->

        /*
         * Copyright ........
         *
         * Use of this source code is governed by a BSD-style license
         * that can be found in the LICENSE file.
         */
        #include "Test.h"
        DEF_TEST(NewUnitTest, reporter) {
            if (1 + 1 != 2) {
                ERRORF(reporter, "%d + %d != %d", 1, 1, 2);
            }
            bool lifeIsGood = true;
            REPORTER_ASSERT(reporter, lifeIsGood);
        }

2.  Add `NewUnitTest.cpp` to `gn/tests.gni`.

3.  Recompile and run test:

    <!--?prettify lang=sh?-->

        ninja -C out/Debug dm
        out/Debug/dm --match NewUnitTest

<span id="gm"></span>

Writing a Rendering Test
------------------------

1.  Add a file `gm/newgmtest.cpp`:

    <!--?prettify lang=cc?-->

        /*
         * Copyright ........
         *
         * Use of this source code is governed by a BSD-style license
         * that can be found in the LICENSE file.
         */
        #include "gm.h"
        DEF_SIMPLE_GM(newgmtest, canvas, 128, 128) {
            canvas->clear(SK_ColorWHITE);
            SkPaint p;
            p.setStrokeWidth(2);
            canvas->drawLine(16, 16, 112, 112, p);
        }

2.  Add `newgmtest.cpp` to `gn/gm.gni`.

3.  Recompile and run test:

    <!--?prettify lang=sh?-->

        ninja -C out/Debug dm
        out/Debug/dm --match newgmtest

4.  Run the GM inside SampleApp:

    <!--?prettify lang=sh?-->

        ninja -C out/Debug SampleApp
        out/Debug/SampleApp --slide GM:newgmtest

<span id="bench"></span>

Writing a Benchmark Test
------------------------

1.  Add a file `bench/FooBench.cpp`:

    <!--?prettify lang=cc?-->

        /*
         * Copyright ........
         *
         * Use of this source code is governed by a BSD-style license
         * that can be found in the LICENSE file.
         */
        #include "Benchmark.h"
        #include "SkCanvas.h"
        namespace {
        class FooBench : public Benchmark {
        public:
            FooBench() {}
            virtual ~FooBench() {}
        protected:
            const char* onGetName() override { return "Foo"; }
            SkIPoint onGetSize() override { return SkIPoint{100, 100}; }
            void onDraw(int loops, SkCanvas* canvas) override {
                while (loops-- > 0) {
                    canvas->drawLine(0.0f, 0.0f, 100.0f, 100.0f, SkPaint());
                }
            }
        };
        }  // namespace
        DEF_BENCH(return new FooBench;)

2.  Add `FooBench.cpp` to `gn/bench.gni`.

3.  Recompile and run nanobench:

    <!--?prettify lang=sh?-->

        ninja -C out/Release nanobench
        out/Release/nanobench --match Foo