epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 7 | |
mtklein | 30e6e2a | 2014-06-18 11:44:15 -0700 | [diff] [blame] | 8 | #include "CrashHandler.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 9 | #include "OverwriteLine.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 10 | #include "Resources.h" |
caryclark | 17f0b6d | 2014-07-22 10:15:34 -0700 | [diff] [blame] | 11 | #include "SkCommonFlags.h" |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 12 | #include "SkGraphics.h" |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 13 | #include "SkOSFile.h" |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 14 | #include "SkTArray.h" |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 15 | #include "SkTaskGroup.h" |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 16 | #include "SkTemplates.h" |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 17 | #include "SkTime.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 18 | #include "Test.h" |
| 19 | |
robertphillips@google.com | bdb1be5 | 2012-09-07 18:24:43 +0000 | [diff] [blame] | 20 | #if SK_SUPPORT_GPU |
| 21 | #include "GrContext.h" |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 22 | #include "GrContextFactory.h" |
robertphillips@google.com | bdb1be5 | 2012-09-07 18:24:43 +0000 | [diff] [blame] | 23 | #endif |
| 24 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 25 | using namespace skiatest; |
| 26 | |
commit-bot@chromium.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 27 | DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps."); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 28 | |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 29 | // need to explicitly declare this, or we get some weird infinite loop llist |
| 30 | template TestRegistry* TestRegistry::gHead; |
| 31 | |
| 32 | class Iter { |
| 33 | public: |
| 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 | |
| 48 | private: |
| 49 | const TestRegistry* fReg; |
| 50 | }; |
| 51 | |
| 52 | class DebugfReporter : public Reporter { |
| 53 | public: |
| 54 | explicit DebugfReporter(int total) : fDone(0), fTotal(total) {} |
| 55 | |
| 56 | virtual bool allowExtendedTest() const SK_OVERRIDE { return FLAGS_extendedTest; } |
commit-bot@chromium.org | 11ea96c | 2014-01-28 21:15:42 +0000 | [diff] [blame] | 57 | virtual bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; } |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 58 | |
| 59 | protected: |
| 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 | |
| 80 | private: |
| 81 | int32_t fDone; // atomic |
| 82 | const int fTotal; |
| 83 | }; |
| 84 | |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 85 | // Deletes self when run. |
| 86 | class SkTestRunnable : public SkRunnable { |
| 87 | public: |
| 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 | |
| 99 | private: |
| 100 | SkAutoTDelete<Test> fTest; |
| 101 | int32_t* fFailCount; |
| 102 | }; |
commit-bot@chromium.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 103 | |
commit-bot@chromium.org | 5a47b09 | 2014-01-30 15:30:50 +0000 | [diff] [blame] | 104 | static 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 | |
caryclark | 17f0b6d | 2014-07-22 10:15:34 -0700 | [diff] [blame] | 117 | int test_main(); |
| 118 | int test_main() { |
mtklein | 30e6e2a | 2014-06-18 11:44:15 -0700 | [diff] [blame] | 119 | SetupCrashHandler(); |
commit-bot@chromium.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 120 | |
bsalomon@google.com | 4e23068 | 2013-01-15 20:37:04 +0000 | [diff] [blame] | 121 | #if SK_ENABLE_INST_COUNT |
commit-bot@chromium.org | 6dda827 | 2014-01-23 17:21:19 +0000 | [diff] [blame] | 122 | if (FLAGS_leaks) { |
| 123 | gPrintInstCount = true; |
| 124 | } |
reed@google.com | a276975 | 2012-07-22 22:33:05 +0000 | [diff] [blame] | 125 | #endif |
caryclark@google.com | d54e1e9 | 2013-04-10 15:57:31 +0000 | [diff] [blame] | 126 | |
reed@google.com | a276975 | 2012-07-22 22:33:05 +0000 | [diff] [blame] | 127 | SkGraphics::Init(); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 128 | |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 129 | { |
| 130 | SkString header("Skia UnitTests:"); |
reed@google.com | 9aff148 | 2013-04-11 18:27:52 +0000 | [diff] [blame] | 131 | if (!FLAGS_match.isEmpty()) { |
caryclark@google.com | b631eec | 2013-05-02 13:14:40 +0000 | [diff] [blame] | 132 | header.appendf(" --match"); |
| 133 | for (int index = 0; index < FLAGS_match.count(); ++index) { |
| 134 | header.appendf(" %s", FLAGS_match[index]); |
| 135 | } |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 136 | } |
scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 137 | SkString tmpDir = Test::GetTmpDir(); |
| 138 | if (!tmpDir.isEmpty()) { |
| 139 | header.appendf(" --tmpDir %s", tmpDir.c_str()); |
djsollen@google.com | cb62650 | 2013-03-20 13:48:20 +0000 | [diff] [blame] | 140 | } |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 141 | SkString resourcePath = GetResourcePath(); |
scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 142 | if (!resourcePath.isEmpty()) { |
| 143 | header.appendf(" --resourcePath %s", resourcePath.c_str()); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 144 | } |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 145 | #ifdef SK_DEBUG |
| 146 | header.append(" SK_DEBUG"); |
| 147 | #else |
| 148 | header.append(" SK_RELEASE"); |
| 149 | #endif |
reed@google.com | 5696baa | 2013-08-29 20:20:39 +0000 | [diff] [blame] | 150 | header.appendf(" skia_arch_width=%d", (int)sizeof(void*) * 8); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 151 | if (FLAGS_veryVerbose) { |
| 152 | header.appendf("\n"); |
| 153 | } |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 154 | SkDebugf(header.c_str()); |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 155 | } |
| 156 | |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 157 | |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 158 | // Count tests first. |
| 159 | int total = 0; |
| 160 | int toRun = 0; |
| 161 | Test* test; |
sglez@google.com | 586db93 | 2013-07-24 17:24:23 +0000 | [diff] [blame] | 162 | |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 163 | Iter iter; |
| 164 | while ((test = iter.next(NULL/*reporter not needed*/)) != NULL) { |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 165 | SkAutoTDelete<Test> owned(test); |
commit-bot@chromium.org | 5a47b09 | 2014-01-30 15:30:50 +0000 | [diff] [blame] | 166 | if (should_run(test->getName(), test->isGPUTest())) { |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 167 | toRun++; |
| 168 | } |
| 169 | total++; |
| 170 | } |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 171 | |
| 172 | // Now run them. |
| 173 | iter.reset(); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 174 | int32_t failCount = 0; |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 175 | int skipCount = 0; |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 176 | |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 177 | SkTaskGroup::Enabler enabled(FLAGS_threads); |
| 178 | SkTaskGroup cpuTests; |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 179 | SkTArray<Test*> gpuTests; // Always passes ownership to an SkTestRunnable |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 180 | |
| 181 | DebugfReporter reporter(toRun); |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 182 | for (int i = 0; i < total; i++) { |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 183 | SkAutoTDelete<Test> test(iter.next(&reporter)); |
commit-bot@chromium.org | 5a47b09 | 2014-01-30 15:30:50 +0000 | [diff] [blame] | 184 | if (!should_run(test->getName(), test->isGPUTest())) { |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 185 | ++skipCount; |
commit-bot@chromium.org | 5a47b09 | 2014-01-30 15:30:50 +0000 | [diff] [blame] | 186 | } else if (test->isGPUTest()) { |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 187 | gpuTests.push_back() = test.detach(); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 188 | } else { |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 189 | cpuTests.add(SkNEW_ARGS(SkTestRunnable, (test.detach(), &failCount))); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 190 | } |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 191 | } |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 192 | |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 193 | #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.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 204 | } |
| 205 | |
commit-bot@chromium.org | a7538ba | 2013-10-10 18:49:04 +0000 | [diff] [blame] | 206 | // Block until threaded tests finish. |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 207 | cpuTests.wait(); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 208 | |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 209 | if (FLAGS_verbose) { |
| 210 | SkDebugf("\nFinished %d tests, %d failures, %d skipped. (%d internal tests)", |
| 211 | toRun, failCount, skipCount, reporter.countTests()); |
caryclark@google.com | d54e1e9 | 2013-04-10 15:57:31 +0000 | [diff] [blame] | 212 | } |
reed@google.com | a276975 | 2012-07-22 22:33:05 +0000 | [diff] [blame] | 213 | SkGraphics::Term(); |
| 214 | |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 215 | SkDebugf("\n"); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 216 | return (failCount == 0) ? 0 : 1; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 217 | } |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 218 | |
borenet@google.com | 7158e6a | 2012-11-01 17:43:44 +0000 | [diff] [blame] | 219 | #if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL) |
caryclark | 17f0b6d | 2014-07-22 10:15:34 -0700 | [diff] [blame] | 220 | int main(int argc, char** argv) { |
| 221 | SkCommandLineFlags::Parse(argc, argv); |
| 222 | return test_main(); |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 223 | } |
| 224 | #endif |