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