• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
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  */
8 #ifndef skiatest_Test_DEFINED
9 #define skiatest_Test_DEFINED
10 
11 #include "SkString.h"
12 #include "SkTRegistry.h"
13 #include "SkTypes.h"
14 
15 class GrContextFactory;
16 class GrContext;
17 class SkGLContext;
18 
19 namespace skiatest {
20 
21 SkString GetTmpDir();
22 
23 struct Failure {
FailureFailure24     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 };
32 
33 class Reporter : SkNoncopyable {
34 public:
~Reporter()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 };
41 
42 #define REPORT_FAILURE(reporter, cond, message) \
43     reporter->reportFailed(skiatest::Failure(__FILE__, __LINE__, cond, message))
44 
45 typedef void (*TestProc)(skiatest::Reporter*, GrContextFactory*);
46 
47 struct Test {
TestTest48     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 };
53 
54 typedef SkTRegistry<Test> TestRegistry;
55 
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 */
74 enum GPUTestContexts {
75     kNone_GPUTestContexts         = 0,
76     kNull_GPUTestContexts         = 1,
77     kDebug_GPUTestContexts        = 1 << 1,
78     kNative_GPUTestContexts       = 1 << 2,
79     kOther_GPUTestContexts        = 1 << 3, // Other than native, used only for below.
80     kAllRendering_GPUTestContexts = kNative_GPUTestContexts | kOther_GPUTestContexts,
81     kAll_GPUTestContexts          = kAllRendering_GPUTestContexts
82                                        | kNull_GPUTestContexts
83                                        | kDebug_GPUTestContexts
84 };
85 template<typename T>
86 void RunWithGPUTestContexts(T testFunction, GPUTestContexts contexts, Reporter* reporter,
87                             GrContextFactory* factory);
88 }  // namespace skiatest
89 
90 #define REPORTER_ASSERT(r, cond)                  \
91     do {                                          \
92         if (!(cond)) {                            \
93             REPORT_FAILURE(r, #cond, SkString()); \
94         }                                         \
95     } while (0)
96 
97 #define REPORTER_ASSERT_MESSAGE(r, cond, message)        \
98     do {                                                 \
99         if (!(cond)) {                                   \
100             REPORT_FAILURE(r, #cond, SkString(message)); \
101         }                                                \
102     } while (0)
103 
104 #define ERRORF(r, ...)                                      \
105     do {                                                    \
106         REPORT_FAILURE(r, "", SkStringPrintf(__VA_ARGS__)); \
107     } while (0)
108 
109 #define INFOF(REPORTER, ...)         \
110     do {                             \
111         if ((REPORTER)->verbose()) { \
112             SkDebugf(__VA_ARGS__);   \
113         }                            \
114     } while (0)
115 
116 #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*)
121 
122 #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 
132 #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)
137 
138 #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 
157 #define REQUIRE_PDF_DOCUMENT(TEST_NAME, REPORTER)                             \
158     do {                                                                      \
159         SkDynamicMemoryWStream testStream;                                    \
160         SkAutoTUnref<SkDocument> testDoc(SkDocument::CreatePDF(&testStream)); \
161         if (!testDoc) {                                                       \
162             INFOF(REPORTER, "PDF disabled; %s test skipped.", #TEST_NAME);    \
163             return;                                                           \
164         }                                                                     \
165     } while (false)
166 
167 #endif
168