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 15 class GrContext; 16 class SkGLContext; 17 18 namespace skiatest { 19 20 class Test; 21 22 class Reporter : public SkRefCnt { 23 public: 24 SK_DECLARE_INST_COUNT(Reporter) 25 Reporter(); 26 27 enum Result { 28 kPassed, // must begin with 0 29 kFailed, 30 ///// 31 kLastResult = kFailed 32 }; 33 34 void resetReporting(); countTests()35 int countTests() const { return fTestCount; } countResults(Result r)36 int countResults(Result r) { 37 SkASSERT((unsigned)r <= kLastResult); 38 return fResultCount[r]; 39 } 40 41 void startTest(Test*); 42 void report(const char testDesc[], Result); 43 void endTest(Test*); 44 45 // helpers for tests assertTrue(bool cond,const char desc[])46 void assertTrue(bool cond, const char desc[]) { 47 if (!cond) { 48 this->report(desc, kFailed); 49 } 50 } assertFalse(bool cond,const char desc[])51 void assertFalse(bool cond, const char desc[]) { 52 if (cond) { 53 this->report(desc, kFailed); 54 } 55 } reportFailed(const char desc[])56 void reportFailed(const char desc[]) { 57 this->report(desc, kFailed); 58 } reportFailed(const SkString & desc)59 void reportFailed(const SkString& desc) { 60 this->report(desc.c_str(), kFailed); 61 } 62 getCurrSuccess()63 bool getCurrSuccess() const { 64 return fCurrTestSuccess; 65 } 66 67 protected: onStart(Test *)68 virtual void onStart(Test*) {} onReport(const char desc[],Result)69 virtual void onReport(const char desc[], Result) {} onEnd(Test *)70 virtual void onEnd(Test*) {} 71 72 private: 73 Test* fCurrTest; 74 int fTestCount; 75 int fResultCount[kLastResult+1]; 76 bool fCurrTestSuccess; 77 78 typedef SkRefCnt INHERITED; 79 }; 80 81 class Test { 82 public: 83 Test(); 84 virtual ~Test(); 85 getReporter()86 Reporter* getReporter() const { return fReporter; } 87 void setReporter(Reporter*); 88 89 const char* getName(); 90 bool run(); // returns true on success 91 92 protected: 93 virtual void onGetName(SkString*) = 0; 94 virtual void onRun(Reporter*) = 0; 95 96 private: 97 Reporter* fReporter; 98 SkString fName; 99 }; 100 101 class GpuTest : public Test{ 102 public: GpuTest()103 GpuTest() : Test() { 104 fContext = GetContext(); 105 } 106 static GrContext* GetContext(); 107 static void DestroyContext(); 108 protected: 109 GrContext* fContext; 110 private: 111 }; 112 113 typedef SkTRegistry<Test*, void*> TestRegistry; 114 } 115 116 #define REPORTER_ASSERT(r, cond) \ 117 do { \ 118 if (!(cond)) { \ 119 SkString desc; \ 120 desc.printf("%s:%d: %s", __FILE__, __LINE__, #cond); \ 121 r->reportFailed(desc); \ 122 } \ 123 } while(0) 124 125 #define REPORTER_ASSERT_MESSAGE(r, cond, message) \ 126 do { \ 127 if (!(cond)) { \ 128 SkString desc; \ 129 desc.printf("%s %s:%d: %s", message, __FILE__, __LINE__, #cond); \ 130 r->reportFailed(desc); \ 131 } \ 132 } while(0) 133 134 135 #endif 136