reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 1 | #include "SkGraphics.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 2 | #include "Test.h" |
| 3 | |
| 4 | using namespace skiatest; |
| 5 | |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 6 | // need to explicitly declare this, or we get some weird infinite loop llist |
| 7 | template TestRegistry* TestRegistry::gHead; |
| 8 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 9 | class Iter { |
| 10 | public: |
| 11 | Iter(Reporter* r) : fReporter(r) { |
| 12 | r->ref(); |
| 13 | fReg = TestRegistry::Head(); |
| 14 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 15 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 16 | ~Iter() { |
| 17 | fReporter->unref(); |
| 18 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 19 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 20 | Test* next() { |
| 21 | if (fReg) { |
| 22 | TestRegistry::Factory fact = fReg->factory(); |
| 23 | fReg = fReg->next(); |
| 24 | Test* test = fact(NULL); |
| 25 | test->setReporter(fReporter); |
| 26 | return test; |
| 27 | } |
| 28 | return NULL; |
| 29 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 30 | |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 31 | static int Count() { |
| 32 | const TestRegistry* reg = TestRegistry::Head(); |
| 33 | int count = 0; |
| 34 | while (reg) { |
| 35 | count += 1; |
| 36 | reg = reg->next(); |
| 37 | } |
| 38 | return count; |
| 39 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 40 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 41 | private: |
| 42 | Reporter* fReporter; |
| 43 | const TestRegistry* fReg; |
| 44 | }; |
| 45 | |
| 46 | static const char* result2string(Reporter::Result result) { |
| 47 | return result == Reporter::kPassed ? "passed" : "FAILED"; |
| 48 | } |
| 49 | |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 50 | class DebugfReporter : public Reporter { |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 51 | public: |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 52 | DebugfReporter(bool androidMode) : fAndroidMode(androidMode) {} |
| 53 | |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 54 | void setIndexOfTotal(int index, int total) { |
| 55 | fIndex = index; |
| 56 | fTotal = total; |
| 57 | } |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 58 | protected: |
| 59 | virtual void onStart(Test* test) { |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 60 | this->dumpState(test, kStarting_State); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 61 | } |
| 62 | virtual void onReport(const char desc[], Reporter::Result result) { |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 63 | if (!fAndroidMode) { |
| 64 | SkDebugf("\t%s: %s\n", result2string(result), desc); |
| 65 | } |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 66 | } |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 67 | virtual void onEnd(Test* test) { |
| 68 | this->dumpState(test, this->getCurrSuccess() ? |
| 69 | kSucceeded_State : kFailed_State); |
| 70 | } |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 71 | private: |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 72 | enum State { |
| 73 | kStarting_State = 1, |
| 74 | kSucceeded_State = 0, |
| 75 | kFailed_State = -2 |
| 76 | }; |
| 77 | |
| 78 | void dumpState(Test* test, State state) { |
| 79 | if (fAndroidMode) { |
| 80 | SkDebugf("INSTRUMENTATION_STATUS: test=%s\n", test->getName()); |
| 81 | SkDebugf("INSTRUMENTATION_STATUS: class=com.skia\n"); |
| 82 | SkDebugf("INSTRUMENTATION_STATUS: current=%d\n", fIndex+1); |
| 83 | SkDebugf("INSTRUMENTATION_STATUS: numtests=%d\n", fTotal); |
| 84 | SkDebugf("INSTRUMENTATION_STATUS_CODE: %d\n", state); |
| 85 | } else { |
| 86 | if (kStarting_State == state) { |
| 87 | SkDebugf("[%d/%d] %s...\n", fIndex+1, fTotal, test->getName()); |
| 88 | } else if (kFailed_State == state) { |
| 89 | SkDebugf("---- FAILED\n"); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 94 | int fIndex, fTotal; |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 95 | bool fAndroidMode; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | int main (int argc, char * const argv[]) { |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 99 | SkAutoGraphics ag; |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 100 | |
| 101 | bool androidMode = false; |
| 102 | for (int i = 1; i < argc; i++) { |
| 103 | if (!strcmp(argv[i], "-android")) { |
| 104 | androidMode = true; |
| 105 | } |
| 106 | } |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 107 | |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 108 | DebugfReporter reporter(androidMode); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 109 | Iter iter(&reporter); |
| 110 | Test* test; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 111 | |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 112 | const int count = Iter::Count(); |
| 113 | int index = 0; |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 114 | int successCount = 0; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 115 | while ((test = iter.next()) != NULL) { |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 116 | reporter.setIndexOfTotal(index, count); |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 117 | successCount += test->run(); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 118 | SkDELETE(test); |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 119 | index += 1; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 120 | } |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 121 | |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 122 | if (!androidMode) { |
| 123 | SkDebugf("Finished %d tests, %d failures.\n", count, |
| 124 | count - successCount); |
| 125 | } |
epoger@google.com | a8fd79d | 2011-05-13 19:26:48 +0000 | [diff] [blame] | 126 | return (count == successCount) ? 0 : 1; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 127 | } |