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 using namespace skiatest; 23 24 Reporter::Reporter() : fTestCount(0) { 25 } 26 27 void Reporter::startTest(Test* test) { 28 this->bumpTestCount(); 29 this->onStart(test); 30 } 31 32 void Reporter::reportFailed(const SkString& desc) { 33 this->onReportFailed(desc); 34 } 35 36 void Reporter::endTest(Test* test) { 37 this->onEnd(test); 38 } 39 40 /////////////////////////////////////////////////////////////////////////////// 41 42 Test::Test() : fReporter(NULL), fPassed(true) {} 43 44 Test::~Test() { 45 SkSafeUnref(fReporter); 46 } 47 48 void Test::setReporter(Reporter* r) { 49 SkRefCnt_SafeAssign(fReporter, r); 50 } 51 52 const char* Test::getName() { 53 if (fName.size() == 0) { 54 this->onGetName(&fName); 55 } 56 return fName.c_str(); 57 } 58 59 namespace { 60 class LocalReporter : public Reporter { 61 public: 62 explicit LocalReporter(Reporter* reporterToMimic) : fReporter(reporterToMimic) {} 63 64 int failure_size() const { return fFailures.count(); } 65 const SkString& failure(int i) const { return fFailures[i]; } 66 67 protected: 68 void onReportFailed(const SkString& desc) SK_OVERRIDE { 69 fFailures.push_back(desc); 70 } 71 72 // Proxy down to fReporter. We assume these calls are threadsafe. 73 virtual bool allowExtendedTest() const SK_OVERRIDE { 74 return fReporter->allowExtendedTest(); 75 } 76 77 virtual bool allowThreaded() const SK_OVERRIDE { 78 return fReporter->allowThreaded(); 79 } 80 81 virtual void bumpTestCount() SK_OVERRIDE { 82 fReporter->bumpTestCount(); 83 } 84 85 virtual bool verbose() const SK_OVERRIDE { 86 return fReporter->verbose(); 87 } 88 89 private: 90 Reporter* fReporter; // Unowned. 91 SkTArray<SkString> fFailures; 92 }; 93 } // namespace 94 95 void 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.failure_size() == 0; 109 fElapsed = SkTime::GetMSecs() - start; 110 111 // Now tell fReporter about any failures and wrap up. 112 for (int i = 0; i < local.failure_size(); i++) { 113 fReporter->reportFailed(local.failure(i)); 114 } 115 fReporter->endTest(this); 116 117 } 118 119 /////////////////////////////////////////////////////////////////////////////// 120 121 #if SK_SUPPORT_GPU 122 #include "GrContextFactory.h" 123 GrContextFactory gGrContextFactory; 124 #endif 125 126 GrContextFactory* GpuTest::GetGrContextFactory() { 127 #if SK_SUPPORT_GPU 128 return &gGrContextFactory; 129 #else 130 return NULL; 131 #endif 132 } 133 134 void GpuTest::DestroyContexts() { 135 #if SK_SUPPORT_GPU 136 gGrContextFactory.destroyContexts(); 137 #endif 138 } 139