epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 10 | #include "SkCommandLineFlags.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 11 | #include "SkError.h" |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 12 | #include "SkString.h" |
| 13 | #include "SkTArray.h" |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 14 | #include "SkTime.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 15 | |
| 16 | #if SK_SUPPORT_GPU |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 17 | #include "GrContext.h" |
tomhudson@google.com | 6bf38b5 | 2012-02-14 15:11:59 +0000 | [diff] [blame] | 18 | #include "gl/SkNativeGLContext.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 19 | #else |
| 20 | class GrContext; |
| 21 | #endif |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 22 | |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 23 | DEFINE_string2(tmpDir, t, NULL, "tmp directory for tests to use."); |
| 24 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 25 | using namespace skiatest; |
| 26 | |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 27 | Reporter::Reporter() : fTestCount(0) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void Reporter::startTest(Test* test) { |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 31 | this->bumpTestCount(); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 32 | this->onStart(test); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 33 | } |
| 34 | |
commit-bot@chromium.org | 1f79286 | 2013-06-18 20:50:34 +0000 | [diff] [blame] | 35 | void Reporter::reportFailed(const SkString& desc) { |
| 36 | this->onReportFailed(desc); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void Reporter::endTest(Test* test) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 40 | this->onEnd(test); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /////////////////////////////////////////////////////////////////////////////// |
| 44 | |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 45 | Test::Test() : fReporter(NULL), fPassed(true) {} |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 46 | |
| 47 | Test::~Test() { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 48 | SkSafeUnref(fReporter); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void Test::setReporter(Reporter* r) { |
| 52 | SkRefCnt_SafeAssign(fReporter, r); |
| 53 | } |
| 54 | |
| 55 | const char* Test::getName() { |
| 56 | if (fName.size() == 0) { |
| 57 | this->onGetName(&fName); |
| 58 | } |
| 59 | return fName.c_str(); |
| 60 | } |
| 61 | |
tfarina@chromium.org | 5867481 | 2014-01-21 23:39:22 +0000 | [diff] [blame] | 62 | class LocalReporter : public Reporter { |
| 63 | public: |
| 64 | explicit LocalReporter(Reporter* reporterToMimic) : fReporter(reporterToMimic) {} |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 65 | |
tfarina@chromium.org | 5867481 | 2014-01-21 23:39:22 +0000 | [diff] [blame] | 66 | int numFailures() const { return fFailures.count(); } |
| 67 | const SkString& failure(int i) const { return fFailures[i]; } |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 68 | |
tfarina@chromium.org | 5867481 | 2014-01-21 23:39:22 +0000 | [diff] [blame] | 69 | protected: |
| 70 | virtual void onReportFailed(const SkString& desc) SK_OVERRIDE { |
| 71 | fFailures.push_back(desc); |
| 72 | } |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 73 | |
tfarina@chromium.org | 5867481 | 2014-01-21 23:39:22 +0000 | [diff] [blame] | 74 | // Proxy down to fReporter. We assume these calls are threadsafe. |
| 75 | virtual bool allowExtendedTest() const SK_OVERRIDE { |
| 76 | return fReporter->allowExtendedTest(); |
| 77 | } |
commit-bot@chromium.org | e1c5429 | 2013-04-22 17:35:55 +0000 | [diff] [blame] | 78 | |
tfarina@chromium.org | 5867481 | 2014-01-21 23:39:22 +0000 | [diff] [blame] | 79 | virtual bool allowThreaded() const SK_OVERRIDE { |
| 80 | return fReporter->allowThreaded(); |
| 81 | } |
commit-bot@chromium.org | c7e08bd | 2013-04-23 11:16:32 +0000 | [diff] [blame] | 82 | |
tfarina@chromium.org | 5867481 | 2014-01-21 23:39:22 +0000 | [diff] [blame] | 83 | virtual void bumpTestCount() SK_OVERRIDE { |
| 84 | fReporter->bumpTestCount(); |
| 85 | } |
commit-bot@chromium.org | e1c5429 | 2013-04-22 17:35:55 +0000 | [diff] [blame] | 86 | |
tfarina@chromium.org | 5867481 | 2014-01-21 23:39:22 +0000 | [diff] [blame] | 87 | virtual bool verbose() const SK_OVERRIDE { |
| 88 | return fReporter->verbose(); |
| 89 | } |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 90 | |
tfarina@chromium.org | 5867481 | 2014-01-21 23:39:22 +0000 | [diff] [blame] | 91 | private: |
| 92 | Reporter* fReporter; // Unowned. |
| 93 | SkTArray<SkString> fFailures; |
| 94 | }; |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 95 | |
| 96 | void Test::run() { |
humper@google.com | 8dd94f0 | 2013-04-25 18:33:49 +0000 | [diff] [blame] | 97 | // Clear the Skia error callback before running any test, to ensure that tests |
| 98 | // don't have unintended side effects when running more than one. |
| 99 | SkSetErrorCallback( NULL, NULL ); |
| 100 | |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 101 | // Tell (likely shared) fReporter that this test has started. |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 102 | fReporter->startTest(this); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 103 | |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 104 | const SkMSec start = SkTime::GetMSecs(); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 105 | // Run the test into a LocalReporter so we know if it's passed or failed without interference |
| 106 | // from other tests that might share fReporter. |
commit-bot@chromium.org | c7e08bd | 2013-04-23 11:16:32 +0000 | [diff] [blame] | 107 | LocalReporter local(fReporter); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 108 | this->onRun(&local); |
tfarina@chromium.org | 5867481 | 2014-01-21 23:39:22 +0000 | [diff] [blame] | 109 | fPassed = local.numFailures() == 0; |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 110 | fElapsed = SkTime::GetMSecs() - start; |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 111 | |
| 112 | // Now tell fReporter about any failures and wrap up. |
tfarina@chromium.org | 5867481 | 2014-01-21 23:39:22 +0000 | [diff] [blame] | 113 | for (int i = 0; i < local.numFailures(); i++) { |
commit-bot@chromium.org | 1f79286 | 2013-06-18 20:50:34 +0000 | [diff] [blame] | 114 | fReporter->reportFailed(local.failure(i)); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 115 | } |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 116 | fReporter->endTest(this); |
humper@google.com | 8dd94f0 | 2013-04-25 18:33:49 +0000 | [diff] [blame] | 117 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 118 | } |
| 119 | |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 120 | SkString Test::GetTmpDir() { |
| 121 | const char* tmpDir = FLAGS_tmpDir.isEmpty() ? NULL : FLAGS_tmpDir[0]; |
| 122 | return SkString(tmpDir); |
djsollen@google.com | 0945bde | 2012-11-29 15:28:45 +0000 | [diff] [blame] | 123 | } |
| 124 | |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 125 | static const char* gResourcePath = NULL; |
| 126 | void Test::SetResourcePath(const char* resourcePath) { gResourcePath = resourcePath; } |
| 127 | |
| 128 | SkString Test::GetResourcePath() { |
| 129 | return SkString(gResourcePath); |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 130 | } |