1 2 /* 3 * Copyright 2011 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 #ifndef skiatest_Test_DEFINED 9 #define skiatest_Test_DEFINED 10 11 #include "SkRefCnt.h" 12 #include "SkString.h" 13 #include "SkTRegistry.h" 14 #include "SkThread.h" 15 #include "SkTypes.h" 16 17 class GrContextFactory; 18 19 namespace skiatest { 20 21 class Test; 22 23 class Reporter : public SkRefCnt { 24 public: 25 SK_DECLARE_INST_COUNT(Reporter) 26 Reporter(); 27 28 int countTests() const { return fTestCount; } 29 30 void startTest(Test*); 31 void reportFailed(const SkString& desc); 32 void endTest(Test*); 33 34 virtual bool allowExtendedTest() const { return false; } 35 virtual bool verbose() const { return false; } 36 virtual void bumpTestCount() { sk_atomic_inc(&fTestCount); } 37 38 protected: 39 virtual void onStart(Test*) {} 40 virtual void onReportFailed(const SkString& desc) {} 41 virtual void onEnd(Test*) {} 42 43 private: 44 int32_t fTestCount; 45 46 typedef SkRefCnt INHERITED; 47 }; 48 49 class Test { 50 public: 51 Test(); 52 virtual ~Test(); 53 54 Reporter* getReporter() const { return fReporter; } 55 void setReporter(Reporter*); 56 57 const char* getName(); 58 void run(); 59 bool passed() const { return fPassed; } 60 SkMSec elapsedMs() const { return fElapsed; } 61 62 static SkString GetTmpDir(); 63 64 virtual bool isGPUTest() const { return false; } 65 virtual void setGrContextFactory(GrContextFactory* factory) {} 66 67 protected: 68 virtual void onGetName(SkString*) = 0; 69 virtual void onRun(Reporter*) = 0; 70 71 private: 72 Reporter* fReporter; 73 SkString fName; 74 bool fPassed; 75 SkMSec fElapsed; 76 }; 77 78 class GpuTest : public Test{ 79 public: 80 GpuTest() : Test(), fGrContextFactory(NULL) {} 81 82 virtual bool isGPUTest() const { return true; } 83 virtual void setGrContextFactory(GrContextFactory* factory) { 84 fGrContextFactory = factory; 85 } 86 87 protected: 88 GrContextFactory* fGrContextFactory; // Unowned. 89 }; 90 91 typedef SkTRegistry<Test*(*)(void*)> TestRegistry; 92 } // namespace skiatest 93 94 /* 95 Use the following macros to make use of the skiatest classes, e.g. 96 97 #include "Test.h" 98 99 DEF_TEST(TestName, reporter) { 100 ... 101 REPORTER_ASSERT(reporter, x == 15); 102 ... 103 REPORTER_ASSERT_MESSAGE(reporter, x == 15, "x should be 15"); 104 ... 105 if (x != 15) { 106 ERRORF(reporter, "x should be 15, but is %d", x); 107 return; 108 } 109 ... 110 } 111 */ 112 113 #define REPORTER_ASSERT(r, cond) \ 114 do { \ 115 if (!(cond)) { \ 116 SkString desc; \ 117 desc.printf("%s:%d\t%s", __FILE__, __LINE__, #cond); \ 118 r->reportFailed(desc); \ 119 } \ 120 } while(0) 121 122 #define REPORTER_ASSERT_MESSAGE(r, cond, message) \ 123 do { \ 124 if (!(cond)) { \ 125 SkString desc; \ 126 desc.printf("%s:%d\t%s: %s", __FILE__, __LINE__, \ 127 message, #cond); \ 128 r->reportFailed(desc); \ 129 } \ 130 } while(0) 131 132 #define ERRORF(reporter, ...) \ 133 do { \ 134 SkString desc; \ 135 desc.printf("%s:%d\t", __FILE__, __LINE__); \ 136 desc.appendf(__VA_ARGS__) ; \ 137 (reporter)->reportFailed(desc); \ 138 } while(0) 139 140 #define DEF_TEST(name, reporter) \ 141 static void test_##name(skiatest::Reporter*); \ 142 namespace skiatest { \ 143 class name##Class : public Test { \ 144 public: \ 145 static Test* Factory(void*) { return SkNEW(name##Class); } \ 146 protected: \ 147 virtual void onGetName(SkString* name) SK_OVERRIDE { \ 148 name->set(#name); \ 149 } \ 150 virtual void onRun(Reporter* r) SK_OVERRIDE { test_##name(r); } \ 151 }; \ 152 static TestRegistry gReg_##name##Class(name##Class::Factory); \ 153 } \ 154 static void test_##name(skiatest::Reporter* reporter) 155 156 #define DEF_GPUTEST(name, reporter, factory) \ 157 static void test_##name(skiatest::Reporter*, GrContextFactory*); \ 158 namespace skiatest { \ 159 class name##Class : public GpuTest { \ 160 public: \ 161 static Test* Factory(void*) { return SkNEW(name##Class); } \ 162 protected: \ 163 virtual void onGetName(SkString* name) SK_OVERRIDE { \ 164 name->set(#name); \ 165 } \ 166 virtual void onRun(Reporter* r) SK_OVERRIDE { \ 167 test_##name(r, fGrContextFactory); \ 168 } \ 169 }; \ 170 static TestRegistry gReg_##name##Class(name##Class::Factory); \ 171 } \ 172 static void test_##name(skiatest::Reporter* reporter, GrContextFactory* factory) 173 174 #endif 175