blob: d643c8382a168068412043f752ccb802c1c5d44c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.comed673312009-02-27 16:24:51 +00008#ifndef skiatest_Test_DEFINED
9#define skiatest_Test_DEFINED
10
reed@android.comed673312009-02-27 16:24:51 +000011#include "SkString.h"
12#include "SkTRegistry.h"
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000013#include "SkTypes.h"
reed@android.comed673312009-02-27 16:24:51 +000014
bsalomon@google.com67b915d2013-02-04 16:13:32 +000015class GrContextFactory;
kkinnunen179a8f52015-11-20 13:32:24 -080016class GrContext;
17class SkGLContext;
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000018
reed@android.comed673312009-02-27 16:24:51 +000019namespace skiatest {
reed@android.com80e39a72009-04-02 16:59:40 +000020
halcanary87f3ba42015-01-20 09:30:20 -080021SkString GetTmpDir();
reed@android.comed673312009-02-27 16:24:51 +000022
halcanary87f3ba42015-01-20 09:30:20 -080023struct Failure {
24 Failure(const char* f, int l, const char* c, const SkString& m)
25 : fileName(f), lineNo(l), condition(c), message(m) {}
26 const char* fileName;
27 int lineNo;
28 const char* condition;
29 SkString message;
30 SkString toString() const;
31};
scroggo0ee26272014-11-07 06:07:32 -080032
halcanary87f3ba42015-01-20 09:30:20 -080033class Reporter : SkNoncopyable {
34public:
35 virtual ~Reporter() {}
36 virtual void bumpTestCount();
37 virtual void reportFailed(const skiatest::Failure&) = 0;
38 virtual bool allowExtendedTest() const;
39 virtual bool verbose() const;
40};
scroggo0ee26272014-11-07 06:07:32 -080041
halcanary87f3ba42015-01-20 09:30:20 -080042#define REPORT_FAILURE(reporter, cond, message) \
43 reporter->reportFailed(skiatest::Failure(__FILE__, __LINE__, cond, message))
scroggo0ee26272014-11-07 06:07:32 -080044
halcanary87f3ba42015-01-20 09:30:20 -080045typedef void (*TestProc)(skiatest::Reporter*, GrContextFactory*);
reed@android.comed673312009-02-27 16:24:51 +000046
halcanary87f3ba42015-01-20 09:30:20 -080047struct Test {
48 Test(const char* n, bool g, TestProc p) : name(n), needsGpu(g), proc(p) {}
49 const char* name;
50 bool needsGpu;
51 TestProc proc;
52};
reed@android.comed673312009-02-27 16:24:51 +000053
halcanary87f3ba42015-01-20 09:30:20 -080054typedef SkTRegistry<Test> TestRegistry;
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +000055
56/*
57 Use the following macros to make use of the skiatest classes, e.g.
58
59 #include "Test.h"
60
61 DEF_TEST(TestName, reporter) {
62 ...
63 REPORTER_ASSERT(reporter, x == 15);
64 ...
65 REPORTER_ASSERT_MESSAGE(reporter, x == 15, "x should be 15");
66 ...
67 if (x != 15) {
68 ERRORF(reporter, "x should be 15, but is %d", x);
69 return;
70 }
71 ...
72 }
73*/
kkinnunen179a8f52015-11-20 13:32:24 -080074enum GPUTestContexts {
75 kNone_GPUTestContexts = 0,
76 kNull_GPUTestContexts = 1,
kkinnunen55eeae92015-12-10 23:19:29 -080077 kDebug_GPUTestContexts = 1 << 1,
78 kNative_GPUTestContexts = 1 << 2,
79 kOther_GPUTestContexts = 1 << 3, // Other than native, used only for below.
kkinnunen179a8f52015-11-20 13:32:24 -080080 kAllRendering_GPUTestContexts = kNative_GPUTestContexts | kOther_GPUTestContexts,
kkinnunen55eeae92015-12-10 23:19:29 -080081 kAll_GPUTestContexts = kAllRendering_GPUTestContexts
82 | kNull_GPUTestContexts
83 | kDebug_GPUTestContexts
kkinnunen179a8f52015-11-20 13:32:24 -080084};
85template<typename T>
86void RunWithGPUTestContexts(T testFunction, GPUTestContexts contexts, Reporter* reporter,
87 GrContextFactory* factory);
halcanary87f3ba42015-01-20 09:30:20 -080088} // namespace skiatest
reed@android.comed673312009-02-27 16:24:51 +000089
halcanary87f3ba42015-01-20 09:30:20 -080090#define REPORTER_ASSERT(r, cond) \
91 do { \
92 if (!(cond)) { \
93 REPORT_FAILURE(r, #cond, SkString()); \
94 } \
95 } while (0)
reed@android.comed673312009-02-27 16:24:51 +000096
halcanary87f3ba42015-01-20 09:30:20 -080097#define REPORTER_ASSERT_MESSAGE(r, cond, message) \
98 do { \
99 if (!(cond)) { \
100 REPORT_FAILURE(r, #cond, SkString(message)); \
101 } \
102 } while (0)
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000103
halcanary87f3ba42015-01-20 09:30:20 -0800104#define ERRORF(r, ...) \
105 do { \
106 REPORT_FAILURE(r, "", SkStringPrintf(__VA_ARGS__)); \
107 } while (0)
reed@android.comed673312009-02-27 16:24:51 +0000108
halcanary7d571242016-02-24 17:59:16 -0800109#define INFOF(REPORTER, ...) \
110 do { \
111 if ((REPORTER)->verbose()) { \
112 SkDebugf(__VA_ARGS__); \
113 } \
114 } while (0)
115
halcanary87f3ba42015-01-20 09:30:20 -0800116#define DEF_TEST(name, reporter) \
117 static void test_##name(skiatest::Reporter*, GrContextFactory*); \
118 skiatest::TestRegistry name##TestRegistry( \
119 skiatest::Test(#name, false, test_##name)); \
120 void test_##name(skiatest::Reporter* reporter, GrContextFactory*)
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +0000121
kkinnunen179a8f52015-11-20 13:32:24 -0800122#define GPUTEST_EXPAND_MSVC(x) x
123#define GPUTEST_APPLY(C, ...) GPUTEST_EXPAND_MSVC(C(__VA_ARGS__))
124#define GPUTEST_SELECT(a1, a2, N, ...) N
125
126#define GPUTEST_CONTEXT_ARGS1(a1) GrContext* a1
127#define GPUTEST_CONTEXT_ARGS2(a1, a2) GrContext* a1, SkGLContext* a2
128#define GPUTEST_CONTEXT_ARGS(...) \
129 GPUTEST_APPLY(GPUTEST_SELECT(__VA_ARGS__, GPUTEST_CONTEXT_ARGS2, GPUTEST_CONTEXT_ARGS1), \
130 __VA_ARGS__)
131
halcanary87f3ba42015-01-20 09:30:20 -0800132#define DEF_GPUTEST(name, reporter, factory) \
133 static void test_##name(skiatest::Reporter*, GrContextFactory*); \
134 skiatest::TestRegistry name##TestRegistry( \
135 skiatest::Test(#name, true, test_##name)); \
136 void test_##name(skiatest::Reporter* reporter, GrContextFactory* factory)
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +0000137
kkinnunen179a8f52015-11-20 13:32:24 -0800138#define DEF_GPUTEST_FOR_CONTEXTS(name, contexts, reporter, ...) \
139 static void test_##name(skiatest::Reporter*, GPUTEST_CONTEXT_ARGS(__VA_ARGS__)); \
140 static void test_gpu_contexts_##name(skiatest::Reporter* reporter, \
141 GrContextFactory* factory) { \
142 skiatest::RunWithGPUTestContexts(test_##name, contexts, reporter, factory); \
143 } \
144 skiatest::TestRegistry name##TestRegistry( \
145 skiatest::Test(#name, true, test_gpu_contexts_##name)); \
146 void test_##name(skiatest::Reporter* reporter, GPUTEST_CONTEXT_ARGS(__VA_ARGS__))
147
148#define DEF_GPUTEST_FOR_ALL_CONTEXTS(name, reporter, ...) \
149 DEF_GPUTEST_FOR_CONTEXTS(name, skiatest::kAll_GPUTestContexts, reporter, __VA_ARGS__)
150#define DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name, reporter, ...) \
151 DEF_GPUTEST_FOR_CONTEXTS(name, skiatest::kAllRendering_GPUTestContexts, reporter, __VA_ARGS__)
152#define DEF_GPUTEST_FOR_NULL_CONTEXT(name, reporter, ...) \
153 DEF_GPUTEST_FOR_CONTEXTS(name, skiatest::kNull_GPUTestContexts, reporter, __VA_ARGS__)
154#define DEF_GPUTEST_FOR_NATIVE_CONTEXT(name, reporter, ...) \
155 DEF_GPUTEST_FOR_CONTEXTS(name, skiatest::kNative_GPUTestContexts, reporter, __VA_ARGS__)
156
halcanary2ccdb632015-08-11 13:35:12 -0700157#define REQUIRE_PDF_DOCUMENT(TEST_NAME, REPORTER) \
158 do { \
159 SkDynamicMemoryWStream testStream; \
160 SkAutoTUnref<SkDocument> testDoc(SkDocument::CreatePDF(&testStream)); \
161 if (!testDoc) { \
halcanary7d571242016-02-24 17:59:16 -0800162 INFOF(REPORTER, "PDF disabled; %s test skipped.", #TEST_NAME); \
halcanary2ccdb632015-08-11 13:35:12 -0700163 return; \
164 } \
165 } while (false)
166
reed@android.comed673312009-02-27 16:24:51 +0000167#endif