Test.cpp revision 7839ce1af63bf12fe7b3caa866970bbbb3afb13d
180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2011 Google Inc.
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "Test.h"
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
107839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger#include "SkString.h"
117839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger#include "SkTArray.h"
127839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger#include "SkTime.h"
137839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger#include "SkError.h"
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#if SK_SUPPORT_GPU
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "GrContext.h"
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "gl/SkNativeGLContext.h"
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#else
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass GrContext;
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSK_DEFINE_INST_COUNT(skiatest::Reporter)
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruusing namespace skiatest;
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
267839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek SollenbergerReporter::Reporter() : fTestCount(0) {
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid Reporter::startTest(Test* test) {
307839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    this->bumpTestCount();
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    this->onStart(test);
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid Reporter::report(const char desc[], Result result) {
357839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    this->onReport(desc ? desc : "<no description>", result);
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid Reporter::endTest(Test* test) {
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    this->onEnd(test);
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru///////////////////////////////////////////////////////////////////////////////
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
447839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek SollenbergerTest::Test() : fReporter(NULL), fPassed(true) {}
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruTest::~Test() {
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkSafeUnref(fReporter);
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid Test::setReporter(Reporter* r) {
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkRefCnt_SafeAssign(fReporter, r);
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruconst char* Test::getName() {
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (fName.size() == 0) {
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        this->onGetName(&fName);
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return fName.c_str();
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
617839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenbergernamespace {
627839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    class LocalReporter : public Reporter {
637839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    public:
647839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger        explicit LocalReporter(Reporter* reporterToMimic) : fReporter(reporterToMimic) {}
657839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger
667839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger        int failure_size() const { return fFailures.count(); }
677839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger        const char* failure(int i) const { return fFailures[i].c_str(); }
687839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger
697839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    protected:
707839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger        void onReport(const char desc[], Result result) SK_OVERRIDE {
717839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger            if (kFailed == result) {
727839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger                fFailures.push_back().set(desc);
737839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger            }
747839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger        }
757839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger
767839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger        // Proxy down to fReporter.  We assume these calls are threadsafe.
777839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger        virtual bool allowExtendedTest() const SK_OVERRIDE {
787839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger            return fReporter->allowExtendedTest();
797839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger        }
807839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger
817839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger        virtual bool allowThreaded() const SK_OVERRIDE {
827839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger            return fReporter->allowThreaded();
837839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger        }
847839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger
857839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger        virtual void bumpTestCount() SK_OVERRIDE {
867839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger            fReporter->bumpTestCount();
877839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger        }
887839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger
897839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    private:
907839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger        Reporter* fReporter;  // Unowned.
917839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger        SkTArray<SkString> fFailures;
927839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    };
937839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger}  // namespace
947839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger
957839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenbergervoid Test::run() {
967839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    // Clear the Skia error callback before running any test, to ensure that tests
977839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    // don't have unintended side effects when running more than one.
987839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    SkSetErrorCallback( NULL, NULL );
997839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger
1007839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    // Tell (likely shared) fReporter that this test has started.
10180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    fReporter->startTest(this);
1027839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger
1037839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    const SkMSec start = SkTime::GetMSecs();
1047839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    // Run the test into a LocalReporter so we know if it's passed or failed without interference
1057839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    // from other tests that might share fReporter.
1067839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    LocalReporter local(fReporter);
1077839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    this->onRun(&local);
1087839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    fPassed = local.failure_size() == 0;
1097839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    fElapsed = SkTime::GetMSecs() - start;
1107839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger
1117839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    // Now tell fReporter about any failures and wrap up.
1127839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    for (int i = 0; i < local.failure_size(); i++) {
1137839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger      fReporter->report(local.failure(i), Reporter::kFailed);
1147839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    }
11580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    fReporter->endTest(this);
1167839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger
11780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
11880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru///////////////////////////////////////////////////////////////////////////////
12080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
12180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#if SK_SUPPORT_GPU
122096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger#include "GrContextFactory.h"
123096defe64d408e54474fe19f418c95bf1a554fc7Derek SollenbergerGrContextFactory gGrContextFactory;
124363e546ed626b6dbbc42f5db87b3594bc0b5944bDerek Sollenberger#endif
125363e546ed626b6dbbc42f5db87b3594bc0b5944bDerek Sollenberger
126096defe64d408e54474fe19f418c95bf1a554fc7Derek SollenbergerGrContextFactory* GpuTest::GetGrContextFactory() {
127363e546ed626b6dbbc42f5db87b3594bc0b5944bDerek Sollenberger#if SK_SUPPORT_GPU
128096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    return &gGrContextFactory;
129096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger#else
130096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    return NULL;
131363e546ed626b6dbbc42f5db87b3594bc0b5944bDerek Sollenberger#endif
132363e546ed626b6dbbc42f5db87b3594bc0b5944bDerek Sollenberger}
133363e546ed626b6dbbc42f5db87b3594bc0b5944bDerek Sollenberger
134096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenbergervoid GpuTest::DestroyContexts() {
135363e546ed626b6dbbc42f5db87b3594bc0b5944bDerek Sollenberger#if SK_SUPPORT_GPU
136096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    gGrContextFactory.destroyContexts();
13780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
13880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
139