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 void bumpTestCount() SK_OVERRIDE { 79 fReporter->bumpTestCount(); 80 } 81 82 virtual bool verbose() const SK_OVERRIDE { 83 return fReporter->verbose(); 84 } 85 86private: 87 Reporter* fReporter; // Unowned. 88 SkTArray<SkString> fFailures; 89}; 90 91void Test::run() { 92 // Clear the Skia error callback before running any test, to ensure that tests 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 without 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 115SkString Test::GetTmpDir() { 116 const char* tmpDir = FLAGS_tmpDir.isEmpty() ? NULL : FLAGS_tmpDir[0]; 117 return SkString(tmpDir); 118} 119