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 17 class GrContextFactory; 18 19 namespace skiatest { 20 21 class Test; 22 23 class Reporter : public SkRefCnt { 24 public: 25 SK_DECLARE_INST_COUNT(Reporter) 26 Reporter(); 27 countTests()28 int countTests() const { return fTestCount; } 29 30 void startTest(Test*); 31 void reportFailed(const SkString& desc); 32 void endTest(Test*); 33 allowExtendedTest()34 virtual bool allowExtendedTest() const { return false; } allowThreaded()35 virtual bool allowThreaded() const { return false; } verbose()36 virtual bool verbose() const { return false; } bumpTestCount()37 virtual void bumpTestCount() { sk_atomic_inc(&fTestCount); } 38 39 protected: onStart(Test *)40 virtual void onStart(Test*) {} onReportFailed(const SkString & desc)41 virtual void onReportFailed(const SkString& desc) {} onEnd(Test *)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 getReporter()55 Reporter* getReporter() const { return fReporter; } 56 void setReporter(Reporter*); 57 58 const char* getName(); 59 void run(); passed()60 bool passed() const { return fPassed; } elapsedMs()61 SkMSec elapsedMs() const { return fElapsed; } 62 63 static SkString GetTmpDir(); 64 65 static SkString GetResourcePath(); 66 isThreadsafe()67 virtual bool isThreadsafe() const { return true; } 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: GpuTest()82 GpuTest() : Test() {} 83 static GrContextFactory* GetGrContextFactory(); 84 static void DestroyContexts(); isThreadsafe()85 virtual bool isThreadsafe() const { return false; } 86 private: 87 }; 88 89 typedef SkTRegistry<Test*(*)(void*)> TestRegistry; 90 } 91 92 #define REPORTER_ASSERT(r, cond) \ 93 do { \ 94 if (!(cond)) { \ 95 SkString desc; \ 96 desc.printf("%s:%d: %s", __FILE__, __LINE__, #cond); \ 97 r->reportFailed(desc); \ 98 } \ 99 } while(0) 100 101 #define REPORTER_ASSERT_MESSAGE(r, cond, message) \ 102 do { \ 103 if (!(cond)) { \ 104 SkString desc; \ 105 desc.printf("%s %s:%d: %s", message, __FILE__, __LINE__, #cond); \ 106 r->reportFailed(desc); \ 107 } \ 108 } while(0) 109 110 111 #endif 112