blob: c52a724c9ad346a0ec337be60b7428d869de5db8 [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 Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkString.h"
11#include "include/core/SkTypes.h"
12#include "src/core/SkClipOpPriv.h"
13#include "src/core/SkTraceEvent.h"
14#include "tools/Registry.h"
15#include "tools/gpu/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
Mike Klein4d907da2019-02-07 09:54:30 -050040 void reportFailedWithContext(const skiatest::Failure&);
41
Brian Osman287f6512016-12-05 11:35:07 -050042 void push(const SkString& message) {
43 fContextStack.push_back(message);
44 }
45 void pop() {
46 fContextStack.pop_back();
47 }
48
49private:
50 SkTArray<SkString> fContextStack;
halcanary87f3ba42015-01-20 09:30:20 -080051};
scroggo0ee26272014-11-07 06:07:32 -080052
halcanary87f3ba42015-01-20 09:30:20 -080053#define REPORT_FAILURE(reporter, cond, message) \
Brian Osman287f6512016-12-05 11:35:07 -050054 reporter->reportFailedWithContext(skiatest::Failure(__FILE__, __LINE__, cond, message))
55
56class ReporterContext : SkNoncopyable {
57public:
58 ReporterContext(Reporter* reporter, const SkString& message) : fReporter(reporter) {
59 fReporter->push(message);
60 }
61 ~ReporterContext() {
62 fReporter->pop();
63 }
64
65private:
66 Reporter* fReporter;
67};
scroggo0ee26272014-11-07 06:07:32 -080068
Brian Salomondcfca432017-11-15 15:48:03 -050069typedef void (*TestProc)(skiatest::Reporter*, const GrContextOptions&);
Robert Phillipsec325342017-10-30 18:02:48 +000070typedef void (*ContextOptionsProc)(GrContextOptions*);
reed@android.comed673312009-02-27 16:24:51 +000071
halcanary87f3ba42015-01-20 09:30:20 -080072struct Test {
Robert Phillipsec325342017-10-30 18:02:48 +000073 Test(const char* n, bool g, TestProc p, ContextOptionsProc optionsProc = nullptr)
74 : name(n), needsGpu(g), proc(p), fContextOptionsProc(optionsProc) {}
halcanary87f3ba42015-01-20 09:30:20 -080075 const char* name;
76 bool needsGpu;
77 TestProc proc;
Robert Phillipsec325342017-10-30 18:02:48 +000078 ContextOptionsProc fContextOptionsProc;
79
80 void modifyGrContextOptions(GrContextOptions* options) {
81 if (fContextOptionsProc) {
82 (*fContextOptionsProc)(options);
83 }
84 }
Mike Kleinb323a5e2017-07-24 15:21:31 -040085
Brian Salomondcfca432017-11-15 15:48:03 -050086 void run(skiatest::Reporter* r, const GrContextOptions& options) const {
Mike Kleinb323a5e2017-07-24 15:21:31 -040087 TRACE_EVENT1("test", TRACE_FUNC, "name", this->name/*these are static*/);
Brian Salomondcfca432017-11-15 15:48:03 -050088 this->proc(r, options);
Mike Kleinb323a5e2017-07-24 15:21:31 -040089 }
halcanary87f3ba42015-01-20 09:30:20 -080090};
reed@android.comed673312009-02-27 16:24:51 +000091
Mike Reedab273fa2017-01-11 13:58:55 -050092typedef sk_tools::Registry<Test> TestRegistry;
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +000093
94/*
95 Use the following macros to make use of the skiatest classes, e.g.
96
Mike Kleinc0bd9f92019-04-23 12:05:21 -050097 #include "tests/Test.h"
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +000098
99 DEF_TEST(TestName, reporter) {
100 ...
101 REPORTER_ASSERT(reporter, x == 15);
102 ...
Brian Salomon1c80e992018-01-29 09:50:47 -0500103 REPORTER_ASSERT(reporter, x == 15, "x should be 15");
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +0000104 ...
105 if (x != 15) {
106 ERRORF(reporter, "x should be 15, but is %d", x);
107 return;
108 }
109 ...
110 }
111*/
bsalomon758586c2016-04-06 14:02:39 -0700112
bsalomon758586c2016-04-06 14:02:39 -0700113using GrContextFactoryContextType = sk_gpu_test::GrContextFactory::ContextType;
bsalomonf2f1c172016-04-05 12:59:06 -0700114
115typedef void GrContextTestFn(Reporter*, const sk_gpu_test::ContextInfo&);
bsalomon758586c2016-04-06 14:02:39 -0700116typedef bool GrContextTypeFilterFn(GrContextFactoryContextType);
bsalomonf2f1c172016-04-05 12:59:06 -0700117
bsalomon758586c2016-04-06 14:02:39 -0700118extern bool IsGLContextType(GrContextFactoryContextType);
bsalomondc0fcd42016-04-11 14:21:33 -0700119extern bool IsVulkanContextType(GrContextFactoryContextType);
Timothy Liang760dbc42018-07-17 13:28:20 -0400120extern bool IsMetalContextType(GrContextFactoryContextType);
bsalomon758586c2016-04-06 14:02:39 -0700121extern bool IsRenderingGLContextType(GrContextFactoryContextType);
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400122extern bool IsRenderingGLOrMetalContextType(GrContextFactoryContextType);
Brian Osman7c597742019-03-26 11:10:11 -0400123extern bool IsMockContextType(GrContextFactoryContextType);
Brian Salomondcfca432017-11-15 15:48:03 -0500124void RunWithGPUTestContexts(GrContextTestFn*, GrContextTypeFilterFn*, Reporter*,
125 const GrContextOptions&);
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700126
127/** Timer provides wall-clock duration since its creation. */
128class Timer {
129public:
130 /** Starts the timer. */
131 Timer();
132
133 /** Nanoseconds since creation. */
134 double elapsedNs() const;
135
136 /** Milliseconds since creation. */
137 double elapsedMs() const;
138
139 /** Milliseconds since creation as an integer.
140 Behavior is undefined for durations longer than SK_MSecMax.
141 */
142 SkMSec elapsedMsInt() const;
143private:
144 double fStartNanos;
145};
146
halcanary87f3ba42015-01-20 09:30:20 -0800147} // namespace skiatest
reed@android.comed673312009-02-27 16:24:51 +0000148
Brian Salomon1c80e992018-01-29 09:50:47 -0500149#define REPORTER_ASSERT(r, cond, ...) \
150 do { \
151 if (!(cond)) { \
152 REPORT_FAILURE(r, #cond, SkStringPrintf(__VA_ARGS__)); \
153 } \
halcanary87f3ba42015-01-20 09:30:20 -0800154 } while (0)
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000155
halcanary87f3ba42015-01-20 09:30:20 -0800156#define ERRORF(r, ...) \
157 do { \
158 REPORT_FAILURE(r, "", SkStringPrintf(__VA_ARGS__)); \
159 } while (0)
reed@android.comed673312009-02-27 16:24:51 +0000160
halcanary7d571242016-02-24 17:59:16 -0800161#define INFOF(REPORTER, ...) \
162 do { \
163 if ((REPORTER)->verbose()) { \
164 SkDebugf(__VA_ARGS__); \
165 } \
166 } while (0)
167
Brian Salomondcfca432017-11-15 15:48:03 -0500168#define DEF_TEST(name, reporter) \
169 static void test_##name(skiatest::Reporter*, const GrContextOptions&); \
170 skiatest::TestRegistry name##TestRegistry(skiatest::Test(#name, false, test_##name)); \
171 void test_##name(skiatest::Reporter* reporter, const GrContextOptions&)
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +0000172
Brian Salomondcfca432017-11-15 15:48:03 -0500173#define DEF_GPUTEST(name, reporter, options) \
174 static void test_##name(skiatest::Reporter*, const GrContextOptions&); \
175 skiatest::TestRegistry name##TestRegistry(skiatest::Test(#name, true, test_##name)); \
176 void test_##name(skiatest::Reporter* reporter, const GrContextOptions& options)
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +0000177
Robert Phillipsec325342017-10-30 18:02:48 +0000178#define DEF_GPUTEST_FOR_CONTEXTS(name, context_filter, reporter, context_info, options_filter) \
Brian Salomondcfca432017-11-15 15:48:03 -0500179 static void test_##name(skiatest::Reporter*, const sk_gpu_test::ContextInfo& context_info); \
Robert Phillipsec325342017-10-30 18:02:48 +0000180 static void test_gpu_contexts_##name(skiatest::Reporter* reporter, \
Brian Salomondcfca432017-11-15 15:48:03 -0500181 const GrContextOptions& options) { \
182 skiatest::RunWithGPUTestContexts(test_##name, context_filter, reporter, options); \
Robert Phillipsec325342017-10-30 18:02:48 +0000183 } \
184 skiatest::TestRegistry name##TestRegistry( \
185 skiatest::Test(#name, true, test_gpu_contexts_##name, options_filter)); \
Brian Salomondcfca432017-11-15 15:48:03 -0500186 void test_##name(skiatest::Reporter* reporter, const sk_gpu_test::ContextInfo& context_info)
kkinnunen179a8f52015-11-20 13:32:24 -0800187
bsalomonfda88072016-04-11 14:40:50 -0700188#define DEF_GPUTEST_FOR_ALL_CONTEXTS(name, reporter, context_info) \
Robert Phillipsec325342017-10-30 18:02:48 +0000189 DEF_GPUTEST_FOR_CONTEXTS(name, nullptr, reporter, context_info, nullptr)
190
bsalomon68d91342016-04-12 09:59:58 -0700191#define DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name, reporter, context_info) \
192 DEF_GPUTEST_FOR_CONTEXTS(name, sk_gpu_test::GrContextFactory::IsRenderingContext, \
Robert Phillipsec325342017-10-30 18:02:48 +0000193 reporter, context_info, nullptr)
bsalomon758586c2016-04-06 14:02:39 -0700194#define DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(name, reporter, context_info) \
Robert Phillipsec325342017-10-30 18:02:48 +0000195 DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsGLContextType, \
196 reporter, context_info, nullptr)
bsalomon758586c2016-04-06 14:02:39 -0700197#define DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(name, reporter, context_info) \
Robert Phillipsec325342017-10-30 18:02:48 +0000198 DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsRenderingGLContextType, \
199 reporter, context_info, nullptr)
Brian Osman7c597742019-03-26 11:10:11 -0400200#define DEF_GPUTEST_FOR_MOCK_CONTEXT(name, reporter, context_info) \
201 DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsMockContextType, \
202 reporter, context_info, nullptr)
bsalomondc0fcd42016-04-11 14:21:33 -0700203#define DEF_GPUTEST_FOR_VULKAN_CONTEXT(name, reporter, context_info) \
Robert Phillipsec325342017-10-30 18:02:48 +0000204 DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsVulkanContextType, \
205 reporter, context_info, nullptr)
Timothy Liang760dbc42018-07-17 13:28:20 -0400206#define DEF_GPUTEST_FOR_METAL_CONTEXT(name, reporter, context_info) \
207 DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsMetalContextType, \
208 reporter, context_info, nullptr)
kkinnunen179a8f52015-11-20 13:32:24 -0800209
halcanary4b656662016-04-27 07:45:18 -0700210#define REQUIRE_PDF_DOCUMENT(TEST_NAME, REPORTER) \
211 do { \
Hal Canary3026d4b2019-01-07 10:00:48 -0500212 SkNullWStream testStream; \
213 auto testDoc = SkPDF::MakeDocument(&testStream); \
halcanary4b656662016-04-27 07:45:18 -0700214 if (!testDoc) { \
215 INFOF(REPORTER, "PDF disabled; %s test skipped.", #TEST_NAME); \
216 return; \
217 } \
halcanary2ccdb632015-08-11 13:35:12 -0700218 } while (false)
219
reed@android.comed673312009-02-27 16:24:51 +0000220#endif