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 "SkString.h" 11 #include "SkTArray.h" 12 #include "SkTime.h" 13 #include "SkError.h" 14 15 #if SK_SUPPORT_GPU 16 #include "GrContext.h" 17 #include "gl/SkNativeGLContext.h" 18 #else 19 class GrContext; 20 #endif 21 22 SK_DEFINE_INST_COUNT(skiatest::Reporter) 23 24 using namespace skiatest; 25 26 Reporter::Reporter() : fTestCount(0) { 27 } 28 29 void Reporter::startTest(Test* test) { 30 this->bumpTestCount(); 31 this->onStart(test); 32 } 33 34 void Reporter::reportFailed(const SkString& desc) { 35 this->onReportFailed(desc); 36 } 37 38 void Reporter::endTest(Test* test) { 39 this->onEnd(test); 40 } 41 42 /////////////////////////////////////////////////////////////////////////////// 43 44 Test::Test() : fReporter(NULL), fPassed(true) {} 45 46 Test::~Test() { 47 SkSafeUnref(fReporter); 48 } 49 50 void Test::setReporter(Reporter* r) { 51 SkRefCnt_SafeAssign(fReporter, r); 52 } 53 54 const char* Test::getName() { 55 if (fName.size() == 0) { 56 this->onGetName(&fName); 57 } 58 return fName.c_str(); 59 } 60 61 namespace { 62 class LocalReporter : public Reporter { 63 public: 64 explicit LocalReporter(Reporter* reporterToMimic) : fReporter(reporterToMimic) {} 65 66 int failure_size() const { return fFailures.count(); } 67 const SkString& failure(int i) const { return fFailures[i]; } 68 69 protected: 70 void onReportFailed(const SkString& desc) SK_OVERRIDE { 71 fFailures.push_back(desc); 72 } 73 74 // Proxy down to fReporter. We assume these calls are threadsafe. 75 virtual bool allowExtendedTest() const SK_OVERRIDE { 76 return fReporter->allowExtendedTest(); 77 } 78 79 virtual bool allowThreaded() const SK_OVERRIDE { 80 return fReporter->allowThreaded(); 81 } 82 83 virtual void bumpTestCount() SK_OVERRIDE { 84 fReporter->bumpTestCount(); 85 } 86 87 virtual bool verbose() const SK_OVERRIDE { 88 return fReporter->verbose(); 89 } 90 91 private: 92 Reporter* fReporter; // Unowned. 93 SkTArray<SkString> fFailures; 94 }; 95 } // namespace 96 97 void Test::run() { 98 // Clear the Skia error callback before running any test, to ensure that tests 99 // don't have unintended side effects when running more than one. 100 SkSetErrorCallback( NULL, NULL ); 101 102 // Tell (likely shared) fReporter that this test has started. 103 fReporter->startTest(this); 104 105 const SkMSec start = SkTime::GetMSecs(); 106 // Run the test into a LocalReporter so we know if it's passed or failed without interference 107 // from other tests that might share fReporter. 108 LocalReporter local(fReporter); 109 this->onRun(&local); 110 fPassed = local.failure_size() == 0; 111 fElapsed = SkTime::GetMSecs() - start; 112 113 // Now tell fReporter about any failures and wrap up. 114 for (int i = 0; i < local.failure_size(); i++) { 115 fReporter->reportFailed(local.failure(i)); 116 } 117 fReporter->endTest(this); 118 119 } 120 121 /////////////////////////////////////////////////////////////////////////////// 122 123 #if SK_SUPPORT_GPU 124 #include "GrContextFactory.h" 125 GrContextFactory gGrContextFactory; 126 #endif 127 128 GrContextFactory* GpuTest::GetGrContextFactory() { 129 #if SK_SUPPORT_GPU 130 return &gGrContextFactory; 131 #else 132 return NULL; 133 #endif 134 } 135 136 void GpuTest::DestroyContexts() { 137 #if SK_SUPPORT_GPU 138 gGrContextFactory.destroyContexts(); 139 #endif 140 } 141