blob: 5558a0be5ecc7f415a4eaa370ed321d47893ec1d [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.orgc7e08bd2013-04-23 11:16:32 +000063 explicit LocalReporter(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.orgc7e08bd2013-04-23 11:16:32 +000075 // Proxy down to fReporter. We assume these calls are threadsafe.
commit-bot@chromium.orge1c54292013-04-22 17:35:55 +000076 virtual bool allowExtendedTest() const SK_OVERRIDE {
commit-bot@chromium.orgc7e08bd2013-04-23 11:16:32 +000077 return fReporter->allowExtendedTest();
commit-bot@chromium.orge1c54292013-04-22 17:35:55 +000078 }
79
80 virtual bool allowThreaded() const SK_OVERRIDE {
commit-bot@chromium.orgc7e08bd2013-04-23 11:16:32 +000081 return fReporter->allowThreaded();
82 }
83
84 virtual void bumpTestCount() SK_OVERRIDE {
85 fReporter->bumpTestCount();
commit-bot@chromium.orge1c54292013-04-22 17:35:55 +000086 }
87
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000088 private:
commit-bot@chromium.orgc7e08bd2013-04-23 11:16:32 +000089 Reporter* fReporter; // Unowned.
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000090 SkTArray<SkString> fFailures;
91 };
92} // namespace
93
94void Test::run() {
95 // Tell (likely shared) fReporter that this test has started.
reed@android.comed673312009-02-27 16:24:51 +000096 fReporter->startTest(this);
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000097
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000098 const SkMSec start = SkTime::GetMSecs();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000099 // Run the test into a LocalReporter so we know if it's passed or failed without interference
100 // from other tests that might share fReporter.
commit-bot@chromium.orgc7e08bd2013-04-23 11:16:32 +0000101 LocalReporter local(fReporter);
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000102 this->onRun(&local);
103 fPassed = local.failure_size() == 0;
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000104 fElapsed = SkTime::GetMSecs() - start;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000105
106 // Now tell fReporter about any failures and wrap up.
107 for (int i = 0; i < local.failure_size(); i++) {
108 fReporter->report(local.failure(i), Reporter::kFailed);
109 }
reed@android.comed673312009-02-27 16:24:51 +0000110 fReporter->endTest(this);
reed@android.comed673312009-02-27 16:24:51 +0000111}
112
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000113///////////////////////////////////////////////////////////////////////////////
114
djsollen@google.com0945bde2012-11-29 15:28:45 +0000115#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000116#include "GrContextFactory.h"
117GrContextFactory gGrContextFactory;
djsollen@google.com0945bde2012-11-29 15:28:45 +0000118#endif
119
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000120GrContextFactory* GpuTest::GetGrContextFactory() {
djsollen@google.com0945bde2012-11-29 15:28:45 +0000121#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000122 return &gGrContextFactory;
123#else
124 return NULL;
djsollen@google.com0945bde2012-11-29 15:28:45 +0000125#endif
126}
127
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000128void GpuTest::DestroyContexts() {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000129#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000130 gGrContextFactory.destroyContexts();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000131#endif
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000132}