blob: b7405907982f00deda9c7348e30e246f0d0175d6 [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
11using namespace skiatest;
12
reed@android.comd252db02009-04-01 18:31:44 +000013// need to explicitly declare this, or we get some weird infinite loop llist
14template TestRegistry* TestRegistry::gHead;
15
reed@android.comed673312009-02-27 16:24:51 +000016class Iter {
17public:
18 Iter(Reporter* r) : fReporter(r) {
19 r->ref();
20 fReg = TestRegistry::Head();
21 }
reed@android.com80e39a72009-04-02 16:59:40 +000022
reed@android.comed673312009-02-27 16:24:51 +000023 ~Iter() {
24 fReporter->unref();
25 }
reed@android.com80e39a72009-04-02 16:59:40 +000026
reed@android.comed673312009-02-27 16:24:51 +000027 Test* next() {
28 if (fReg) {
29 TestRegistry::Factory fact = fReg->factory();
30 fReg = fReg->next();
31 Test* test = fact(NULL);
32 test->setReporter(fReporter);
33 return test;
34 }
35 return NULL;
36 }
reed@android.com80e39a72009-04-02 16:59:40 +000037
reed@android.com57b799e2009-04-01 20:26:42 +000038 static int Count() {
39 const TestRegistry* reg = TestRegistry::Head();
40 int count = 0;
41 while (reg) {
42 count += 1;
43 reg = reg->next();
44 }
45 return count;
46 }
reed@android.com80e39a72009-04-02 16:59:40 +000047
reed@android.comed673312009-02-27 16:24:51 +000048private:
49 Reporter* fReporter;
50 const TestRegistry* fReg;
51};
52
53static const char* result2string(Reporter::Result result) {
54 return result == Reporter::kPassed ? "passed" : "FAILED";
55}
56
reed@android.comd252db02009-04-01 18:31:44 +000057class DebugfReporter : public Reporter {
reed@android.com57b799e2009-04-01 20:26:42 +000058public:
reed@android.comeeb3b7f2009-04-09 04:06:54 +000059 DebugfReporter(bool androidMode) : fAndroidMode(androidMode) {}
60
reed@android.com57b799e2009-04-01 20:26:42 +000061 void setIndexOfTotal(int index, int total) {
62 fIndex = index;
63 fTotal = total;
64 }
reed@android.comed673312009-02-27 16:24:51 +000065protected:
66 virtual void onStart(Test* test) {
reed@android.comeeb3b7f2009-04-09 04:06:54 +000067 this->dumpState(test, kStarting_State);
reed@android.comed673312009-02-27 16:24:51 +000068 }
69 virtual void onReport(const char desc[], Reporter::Result result) {
reed@android.comeeb3b7f2009-04-09 04:06:54 +000070 if (!fAndroidMode) {
71 SkDebugf("\t%s: %s\n", result2string(result), desc);
72 }
reed@android.comed673312009-02-27 16:24:51 +000073 }
reed@android.comeeb3b7f2009-04-09 04:06:54 +000074 virtual void onEnd(Test* test) {
75 this->dumpState(test, this->getCurrSuccess() ?
76 kSucceeded_State : kFailed_State);
77 }
reed@android.com57b799e2009-04-01 20:26:42 +000078private:
reed@android.comeeb3b7f2009-04-09 04:06:54 +000079 enum State {
80 kStarting_State = 1,
81 kSucceeded_State = 0,
82 kFailed_State = -2
83 };
84
85 void dumpState(Test* test, State state) {
86 if (fAndroidMode) {
87 SkDebugf("INSTRUMENTATION_STATUS: test=%s\n", test->getName());
88 SkDebugf("INSTRUMENTATION_STATUS: class=com.skia\n");
89 SkDebugf("INSTRUMENTATION_STATUS: current=%d\n", fIndex+1);
90 SkDebugf("INSTRUMENTATION_STATUS: numtests=%d\n", fTotal);
91 SkDebugf("INSTRUMENTATION_STATUS_CODE: %d\n", state);
92 } else {
93 if (kStarting_State == state) {
94 SkDebugf("[%d/%d] %s...\n", fIndex+1, fTotal, test->getName());
95 } else if (kFailed_State == state) {
96 SkDebugf("---- FAILED\n");
97 }
98 }
99 }
100
reed@android.com57b799e2009-04-01 20:26:42 +0000101 int fIndex, fTotal;
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000102 bool fAndroidMode;
reed@android.comed673312009-02-27 16:24:51 +0000103};
104
105int main (int argc, char * const argv[]) {
reed@android.com5e5adfd2009-03-07 03:39:23 +0000106 SkAutoGraphics ag;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000107
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000108 bool androidMode = false;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000109 const char* matchStr = NULL;
110
111 char* const* stop = argv + argc;
112 for (++argv; argv < stop; ++argv) {
113 if (strcmp(*argv, "-android") == 0) {
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000114 androidMode = true;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000115
116 } else if (strcmp(*argv, "--match") == 0) {
117 ++argv;
118 if (argv < stop && **argv) {
119 matchStr = *argv;
120 }
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000121 }
122 }
reed@android.com5e5adfd2009-03-07 03:39:23 +0000123
reed@google.com91d449e2011-10-26 15:25:18 +0000124 {
125 SkString header("Skia UnitTests:");
126 if (matchStr) {
127 header.appendf(" --match %s", matchStr);
128 }
129#ifdef SK_DEBUG
130 header.append(" SK_DEBUG");
131#else
132 header.append(" SK_RELEASE");
133#endif
134#ifdef SK_SCALAR_IS_FIXED
135 header.append(" SK_SCALAR_IS_FIXED");
136#else
137 header.append(" SK_SCALAR_IS_FLOAT");
138#endif
139 if (!androidMode) {
140 SkDebugf("%s\n", header.c_str());
141 }
142 }
143
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000144 DebugfReporter reporter(androidMode);
reed@android.comed673312009-02-27 16:24:51 +0000145 Iter iter(&reporter);
146 Test* test;
reed@android.com80e39a72009-04-02 16:59:40 +0000147
reed@android.com57b799e2009-04-01 20:26:42 +0000148 const int count = Iter::Count();
149 int index = 0;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000150 int failCount = 0;
151 int skipCount = 0;
reed@android.comed673312009-02-27 16:24:51 +0000152 while ((test = iter.next()) != NULL) {
reed@android.com57b799e2009-04-01 20:26:42 +0000153 reporter.setIndexOfTotal(index, count);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000154 if (NULL != matchStr && !strstr(test->getName(), matchStr)) {
155 ++skipCount;
156 } else {
157 if (!test->run()) {
158 ++failCount;
159 }
160 }
reed@android.comed673312009-02-27 16:24:51 +0000161 SkDELETE(test);
reed@android.com57b799e2009-04-01 20:26:42 +0000162 index += 1;
reed@android.comed673312009-02-27 16:24:51 +0000163 }
reed@android.com57b799e2009-04-01 20:26:42 +0000164
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000165 if (!androidMode) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000166 SkDebugf("Finished %d tests, %d failures, %d skipped.\n",
167 count, failCount, skipCount);
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000168 }
bungeman@google.com5af16f82011-09-02 15:06:44 +0000169 return (failCount == 0) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000170}