skia_test.cpp revision cb62650ecfd323a1ac215103a4d3106faad3c3cd
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 */
8#include "SkGraphics.h"
9#include "Test.h"
10#include "SkOSFile.h"
11
12#if SK_SUPPORT_GPU
13#include "GrContext.h"
14#endif
15
16using namespace skiatest;
17
18// need to explicitly declare this, or we get some weird infinite loop llist
19template TestRegistry* TestRegistry::gHead;
20
21class Iter {
22public:
23    Iter(Reporter* r) : fReporter(r) {
24        r->ref();
25        fReg = TestRegistry::Head();
26    }
27
28    ~Iter() {
29        fReporter->unref();
30    }
31
32    Test* next() {
33        if (fReg) {
34            TestRegistry::Factory fact = fReg->factory();
35            fReg = fReg->next();
36            Test* test = fact(NULL);
37            test->setReporter(fReporter);
38            return test;
39        }
40        return NULL;
41    }
42
43    static int Count() {
44        const TestRegistry* reg = TestRegistry::Head();
45        int count = 0;
46        while (reg) {
47            count += 1;
48            reg = reg->next();
49        }
50        return count;
51    }
52
53private:
54    Reporter* fReporter;
55    const TestRegistry* fReg;
56};
57
58static const char* result2string(Reporter::Result result) {
59    return result == Reporter::kPassed ? "passed" : "FAILED";
60}
61
62class DebugfReporter : public Reporter {
63public:
64    DebugfReporter() : fIndex(0), fTotal(0) {}
65
66    void setIndexOfTotal(int index, int total) {
67        fIndex = index;
68        fTotal = total;
69    }
70protected:
71    virtual void onStart(Test* test) {
72        SkDebugf("[%d/%d] %s...\n", fIndex+1, fTotal, test->getName());
73    }
74    virtual void onReport(const char desc[], Reporter::Result result) {
75        SkDebugf("\t%s: %s\n", result2string(result), desc);
76    }
77    virtual void onEnd(Test*) {
78        if (!this->getCurrSuccess()) {
79            SkDebugf("---- FAILED\n");
80        }
81    }
82private:
83    int fIndex, fTotal;
84};
85
86static const char* make_canonical_dir_path(const char* path, SkString* storage) {
87    if (path) {
88        // clean it up so it always has a trailing searator
89        size_t len = strlen(path);
90        if (0 == len) {
91            path = NULL;
92        } else if (SkPATH_SEPARATOR != path[len - 1]) {
93            // resize to len + 1, to make room for searator
94            storage->set(path, len + 1);
95            storage->writable_str()[len] = SkPATH_SEPARATOR;
96            path = storage->c_str();
97        }
98    }
99    return path;
100}
101
102static SkString gTmpDir;
103
104const SkString& Test::GetTmpDir() {
105    return gTmpDir;
106}
107
108static SkString gResourcePath;
109
110const SkString& Test::GetResourcePath() {
111    return gResourcePath;
112}
113
114int tool_main(int argc, char** argv);
115int tool_main(int argc, char** argv) {
116#if SK_ENABLE_INST_COUNT
117    gPrintInstCount = true;
118#endif
119    SkGraphics::Init();
120
121    const char* matchStr = NULL;
122
123    char* const* stop = argv + argc;
124    for (++argv; argv < stop; ++argv) {
125        if (strcmp(*argv, "--match") == 0) {
126            ++argv;
127            if (argv < stop && **argv) {
128                matchStr = *argv;
129            } else {
130                SkDebugf("no following argument to --match\n");
131                return -1;
132            }
133        } else if (0 == strcmp(*argv, "--tmpDir")) {
134            ++argv;
135            if (argv < stop && **argv) {
136                make_canonical_dir_path(*argv, &gTmpDir);
137            } else {
138                SkDebugf("no following argument to --tmpDir\n");
139                return -1;
140            }
141        } else if ((0 == strcmp(*argv, "--resourcePath")) ||
142                   (0 == strcmp(*argv, "-i"))) {
143            argv++;
144            if (argv < stop && **argv) {
145                make_canonical_dir_path(*argv, &gResourcePath);
146            }
147        }
148    }
149
150    {
151        SkString header("Skia UnitTests:");
152        if (matchStr) {
153            header.appendf(" --match %s", matchStr);
154        }
155        if (!gTmpDir.isEmpty()) {
156            header.appendf(" --tmpDir %s", gTmpDir.c_str());
157        }
158        if (!gResourcePath.isEmpty()) {
159            header.appendf(" --resourcePath %s", gResourcePath.c_str());
160        }
161#ifdef SK_DEBUG
162        header.append(" SK_DEBUG");
163#else
164        header.append(" SK_RELEASE");
165#endif
166#ifdef SK_SCALAR_IS_FIXED
167        header.append(" SK_SCALAR_IS_FIXED");
168#else
169        header.append(" SK_SCALAR_IS_FLOAT");
170#endif
171        SkDebugf("%s\n", header.c_str());
172    }
173
174    DebugfReporter reporter;
175    Iter iter(&reporter);
176    Test* test;
177
178    const int count = Iter::Count();
179    int index = 0;
180    int failCount = 0;
181    int skipCount = 0;
182    while ((test = iter.next()) != NULL) {
183        reporter.setIndexOfTotal(index, count);
184        if (NULL != matchStr && !strstr(test->getName(), matchStr)) {
185            ++skipCount;
186        } else {
187            if (!test->run()) {
188                ++failCount;
189            }
190        }
191        SkDELETE(test);
192        index += 1;
193    }
194
195    SkDebugf("Finished %d tests, %d failures, %d skipped.\n",
196             count, failCount, skipCount);
197
198#if SK_SUPPORT_GPU
199
200#if GR_CACHE_STATS
201    GrContext *gr = GpuTest::GetContext();
202
203    gr->printCacheStats();
204#endif
205
206#endif
207
208    SkGraphics::Term();
209    GpuTest::DestroyContexts();
210
211    return (failCount == 0) ? 0 : 1;
212}
213
214#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
215int main(int argc, char * const argv[]) {
216    return tool_main(argc, (char**) argv);
217}
218#endif
219