Home | History | Annotate | Download | only in tests
      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 "SkTLazy.h"
     11 
     12 #if SK_SUPPORT_GPU
     13 #include "GrContext.h"
     14 #include "gl/SkNativeGLContext.h"
     15 #else
     16 class GrContext;
     17 #endif
     18 
     19 SK_DEFINE_INST_COUNT(skiatest::Reporter)
     20 
     21 using namespace skiatest;
     22 
     23 Reporter::Reporter() {
     24     this->resetReporting();
     25 }
     26 
     27 void Reporter::resetReporting() {
     28     fCurrTest = NULL;
     29     fTestCount = 0;
     30     sk_bzero(fResultCount, sizeof(fResultCount));
     31 }
     32 
     33 void Reporter::startTest(Test* test) {
     34     SkASSERT(NULL == fCurrTest);
     35     fCurrTest = test;
     36     this->onStart(test);
     37     fTestCount += 1;
     38     fCurrTestSuccess = true;    // we're optimistic
     39 }
     40 
     41 void Reporter::report(const char desc[], Result result) {
     42     if (NULL == desc) {
     43         desc = "<no description>";
     44     }
     45     this->onReport(desc, result);
     46     fResultCount[result] += 1;
     47     if (kFailed == result) {
     48         fCurrTestSuccess = false;
     49     }
     50 }
     51 
     52 void Reporter::endTest(Test* test) {
     53     SkASSERT(test == fCurrTest);
     54     this->onEnd(test);
     55     fCurrTest = NULL;
     56 }
     57 
     58 ///////////////////////////////////////////////////////////////////////////////
     59 
     60 Test::Test() : fReporter(NULL) {}
     61 
     62 Test::~Test() {
     63     SkSafeUnref(fReporter);
     64 }
     65 
     66 void Test::setReporter(Reporter* r) {
     67     SkRefCnt_SafeAssign(fReporter, r);
     68 }
     69 
     70 const char* Test::getName() {
     71     if (fName.size() == 0) {
     72         this->onGetName(&fName);
     73     }
     74     return fName.c_str();
     75 }
     76 
     77 bool Test::run() {
     78     fReporter->startTest(this);
     79     this->onRun(fReporter);
     80     fReporter->endTest(this);
     81     return fReporter->getCurrSuccess();
     82 }
     83 
     84 ///////////////////////////////////////////////////////////////////////////////
     85 
     86 #if SK_SUPPORT_GPU
     87     static SkAutoTUnref<SkNativeGLContext> gGLContext;
     88     static SkAutoTUnref<GrContext> gGrContext;
     89 #endif
     90 
     91 void GpuTest::DestroyContext() {
     92 #if SK_SUPPORT_GPU
     93     // preserve this order, we want gGrContext destroyed before gGLContext
     94     gGrContext.reset(NULL);
     95     gGLContext.reset(NULL);
     96 #endif
     97 }
     98 
     99 
    100 GrContext* GpuTest::GetContext() {
    101 #if SK_SUPPORT_GPU
    102     if (NULL == gGrContext.get()) {
    103         gGLContext.reset(new SkNativeGLContext());
    104         if (gGLContext.get()->init(800, 600)) {
    105             GrBackendContext ctx = reinterpret_cast<GrBackendContext>(gGLContext.get()->gl());
    106             gGrContext.reset(GrContext::Create(kOpenGL_GrBackend, ctx));
    107         }
    108     }
    109     if (gGLContext.get()) {
    110         gGLContext.get()->makeCurrent();
    111     }
    112     return gGrContext.get();
    113 #else
    114     return NULL;
    115 #endif
    116 }
    117