Test.h revision a22e2117e44efa4298dd0eb6df304a8166c8e9c3
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
15class GrContext;
16class SkGLContext;
17
18namespace skiatest {
19
20    class Test;
21
22    class Reporter : public SkRefCnt {
23    public:
24        SK_DECLARE_INST_COUNT(Reporter)
25        Reporter();
26
27        enum Result {
28            kPassed,    // must begin with 0
29            kFailed,
30            /////
31            kLastResult = kFailed
32        };
33
34        void resetReporting();
35        int countTests() const { return fTestCount; }
36        int countResults(Result r) {
37            SkASSERT((unsigned)r <= kLastResult);
38            return fResultCount[r];
39        }
40
41        void startTest(Test*);
42        void report(const char testDesc[], Result);
43        void endTest(Test*);
44
45        // helpers for tests
46        void assertTrue(bool cond, const char desc[]) {
47            if (!cond) {
48                this->report(desc, kFailed);
49            }
50        }
51        void assertFalse(bool cond, const char desc[]) {
52            if (cond) {
53                this->report(desc, kFailed);
54            }
55        }
56        void reportFailed(const char desc[]) {
57            this->report(desc, kFailed);
58        }
59        void reportFailed(const SkString& desc) {
60            this->report(desc.c_str(), kFailed);
61        }
62
63        bool getCurrSuccess() const {
64            return fCurrTestSuccess;
65        }
66
67    protected:
68        virtual void onStart(Test*) {}
69        virtual void onReport(const char desc[], Result) {}
70        virtual void onEnd(Test*) {}
71
72    private:
73        Test* fCurrTest;
74        int fTestCount;
75        int fResultCount[kLastResult+1];
76        bool fCurrTestSuccess;
77
78        typedef SkRefCnt INHERITED;
79    };
80
81    class Test {
82    public:
83        Test();
84        virtual ~Test();
85
86        Reporter* getReporter() const { return fReporter; }
87        void setReporter(Reporter*);
88
89        const char* getName();
90        bool run(); // returns true on success
91
92    protected:
93        virtual void onGetName(SkString*) = 0;
94        virtual void onRun(Reporter*) = 0;
95
96    private:
97        Reporter*   fReporter;
98        SkString    fName;
99    };
100
101    class GpuTest : public Test{
102    public:
103        GpuTest() : Test() {
104            fContext = GetContext();
105        }
106    protected:
107        GrContext* fContext;
108    private:
109        static GrContext* GetContext();
110    };
111
112    typedef SkTRegistry<Test*, void*> TestRegistry;
113}
114
115#define REPORTER_ASSERT(r, cond)                                        \
116    do {                                                                \
117        if (!(cond)) {                                                  \
118            SkString desc;                                              \
119            desc.printf("%s:%d: %s", __FILE__, __LINE__, #cond);        \
120            r->reportFailed(desc);                                      \
121        }                                                               \
122    } while(0)
123
124#define REPORTER_ASSERT_MESSAGE(r, cond, message)                            \
125    do {                                                                     \
126        if (!(cond)) {                                                       \
127            SkString desc;                                                   \
128            desc.printf("%s %s:%d: %s", message, __FILE__, __LINE__, #cond); \
129            r->reportFailed(desc);                                           \
130        }                                                                    \
131    } while(0)
132
133
134#endif
135