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