blob: 2cbd00b594edf062df01888fbc244f7f3ebc3864 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@android.comed673312009-02-27 16:24:51 +00008#ifndef skiatest_Test_DEFINED
9#define skiatest_Test_DEFINED
10
11#include "SkRefCnt.h"
12#include "SkString.h"
13#include "SkTRegistry.h"
14
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000015class GrContext;
bsalomon@google.come2953132011-10-13 13:33:08 +000016class SkGLContext;
bsalomon@google.coma8e686e2011-08-16 15:45:58 +000017
reed@android.comed673312009-02-27 16:24:51 +000018namespace skiatest {
reed@android.com80e39a72009-04-02 16:59:40 +000019
reed@android.comed673312009-02-27 16:24:51 +000020 class Test;
21
22 class Reporter : public SkRefCnt {
23 public:
robertphillips@google.coma22e2112012-08-16 14:58:06 +000024 SK_DECLARE_INST_COUNT(Reporter)
reed@android.comed673312009-02-27 16:24:51 +000025 Reporter();
26
27 enum Result {
28 kPassed, // must begin with 0
29 kFailed,
30 /////
31 kLastResult = kFailed
32 };
reed@android.com80e39a72009-04-02 16:59:40 +000033
reed@android.comed673312009-02-27 16:24:51 +000034 void resetReporting();
35 int countTests() const { return fTestCount; }
36 int countResults(Result r) {
37 SkASSERT((unsigned)r <= kLastResult);
38 return fResultCount[r];
39 }
40
41 void startTest(Test*);
42 void report(const char testDesc[], Result);
43 void endTest(Test*);
reed@android.com80e39a72009-04-02 16:59:40 +000044
reed@android.comed673312009-02-27 16:24:51 +000045 // helpers for tests
46 void assertTrue(bool cond, const char desc[]) {
47 if (!cond) {
48 this->report(desc, kFailed);
49 }
50 }
51 void assertFalse(bool cond, const char desc[]) {
52 if (cond) {
53 this->report(desc, kFailed);
54 }
55 }
56 void reportFailed(const char desc[]) {
57 this->report(desc, kFailed);
58 }
59 void reportFailed(const SkString& desc) {
60 this->report(desc.c_str(), kFailed);
61 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000062
reed@android.comeeb3b7f2009-04-09 04:06:54 +000063 bool getCurrSuccess() const {
64 return fCurrTestSuccess;
65 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000066
reed@android.comed673312009-02-27 16:24:51 +000067 protected:
68 virtual void onStart(Test*) {}
69 virtual void onReport(const char desc[], Result) {}
70 virtual void onEnd(Test*) {}
reed@android.com80e39a72009-04-02 16:59:40 +000071
reed@android.comed673312009-02-27 16:24:51 +000072 private:
73 Test* fCurrTest;
74 int fTestCount;
75 int fResultCount[kLastResult+1];
reed@android.comeeb3b7f2009-04-09 04:06:54 +000076 bool fCurrTestSuccess;
reed@android.com80e39a72009-04-02 16:59:40 +000077
reed@android.comed673312009-02-27 16:24:51 +000078 typedef SkRefCnt INHERITED;
79 };
reed@android.com80e39a72009-04-02 16:59:40 +000080
reed@android.comed673312009-02-27 16:24:51 +000081 class Test {
reed@android.com80e39a72009-04-02 16:59:40 +000082 public:
reed@android.comed673312009-02-27 16:24:51 +000083 Test();
84 virtual ~Test();
85
86 Reporter* getReporter() const { return fReporter; }
87 void setReporter(Reporter*);
reed@android.com80e39a72009-04-02 16:59:40 +000088
reed@android.comed673312009-02-27 16:24:51 +000089 const char* getName();
reed@android.comeeb3b7f2009-04-09 04:06:54 +000090 bool run(); // returns true on success
reed@android.com80e39a72009-04-02 16:59:40 +000091
reed@android.comed673312009-02-27 16:24:51 +000092 protected:
93 virtual void onGetName(SkString*) = 0;
94 virtual void onRun(Reporter*) = 0;
reed@android.com80e39a72009-04-02 16:59:40 +000095
reed@android.comed673312009-02-27 16:24:51 +000096 private:
97 Reporter* fReporter;
98 SkString fName;
99 };
100
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000101 class GpuTest : public Test{
102 public:
103 GpuTest() : Test() {
104 fContext = GetContext();
105 }
robertphillips@google.combdb1be52012-09-07 18:24:43 +0000106 static GrContext* GetContext();
djsollen@google.com0945bde2012-11-29 15:28:45 +0000107 static void DestroyContext();
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000108 protected:
109 GrContext* fContext;
110 private:
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000111 };
112
reed@android.comed673312009-02-27 16:24:51 +0000113 typedef SkTRegistry<Test*, void*> TestRegistry;
114}
115
116#define REPORTER_ASSERT(r, cond) \
117 do { \
118 if (!(cond)) { \
119 SkString desc; \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000120 desc.printf("%s:%d: %s", __FILE__, __LINE__, #cond); \
reed@android.comed673312009-02-27 16:24:51 +0000121 r->reportFailed(desc); \
122 } \
123 } while(0)
124
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000125#define REPORTER_ASSERT_MESSAGE(r, cond, message) \
126 do { \
127 if (!(cond)) { \
128 SkString desc; \
129 desc.printf("%s %s:%d: %s", message, __FILE__, __LINE__, #cond); \
130 r->reportFailed(desc); \
131 } \
132 } while(0)
133
reed@android.comed673312009-02-27 16:24:51 +0000134
135#endif