blob: e3df2d71573d7d37c5dec1047e36282967139a74 [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"
reed@google.com789c6f22013-02-25 20:24:24 +000010#include "SkOSFile.h"
reed@android.comed673312009-02-27 16:24:51 +000011
robertphillips@google.combdb1be52012-09-07 18:24:43 +000012#if SK_SUPPORT_GPU
13#include "GrContext.h"
14#endif
15
reed@android.comed673312009-02-27 16:24:51 +000016using namespace skiatest;
17
reed@android.comd252db02009-04-01 18:31:44 +000018// need to explicitly declare this, or we get some weird infinite loop llist
19template TestRegistry* TestRegistry::gHead;
20
reed@android.comed673312009-02-27 16:24:51 +000021class Iter {
22public:
23 Iter(Reporter* r) : fReporter(r) {
24 r->ref();
25 fReg = TestRegistry::Head();
26 }
reed@android.com80e39a72009-04-02 16:59:40 +000027
reed@android.comed673312009-02-27 16:24:51 +000028 ~Iter() {
29 fReporter->unref();
30 }
reed@android.com80e39a72009-04-02 16:59:40 +000031
reed@android.comed673312009-02-27 16:24:51 +000032 Test* next() {
33 if (fReg) {
34 TestRegistry::Factory fact = fReg->factory();
35 fReg = fReg->next();
36 Test* test = fact(NULL);
37 test->setReporter(fReporter);
38 return test;
39 }
40 return NULL;
41 }
reed@android.com80e39a72009-04-02 16:59:40 +000042
reed@android.com57b799e2009-04-01 20:26:42 +000043 static int Count() {
44 const TestRegistry* reg = TestRegistry::Head();
45 int count = 0;
46 while (reg) {
47 count += 1;
48 reg = reg->next();
49 }
50 return count;
51 }
reed@android.com80e39a72009-04-02 16:59:40 +000052
reed@android.comed673312009-02-27 16:24:51 +000053private:
54 Reporter* fReporter;
55 const TestRegistry* fReg;
56};
57
58static const char* result2string(Reporter::Result result) {
59 return result == Reporter::kPassed ? "passed" : "FAILED";
60}
61
reed@android.comd252db02009-04-01 18:31:44 +000062class DebugfReporter : public Reporter {
reed@android.com57b799e2009-04-01 20:26:42 +000063public:
djsollen@google.comf4d1b392012-11-29 16:29:58 +000064 DebugfReporter() : fIndex(0), fTotal(0) {}
reed@android.comeeb3b7f2009-04-09 04:06:54 +000065
reed@android.com57b799e2009-04-01 20:26:42 +000066 void setIndexOfTotal(int index, int total) {
67 fIndex = index;
68 fTotal = total;
69 }
reed@android.comed673312009-02-27 16:24:51 +000070protected:
71 virtual void onStart(Test* test) {
djsollen@google.comf4d1b392012-11-29 16:29:58 +000072 SkDebugf("[%d/%d] %s...\n", fIndex+1, fTotal, test->getName());
reed@android.comed673312009-02-27 16:24:51 +000073 }
74 virtual void onReport(const char desc[], Reporter::Result result) {
djsollen@google.comf4d1b392012-11-29 16:29:58 +000075 SkDebugf("\t%s: %s\n", result2string(result), desc);
reed@android.comed673312009-02-27 16:24:51 +000076 }
reed@android.comeeb3b7f2009-04-09 04:06:54 +000077 virtual void onEnd(Test* test) {
djsollen@google.comf4d1b392012-11-29 16:29:58 +000078 if (!this->getCurrSuccess()) {
79 SkDebugf("---- FAILED\n");
reed@android.comeeb3b7f2009-04-09 04:06:54 +000080 }
81 }
djsollen@google.comf4d1b392012-11-29 16:29:58 +000082private:
reed@android.com57b799e2009-04-01 20:26:42 +000083 int fIndex, fTotal;
reed@android.comed673312009-02-27 16:24:51 +000084};
85
reed@google.com789c6f22013-02-25 20:24:24 +000086static const char* make_canonical_dir_path(const char* path, SkString* storage) {
87 if (path) {
88 // clean it up so it always has a trailing searator
89 size_t len = strlen(path);
90 if (0 == len) {
91 path = NULL;
92 } else if (SkPATH_SEPARATOR != path[len - 1]) {
93 // resize to len + 1, to make room for searator
94 storage->set(path, len + 1);
95 storage->writable_str()[len] = SkPATH_SEPARATOR;
96 path = storage->c_str();
97 }
98 }
99 return path;
100}
101
102static const char* gTmpDir;
103
104const char* Test::GetTmpDir() {
105 return gTmpDir;
106}
107
caryclark@google.com5987f582012-10-02 18:33:14 +0000108int tool_main(int argc, char** argv);
109int tool_main(int argc, char** argv) {
bsalomon@google.com4e230682013-01-15 20:37:04 +0000110#if SK_ENABLE_INST_COUNT
reed@google.coma2769752012-07-22 22:33:05 +0000111 gPrintInstCount = true;
112#endif
113 SkGraphics::Init();
bungeman@google.com5af16f82011-09-02 15:06:44 +0000114
bungeman@google.com5af16f82011-09-02 15:06:44 +0000115 const char* matchStr = NULL;
116
117 char* const* stop = argv + argc;
118 for (++argv; argv < stop; ++argv) {
djsollen@google.comf4d1b392012-11-29 16:29:58 +0000119 if (strcmp(*argv, "--match") == 0) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000120 ++argv;
121 if (argv < stop && **argv) {
122 matchStr = *argv;
reed@google.com789c6f22013-02-25 20:24:24 +0000123 } else {
124 SkDebugf("no following argument to --match\n");
125 return -1;
126 }
127 } else if (0 == strcmp(*argv, "--tmpDir")) {
128 ++argv;
129 if (argv < stop && **argv) {
130 gTmpDir = *argv;
131 } else {
132 SkDebugf("no following argument to --tmpDir\n");
133 return -1;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000134 }
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000135 }
136 }
reed@android.com5e5adfd2009-03-07 03:39:23 +0000137
reed@google.com789c6f22013-02-25 20:24:24 +0000138 SkString tmpDirStorage;
139 gTmpDir = make_canonical_dir_path(gTmpDir, &tmpDirStorage);
140
reed@google.com91d449e2011-10-26 15:25:18 +0000141 {
142 SkString header("Skia UnitTests:");
143 if (matchStr) {
144 header.appendf(" --match %s", matchStr);
145 }
reed@google.com789c6f22013-02-25 20:24:24 +0000146 if (gTmpDir) {
147 header.appendf(" --tmpDir %s", gTmpDir);
148 }
reed@google.com91d449e2011-10-26 15:25:18 +0000149#ifdef SK_DEBUG
150 header.append(" SK_DEBUG");
151#else
152 header.append(" SK_RELEASE");
153#endif
154#ifdef SK_SCALAR_IS_FIXED
155 header.append(" SK_SCALAR_IS_FIXED");
156#else
157 header.append(" SK_SCALAR_IS_FLOAT");
158#endif
djsollen@google.comf4d1b392012-11-29 16:29:58 +0000159 SkDebugf("%s\n", header.c_str());
reed@google.com91d449e2011-10-26 15:25:18 +0000160 }
161
djsollen@google.comf4d1b392012-11-29 16:29:58 +0000162 DebugfReporter reporter;
reed@android.comed673312009-02-27 16:24:51 +0000163 Iter iter(&reporter);
164 Test* test;
reed@android.com80e39a72009-04-02 16:59:40 +0000165
reed@android.com57b799e2009-04-01 20:26:42 +0000166 const int count = Iter::Count();
167 int index = 0;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000168 int failCount = 0;
169 int skipCount = 0;
reed@android.comed673312009-02-27 16:24:51 +0000170 while ((test = iter.next()) != NULL) {
reed@android.com57b799e2009-04-01 20:26:42 +0000171 reporter.setIndexOfTotal(index, count);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000172 if (NULL != matchStr && !strstr(test->getName(), matchStr)) {
173 ++skipCount;
174 } else {
175 if (!test->run()) {
176 ++failCount;
177 }
178 }
reed@android.comed673312009-02-27 16:24:51 +0000179 SkDELETE(test);
reed@android.com57b799e2009-04-01 20:26:42 +0000180 index += 1;
reed@android.comed673312009-02-27 16:24:51 +0000181 }
reed@android.com57b799e2009-04-01 20:26:42 +0000182
djsollen@google.comf4d1b392012-11-29 16:29:58 +0000183 SkDebugf("Finished %d tests, %d failures, %d skipped.\n",
184 count, failCount, skipCount);
reed@google.coma2769752012-07-22 22:33:05 +0000185
robertphillips@google.combdb1be52012-09-07 18:24:43 +0000186#if SK_SUPPORT_GPU
187
188#if GR_CACHE_STATS
189 GrContext *gr = GpuTest::GetContext();
190
191 gr->printCacheStats();
192#endif
193
194#endif
195
reed@google.coma2769752012-07-22 22:33:05 +0000196 SkGraphics::Term();
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000197 GpuTest::DestroyContexts();
reed@google.coma2769752012-07-22 22:33:05 +0000198
bungeman@google.com5af16f82011-09-02 15:06:44 +0000199 return (failCount == 0) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000200}
caryclark@google.com5987f582012-10-02 18:33:14 +0000201
borenet@google.com7158e6a2012-11-01 17:43:44 +0000202#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +0000203int main(int argc, char * const argv[]) {
204 return tool_main(argc, (char**) argv);
205}
206#endif