blob: 9a632172d204eb65d0ff7c67fd0f9328d2f1cd43 [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 }
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000077 virtual void onEnd(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
djsollen@google.comcb626502013-03-20 13:48:20 +0000102static SkString gTmpDir;
reed@google.com789c6f22013-02-25 20:24:24 +0000103
djsollen@google.comcb626502013-03-20 13:48:20 +0000104const SkString& Test::GetTmpDir() {
reed@google.com789c6f22013-02-25 20:24:24 +0000105 return gTmpDir;
106}
107
djsollen@google.comcb626502013-03-20 13:48:20 +0000108static SkString gResourcePath;
109
110const SkString& Test::GetResourcePath() {
111 return gResourcePath;
112}
113
caryclark@google.com5987f582012-10-02 18:33:14 +0000114int tool_main(int argc, char** argv);
115int tool_main(int argc, char** argv) {
bsalomon@google.com4e230682013-01-15 20:37:04 +0000116#if SK_ENABLE_INST_COUNT
reed@google.coma2769752012-07-22 22:33:05 +0000117 gPrintInstCount = true;
118#endif
119 SkGraphics::Init();
bungeman@google.com5af16f82011-09-02 15:06:44 +0000120
bungeman@google.com5af16f82011-09-02 15:06:44 +0000121 const char* matchStr = NULL;
122
123 char* const* stop = argv + argc;
124 for (++argv; argv < stop; ++argv) {
djsollen@google.comf4d1b392012-11-29 16:29:58 +0000125 if (strcmp(*argv, "--match") == 0) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000126 ++argv;
127 if (argv < stop && **argv) {
128 matchStr = *argv;
reed@google.com789c6f22013-02-25 20:24:24 +0000129 } else {
130 SkDebugf("no following argument to --match\n");
131 return -1;
132 }
133 } else if (0 == strcmp(*argv, "--tmpDir")) {
134 ++argv;
135 if (argv < stop && **argv) {
djsollen@google.comcb626502013-03-20 13:48:20 +0000136 make_canonical_dir_path(*argv, &gTmpDir);
reed@google.com789c6f22013-02-25 20:24:24 +0000137 } else {
138 SkDebugf("no following argument to --tmpDir\n");
139 return -1;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000140 }
djsollen@google.comcb626502013-03-20 13:48:20 +0000141 } else if ((0 == strcmp(*argv, "--resourcePath")) ||
142 (0 == strcmp(*argv, "-i"))) {
143 argv++;
144 if (argv < stop && **argv) {
145 make_canonical_dir_path(*argv, &gResourcePath);
146 }
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000147 }
148 }
reed@android.com5e5adfd2009-03-07 03:39:23 +0000149
reed@google.com91d449e2011-10-26 15:25:18 +0000150 {
151 SkString header("Skia UnitTests:");
152 if (matchStr) {
153 header.appendf(" --match %s", matchStr);
154 }
djsollen@google.comcb626502013-03-20 13:48:20 +0000155 if (!gTmpDir.isEmpty()) {
156 header.appendf(" --tmpDir %s", gTmpDir.c_str());
157 }
158 if (!gResourcePath.isEmpty()) {
159 header.appendf(" --resourcePath %s", gResourcePath.c_str());
reed@google.com789c6f22013-02-25 20:24:24 +0000160 }
reed@google.com91d449e2011-10-26 15:25:18 +0000161#ifdef SK_DEBUG
162 header.append(" SK_DEBUG");
163#else
164 header.append(" SK_RELEASE");
165#endif
166#ifdef SK_SCALAR_IS_FIXED
167 header.append(" SK_SCALAR_IS_FIXED");
168#else
169 header.append(" SK_SCALAR_IS_FLOAT");
170#endif
djsollen@google.comf4d1b392012-11-29 16:29:58 +0000171 SkDebugf("%s\n", header.c_str());
reed@google.com91d449e2011-10-26 15:25:18 +0000172 }
173
djsollen@google.comf4d1b392012-11-29 16:29:58 +0000174 DebugfReporter reporter;
reed@android.comed673312009-02-27 16:24:51 +0000175 Iter iter(&reporter);
176 Test* test;
reed@android.com80e39a72009-04-02 16:59:40 +0000177
reed@android.com57b799e2009-04-01 20:26:42 +0000178 const int count = Iter::Count();
179 int index = 0;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000180 int failCount = 0;
181 int skipCount = 0;
reed@android.comed673312009-02-27 16:24:51 +0000182 while ((test = iter.next()) != NULL) {
reed@android.com57b799e2009-04-01 20:26:42 +0000183 reporter.setIndexOfTotal(index, count);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000184 if (NULL != matchStr && !strstr(test->getName(), matchStr)) {
185 ++skipCount;
186 } else {
187 if (!test->run()) {
188 ++failCount;
189 }
190 }
reed@android.comed673312009-02-27 16:24:51 +0000191 SkDELETE(test);
reed@android.com57b799e2009-04-01 20:26:42 +0000192 index += 1;
reed@android.comed673312009-02-27 16:24:51 +0000193 }
reed@android.com57b799e2009-04-01 20:26:42 +0000194
djsollen@google.comf4d1b392012-11-29 16:29:58 +0000195 SkDebugf("Finished %d tests, %d failures, %d skipped.\n",
196 count, failCount, skipCount);
reed@google.coma2769752012-07-22 22:33:05 +0000197
robertphillips@google.combdb1be52012-09-07 18:24:43 +0000198#if SK_SUPPORT_GPU
199
200#if GR_CACHE_STATS
201 GrContext *gr = GpuTest::GetContext();
202
203 gr->printCacheStats();
204#endif
205
206#endif
207
reed@google.coma2769752012-07-22 22:33:05 +0000208 SkGraphics::Term();
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000209 GpuTest::DestroyContexts();
reed@google.coma2769752012-07-22 22:33:05 +0000210
bungeman@google.com5af16f82011-09-02 15:06:44 +0000211 return (failCount == 0) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000212}
caryclark@google.com5987f582012-10-02 18:33:14 +0000213
borenet@google.com7158e6a2012-11-01 17:43:44 +0000214#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +0000215int main(int argc, char * const argv[]) {
216 return tool_main(argc, (char**) argv);
217}
218#endif