blob: 0058215ccfcb7a181b678edefbcdfa4b2ad18d8e [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"
mtklein406654b2014-09-03 15:34:37 -070015#include "SkTaskGroup.h"
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000016#include "SkTemplates.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; }
commit-bot@chromium.org11ea96c2014-01-28 21:15:42 +000057 virtual bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; }
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000058
59protected:
60 virtual void onReportFailed(const SkString& desc) SK_OVERRIDE {
61 SkDebugf("\nFAILED: %s", desc.c_str());
62 }
63
64 virtual void onEnd(Test* test) SK_OVERRIDE {
65 const int done = 1 + sk_atomic_inc(&fDone);
66
67 if (!test->passed()) {
68 SkDebugf("\n---- %s FAILED", test->getName());
69 }
70
71 SkString prefix(kSkOverwriteLine);
72 SkString time;
73 if (FLAGS_verbose) {
74 prefix.printf("\n");
75 time.printf("%5dms ", test->elapsedMs());
76 }
77 SkDebugf("%s[%3d/%3d] %s%s", prefix.c_str(), done, fTotal, time.c_str(), test->getName());
78 }
79
80private:
81 int32_t fDone; // atomic
82 const int fTotal;
83};
84
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000085// Deletes self when run.
86class SkTestRunnable : public SkRunnable {
87public:
88 // Takes ownership of test.
89 SkTestRunnable(Test* test, int32_t* failCount) : fTest(test), fFailCount(failCount) {}
90
91 virtual void run() {
92 fTest->run();
93 if(!fTest->passed()) {
94 sk_atomic_inc(fFailCount);
95 }
96 SkDELETE(this);
97 }
98
99private:
100 SkAutoTDelete<Test> fTest;
101 int32_t* fFailCount;
102};
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000103
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000104static bool should_run(const char* testName, bool isGPUTest) {
105 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, testName)) {
106 return false;
107 }
108 if (!FLAGS_cpu && !isGPUTest) {
109 return false;
110 }
111 if (!FLAGS_gpu && isGPUTest) {
112 return false;
113 }
114 return true;
115}
116
caryclark17f0b6d2014-07-22 10:15:34 -0700117int test_main();
118int test_main() {
mtklein30e6e2a2014-06-18 11:44:15 -0700119 SetupCrashHandler();
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000120
bsalomon@google.com4e230682013-01-15 20:37:04 +0000121#if SK_ENABLE_INST_COUNT
commit-bot@chromium.org6dda8272014-01-23 17:21:19 +0000122 if (FLAGS_leaks) {
123 gPrintInstCount = true;
124 }
reed@google.coma2769752012-07-22 22:33:05 +0000125#endif
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000126
reed@google.coma2769752012-07-22 22:33:05 +0000127 SkGraphics::Init();
bungeman@google.com5af16f82011-09-02 15:06:44 +0000128
reed@google.com91d449e2011-10-26 15:25:18 +0000129 {
130 SkString header("Skia UnitTests:");
reed@google.com9aff1482013-04-11 18:27:52 +0000131 if (!FLAGS_match.isEmpty()) {
caryclark@google.comb631eec2013-05-02 13:14:40 +0000132 header.appendf(" --match");
133 for (int index = 0; index < FLAGS_match.count(); ++index) {
134 header.appendf(" %s", FLAGS_match[index]);
135 }
reed@google.com91d449e2011-10-26 15:25:18 +0000136 }
scroggo@google.comc76218d2013-06-06 14:59:56 +0000137 SkString tmpDir = Test::GetTmpDir();
138 if (!tmpDir.isEmpty()) {
139 header.appendf(" --tmpDir %s", tmpDir.c_str());
djsollen@google.comcb626502013-03-20 13:48:20 +0000140 }
tfarinabcbc1782014-06-18 14:32:48 -0700141 SkString resourcePath = GetResourcePath();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000142 if (!resourcePath.isEmpty()) {
143 header.appendf(" --resourcePath %s", resourcePath.c_str());
reed@google.com789c6f22013-02-25 20:24:24 +0000144 }
reed@google.com91d449e2011-10-26 15:25:18 +0000145#ifdef SK_DEBUG
146 header.append(" SK_DEBUG");
147#else
148 header.append(" SK_RELEASE");
149#endif
reed@google.com5696baa2013-08-29 20:20:39 +0000150 header.appendf(" skia_arch_width=%d", (int)sizeof(void*) * 8);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000151 if (FLAGS_veryVerbose) {
152 header.appendf("\n");
153 }
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000154 SkDebugf(header.c_str());
reed@google.com91d449e2011-10-26 15:25:18 +0000155 }
156
reed@android.com80e39a72009-04-02 16:59:40 +0000157
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000158 // Count tests first.
159 int total = 0;
160 int toRun = 0;
161 Test* test;
sglez@google.com586db932013-07-24 17:24:23 +0000162
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000163 Iter iter;
164 while ((test = iter.next(NULL/*reporter not needed*/)) != NULL) {
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000165 SkAutoTDelete<Test> owned(test);
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000166 if (should_run(test->getName(), test->isGPUTest())) {
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000167 toRun++;
168 }
169 total++;
170 }
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000171
172 // Now run them.
173 iter.reset();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000174 int32_t failCount = 0;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000175 int skipCount = 0;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000176
mtklein406654b2014-09-03 15:34:37 -0700177 SkTaskGroup::Enabler enabled(FLAGS_threads);
178 SkTaskGroup cpuTests;
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 {
mtklein406654b2014-09-03 15:34:37 -0700189 cpuTests.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.
mtklein406654b2014-09-03 15:34:37 -0700207 cpuTests.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