blob: 5c80a01fc35cbaa548447bc292415c702a0eb9d7 [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
109int main (int argc, char * const argv[]) {
reed@google.coma2769752012-07-22 22:33:05 +0000110#ifdef SK_ENABLE_INST_COUNT
111 gPrintInstCount = true;
112#endif
113 SkGraphics::Init();
bungeman@google.com5af16f82011-09-02 15:06:44 +0000114
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000115 bool androidMode = false;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000116 const char* matchStr = NULL;
117
118 char* const* stop = argv + argc;
119 for (++argv; argv < stop; ++argv) {
120 if (strcmp(*argv, "-android") == 0) {
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000121 androidMode = true;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000122
bungeman@google.com5af16f82011-09-02 15:06:44 +0000123 } else if (strcmp(*argv, "--match") == 0) {
124 ++argv;
125 if (argv < stop && **argv) {
126 matchStr = *argv;
127 }
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000128 }
129 }
reed@android.com5e5adfd2009-03-07 03:39:23 +0000130
reed@google.com91d449e2011-10-26 15:25:18 +0000131 {
132 SkString header("Skia UnitTests:");
133 if (matchStr) {
134 header.appendf(" --match %s", matchStr);
135 }
136#ifdef SK_DEBUG
137 header.append(" SK_DEBUG");
138#else
139 header.append(" SK_RELEASE");
140#endif
141#ifdef SK_SCALAR_IS_FIXED
142 header.append(" SK_SCALAR_IS_FIXED");
143#else
144 header.append(" SK_SCALAR_IS_FLOAT");
145#endif
146 if (!androidMode) {
147 SkDebugf("%s\n", header.c_str());
148 }
149 }
150
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000151 DebugfReporter reporter(androidMode);
reed@android.comed673312009-02-27 16:24:51 +0000152 Iter iter(&reporter);
153 Test* test;
reed@android.com80e39a72009-04-02 16:59:40 +0000154
reed@android.com57b799e2009-04-01 20:26:42 +0000155 const int count = Iter::Count();
156 int index = 0;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000157 int failCount = 0;
158 int skipCount = 0;
reed@android.comed673312009-02-27 16:24:51 +0000159 while ((test = iter.next()) != NULL) {
reed@android.com57b799e2009-04-01 20:26:42 +0000160 reporter.setIndexOfTotal(index, count);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000161 if (NULL != matchStr && !strstr(test->getName(), matchStr)) {
162 ++skipCount;
163 } else {
164 if (!test->run()) {
165 ++failCount;
166 }
167 }
reed@android.comed673312009-02-27 16:24:51 +0000168 SkDELETE(test);
reed@android.com57b799e2009-04-01 20:26:42 +0000169 index += 1;
reed@android.comed673312009-02-27 16:24:51 +0000170 }
reed@android.com57b799e2009-04-01 20:26:42 +0000171
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000172 if (!androidMode) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000173 SkDebugf("Finished %d tests, %d failures, %d skipped.\n",
174 count, failCount, skipCount);
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000175 }
reed@google.coma2769752012-07-22 22:33:05 +0000176
robertphillips@google.combdb1be52012-09-07 18:24:43 +0000177#if SK_SUPPORT_GPU
178
179#if GR_CACHE_STATS
180 GrContext *gr = GpuTest::GetContext();
181
182 gr->printCacheStats();
183#endif
184
185#endif
186
reed@google.coma2769752012-07-22 22:33:05 +0000187 SkGraphics::Term();
188
bungeman@google.com5af16f82011-09-02 15:06:44 +0000189 return (failCount == 0) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000190}