1Writing Skia Tests 2================== 3 4+ [Unit Tests](#test) 5+ [Rendering Tests](#gm) 6+ [Benchmark Tests](#bench) 7 8We assume you have already synced Skia's dependecies and set up Skia's build system. 9 10<!--?prettify lang=sh?--> 11 12 python tools/git-sync-deps 13 bin/gn gen out/Debug 14 bin/gn gen out/Release --args='is_debug=false' 15 16<span id="test"></span> 17 18Writing a Unit Test 19------------------- 20 211. Add a file `tests/NewUnitTest.cpp`: 22 23 <!--?prettify lang=cc?--> 24 25 /* 26 * Copyright ........ 27 * 28 * Use of this source code is governed by a BSD-style license 29 * that can be found in the LICENSE file. 30 */ 31 #include "Test.h" 32 DEF_TEST(NewUnitTest, reporter) { 33 if (1 + 1 != 2) { 34 ERRORF(reporter, "%d + %d != %d", 1, 1, 2); 35 } 36 bool lifeIsGood = true; 37 REPORTER_ASSERT(reporter, lifeIsGood); 38 } 39 402. Add `NewUnitTest.cpp` to `gn/tests.gni`. 41 423. Recompile and run test: 43 44 <!--?prettify lang=sh?--> 45 46 ninja -C out/Debug dm 47 out/Debug/dm --match NewUnitTest 48 49<span id="gm"></span> 50 51Writing a Rendering Test 52------------------------ 53 541. Add a file `gm/newgmtest.cpp`: 55 56 <!--?prettify lang=cc?--> 57 58 /* 59 * Copyright ........ 60 * 61 * Use of this source code is governed by a BSD-style license 62 * that can be found in the LICENSE file. 63 */ 64 #include "gm.h" 65 DEF_SIMPLE_GM(newgmtest, canvas, 128, 128) { 66 canvas->clear(SK_ColorWHITE); 67 SkPaint p; 68 p.setStrokeWidth(2); 69 canvas->drawLine(16, 16, 112, 112, p); 70 } 71 722. Add `newgmtest.cpp` to `gn/gm.gni`. 73 743. Recompile and run test: 75 76 <!--?prettify lang=sh?--> 77 78 ninja -C out/Debug dm 79 out/Debug/dm --match newgmtest 80 814. Run the GM inside SampleApp: 82 83 <!--?prettify lang=sh?--> 84 85 ninja -C out/Debug SampleApp 86 out/Debug/SampleApp --slide GM:newgmtest 87 88<span id="bench"></span> 89 90Writing a Benchmark Test 91------------------------ 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