blob: 97604a2c726e1cceb3638cd2412fbe34a3c89c40 [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
Mike Reedab273fa2017-01-11 13:58:55 -050010#include "../tools/Registry.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050011#include "SkClipOpPriv.h"
Mike Kleinb323a5e2017-07-24 15:21:31 -040012#include "SkString.h"
13#include "SkTraceEvent.h"
14#include "SkTypes.h"
bsalomonf2f1c172016-04-05 12:59:06 -070015#include "GrContextFactory.h"
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000016
reed@android.comed673312009-02-27 16:24:51 +000017namespace skiatest {
reed@android.com80e39a72009-04-02 16:59:40 +000018
halcanary87f3ba42015-01-20 09:30:20 -080019SkString GetTmpDir();
reed@android.comed673312009-02-27 16:24:51 +000020
halcanary87f3ba42015-01-20 09:30:20 -080021struct Failure {
22 Failure(const char* f, int l, const char* c, const SkString& m)
23 : fileName(f), lineNo(l), condition(c), message(m) {}
24 const char* fileName;
25 int lineNo;
26 const char* condition;
27 SkString message;
28 SkString toString() const;
29};
scroggo0ee26272014-11-07 06:07:32 -080030
halcanary87f3ba42015-01-20 09:30:20 -080031class Reporter : SkNoncopyable {
32public:
33 virtual ~Reporter() {}
34 virtual void bumpTestCount();
35 virtual void reportFailed(const skiatest::Failure&) = 0;
36 virtual bool allowExtendedTest() const;
37 virtual bool verbose() const;
Cary Clarkab87d7a2016-10-04 10:01:04 -040038 virtual void* stats() const { return nullptr; }
Brian Osman287f6512016-12-05 11:35:07 -050039
40 void reportFailedWithContext(const skiatest::Failure& f) {
41 SkString fullMessage = f.message;
42 if (!fContextStack.empty()) {
43 fullMessage.append(" [");
44 for (int i = 0; i < fContextStack.count(); ++i) {
45 if (i > 0) {
46 fullMessage.append(", ");
47 }
48 fullMessage.append(fContextStack[i]);
49 }
50 fullMessage.append("]");
51 }
52 this->reportFailed(skiatest::Failure(f.fileName, f.lineNo, f.condition, fullMessage));
53 }
54 void push(const SkString& message) {
55 fContextStack.push_back(message);
56 }
57 void pop() {
58 fContextStack.pop_back();
59 }
60
61private:
62 SkTArray<SkString> fContextStack;
halcanary87f3ba42015-01-20 09:30:20 -080063};
scroggo0ee26272014-11-07 06:07:32 -080064
halcanary87f3ba42015-01-20 09:30:20 -080065#define REPORT_FAILURE(reporter, cond, message) \
Brian Osman287f6512016-12-05 11:35:07 -050066 reporter->reportFailedWithContext(skiatest::Failure(__FILE__, __LINE__, cond, message))
67
68class ReporterContext : SkNoncopyable {
69public:
70 ReporterContext(Reporter* reporter, const SkString& message) : fReporter(reporter) {
71 fReporter->push(message);
72 }
73 ~ReporterContext() {
74 fReporter->pop();
75 }
76
77private:
78 Reporter* fReporter;
79};
scroggo0ee26272014-11-07 06:07:32 -080080
Brian Salomondcfca432017-11-15 15:48:03 -050081typedef void (*TestProc)(skiatest::Reporter*, const GrContextOptions&);
Robert Phillipsec325342017-10-30 18:02:48 +000082typedef void (*ContextOptionsProc)(GrContextOptions*);
reed@android.comed673312009-02-27 16:24:51 +000083
halcanary87f3ba42015-01-20 09:30:20 -080084struct Test {
Robert Phillipsec325342017-10-30 18:02:48 +000085 Test(const char* n, bool g, TestProc p, ContextOptionsProc optionsProc = nullptr)
86 : name(n), needsGpu(g), proc(p), fContextOptionsProc(optionsProc) {}
halcanary87f3ba42015-01-20 09:30:20 -080087 const char* name;
88 bool needsGpu;
89 TestProc proc;
Robert Phillipsec325342017-10-30 18:02:48 +000090 ContextOptionsProc fContextOptionsProc;
91
92 void modifyGrContextOptions(GrContextOptions* options) {
93 if (fContextOptionsProc) {
94 (*fContextOptionsProc)(options);
95 }
96 }
Mike Kleinb323a5e2017-07-24 15:21:31 -040097
Brian Salomondcfca432017-11-15 15:48:03 -050098 void run(skiatest::Reporter* r, const GrContextOptions& options) const {
Mike Kleinb323a5e2017-07-24 15:21:31 -040099 TRACE_EVENT1("test", TRACE_FUNC, "name", this->name/*these are static*/);
Brian Salomondcfca432017-11-15 15:48:03 -0500100 this->proc(r, options);
Mike Kleinb323a5e2017-07-24 15:21:31 -0400101 }
halcanary87f3ba42015-01-20 09:30:20 -0800102};
reed@android.comed673312009-02-27 16:24:51 +0000103
Mike Reedab273fa2017-01-11 13:58:55 -0500104typedef sk_tools::Registry<Test> TestRegistry;
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +0000105
106/*
107 Use the following macros to make use of the skiatest classes, e.g.
108
109 #include "Test.h"
110
111 DEF_TEST(TestName, reporter) {
112 ...
113 REPORTER_ASSERT(reporter, x == 15);
114 ...
Brian Salomon1c80e992018-01-29 09:50:47 -0500115 REPORTER_ASSERT(reporter, x == 15, "x should be 15");
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +0000116 ...
117 if (x != 15) {
118 ERRORF(reporter, "x should be 15, but is %d", x);
119 return;
120 }
121 ...
122 }
123*/
bsalomon758586c2016-04-06 14:02:39 -0700124
bsalomon758586c2016-04-06 14:02:39 -0700125using GrContextFactoryContextType = sk_gpu_test::GrContextFactory::ContextType;
bsalomonf2f1c172016-04-05 12:59:06 -0700126
127typedef void GrContextTestFn(Reporter*, const sk_gpu_test::ContextInfo&);
bsalomon758586c2016-04-06 14:02:39 -0700128typedef bool GrContextTypeFilterFn(GrContextFactoryContextType);
bsalomonf2f1c172016-04-05 12:59:06 -0700129
bsalomon758586c2016-04-06 14:02:39 -0700130extern bool IsGLContextType(GrContextFactoryContextType);
bsalomondc0fcd42016-04-11 14:21:33 -0700131extern bool IsVulkanContextType(GrContextFactoryContextType);
bsalomon758586c2016-04-06 14:02:39 -0700132extern bool IsRenderingGLContextType(GrContextFactoryContextType);
133extern bool IsNullGLContextType(GrContextFactoryContextType);
Brian Salomondcfca432017-11-15 15:48:03 -0500134void RunWithGPUTestContexts(GrContextTestFn*, GrContextTypeFilterFn*, Reporter*,
135 const GrContextOptions&);
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700136
137/** Timer provides wall-clock duration since its creation. */
138class Timer {
139public:
140 /** Starts the timer. */
141 Timer();
142
143 /** Nanoseconds since creation. */
144 double elapsedNs() const;
145
146 /** Milliseconds since creation. */
147 double elapsedMs() const;
148
149 /** Milliseconds since creation as an integer.
150 Behavior is undefined for durations longer than SK_MSecMax.
151 */
152 SkMSec elapsedMsInt() const;
153private:
154 double fStartNanos;
155};
156
halcanary87f3ba42015-01-20 09:30:20 -0800157} // namespace skiatest
reed@android.comed673312009-02-27 16:24:51 +0000158
Brian Salomon1c80e992018-01-29 09:50:47 -0500159#define REPORTER_ASSERT(r, cond, ...) \
160 do { \
161 if (!(cond)) { \
162 REPORT_FAILURE(r, #cond, SkStringPrintf(__VA_ARGS__)); \
163 } \
halcanary87f3ba42015-01-20 09:30:20 -0800164 } while (0)
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000165
halcanary87f3ba42015-01-20 09:30:20 -0800166#define ERRORF(r, ...) \
167 do { \
168 REPORT_FAILURE(r, "", SkStringPrintf(__VA_ARGS__)); \
169 } while (0)
reed@android.comed673312009-02-27 16:24:51 +0000170
halcanary7d571242016-02-24 17:59:16 -0800171#define INFOF(REPORTER, ...) \
172 do { \
173 if ((REPORTER)->verbose()) { \
174 SkDebugf(__VA_ARGS__); \
175 } \
176 } while (0)
177
Brian Salomondcfca432017-11-15 15:48:03 -0500178#define DEF_TEST(name, reporter) \
179 static void test_##name(skiatest::Reporter*, const GrContextOptions&); \
180 skiatest::TestRegistry name##TestRegistry(skiatest::Test(#name, false, test_##name)); \
181 void test_##name(skiatest::Reporter* reporter, const GrContextOptions&)
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +0000182
Brian Salomondcfca432017-11-15 15:48:03 -0500183#define DEF_GPUTEST(name, reporter, options) \
184 static void test_##name(skiatest::Reporter*, const GrContextOptions&); \
185 skiatest::TestRegistry name##TestRegistry(skiatest::Test(#name, true, test_##name)); \
186 void test_##name(skiatest::Reporter* reporter, const GrContextOptions& options)
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +0000187
Robert Phillipsec325342017-10-30 18:02:48 +0000188#define DEF_GPUTEST_FOR_CONTEXTS(name, context_filter, reporter, context_info, options_filter) \
Brian Salomondcfca432017-11-15 15:48:03 -0500189 static void test_##name(skiatest::Reporter*, const sk_gpu_test::ContextInfo& context_info); \
Robert Phillipsec325342017-10-30 18:02:48 +0000190 static void test_gpu_contexts_##name(skiatest::Reporter* reporter, \
Brian Salomondcfca432017-11-15 15:48:03 -0500191 const GrContextOptions& options) { \
192 skiatest::RunWithGPUTestContexts(test_##name, context_filter, reporter, options); \
Robert Phillipsec325342017-10-30 18:02:48 +0000193 } \
194 skiatest::TestRegistry name##TestRegistry( \
195 skiatest::Test(#name, true, test_gpu_contexts_##name, options_filter)); \
Brian Salomondcfca432017-11-15 15:48:03 -0500196 void test_##name(skiatest::Reporter* reporter, const sk_gpu_test::ContextInfo& context_info)
kkinnunen179a8f52015-11-20 13:32:24 -0800197
bsalomonfda88072016-04-11 14:40:50 -0700198#define DEF_GPUTEST_FOR_ALL_CONTEXTS(name, reporter, context_info) \
Robert Phillipsec325342017-10-30 18:02:48 +0000199 DEF_GPUTEST_FOR_CONTEXTS(name, nullptr, reporter, context_info, nullptr)
200
bsalomon68d91342016-04-12 09:59:58 -0700201#define DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name, reporter, context_info) \
202 DEF_GPUTEST_FOR_CONTEXTS(name, sk_gpu_test::GrContextFactory::IsRenderingContext, \
Robert Phillipsec325342017-10-30 18:02:48 +0000203 reporter, context_info, nullptr)
bsalomon758586c2016-04-06 14:02:39 -0700204#define DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(name, reporter, context_info) \
Robert Phillipsec325342017-10-30 18:02:48 +0000205 DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsGLContextType, \
206 reporter, context_info, nullptr)
bsalomon758586c2016-04-06 14:02:39 -0700207#define DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(name, reporter, context_info) \
Robert Phillipsec325342017-10-30 18:02:48 +0000208 DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsRenderingGLContextType, \
209 reporter, context_info, nullptr)
bsalomon758586c2016-04-06 14:02:39 -0700210#define DEF_GPUTEST_FOR_NULLGL_CONTEXT(name, reporter, context_info) \
Robert Phillipsec325342017-10-30 18:02:48 +0000211 DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsNullGLContextType, \
212 reporter, context_info, nullptr)
bsalomondc0fcd42016-04-11 14:21:33 -0700213#define DEF_GPUTEST_FOR_VULKAN_CONTEXT(name, reporter, context_info) \
Robert Phillipsec325342017-10-30 18:02:48 +0000214 DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsVulkanContextType, \
215 reporter, context_info, nullptr)
kkinnunen179a8f52015-11-20 13:32:24 -0800216
halcanary4b656662016-04-27 07:45:18 -0700217#define REQUIRE_PDF_DOCUMENT(TEST_NAME, REPORTER) \
218 do { \
219 SkDynamicMemoryWStream testStream; \
220 sk_sp<SkDocument> testDoc(SkDocument::MakePDF(&testStream)); \
221 if (!testDoc) { \
222 INFOF(REPORTER, "PDF disabled; %s test skipped.", #TEST_NAME); \
223 return; \
224 } \
halcanary2ccdb632015-08-11 13:35:12 -0700225 } while (false)
226
reed@android.comed673312009-02-27 16:24:51 +0000227#endif