epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 8 | utils#include "SampleCode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #include "SkView.h" |
| 10 | #include "SkCanvas.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | |
| 12 | #include "test.h" |
| 13 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 14 | namespace skiatest { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 15 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 16 | class MyReporter : public Reporter { |
| 17 | protected: |
| 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 | |
| 25 | class Iter { |
| 26 | public: |
| 27 | Iter(Reporter* r) : fReporter(r) { |
| 28 | r->ref(); |
| 29 | fReg = TestRegistry::Head(); |
| 30 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 31 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 32 | ~Iter() { |
| 33 | fReporter->unref(); |
| 34 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 35 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 36 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 46 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 47 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 56 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 57 | private: |
| 58 | Reporter* fReporter; |
| 59 | const TestRegistry* fReg; |
| 60 | }; |
| 61 | } |
| 62 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | class TestsView : public SkView { |
| 64 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 65 | TestsView() {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | |
| 67 | protected: |
| 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 76 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 77 | void drawBG(SkCanvas* canvas) { |
| 78 | canvas->drawColor(SK_ColorWHITE); |
| 79 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 80 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | virtual void onDraw(SkCanvas* canvas) { |
| 82 | this->drawBG(canvas); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 83 | |
| 84 | skiatest::MyReporter reporter; |
| 85 | skiatest::Iter iter(&reporter); |
| 86 | skiatest::Test* test; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 87 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 88 | while ((test = iter.next()) != NULL) { |
| 89 | test->run(); |
| 90 | SkDELETE(test); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 93 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
| 95 | this->inval(NULL); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 96 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | return this->INHERITED::onFindClickHandler(x, y); |
| 98 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 99 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | virtual bool onClick(Click* click) { |
| 101 | this->inval(NULL); |
| 102 | return this->INHERITED::onClick(click); |
| 103 | } |
| 104 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 105 | virtual bool handleKey(SkKey key) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | this->inval(NULL); |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | typedef SkView INHERITED; |
| 112 | }; |
| 113 | |
| 114 | ////////////////////////////////////////////////////////////////////////////// |
| 115 | |
| 116 | static SkView* MyFactory() { return new TestsView; } |
| 117 | static SkViewRegister reg(MyFactory); |