blob: 8b60039da09270d0f286f57eb13f961b6acdd100 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.comed673312009-02-27 16:24:51 +00007#ifndef skiatest_Test_DEFINED
8#define skiatest_Test_DEFINED
9
reed@android.comed673312009-02-27 16:24:51 +000010#include "SkString.h"
11#include "SkTRegistry.h"
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000012#include "SkTypes.h"
reed@android.comed673312009-02-27 16:24:51 +000013
bsalomonf2f1c172016-04-05 12:59:06 -070014#if SK_SUPPORT_GPU
15#include "GrContextFactory.h"
16#else
bsalomon3724e572016-03-30 18:56:19 -070017namespace sk_gpu_test {
bsalomon@google.com67b915d2013-02-04 16:13:32 +000018class GrContextFactory;
bsalomonf2f1c172016-04-05 12:59:06 -070019class ContextInfo;
bsalomon273c0f52016-03-31 10:59:06 -070020class GLTestContext;
bsalomon3724e572016-03-30 18:56:19 -070021} // namespace sk_gpu_test
kkinnunen179a8f52015-11-20 13:32:24 -080022class GrContext;
bsalomonf2f1c172016-04-05 12:59:06 -070023#endif
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000024
reed@android.comed673312009-02-27 16:24:51 +000025namespace skiatest {
reed@android.com80e39a72009-04-02 16:59:40 +000026
halcanary87f3ba42015-01-20 09:30:20 -080027SkString GetTmpDir();
reed@android.comed673312009-02-27 16:24:51 +000028
halcanary87f3ba42015-01-20 09:30:20 -080029struct 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};
scroggo0ee26272014-11-07 06:07:32 -080038
halcanary87f3ba42015-01-20 09:30:20 -080039class Reporter : SkNoncopyable {
40public:
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;
Cary Clarkab87d7a2016-10-04 10:01:04 -040046 virtual void* stats() const { return nullptr; }
Brian Osman287f6512016-12-05 11:35:07 -050047
48 void reportFailedWithContext(const skiatest::Failure& f) {
49 SkString fullMessage = f.message;
50 if (!fContextStack.empty()) {
51 fullMessage.append(" [");
52 for (int i = 0; i < fContextStack.count(); ++i) {
53 if (i > 0) {
54 fullMessage.append(", ");
55 }
56 fullMessage.append(fContextStack[i]);
57 }
58 fullMessage.append("]");
59 }
60 this->reportFailed(skiatest::Failure(f.fileName, f.lineNo, f.condition, fullMessage));
61 }
62 void push(const SkString& message) {
63 fContextStack.push_back(message);
64 }
65 void pop() {
66 fContextStack.pop_back();
67 }
68
69private:
70 SkTArray<SkString> fContextStack;
halcanary87f3ba42015-01-20 09:30:20 -080071};
scroggo0ee26272014-11-07 06:07:32 -080072
halcanary87f3ba42015-01-20 09:30:20 -080073#define REPORT_FAILURE(reporter, cond, message) \
Brian Osman287f6512016-12-05 11:35:07 -050074 reporter->reportFailedWithContext(skiatest::Failure(__FILE__, __LINE__, cond, message))
75
76class ReporterContext : SkNoncopyable {
77public:
78 ReporterContext(Reporter* reporter, const SkString& message) : fReporter(reporter) {
79 fReporter->push(message);
80 }
81 ~ReporterContext() {
82 fReporter->pop();
83 }
84
85private:
86 Reporter* fReporter;
87};
scroggo0ee26272014-11-07 06:07:32 -080088
bsalomon3724e572016-03-30 18:56:19 -070089typedef void (*TestProc)(skiatest::Reporter*, sk_gpu_test::GrContextFactory*);
reed@android.comed673312009-02-27 16:24:51 +000090
halcanary87f3ba42015-01-20 09:30:20 -080091struct Test {
92 Test(const char* n, bool g, TestProc p) : name(n), needsGpu(g), proc(p) {}
93 const char* name;
94 bool needsGpu;
95 TestProc proc;
96};
reed@android.comed673312009-02-27 16:24:51 +000097
halcanary87f3ba42015-01-20 09:30:20 -080098typedef SkTRegistry<Test> TestRegistry;
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +000099
100/*
101 Use the following macros to make use of the skiatest classes, e.g.
102
103 #include "Test.h"
104
105 DEF_TEST(TestName, reporter) {
106 ...
107 REPORTER_ASSERT(reporter, x == 15);
108 ...
109 REPORTER_ASSERT_MESSAGE(reporter, x == 15, "x should be 15");
110 ...
111 if (x != 15) {
112 ERRORF(reporter, "x should be 15, but is %d", x);
113 return;
114 }
115 ...
116 }
117*/
bsalomon758586c2016-04-06 14:02:39 -0700118
119#if SK_SUPPORT_GPU
120using GrContextFactoryContextType = sk_gpu_test::GrContextFactory::ContextType;
121#else
122using GrContextFactoryContextType = int;
123#endif
bsalomonf2f1c172016-04-05 12:59:06 -0700124
125typedef void GrContextTestFn(Reporter*, const sk_gpu_test::ContextInfo&);
bsalomon758586c2016-04-06 14:02:39 -0700126typedef bool GrContextTypeFilterFn(GrContextFactoryContextType);
bsalomonf2f1c172016-04-05 12:59:06 -0700127
bsalomon758586c2016-04-06 14:02:39 -0700128extern bool IsGLContextType(GrContextFactoryContextType);
bsalomondc0fcd42016-04-11 14:21:33 -0700129extern bool IsVulkanContextType(GrContextFactoryContextType);
bsalomon758586c2016-04-06 14:02:39 -0700130extern bool IsRenderingGLContextType(GrContextFactoryContextType);
131extern bool IsNullGLContextType(GrContextFactoryContextType);
bsalomon758586c2016-04-06 14:02:39 -0700132void RunWithGPUTestContexts(GrContextTestFn*, GrContextTypeFilterFn*,
133 Reporter*, sk_gpu_test::GrContextFactory*);
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700134
135/** Timer provides wall-clock duration since its creation. */
136class Timer {
137public:
138 /** Starts the timer. */
139 Timer();
140
141 /** Nanoseconds since creation. */
142 double elapsedNs() const;
143
144 /** Milliseconds since creation. */
145 double elapsedMs() const;
146
147 /** Milliseconds since creation as an integer.
148 Behavior is undefined for durations longer than SK_MSecMax.
149 */
150 SkMSec elapsedMsInt() const;
151private:
152 double fStartNanos;
153};
154
halcanary87f3ba42015-01-20 09:30:20 -0800155} // namespace skiatest
reed@android.comed673312009-02-27 16:24:51 +0000156
halcanary87f3ba42015-01-20 09:30:20 -0800157#define REPORTER_ASSERT(r, cond) \
158 do { \
159 if (!(cond)) { \
160 REPORT_FAILURE(r, #cond, SkString()); \
161 } \
162 } while (0)
reed@android.comed673312009-02-27 16:24:51 +0000163
halcanary87f3ba42015-01-20 09:30:20 -0800164#define REPORTER_ASSERT_MESSAGE(r, cond, message) \
165 do { \
166 if (!(cond)) { \
167 REPORT_FAILURE(r, #cond, SkString(message)); \
168 } \
169 } while (0)
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000170
halcanary87f3ba42015-01-20 09:30:20 -0800171#define ERRORF(r, ...) \
172 do { \
173 REPORT_FAILURE(r, "", SkStringPrintf(__VA_ARGS__)); \
174 } while (0)
reed@android.comed673312009-02-27 16:24:51 +0000175
halcanary7d571242016-02-24 17:59:16 -0800176#define INFOF(REPORTER, ...) \
177 do { \
178 if ((REPORTER)->verbose()) { \
179 SkDebugf(__VA_ARGS__); \
180 } \
181 } while (0)
182
bsalomon3724e572016-03-30 18:56:19 -0700183#define DEF_TEST(name, reporter) \
184 static void test_##name(skiatest::Reporter*, sk_gpu_test::GrContextFactory*); \
185 skiatest::TestRegistry name##TestRegistry( \
186 skiatest::Test(#name, false, test_##name)); \
187 void test_##name(skiatest::Reporter* reporter, sk_gpu_test::GrContextFactory*)
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +0000188
kkinnunen179a8f52015-11-20 13:32:24 -0800189
bsalomonf2f1c172016-04-05 12:59:06 -0700190#define DEF_GPUTEST(name, reporter, factory) \
191 static void test_##name(skiatest::Reporter*, sk_gpu_test::GrContextFactory*); \
192 skiatest::TestRegistry name##TestRegistry( \
193 skiatest::Test(#name, true, test_##name)); \
bsalomon3724e572016-03-30 18:56:19 -0700194 void test_##name(skiatest::Reporter* reporter, sk_gpu_test::GrContextFactory* factory)
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +0000195
bsalomonfda88072016-04-11 14:40:50 -0700196#define DEF_GPUTEST_FOR_CONTEXTS(name, context_filter, reporter, context_info) \
197 static void test_##name(skiatest::Reporter*, \
198 const sk_gpu_test::ContextInfo& context_info); \
199 static void test_gpu_contexts_##name(skiatest::Reporter* reporter, \
200 sk_gpu_test::GrContextFactory* factory) { \
201 skiatest::RunWithGPUTestContexts(test_##name, context_filter, reporter, factory); \
202 } \
203 skiatest::TestRegistry name##TestRegistry( \
204 skiatest::Test(#name, true, test_gpu_contexts_##name)); \
205 void test_##name(skiatest::Reporter* reporter, \
bsalomonf2f1c172016-04-05 12:59:06 -0700206 const sk_gpu_test::ContextInfo& context_info)
kkinnunen179a8f52015-11-20 13:32:24 -0800207
bsalomonfda88072016-04-11 14:40:50 -0700208#define DEF_GPUTEST_FOR_ALL_CONTEXTS(name, reporter, context_info) \
209 DEF_GPUTEST_FOR_CONTEXTS(name, nullptr, reporter, context_info)
bsalomon68d91342016-04-12 09:59:58 -0700210#define DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name, reporter, context_info) \
211 DEF_GPUTEST_FOR_CONTEXTS(name, sk_gpu_test::GrContextFactory::IsRenderingContext, \
212 reporter, context_info)
bsalomon758586c2016-04-06 14:02:39 -0700213#define DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(name, reporter, context_info) \
214 DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsGLContextType, reporter, context_info)
215#define DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(name, reporter, context_info) \
216 DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsRenderingGLContextType, reporter, context_info)
217#define DEF_GPUTEST_FOR_NULLGL_CONTEXT(name, reporter, context_info) \
218 DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsNullGLContextType, reporter, context_info)
bsalomondc0fcd42016-04-11 14:21:33 -0700219#define DEF_GPUTEST_FOR_VULKAN_CONTEXT(name, reporter, context_info) \
220 DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsVulkanContextType, reporter, context_info)
kkinnunen179a8f52015-11-20 13:32:24 -0800221
halcanary4b656662016-04-27 07:45:18 -0700222#define REQUIRE_PDF_DOCUMENT(TEST_NAME, REPORTER) \
223 do { \
224 SkDynamicMemoryWStream testStream; \
225 sk_sp<SkDocument> testDoc(SkDocument::MakePDF(&testStream)); \
226 if (!testDoc) { \
227 INFOF(REPORTER, "PDF disabled; %s test skipped.", #TEST_NAME); \
228 return; \
229 } \
halcanary2ccdb632015-08-11 13:35:12 -0700230 } while (false)
231
reed@android.comed673312009-02-27 16:24:51 +0000232#endif