blob: f4b67c9bc40f4df6d3821096e5ee587b89b2e997 [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
mtklein30e6e2a2014-06-18 11:44:15 -07008#include "CrashHandler.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +00009#include "OverwriteLine.h"
tfarinabcbc1782014-06-18 14:32:48 -070010#include "Resources.h"
scroggo@google.com5a6324e2013-04-11 20:11:40 +000011#include "SkCommandLineFlags.h"
reed@android.com5e5adfd2009-03-07 03:39:23 +000012#include "SkGraphics.h"
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000013#include "SkOSFile.h"
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000014#include "SkTArray.h"
15#include "SkTemplates.h"
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000016#include "SkThreadPool.h"
17#include "SkTime.h"
reed@android.comed673312009-02-27 16:24:51 +000018#include "Test.h"
19
robertphillips@google.combdb1be52012-09-07 18:24:43 +000020#if SK_SUPPORT_GPU
21#include "GrContext.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000022#include "GrContextFactory.h"
robertphillips@google.combdb1be52012-09-07 18:24:43 +000023#endif
24
reed@android.comed673312009-02-27 16:24:51 +000025using namespace skiatest;
26
caryclark@google.comb631eec2013-05-02 13:14:40 +000027DEFINE_string2(match, m, NULL, "[~][^]substring[$] [...] of test name to run.\n" \
28 "Multiple matches may be separated by spaces.\n" \
29 "~ causes a matching test to always be skipped\n" \
30 "^ requires the start of the test to match\n" \
31 "$ requires the end of the test to match\n" \
32 "^ and $ requires an exact match\n" \
33 "If a test does not match any list entry,\n" \
34 "it is skipped unless some list entry starts with ~");
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +000035DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps.");
commit-bot@chromium.org6dda8272014-01-23 17:21:19 +000036DEFINE_bool2(leaks, l, false, "show leaked ref cnt'd objects.");
caryclark@google.com8d0a5242013-07-16 16:11:16 +000037DEFINE_bool2(single, z, false, "run tests on a single thread internally.");
commit-bot@chromium.org11ea96c2014-01-28 21:15:42 +000038DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver.");
39DEFINE_bool2(veryVerbose, V, false, "tell individual tests to be verbose.");
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +000040DEFINE_bool(cpu, true, "whether or not to run CPU tests.");
41DEFINE_bool(gpu, true, "whether or not to run GPU tests.");
commit-bot@chromium.org44c661f2013-04-22 15:23:14 +000042DEFINE_int32(threads, SkThreadPool::kThreadPerCore,
43 "Run threadsafe tests on a threadpool with this many threads.");
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000044
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000045// need to explicitly declare this, or we get some weird infinite loop llist
46template TestRegistry* TestRegistry::gHead;
47
48class Iter {
49public:
50 Iter() { this->reset(); }
51 void reset() { fReg = TestRegistry::Head(); }
52
53 Test* next(Reporter* r) {
54 if (fReg) {
55 TestRegistry::Factory fact = fReg->factory();
56 fReg = fReg->next();
57 Test* test = fact(NULL);
58 test->setReporter(r);
59 return test;
60 }
61 return NULL;
62 }
63
64private:
65 const TestRegistry* fReg;
66};
67
68class DebugfReporter : public Reporter {
69public:
70 explicit DebugfReporter(int total) : fDone(0), fTotal(total) {}
71
72 virtual bool allowExtendedTest() const SK_OVERRIDE { return FLAGS_extendedTest; }
73 virtual bool allowThreaded() const SK_OVERRIDE { return !FLAGS_single; }
commit-bot@chromium.org11ea96c2014-01-28 21:15:42 +000074 virtual bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; }
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000075
76protected:
77 virtual void onReportFailed(const SkString& desc) SK_OVERRIDE {
78 SkDebugf("\nFAILED: %s", desc.c_str());
79 }
80
81 virtual void onEnd(Test* test) SK_OVERRIDE {
82 const int done = 1 + sk_atomic_inc(&fDone);
83
84 if (!test->passed()) {
85 SkDebugf("\n---- %s FAILED", test->getName());
86 }
87
88 SkString prefix(kSkOverwriteLine);
89 SkString time;
90 if (FLAGS_verbose) {
91 prefix.printf("\n");
92 time.printf("%5dms ", test->elapsedMs());
93 }
94 SkDebugf("%s[%3d/%3d] %s%s", prefix.c_str(), done, fTotal, time.c_str(), test->getName());
95 }
96
97private:
98 int32_t fDone; // atomic
99 const int fTotal;
100};
101
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000102// Deletes self when run.
103class SkTestRunnable : public SkRunnable {
104public:
105 // Takes ownership of test.
106 SkTestRunnable(Test* test, int32_t* failCount) : fTest(test), fFailCount(failCount) {}
107
108 virtual void run() {
109 fTest->run();
110 if(!fTest->passed()) {
111 sk_atomic_inc(fFailCount);
112 }
113 SkDELETE(this);
114 }
115
116private:
117 SkAutoTDelete<Test> fTest;
118 int32_t* fFailCount;
119};
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000120
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000121static bool should_run(const char* testName, bool isGPUTest) {
122 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, testName)) {
123 return false;
124 }
125 if (!FLAGS_cpu && !isGPUTest) {
126 return false;
127 }
128 if (!FLAGS_gpu && isGPUTest) {
129 return false;
130 }
131 return true;
132}
133
caryclark@google.com5987f582012-10-02 18:33:14 +0000134int tool_main(int argc, char** argv);
135int tool_main(int argc, char** argv) {
mtklein30e6e2a2014-06-18 11:44:15 -0700136 SetupCrashHandler();
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000137 SkCommandLineFlags::SetUsage("");
138 SkCommandLineFlags::Parse(argc, argv);
139
bsalomon@google.com4e230682013-01-15 20:37:04 +0000140#if SK_ENABLE_INST_COUNT
commit-bot@chromium.org6dda8272014-01-23 17:21:19 +0000141 if (FLAGS_leaks) {
142 gPrintInstCount = true;
143 }
reed@google.coma2769752012-07-22 22:33:05 +0000144#endif
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000145
reed@google.coma2769752012-07-22 22:33:05 +0000146 SkGraphics::Init();
bungeman@google.com5af16f82011-09-02 15:06:44 +0000147
reed@google.com91d449e2011-10-26 15:25:18 +0000148 {
149 SkString header("Skia UnitTests:");
reed@google.com9aff1482013-04-11 18:27:52 +0000150 if (!FLAGS_match.isEmpty()) {
caryclark@google.comb631eec2013-05-02 13:14:40 +0000151 header.appendf(" --match");
152 for (int index = 0; index < FLAGS_match.count(); ++index) {
153 header.appendf(" %s", FLAGS_match[index]);
154 }
reed@google.com91d449e2011-10-26 15:25:18 +0000155 }
scroggo@google.comc76218d2013-06-06 14:59:56 +0000156 SkString tmpDir = Test::GetTmpDir();
157 if (!tmpDir.isEmpty()) {
158 header.appendf(" --tmpDir %s", tmpDir.c_str());
djsollen@google.comcb626502013-03-20 13:48:20 +0000159 }
tfarinabcbc1782014-06-18 14:32:48 -0700160 SkString resourcePath = GetResourcePath();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000161 if (!resourcePath.isEmpty()) {
162 header.appendf(" --resourcePath %s", resourcePath.c_str());
reed@google.com789c6f22013-02-25 20:24:24 +0000163 }
reed@google.com91d449e2011-10-26 15:25:18 +0000164#ifdef SK_DEBUG
165 header.append(" SK_DEBUG");
166#else
167 header.append(" SK_RELEASE");
168#endif
reed@google.com5696baa2013-08-29 20:20:39 +0000169 header.appendf(" skia_arch_width=%d", (int)sizeof(void*) * 8);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000170 if (FLAGS_veryVerbose) {
171 header.appendf("\n");
172 }
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000173 SkDebugf(header.c_str());
reed@google.com91d449e2011-10-26 15:25:18 +0000174 }
175
reed@android.com80e39a72009-04-02 16:59:40 +0000176
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000177 // Count tests first.
178 int total = 0;
179 int toRun = 0;
180 Test* test;
sglez@google.com586db932013-07-24 17:24:23 +0000181
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000182 Iter iter;
183 while ((test = iter.next(NULL/*reporter not needed*/)) != NULL) {
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000184 SkAutoTDelete<Test> owned(test);
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000185 if (should_run(test->getName(), test->isGPUTest())) {
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000186 toRun++;
187 }
188 total++;
189 }
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000190
191 // Now run them.
192 iter.reset();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000193 int32_t failCount = 0;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000194 int skipCount = 0;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000195
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +0000196 SkThreadPool threadpool(FLAGS_threads);
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000197 SkTArray<Test*> gpuTests; // Always passes ownership to an SkTestRunnable
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000198
199 DebugfReporter reporter(toRun);
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000200 for (int i = 0; i < total; i++) {
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000201 SkAutoTDelete<Test> test(iter.next(&reporter));
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000202 if (!should_run(test->getName(), test->isGPUTest())) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000203 ++skipCount;
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000204 } else if (test->isGPUTest()) {
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000205 gpuTests.push_back() = test.detach();
bungeman@google.com5af16f82011-09-02 15:06:44 +0000206 } else {
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +0000207 threadpool.add(SkNEW_ARGS(SkTestRunnable, (test.detach(), &failCount)));
bungeman@google.com5af16f82011-09-02 15:06:44 +0000208 }
reed@android.comed673312009-02-27 16:24:51 +0000209 }
reed@android.com57b799e2009-04-01 20:26:42 +0000210
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000211#if SK_SUPPORT_GPU
212 // Give GPU tests a context factory if that makes sense on this machine.
213 GrContextFactory grContextFactory;
214 for (int i = 0; i < gpuTests.count(); i++) {
215 gpuTests[i]->setGrContextFactory(&grContextFactory);
216 }
217#endif
218
219 // Run GPU tests on this thread.
220 for (int i = 0; i < gpuTests.count(); i++) {
221 SkNEW_ARGS(SkTestRunnable, (gpuTests[i], &failCount))->run();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000222 }
223
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +0000224 // Block until threaded tests finish.
225 threadpool.wait();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000226
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000227 if (FLAGS_verbose) {
228 SkDebugf("\nFinished %d tests, %d failures, %d skipped. (%d internal tests)",
229 toRun, failCount, skipCount, reporter.countTests());
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000230 }
reed@google.coma2769752012-07-22 22:33:05 +0000231 SkGraphics::Term();
232
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000233 SkDebugf("\n");
bungeman@google.com5af16f82011-09-02 15:06:44 +0000234 return (failCount == 0) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000235}
caryclark@google.com5987f582012-10-02 18:33:14 +0000236
borenet@google.com7158e6a2012-11-01 17:43:44 +0000237#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +0000238int main(int argc, char * const argv[]) {
239 return tool_main(argc, (char**) argv);
240}
241#endif