blob: db2552b614a6c7c4d81a3266b6d6c58096745233 [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"
Mike Reedab273fa2017-01-11 13:58:55 -050011#include "../tools/Registry.h"
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000012#include "SkTypes.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050013#include "SkClipOpPriv.h"
reed@android.comed673312009-02-27 16:24:51 +000014
bsalomonf2f1c172016-04-05 12:59:06 -070015#if SK_SUPPORT_GPU
16#include "GrContextFactory.h"
17#else
bsalomon3724e572016-03-30 18:56:19 -070018namespace sk_gpu_test {
bsalomon@google.com67b915d2013-02-04 16:13:32 +000019class GrContextFactory;
bsalomonf2f1c172016-04-05 12:59:06 -070020class ContextInfo;
bsalomon273c0f52016-03-31 10:59:06 -070021class GLTestContext;
bsalomon3724e572016-03-30 18:56:19 -070022} // namespace sk_gpu_test
kkinnunen179a8f52015-11-20 13:32:24 -080023class GrContext;
bsalomonf2f1c172016-04-05 12:59:06 -070024#endif
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000025
reed@android.comed673312009-02-27 16:24:51 +000026namespace skiatest {
reed@android.com80e39a72009-04-02 16:59:40 +000027
halcanary87f3ba42015-01-20 09:30:20 -080028SkString GetTmpDir();
reed@android.comed673312009-02-27 16:24:51 +000029
halcanary87f3ba42015-01-20 09:30:20 -080030struct 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};
scroggo0ee26272014-11-07 06:07:32 -080039
halcanary87f3ba42015-01-20 09:30:20 -080040class Reporter : SkNoncopyable {
41public:
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 Clarkab87d7a2016-10-04 10:01:04 -040047 virtual void* stats() const { return nullptr; }
Brian Osman287f6512016-12-05 11:35:07 -050048
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
70private:
71 SkTArray<SkString> fContextStack;
halcanary87f3ba42015-01-20 09:30:20 -080072};
scroggo0ee26272014-11-07 06:07:32 -080073
halcanary87f3ba42015-01-20 09:30:20 -080074#define REPORT_FAILURE(reporter, cond, message) \
Brian Osman287f6512016-12-05 11:35:07 -050075 reporter->reportFailedWithContext(skiatest::Failure(__FILE__, __LINE__, cond, message))
76
77class ReporterContext : SkNoncopyable {
78public:
79 ReporterContext(Reporter* reporter, const SkString& message) : fReporter(reporter) {
80 fReporter->push(message);
81 }
82 ~ReporterContext() {
83 fReporter->pop();
84 }
85
86private:
87 Reporter* fReporter;
88};
scroggo0ee26272014-11-07 06:07:32 -080089
bsalomon3724e572016-03-30 18:56:19 -070090typedef void (*TestProc)(skiatest::Reporter*, sk_gpu_test::GrContextFactory*);
reed@android.comed673312009-02-27 16:24:51 +000091
halcanary87f3ba42015-01-20 09:30:20 -080092struct 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.comed673312009-02-27 16:24:51 +000098
Mike Reedab273fa2017-01-11 13:58:55 -050099typedef sk_tools::Registry<Test> TestRegistry;
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +0000100
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*/
bsalomon758586c2016-04-06 14:02:39 -0700119
120#if SK_SUPPORT_GPU
121using GrContextFactoryContextType = sk_gpu_test::GrContextFactory::ContextType;
122#else
123using GrContextFactoryContextType = int;
124#endif
bsalomonf2f1c172016-04-05 12:59:06 -0700125
126typedef void GrContextTestFn(Reporter*, const sk_gpu_test::ContextInfo&);
bsalomon758586c2016-04-06 14:02:39 -0700127typedef bool GrContextTypeFilterFn(GrContextFactoryContextType);
bsalomonf2f1c172016-04-05 12:59:06 -0700128
bsalomon758586c2016-04-06 14:02:39 -0700129extern bool IsGLContextType(GrContextFactoryContextType);
bsalomondc0fcd42016-04-11 14:21:33 -0700130extern bool IsVulkanContextType(GrContextFactoryContextType);
bsalomon758586c2016-04-06 14:02:39 -0700131extern bool IsRenderingGLContextType(GrContextFactoryContextType);
132extern bool IsNullGLContextType(GrContextFactoryContextType);
bsalomon758586c2016-04-06 14:02:39 -0700133void RunWithGPUTestContexts(GrContextTestFn*, GrContextTypeFilterFn*,
134 Reporter*, sk_gpu_test::GrContextFactory*);
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700135
136/** Timer provides wall-clock duration since its creation. */
137class Timer {
138public:
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;
152private:
153 double fStartNanos;
154};
155
halcanary87f3ba42015-01-20 09:30:20 -0800156} // namespace skiatest
reed@android.comed673312009-02-27 16:24:51 +0000157
halcanary87f3ba42015-01-20 09:30:20 -0800158#define REPORTER_ASSERT(r, cond) \
159 do { \
160 if (!(cond)) { \
161 REPORT_FAILURE(r, #cond, SkString()); \
162 } \
163 } while (0)
reed@android.comed673312009-02-27 16:24:51 +0000164
halcanary87f3ba42015-01-20 09:30:20 -0800165#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.org1cc8f6f2012-02-22 21:00:42 +0000171
halcanary87f3ba42015-01-20 09:30:20 -0800172#define ERRORF(r, ...) \
173 do { \
174 REPORT_FAILURE(r, "", SkStringPrintf(__VA_ARGS__)); \
175 } while (0)
reed@android.comed673312009-02-27 16:24:51 +0000176
halcanary7d571242016-02-24 17:59:16 -0800177#define INFOF(REPORTER, ...) \
178 do { \
179 if ((REPORTER)->verbose()) { \
180 SkDebugf(__VA_ARGS__); \
181 } \
182 } while (0)
183
bsalomon3724e572016-03-30 18:56:19 -0700184#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.orge2eac8b2014-01-14 21:04:37 +0000189
kkinnunen179a8f52015-11-20 13:32:24 -0800190
bsalomonf2f1c172016-04-05 12:59:06 -0700191#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)); \
bsalomon3724e572016-03-30 18:56:19 -0700195 void test_##name(skiatest::Reporter* reporter, sk_gpu_test::GrContextFactory* factory)
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +0000196
bsalomonfda88072016-04-11 14:40:50 -0700197#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, \
bsalomonf2f1c172016-04-05 12:59:06 -0700207 const sk_gpu_test::ContextInfo& context_info)
kkinnunen179a8f52015-11-20 13:32:24 -0800208
bsalomonfda88072016-04-11 14:40:50 -0700209#define DEF_GPUTEST_FOR_ALL_CONTEXTS(name, reporter, context_info) \
210 DEF_GPUTEST_FOR_CONTEXTS(name, nullptr, reporter, context_info)
bsalomon68d91342016-04-12 09:59:58 -0700211#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)
bsalomon758586c2016-04-06 14:02:39 -0700214#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)
bsalomondc0fcd42016-04-11 14:21:33 -0700220#define DEF_GPUTEST_FOR_VULKAN_CONTEXT(name, reporter, context_info) \
221 DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsVulkanContextType, reporter, context_info)
kkinnunen179a8f52015-11-20 13:32:24 -0800222
halcanary4b656662016-04-27 07:45:18 -0700223#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 } \
halcanary2ccdb632015-08-11 13:35:12 -0700231 } while (false)
232
reed@android.comed673312009-02-27 16:24:51 +0000233#endif