blob: 4d2cf2b6392349ed0751eb52274d3d219d27b7d9 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@android.comed673312009-02-27 16:24:51 +00008#ifndef skiatest_Test_DEFINED
9#define skiatest_Test_DEFINED
10
11#include "SkRefCnt.h"
12#include "SkString.h"
13#include "SkTRegistry.h"
caryclark@google.comdb60de72013-04-11 12:33:23 +000014#include "SkThread.h"
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000015#include "SkTypes.h"
reed@android.comed673312009-02-27 16:24:51 +000016
bsalomon@google.com67b915d2013-02-04 16:13:32 +000017class GrContextFactory;
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000018
reed@android.comed673312009-02-27 16:24:51 +000019namespace skiatest {
reed@android.com80e39a72009-04-02 16:59:40 +000020
reed@android.comed673312009-02-27 16:24:51 +000021 class Test;
22
23 class Reporter : public SkRefCnt {
24 public:
robertphillips@google.coma22e2112012-08-16 14:58:06 +000025 SK_DECLARE_INST_COUNT(Reporter)
reed@android.comed673312009-02-27 16:24:51 +000026 Reporter();
27
reed@android.comed673312009-02-27 16:24:51 +000028 int countTests() const { return fTestCount; }
reed@android.comed673312009-02-27 16:24:51 +000029
30 void startTest(Test*);
commit-bot@chromium.org1f792862013-06-18 20:50:34 +000031 void reportFailed(const SkString& desc);
reed@android.comed673312009-02-27 16:24:51 +000032 void endTest(Test*);
commit-bot@chromium.orgc7e08bd2013-04-23 11:16:32 +000033
caryclark@google.comd54e1e92013-04-10 15:57:31 +000034 virtual bool allowExtendedTest() const { return false; }
caryclark@google.com16cfe402013-04-18 18:47:37 +000035 virtual bool allowThreaded() const { return false; }
caryclark@google.com07e97fc2013-07-08 17:17:02 +000036 virtual bool verbose() const { return false; }
commit-bot@chromium.orgc7e08bd2013-04-23 11:16:32 +000037 virtual void bumpTestCount() { sk_atomic_inc(&fTestCount); }
38
reed@android.comed673312009-02-27 16:24:51 +000039 protected:
40 virtual void onStart(Test*) {}
commit-bot@chromium.org1f792862013-06-18 20:50:34 +000041 virtual void onReportFailed(const SkString& desc) {}
reed@android.comed673312009-02-27 16:24:51 +000042 virtual void onEnd(Test*) {}
reed@android.com80e39a72009-04-02 16:59:40 +000043
reed@android.comed673312009-02-27 16:24:51 +000044 private:
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000045 int32_t fTestCount;
reed@android.com80e39a72009-04-02 16:59:40 +000046
reed@android.comed673312009-02-27 16:24:51 +000047 typedef SkRefCnt INHERITED;
48 };
reed@android.com80e39a72009-04-02 16:59:40 +000049
reed@android.comed673312009-02-27 16:24:51 +000050 class Test {
reed@android.com80e39a72009-04-02 16:59:40 +000051 public:
reed@android.comed673312009-02-27 16:24:51 +000052 Test();
53 virtual ~Test();
54
55 Reporter* getReporter() const { return fReporter; }
56 void setReporter(Reporter*);
reed@android.com80e39a72009-04-02 16:59:40 +000057
reed@android.comed673312009-02-27 16:24:51 +000058 const char* getName();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000059 void run();
60 bool passed() const { return fPassed; }
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000061 SkMSec elapsedMs() const { return fElapsed; }
reed@android.com80e39a72009-04-02 16:59:40 +000062
scroggo@google.comc76218d2013-06-06 14:59:56 +000063 static SkString GetTmpDir();
djsollen@google.comcb626502013-03-20 13:48:20 +000064
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000065 static void SetResourcePath(const char*);
scroggo@google.comc76218d2013-06-06 14:59:56 +000066 static SkString GetResourcePath();
reed@google.com789c6f22013-02-25 20:24:24 +000067
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +000068 virtual bool isGPUTest() const { return false; }
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000069 virtual void setGrContextFactory(GrContextFactory* factory) {}
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000070
reed@android.comed673312009-02-27 16:24:51 +000071 protected:
72 virtual void onGetName(SkString*) = 0;
73 virtual void onRun(Reporter*) = 0;
reed@android.com80e39a72009-04-02 16:59:40 +000074
reed@android.comed673312009-02-27 16:24:51 +000075 private:
76 Reporter* fReporter;
77 SkString fName;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000078 bool fPassed;
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000079 SkMSec fElapsed;
reed@android.comed673312009-02-27 16:24:51 +000080 };
81
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000082 class GpuTest : public Test{
83 public:
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000084 GpuTest() : Test(), fGrContextFactory(NULL) {}
85
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +000086 virtual bool isGPUTest() const { return true; }
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000087 virtual void setGrContextFactory(GrContextFactory* factory) {
88 fGrContextFactory = factory;
89 }
90
91 protected:
92 GrContextFactory* fGrContextFactory; // Unowned.
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000093 };
94
mtklein@google.combd6343b2013-09-04 17:20:18 +000095 typedef SkTRegistry<Test*(*)(void*)> TestRegistry;
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +000096} // namespace skiatest
97
98/*
99 Use the following macros to make use of the skiatest classes, e.g.
100
101 #include "Test.h"
102
103 DEF_TEST(TestName, reporter) {
104 ...
105 REPORTER_ASSERT(reporter, x == 15);
106 ...
107 REPORTER_ASSERT_MESSAGE(reporter, x == 15, "x should be 15");
108 ...
109 if (x != 15) {
110 ERRORF(reporter, "x should be 15, but is %d", x);
111 return;
112 }
113 ...
114 }
115*/
reed@android.comed673312009-02-27 16:24:51 +0000116
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000117#define REPORTER_ASSERT(r, cond) \
118 do { \
119 if (!(cond)) { \
120 SkString desc; \
121 desc.printf("%s:%d\t%s", __FILE__, __LINE__, #cond); \
122 r->reportFailed(desc); \
123 } \
reed@android.comed673312009-02-27 16:24:51 +0000124 } while(0)
125
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000126#define REPORTER_ASSERT_MESSAGE(r, cond, message) \
127 do { \
128 if (!(cond)) { \
129 SkString desc; \
130 desc.printf("%s:%d\t%s: %s", __FILE__, __LINE__, \
131 message, #cond); \
132 r->reportFailed(desc); \
133 } \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000134 } while(0)
135
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000136#define ERRORF(reporter, ...) \
137 do { \
138 SkString desc; \
139 desc.printf("%s:%d\t", __FILE__, __LINE__); \
140 desc.appendf(__VA_ARGS__) ; \
141 (reporter)->reportFailed(desc); \
142 } while(0)
reed@android.comed673312009-02-27 16:24:51 +0000143
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +0000144#define DEF_TEST(name, reporter) \
145 static void name(skiatest::Reporter*); \
146 namespace skiatest { \
147 class name##Class : public Test { \
148 public: \
149 static Test* Factory(void*) { return SkNEW(name##Class); } \
150 protected: \
151 virtual void onGetName(SkString* name) SK_OVERRIDE { \
152 name->set(#name); \
153 } \
154 virtual void onRun(Reporter* r) SK_OVERRIDE { name(r); } \
155 }; \
156 static TestRegistry gReg_##name##Class(name##Class::Factory); \
157 } \
158 static void name(skiatest::Reporter* reporter)
159
160#define DEF_GPUTEST(name, reporter, factory) \
161 static void name(skiatest::Reporter*, GrContextFactory*); \
162 namespace skiatest { \
163 class name##Class : public GpuTest { \
164 public: \
165 static Test* Factory(void*) { return SkNEW(name##Class); } \
166 protected: \
167 virtual void onGetName(SkString* name) SK_OVERRIDE { \
168 name->set(#name); \
169 } \
170 virtual void onRun(Reporter* r) SK_OVERRIDE { \
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000171 name(r, fGrContextFactory); \
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +0000172 } \
173 }; \
174 static TestRegistry gReg_##name##Class(name##Class::Factory); \
175 } \
176 static void name(skiatest::Reporter* reporter, GrContextFactory* factory)
177
reed@android.comed673312009-02-27 16:24:51 +0000178#endif