blob: 97ac29b0fd3cf03d935b595e7b4374290b51e770 [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"
caryclark17f0b6d2014-07-22 10:15:34 -070011#include "SkCommonFlags.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"
mtklein2460bbd2014-09-03 14:17:48 -070016#include "SkThreadPool.h"
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000017#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
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +000027DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps.");
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000028
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000029// need to explicitly declare this, or we get some weird infinite loop llist
30template TestRegistry* TestRegistry::gHead;
31
32class Iter {
33public:
34 Iter() { this->reset(); }
35 void reset() { fReg = TestRegistry::Head(); }
36
37 Test* next(Reporter* r) {
38 if (fReg) {
39 TestRegistry::Factory fact = fReg->factory();
40 fReg = fReg->next();
41 Test* test = fact(NULL);
42 test->setReporter(r);
43 return test;
44 }
45 return NULL;
46 }
47
48private:
49 const TestRegistry* fReg;
50};
51
52class DebugfReporter : public Reporter {
53public:
54 explicit DebugfReporter(int total) : fDone(0), fTotal(total) {}
55
56 virtual bool allowExtendedTest() const SK_OVERRIDE { return FLAGS_extendedTest; }
mtklein2460bbd2014-09-03 14:17:48 -070057 virtual bool allowThreaded() const SK_OVERRIDE { return !FLAGS_single; }
commit-bot@chromium.org11ea96c2014-01-28 21:15:42 +000058 virtual bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; }
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000059
60protected:
61 virtual void onReportFailed(const SkString& desc) SK_OVERRIDE {
62 SkDebugf("\nFAILED: %s", desc.c_str());
63 }
64
65 virtual void onEnd(Test* test) SK_OVERRIDE {
66 const int done = 1 + sk_atomic_inc(&fDone);
67
68 if (!test->passed()) {
69 SkDebugf("\n---- %s FAILED", test->getName());
70 }
71
72 SkString prefix(kSkOverwriteLine);
73 SkString time;
74 if (FLAGS_verbose) {
75 prefix.printf("\n");
76 time.printf("%5dms ", test->elapsedMs());
77 }
78 SkDebugf("%s[%3d/%3d] %s%s", prefix.c_str(), done, fTotal, time.c_str(), test->getName());
79 }
80
81private:
82 int32_t fDone; // atomic
83 const int fTotal;
84};
85
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000086// Deletes self when run.
87class SkTestRunnable : public SkRunnable {
88public:
89 // Takes ownership of test.
90 SkTestRunnable(Test* test, int32_t* failCount) : fTest(test), fFailCount(failCount) {}
91
92 virtual void run() {
93 fTest->run();
94 if(!fTest->passed()) {
95 sk_atomic_inc(fFailCount);
96 }
97 SkDELETE(this);
98 }
99
100private:
101 SkAutoTDelete<Test> fTest;
102 int32_t* fFailCount;
103};
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000104
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000105static bool should_run(const char* testName, bool isGPUTest) {
106 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, testName)) {
107 return false;
108 }
109 if (!FLAGS_cpu && !isGPUTest) {
110 return false;
111 }
112 if (!FLAGS_gpu && isGPUTest) {
113 return false;
114 }
115 return true;
116}
117
caryclark17f0b6d2014-07-22 10:15:34 -0700118int test_main();
119int test_main() {
mtklein30e6e2a2014-06-18 11:44:15 -0700120 SetupCrashHandler();
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000121
bsalomon@google.com4e230682013-01-15 20:37:04 +0000122#if SK_ENABLE_INST_COUNT
commit-bot@chromium.org6dda8272014-01-23 17:21:19 +0000123 if (FLAGS_leaks) {
124 gPrintInstCount = true;
125 }
reed@google.coma2769752012-07-22 22:33:05 +0000126#endif
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000127
reed@google.coma2769752012-07-22 22:33:05 +0000128 SkGraphics::Init();
bungeman@google.com5af16f82011-09-02 15:06:44 +0000129
reed@google.com91d449e2011-10-26 15:25:18 +0000130 {
131 SkString header("Skia UnitTests:");
reed@google.com9aff1482013-04-11 18:27:52 +0000132 if (!FLAGS_match.isEmpty()) {
caryclark@google.comb631eec2013-05-02 13:14:40 +0000133 header.appendf(" --match");
134 for (int index = 0; index < FLAGS_match.count(); ++index) {
135 header.appendf(" %s", FLAGS_match[index]);
136 }
reed@google.com91d449e2011-10-26 15:25:18 +0000137 }
scroggo@google.comc76218d2013-06-06 14:59:56 +0000138 SkString tmpDir = Test::GetTmpDir();
139 if (!tmpDir.isEmpty()) {
140 header.appendf(" --tmpDir %s", tmpDir.c_str());
djsollen@google.comcb626502013-03-20 13:48:20 +0000141 }
tfarinabcbc1782014-06-18 14:32:48 -0700142 SkString resourcePath = GetResourcePath();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000143 if (!resourcePath.isEmpty()) {
144 header.appendf(" --resourcePath %s", resourcePath.c_str());
reed@google.com789c6f22013-02-25 20:24:24 +0000145 }
reed@google.com91d449e2011-10-26 15:25:18 +0000146#ifdef SK_DEBUG
147 header.append(" SK_DEBUG");
148#else
149 header.append(" SK_RELEASE");
150#endif
reed@google.com5696baa2013-08-29 20:20:39 +0000151 header.appendf(" skia_arch_width=%d", (int)sizeof(void*) * 8);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000152 if (FLAGS_veryVerbose) {
153 header.appendf("\n");
154 }
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000155 SkDebugf(header.c_str());
reed@google.com91d449e2011-10-26 15:25:18 +0000156 }
157
reed@android.com80e39a72009-04-02 16:59:40 +0000158
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000159 // Count tests first.
160 int total = 0;
161 int toRun = 0;
162 Test* test;
sglez@google.com586db932013-07-24 17:24:23 +0000163
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000164 Iter iter;
165 while ((test = iter.next(NULL/*reporter not needed*/)) != NULL) {
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000166 SkAutoTDelete<Test> owned(test);
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000167 if (should_run(test->getName(), test->isGPUTest())) {
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000168 toRun++;
169 }
170 total++;
171 }
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000172
173 // Now run them.
174 iter.reset();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000175 int32_t failCount = 0;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000176 int skipCount = 0;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000177
mtklein2460bbd2014-09-03 14:17:48 -0700178 SkThreadPool threadpool(FLAGS_threads);
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000179 SkTArray<Test*> gpuTests; // Always passes ownership to an SkTestRunnable
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000180
181 DebugfReporter reporter(toRun);
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000182 for (int i = 0; i < total; i++) {
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000183 SkAutoTDelete<Test> test(iter.next(&reporter));
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000184 if (!should_run(test->getName(), test->isGPUTest())) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000185 ++skipCount;
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000186 } else if (test->isGPUTest()) {
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000187 gpuTests.push_back() = test.detach();
bungeman@google.com5af16f82011-09-02 15:06:44 +0000188 } else {
mtklein2460bbd2014-09-03 14:17:48 -0700189 threadpool.add(SkNEW_ARGS(SkTestRunnable, (test.detach(), &failCount)));
bungeman@google.com5af16f82011-09-02 15:06:44 +0000190 }
reed@android.comed673312009-02-27 16:24:51 +0000191 }
reed@android.com57b799e2009-04-01 20:26:42 +0000192
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000193#if SK_SUPPORT_GPU
194 // Give GPU tests a context factory if that makes sense on this machine.
195 GrContextFactory grContextFactory;
196 for (int i = 0; i < gpuTests.count(); i++) {
197 gpuTests[i]->setGrContextFactory(&grContextFactory);
198 }
199#endif
200
201 // Run GPU tests on this thread.
202 for (int i = 0; i < gpuTests.count(); i++) {
203 SkNEW_ARGS(SkTestRunnable, (gpuTests[i], &failCount))->run();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000204 }
205
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +0000206 // Block until threaded tests finish.
mtklein2460bbd2014-09-03 14:17:48 -0700207 threadpool.wait();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000208
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000209 if (FLAGS_verbose) {
210 SkDebugf("\nFinished %d tests, %d failures, %d skipped. (%d internal tests)",
211 toRun, failCount, skipCount, reporter.countTests());
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000212 }
reed@google.coma2769752012-07-22 22:33:05 +0000213 SkGraphics::Term();
214
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000215 SkDebugf("\n");
bungeman@google.com5af16f82011-09-02 15:06:44 +0000216 return (failCount == 0) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000217}
caryclark@google.com5987f582012-10-02 18:33:14 +0000218
borenet@google.com7158e6a2012-11-01 17:43:44 +0000219#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark17f0b6d2014-07-22 10:15:34 -0700220int main(int argc, char** argv) {
221 SkCommandLineFlags::Parse(argc, argv);
222 return test_main();
caryclark@google.com5987f582012-10-02 18:33:14 +0000223}
224#endif