1#include "SkGraphics.h"
2#include "Test.h"
3
4using namespace skiatest;
5
6// need to explicitly declare this, or we get some weird infinite loop llist
7template TestRegistry* TestRegistry::gHead;
8
9class Iter {
10public:
11    Iter(Reporter* r) : fReporter(r) {
12        r->ref();
13        fReg = TestRegistry::Head();
14    }
15
16    ~Iter() {
17        fReporter->unref();
18    }
19
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    }
30
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    }
40
41private:
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
50class DebugfReporter : public Reporter {
51public:
52    DebugfReporter(bool androidMode) : fAndroidMode(androidMode) {}
53
54    void setIndexOfTotal(int index, int total) {
55        fIndex = index;
56        fTotal = total;
57    }
58protected:
59    virtual void onStart(Test* test) {
60        this->dumpState(test, kStarting_State);
61    }
62    virtual void onReport(const char desc[], Reporter::Result result) {
63        if (!fAndroidMode) {
64            SkDebugf("\t%s: %s\n", result2string(result), desc);
65        }
66    }
67    virtual void onEnd(Test* test) {
68        this->dumpState(test, this->getCurrSuccess() ?
69                        kSucceeded_State : kFailed_State);
70    }
71private:
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
94    int fIndex, fTotal;
95    bool fAndroidMode;
96};
97
98int main (int argc, char * const argv[]) {
99    SkAutoGraphics ag;
100
101    bool androidMode = false;
102    for (int i = 1; i < argc; i++) {
103        if (!strcmp(argv[i], "-android")) {
104            androidMode = true;
105        }
106    }
107
108    DebugfReporter reporter(androidMode);
109    Iter iter(&reporter);
110    Test* test;
111
112    const int count = Iter::Count();
113    int index = 0;
114    int successCount = 0;
115    while ((test = iter.next()) != NULL) {
116        reporter.setIndexOfTotal(index, count);
117        successCount += test->run();
118        SkDELETE(test);
119        index += 1;
120    }
121
122    if (!androidMode) {
123        SkDebugf("Finished %d tests, %d failures.\n", count,
124                 count - successCount);
125    }
126    return (count == successCount) ? 0 : 1;
127}
128