blob: f931d6204effc0f2bf0c526b29ca660a7a0bd6f9 [file] [log] [blame]
reed@android.com5e5adfd2009-03-07 03:39:23 +00001#include "SkGraphics.h"
reed@android.comed673312009-02-27 16:24:51 +00002#include "Test.h"
3
4using namespace skiatest;
5
reed@android.comd252db02009-04-01 18:31:44 +00006// need to explicitly declare this, or we get some weird infinite loop llist
7template TestRegistry* TestRegistry::gHead;
8
reed@android.comed673312009-02-27 16:24:51 +00009class Iter {
10public:
11 Iter(Reporter* r) : fReporter(r) {
12 r->ref();
13 fReg = TestRegistry::Head();
14 }
reed@android.com80e39a72009-04-02 16:59:40 +000015
reed@android.comed673312009-02-27 16:24:51 +000016 ~Iter() {
17 fReporter->unref();
18 }
reed@android.com80e39a72009-04-02 16:59:40 +000019
reed@android.comed673312009-02-27 16:24:51 +000020 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.com80e39a72009-04-02 16:59:40 +000030
reed@android.com57b799e2009-04-01 20:26:42 +000031 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.com80e39a72009-04-02 16:59:40 +000040
reed@android.comed673312009-02-27 16:24:51 +000041private:
42 Reporter* fReporter;
43 const TestRegistry* fReg;
44};
45
46static const char* result2string(Reporter::Result result) {
47 return result == Reporter::kPassed ? "passed" : "FAILED";
48}
49
reed@android.comd252db02009-04-01 18:31:44 +000050class DebugfReporter : public Reporter {
reed@android.com57b799e2009-04-01 20:26:42 +000051public:
reed@android.comeeb3b7f2009-04-09 04:06:54 +000052 DebugfReporter(bool androidMode) : fAndroidMode(androidMode) {}
53
reed@android.com57b799e2009-04-01 20:26:42 +000054 void setIndexOfTotal(int index, int total) {
55 fIndex = index;
56 fTotal = total;
57 }
reed@android.comed673312009-02-27 16:24:51 +000058protected:
59 virtual void onStart(Test* test) {
reed@android.comeeb3b7f2009-04-09 04:06:54 +000060 this->dumpState(test, kStarting_State);
reed@android.comed673312009-02-27 16:24:51 +000061 }
62 virtual void onReport(const char desc[], Reporter::Result result) {
reed@android.comeeb3b7f2009-04-09 04:06:54 +000063 if (!fAndroidMode) {
64 SkDebugf("\t%s: %s\n", result2string(result), desc);
65 }
reed@android.comed673312009-02-27 16:24:51 +000066 }
reed@android.comeeb3b7f2009-04-09 04:06:54 +000067 virtual void onEnd(Test* test) {
68 this->dumpState(test, this->getCurrSuccess() ?
69 kSucceeded_State : kFailed_State);
70 }
reed@android.com57b799e2009-04-01 20:26:42 +000071private:
reed@android.comeeb3b7f2009-04-09 04:06:54 +000072 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.com57b799e2009-04-01 20:26:42 +000094 int fIndex, fTotal;
reed@android.comeeb3b7f2009-04-09 04:06:54 +000095 bool fAndroidMode;
reed@android.comed673312009-02-27 16:24:51 +000096};
97
98int main (int argc, char * const argv[]) {
reed@android.com5e5adfd2009-03-07 03:39:23 +000099 SkAutoGraphics ag;
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000100
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.com5e5adfd2009-03-07 03:39:23 +0000107
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000108 DebugfReporter reporter(androidMode);
reed@android.comed673312009-02-27 16:24:51 +0000109 Iter iter(&reporter);
110 Test* test;
reed@android.com80e39a72009-04-02 16:59:40 +0000111
reed@android.com57b799e2009-04-01 20:26:42 +0000112 const int count = Iter::Count();
113 int index = 0;
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000114 int successCount = 0;
reed@android.comed673312009-02-27 16:24:51 +0000115 while ((test = iter.next()) != NULL) {
reed@android.com57b799e2009-04-01 20:26:42 +0000116 reporter.setIndexOfTotal(index, count);
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000117 successCount += test->run();
reed@android.comed673312009-02-27 16:24:51 +0000118 SkDELETE(test);
reed@android.com57b799e2009-04-01 20:26:42 +0000119 index += 1;
reed@android.comed673312009-02-27 16:24:51 +0000120 }
reed@android.com57b799e2009-04-01 20:26:42 +0000121
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000122 if (!androidMode) {
123 SkDebugf("Finished %d tests, %d failures.\n", count,
124 count - successCount);
125 }
epoger@google.coma8fd79d2011-05-13 19:26:48 +0000126 return (count == successCount) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000127}