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 
17 namespace skiatest {
18 
19 SkString GetTmpDir();
20 
21 struct Failure {
FailureFailure22     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 };
30 
31 class Reporter : SkNoncopyable {
32 public:
~Reporter()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;
38 };
39 
40 #define REPORT_FAILURE(reporter, cond, message) \
41     reporter->reportFailed(skiatest::Failure(__FILE__, __LINE__, cond, message))
42 
43 typedef void (*TestProc)(skiatest::Reporter*, GrContextFactory*);
44 
45 struct Test {
TestTest46     Test(const char* n, bool g, TestProc p) : name(n), needsGpu(g), proc(p) {}
47     const char* name;
48     bool needsGpu;
49     TestProc proc;
50 };
51 
52 typedef SkTRegistry<Test> TestRegistry;
53 
54 /*
55     Use the following macros to make use of the skiatest classes, e.g.
56 
57     #include "Test.h"
58 
59     DEF_TEST(TestName, reporter) {
60         ...
61         REPORTER_ASSERT(reporter, x == 15);
62         ...
63         REPORTER_ASSERT_MESSAGE(reporter, x == 15, "x should be 15");
64         ...
65         if (x != 15) {
66             ERRORF(reporter, "x should be 15, but is %d", x);
67             return;
68         }
69         ...
70     }
71 */
72 }  // namespace skiatest
73 
74 #define REPORTER_ASSERT(r, cond)                  \
75     do {                                          \
76         if (!(cond)) {                            \
77             REPORT_FAILURE(r, #cond, SkString()); \
78         }                                         \
79     } while (0)
80 
81 #define REPORTER_ASSERT_MESSAGE(r, cond, message)        \
82     do {                                                 \
83         if (!(cond)) {                                   \
84             REPORT_FAILURE(r, #cond, SkString(message)); \
85         }                                                \
86     } while (0)
87 
88 #define ERRORF(r, ...)                                      \
89     do {                                                    \
90         REPORT_FAILURE(r, "", SkStringPrintf(__VA_ARGS__)); \
91     } while (0)
92 
93 #define DEF_TEST(name, reporter)                                     \
94     static void test_##name(skiatest::Reporter*, GrContextFactory*); \
95     skiatest::TestRegistry name##TestRegistry(                       \
96             skiatest::Test(#name, false, test_##name));              \
97     void test_##name(skiatest::Reporter* reporter, GrContextFactory*)
98 
99 #define DEF_GPUTEST(name, reporter, factory)                         \
100     static void test_##name(skiatest::Reporter*, GrContextFactory*); \
101     skiatest::TestRegistry name##TestRegistry(                       \
102             skiatest::Test(#name, true, test_##name));               \
103     void test_##name(skiatest::Reporter* reporter, GrContextFactory* factory)
104 
105 #endif
106