epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 7 | #ifndef skiatest_Test_DEFINED |
| 8 | #define skiatest_Test_DEFINED |
| 9 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 10 | #include "SkString.h" |
| 11 | #include "SkTRegistry.h" |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 12 | #include "SkTypes.h" |
Mike Reed | ebfce6d | 2016-12-12 10:02:12 -0500 | [diff] [blame] | 13 | #include "SkClipOpPriv.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 14 | |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 15 | #if SK_SUPPORT_GPU |
| 16 | #include "GrContextFactory.h" |
| 17 | #else |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 18 | namespace sk_gpu_test { |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 19 | class GrContextFactory; |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 20 | class ContextInfo; |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 21 | class GLTestContext; |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 22 | } // namespace sk_gpu_test |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 23 | class GrContext; |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 24 | #endif |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 25 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 26 | namespace skiatest { |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 27 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 28 | SkString GetTmpDir(); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 29 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 30 | struct Failure { |
| 31 | Failure(const char* f, int l, const char* c, const SkString& m) |
| 32 | : fileName(f), lineNo(l), condition(c), message(m) {} |
| 33 | const char* fileName; |
| 34 | int lineNo; |
| 35 | const char* condition; |
| 36 | SkString message; |
| 37 | SkString toString() const; |
| 38 | }; |
scroggo | 0ee2627 | 2014-11-07 06:07:32 -0800 | [diff] [blame] | 39 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 40 | class Reporter : SkNoncopyable { |
| 41 | public: |
| 42 | virtual ~Reporter() {} |
| 43 | virtual void bumpTestCount(); |
| 44 | virtual void reportFailed(const skiatest::Failure&) = 0; |
| 45 | virtual bool allowExtendedTest() const; |
| 46 | virtual bool verbose() const; |
Cary Clark | ab87d7a | 2016-10-04 10:01:04 -0400 | [diff] [blame] | 47 | virtual void* stats() const { return nullptr; } |
Brian Osman | 287f651 | 2016-12-05 11:35:07 -0500 | [diff] [blame] | 48 | |
| 49 | void reportFailedWithContext(const skiatest::Failure& f) { |
| 50 | SkString fullMessage = f.message; |
| 51 | if (!fContextStack.empty()) { |
| 52 | fullMessage.append(" ["); |
| 53 | for (int i = 0; i < fContextStack.count(); ++i) { |
| 54 | if (i > 0) { |
| 55 | fullMessage.append(", "); |
| 56 | } |
| 57 | fullMessage.append(fContextStack[i]); |
| 58 | } |
| 59 | fullMessage.append("]"); |
| 60 | } |
| 61 | this->reportFailed(skiatest::Failure(f.fileName, f.lineNo, f.condition, fullMessage)); |
| 62 | } |
| 63 | void push(const SkString& message) { |
| 64 | fContextStack.push_back(message); |
| 65 | } |
| 66 | void pop() { |
| 67 | fContextStack.pop_back(); |
| 68 | } |
| 69 | |
| 70 | private: |
| 71 | SkTArray<SkString> fContextStack; |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 72 | }; |
scroggo | 0ee2627 | 2014-11-07 06:07:32 -0800 | [diff] [blame] | 73 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 74 | #define REPORT_FAILURE(reporter, cond, message) \ |
Brian Osman | 287f651 | 2016-12-05 11:35:07 -0500 | [diff] [blame] | 75 | reporter->reportFailedWithContext(skiatest::Failure(__FILE__, __LINE__, cond, message)) |
| 76 | |
| 77 | class ReporterContext : SkNoncopyable { |
| 78 | public: |
| 79 | ReporterContext(Reporter* reporter, const SkString& message) : fReporter(reporter) { |
| 80 | fReporter->push(message); |
| 81 | } |
| 82 | ~ReporterContext() { |
| 83 | fReporter->pop(); |
| 84 | } |
| 85 | |
| 86 | private: |
| 87 | Reporter* fReporter; |
| 88 | }; |
scroggo | 0ee2627 | 2014-11-07 06:07:32 -0800 | [diff] [blame] | 89 | |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 90 | typedef void (*TestProc)(skiatest::Reporter*, sk_gpu_test::GrContextFactory*); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 91 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 92 | struct Test { |
| 93 | Test(const char* n, bool g, TestProc p) : name(n), needsGpu(g), proc(p) {} |
| 94 | const char* name; |
| 95 | bool needsGpu; |
| 96 | TestProc proc; |
| 97 | }; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 98 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 99 | typedef SkTRegistry<Test> TestRegistry; |
commit-bot@chromium.org | e2eac8b | 2014-01-14 21:04:37 +0000 | [diff] [blame] | 100 | |
| 101 | /* |
| 102 | Use the following macros to make use of the skiatest classes, e.g. |
| 103 | |
| 104 | #include "Test.h" |
| 105 | |
| 106 | DEF_TEST(TestName, reporter) { |
| 107 | ... |
| 108 | REPORTER_ASSERT(reporter, x == 15); |
| 109 | ... |
| 110 | REPORTER_ASSERT_MESSAGE(reporter, x == 15, "x should be 15"); |
| 111 | ... |
| 112 | if (x != 15) { |
| 113 | ERRORF(reporter, "x should be 15, but is %d", x); |
| 114 | return; |
| 115 | } |
| 116 | ... |
| 117 | } |
| 118 | */ |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 119 | |
| 120 | #if SK_SUPPORT_GPU |
| 121 | using GrContextFactoryContextType = sk_gpu_test::GrContextFactory::ContextType; |
| 122 | #else |
| 123 | using GrContextFactoryContextType = int; |
| 124 | #endif |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 125 | |
| 126 | typedef void GrContextTestFn(Reporter*, const sk_gpu_test::ContextInfo&); |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 127 | typedef bool GrContextTypeFilterFn(GrContextFactoryContextType); |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 128 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 129 | extern bool IsGLContextType(GrContextFactoryContextType); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 130 | extern bool IsVulkanContextType(GrContextFactoryContextType); |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 131 | extern bool IsRenderingGLContextType(GrContextFactoryContextType); |
| 132 | extern bool IsNullGLContextType(GrContextFactoryContextType); |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 133 | void RunWithGPUTestContexts(GrContextTestFn*, GrContextTypeFilterFn*, |
| 134 | Reporter*, sk_gpu_test::GrContextFactory*); |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 135 | |
| 136 | /** Timer provides wall-clock duration since its creation. */ |
| 137 | class Timer { |
| 138 | public: |
| 139 | /** Starts the timer. */ |
| 140 | Timer(); |
| 141 | |
| 142 | /** Nanoseconds since creation. */ |
| 143 | double elapsedNs() const; |
| 144 | |
| 145 | /** Milliseconds since creation. */ |
| 146 | double elapsedMs() const; |
| 147 | |
| 148 | /** Milliseconds since creation as an integer. |
| 149 | Behavior is undefined for durations longer than SK_MSecMax. |
| 150 | */ |
| 151 | SkMSec elapsedMsInt() const; |
| 152 | private: |
| 153 | double fStartNanos; |
| 154 | }; |
| 155 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 156 | } // namespace skiatest |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 157 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 158 | #define REPORTER_ASSERT(r, cond) \ |
| 159 | do { \ |
| 160 | if (!(cond)) { \ |
| 161 | REPORT_FAILURE(r, #cond, SkString()); \ |
| 162 | } \ |
| 163 | } while (0) |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 164 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 165 | #define REPORTER_ASSERT_MESSAGE(r, cond, message) \ |
| 166 | do { \ |
| 167 | if (!(cond)) { \ |
| 168 | REPORT_FAILURE(r, #cond, SkString(message)); \ |
| 169 | } \ |
| 170 | } while (0) |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 171 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 172 | #define ERRORF(r, ...) \ |
| 173 | do { \ |
| 174 | REPORT_FAILURE(r, "", SkStringPrintf(__VA_ARGS__)); \ |
| 175 | } while (0) |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 176 | |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 177 | #define INFOF(REPORTER, ...) \ |
| 178 | do { \ |
| 179 | if ((REPORTER)->verbose()) { \ |
| 180 | SkDebugf(__VA_ARGS__); \ |
| 181 | } \ |
| 182 | } while (0) |
| 183 | |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 184 | #define DEF_TEST(name, reporter) \ |
| 185 | static void test_##name(skiatest::Reporter*, sk_gpu_test::GrContextFactory*); \ |
| 186 | skiatest::TestRegistry name##TestRegistry( \ |
| 187 | skiatest::Test(#name, false, test_##name)); \ |
| 188 | void test_##name(skiatest::Reporter* reporter, sk_gpu_test::GrContextFactory*) |
commit-bot@chromium.org | e2eac8b | 2014-01-14 21:04:37 +0000 | [diff] [blame] | 189 | |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 190 | |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 191 | #define DEF_GPUTEST(name, reporter, factory) \ |
| 192 | static void test_##name(skiatest::Reporter*, sk_gpu_test::GrContextFactory*); \ |
| 193 | skiatest::TestRegistry name##TestRegistry( \ |
| 194 | skiatest::Test(#name, true, test_##name)); \ |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 195 | void test_##name(skiatest::Reporter* reporter, sk_gpu_test::GrContextFactory* factory) |
commit-bot@chromium.org | e2eac8b | 2014-01-14 21:04:37 +0000 | [diff] [blame] | 196 | |
bsalomon | fda8807 | 2016-04-11 14:40:50 -0700 | [diff] [blame] | 197 | #define DEF_GPUTEST_FOR_CONTEXTS(name, context_filter, reporter, context_info) \ |
| 198 | static void test_##name(skiatest::Reporter*, \ |
| 199 | const sk_gpu_test::ContextInfo& context_info); \ |
| 200 | static void test_gpu_contexts_##name(skiatest::Reporter* reporter, \ |
| 201 | sk_gpu_test::GrContextFactory* factory) { \ |
| 202 | skiatest::RunWithGPUTestContexts(test_##name, context_filter, reporter, factory); \ |
| 203 | } \ |
| 204 | skiatest::TestRegistry name##TestRegistry( \ |
| 205 | skiatest::Test(#name, true, test_gpu_contexts_##name)); \ |
| 206 | void test_##name(skiatest::Reporter* reporter, \ |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 207 | const sk_gpu_test::ContextInfo& context_info) |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 208 | |
bsalomon | fda8807 | 2016-04-11 14:40:50 -0700 | [diff] [blame] | 209 | #define DEF_GPUTEST_FOR_ALL_CONTEXTS(name, reporter, context_info) \ |
| 210 | DEF_GPUTEST_FOR_CONTEXTS(name, nullptr, reporter, context_info) |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 211 | #define DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name, reporter, context_info) \ |
| 212 | DEF_GPUTEST_FOR_CONTEXTS(name, sk_gpu_test::GrContextFactory::IsRenderingContext, \ |
| 213 | reporter, context_info) |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 214 | #define DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(name, reporter, context_info) \ |
| 215 | DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsGLContextType, reporter, context_info) |
| 216 | #define DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(name, reporter, context_info) \ |
| 217 | DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsRenderingGLContextType, reporter, context_info) |
| 218 | #define DEF_GPUTEST_FOR_NULLGL_CONTEXT(name, reporter, context_info) \ |
| 219 | DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsNullGLContextType, reporter, context_info) |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 220 | #define DEF_GPUTEST_FOR_VULKAN_CONTEXT(name, reporter, context_info) \ |
| 221 | DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsVulkanContextType, reporter, context_info) |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 222 | |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 223 | #define REQUIRE_PDF_DOCUMENT(TEST_NAME, REPORTER) \ |
| 224 | do { \ |
| 225 | SkDynamicMemoryWStream testStream; \ |
| 226 | sk_sp<SkDocument> testDoc(SkDocument::MakePDF(&testStream)); \ |
| 227 | if (!testDoc) { \ |
| 228 | INFOF(REPORTER, "PDF disabled; %s test skipped.", #TEST_NAME); \ |
| 229 | return; \ |
| 230 | } \ |
halcanary | 2ccdb63 | 2015-08-11 13:35:12 -0700 | [diff] [blame] | 231 | } while (false) |
| 232 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 233 | #endif |