blob: b904d5a76d739e184c8937b0533c6fd17f262df1 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
tfarina880914c2014-06-09 12:05:34 -07007
reed@android.comed673312009-02-27 16:24:51 +00008#include "Test.h"
9
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000010#include "SkCommandLineFlags.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000011#include "SkError.h"
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000012#include "SkString.h"
13#include "SkTArray.h"
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000014#include "SkTime.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000015
16#if SK_SUPPORT_GPU
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000017#include "GrContext.h"
tomhudson@google.com6bf38b52012-02-14 15:11:59 +000018#include "gl/SkNativeGLContext.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000019#else
20class GrContext;
21#endif
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000022
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000023DEFINE_string2(tmpDir, t, NULL, "tmp directory for tests to use.");
24
reed@android.comed673312009-02-27 16:24:51 +000025using namespace skiatest;
26
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000027Reporter::Reporter() : fTestCount(0) {
reed@android.comed673312009-02-27 16:24:51 +000028}
29
30void Reporter::startTest(Test* test) {
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
tfarina880914c2014-06-09 12:05:34 -070044const char* Test::gResourcePath;
45
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000046Test::Test() : fReporter(NULL), fPassed(true) {}
reed@android.comed673312009-02-27 16:24:51 +000047
48Test::~Test() {
reed@google.com82065d62011-02-07 15:30:46 +000049 SkSafeUnref(fReporter);
reed@android.comed673312009-02-27 16:24:51 +000050}
51
52void Test::setReporter(Reporter* r) {
53 SkRefCnt_SafeAssign(fReporter, r);
54}
55
56const char* Test::getName() {
57 if (fName.size() == 0) {
58 this->onGetName(&fName);
59 }
60 return fName.c_str();
61}
62
tfarina@chromium.org58674812014-01-21 23:39:22 +000063class LocalReporter : public Reporter {
64public:
65 explicit LocalReporter(Reporter* reporterToMimic) : fReporter(reporterToMimic) {}
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000066
tfarina@chromium.org58674812014-01-21 23:39:22 +000067 int numFailures() const { return fFailures.count(); }
68 const SkString& failure(int i) const { return fFailures[i]; }
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000069
tfarina@chromium.org58674812014-01-21 23:39:22 +000070protected:
71 virtual void onReportFailed(const SkString& desc) SK_OVERRIDE {
72 fFailures.push_back(desc);
73 }
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000074
tfarina@chromium.org58674812014-01-21 23:39:22 +000075 // Proxy down to fReporter. We assume these calls are threadsafe.
76 virtual bool allowExtendedTest() const SK_OVERRIDE {
77 return fReporter->allowExtendedTest();
78 }
commit-bot@chromium.orge1c54292013-04-22 17:35:55 +000079
tfarina@chromium.org58674812014-01-21 23:39:22 +000080 virtual bool allowThreaded() const SK_OVERRIDE {
81 return fReporter->allowThreaded();
82 }
commit-bot@chromium.orgc7e08bd2013-04-23 11:16:32 +000083
tfarina@chromium.org58674812014-01-21 23:39:22 +000084 virtual void bumpTestCount() SK_OVERRIDE {
85 fReporter->bumpTestCount();
86 }
commit-bot@chromium.orge1c54292013-04-22 17:35:55 +000087
tfarina@chromium.org58674812014-01-21 23:39:22 +000088 virtual bool verbose() const SK_OVERRIDE {
89 return fReporter->verbose();
90 }
caryclark@google.com8d0a5242013-07-16 16:11:16 +000091
tfarina@chromium.org58674812014-01-21 23:39:22 +000092private:
93 Reporter* fReporter; // Unowned.
94 SkTArray<SkString> fFailures;
95};
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000096
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);
tfarina@chromium.org58674812014-01-21 23:39:22 +0000110 fPassed = local.numFailures() == 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.
tfarina@chromium.org58674812014-01-21 23:39:22 +0000114 for (int i = 0; i < local.numFailures(); 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
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000121SkString Test::GetTmpDir() {
122 const char* tmpDir = FLAGS_tmpDir.isEmpty() ? NULL : FLAGS_tmpDir[0];
123 return SkString(tmpDir);
djsollen@google.com0945bde2012-11-29 15:28:45 +0000124}
125
tfarina880914c2014-06-09 12:05:34 -0700126void Test::SetResourcePath(const char* resourcePath) {
127 gResourcePath = resourcePath;
128}
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000129
130SkString Test::GetResourcePath() {
131 return SkString(gResourcePath);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000132}