OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #ifndef skiatest_Test_DEFINED | 8 #ifndef skiatest_Test_DEFINED |
9 #define skiatest_Test_DEFINED | 9 #define skiatest_Test_DEFINED |
10 | 10 |
11 #include "SkRefCnt.h" | |
12 #include "SkString.h" | 11 #include "SkString.h" |
13 #include "SkTRegistry.h" | 12 #include "SkTRegistry.h" |
14 #include "SkThread.h" | |
15 #include "SkTypes.h" | 13 #include "SkTypes.h" |
16 | 14 |
17 class GrContextFactory; | 15 class GrContextFactory; |
18 | 16 |
19 namespace skiatest { | 17 namespace skiatest { |
20 | 18 |
21 class Test; | 19 SkString GetTmpDir(); |
22 | 20 |
23 /** | 21 struct Failure { |
24 * Information about a single failure from a Test. | 22 Failure(const char* f, int l, const char* c, const SkString& m) |
25 * | 23 : fileName(f), lineNo(l), condition(c), message(m) {} |
26 * Not intended to be created/modified directly. To create one, use one of | 24 const char* fileName; |
27 * | 25 int lineNo; |
28 * REPORTER_ASSERT | 26 const char* condition; |
29 * REPORTER_ASSERT_MESSAGE | 27 SkString message; |
30 * ERRORF | 28 SkString toString() const; |
31 * | 29 }; |
32 * described in more detail further down in this file. | |
33 */ | |
34 struct Failure { | |
35 const char* fileName; | |
36 int lineNo; | |
37 const char* condition; | |
38 SkString message; | |
39 | 30 |
40 // Helper to combine the failure info into one string. | 31 class Reporter : SkNoncopyable { |
41 void getFailureString(SkString* result) const { | 32 public: |
42 if (!result) { | 33 virtual ~Reporter() {} |
43 return; | 34 virtual void bumpTestCount(); |
44 } | 35 virtual void reportFailed(const skiatest::Failure&) = 0; |
45 result->printf("%s:%d\t", fileName, lineNo); | 36 virtual bool allowExtendedTest() const; |
46 if (!message.isEmpty()) { | 37 virtual bool verbose() const; |
47 result->append(message); | 38 }; |
48 if (strlen(condition) > 0) { | |
49 result->append(": "); | |
50 } | |
51 } | |
52 result->append(condition); | |
53 } | |
54 }; | |
55 | 39 |
| 40 #define REPORT_FAILURE(reporter, cond, message) \ |
| 41 reporter->reportFailed(skiatest::Failure(__FILE__, __LINE__, cond, message)) |
56 | 42 |
57 class Reporter : public SkRefCnt { | 43 typedef void (*TestProc)(skiatest::Reporter*, GrContextFactory*); |
58 public: | |
59 SK_DECLARE_INST_COUNT(Reporter) | |
60 Reporter(); | |
61 | 44 |
62 int countTests() const { return fTestCount; } | 45 struct Test { |
| 46 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 }; |
63 | 51 |
64 void startTest(Test*); | 52 typedef SkTRegistry<Test> TestRegistry; |
65 void reportFailed(const Failure&); | |
66 void endTest(Test*); | |
67 | |
68 virtual bool allowExtendedTest() const { return false; } | |
69 virtual bool verbose() const { return false; } | |
70 virtual void bumpTestCount() { sk_atomic_inc(&fTestCount); } | |
71 | |
72 protected: | |
73 virtual void onStart(Test*) {} | |
74 virtual void onReportFailed(const Failure&) {} | |
75 virtual void onEnd(Test*) {} | |
76 | |
77 private: | |
78 int32_t fTestCount; | |
79 | |
80 typedef SkRefCnt INHERITED; | |
81 }; | |
82 | |
83 class Test { | |
84 public: | |
85 Test(); | |
86 virtual ~Test(); | |
87 | |
88 Reporter* getReporter() const { return fReporter; } | |
89 void setReporter(Reporter*); | |
90 | |
91 const char* getName(); | |
92 void run(); | |
93 bool passed() const { return fPassed; } | |
94 SkMSec elapsedMs() const { return fElapsed; } | |
95 | |
96 static SkString GetTmpDir(); | |
97 | |
98 virtual bool isGPUTest() const { return false; } | |
99 virtual void setGrContextFactory(GrContextFactory* factory) {} | |
100 | |
101 protected: | |
102 virtual void onGetName(SkString*) = 0; | |
103 virtual void onRun(Reporter*) = 0; | |
104 | |
105 private: | |
106 Reporter* fReporter; | |
107 SkString fName; | |
108 bool fPassed; | |
109 SkMSec fElapsed; | |
110 }; | |
111 | |
112 class GpuTest : public Test{ | |
113 public: | |
114 GpuTest() : Test(), fGrContextFactory(NULL) {} | |
115 | |
116 virtual bool isGPUTest() const { return true; } | |
117 virtual void setGrContextFactory(GrContextFactory* factory) { | |
118 fGrContextFactory = factory; | |
119 } | |
120 | |
121 protected: | |
122 GrContextFactory* fGrContextFactory; // Unowned. | |
123 }; | |
124 | |
125 typedef SkTRegistry<Test*(*)(void*)> TestRegistry; | |
126 } // namespace skiatest | |
127 | 53 |
128 /* | 54 /* |
129 Use the following macros to make use of the skiatest classes, e.g. | 55 Use the following macros to make use of the skiatest classes, e.g. |
130 | 56 |
131 #include "Test.h" | 57 #include "Test.h" |
132 | 58 |
133 DEF_TEST(TestName, reporter) { | 59 DEF_TEST(TestName, reporter) { |
134 ... | 60 ... |
135 REPORTER_ASSERT(reporter, x == 15); | 61 REPORTER_ASSERT(reporter, x == 15); |
136 ... | 62 ... |
137 REPORTER_ASSERT_MESSAGE(reporter, x == 15, "x should be 15"); | 63 REPORTER_ASSERT_MESSAGE(reporter, x == 15, "x should be 15"); |
138 ... | 64 ... |
139 if (x != 15) { | 65 if (x != 15) { |
140 ERRORF(reporter, "x should be 15, but is %d", x); | 66 ERRORF(reporter, "x should be 15, but is %d", x); |
141 return; | 67 return; |
142 } | 68 } |
143 ... | 69 ... |
144 } | 70 } |
145 */ | 71 */ |
| 72 } // namespace skiatest |
146 | 73 |
147 #define REPORTER_ASSERT(r, cond) \ | 74 #define REPORTER_ASSERT(r, cond) \ |
148 do { \ | 75 do { \ |
149 if (!(cond)) { \ | 76 if (!(cond)) { \ |
150 skiatest::Failure failure = { __FILE__, __LINE__, \ | 77 REPORT_FAILURE(r, #cond, SkString()); \ |
151 #cond, SkString() }; \ | 78 } \ |
152 r->reportFailed(failure); \ | 79 } while (0) |
153 } \ | |
154 } while(0) | |
155 | 80 |
156 #define REPORTER_ASSERT_MESSAGE(r, cond, message) \ | 81 #define REPORTER_ASSERT_MESSAGE(r, cond, message) \ |
157 do { \ | 82 do { \ |
158 if (!(cond)) { \ | 83 if (!(cond)) { \ |
159 skiatest::Failure failure = { __FILE__, __LINE__, \ | 84 REPORT_FAILURE(r, #cond, SkString(message)); \ |
160 #cond, SkString(message) }; \ | 85 } \ |
161 r->reportFailed(failure); \ | 86 } while (0) |
162 } \ | |
163 } while(0) | |
164 | 87 |
165 #define ERRORF(r, ...) \ | 88 #define ERRORF(r, ...) \ |
166 do { \ | 89 do { \ |
167 SkString desc; \ | 90 REPORT_FAILURE(r, "", SkStringPrintf(__VA_ARGS__)); \ |
168 desc.appendf(__VA_ARGS__) ; \ | 91 } while (0) |
169 skiatest::Failure failure = { __FILE__, __LINE__, \ | |
170 "", SkString(desc) }; \ | |
171 r->reportFailed(failure); \ | |
172 } while(0) | |
173 | 92 |
174 #define DEF_TEST(name, reporter) \ | 93 #define DEF_TEST(name, reporter) \ |
175 static void test_##name(skiatest::Reporter*); \ | 94 static void test_##name(skiatest::Reporter*, GrContextFactory*); \ |
176 namespace skiatest { \ | 95 skiatest::TestRegistry name##TestRegistry( \ |
177 class name##Class : public Test { \ | 96 skiatest::Test(#name, false, test_##name)); \ |
178 public: \ | 97 void test_##name(skiatest::Reporter* reporter, GrContextFactory*) |
179 static Test* Factory(void*) { return SkNEW(name##Class); } \ | |
180 protected: \ | |
181 void onGetName(SkString* name) SK_OVERRIDE { \ | |
182 name->set(#name); \ | |
183 } \ | |
184 void onRun(Reporter* r) SK_OVERRIDE { test_##name(r); } \ | |
185 }; \ | |
186 static TestRegistry gReg_##name##Class(name##Class::Factory); \ | |
187 } \ | |
188 static void test_##name(skiatest::Reporter* reporter) | |
189 | 98 |
190 #define DEF_GPUTEST(name, reporter, factory) \ | 99 #define DEF_GPUTEST(name, reporter, factory) \ |
191 static void test_##name(skiatest::Reporter*, GrContextFactory*); \ | 100 static void test_##name(skiatest::Reporter*, GrContextFactory*); \ |
192 namespace skiatest { \ | 101 skiatest::TestRegistry name##TestRegistry( \ |
193 class name##Class : public GpuTest { \ | 102 skiatest::Test(#name, true, test_##name)); \ |
194 public: \ | 103 void test_##name(skiatest::Reporter* reporter, GrContextFactory* factory) |
195 static Test* Factory(void*) { return SkNEW(name##Class); } \ | |
196 protected: \ | |
197 void onGetName(SkString* name) SK_OVERRIDE { \ | |
198 name->set(#name); \ | |
199 } \ | |
200 void onRun(Reporter* r) SK_OVERRIDE { \ | |
201 test_##name(r, fGrContextFactory); \ | |
202 } \ | |
203 }; \ | |
204 static TestRegistry gReg_##name##Class(name##Class::Factory); \ | |
205 } \ | |
206 static void test_##name(skiatest::Reporter* reporter, GrContextFactory* fact
ory) | |
207 | 104 |
208 #endif | 105 #endif |
OLD | NEW |