Test.h revision 52e4f413ffe2d281f9e90ff2147db08083ffcba7
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 "SkRefCnt.h"
12#include "SkString.h"
13#include "SkTRegistry.h"
14#include "SkThread.h"
15#include "SkTypes.h"
16
17class GrContextFactory;
18
19namespace skiatest {
20
21    class Test;
22
23    class Reporter : public SkRefCnt {
24    public:
25        SK_DECLARE_INST_COUNT(Reporter)
26        Reporter();
27
28        int countTests() const { return fTestCount; }
29
30        void startTest(Test*);
31        void reportFailed(const SkString& desc);
32        void endTest(Test*);
33
34        virtual bool allowExtendedTest() const { return false; }
35        virtual bool allowThreaded() const { return false; }
36        virtual bool verbose() const { return false; }
37        virtual void bumpTestCount() { sk_atomic_inc(&fTestCount); }
38
39    protected:
40        virtual void onStart(Test*) {}
41        virtual void onReportFailed(const SkString& desc) {}
42        virtual void onEnd(Test*) {}
43
44    private:
45        int32_t fTestCount;
46
47        typedef SkRefCnt INHERITED;
48    };
49
50    class Test {
51    public:
52        Test();
53        virtual ~Test();
54
55        Reporter* getReporter() const { return fReporter; }
56        void setReporter(Reporter*);
57
58        const char* getName();
59        void run();
60        bool passed() const { return fPassed; }
61        SkMSec elapsedMs() const { return fElapsed; }
62
63        static SkString GetTmpDir();
64
65        static void SetResourcePath(const char*);
66        static SkString GetResourcePath();
67
68        virtual bool isGPUTest() const { return false; }
69        virtual void setGrContextFactory(GrContextFactory* factory) {}
70
71    protected:
72        virtual void onGetName(SkString*) = 0;
73        virtual void onRun(Reporter*) = 0;
74
75    private:
76        static const char* gResourcePath;
77
78        Reporter*   fReporter;
79        SkString    fName;
80        bool        fPassed;
81        SkMSec      fElapsed;
82    };
83
84    class GpuTest : public Test{
85    public:
86        GpuTest() : Test(), fGrContextFactory(NULL) {}
87
88        virtual bool isGPUTest() const { return true; }
89        virtual void setGrContextFactory(GrContextFactory* factory) {
90            fGrContextFactory = factory;
91        }
92
93    protected:
94        GrContextFactory* fGrContextFactory;  // Unowned.
95    };
96
97    typedef SkTRegistry<Test*(*)(void*)> TestRegistry;
98}  // namespace skiatest
99
100/*
101    Use the following macros to make use of the skiatest classes, e.g.
102
103    #include "Test.h"
104
105    DEF_TEST(TestName, reporter) {
106        ...
107        REPORTER_ASSERT(reporter, x == 15);
108        ...
109        REPORTER_ASSERT_MESSAGE(reporter, x == 15, "x should be 15");
110        ...
111        if (x != 15) {
112            ERRORF(reporter, "x should be 15, but is %d", x);
113            return;
114        }
115        ...
116    }
117*/
118
119#define REPORTER_ASSERT(r, cond)                                 \
120    do {                                                         \
121        if (!(cond)) {                                           \
122            SkString desc;                                       \
123            desc.printf("%s:%d\t%s", __FILE__, __LINE__, #cond); \
124            r->reportFailed(desc);                               \
125        }                                                        \
126    } while(0)
127
128#define REPORTER_ASSERT_MESSAGE(r, cond, message)            \
129    do {                                                     \
130        if (!(cond)) {                                       \
131            SkString desc;                                   \
132            desc.printf("%s:%d\t%s: %s", __FILE__, __LINE__, \
133                        message, #cond);                     \
134            r->reportFailed(desc);                           \
135        }                                                    \
136    } while(0)
137
138#define ERRORF(reporter, ...)                       \
139    do {                                            \
140        SkString desc;                              \
141        desc.printf("%s:%d\t", __FILE__, __LINE__); \
142        desc.appendf(__VA_ARGS__) ;                 \
143        (reporter)->reportFailed(desc);             \
144    } while(0)
145
146#define DEF_TEST(name, reporter)                                   \
147    static void name(skiatest::Reporter*);                         \
148    namespace skiatest {                                           \
149    class name##Class : public Test {                              \
150    public:                                                        \
151        static Test* Factory(void*) { return SkNEW(name##Class); } \
152    protected:                                                     \
153        virtual void onGetName(SkString* name) SK_OVERRIDE {       \
154            name->set(#name);                                      \
155        }                                                          \
156        virtual void onRun(Reporter* r) SK_OVERRIDE { name(r); }   \
157    };                                                             \
158    static TestRegistry gReg_##name##Class(name##Class::Factory);  \
159    }                                                              \
160    static void name(skiatest::Reporter* reporter)
161
162#define DEF_GPUTEST(name, reporter, factory)                       \
163    static void name(skiatest::Reporter*, GrContextFactory*);      \
164    namespace skiatest {                                           \
165    class name##Class : public GpuTest {                           \
166    public:                                                        \
167        static Test* Factory(void*) { return SkNEW(name##Class); } \
168    protected:                                                     \
169        virtual void onGetName(SkString* name) SK_OVERRIDE {       \
170            name->set(#name);                                      \
171        }                                                          \
172        virtual void onRun(Reporter* r) SK_OVERRIDE {              \
173            name(r, fGrContextFactory);                            \
174        }                                                          \
175    };                                                             \
176    static TestRegistry gReg_##name##Class(name##Class::Factory);  \
177    }                                                              \
178    static void name(skiatest::Reporter* reporter, GrContextFactory* factory)
179
180#endif
181