blob: 90cd25998f04397258ac3da5602d7c1e9f4ec53e [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
scroggo0ee26272014-11-07 06:07:32 -080023 /**
24 * Information about a single failure from a Test.
25 *
26 * Not intended to be created/modified directly. To create one, use one of
27 *
28 * REPORTER_ASSERT
29 * REPORTER_ASSERT_MESSAGE
30 * ERRORF
31 *
32 * described in more detail further down in this file.
33 */
34 struct Failure {
35 const char* fileName;
36 int lineNo;
37 const char* condition;
38 SkString message;
39
40 // Helper to combine the failure info into one string.
41 void getFailureString(SkString* result) const {
42 if (!result) {
43 return;
44 }
45 result->printf("%s:%d\t", fileName, lineNo);
46 if (!message.isEmpty()) {
47 result->append(message);
48 if (strlen(condition) > 0) {
49 result->append(": ");
50 }
51 }
52 result->append(condition);
53 }
54 };
55
56
reed@android.comed673312009-02-27 16:24:51 +000057 class Reporter : public SkRefCnt {
58 public:
robertphillips@google.coma22e2112012-08-16 14:58:06 +000059 SK_DECLARE_INST_COUNT(Reporter)
reed@android.comed673312009-02-27 16:24:51 +000060 Reporter();
61
reed@android.comed673312009-02-27 16:24:51 +000062 int countTests() const { return fTestCount; }
reed@android.comed673312009-02-27 16:24:51 +000063
64 void startTest(Test*);
scroggo0ee26272014-11-07 06:07:32 -080065 void reportFailed(const Failure&);
reed@android.comed673312009-02-27 16:24:51 +000066 void endTest(Test*);
commit-bot@chromium.orgc7e08bd2013-04-23 11:16:32 +000067
caryclark@google.comd54e1e92013-04-10 15:57:31 +000068 virtual bool allowExtendedTest() const { return false; }
caryclark@google.com07e97fc2013-07-08 17:17:02 +000069 virtual bool verbose() const { return false; }
commit-bot@chromium.orgc7e08bd2013-04-23 11:16:32 +000070 virtual void bumpTestCount() { sk_atomic_inc(&fTestCount); }
71
reed@android.comed673312009-02-27 16:24:51 +000072 protected:
73 virtual void onStart(Test*) {}
scroggo0ee26272014-11-07 06:07:32 -080074 virtual void onReportFailed(const Failure&) {}
reed@android.comed673312009-02-27 16:24:51 +000075 virtual void onEnd(Test*) {}
reed@android.com80e39a72009-04-02 16:59:40 +000076
reed@android.comed673312009-02-27 16:24:51 +000077 private:
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000078 int32_t fTestCount;
reed@android.com80e39a72009-04-02 16:59:40 +000079
reed@android.comed673312009-02-27 16:24:51 +000080 typedef SkRefCnt INHERITED;
81 };
reed@android.com80e39a72009-04-02 16:59:40 +000082
reed@android.comed673312009-02-27 16:24:51 +000083 class Test {
reed@android.com80e39a72009-04-02 16:59:40 +000084 public:
reed@android.comed673312009-02-27 16:24:51 +000085 Test();
86 virtual ~Test();
87
88 Reporter* getReporter() const { return fReporter; }
89 void setReporter(Reporter*);
reed@android.com80e39a72009-04-02 16:59:40 +000090
reed@android.comed673312009-02-27 16:24:51 +000091 const char* getName();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000092 void run();
93 bool passed() const { return fPassed; }
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000094 SkMSec elapsedMs() const { return fElapsed; }
reed@android.com80e39a72009-04-02 16:59:40 +000095
scroggo@google.comc76218d2013-06-06 14:59:56 +000096 static SkString GetTmpDir();
djsollen@google.comcb626502013-03-20 13:48:20 +000097
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +000098 virtual bool isGPUTest() const { return false; }
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000099 virtual void setGrContextFactory(GrContextFactory* factory) {}
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000100
reed@android.comed673312009-02-27 16:24:51 +0000101 protected:
102 virtual void onGetName(SkString*) = 0;
103 virtual void onRun(Reporter*) = 0;
reed@android.com80e39a72009-04-02 16:59:40 +0000104
reed@android.comed673312009-02-27 16:24:51 +0000105 private:
106 Reporter* fReporter;
107 SkString fName;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000108 bool fPassed;
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000109 SkMSec fElapsed;
reed@android.comed673312009-02-27 16:24:51 +0000110 };
111
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000112 class GpuTest : public Test{
113 public:
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000114 GpuTest() : Test(), fGrContextFactory(NULL) {}
115
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000116 virtual bool isGPUTest() const { return true; }
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000117 virtual void setGrContextFactory(GrContextFactory* factory) {
118 fGrContextFactory = factory;
119 }
120
121 protected:
122 GrContextFactory* fGrContextFactory; // Unowned.
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000123 };
124
mtklein@google.combd6343b2013-09-04 17:20:18 +0000125 typedef SkTRegistry<Test*(*)(void*)> TestRegistry;
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +0000126} // namespace skiatest
127
128/*
129 Use the following macros to make use of the skiatest classes, e.g.
130
131 #include "Test.h"
132
133 DEF_TEST(TestName, reporter) {
134 ...
135 REPORTER_ASSERT(reporter, x == 15);
136 ...
137 REPORTER_ASSERT_MESSAGE(reporter, x == 15, "x should be 15");
138 ...
139 if (x != 15) {
140 ERRORF(reporter, "x should be 15, but is %d", x);
141 return;
142 }
143 ...
144 }
145*/
reed@android.comed673312009-02-27 16:24:51 +0000146
scroggo0ee26272014-11-07 06:07:32 -0800147#define REPORTER_ASSERT(r, cond) \
148 do { \
149 if (!(cond)) { \
150 skiatest::Failure failure = { __FILE__, __LINE__, \
151 #cond, SkString() }; \
152 r->reportFailed(failure); \
153 } \
reed@android.comed673312009-02-27 16:24:51 +0000154 } while(0)
155
scroggo0ee26272014-11-07 06:07:32 -0800156#define REPORTER_ASSERT_MESSAGE(r, cond, message) \
157 do { \
158 if (!(cond)) { \
159 skiatest::Failure failure = { __FILE__, __LINE__, \
160 #cond, SkString(message) }; \
161 r->reportFailed(failure); \
162 } \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000163 } while(0)
164
scroggo0ee26272014-11-07 06:07:32 -0800165#define ERRORF(r, ...) \
166 do { \
167 SkString desc; \
168 desc.appendf(__VA_ARGS__) ; \
169 skiatest::Failure failure = { __FILE__, __LINE__, \
170 "", SkString(desc) }; \
171 r->reportFailed(failure); \
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000172 } while(0)
reed@android.comed673312009-02-27 16:24:51 +0000173
tfarina9ea53f92014-06-24 06:50:39 -0700174#define DEF_TEST(name, reporter) \
175 static void test_##name(skiatest::Reporter*); \
176 namespace skiatest { \
177 class name##Class : public Test { \
178 public: \
179 static Test* Factory(void*) { return SkNEW(name##Class); } \
180 protected: \
181 virtual void onGetName(SkString* name) SK_OVERRIDE { \
182 name->set(#name); \
183 } \
184 virtual void onRun(Reporter* r) SK_OVERRIDE { test_##name(r); } \
185 }; \
186 static TestRegistry gReg_##name##Class(name##Class::Factory); \
187 } \
188 static void test_##name(skiatest::Reporter* reporter)
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +0000189
tfarina9ea53f92014-06-24 06:50:39 -0700190#define DEF_GPUTEST(name, reporter, factory) \
191 static void test_##name(skiatest::Reporter*, GrContextFactory*); \
192 namespace skiatest { \
193 class name##Class : public GpuTest { \
194 public: \
195 static Test* Factory(void*) { return SkNEW(name##Class); } \
196 protected: \
197 virtual void onGetName(SkString* name) SK_OVERRIDE { \
198 name->set(#name); \
199 } \
200 virtual void onRun(Reporter* r) SK_OVERRIDE { \
201 test_##name(r, fGrContextFactory); \
202 } \
203 }; \
204 static TestRegistry gReg_##name##Class(name##Class::Factory); \
205 } \
206 static void test_##name(skiatest::Reporter* reporter, GrContextFactory* factory)
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +0000207
reed@android.comed673312009-02-27 16:24:51 +0000208#endif