blob: 07db96953e7bc01a2603c5adf65c7d44e386a5d1 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.comed673312009-02-27 16:24:51 +00008#include "Test.h"
9
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000010#include "SkString.h"
11#include "SkTArray.h"
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000012#include "SkTime.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000013
14#if SK_SUPPORT_GPU
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000015#include "GrContext.h"
tomhudson@google.com6bf38b52012-02-14 15:11:59 +000016#include "gl/SkNativeGLContext.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000017#else
18class GrContext;
19#endif
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000020
robertphillips@google.coma22e2112012-08-16 14:58:06 +000021SK_DEFINE_INST_COUNT(skiatest::Reporter)
22
reed@android.comed673312009-02-27 16:24:51 +000023using namespace skiatest;
24
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000025Reporter::Reporter() : fTestCount(0) {
reed@android.comed673312009-02-27 16:24:51 +000026}
27
28void Reporter::startTest(Test* test) {
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000029 this->bumpTestCount();
reed@android.comed673312009-02-27 16:24:51 +000030 this->onStart(test);
reed@android.comed673312009-02-27 16:24:51 +000031}
32
33void Reporter::report(const char desc[], Result result) {
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000034 this->onReport(desc ? desc : "<no description>", result);
reed@android.comed673312009-02-27 16:24:51 +000035}
36
37void Reporter::endTest(Test* test) {
reed@android.comed673312009-02-27 16:24:51 +000038 this->onEnd(test);
reed@android.comed673312009-02-27 16:24:51 +000039}
40
41///////////////////////////////////////////////////////////////////////////////
42
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000043Test::Test() : fReporter(NULL), fPassed(true) {}
reed@android.comed673312009-02-27 16:24:51 +000044
45Test::~Test() {
reed@google.com82065d62011-02-07 15:30:46 +000046 SkSafeUnref(fReporter);
reed@android.comed673312009-02-27 16:24:51 +000047}
48
49void Test::setReporter(Reporter* r) {
50 SkRefCnt_SafeAssign(fReporter, r);
51}
52
53const char* Test::getName() {
54 if (fName.size() == 0) {
55 this->onGetName(&fName);
56 }
57 return fName.c_str();
58}
59
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000060namespace {
61 class LocalReporter : public Reporter {
62 public:
commit-bot@chromium.orge1c54292013-04-22 17:35:55 +000063 explicit LocalReporter(const Reporter& reporterToMimic) : fReporter(reporterToMimic) {}
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000064
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.orge1c54292013-04-22 17:35:55 +000075 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.org197845a2013-04-19 13:24:28 +000083 private:
commit-bot@chromium.orge1c54292013-04-22 17:35:55 +000084 const Reporter& fReporter;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000085 SkTArray<SkString> fFailures;
86 };
87} // namespace
88
89void Test::run() {
90 // Tell (likely shared) fReporter that this test has started.
reed@android.comed673312009-02-27 16:24:51 +000091 fReporter->startTest(this);
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000092
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000093 const SkMSec start = SkTime::GetMSecs();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000094 // 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.orge1c54292013-04-22 17:35:55 +000096 LocalReporter local(*fReporter);
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000097 this->onRun(&local);
98 fPassed = local.failure_size() == 0;
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000099 fElapsed = SkTime::GetMSecs() - start;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000100
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.comed673312009-02-27 16:24:51 +0000105 fReporter->endTest(this);
reed@android.comed673312009-02-27 16:24:51 +0000106}
107
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000108///////////////////////////////////////////////////////////////////////////////
109
djsollen@google.com0945bde2012-11-29 15:28:45 +0000110#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000111#include "GrContextFactory.h"
112GrContextFactory gGrContextFactory;
djsollen@google.com0945bde2012-11-29 15:28:45 +0000113#endif
114
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000115GrContextFactory* GpuTest::GetGrContextFactory() {
djsollen@google.com0945bde2012-11-29 15:28:45 +0000116#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000117 return &gGrContextFactory;
118#else
119 return NULL;
djsollen@google.com0945bde2012-11-29 15:28:45 +0000120#endif
121}
122
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000123void GpuTest::DestroyContexts() {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000124#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000125 gGrContextFactory.destroyContexts();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000126#endif
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000127}