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 | |
scroggo | 0ee2627 | 2014-11-07 06:07:32 -0800 | [diff] [blame] | 23 | /** |
| 24 | * Information about a single failure from a Test. |
| 25 | * |
| 26 | * Not intended to be created/modified directly. To create one, use one of |
| 27 | * |
| 28 | * REPORTER_ASSERT |
| 29 | * REPORTER_ASSERT_MESSAGE |
| 30 | * ERRORF |
| 31 | * |
| 32 | * described in more detail further down in this file. |
| 33 | */ |
| 34 | struct Failure { |
| 35 | const char* fileName; |
| 36 | int lineNo; |
| 37 | const char* condition; |
| 38 | SkString message; |
| 39 | |
| 40 | // Helper to combine the failure info into one string. |
| 41 | void getFailureString(SkString* result) const { |
| 42 | if (!result) { |
| 43 | return; |
| 44 | } |
| 45 | result->printf("%s:%d\t", fileName, lineNo); |
| 46 | if (!message.isEmpty()) { |
| 47 | result->append(message); |
| 48 | if (strlen(condition) > 0) { |
| 49 | result->append(": "); |
| 50 | } |
| 51 | } |
| 52 | result->append(condition); |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 57 | class Reporter : public SkRefCnt { |
| 58 | public: |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 59 | SK_DECLARE_INST_COUNT(Reporter) |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 60 | Reporter(); |
| 61 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 62 | int countTests() const { return fTestCount; } |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 63 | |
| 64 | void startTest(Test*); |
scroggo | 0ee2627 | 2014-11-07 06:07:32 -0800 | [diff] [blame] | 65 | void reportFailed(const Failure&); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 66 | void endTest(Test*); |
commit-bot@chromium.org | c7e08bd | 2013-04-23 11:16:32 +0000 | [diff] [blame] | 67 | |
caryclark@google.com | d54e1e9 | 2013-04-10 15:57:31 +0000 | [diff] [blame] | 68 | virtual bool allowExtendedTest() const { return false; } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 69 | virtual bool verbose() const { return false; } |
commit-bot@chromium.org | c7e08bd | 2013-04-23 11:16:32 +0000 | [diff] [blame] | 70 | virtual void bumpTestCount() { sk_atomic_inc(&fTestCount); } |
| 71 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 72 | protected: |
| 73 | virtual void onStart(Test*) {} |
scroggo | 0ee2627 | 2014-11-07 06:07:32 -0800 | [diff] [blame] | 74 | virtual void onReportFailed(const Failure&) {} |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 75 | virtual void onEnd(Test*) {} |
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 | private: |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 78 | int32_t fTestCount; |
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 | typedef SkRefCnt INHERITED; |
| 81 | }; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 82 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 83 | class Test { |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 84 | public: |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 85 | Test(); |
| 86 | virtual ~Test(); |
| 87 | |
| 88 | Reporter* getReporter() const { return fReporter; } |
| 89 | void setReporter(Reporter*); |
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 | const char* getName(); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 92 | void run(); |
| 93 | bool passed() const { return fPassed; } |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 94 | SkMSec elapsedMs() const { return fElapsed; } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 95 | |
scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 96 | static SkString GetTmpDir(); |
djsollen@google.com | cb62650 | 2013-03-20 13:48:20 +0000 | [diff] [blame] | 97 | |
commit-bot@chromium.org | 5a47b09 | 2014-01-30 15:30:50 +0000 | [diff] [blame] | 98 | virtual bool isGPUTest() const { return false; } |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 99 | virtual void setGrContextFactory(GrContextFactory* factory) {} |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 100 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 101 | protected: |
| 102 | virtual void onGetName(SkString*) = 0; |
| 103 | virtual void onRun(Reporter*) = 0; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 104 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 105 | private: |
| 106 | Reporter* fReporter; |
| 107 | SkString fName; |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 108 | bool fPassed; |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 109 | SkMSec fElapsed; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 112 | class GpuTest : public Test{ |
| 113 | public: |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 114 | GpuTest() : Test(), fGrContextFactory(NULL) {} |
| 115 | |
commit-bot@chromium.org | 5a47b09 | 2014-01-30 15:30:50 +0000 | [diff] [blame] | 116 | virtual bool isGPUTest() const { return true; } |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 117 | virtual void setGrContextFactory(GrContextFactory* factory) { |
| 118 | fGrContextFactory = factory; |
| 119 | } |
| 120 | |
| 121 | protected: |
| 122 | GrContextFactory* fGrContextFactory; // Unowned. |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 125 | typedef SkTRegistry<Test*(*)(void*)> TestRegistry; |
commit-bot@chromium.org | e2eac8b | 2014-01-14 21:04:37 +0000 | [diff] [blame] | 126 | } // namespace skiatest |
| 127 | |
| 128 | /* |
| 129 | Use the following macros to make use of the skiatest classes, e.g. |
| 130 | |
| 131 | #include "Test.h" |
| 132 | |
| 133 | DEF_TEST(TestName, reporter) { |
| 134 | ... |
| 135 | REPORTER_ASSERT(reporter, x == 15); |
| 136 | ... |
| 137 | REPORTER_ASSERT_MESSAGE(reporter, x == 15, "x should be 15"); |
| 138 | ... |
| 139 | if (x != 15) { |
| 140 | ERRORF(reporter, "x should be 15, but is %d", x); |
| 141 | return; |
| 142 | } |
| 143 | ... |
| 144 | } |
| 145 | */ |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 146 | |
scroggo | 0ee2627 | 2014-11-07 06:07:32 -0800 | [diff] [blame] | 147 | #define REPORTER_ASSERT(r, cond) \ |
| 148 | do { \ |
| 149 | if (!(cond)) { \ |
| 150 | skiatest::Failure failure = { __FILE__, __LINE__, \ |
| 151 | #cond, SkString() }; \ |
| 152 | r->reportFailed(failure); \ |
| 153 | } \ |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 154 | } while(0) |
| 155 | |
scroggo | 0ee2627 | 2014-11-07 06:07:32 -0800 | [diff] [blame] | 156 | #define REPORTER_ASSERT_MESSAGE(r, cond, message) \ |
| 157 | do { \ |
| 158 | if (!(cond)) { \ |
| 159 | skiatest::Failure failure = { __FILE__, __LINE__, \ |
| 160 | #cond, SkString(message) }; \ |
| 161 | r->reportFailed(failure); \ |
| 162 | } \ |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 163 | } while(0) |
| 164 | |
scroggo | 0ee2627 | 2014-11-07 06:07:32 -0800 | [diff] [blame] | 165 | #define ERRORF(r, ...) \ |
| 166 | do { \ |
| 167 | SkString desc; \ |
| 168 | desc.appendf(__VA_ARGS__) ; \ |
| 169 | skiatest::Failure failure = { __FILE__, __LINE__, \ |
| 170 | "", SkString(desc) }; \ |
| 171 | r->reportFailed(failure); \ |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 172 | } while(0) |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 173 | |
tfarina | 9ea53f9 | 2014-06-24 06:50:39 -0700 | [diff] [blame] | 174 | #define DEF_TEST(name, reporter) \ |
| 175 | static void test_##name(skiatest::Reporter*); \ |
| 176 | namespace skiatest { \ |
| 177 | class name##Class : public Test { \ |
| 178 | public: \ |
| 179 | static Test* Factory(void*) { return SkNEW(name##Class); } \ |
| 180 | protected: \ |
| 181 | virtual void onGetName(SkString* name) SK_OVERRIDE { \ |
| 182 | name->set(#name); \ |
| 183 | } \ |
| 184 | virtual void onRun(Reporter* r) SK_OVERRIDE { test_##name(r); } \ |
| 185 | }; \ |
| 186 | static TestRegistry gReg_##name##Class(name##Class::Factory); \ |
| 187 | } \ |
| 188 | static void test_##name(skiatest::Reporter* reporter) |
commit-bot@chromium.org | e2eac8b | 2014-01-14 21:04:37 +0000 | [diff] [blame] | 189 | |
tfarina | 9ea53f9 | 2014-06-24 06:50:39 -0700 | [diff] [blame] | 190 | #define DEF_GPUTEST(name, reporter, factory) \ |
| 191 | static void test_##name(skiatest::Reporter*, GrContextFactory*); \ |
| 192 | namespace skiatest { \ |
| 193 | class name##Class : public GpuTest { \ |
| 194 | public: \ |
| 195 | static Test* Factory(void*) { return SkNEW(name##Class); } \ |
| 196 | protected: \ |
| 197 | virtual void onGetName(SkString* name) SK_OVERRIDE { \ |
| 198 | name->set(#name); \ |
| 199 | } \ |
| 200 | virtual void onRun(Reporter* r) SK_OVERRIDE { \ |
| 201 | test_##name(r, fGrContextFactory); \ |
| 202 | } \ |
| 203 | }; \ |
| 204 | static TestRegistry gReg_##name##Class(name##Class::Factory); \ |
| 205 | } \ |
| 206 | static void test_##name(skiatest::Reporter* reporter, GrContextFactory* factory) |
commit-bot@chromium.org | e2eac8b | 2014-01-14 21:04:37 +0000 | [diff] [blame] | 207 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 208 | #endif |