Test.cpp revision 79e13260cf94427e6ccbfff8242bf85ed4c8187b
1
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 */
8#include "Test.h"
9
10#include "SkError.h"
11#include "SkString.h"
12#include "SkTArray.h"
13#include "SkTime.h"
14
15#if SK_SUPPORT_GPU
16#include "GrContext.h"
17#include "gl/SkNativeGLContext.h"
18#else
19class GrContext;
20#endif
21
22using namespace skiatest;
23
24Reporter::Reporter() : fTestCount(0) {
25}
26
27void Reporter::startTest(Test* test) {
28    this->bumpTestCount();
29    this->onStart(test);
30}
31
32void Reporter::reportFailed(const SkString& desc) {
33    this->onReportFailed(desc);
34}
35
36void Reporter::endTest(Test* test) {
37    this->onEnd(test);
38}
39
40///////////////////////////////////////////////////////////////////////////////
41
42Test::Test() : fReporter(NULL), fPassed(true) {}
43
44Test::~Test() {
45    SkSafeUnref(fReporter);
46}
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
59class LocalReporter : public Reporter {
60public:
61    explicit LocalReporter(Reporter* reporterToMimic) : fReporter(reporterToMimic) {}
62
63    int numFailures() const { return fFailures.count(); }
64    const SkString& failure(int i) const { return fFailures[i]; }
65
66protected:
67    virtual void onReportFailed(const SkString& desc) SK_OVERRIDE {
68        fFailures.push_back(desc);
69    }
70
71    // Proxy down to fReporter.  We assume these calls are threadsafe.
72    virtual bool allowExtendedTest() const SK_OVERRIDE {
73        return fReporter->allowExtendedTest();
74    }
75
76    virtual bool allowThreaded() const SK_OVERRIDE {
77        return fReporter->allowThreaded();
78    }
79
80    virtual void bumpTestCount() SK_OVERRIDE {
81        fReporter->bumpTestCount();
82    }
83
84    virtual bool verbose() const SK_OVERRIDE {
85        return fReporter->verbose();
86    }
87
88private:
89    Reporter* fReporter;  // Unowned.
90    SkTArray<SkString> fFailures;
91};
92
93void Test::run() {
94    // 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
98    // Tell (likely shared) fReporter that this test has started.
99    fReporter->startTest(this);
100
101    const SkMSec start = SkTime::GetMSecs();
102    // 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.
104    LocalReporter local(fReporter);
105    this->onRun(&local);
106    fPassed = local.numFailures() == 0;
107    fElapsed = SkTime::GetMSecs() - start;
108
109    // Now tell fReporter about any failures and wrap up.
110    for (int i = 0; i < local.numFailures(); i++) {
111      fReporter->reportFailed(local.failure(i));
112    }
113    fReporter->endTest(this);
114
115}
116
117///////////////////////////////////////////////////////////////////////////////
118
119#if SK_SUPPORT_GPU
120#include "GrContextFactory.h"
121GrContextFactory gGrContextFactory;
122#endif
123
124GrContextFactory* GpuTest::GetGrContextFactory() {
125#if SK_SUPPORT_GPU
126    return &gGrContextFactory;
127#else
128    return NULL;
129#endif
130}
131
132void GpuTest::DestroyContexts() {
133#if SK_SUPPORT_GPU
134    gGrContextFactory.destroyContexts();
135#endif
136}
137