blob: 26f8a3a8969b8aca1c3cc59a4f32b94135924d6b [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +00007
8#include "SkCommandLineFlags.h"
reed@android.com5e5adfd2009-03-07 03:39:23 +00009#include "SkGraphics.h"
reed@android.comed673312009-02-27 16:24:51 +000010#include "Test.h"
reed@google.com789c6f22013-02-25 20:24:24 +000011#include "SkOSFile.h"
reed@android.comed673312009-02-27 16:24:51 +000012
robertphillips@google.combdb1be52012-09-07 18:24:43 +000013#if SK_SUPPORT_GPU
14#include "GrContext.h"
15#endif
16
reed@android.comed673312009-02-27 16:24:51 +000017using namespace skiatest;
18
reed@android.comd252db02009-04-01 18:31:44 +000019// need to explicitly declare this, or we get some weird infinite loop llist
20template TestRegistry* TestRegistry::gHead;
21
reed@android.comed673312009-02-27 16:24:51 +000022class Iter {
23public:
24 Iter(Reporter* r) : fReporter(r) {
25 r->ref();
26 fReg = TestRegistry::Head();
27 }
reed@android.com80e39a72009-04-02 16:59:40 +000028
reed@android.comed673312009-02-27 16:24:51 +000029 ~Iter() {
30 fReporter->unref();
31 }
reed@android.com80e39a72009-04-02 16:59:40 +000032
reed@android.comed673312009-02-27 16:24:51 +000033 Test* next() {
34 if (fReg) {
35 TestRegistry::Factory fact = fReg->factory();
36 fReg = fReg->next();
37 Test* test = fact(NULL);
38 test->setReporter(fReporter);
39 return test;
40 }
41 return NULL;
42 }
reed@android.com80e39a72009-04-02 16:59:40 +000043
reed@android.com57b799e2009-04-01 20:26:42 +000044 static int Count() {
45 const TestRegistry* reg = TestRegistry::Head();
46 int count = 0;
47 while (reg) {
48 count += 1;
49 reg = reg->next();
50 }
51 return count;
52 }
reed@android.com80e39a72009-04-02 16:59:40 +000053
reed@android.comed673312009-02-27 16:24:51 +000054private:
55 Reporter* fReporter;
56 const TestRegistry* fReg;
57};
58
59static const char* result2string(Reporter::Result result) {
60 return result == Reporter::kPassed ? "passed" : "FAILED";
61}
62
reed@android.comd252db02009-04-01 18:31:44 +000063class DebugfReporter : public Reporter {
reed@android.com57b799e2009-04-01 20:26:42 +000064public:
caryclark@google.comd54e1e92013-04-10 15:57:31 +000065 DebugfReporter(bool allowExtendedTest)
66 : fIndex(0)
67 , fTotal(0)
68 , fAllowExtendedTest(allowExtendedTest) {
69 }
reed@android.comeeb3b7f2009-04-09 04:06:54 +000070
reed@android.com57b799e2009-04-01 20:26:42 +000071 void setIndexOfTotal(int index, int total) {
72 fIndex = index;
73 fTotal = total;
74 }
caryclark@google.comd54e1e92013-04-10 15:57:31 +000075
76 virtual bool allowExtendedTest() const {
skia.committer@gmail.com391ca662013-04-11 07:01:45 +000077 return fAllowExtendedTest;
caryclark@google.comd54e1e92013-04-10 15:57:31 +000078 }
79
reed@android.comed673312009-02-27 16:24:51 +000080protected:
81 virtual void onStart(Test* test) {
djsollen@google.comf4d1b392012-11-29 16:29:58 +000082 SkDebugf("[%d/%d] %s...\n", fIndex+1, fTotal, test->getName());
reed@android.comed673312009-02-27 16:24:51 +000083 }
84 virtual void onReport(const char desc[], Reporter::Result result) {
djsollen@google.comf4d1b392012-11-29 16:29:58 +000085 SkDebugf("\t%s: %s\n", result2string(result), desc);
reed@android.comed673312009-02-27 16:24:51 +000086 }
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000087 virtual void onEnd(Test*) {
djsollen@google.comf4d1b392012-11-29 16:29:58 +000088 if (!this->getCurrSuccess()) {
89 SkDebugf("---- FAILED\n");
reed@android.comeeb3b7f2009-04-09 04:06:54 +000090 }
91 }
djsollen@google.comf4d1b392012-11-29 16:29:58 +000092private:
reed@android.com57b799e2009-04-01 20:26:42 +000093 int fIndex, fTotal;
caryclark@google.comd54e1e92013-04-10 15:57:31 +000094 bool fAllowExtendedTest;
reed@android.comed673312009-02-27 16:24:51 +000095};
96
reed@google.com789c6f22013-02-25 20:24:24 +000097static const char* make_canonical_dir_path(const char* path, SkString* storage) {
98 if (path) {
99 // clean it up so it always has a trailing searator
100 size_t len = strlen(path);
101 if (0 == len) {
102 path = NULL;
103 } else if (SkPATH_SEPARATOR != path[len - 1]) {
104 // resize to len + 1, to make room for searator
105 storage->set(path, len + 1);
106 storage->writable_str()[len] = SkPATH_SEPARATOR;
107 path = storage->c_str();
108 }
109 }
110 return path;
111}
112
djsollen@google.comcb626502013-03-20 13:48:20 +0000113static SkString gTmpDir;
reed@google.com789c6f22013-02-25 20:24:24 +0000114
djsollen@google.comcb626502013-03-20 13:48:20 +0000115const SkString& Test::GetTmpDir() {
reed@google.com789c6f22013-02-25 20:24:24 +0000116 return gTmpDir;
117}
118
djsollen@google.comcb626502013-03-20 13:48:20 +0000119static SkString gResourcePath;
120
121const SkString& Test::GetResourcePath() {
122 return gResourcePath;
123}
124
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000125DEFINE_string2(matchStr, m, NULL, "substring of test name to run.");
126DEFINE_string2(tmpDir, t, NULL, "tmp directory for tests to use.");
127DEFINE_string2(resourcePath, i, NULL, "directory for test resources.");
128DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps.");
129DEFINE_bool2(verbose, v, false, "enable verbose output.");
130
caryclark@google.com5987f582012-10-02 18:33:14 +0000131int tool_main(int argc, char** argv);
132int tool_main(int argc, char** argv) {
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000133 SkCommandLineFlags::SetUsage("");
134 SkCommandLineFlags::Parse(argc, argv);
135
136 if (!FLAGS_tmpDir.isEmpty()) {
137 make_canonical_dir_path(FLAGS_tmpDir[0], &gTmpDir);
138 }
139 if (!FLAGS_resourcePath.isEmpty()) {
140 make_canonical_dir_path(FLAGS_resourcePath[0], &gResourcePath);
141 }
142
bsalomon@google.com4e230682013-01-15 20:37:04 +0000143#if SK_ENABLE_INST_COUNT
reed@google.coma2769752012-07-22 22:33:05 +0000144 gPrintInstCount = true;
145#endif
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000146
reed@google.coma2769752012-07-22 22:33:05 +0000147 SkGraphics::Init();
bungeman@google.com5af16f82011-09-02 15:06:44 +0000148
reed@google.com91d449e2011-10-26 15:25:18 +0000149 {
150 SkString header("Skia UnitTests:");
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000151 if (!FLAGS_matchStr.isEmpty()) {
152 header.appendf(" --match %s", FLAGS_matchStr[0]);
reed@google.com91d449e2011-10-26 15:25:18 +0000153 }
djsollen@google.comcb626502013-03-20 13:48:20 +0000154 if (!gTmpDir.isEmpty()) {
155 header.appendf(" --tmpDir %s", gTmpDir.c_str());
156 }
157 if (!gResourcePath.isEmpty()) {
158 header.appendf(" --resourcePath %s", gResourcePath.c_str());
reed@google.com789c6f22013-02-25 20:24:24 +0000159 }
reed@google.com91d449e2011-10-26 15:25:18 +0000160#ifdef SK_DEBUG
161 header.append(" SK_DEBUG");
162#else
163 header.append(" SK_RELEASE");
164#endif
165#ifdef SK_SCALAR_IS_FIXED
166 header.append(" SK_SCALAR_IS_FIXED");
167#else
168 header.append(" SK_SCALAR_IS_FLOAT");
169#endif
djsollen@google.comf4d1b392012-11-29 16:29:58 +0000170 SkDebugf("%s\n", header.c_str());
reed@google.com91d449e2011-10-26 15:25:18 +0000171 }
172
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000173 DebugfReporter reporter(FLAGS_extendedTest);
reed@android.comed673312009-02-27 16:24:51 +0000174 Iter iter(&reporter);
175 Test* test;
reed@android.com80e39a72009-04-02 16:59:40 +0000176
reed@android.com57b799e2009-04-01 20:26:42 +0000177 const int count = Iter::Count();
178 int index = 0;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000179 int failCount = 0;
180 int skipCount = 0;
reed@android.comed673312009-02-27 16:24:51 +0000181 while ((test = iter.next()) != NULL) {
reed@android.com57b799e2009-04-01 20:26:42 +0000182 reporter.setIndexOfTotal(index, count);
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000183 if (!FLAGS_matchStr.isEmpty() && !strstr(test->getName(), FLAGS_matchStr[0])) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000184 ++skipCount;
185 } else {
186 if (!test->run()) {
187 ++failCount;
188 }
189 }
reed@android.comed673312009-02-27 16:24:51 +0000190 SkDELETE(test);
reed@android.com57b799e2009-04-01 20:26:42 +0000191 index += 1;
reed@android.comed673312009-02-27 16:24:51 +0000192 }
reed@android.com57b799e2009-04-01 20:26:42 +0000193
djsollen@google.comf4d1b392012-11-29 16:29:58 +0000194 SkDebugf("Finished %d tests, %d failures, %d skipped.\n",
195 count, failCount, skipCount);
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000196 int testCount = reporter.countTests();
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000197 if (FLAGS_verbose && testCount > 0) {
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000198 SkDebugf("Ran %d Internal tests.\n", testCount);
199 }
robertphillips@google.combdb1be52012-09-07 18:24:43 +0000200#if SK_SUPPORT_GPU
201
202#if GR_CACHE_STATS
203 GrContext *gr = GpuTest::GetContext();
204
205 gr->printCacheStats();
206#endif
207
208#endif
209
reed@google.coma2769752012-07-22 22:33:05 +0000210 SkGraphics::Term();
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000211 GpuTest::DestroyContexts();
reed@google.coma2769752012-07-22 22:33:05 +0000212
bungeman@google.com5af16f82011-09-02 15:06:44 +0000213 return (failCount == 0) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000214}
caryclark@google.com5987f582012-10-02 18:33:14 +0000215
borenet@google.com7158e6a2012-11-01 17:43:44 +0000216#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +0000217int main(int argc, char * const argv[]) {
218 return tool_main(argc, (char**) argv);
219}
220#endif