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