blob: 90a0139f56d6b060e685f745cc75246c87b2f3a8 [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;
46};
scroggo0ee26272014-11-07 06:07:32 -080047
halcanary87f3ba42015-01-20 09:30:20 -080048#define REPORT_FAILURE(reporter, cond, message) \
49 reporter->reportFailed(skiatest::Failure(__FILE__, __LINE__, cond, message))
scroggo0ee26272014-11-07 06:07:32 -080050
bsalomon3724e572016-03-30 18:56:19 -070051typedef void (*TestProc)(skiatest::Reporter*, sk_gpu_test::GrContextFactory*);
reed@android.comed673312009-02-27 16:24:51 +000052
halcanary87f3ba42015-01-20 09:30:20 -080053struct 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.comed673312009-02-27 16:24:51 +000059
halcanary87f3ba42015-01-20 09:30:20 -080060typedef SkTRegistry<Test> TestRegistry;
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +000061
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*/
bsalomon758586c2016-04-06 14:02:39 -070080
81#if SK_SUPPORT_GPU
82using GrContextFactoryContextType = sk_gpu_test::GrContextFactory::ContextType;
83#else
84using GrContextFactoryContextType = int;
85#endif
bsalomonf2f1c172016-04-05 12:59:06 -070086
87typedef void GrContextTestFn(Reporter*, const sk_gpu_test::ContextInfo&);
bsalomon758586c2016-04-06 14:02:39 -070088typedef bool GrContextTypeFilterFn(GrContextFactoryContextType);
bsalomonf2f1c172016-04-05 12:59:06 -070089
bsalomon758586c2016-04-06 14:02:39 -070090extern bool IsGLContextType(GrContextFactoryContextType);
bsalomondc0fcd42016-04-11 14:21:33 -070091extern bool IsVulkanContextType(GrContextFactoryContextType);
bsalomon758586c2016-04-06 14:02:39 -070092extern bool IsRenderingGLContextType(GrContextFactoryContextType);
93extern bool IsNullGLContextType(GrContextFactoryContextType);
94
95void RunWithGPUTestContexts(GrContextTestFn*, GrContextTypeFilterFn*,
96 Reporter*, sk_gpu_test::GrContextFactory*);
benjaminwagnerec4d4d72016-03-25 12:59:53 -070097
98/** Timer provides wall-clock duration since its creation. */
99class Timer {
100public:
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;
114private:
115 double fStartNanos;
116};
117
halcanary87f3ba42015-01-20 09:30:20 -0800118} // namespace skiatest
reed@android.comed673312009-02-27 16:24:51 +0000119
halcanary87f3ba42015-01-20 09:30:20 -0800120#define REPORTER_ASSERT(r, cond) \
121 do { \
122 if (!(cond)) { \
123 REPORT_FAILURE(r, #cond, SkString()); \
124 } \
125 } while (0)
reed@android.comed673312009-02-27 16:24:51 +0000126
halcanary87f3ba42015-01-20 09:30:20 -0800127#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.org1cc8f6f2012-02-22 21:00:42 +0000133
halcanary87f3ba42015-01-20 09:30:20 -0800134#define ERRORF(r, ...) \
135 do { \
136 REPORT_FAILURE(r, "", SkStringPrintf(__VA_ARGS__)); \
137 } while (0)
reed@android.comed673312009-02-27 16:24:51 +0000138
halcanary7d571242016-02-24 17:59:16 -0800139#define INFOF(REPORTER, ...) \
140 do { \
141 if ((REPORTER)->verbose()) { \
142 SkDebugf(__VA_ARGS__); \
143 } \
144 } while (0)
145
bsalomon3724e572016-03-30 18:56:19 -0700146#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.orge2eac8b2014-01-14 21:04:37 +0000151
kkinnunen179a8f52015-11-20 13:32:24 -0800152
bsalomonf2f1c172016-04-05 12:59:06 -0700153#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)); \
bsalomon3724e572016-03-30 18:56:19 -0700157 void test_##name(skiatest::Reporter* reporter, sk_gpu_test::GrContextFactory* factory)
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +0000158
bsalomonfda88072016-04-11 14:40:50 -0700159#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, \
bsalomonf2f1c172016-04-05 12:59:06 -0700169 const sk_gpu_test::ContextInfo& context_info)
kkinnunen179a8f52015-11-20 13:32:24 -0800170
bsalomonfda88072016-04-11 14:40:50 -0700171#define DEF_GPUTEST_FOR_ALL_CONTEXTS(name, reporter, context_info) \
172 DEF_GPUTEST_FOR_CONTEXTS(name, nullptr, reporter, context_info)
bsalomon68d91342016-04-12 09:59:58 -0700173#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)
bsalomon758586c2016-04-06 14:02:39 -0700176#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)
bsalomondc0fcd42016-04-11 14:21:33 -0700182#define DEF_GPUTEST_FOR_VULKAN_CONTEXT(name, reporter, context_info) \
183 DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsVulkanContextType, reporter, context_info)
kkinnunen179a8f52015-11-20 13:32:24 -0800184
halcanary2ccdb632015-08-11 13:35:12 -0700185#define REQUIRE_PDF_DOCUMENT(TEST_NAME, REPORTER) \
186 do { \
187 SkDynamicMemoryWStream testStream; \
188 SkAutoTUnref<SkDocument> testDoc(SkDocument::CreatePDF(&testStream)); \
189 if (!testDoc) { \
halcanary7d571242016-02-24 17:59:16 -0800190 INFOF(REPORTER, "PDF disabled; %s test skipped.", #TEST_NAME); \
halcanary2ccdb632015-08-11 13:35:12 -0700191 return; \
192 } \
193 } while (false)
194
reed@android.comed673312009-02-27 16:24:51 +0000195#endif