blob: 1b5584b6a6195fe3b52124c06796826bd6f6c8df [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"
reed89889b62014-10-29 12:36:45 -070014#include "SkRunnable.h"
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000015#include "SkTArray.h"
mtklein406654b2014-09-03 15:34:37 -070016#include "SkTaskGroup.h"
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000017#include "SkTemplates.h"
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000018#include "SkTime.h"
reed@android.comed673312009-02-27 16:24:51 +000019#include "Test.h"
20
robertphillips@google.combdb1be52012-09-07 18:24:43 +000021#if SK_SUPPORT_GPU
22#include "GrContext.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000023#include "GrContextFactory.h"
robertphillips@google.combdb1be52012-09-07 18:24:43 +000024#endif
25
reed@android.comed673312009-02-27 16:24:51 +000026using namespace skiatest;
27
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +000028DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps.");
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000029
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000030// need to explicitly declare this, or we get some weird infinite loop llist
31template TestRegistry* TestRegistry::gHead;
32
33class Iter {
34public:
35 Iter() { this->reset(); }
36 void reset() { fReg = TestRegistry::Head(); }
37
38 Test* next(Reporter* r) {
39 if (fReg) {
40 TestRegistry::Factory fact = fReg->factory();
41 fReg = fReg->next();
42 Test* test = fact(NULL);
43 test->setReporter(r);
44 return test;
45 }
46 return NULL;
47 }
48
49private:
50 const TestRegistry* fReg;
51};
52
53class DebugfReporter : public Reporter {
54public:
55 explicit DebugfReporter(int total) : fDone(0), fTotal(total) {}
56
57 virtual bool allowExtendedTest() const SK_OVERRIDE { return FLAGS_extendedTest; }
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:
scroggo0ee26272014-11-07 06:07:32 -080061 virtual void onReportFailed(const skiatest::Failure& failure) SK_OVERRIDE {
62 SkString desc;
63 failure.getFailureString(&desc);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000064 SkDebugf("\nFAILED: %s", desc.c_str());
65 }
66
67 virtual void onEnd(Test* test) SK_OVERRIDE {
68 const int done = 1 + sk_atomic_inc(&fDone);
69
70 if (!test->passed()) {
71 SkDebugf("\n---- %s FAILED", test->getName());
72 }
73
74 SkString prefix(kSkOverwriteLine);
75 SkString time;
76 if (FLAGS_verbose) {
77 prefix.printf("\n");
78 time.printf("%5dms ", test->elapsedMs());
79 }
80 SkDebugf("%s[%3d/%3d] %s%s", prefix.c_str(), done, fTotal, time.c_str(), test->getName());
81 }
82
83private:
84 int32_t fDone; // atomic
85 const int fTotal;
86};
87
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000088// Deletes self when run.
89class SkTestRunnable : public SkRunnable {
90public:
91 // Takes ownership of test.
92 SkTestRunnable(Test* test, int32_t* failCount) : fTest(test), fFailCount(failCount) {}
93
94 virtual void run() {
95 fTest->run();
96 if(!fTest->passed()) {
97 sk_atomic_inc(fFailCount);
98 }
99 SkDELETE(this);
100 }
101
102private:
103 SkAutoTDelete<Test> fTest;
104 int32_t* fFailCount;
105};
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000106
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000107static bool should_run(const char* testName, bool isGPUTest) {
108 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, testName)) {
109 return false;
110 }
111 if (!FLAGS_cpu && !isGPUTest) {
112 return false;
113 }
114 if (!FLAGS_gpu && isGPUTest) {
115 return false;
116 }
117 return true;
118}
119
caryclark17f0b6d2014-07-22 10:15:34 -0700120int test_main();
121int test_main() {
mtklein30e6e2a2014-06-18 11:44:15 -0700122 SetupCrashHandler();
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000123
bsalomon@google.com4e230682013-01-15 20:37:04 +0000124#if SK_ENABLE_INST_COUNT
commit-bot@chromium.org6dda8272014-01-23 17:21:19 +0000125 if (FLAGS_leaks) {
126 gPrintInstCount = true;
127 }
reed@google.coma2769752012-07-22 22:33:05 +0000128#endif
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000129
tfarinaa71d3af2014-11-07 06:12:30 -0800130 SkAutoGraphics ag;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000131
reed@google.com91d449e2011-10-26 15:25:18 +0000132 {
133 SkString header("Skia UnitTests:");
reed@google.com9aff1482013-04-11 18:27:52 +0000134 if (!FLAGS_match.isEmpty()) {
caryclark@google.comb631eec2013-05-02 13:14:40 +0000135 header.appendf(" --match");
136 for (int index = 0; index < FLAGS_match.count(); ++index) {
137 header.appendf(" %s", FLAGS_match[index]);
138 }
reed@google.com91d449e2011-10-26 15:25:18 +0000139 }
scroggo@google.comc76218d2013-06-06 14:59:56 +0000140 SkString tmpDir = Test::GetTmpDir();
141 if (!tmpDir.isEmpty()) {
142 header.appendf(" --tmpDir %s", tmpDir.c_str());
djsollen@google.comcb626502013-03-20 13:48:20 +0000143 }
tfarinabcbc1782014-06-18 14:32:48 -0700144 SkString resourcePath = GetResourcePath();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000145 if (!resourcePath.isEmpty()) {
146 header.appendf(" --resourcePath %s", resourcePath.c_str());
reed@google.com789c6f22013-02-25 20:24:24 +0000147 }
reed@google.com91d449e2011-10-26 15:25:18 +0000148#ifdef SK_DEBUG
149 header.append(" SK_DEBUG");
150#else
151 header.append(" SK_RELEASE");
152#endif
reed@google.com5696baa2013-08-29 20:20:39 +0000153 header.appendf(" skia_arch_width=%d", (int)sizeof(void*) * 8);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000154 if (FLAGS_veryVerbose) {
155 header.appendf("\n");
156 }
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000157 SkDebugf(header.c_str());
reed@google.com91d449e2011-10-26 15:25:18 +0000158 }
159
reed@android.com80e39a72009-04-02 16:59:40 +0000160
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000161 // Count tests first.
162 int total = 0;
163 int toRun = 0;
164 Test* test;
sglez@google.com586db932013-07-24 17:24:23 +0000165
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000166 Iter iter;
167 while ((test = iter.next(NULL/*reporter not needed*/)) != NULL) {
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000168 SkAutoTDelete<Test> owned(test);
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000169 if (should_run(test->getName(), test->isGPUTest())) {
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000170 toRun++;
171 }
172 total++;
173 }
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000174
175 // Now run them.
176 iter.reset();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000177 int32_t failCount = 0;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000178 int skipCount = 0;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000179
mtklein406654b2014-09-03 15:34:37 -0700180 SkTaskGroup::Enabler enabled(FLAGS_threads);
181 SkTaskGroup cpuTests;
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000182 SkTArray<Test*> gpuTests; // Always passes ownership to an SkTestRunnable
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000183
184 DebugfReporter reporter(toRun);
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000185 for (int i = 0; i < total; i++) {
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000186 SkAutoTDelete<Test> test(iter.next(&reporter));
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000187 if (!should_run(test->getName(), test->isGPUTest())) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000188 ++skipCount;
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000189 } else if (test->isGPUTest()) {
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000190 gpuTests.push_back() = test.detach();
bungeman@google.com5af16f82011-09-02 15:06:44 +0000191 } else {
mtklein406654b2014-09-03 15:34:37 -0700192 cpuTests.add(SkNEW_ARGS(SkTestRunnable, (test.detach(), &failCount)));
bungeman@google.com5af16f82011-09-02 15:06:44 +0000193 }
reed@android.comed673312009-02-27 16:24:51 +0000194 }
reed@android.com57b799e2009-04-01 20:26:42 +0000195
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000196#if SK_SUPPORT_GPU
197 // Give GPU tests a context factory if that makes sense on this machine.
198 GrContextFactory grContextFactory;
199 for (int i = 0; i < gpuTests.count(); i++) {
200 gpuTests[i]->setGrContextFactory(&grContextFactory);
201 }
202#endif
203
204 // Run GPU tests on this thread.
205 for (int i = 0; i < gpuTests.count(); i++) {
206 SkNEW_ARGS(SkTestRunnable, (gpuTests[i], &failCount))->run();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000207 }
208
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +0000209 // Block until threaded tests finish.
mtklein406654b2014-09-03 15:34:37 -0700210 cpuTests.wait();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000211
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000212 if (FLAGS_verbose) {
213 SkDebugf("\nFinished %d tests, %d failures, %d skipped. (%d internal tests)",
214 toRun, failCount, skipCount, reporter.countTests());
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000215 }
reed@google.coma2769752012-07-22 22:33:05 +0000216
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000217 SkDebugf("\n");
bungeman@google.com5af16f82011-09-02 15:06:44 +0000218 return (failCount == 0) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000219}
caryclark@google.com5987f582012-10-02 18:33:14 +0000220
borenet@google.com7158e6a2012-11-01 17:43:44 +0000221#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark17f0b6d2014-07-22 10:15:34 -0700222int main(int argc, char** argv) {
223 SkCommandLineFlags::Parse(argc, argv);
224 return test_main();
caryclark@google.com5987f582012-10-02 18:33:14 +0000225}
226#endif