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