Test.h revision c7e08bd6d06a421050ddd7060fbafa5b5e047752
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        enum Result {
29            kPassed,    // must begin with 0
30            kFailed,
31            /////
32            kLastResult = kFailed
33        };
34
35        int countTests() const { return fTestCount; }
36
37        void startTest(Test*);
38        void report(const char testDesc[], Result);
39        void endTest(Test*);
40
41        virtual bool allowExtendedTest() const { return false; }
42        virtual bool allowThreaded() const { return false; }
43        virtual void bumpTestCount() { sk_atomic_inc(&fTestCount); }
44
45        // helpers for tests
46        void reportFailed(const char desc[]) {
47            this->report(desc, kFailed);
48        }
49        void reportFailed(const SkString& desc) {
50            this->report(desc.c_str(), kFailed);
51        }
52
53
54    protected:
55        virtual void onStart(Test*) {}
56        virtual void onReport(const char desc[], Result) {}
57        virtual void onEnd(Test*) {}
58
59    private:
60        int32_t fTestCount;
61
62        typedef SkRefCnt INHERITED;
63    };
64
65    class Test {
66    public:
67        Test();
68        virtual ~Test();
69
70        Reporter* getReporter() const { return fReporter; }
71        void setReporter(Reporter*);
72
73        const char* getName();
74        void run();
75        bool passed() const { return fPassed; }
76        SkMSec elapsedMs() const { return fElapsed; }
77
78        static const SkString& GetTmpDir();
79
80        static const SkString& GetResourcePath();
81
82        virtual bool isThreadsafe() const { return true; }
83
84    protected:
85        virtual void onGetName(SkString*) = 0;
86        virtual void onRun(Reporter*) = 0;
87
88    private:
89        Reporter*   fReporter;
90        SkString    fName;
91        bool        fPassed;
92        SkMSec      fElapsed;
93    };
94
95    class GpuTest : public Test{
96    public:
97        GpuTest() : Test() {}
98        static GrContextFactory* GetGrContextFactory();
99        static void DestroyContexts();
100        virtual bool isThreadsafe() const { return false; }
101    private:
102    };
103
104    typedef SkTRegistry<Test*, void*> TestRegistry;
105}
106
107#define REPORTER_ASSERT(r, cond)                                        \
108    do {                                                                \
109        if (!(cond)) {                                                  \
110            SkString desc;                                              \
111            desc.printf("%s:%d: %s", __FILE__, __LINE__, #cond);        \
112            r->reportFailed(desc);                                      \
113        }                                                               \
114    } while(0)
115
116#define REPORTER_ASSERT_MESSAGE(r, cond, message)                            \
117    do {                                                                     \
118        if (!(cond)) {                                                       \
119            SkString desc;                                                   \
120            desc.printf("%s %s:%d: %s", message, __FILE__, __LINE__, #cond); \
121            r->reportFailed(desc);                                           \
122        }                                                                    \
123    } while(0)
124
125
126#endif
127