blob: d0fab416b7e60b1a2eca7ee875c58c3b5d4adb6f [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@google.com81e3d7f2011-06-01 12:42:36 +00008utils#include "SampleCode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#include "SkView.h"
10#include "SkCanvas.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011
12#include "test.h"
13
reed@google.com04863fa2011-05-15 04:08:24 +000014namespace skiatest {
rmistry@google.comae933ce2012-08-23 18:19:56 +000015
reed@google.com04863fa2011-05-15 04:08:24 +000016class MyReporter : public Reporter {
17protected:
18 virtual void onStart(Test* test) {}
19 virtual void onReport(const char desc[], Reporter::Result result) {
20 SkASSERT(Reporter::kPassed == result);
21 }
22 virtual void onEnd(Test* test) {}
23};
24
25class Iter {
26public:
27 Iter(Reporter* r) : fReporter(r) {
28 r->ref();
29 fReg = TestRegistry::Head();
30 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000031
reed@google.com04863fa2011-05-15 04:08:24 +000032 ~Iter() {
33 fReporter->unref();
34 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000035
reed@google.com04863fa2011-05-15 04:08:24 +000036 Test* next() {
37 if (fReg) {
38 TestRegistry::Factory fact = fReg->factory();
39 fReg = fReg->next();
40 Test* test = fact(NULL);
41 test->setReporter(fReporter);
42 return test;
43 }
44 return NULL;
45 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000046
reed@google.com04863fa2011-05-15 04:08:24 +000047 static int Count() {
48 const TestRegistry* reg = TestRegistry::Head();
49 int count = 0;
50 while (reg) {
51 count += 1;
52 reg = reg->next();
53 }
54 return count;
55 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000056
reed@google.com04863fa2011-05-15 04:08:24 +000057private:
58 Reporter* fReporter;
59 const TestRegistry* fReg;
60};
61}
62
reed@android.com8a1c16f2008-12-17 15:59:43 +000063class TestsView : public SkView {
64public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000065 TestsView() {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000066
67protected:
68 // overrides from SkEventSink
69 virtual bool onQuery(SkEvent* evt) {
70 if (SampleCode::TitleQ(*evt)) {
71 SampleCode::TitleR(evt, "Tests");
72 return true;
73 }
74 return this->INHERITED::onQuery(evt);
75 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000076
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 void drawBG(SkCanvas* canvas) {
78 canvas->drawColor(SK_ColorWHITE);
79 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000080
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 virtual void onDraw(SkCanvas* canvas) {
82 this->drawBG(canvas);
reed@google.com04863fa2011-05-15 04:08:24 +000083
84 skiatest::MyReporter reporter;
85 skiatest::Iter iter(&reporter);
86 skiatest::Test* test;
rmistry@google.comae933ce2012-08-23 18:19:56 +000087
reed@google.com04863fa2011-05-15 04:08:24 +000088 while ((test = iter.next()) != NULL) {
89 test->run();
90 SkDELETE(test);
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000093
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
95 this->inval(NULL);
rmistry@google.comae933ce2012-08-23 18:19:56 +000096
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 return this->INHERITED::onFindClickHandler(x, y);
98 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000099
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 virtual bool onClick(Click* click) {
101 this->inval(NULL);
102 return this->INHERITED::onClick(click);
103 }
104
rmistry@google.comae933ce2012-08-23 18:19:56 +0000105 virtual bool handleKey(SkKey key) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 this->inval(NULL);
107 return true;
108 }
109
110private:
111 typedef SkView INHERITED;
112};
113
114//////////////////////////////////////////////////////////////////////////////
115
116static SkView* MyFactory() { return new TestsView; }
117static SkViewRegister reg(MyFactory);