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