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 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 11 | #include "SkString.h" |
| 12 | #include "SkTRegistry.h" |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 13 | #include "SkTypes.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 14 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 15 | class GrContextFactory; |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 16 | class GrContext; |
| 17 | class SkGLContext; |
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 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 21 | SkString GetTmpDir(); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 22 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 23 | struct Failure { |
| 24 | Failure(const char* f, int l, const char* c, const SkString& m) |
| 25 | : fileName(f), lineNo(l), condition(c), message(m) {} |
| 26 | const char* fileName; |
| 27 | int lineNo; |
| 28 | const char* condition; |
| 29 | SkString message; |
| 30 | SkString toString() const; |
| 31 | }; |
scroggo | 0ee2627 | 2014-11-07 06:07:32 -0800 | [diff] [blame] | 32 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 33 | class Reporter : SkNoncopyable { |
| 34 | public: |
| 35 | virtual ~Reporter() {} |
| 36 | virtual void bumpTestCount(); |
| 37 | virtual void reportFailed(const skiatest::Failure&) = 0; |
| 38 | virtual bool allowExtendedTest() const; |
| 39 | virtual bool verbose() const; |
| 40 | }; |
scroggo | 0ee2627 | 2014-11-07 06:07:32 -0800 | [diff] [blame] | 41 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 42 | #define REPORT_FAILURE(reporter, cond, message) \ |
| 43 | reporter->reportFailed(skiatest::Failure(__FILE__, __LINE__, cond, message)) |
scroggo | 0ee2627 | 2014-11-07 06:07:32 -0800 | [diff] [blame] | 44 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 45 | typedef void (*TestProc)(skiatest::Reporter*, GrContextFactory*); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 46 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 47 | struct Test { |
| 48 | Test(const char* n, bool g, TestProc p) : name(n), needsGpu(g), proc(p) {} |
| 49 | const char* name; |
| 50 | bool needsGpu; |
| 51 | TestProc proc; |
| 52 | }; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 53 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 54 | typedef SkTRegistry<Test> TestRegistry; |
commit-bot@chromium.org | e2eac8b | 2014-01-14 21:04:37 +0000 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | Use the following macros to make use of the skiatest classes, e.g. |
| 58 | |
| 59 | #include "Test.h" |
| 60 | |
| 61 | DEF_TEST(TestName, reporter) { |
| 62 | ... |
| 63 | REPORTER_ASSERT(reporter, x == 15); |
| 64 | ... |
| 65 | REPORTER_ASSERT_MESSAGE(reporter, x == 15, "x should be 15"); |
| 66 | ... |
| 67 | if (x != 15) { |
| 68 | ERRORF(reporter, "x should be 15, but is %d", x); |
| 69 | return; |
| 70 | } |
| 71 | ... |
| 72 | } |
| 73 | */ |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 74 | enum GPUTestContexts { |
| 75 | kNone_GPUTestContexts = 0, |
| 76 | kNull_GPUTestContexts = 1, |
kkinnunen | 55eeae9 | 2015-12-10 23:19:29 -0800 | [diff] [blame] | 77 | kDebug_GPUTestContexts = 1 << 1, |
| 78 | kNative_GPUTestContexts = 1 << 2, |
| 79 | kOther_GPUTestContexts = 1 << 3, // Other than native, used only for below. |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 80 | kAllRendering_GPUTestContexts = kNative_GPUTestContexts | kOther_GPUTestContexts, |
kkinnunen | 55eeae9 | 2015-12-10 23:19:29 -0800 | [diff] [blame] | 81 | kAll_GPUTestContexts = kAllRendering_GPUTestContexts |
| 82 | | kNull_GPUTestContexts |
| 83 | | kDebug_GPUTestContexts |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 84 | }; |
| 85 | template<typename T> |
| 86 | void RunWithGPUTestContexts(T testFunction, GPUTestContexts contexts, Reporter* reporter, |
| 87 | GrContextFactory* factory); |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 88 | } // namespace skiatest |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 89 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 90 | #define REPORTER_ASSERT(r, cond) \ |
| 91 | do { \ |
| 92 | if (!(cond)) { \ |
| 93 | REPORT_FAILURE(r, #cond, SkString()); \ |
| 94 | } \ |
| 95 | } while (0) |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 96 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 97 | #define REPORTER_ASSERT_MESSAGE(r, cond, message) \ |
| 98 | do { \ |
| 99 | if (!(cond)) { \ |
| 100 | REPORT_FAILURE(r, #cond, SkString(message)); \ |
| 101 | } \ |
| 102 | } while (0) |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 103 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 104 | #define ERRORF(r, ...) \ |
| 105 | do { \ |
| 106 | REPORT_FAILURE(r, "", SkStringPrintf(__VA_ARGS__)); \ |
| 107 | } while (0) |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 108 | |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 109 | #define INFOF(REPORTER, ...) \ |
| 110 | do { \ |
| 111 | if ((REPORTER)->verbose()) { \ |
| 112 | SkDebugf(__VA_ARGS__); \ |
| 113 | } \ |
| 114 | } while (0) |
| 115 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 116 | #define DEF_TEST(name, reporter) \ |
| 117 | static void test_##name(skiatest::Reporter*, GrContextFactory*); \ |
| 118 | skiatest::TestRegistry name##TestRegistry( \ |
| 119 | skiatest::Test(#name, false, test_##name)); \ |
| 120 | void test_##name(skiatest::Reporter* reporter, GrContextFactory*) |
commit-bot@chromium.org | e2eac8b | 2014-01-14 21:04:37 +0000 | [diff] [blame] | 121 | |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 122 | #define GPUTEST_EXPAND_MSVC(x) x |
| 123 | #define GPUTEST_APPLY(C, ...) GPUTEST_EXPAND_MSVC(C(__VA_ARGS__)) |
| 124 | #define GPUTEST_SELECT(a1, a2, N, ...) N |
| 125 | |
| 126 | #define GPUTEST_CONTEXT_ARGS1(a1) GrContext* a1 |
| 127 | #define GPUTEST_CONTEXT_ARGS2(a1, a2) GrContext* a1, SkGLContext* a2 |
| 128 | #define GPUTEST_CONTEXT_ARGS(...) \ |
| 129 | GPUTEST_APPLY(GPUTEST_SELECT(__VA_ARGS__, GPUTEST_CONTEXT_ARGS2, GPUTEST_CONTEXT_ARGS1), \ |
| 130 | __VA_ARGS__) |
| 131 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 132 | #define DEF_GPUTEST(name, reporter, factory) \ |
| 133 | static void test_##name(skiatest::Reporter*, GrContextFactory*); \ |
| 134 | skiatest::TestRegistry name##TestRegistry( \ |
| 135 | skiatest::Test(#name, true, test_##name)); \ |
| 136 | void test_##name(skiatest::Reporter* reporter, GrContextFactory* factory) |
commit-bot@chromium.org | e2eac8b | 2014-01-14 21:04:37 +0000 | [diff] [blame] | 137 | |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 138 | #define DEF_GPUTEST_FOR_CONTEXTS(name, contexts, reporter, ...) \ |
| 139 | static void test_##name(skiatest::Reporter*, GPUTEST_CONTEXT_ARGS(__VA_ARGS__)); \ |
| 140 | static void test_gpu_contexts_##name(skiatest::Reporter* reporter, \ |
| 141 | GrContextFactory* factory) { \ |
| 142 | skiatest::RunWithGPUTestContexts(test_##name, contexts, reporter, factory); \ |
| 143 | } \ |
| 144 | skiatest::TestRegistry name##TestRegistry( \ |
| 145 | skiatest::Test(#name, true, test_gpu_contexts_##name)); \ |
| 146 | void test_##name(skiatest::Reporter* reporter, GPUTEST_CONTEXT_ARGS(__VA_ARGS__)) |
| 147 | |
| 148 | #define DEF_GPUTEST_FOR_ALL_CONTEXTS(name, reporter, ...) \ |
| 149 | DEF_GPUTEST_FOR_CONTEXTS(name, skiatest::kAll_GPUTestContexts, reporter, __VA_ARGS__) |
| 150 | #define DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name, reporter, ...) \ |
| 151 | DEF_GPUTEST_FOR_CONTEXTS(name, skiatest::kAllRendering_GPUTestContexts, reporter, __VA_ARGS__) |
| 152 | #define DEF_GPUTEST_FOR_NULL_CONTEXT(name, reporter, ...) \ |
| 153 | DEF_GPUTEST_FOR_CONTEXTS(name, skiatest::kNull_GPUTestContexts, reporter, __VA_ARGS__) |
| 154 | #define DEF_GPUTEST_FOR_NATIVE_CONTEXT(name, reporter, ...) \ |
| 155 | DEF_GPUTEST_FOR_CONTEXTS(name, skiatest::kNative_GPUTestContexts, reporter, __VA_ARGS__) |
| 156 | |
halcanary | 2ccdb63 | 2015-08-11 13:35:12 -0700 | [diff] [blame] | 157 | #define REQUIRE_PDF_DOCUMENT(TEST_NAME, REPORTER) \ |
| 158 | do { \ |
| 159 | SkDynamicMemoryWStream testStream; \ |
| 160 | SkAutoTUnref<SkDocument> testDoc(SkDocument::CreatePDF(&testStream)); \ |
| 161 | if (!testDoc) { \ |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 162 | INFOF(REPORTER, "PDF disabled; %s test skipped.", #TEST_NAME); \ |
halcanary | 2ccdb63 | 2015-08-11 13:35:12 -0700 | [diff] [blame] | 163 | return; \ |
| 164 | } \ |
| 165 | } while (false) |
| 166 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 167 | #endif |