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