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 allowThreaded() const { return false; } 36 virtual bool verbose() const { return false; } 37 virtual void bumpTestCount() { sk_atomic_inc(&fTestCount); } 38 39 protected: 40 virtual void onStart(Test*) {} 41 virtual void onReportFailed(const SkString& desc) {} 42 virtual void onEnd(Test*) {} 43 44 private: 45 int32_t fTestCount; 46 47 typedef SkRefCnt INHERITED; 48 }; 49 50 class Test { 51 public: 52 Test(); 53 virtual ~Test(); 54 55 Reporter* getReporter() const { return fReporter; } 56 void setReporter(Reporter*); 57 58 const char* getName(); 59 void run(); 60 bool passed() const { return fPassed; } 61 SkMSec elapsedMs() const { return fElapsed; } 62 63 static SkString GetTmpDir(); 64 65 virtual bool isGPUTest() const { return false; } 66 virtual void setGrContextFactory(GrContextFactory* factory) {} 67 68 protected: 69 virtual void onGetName(SkString*) = 0; 70 virtual void onRun(Reporter*) = 0; 71 72 private: 73 Reporter* fReporter; 74 SkString fName; 75 bool fPassed; 76 SkMSec fElapsed; 77 }; 78 79 class GpuTest : public Test{ 80 public: 81 GpuTest() : Test(), fGrContextFactory(NULL) {} 82 83 virtual bool isGPUTest() const { return true; } 84 virtual void setGrContextFactory(GrContextFactory* factory) { 85 fGrContextFactory = factory; 86 } 87 88 protected: 89 GrContextFactory* fGrContextFactory; // Unowned. 90 }; 91 92 typedef SkTRegistry<Test*(*)(void*)> TestRegistry; 93 } // namespace skiatest 94 95 /* 96 Use the following macros to make use of the skiatest classes, e.g. 97 98 #include "Test.h" 99 100 DEF_TEST(TestName, reporter) { 101 ... 102 REPORTER_ASSERT(reporter, x == 15); 103 ... 104 REPORTER_ASSERT_MESSAGE(reporter, x == 15, "x should be 15"); 105 ... 106 if (x != 15) { 107 ERRORF(reporter, "x should be 15, but is %d", x); 108 return; 109 } 110 ... 111 } 112 */ 113 114 #define REPORTER_ASSERT(r, cond) \ 115 do { \ 116 if (!(cond)) { \ 117 SkString desc; \ 118 desc.printf("%s:%d\t%s", __FILE__, __LINE__, #cond); \ 119 r->reportFailed(desc); \ 120 } \ 121 } while(0) 122 123 #define REPORTER_ASSERT_MESSAGE(r, cond, message) \ 124 do { \ 125 if (!(cond)) { \ 126 SkString desc; \ 127 desc.printf("%s:%d\t%s: %s", __FILE__, __LINE__, \ 128 message, #cond); \ 129 r->reportFailed(desc); \ 130 } \ 131 } while(0) 132 133 #define ERRORF(reporter, ...) \ 134 do { \ 135 SkString desc; \ 136 desc.printf("%s:%d\t", __FILE__, __LINE__); \ 137 desc.appendf(__VA_ARGS__) ; \ 138 (reporter)->reportFailed(desc); \ 139 } while(0) 140 141 #define DEF_TEST(name, reporter) \ 142 static void name(skiatest::Reporter*); \ 143 namespace skiatest { \ 144 class name##Class : public Test { \ 145 public: \ 146 static Test* Factory(void*) { return SkNEW(name##Class); } \ 147 protected: \ 148 virtual void onGetName(SkString* name) SK_OVERRIDE { \ 149 name->set(#name); \ 150 } \ 151 virtual void onRun(Reporter* r) SK_OVERRIDE { name(r); } \ 152 }; \ 153 static TestRegistry gReg_##name##Class(name##Class::Factory); \ 154 } \ 155 static void name(skiatest::Reporter* reporter) 156 157 #define DEF_GPUTEST(name, reporter, factory) \ 158 static void name(skiatest::Reporter*, GrContextFactory*); \ 159 namespace skiatest { \ 160 class name##Class : public GpuTest { \ 161 public: \ 162 static Test* Factory(void*) { return SkNEW(name##Class); } \ 163 protected: \ 164 virtual void onGetName(SkString* name) SK_OVERRIDE { \ 165 name->set(#name); \ 166 } \ 167 virtual void onRun(Reporter* r) SK_OVERRIDE { \ 168 name(r, fGrContextFactory); \ 169 } \ 170 }; \ 171 static TestRegistry gReg_##name##Class(name##Class::Factory); \ 172 } \ 173 static void name(skiatest::Reporter* reporter, GrContextFactory* factory) 174 175 #endif 176