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