blob: 6c85b32bce6d95204b1ac8e66d57676f4941281c [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.com07e97fc2013-07-08 17:17:02 +000035 virtual bool verbose() const { return false; }
commit-bot@chromium.orgc7e08bd2013-04-23 11:16:32 +000036 virtual void bumpTestCount() { sk_atomic_inc(&fTestCount); }
37
reed@android.comed673312009-02-27 16:24:51 +000038 protected:
39 virtual void onStart(Test*) {}
commit-bot@chromium.org1f792862013-06-18 20:50:34 +000040 virtual void onReportFailed(const SkString& desc) {}
reed@android.comed673312009-02-27 16:24:51 +000041 virtual void onEnd(Test*) {}
reed@android.com80e39a72009-04-02 16:59:40 +000042
reed@android.comed673312009-02-27 16:24:51 +000043 private:
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000044 int32_t fTestCount;
reed@android.com80e39a72009-04-02 16:59:40 +000045
reed@android.comed673312009-02-27 16:24:51 +000046 typedef SkRefCnt INHERITED;
47 };
reed@android.com80e39a72009-04-02 16:59:40 +000048
reed@android.comed673312009-02-27 16:24:51 +000049 class Test {
reed@android.com80e39a72009-04-02 16:59:40 +000050 public:
reed@android.comed673312009-02-27 16:24:51 +000051 Test();
52 virtual ~Test();
53
54 Reporter* getReporter() const { return fReporter; }
55 void setReporter(Reporter*);
reed@android.com80e39a72009-04-02 16:59:40 +000056
reed@android.comed673312009-02-27 16:24:51 +000057 const char* getName();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000058 void run();
59 bool passed() const { return fPassed; }
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000060 SkMSec elapsedMs() const { return fElapsed; }
reed@android.com80e39a72009-04-02 16:59:40 +000061
scroggo@google.comc76218d2013-06-06 14:59:56 +000062 static SkString GetTmpDir();
djsollen@google.comcb626502013-03-20 13:48:20 +000063
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +000064 virtual bool isGPUTest() const { return false; }
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000065 virtual void setGrContextFactory(GrContextFactory* factory) {}
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000066
reed@android.comed673312009-02-27 16:24:51 +000067 protected:
68 virtual void onGetName(SkString*) = 0;
69 virtual void onRun(Reporter*) = 0;
reed@android.com80e39a72009-04-02 16:59:40 +000070
reed@android.comed673312009-02-27 16:24:51 +000071 private:
72 Reporter* fReporter;
73 SkString fName;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000074 bool fPassed;
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000075 SkMSec fElapsed;
reed@android.comed673312009-02-27 16:24:51 +000076 };
77
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000078 class GpuTest : public Test{
79 public:
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000080 GpuTest() : Test(), fGrContextFactory(NULL) {}
81
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +000082 virtual bool isGPUTest() const { return true; }
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000083 virtual void setGrContextFactory(GrContextFactory* factory) {
84 fGrContextFactory = factory;
85 }
86
87 protected:
88 GrContextFactory* fGrContextFactory; // Unowned.
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000089 };
90
mtklein@google.combd6343b2013-09-04 17:20:18 +000091 typedef SkTRegistry<Test*(*)(void*)> TestRegistry;
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +000092} // namespace skiatest
93
94/*
95 Use the following macros to make use of the skiatest classes, e.g.
96
97 #include "Test.h"
98
99 DEF_TEST(TestName, reporter) {
100 ...
101 REPORTER_ASSERT(reporter, x == 15);
102 ...
103 REPORTER_ASSERT_MESSAGE(reporter, x == 15, "x should be 15");
104 ...
105 if (x != 15) {
106 ERRORF(reporter, "x should be 15, but is %d", x);
107 return;
108 }
109 ...
110 }
111*/
reed@android.comed673312009-02-27 16:24:51 +0000112
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000113#define REPORTER_ASSERT(r, cond) \
114 do { \
115 if (!(cond)) { \
116 SkString desc; \
117 desc.printf("%s:%d\t%s", __FILE__, __LINE__, #cond); \
118 r->reportFailed(desc); \
119 } \
reed@android.comed673312009-02-27 16:24:51 +0000120 } while(0)
121
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000122#define REPORTER_ASSERT_MESSAGE(r, cond, message) \
123 do { \
124 if (!(cond)) { \
125 SkString desc; \
126 desc.printf("%s:%d\t%s: %s", __FILE__, __LINE__, \
127 message, #cond); \
128 r->reportFailed(desc); \
129 } \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000130 } while(0)
131
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000132#define ERRORF(reporter, ...) \
133 do { \
134 SkString desc; \
135 desc.printf("%s:%d\t", __FILE__, __LINE__); \
136 desc.appendf(__VA_ARGS__) ; \
137 (reporter)->reportFailed(desc); \
138 } while(0)
reed@android.comed673312009-02-27 16:24:51 +0000139
tfarina9ea53f92014-06-24 06:50:39 -0700140#define DEF_TEST(name, reporter) \
141 static void test_##name(skiatest::Reporter*); \
142 namespace skiatest { \
143 class name##Class : public Test { \
144 public: \
145 static Test* Factory(void*) { return SkNEW(name##Class); } \
146 protected: \
147 virtual void onGetName(SkString* name) SK_OVERRIDE { \
148 name->set(#name); \
149 } \
150 virtual void onRun(Reporter* r) SK_OVERRIDE { test_##name(r); } \
151 }; \
152 static TestRegistry gReg_##name##Class(name##Class::Factory); \
153 } \
154 static void test_##name(skiatest::Reporter* reporter)
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +0000155
tfarina9ea53f92014-06-24 06:50:39 -0700156#define DEF_GPUTEST(name, reporter, factory) \
157 static void test_##name(skiatest::Reporter*, GrContextFactory*); \
158 namespace skiatest { \
159 class name##Class : public GpuTest { \
160 public: \
161 static Test* Factory(void*) { return SkNEW(name##Class); } \
162 protected: \
163 virtual void onGetName(SkString* name) SK_OVERRIDE { \
164 name->set(#name); \
165 } \
166 virtual void onRun(Reporter* r) SK_OVERRIDE { \
167 test_##name(r, fGrContextFactory); \
168 } \
169 }; \
170 static TestRegistry gReg_##name##Class(name##Class::Factory); \
171 } \
172 static void test_##name(skiatest::Reporter* reporter, GrContextFactory* factory)
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +0000173
reed@android.comed673312009-02-27 16:24:51 +0000174#endif