blob: 61e16533e442f720e3239c2623c7b327e52838c4 [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"
humper@google.com8dd94f02013-04-25 18:33:49 +000013#include "SkError.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000014
15#if SK_SUPPORT_GPU
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000016#include "GrContext.h"
tomhudson@google.com6bf38b52012-02-14 15:11:59 +000017#include "gl/SkNativeGLContext.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000018#else
19class GrContext;
20#endif
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000021
robertphillips@google.coma22e2112012-08-16 14:58:06 +000022SK_DEFINE_INST_COUNT(skiatest::Reporter)
23
reed@android.comed673312009-02-27 16:24:51 +000024using namespace skiatest;
25
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000026Reporter::Reporter() : fTestCount(0) {
reed@android.comed673312009-02-27 16:24:51 +000027}
28
29void Reporter::startTest(Test* test) {
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000030 this->bumpTestCount();
reed@android.comed673312009-02-27 16:24:51 +000031 this->onStart(test);
reed@android.comed673312009-02-27 16:24:51 +000032}
33
commit-bot@chromium.org1f792862013-06-18 20:50:34 +000034void Reporter::reportFailed(const SkString& desc) {
35 this->onReportFailed(desc);
reed@android.comed673312009-02-27 16:24:51 +000036}
37
38void Reporter::endTest(Test* test) {
reed@android.comed673312009-02-27 16:24:51 +000039 this->onEnd(test);
reed@android.comed673312009-02-27 16:24:51 +000040}
41
42///////////////////////////////////////////////////////////////////////////////
43
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000044Test::Test() : fReporter(NULL), fPassed(true) {}
reed@android.comed673312009-02-27 16:24:51 +000045
46Test::~Test() {
reed@google.com82065d62011-02-07 15:30:46 +000047 SkSafeUnref(fReporter);
reed@android.comed673312009-02-27 16:24:51 +000048}
49
50void Test::setReporter(Reporter* r) {
51 SkRefCnt_SafeAssign(fReporter, r);
52}
53
54const char* Test::getName() {
55 if (fName.size() == 0) {
56 this->onGetName(&fName);
57 }
58 return fName.c_str();
59}
60
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000061namespace {
62 class LocalReporter : public Reporter {
63 public:
commit-bot@chromium.orgc7e08bd2013-04-23 11:16:32 +000064 explicit LocalReporter(Reporter* reporterToMimic) : fReporter(reporterToMimic) {}
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000065
66 int failure_size() const { return fFailures.count(); }
commit-bot@chromium.org1f792862013-06-18 20:50:34 +000067 const SkString& failure(int i) const { return fFailures[i]; }
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000068
69 protected:
commit-bot@chromium.org1f792862013-06-18 20:50:34 +000070 void onReportFailed(const SkString& desc) SK_OVERRIDE {
71 fFailures.push_back(desc);
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000072 }
73
commit-bot@chromium.orgc7e08bd2013-04-23 11:16:32 +000074 // Proxy down to fReporter. We assume these calls are threadsafe.
commit-bot@chromium.orge1c54292013-04-22 17:35:55 +000075 virtual bool allowExtendedTest() const SK_OVERRIDE {
commit-bot@chromium.orgc7e08bd2013-04-23 11:16:32 +000076 return fReporter->allowExtendedTest();
commit-bot@chromium.orge1c54292013-04-22 17:35:55 +000077 }
78
79 virtual bool allowThreaded() const SK_OVERRIDE {
commit-bot@chromium.orgc7e08bd2013-04-23 11:16:32 +000080 return fReporter->allowThreaded();
81 }
82
83 virtual void bumpTestCount() SK_OVERRIDE {
84 fReporter->bumpTestCount();
commit-bot@chromium.orge1c54292013-04-22 17:35:55 +000085 }
86
caryclark@google.com8d0a5242013-07-16 16:11:16 +000087 virtual bool verbose() const SK_OVERRIDE {
88 return fReporter->verbose();
89 }
90
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000091 private:
commit-bot@chromium.orgc7e08bd2013-04-23 11:16:32 +000092 Reporter* fReporter; // Unowned.
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000093 SkTArray<SkString> fFailures;
94 };
95} // namespace
96
97void Test::run() {
humper@google.com8dd94f02013-04-25 18:33:49 +000098 // Clear the Skia error callback before running any test, to ensure that tests
99 // don't have unintended side effects when running more than one.
100 SkSetErrorCallback( NULL, NULL );
101
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000102 // Tell (likely shared) fReporter that this test has started.
reed@android.comed673312009-02-27 16:24:51 +0000103 fReporter->startTest(this);
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000104
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000105 const SkMSec start = SkTime::GetMSecs();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000106 // Run the test into a LocalReporter so we know if it's passed or failed without interference
107 // from other tests that might share fReporter.
commit-bot@chromium.orgc7e08bd2013-04-23 11:16:32 +0000108 LocalReporter local(fReporter);
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000109 this->onRun(&local);
110 fPassed = local.failure_size() == 0;
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000111 fElapsed = SkTime::GetMSecs() - start;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000112
113 // Now tell fReporter about any failures and wrap up.
114 for (int i = 0; i < local.failure_size(); i++) {
commit-bot@chromium.org1f792862013-06-18 20:50:34 +0000115 fReporter->reportFailed(local.failure(i));
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000116 }
reed@android.comed673312009-02-27 16:24:51 +0000117 fReporter->endTest(this);
humper@google.com8dd94f02013-04-25 18:33:49 +0000118
reed@android.comed673312009-02-27 16:24:51 +0000119}
120
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000121///////////////////////////////////////////////////////////////////////////////
122
djsollen@google.com0945bde2012-11-29 15:28:45 +0000123#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000124#include "GrContextFactory.h"
125GrContextFactory gGrContextFactory;
djsollen@google.com0945bde2012-11-29 15:28:45 +0000126#endif
127
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000128GrContextFactory* GpuTest::GetGrContextFactory() {
djsollen@google.com0945bde2012-11-29 15:28:45 +0000129#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000130 return &gGrContextFactory;
131#else
132 return NULL;
djsollen@google.com0945bde2012-11-29 15:28:45 +0000133#endif
134}
135
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000136void GpuTest::DestroyContexts() {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000137#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000138 gGrContextFactory.destroyContexts();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000139#endif
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000140}