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" |
| 15 | #include "SkTemplates.h" |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 16 | #include "SkThreadPool.h" |
| 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; } |
| 57 | virtual bool allowThreaded() const SK_OVERRIDE { return !FLAGS_single; } |
commit-bot@chromium.org | 11ea96c | 2014-01-28 21:15:42 +0000 | [diff] [blame] | 58 | virtual bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; } |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 59 | |
| 60 | protected: |
| 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 | |
| 81 | private: |
| 82 | int32_t fDone; // atomic |
| 83 | const int fTotal; |
| 84 | }; |
| 85 | |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 86 | // Deletes self when run. |
| 87 | class SkTestRunnable : public SkRunnable { |
| 88 | public: |
| 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 | |
| 100 | private: |
| 101 | SkAutoTDelete<Test> fTest; |
| 102 | int32_t* fFailCount; |
| 103 | }; |
commit-bot@chromium.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 104 | |
commit-bot@chromium.org | 5a47b09 | 2014-01-30 15:30:50 +0000 | [diff] [blame] | 105 | static 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 | |
caryclark | 17f0b6d | 2014-07-22 10:15:34 -0700 | [diff] [blame] | 118 | int test_main(); |
| 119 | int test_main() { |
mtklein | 30e6e2a | 2014-06-18 11:44:15 -0700 | [diff] [blame] | 120 | SetupCrashHandler(); |
commit-bot@chromium.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 121 | |
bsalomon@google.com | 4e23068 | 2013-01-15 20:37:04 +0000 | [diff] [blame] | 122 | #if SK_ENABLE_INST_COUNT |
commit-bot@chromium.org | 6dda827 | 2014-01-23 17:21:19 +0000 | [diff] [blame] | 123 | if (FLAGS_leaks) { |
| 124 | gPrintInstCount = true; |
| 125 | } |
reed@google.com | a276975 | 2012-07-22 22:33:05 +0000 | [diff] [blame] | 126 | #endif |
caryclark@google.com | d54e1e9 | 2013-04-10 15:57:31 +0000 | [diff] [blame] | 127 | |
reed@google.com | a276975 | 2012-07-22 22:33:05 +0000 | [diff] [blame] | 128 | SkGraphics::Init(); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 129 | |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 130 | { |
| 131 | SkString header("Skia UnitTests:"); |
reed@google.com | 9aff148 | 2013-04-11 18:27:52 +0000 | [diff] [blame] | 132 | if (!FLAGS_match.isEmpty()) { |
caryclark@google.com | b631eec | 2013-05-02 13:14:40 +0000 | [diff] [blame] | 133 | header.appendf(" --match"); |
| 134 | for (int index = 0; index < FLAGS_match.count(); ++index) { |
| 135 | header.appendf(" %s", FLAGS_match[index]); |
| 136 | } |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 137 | } |
scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 138 | SkString tmpDir = Test::GetTmpDir(); |
| 139 | if (!tmpDir.isEmpty()) { |
| 140 | header.appendf(" --tmpDir %s", tmpDir.c_str()); |
djsollen@google.com | cb62650 | 2013-03-20 13:48:20 +0000 | [diff] [blame] | 141 | } |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 142 | SkString resourcePath = GetResourcePath(); |
scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 143 | if (!resourcePath.isEmpty()) { |
| 144 | header.appendf(" --resourcePath %s", resourcePath.c_str()); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 145 | } |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 146 | #ifdef SK_DEBUG |
| 147 | header.append(" SK_DEBUG"); |
| 148 | #else |
| 149 | header.append(" SK_RELEASE"); |
| 150 | #endif |
reed@google.com | 5696baa | 2013-08-29 20:20:39 +0000 | [diff] [blame] | 151 | header.appendf(" skia_arch_width=%d", (int)sizeof(void*) * 8); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 152 | if (FLAGS_veryVerbose) { |
| 153 | header.appendf("\n"); |
| 154 | } |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 155 | SkDebugf(header.c_str()); |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 156 | } |
| 157 | |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 158 | |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 159 | // Count tests first. |
| 160 | int total = 0; |
| 161 | int toRun = 0; |
| 162 | Test* test; |
sglez@google.com | 586db93 | 2013-07-24 17:24:23 +0000 | [diff] [blame] | 163 | |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 164 | Iter iter; |
| 165 | while ((test = iter.next(NULL/*reporter not needed*/)) != NULL) { |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 166 | SkAutoTDelete<Test> owned(test); |
commit-bot@chromium.org | 5a47b09 | 2014-01-30 15:30:50 +0000 | [diff] [blame] | 167 | if (should_run(test->getName(), test->isGPUTest())) { |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 168 | toRun++; |
| 169 | } |
| 170 | total++; |
| 171 | } |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 172 | |
| 173 | // Now run them. |
| 174 | iter.reset(); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 175 | int32_t failCount = 0; |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 176 | int skipCount = 0; |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 177 | |
commit-bot@chromium.org | a7538ba | 2013-10-10 18:49:04 +0000 | [diff] [blame] | 178 | SkThreadPool threadpool(FLAGS_threads); |
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 { |
commit-bot@chromium.org | a7538ba | 2013-10-10 18:49:04 +0000 | [diff] [blame] | 189 | threadpool.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. |
| 207 | threadpool.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 |