blob: 81c4ef980787d4d346115789b0f36b261fbe66cb [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:
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
80void Test::run() {
81 // Tell (likely shared) fReporter that this test has started.
reed@android.comed673312009-02-27 16:24:51 +000082 fReporter->startTest(this);
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000083
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000084 const SkMSec start = SkTime::GetMSecs();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000085 // 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.org0506b9d2013-04-22 16:43:07 +000090 fElapsed = SkTime::GetMSecs() - start;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000091
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.comed673312009-02-27 16:24:51 +000096 fReporter->endTest(this);
reed@android.comed673312009-02-27 16:24:51 +000097}
98
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000099///////////////////////////////////////////////////////////////////////////////
100
djsollen@google.com0945bde2012-11-29 15:28:45 +0000101#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000102#include "GrContextFactory.h"
103GrContextFactory gGrContextFactory;
djsollen@google.com0945bde2012-11-29 15:28:45 +0000104#endif
105
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000106GrContextFactory* GpuTest::GetGrContextFactory() {
djsollen@google.com0945bde2012-11-29 15:28:45 +0000107#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000108 return &gGrContextFactory;
109#else
110 return NULL;
djsollen@google.com0945bde2012-11-29 15:28:45 +0000111#endif
112}
113
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000114void GpuTest::DestroyContexts() {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000115#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000116 gGrContextFactory.destroyContexts();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000117#endif
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000118}