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