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