blob: 994a342128781d0b2fb816bcc2a45a7f41adf5fc [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
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
commit-bot@chromium.org1f792862013-06-18 20:50:34 +000032void Reporter::reportFailed(const SkString& desc) {
33 this->onReportFailed(desc);
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
tfarina@chromium.org58674812014-01-21 23:39:22 +000059class LocalReporter : public Reporter {
60public:
61 explicit LocalReporter(Reporter* reporterToMimic) : fReporter(reporterToMimic) {}
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000062
tfarina@chromium.org58674812014-01-21 23:39:22 +000063 int numFailures() const { return fFailures.count(); }
64 const SkString& failure(int i) const { return fFailures[i]; }
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000065
tfarina@chromium.org58674812014-01-21 23:39:22 +000066protected:
67 virtual void onReportFailed(const SkString& desc) SK_OVERRIDE {
68 fFailures.push_back(desc);
69 }
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000070
tfarina@chromium.org58674812014-01-21 23:39:22 +000071 // Proxy down to fReporter. We assume these calls are threadsafe.
72 virtual bool allowExtendedTest() const SK_OVERRIDE {
73 return fReporter->allowExtendedTest();
74 }
commit-bot@chromium.orge1c54292013-04-22 17:35:55 +000075
tfarina@chromium.org58674812014-01-21 23:39:22 +000076 virtual bool allowThreaded() const SK_OVERRIDE {
77 return fReporter->allowThreaded();
78 }
commit-bot@chromium.orgc7e08bd2013-04-23 11:16:32 +000079
tfarina@chromium.org58674812014-01-21 23:39:22 +000080 virtual void bumpTestCount() SK_OVERRIDE {
81 fReporter->bumpTestCount();
82 }
commit-bot@chromium.orge1c54292013-04-22 17:35:55 +000083
tfarina@chromium.org58674812014-01-21 23:39:22 +000084 virtual bool verbose() const SK_OVERRIDE {
85 return fReporter->verbose();
86 }
caryclark@google.com8d0a5242013-07-16 16:11:16 +000087
tfarina@chromium.org58674812014-01-21 23:39:22 +000088private:
89 Reporter* fReporter; // Unowned.
90 SkTArray<SkString> fFailures;
91};
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000092
93void Test::run() {
humper@google.com8dd94f02013-04-25 18:33:49 +000094 // Clear the Skia error callback before running any test, to ensure that tests
95 // don't have unintended side effects when running more than one.
96 SkSetErrorCallback( NULL, NULL );
97
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000098 // Tell (likely shared) fReporter that this test has started.
reed@android.comed673312009-02-27 16:24:51 +000099 fReporter->startTest(this);
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000100
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000101 const SkMSec start = SkTime::GetMSecs();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000102 // Run the test into a LocalReporter so we know if it's passed or failed without interference
103 // from other tests that might share fReporter.
commit-bot@chromium.orgc7e08bd2013-04-23 11:16:32 +0000104 LocalReporter local(fReporter);
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000105 this->onRun(&local);
tfarina@chromium.org58674812014-01-21 23:39:22 +0000106 fPassed = local.numFailures() == 0;
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000107 fElapsed = SkTime::GetMSecs() - start;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000108
109 // Now tell fReporter about any failures and wrap up.
tfarina@chromium.org58674812014-01-21 23:39:22 +0000110 for (int i = 0; i < local.numFailures(); i++) {
commit-bot@chromium.org1f792862013-06-18 20:50:34 +0000111 fReporter->reportFailed(local.failure(i));
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000112 }
reed@android.comed673312009-02-27 16:24:51 +0000113 fReporter->endTest(this);
humper@google.com8dd94f02013-04-25 18:33:49 +0000114
reed@android.comed673312009-02-27 16:24:51 +0000115}
116
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000117///////////////////////////////////////////////////////////////////////////////
118
djsollen@google.com0945bde2012-11-29 15:28:45 +0000119#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000120#include "GrContextFactory.h"
121GrContextFactory gGrContextFactory;
djsollen@google.com0945bde2012-11-29 15:28:45 +0000122#endif
123
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000124GrContextFactory* GpuTest::GetGrContextFactory() {
djsollen@google.com0945bde2012-11-29 15:28:45 +0000125#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000126 return &gGrContextFactory;
127#else
128 return NULL;
djsollen@google.com0945bde2012-11-29 15:28:45 +0000129#endif
130}
131
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000132void GpuTest::DestroyContexts() {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000133#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000134 gGrContextFactory.destroyContexts();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000135#endif
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000136}