1Writing Skia Tests 2================== 3 4+ [Unit Tests](#test) 5+ [Rendering Tests](#gm) 6+ [Benchmark Tests](#bench) 7 8<span id="test"></span> 9 10Writing a Unit Test 11------------------- 12 131. Add a file `tests/NewUnitTest.cpp`: 14 15 <!--?prettify lang=cc?--> 16 17 /* 18 * Copyright ........ 19 * 20 * Use of this source code is governed by a BSD-style license 21 * that can be found in the LICENSE file. 22 */ 23 #include "Test.h" 24 DEF_TEST(NewUnitTest, reporter) { 25 if (1 + 1 != 2) { 26 ERRORF(reporter, "%d + %d != %d", 1, 1, 2); 27 } 28 bool lifeIsGood = true; 29 REPORTER_ASSERT(reporter, lifeIsGood); 30 } 31 322. Recompile and run test: 33 34 python bin/sync-and-gyp 35 ninja -C out/Debug dm 36 out/Debug/dm --match NewUnitTest 37 38<span id="gm"></span> 39 40Writing a Rendering Test 41------------------------ 42 431. Add a file `gm/newgmtest.cpp`: 44 45 <!--?prettify lang=cc?--> 46 47 /* 48 * Copyright ........ 49 * 50 * Use of this source code is governed by a BSD-style license 51 * that can be found in the LICENSE file. 52 */ 53 #include "gm.h" 54 DEF_SIMPLE_GM(newgmtest, canvas, 128, 128) { 55 canvas->clear(SK_ColorWHITE); 56 SkPaint p; 57 p.setStrokeWidth(2); 58 canvas->drawLine(16, 16, 112, 112, p); 59 } 60 612. Recompile and run test: 62 63 python bin/sync-and-gyp 64 ninja -C out/Debug dm 65 out/Debug/dm --match newgmtest 66 673. Run the GM inside SampleApp: 68 69 python bin/sync-and-gyp 70 ninja -C out/Debug SampleApp 71 out/Debug/SampleApp --slide GM:newgmtest 72 73 On MacOS, try this: 74 75 out/Debug/SampleApp.app/Contents/MacOS/SampleApp --slide GM:newgmtest 76 77<span id="bench"></span> 78 79Writing a Benchmark Test 80------------------------ 81 821. Add a file `bench/FooBench.cpp`: 83 84 <!--?prettify lang=cc?--> 85 86 /* 87 * Copyright ........ 88 * 89 * Use of this source code is governed by a BSD-style license 90 * that can be found in the LICENSE file. 91 */ 92 #include "Benchmark.h" 93 #include "SkCanvas.h" 94 namespace { 95 class FooBench : public Benchmark { 96 public: 97 FooBench() {} 98 virtual ~FooBench() {} 99 protected: 100 const char* onGetName() override { return "Foo"; } 101 SkIPoint onGetSize() override { return SkIPoint{100, 100}; } 102 void onDraw(int loops, SkCanvas* canvas) override { 103 while (loops-- > 0) { 104 canvas->drawLine(0.0f, 0.0f, 100.0f, 100.0f, SkPaint()); 105 } 106 } 107 }; 108 } // namespace 109 DEF_BENCH(return new FooBench;) 110 111 1122. Recompile and run nanobench: 113 114 python bin/sync-and-gyp 115 ninja -C out/Release nanobench 116 out/Release/nanobench --match Foo 117