Test.h revision 6bd250a2a340348434b7b16bd4e4b5da0f598e3e
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 17class GrContextFactory; 18 19namespace 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 static void SetResourcePath(const char*); 66 static SkString GetResourcePath(); 67 68 virtual bool isGPUTest() const { return false; } 69 virtual void setGrContextFactory(GrContextFactory* factory) {} 70 71 protected: 72 virtual void onGetName(SkString*) = 0; 73 virtual void onRun(Reporter*) = 0; 74 75 private: 76 Reporter* fReporter; 77 SkString fName; 78 bool fPassed; 79 SkMSec fElapsed; 80 }; 81 82 class GpuTest : public Test{ 83 public: 84 GpuTest() : Test() {} 85 86 virtual bool isGPUTest() const { return true; } 87 virtual void setGrContextFactory(GrContextFactory* factory) { 88 fGrContextFactory = factory; 89 } 90 91 protected: 92 GrContextFactory* fGrContextFactory; // Unowned. 93 }; 94 95 typedef SkTRegistry<Test*(*)(void*)> TestRegistry; 96} // namespace skiatest 97 98/* 99 Use the following macros to make use of the skiatest classes, e.g. 100 101 #include "Test.h" 102 103 DEF_TEST(TestName, reporter) { 104 ... 105 REPORTER_ASSERT(reporter, x == 15); 106 ... 107 REPORTER_ASSERT_MESSAGE(reporter, x == 15, "x should be 15"); 108 ... 109 if (x != 15) { 110 ERRORF(reporter, "x should be 15, but is %d", x); 111 return; 112 } 113 ... 114 } 115*/ 116 117#define REPORTER_ASSERT(r, cond) \ 118 do { \ 119 if (!(cond)) { \ 120 SkString desc; \ 121 desc.printf("%s:%d\t%s", __FILE__, __LINE__, #cond); \ 122 r->reportFailed(desc); \ 123 } \ 124 } while(0) 125 126#define REPORTER_ASSERT_MESSAGE(r, cond, message) \ 127 do { \ 128 if (!(cond)) { \ 129 SkString desc; \ 130 desc.printf("%s:%d\t%s: %s", __FILE__, __LINE__, \ 131 message, #cond); \ 132 r->reportFailed(desc); \ 133 } \ 134 } while(0) 135 136#define ERRORF(reporter, ...) \ 137 do { \ 138 SkString desc; \ 139 desc.printf("%s:%d\t", __FILE__, __LINE__); \ 140 desc.appendf(__VA_ARGS__) ; \ 141 (reporter)->reportFailed(desc); \ 142 } while(0) 143 144#define DEF_TEST(name, reporter) \ 145 static void name(skiatest::Reporter*); \ 146 namespace skiatest { \ 147 class name##Class : public Test { \ 148 public: \ 149 static Test* Factory(void*) { return SkNEW(name##Class); } \ 150 protected: \ 151 virtual void onGetName(SkString* name) SK_OVERRIDE { \ 152 name->set(#name); \ 153 } \ 154 virtual void onRun(Reporter* r) SK_OVERRIDE { name(r); } \ 155 }; \ 156 static TestRegistry gReg_##name##Class(name##Class::Factory); \ 157 } \ 158 static void name(skiatest::Reporter* reporter) 159 160#define DEF_GPUTEST(name, reporter, factory) \ 161 static void name(skiatest::Reporter*, GrContextFactory*); \ 162 namespace skiatest { \ 163 class name##Class : public GpuTest { \ 164 public: \ 165 static Test* Factory(void*) { return SkNEW(name##Class); } \ 166 protected: \ 167 virtual void onGetName(SkString* name) SK_OVERRIDE { \ 168 name->set(#name); \ 169 } \ 170 virtual void onRun(Reporter* r) SK_OVERRIDE { \ 171 name(r, fGrContextFactory); \ 172 } \ 173 }; \ 174 static TestRegistry gReg_##name##Class(name##Class::Factory); \ 175 } \ 176 static void name(skiatest::Reporter* reporter, GrContextFactory* factory) 177 178#endif 179