• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
7 
8 #include "Test.h"
9 
10 #include "SkCommandLineFlags.h"
11 #include "SkError.h"
12 #include "SkString.h"
13 #include "SkTArray.h"
14 #include "SkTime.h"
15 
16 #if SK_SUPPORT_GPU
17 #include "GrContext.h"
18 #include "gl/SkNativeGLContext.h"
19 #else
20 class GrContext;
21 #endif
22 
23 DEFINE_string2(tmpDir, t, NULL, "tmp directory for tests to use.");
24 
25 using namespace skiatest;
26 
Reporter()27 Reporter::Reporter() : fTestCount(0) {
28 }
29 
startTest(Test * test)30 void Reporter::startTest(Test* test) {
31     this->onStart(test);
32 }
33 
reportFailed(const SkString & desc)34 void Reporter::reportFailed(const SkString& desc) {
35     this->onReportFailed(desc);
36 }
37 
endTest(Test * test)38 void Reporter::endTest(Test* test) {
39     this->onEnd(test);
40 }
41 
42 ///////////////////////////////////////////////////////////////////////////////
43 
Test()44 Test::Test() : fReporter(NULL), fPassed(true) {}
45 
~Test()46 Test::~Test() {
47     SkSafeUnref(fReporter);
48 }
49 
setReporter(Reporter * r)50 void Test::setReporter(Reporter* r) {
51     SkRefCnt_SafeAssign(fReporter, r);
52 }
53 
getName()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:
LocalReporter(Reporter * reporterToMimic)63     explicit LocalReporter(Reporter* reporterToMimic) : fReporter(reporterToMimic) {}
64 
numFailures() const65     int numFailures() const { return fFailures.count(); }
failure(int i) const66     const SkString& failure(int i) const { return fFailures[i]; }
67 
68 protected:
onReportFailed(const SkString & desc)69     virtual void onReportFailed(const SkString& desc) SK_OVERRIDE {
70         fFailures.push_back(desc);
71     }
72 
73     // Proxy down to fReporter.  We assume these calls are threadsafe.
allowExtendedTest() const74     virtual bool allowExtendedTest() const SK_OVERRIDE {
75         return fReporter->allowExtendedTest();
76     }
77 
allowThreaded() const78     virtual bool allowThreaded() const SK_OVERRIDE {
79         return fReporter->allowThreaded();
80     }
81 
bumpTestCount()82     virtual void bumpTestCount() SK_OVERRIDE {
83         fReporter->bumpTestCount();
84     }
85 
verbose() const86     virtual bool verbose() const SK_OVERRIDE {
87         return fReporter->verbose();
88     }
89 
90 private:
91     Reporter* fReporter;  // Unowned.
92     SkTArray<SkString> fFailures;
93 };
94 
run()95 void Test::run() {
96     // Clear the Skia error callback before running any test, to ensure that tests
97     // don't have unintended side effects when running more than one.
98     SkSetErrorCallback( NULL, NULL );
99 
100     // Tell (likely shared) fReporter that this test has started.
101     fReporter->startTest(this);
102 
103     const SkMSec start = SkTime::GetMSecs();
104     // Run the test into a LocalReporter so we know if it's passed or failed without interference
105     // from other tests that might share fReporter.
106     LocalReporter local(fReporter);
107     this->onRun(&local);
108     fPassed = local.numFailures() == 0;
109     fElapsed = SkTime::GetMSecs() - start;
110 
111     // Now tell fReporter about any failures and wrap up.
112     for (int i = 0; i < local.numFailures(); i++) {
113       fReporter->reportFailed(local.failure(i));
114     }
115     fReporter->endTest(this);
116 
117 }
118 
GetTmpDir()119 SkString Test::GetTmpDir() {
120     const char* tmpDir = FLAGS_tmpDir.isEmpty() ? NULL : FLAGS_tmpDir[0];
121     return SkString(tmpDir);
122 }
123