blob: f884a6458b0a0b79a462a98e48932477ebb97000 [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.com5e5adfd2009-03-07 03:39:23 +00008#include "SkGraphics.h"
reed@android.comed673312009-02-27 16:24:51 +00009#include "Test.h"
10
robertphillips@google.combdb1be52012-09-07 18:24:43 +000011#if SK_SUPPORT_GPU
12#include "GrContext.h"
13#endif
14
reed@android.comed673312009-02-27 16:24:51 +000015using namespace skiatest;
16
reed@android.comd252db02009-04-01 18:31:44 +000017// need to explicitly declare this, or we get some weird infinite loop llist
18template TestRegistry* TestRegistry::gHead;
19
reed@android.comed673312009-02-27 16:24:51 +000020class Iter {
21public:
22 Iter(Reporter* r) : fReporter(r) {
23 r->ref();
24 fReg = TestRegistry::Head();
25 }
reed@android.com80e39a72009-04-02 16:59:40 +000026
reed@android.comed673312009-02-27 16:24:51 +000027 ~Iter() {
28 fReporter->unref();
29 }
reed@android.com80e39a72009-04-02 16:59:40 +000030
reed@android.comed673312009-02-27 16:24:51 +000031 Test* next() {
32 if (fReg) {
33 TestRegistry::Factory fact = fReg->factory();
34 fReg = fReg->next();
35 Test* test = fact(NULL);
36 test->setReporter(fReporter);
37 return test;
38 }
39 return NULL;
40 }
reed@android.com80e39a72009-04-02 16:59:40 +000041
reed@android.com57b799e2009-04-01 20:26:42 +000042 static int Count() {
43 const TestRegistry* reg = TestRegistry::Head();
44 int count = 0;
45 while (reg) {
46 count += 1;
47 reg = reg->next();
48 }
49 return count;
50 }
reed@android.com80e39a72009-04-02 16:59:40 +000051
reed@android.comed673312009-02-27 16:24:51 +000052private:
53 Reporter* fReporter;
54 const TestRegistry* fReg;
55};
56
57static const char* result2string(Reporter::Result result) {
58 return result == Reporter::kPassed ? "passed" : "FAILED";
59}
60
reed@android.comd252db02009-04-01 18:31:44 +000061class DebugfReporter : public Reporter {
reed@android.com57b799e2009-04-01 20:26:42 +000062public:
reed@android.comeeb3b7f2009-04-09 04:06:54 +000063 DebugfReporter(bool androidMode) : fAndroidMode(androidMode) {}
64
reed@android.com57b799e2009-04-01 20:26:42 +000065 void setIndexOfTotal(int index, int total) {
66 fIndex = index;
67 fTotal = total;
68 }
reed@android.comed673312009-02-27 16:24:51 +000069protected:
70 virtual void onStart(Test* test) {
reed@android.comeeb3b7f2009-04-09 04:06:54 +000071 this->dumpState(test, kStarting_State);
reed@android.comed673312009-02-27 16:24:51 +000072 }
73 virtual void onReport(const char desc[], Reporter::Result result) {
reed@android.comeeb3b7f2009-04-09 04:06:54 +000074 if (!fAndroidMode) {
75 SkDebugf("\t%s: %s\n", result2string(result), desc);
76 }
reed@android.comed673312009-02-27 16:24:51 +000077 }
reed@android.comeeb3b7f2009-04-09 04:06:54 +000078 virtual void onEnd(Test* test) {
79 this->dumpState(test, this->getCurrSuccess() ?
80 kSucceeded_State : kFailed_State);
81 }
reed@android.com57b799e2009-04-01 20:26:42 +000082private:
reed@android.comeeb3b7f2009-04-09 04:06:54 +000083 enum State {
84 kStarting_State = 1,
85 kSucceeded_State = 0,
86 kFailed_State = -2
87 };
88
89 void dumpState(Test* test, State state) {
90 if (fAndroidMode) {
91 SkDebugf("INSTRUMENTATION_STATUS: test=%s\n", test->getName());
92 SkDebugf("INSTRUMENTATION_STATUS: class=com.skia\n");
93 SkDebugf("INSTRUMENTATION_STATUS: current=%d\n", fIndex+1);
94 SkDebugf("INSTRUMENTATION_STATUS: numtests=%d\n", fTotal);
95 SkDebugf("INSTRUMENTATION_STATUS_CODE: %d\n", state);
96 } else {
97 if (kStarting_State == state) {
98 SkDebugf("[%d/%d] %s...\n", fIndex+1, fTotal, test->getName());
99 } else if (kFailed_State == state) {
100 SkDebugf("---- FAILED\n");
101 }
102 }
103 }
104
reed@android.com57b799e2009-04-01 20:26:42 +0000105 int fIndex, fTotal;
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000106 bool fAndroidMode;
reed@android.comed673312009-02-27 16:24:51 +0000107};
108
caryclark@google.com5987f582012-10-02 18:33:14 +0000109int tool_main(int argc, char** argv);
110int tool_main(int argc, char** argv) {
reed@google.coma2769752012-07-22 22:33:05 +0000111#ifdef SK_ENABLE_INST_COUNT
112 gPrintInstCount = true;
113#endif
114 SkGraphics::Init();
bungeman@google.com5af16f82011-09-02 15:06:44 +0000115
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000116 bool androidMode = false;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000117 const char* matchStr = NULL;
118
119 char* const* stop = argv + argc;
120 for (++argv; argv < stop; ++argv) {
121 if (strcmp(*argv, "-android") == 0) {
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000122 androidMode = true;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000123
bungeman@google.com5af16f82011-09-02 15:06:44 +0000124 } else if (strcmp(*argv, "--match") == 0) {
125 ++argv;
126 if (argv < stop && **argv) {
127 matchStr = *argv;
128 }
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000129 }
130 }
reed@android.com5e5adfd2009-03-07 03:39:23 +0000131
reed@google.com91d449e2011-10-26 15:25:18 +0000132 {
133 SkString header("Skia UnitTests:");
134 if (matchStr) {
135 header.appendf(" --match %s", matchStr);
136 }
137#ifdef SK_DEBUG
138 header.append(" SK_DEBUG");
139#else
140 header.append(" SK_RELEASE");
141#endif
142#ifdef SK_SCALAR_IS_FIXED
143 header.append(" SK_SCALAR_IS_FIXED");
144#else
145 header.append(" SK_SCALAR_IS_FLOAT");
146#endif
147 if (!androidMode) {
148 SkDebugf("%s\n", header.c_str());
149 }
150 }
151
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000152 DebugfReporter reporter(androidMode);
reed@android.comed673312009-02-27 16:24:51 +0000153 Iter iter(&reporter);
154 Test* test;
reed@android.com80e39a72009-04-02 16:59:40 +0000155
reed@android.com57b799e2009-04-01 20:26:42 +0000156 const int count = Iter::Count();
157 int index = 0;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000158 int failCount = 0;
159 int skipCount = 0;
reed@android.comed673312009-02-27 16:24:51 +0000160 while ((test = iter.next()) != NULL) {
reed@android.com57b799e2009-04-01 20:26:42 +0000161 reporter.setIndexOfTotal(index, count);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000162 if (NULL != matchStr && !strstr(test->getName(), matchStr)) {
163 ++skipCount;
164 } else {
165 if (!test->run()) {
166 ++failCount;
167 }
168 }
reed@android.comed673312009-02-27 16:24:51 +0000169 SkDELETE(test);
reed@android.com57b799e2009-04-01 20:26:42 +0000170 index += 1;
reed@android.comed673312009-02-27 16:24:51 +0000171 }
reed@android.com57b799e2009-04-01 20:26:42 +0000172
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000173 if (!androidMode) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000174 SkDebugf("Finished %d tests, %d failures, %d skipped.\n",
175 count, failCount, skipCount);
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000176 }
reed@google.coma2769752012-07-22 22:33:05 +0000177
robertphillips@google.combdb1be52012-09-07 18:24:43 +0000178#if SK_SUPPORT_GPU
179
180#if GR_CACHE_STATS
181 GrContext *gr = GpuTest::GetContext();
182
183 gr->printCacheStats();
184#endif
185
186#endif
187
reed@google.coma2769752012-07-22 22:33:05 +0000188 SkGraphics::Term();
189
bungeman@google.com5af16f82011-09-02 15:06:44 +0000190 return (failCount == 0) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000191}
caryclark@google.com5987f582012-10-02 18:33:14 +0000192
193#if !defined SK_BUILD_FOR_IOS
194int main(int argc, char * const argv[]) {
195 return tool_main(argc, (char**) argv);
196}
197#endif
198