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: |
| 63 | LocalReporter() {} |
| 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 | |
| 75 | private: |
| 76 | SkTArray<SkString> fFailures; |
| 77 | }; |
| 78 | } // namespace |
| 79 | |
| 80 | void Test::run() { |
| 81 | // Tell (likely shared) fReporter that this test has started. |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 82 | fReporter->startTest(this); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 83 | |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame^] | 84 | const SkMSec start = SkTime::GetMSecs(); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 85 | // Run the test into a LocalReporter so we know if it's passed or failed without interference |
| 86 | // from other tests that might share fReporter. |
| 87 | LocalReporter local; |
| 88 | this->onRun(&local); |
| 89 | fPassed = local.failure_size() == 0; |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame^] | 90 | fElapsed = SkTime::GetMSecs() - start; |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 91 | |
| 92 | // Now tell fReporter about any failures and wrap up. |
| 93 | for (int i = 0; i < local.failure_size(); i++) { |
| 94 | fReporter->report(local.failure(i), Reporter::kFailed); |
| 95 | } |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 96 | fReporter->endTest(this); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 97 | } |
| 98 | |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 99 | /////////////////////////////////////////////////////////////////////////////// |
| 100 | |
djsollen@google.com | 0945bde | 2012-11-29 15:28:45 +0000 | [diff] [blame] | 101 | #if SK_SUPPORT_GPU |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 102 | #include "GrContextFactory.h" |
| 103 | GrContextFactory gGrContextFactory; |
djsollen@google.com | 0945bde | 2012-11-29 15:28:45 +0000 | [diff] [blame] | 104 | #endif |
| 105 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 106 | GrContextFactory* GpuTest::GetGrContextFactory() { |
djsollen@google.com | 0945bde | 2012-11-29 15:28:45 +0000 | [diff] [blame] | 107 | #if SK_SUPPORT_GPU |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 108 | return &gGrContextFactory; |
| 109 | #else |
| 110 | return NULL; |
djsollen@google.com | 0945bde | 2012-11-29 15:28:45 +0000 | [diff] [blame] | 111 | #endif |
| 112 | } |
| 113 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 114 | void GpuTest::DestroyContexts() { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 115 | #if SK_SUPPORT_GPU |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 116 | gGrContextFactory.destroyContexts(); |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 117 | #endif |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 118 | } |