blob: 408e14f6917479c2dd07a472fdd6b47751430922 [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"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000012
13#if SK_SUPPORT_GPU
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000014#include "GrContext.h"
tomhudson@google.com6bf38b52012-02-14 15:11:59 +000015#include "gl/SkNativeGLContext.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000016#else
17class GrContext;
18#endif
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000019
robertphillips@google.coma22e2112012-08-16 14:58:06 +000020SK_DEFINE_INST_COUNT(skiatest::Reporter)
21
reed@android.comed673312009-02-27 16:24:51 +000022using namespace skiatest;
23
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000024Reporter::Reporter() : fTestCount(0) {
reed@android.comed673312009-02-27 16:24:51 +000025}
26
27void Reporter::startTest(Test* test) {
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000028 this->bumpTestCount();
reed@android.comed673312009-02-27 16:24:51 +000029 this->onStart(test);
reed@android.comed673312009-02-27 16:24:51 +000030}
31
32void Reporter::report(const char desc[], Result result) {
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000033 this->onReport(desc ? desc : "<no description>", result);
reed@android.comed673312009-02-27 16:24:51 +000034}
35
36void Reporter::endTest(Test* test) {
reed@android.comed673312009-02-27 16:24:51 +000037 this->onEnd(test);
reed@android.comed673312009-02-27 16:24:51 +000038}
39
40///////////////////////////////////////////////////////////////////////////////
41
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000042Test::Test() : fReporter(NULL), fPassed(true) {}
reed@android.comed673312009-02-27 16:24:51 +000043
44Test::~Test() {
reed@google.com82065d62011-02-07 15:30:46 +000045 SkSafeUnref(fReporter);
reed@android.comed673312009-02-27 16:24:51 +000046}
47
48void Test::setReporter(Reporter* r) {
49 SkRefCnt_SafeAssign(fReporter, r);
50}
51
52const char* Test::getName() {
53 if (fName.size() == 0) {
54 this->onGetName(&fName);
55 }
56 return fName.c_str();
57}
58
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000059namespace {
60 class LocalReporter : public Reporter {
61 public:
62 LocalReporter() {}
63
64 int failure_size() const { return fFailures.count(); }
65 const char* failure(int i) const { return fFailures[i].c_str(); }
66
67 protected:
68 void onReport(const char desc[], Result result) SK_OVERRIDE {
69 if (kFailed == result) {
70 fFailures.push_back().set(desc);
71 }
72 }
73
74 private:
75 SkTArray<SkString> fFailures;
76 };
77} // namespace
78
79void Test::run() {
80 // Tell (likely shared) fReporter that this test has started.
reed@android.comed673312009-02-27 16:24:51 +000081 fReporter->startTest(this);
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000082
83 // Run the test into a LocalReporter so we know if it's passed or failed without interference
84 // from other tests that might share fReporter.
85 LocalReporter local;
86 this->onRun(&local);
87 fPassed = local.failure_size() == 0;
88
89 // Now tell fReporter about any failures and wrap up.
90 for (int i = 0; i < local.failure_size(); i++) {
91 fReporter->report(local.failure(i), Reporter::kFailed);
92 }
reed@android.comed673312009-02-27 16:24:51 +000093 fReporter->endTest(this);
reed@android.comed673312009-02-27 16:24:51 +000094}
95
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000096///////////////////////////////////////////////////////////////////////////////
97
djsollen@google.com0945bde2012-11-29 15:28:45 +000098#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +000099#include "GrContextFactory.h"
100GrContextFactory gGrContextFactory;
djsollen@google.com0945bde2012-11-29 15:28:45 +0000101#endif
102
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000103GrContextFactory* GpuTest::GetGrContextFactory() {
djsollen@google.com0945bde2012-11-29 15:28:45 +0000104#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000105 return &gGrContextFactory;
106#else
107 return NULL;
djsollen@google.com0945bde2012-11-29 15:28:45 +0000108#endif
109}
110
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000111void GpuTest::DestroyContexts() {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000112#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000113 gGrContextFactory.destroyContexts();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000114#endif
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000115}