OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Test.h" | 8 #include "Test.h" |
9 | 9 |
10 #include "SkCommandLineFlags.h" | 10 #include "SkCommandLineFlags.h" |
11 #include "SkError.h" | 11 #include "SkError.h" |
12 #include "SkString.h" | 12 #include "SkString.h" |
13 #include "SkTArray.h" | |
14 #include "SkTime.h" | 13 #include "SkTime.h" |
15 | 14 |
16 #if SK_SUPPORT_GPU | 15 DEFINE_string2(tmpDir, t, NULL, "Temp directory to use."); |
17 #include "GrContext.h" | |
18 #include "gl/SkGLContext.h" | |
19 #else | |
20 class GrContext; | |
21 #endif | |
22 | 16 |
23 DEFINE_string2(tmpDir, t, NULL, "tmp directory for tests to use."); | 17 void skiatest::Reporter::bumpTestCount() {} |
24 | 18 |
25 using namespace skiatest; | 19 bool skiatest::Reporter::allowExtendedTest() const { return false; } |
26 | 20 |
27 Reporter::Reporter() : fTestCount(0) { | 21 bool skiatest::Reporter::verbose() const { return false; } |
| 22 |
| 23 SkString skiatest::Failure::toString() const { |
| 24 SkString result = SkStringPrintf("%s:%d\t", this->fileName, this->lineNo); |
| 25 if (!this->message.isEmpty()) { |
| 26 result.append(this->message); |
| 27 if (strlen(this->condition) > 0) { |
| 28 result.append(": "); |
| 29 } |
| 30 } |
| 31 result.append(this->condition); |
| 32 return result; |
28 } | 33 } |
29 | 34 |
30 void Reporter::startTest(Test* test) { | 35 SkString skiatest::GetTmpDir() { |
31 this->onStart(test); | |
32 } | |
33 | |
34 void Reporter::reportFailed(const skiatest::Failure& failure) { | |
35 this->onReportFailed(failure); | |
36 } | |
37 | |
38 void Reporter::endTest(Test* test) { | |
39 this->onEnd(test); | |
40 } | |
41 | |
42 /////////////////////////////////////////////////////////////////////////////// | |
43 | |
44 Test::Test() : fReporter(NULL), fPassed(true) {} | |
45 | |
46 Test::~Test() { | |
47 SkSafeUnref(fReporter); | |
48 } | |
49 | |
50 void Test::setReporter(Reporter* r) { | |
51 SkRefCnt_SafeAssign(fReporter, r); | |
52 } | |
53 | |
54 const char* Test::getName() { | |
55 if (fName.size() == 0) { | |
56 this->onGetName(&fName); | |
57 } | |
58 return fName.c_str(); | |
59 } | |
60 | |
61 class LocalReporter : public Reporter { | |
62 public: | |
63 explicit LocalReporter(Reporter* reporterToMimic) : fReporter(reporterToMimi
c) {} | |
64 | |
65 int numFailures() const { return fFailures.count(); } | |
66 const skiatest::Failure& failure(int i) const { return fFailures[i]; } | |
67 | |
68 protected: | |
69 void onReportFailed(const Failure& failure) SK_OVERRIDE { | |
70 fFailures.push_back(failure); | |
71 } | |
72 | |
73 // Proxy down to fReporter. We assume these calls are threadsafe. | |
74 bool allowExtendedTest() const SK_OVERRIDE { | |
75 return fReporter->allowExtendedTest(); | |
76 } | |
77 | |
78 void bumpTestCount() SK_OVERRIDE { | |
79 fReporter->bumpTestCount(); | |
80 } | |
81 | |
82 bool verbose() const SK_OVERRIDE { | |
83 return fReporter->verbose(); | |
84 } | |
85 | |
86 private: | |
87 Reporter* fReporter; // Unowned. | |
88 SkTArray<skiatest::Failure> fFailures; | |
89 }; | |
90 | |
91 void Test::run() { | |
92 // Clear the Skia error callback before running any test, to ensure that tes
ts | |
93 // don't have unintended side effects when running more than one. | |
94 SkSetErrorCallback( NULL, NULL ); | |
95 | |
96 // Tell (likely shared) fReporter that this test has started. | |
97 fReporter->startTest(this); | |
98 | |
99 const SkMSec start = SkTime::GetMSecs(); | |
100 // Run the test into a LocalReporter so we know if it's passed or failed wit
hout interference | |
101 // from other tests that might share fReporter. | |
102 LocalReporter local(fReporter); | |
103 this->onRun(&local); | |
104 fPassed = local.numFailures() == 0; | |
105 fElapsed = SkTime::GetMSecs() - start; | |
106 | |
107 // Now tell fReporter about any failures and wrap up. | |
108 for (int i = 0; i < local.numFailures(); i++) { | |
109 fReporter->reportFailed(local.failure(i)); | |
110 } | |
111 fReporter->endTest(this); | |
112 | |
113 } | |
114 | |
115 SkString Test::GetTmpDir() { | |
116 const char* tmpDir = FLAGS_tmpDir.isEmpty() ? NULL : FLAGS_tmpDir[0]; | 36 const char* tmpDir = FLAGS_tmpDir.isEmpty() ? NULL : FLAGS_tmpDir[0]; |
117 return SkString(tmpDir); | 37 return SkString(tmpDir); |
118 } | 38 } |
OLD | NEW |