2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "CrashHandler.h"
9 #include "OverwriteLine.h"
10 #include "Resources.h"
11 #include "SkCommonFlags.h"
12 #include "SkGraphics.h"
15 #include "SkTaskGroup.h"
16 #include "SkTemplates.h"
21 #include "GrContext.h"
22 #include "GrContextFactory.h"
25 using namespace skiatest;
27 DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps.");
29 // need to explicitly declare this, or we get some weird infinite loop llist
30 template TestRegistry* TestRegistry::gHead;
34 Iter() { this->reset(); }
35 void reset() { fReg = TestRegistry::Head(); }
37 Test* next(Reporter* r) {
39 TestRegistry::Factory fact = fReg->factory();
41 Test* test = fact(NULL);
49 const TestRegistry* fReg;
52 class DebugfReporter : public Reporter {
54 explicit DebugfReporter(int total) : fDone(0), fTotal(total) {}
56 virtual bool allowExtendedTest() const SK_OVERRIDE { return FLAGS_extendedTest; }
57 virtual bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; }
60 virtual void onReportFailed(const SkString& desc) SK_OVERRIDE {
61 SkDebugf("\nFAILED: %s", desc.c_str());
64 virtual void onEnd(Test* test) SK_OVERRIDE {
65 const int done = 1 + sk_atomic_inc(&fDone);
67 if (!test->passed()) {
68 SkDebugf("\n---- %s FAILED", test->getName());
71 SkString prefix(kSkOverwriteLine);
75 time.printf("%5dms ", test->elapsedMs());
77 SkDebugf("%s[%3d/%3d] %s%s", prefix.c_str(), done, fTotal, time.c_str(), test->getName());
81 int32_t fDone; // atomic
85 // Deletes self when run.
86 class SkTestRunnable : public SkRunnable {
88 // Takes ownership of test.
89 SkTestRunnable(Test* test, int32_t* failCount) : fTest(test), fFailCount(failCount) {}
93 if(!fTest->passed()) {
94 sk_atomic_inc(fFailCount);
100 SkAutoTDelete<Test> fTest;
104 static bool should_run(const char* testName, bool isGPUTest) {
105 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, testName)) {
108 if (!FLAGS_cpu && !isGPUTest) {
111 if (!FLAGS_gpu && isGPUTest) {
121 #if SK_ENABLE_INST_COUNT
123 gPrintInstCount = true;
130 SkString header("Skia UnitTests:");
131 if (!FLAGS_match.isEmpty()) {
132 header.appendf(" --match");
133 for (int index = 0; index < FLAGS_match.count(); ++index) {
134 header.appendf(" %s", FLAGS_match[index]);
137 SkString tmpDir = Test::GetTmpDir();
138 if (!tmpDir.isEmpty()) {
139 header.appendf(" --tmpDir %s", tmpDir.c_str());
141 SkString resourcePath = GetResourcePath();
142 if (!resourcePath.isEmpty()) {
143 header.appendf(" --resourcePath %s", resourcePath.c_str());
146 header.append(" SK_DEBUG");
148 header.append(" SK_RELEASE");
150 header.appendf(" skia_arch_width=%d", (int)sizeof(void*) * 8);
151 if (FLAGS_veryVerbose) {
152 header.appendf("\n");
154 SkDebugf(header.c_str());
158 // Count tests first.
164 while ((test = iter.next(NULL/*reporter not needed*/)) != NULL) {
165 SkAutoTDelete<Test> owned(test);
166 if (should_run(test->getName(), test->isGPUTest())) {
174 int32_t failCount = 0;
177 SkTaskGroup::Enabler enabled(FLAGS_threads);
178 SkTaskGroup cpuTests;
179 SkTArray<Test*> gpuTests; // Always passes ownership to an SkTestRunnable
181 DebugfReporter reporter(toRun);
182 for (int i = 0; i < total; i++) {
183 SkAutoTDelete<Test> test(iter.next(&reporter));
184 if (!should_run(test->getName(), test->isGPUTest())) {
186 } else if (test->isGPUTest()) {
187 gpuTests.push_back() = test.detach();
189 cpuTests.add(SkNEW_ARGS(SkTestRunnable, (test.detach(), &failCount)));
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);
201 // Run GPU tests on this thread.
202 for (int i = 0; i < gpuTests.count(); i++) {
203 SkNEW_ARGS(SkTestRunnable, (gpuTests[i], &failCount))->run();
206 // Block until threaded tests finish.
210 SkDebugf("\nFinished %d tests, %d failures, %d skipped. (%d internal tests)",
211 toRun, failCount, skipCount, reporter.countTests());
216 return (failCount == 0) ? 0 : 1;
219 #if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
220 int main(int argc, char** argv) {
221 SkCommandLineFlags::Parse(argc, argv);