| 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 | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 15 | class GrContextFactory; |
| bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 16 | |
| reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 17 | namespace skiatest { |
| reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 18 | |
| reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 19 | class Test; |
| 20 | |
| 21 | class Reporter : public SkRefCnt { |
| 22 | public: |
| robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 23 | SK_DECLARE_INST_COUNT(Reporter) |
| reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 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(); |
| caryclark@google.com | d54e1e9 | 2013-04-10 15:57:31 +0000 | [diff] [blame^] | 34 | void bumpTestCount() { ++fTestCount; } |
| reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 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*); |
| caryclark@google.com | d54e1e9 | 2013-04-10 15:57:31 +0000 | [diff] [blame^] | 44 | virtual bool allowExtendedTest() const { return false; } |
| reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 45 | |
| reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 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 | } |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 63 | |
| reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 64 | bool getCurrSuccess() const { |
| 65 | return fCurrTestSuccess; |
| 66 | } |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 67 | |
| reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 68 | protected: |
| 69 | virtual void onStart(Test*) {} |
| 70 | virtual void onReport(const char desc[], Result) {} |
| 71 | virtual void onEnd(Test*) {} |
| reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 72 | |
| reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 73 | private: |
| 74 | Test* fCurrTest; |
| 75 | int fTestCount; |
| 76 | int fResultCount[kLastResult+1]; |
| reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 77 | bool fCurrTestSuccess; |
| reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 78 | |
| reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 79 | typedef SkRefCnt INHERITED; |
| 80 | }; |
| reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 81 | |
| reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 82 | class Test { |
| reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 83 | public: |
| reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 84 | Test(); |
| 85 | virtual ~Test(); |
| 86 | |
| 87 | Reporter* getReporter() const { return fReporter; } |
| 88 | void setReporter(Reporter*); |
| reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 89 | |
| reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 90 | const char* getName(); |
| reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 91 | bool run(); // returns true on success |
| reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 92 | |
| djsollen@google.com | cb62650 | 2013-03-20 13:48:20 +0000 | [diff] [blame] | 93 | static const SkString& GetTmpDir(); |
| 94 | |
| 95 | static const SkString& GetResourcePath(); |
| reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 96 | |
| reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 97 | protected: |
| 98 | virtual void onGetName(SkString*) = 0; |
| 99 | virtual void onRun(Reporter*) = 0; |
| reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 100 | |
| reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 101 | private: |
| 102 | Reporter* fReporter; |
| 103 | SkString fName; |
| 104 | }; |
| 105 | |
| bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 106 | class GpuTest : public Test{ |
| 107 | public: |
| bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 108 | GpuTest() : Test() {} |
| 109 | static GrContextFactory* GetGrContextFactory(); |
| 110 | static void DestroyContexts(); |
| bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 111 | private: |
| bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
| reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 114 | typedef SkTRegistry<Test*, void*> TestRegistry; |
| 115 | } |
| 116 | |
| 117 | #define REPORTER_ASSERT(r, cond) \ |
| 118 | do { \ |
| 119 | if (!(cond)) { \ |
| 120 | SkString desc; \ |
| junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 121 | desc.printf("%s:%d: %s", __FILE__, __LINE__, #cond); \ |
| reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 122 | r->reportFailed(desc); \ |
| 123 | } \ |
| 124 | } while(0) |
| 125 | |
| junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 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 | |
| reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 135 | |
| 136 | #endif |