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" |
caryclark@google.com | db60de7 | 2013-04-11 12:33:23 +0000 | [diff] [blame] | 14 | #include "SkThread.h" |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 15 | #include "SkTypes.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 16 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 17 | class GrContextFactory; |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 18 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 19 | namespace skiatest { |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 20 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 21 | class Test; |
| 22 | |
| 23 | class Reporter : public SkRefCnt { |
| 24 | public: |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 25 | SK_DECLARE_INST_COUNT(Reporter) |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 26 | Reporter(); |
| 27 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 28 | int countTests() const { return fTestCount; } |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 29 | |
| 30 | void startTest(Test*); |
commit-bot@chromium.org | 1f79286 | 2013-06-18 20:50:34 +0000 | [diff] [blame] | 31 | void reportFailed(const SkString& desc); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 32 | void endTest(Test*); |
commit-bot@chromium.org | c7e08bd | 2013-04-23 11:16:32 +0000 | [diff] [blame] | 33 | |
caryclark@google.com | d54e1e9 | 2013-04-10 15:57:31 +0000 | [diff] [blame] | 34 | virtual bool allowExtendedTest() const { return false; } |
caryclark@google.com | 16cfe40 | 2013-04-18 18:47:37 +0000 | [diff] [blame] | 35 | virtual bool allowThreaded() const { return false; } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 36 | virtual bool verbose() const { return false; } |
commit-bot@chromium.org | c7e08bd | 2013-04-23 11:16:32 +0000 | [diff] [blame] | 37 | virtual void bumpTestCount() { sk_atomic_inc(&fTestCount); } |
| 38 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 39 | protected: |
| 40 | virtual void onStart(Test*) {} |
commit-bot@chromium.org | 1f79286 | 2013-06-18 20:50:34 +0000 | [diff] [blame] | 41 | virtual void onReportFailed(const SkString& desc) {} |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 42 | virtual void onEnd(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 | private: |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 45 | int32_t fTestCount; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 46 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 47 | typedef SkRefCnt INHERITED; |
| 48 | }; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 49 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 50 | class Test { |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 51 | public: |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 52 | Test(); |
| 53 | virtual ~Test(); |
| 54 | |
| 55 | Reporter* getReporter() const { return fReporter; } |
| 56 | void setReporter(Reporter*); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 57 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 58 | const char* getName(); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 59 | void run(); |
| 60 | bool passed() const { return fPassed; } |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 61 | SkMSec elapsedMs() const { return fElapsed; } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 62 | |
scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 63 | static SkString GetTmpDir(); |
djsollen@google.com | cb62650 | 2013-03-20 13:48:20 +0000 | [diff] [blame] | 64 | |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 65 | static void SetResourcePath(const char*); |
scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 66 | static SkString GetResourcePath(); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 67 | |
commit-bot@chromium.org | 5a47b09 | 2014-01-30 15:30:50 +0000 | [diff] [blame] | 68 | virtual bool isGPUTest() const { return false; } |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 69 | virtual void setGrContextFactory(GrContextFactory* factory) {} |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 70 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 71 | protected: |
| 72 | virtual void onGetName(SkString*) = 0; |
| 73 | virtual void onRun(Reporter*) = 0; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 74 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 75 | private: |
| 76 | Reporter* fReporter; |
| 77 | SkString fName; |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 78 | bool fPassed; |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 79 | SkMSec fElapsed; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 82 | class GpuTest : public Test{ |
| 83 | public: |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 84 | GpuTest() : Test(), fGrContextFactory(NULL) {} |
| 85 | |
commit-bot@chromium.org | 5a47b09 | 2014-01-30 15:30:50 +0000 | [diff] [blame] | 86 | virtual bool isGPUTest() const { return true; } |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 87 | virtual void setGrContextFactory(GrContextFactory* factory) { |
| 88 | fGrContextFactory = factory; |
| 89 | } |
| 90 | |
| 91 | protected: |
| 92 | GrContextFactory* fGrContextFactory; // Unowned. |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 95 | typedef SkTRegistry<Test*(*)(void*)> TestRegistry; |
commit-bot@chromium.org | e2eac8b | 2014-01-14 21:04:37 +0000 | [diff] [blame] | 96 | } // namespace skiatest |
| 97 | |
| 98 | /* |
| 99 | Use the following macros to make use of the skiatest classes, e.g. |
| 100 | |
| 101 | #include "Test.h" |
| 102 | |
| 103 | DEF_TEST(TestName, reporter) { |
| 104 | ... |
| 105 | REPORTER_ASSERT(reporter, x == 15); |
| 106 | ... |
| 107 | REPORTER_ASSERT_MESSAGE(reporter, x == 15, "x should be 15"); |
| 108 | ... |
| 109 | if (x != 15) { |
| 110 | ERRORF(reporter, "x should be 15, but is %d", x); |
| 111 | return; |
| 112 | } |
| 113 | ... |
| 114 | } |
| 115 | */ |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 116 | |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 117 | #define REPORTER_ASSERT(r, cond) \ |
| 118 | do { \ |
| 119 | if (!(cond)) { \ |
| 120 | SkString desc; \ |
| 121 | desc.printf("%s:%d\t%s", __FILE__, __LINE__, #cond); \ |
| 122 | r->reportFailed(desc); \ |
| 123 | } \ |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 124 | } while(0) |
| 125 | |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 126 | #define REPORTER_ASSERT_MESSAGE(r, cond, message) \ |
| 127 | do { \ |
| 128 | if (!(cond)) { \ |
| 129 | SkString desc; \ |
| 130 | desc.printf("%s:%d\t%s: %s", __FILE__, __LINE__, \ |
| 131 | message, #cond); \ |
| 132 | r->reportFailed(desc); \ |
| 133 | } \ |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 134 | } while(0) |
| 135 | |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 136 | #define ERRORF(reporter, ...) \ |
| 137 | do { \ |
| 138 | SkString desc; \ |
| 139 | desc.printf("%s:%d\t", __FILE__, __LINE__); \ |
| 140 | desc.appendf(__VA_ARGS__) ; \ |
| 141 | (reporter)->reportFailed(desc); \ |
| 142 | } while(0) |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 143 | |
commit-bot@chromium.org | e2eac8b | 2014-01-14 21:04:37 +0000 | [diff] [blame] | 144 | #define DEF_TEST(name, reporter) \ |
| 145 | static void name(skiatest::Reporter*); \ |
| 146 | namespace skiatest { \ |
| 147 | class name##Class : public Test { \ |
| 148 | public: \ |
| 149 | static Test* Factory(void*) { return SkNEW(name##Class); } \ |
| 150 | protected: \ |
| 151 | virtual void onGetName(SkString* name) SK_OVERRIDE { \ |
| 152 | name->set(#name); \ |
| 153 | } \ |
| 154 | virtual void onRun(Reporter* r) SK_OVERRIDE { name(r); } \ |
| 155 | }; \ |
| 156 | static TestRegistry gReg_##name##Class(name##Class::Factory); \ |
| 157 | } \ |
| 158 | static void name(skiatest::Reporter* reporter) |
| 159 | |
| 160 | #define DEF_GPUTEST(name, reporter, factory) \ |
| 161 | static void name(skiatest::Reporter*, GrContextFactory*); \ |
| 162 | namespace skiatest { \ |
| 163 | class name##Class : public GpuTest { \ |
| 164 | public: \ |
| 165 | static Test* Factory(void*) { return SkNEW(name##Class); } \ |
| 166 | protected: \ |
| 167 | virtual void onGetName(SkString* name) SK_OVERRIDE { \ |
| 168 | name->set(#name); \ |
| 169 | } \ |
| 170 | virtual void onRun(Reporter* r) SK_OVERRIDE { \ |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 171 | name(r, fGrContextFactory); \ |
commit-bot@chromium.org | e2eac8b | 2014-01-14 21:04:37 +0000 | [diff] [blame] | 172 | } \ |
| 173 | }; \ |
| 174 | static TestRegistry gReg_##name##Class(name##Class::Factory); \ |
| 175 | } \ |
| 176 | static void name(skiatest::Reporter* reporter, GrContextFactory* factory) |
| 177 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 178 | #endif |