3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
8 #ifndef skiatest_Test_DEFINED
9 #define skiatest_Test_DEFINED
13 #include "SkTRegistry.h"
17 class GrContextFactory;
23 class Reporter : public SkRefCnt {
25 SK_DECLARE_INST_COUNT(Reporter)
28 int countTests() const { return fTestCount; }
30 void startTest(Test*);
31 void reportFailed(const SkString& desc);
34 virtual bool allowExtendedTest() const { return false; }
35 virtual bool verbose() const { return false; }
36 virtual void bumpTestCount() { sk_atomic_inc(&fTestCount); }
39 virtual void onStart(Test*) {}
40 virtual void onReportFailed(const SkString& desc) {}
41 virtual void onEnd(Test*) {}
46 typedef SkRefCnt INHERITED;
54 Reporter* getReporter() const { return fReporter; }
55 void setReporter(Reporter*);
57 const char* getName();
59 bool passed() const { return fPassed; }
60 SkMSec elapsedMs() const { return fElapsed; }
62 static SkString GetTmpDir();
64 virtual bool isGPUTest() const { return false; }
65 virtual void setGrContextFactory(GrContextFactory* factory) {}
68 virtual void onGetName(SkString*) = 0;
69 virtual void onRun(Reporter*) = 0;
78 class GpuTest : public Test{
80 GpuTest() : Test(), fGrContextFactory(NULL) {}
82 virtual bool isGPUTest() const { return true; }
83 virtual void setGrContextFactory(GrContextFactory* factory) {
84 fGrContextFactory = factory;
88 GrContextFactory* fGrContextFactory; // Unowned.
91 typedef SkTRegistry<Test*(*)(void*)> TestRegistry;
92 } // namespace skiatest
95 Use the following macros to make use of the skiatest classes, e.g.
99 DEF_TEST(TestName, reporter) {
101 REPORTER_ASSERT(reporter, x == 15);
103 REPORTER_ASSERT_MESSAGE(reporter, x == 15, "x should be 15");
106 ERRORF(reporter, "x should be 15, but is %d", x);
113 #define REPORTER_ASSERT(r, cond) \
117 desc.printf("%s:%d\t%s", __FILE__, __LINE__, #cond); \
118 r->reportFailed(desc); \
122 #define REPORTER_ASSERT_MESSAGE(r, cond, message) \
126 desc.printf("%s:%d\t%s: %s", __FILE__, __LINE__, \
128 r->reportFailed(desc); \
132 #define ERRORF(reporter, ...) \
135 desc.printf("%s:%d\t", __FILE__, __LINE__); \
136 desc.appendf(__VA_ARGS__) ; \
137 (reporter)->reportFailed(desc); \
140 #define DEF_TEST(name, reporter) \
141 static void test_##name(skiatest::Reporter*); \
142 namespace skiatest { \
143 class name##Class : public Test { \
145 static Test* Factory(void*) { return SkNEW(name##Class); } \
147 virtual void onGetName(SkString* name) SK_OVERRIDE { \
150 virtual void onRun(Reporter* r) SK_OVERRIDE { test_##name(r); } \
152 static TestRegistry gReg_##name##Class(name##Class::Factory); \
154 static void test_##name(skiatest::Reporter* reporter)
156 #define DEF_GPUTEST(name, reporter, factory) \
157 static void test_##name(skiatest::Reporter*, GrContextFactory*); \
158 namespace skiatest { \
159 class name##Class : public GpuTest { \
161 static Test* Factory(void*) { return SkNEW(name##Class); } \
163 virtual void onGetName(SkString* name) SK_OVERRIDE { \
166 virtual void onRun(Reporter* r) SK_OVERRIDE { \
167 test_##name(r, fGrContextFactory); \
170 static TestRegistry gReg_##name##Class(name##Class::Factory); \
172 static void test_##name(skiatest::Reporter* reporter, GrContextFactory* factory)