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 20class GrContext; 21#endif 22 23DEFINE_string2(tmpDir, t, NULL, "tmp directory for tests to use."); 24 25using namespace skiatest; 26 27Reporter::Reporter() : fTestCount(0) { 28} 29 30void Reporter::startTest(Test* test) { 31 this->onStart(test); 32} 33 34void Reporter::reportFailed(const SkString& desc) { 35 this->onReportFailed(desc); 36} 37 38void Reporter::endTest(Test* test) { 39 this->onEnd(test); 40} 41 42/////////////////////////////////////////////////////////////////////////////// 43 44Test::Test() : fReporter(NULL), fPassed(true) {} 45 46Test::~Test() { 47 SkSafeUnref(fReporter); 48} 49 50void Test::setReporter(Reporter* r) { 51 SkRefCnt_SafeAssign(fReporter, r); 52} 53 54const char* Test::getName() { 55 if (fName.size() == 0) { 56 this->onGetName(&fName); 57 } 58 return fName.c_str(); 59} 60 61class LocalReporter : public Reporter { 62public: 63 explicit LocalReporter(Reporter* reporterToMimic) : fReporter(reporterToMimic) {} 64 65 int numFailures() const { return fFailures.count(); } 66 const SkString& failure(int i) const { return fFailures[i]; } 67 68protected: 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. 74 virtual bool allowExtendedTest() const SK_OVERRIDE { 75 return fReporter->allowExtendedTest(); 76 } 77 78 virtual bool allowThreaded() const SK_OVERRIDE { 79 return fReporter->allowThreaded(); 80 } 81 82 virtual void bumpTestCount() SK_OVERRIDE { 83 fReporter->bumpTestCount(); 84 } 85 86 virtual bool verbose() const SK_OVERRIDE { 87 return fReporter->verbose(); 88 } 89 90private: 91 Reporter* fReporter; // Unowned. 92 SkTArray<SkString> fFailures; 93}; 94 95void 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 119SkString Test::GetTmpDir() { 120 const char* tmpDir = FLAGS_tmpDir.isEmpty() ? NULL : FLAGS_tmpDir[0]; 121 return SkString(tmpDir); 122} 123