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