skia_test.cpp revision 391ca66276b27464f255c371e7e95f56f9042042
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(bool allowExtendedTest) 65 : fIndex(0) 66 , fTotal(0) 67 , fAllowExtendedTest(allowExtendedTest) { 68 } 69 70 void setIndexOfTotal(int index, int total) { 71 fIndex = index; 72 fTotal = total; 73 } 74 75 virtual bool allowExtendedTest() const { 76 return fAllowExtendedTest; 77 } 78 79protected: 80 virtual void onStart(Test* test) { 81 SkDebugf("[%d/%d] %s...\n", fIndex+1, fTotal, test->getName()); 82 } 83 virtual void onReport(const char desc[], Reporter::Result result) { 84 SkDebugf("\t%s: %s\n", result2string(result), desc); 85 } 86 virtual void onEnd(Test*) { 87 if (!this->getCurrSuccess()) { 88 SkDebugf("---- FAILED\n"); 89 } 90 } 91private: 92 int fIndex, fTotal; 93 bool fAllowExtendedTest; 94}; 95 96static const char* make_canonical_dir_path(const char* path, SkString* storage) { 97 if (path) { 98 // clean it up so it always has a trailing searator 99 size_t len = strlen(path); 100 if (0 == len) { 101 path = NULL; 102 } else if (SkPATH_SEPARATOR != path[len - 1]) { 103 // resize to len + 1, to make room for searator 104 storage->set(path, len + 1); 105 storage->writable_str()[len] = SkPATH_SEPARATOR; 106 path = storage->c_str(); 107 } 108 } 109 return path; 110} 111 112static SkString gTmpDir; 113 114const SkString& Test::GetTmpDir() { 115 return gTmpDir; 116} 117 118static SkString gResourcePath; 119 120const SkString& Test::GetResourcePath() { 121 return gResourcePath; 122} 123 124int tool_main(int argc, char** argv); 125int tool_main(int argc, char** argv) { 126#if SK_ENABLE_INST_COUNT 127 gPrintInstCount = true; 128#endif 129 bool allowExtendedTest = false; 130 bool verboseOutput = false; 131 132 SkGraphics::Init(); 133 134 const char* matchStr = NULL; 135 136 char* const* stop = argv + argc; 137 for (++argv; argv < stop; ++argv) { 138 if (0 == strcmp(*argv, "--match") || 0 == strcmp(*argv, "-m")) { 139 ++argv; 140 if (argv < stop && **argv) { 141 matchStr = *argv; 142 } else { 143 SkDebugf("no following argument to --match\n"); 144 return -1; 145 } 146 } else if (0 == strcmp(*argv, "--tmpDir") || 0 == strcmp(*argv, "-t")) { 147 ++argv; 148 if (argv < stop && **argv) { 149 make_canonical_dir_path(*argv, &gTmpDir); 150 } else { 151 SkDebugf("no following argument to --tmpDir\n"); 152 return -1; 153 } 154 } else if (0 == strcmp(*argv, "--resourcePath") || 0 == strcmp(*argv, "-i")) { 155 argv++; 156 if (argv < stop && **argv) { 157 make_canonical_dir_path(*argv, &gResourcePath); 158 } 159 } else if (0 == strcmp(*argv, "--extendedTest") || 0 == strcmp(*argv, "-x")) { 160 allowExtendedTest = true; 161 } else if (0 == strcmp(*argv, "--verbose") || 0 == strcmp(*argv, "-v")) { 162 verboseOutput = true; 163 } else { 164 if (0 != strcmp(*argv, "--help") && 0 != strcmp(*argv, "-h") 165 && 0 != strcmp(*argv, "-?")) { 166 SkDebugf("Unknown option %s. ", *argv); 167 } 168 SkDebugf("Skia UnitTests options are:\n"); 169 SkDebugf(" -m --match [test-name-substring]\n"); 170 SkDebugf(" -t --tmpDir [dir]\n"); 171 SkDebugf(" -i --resourcePath [dir]\n"); 172 SkDebugf(" -x --extendedTest\n"); 173 SkDebugf(" -v --verbose\n"); 174 return 1; 175 } 176 } 177 178 { 179 SkString header("Skia UnitTests:"); 180 if (matchStr) { 181 header.appendf(" --match %s", matchStr); 182 } 183 if (!gTmpDir.isEmpty()) { 184 header.appendf(" --tmpDir %s", gTmpDir.c_str()); 185 } 186 if (!gResourcePath.isEmpty()) { 187 header.appendf(" --resourcePath %s", gResourcePath.c_str()); 188 } 189#ifdef SK_DEBUG 190 header.append(" SK_DEBUG"); 191#else 192 header.append(" SK_RELEASE"); 193#endif 194#ifdef SK_SCALAR_IS_FIXED 195 header.append(" SK_SCALAR_IS_FIXED"); 196#else 197 header.append(" SK_SCALAR_IS_FLOAT"); 198#endif 199 SkDebugf("%s\n", header.c_str()); 200 } 201 202 DebugfReporter reporter(allowExtendedTest); 203 Iter iter(&reporter); 204 Test* test; 205 206 const int count = Iter::Count(); 207 int index = 0; 208 int failCount = 0; 209 int skipCount = 0; 210 while ((test = iter.next()) != NULL) { 211 reporter.setIndexOfTotal(index, count); 212 if (NULL != matchStr && !strstr(test->getName(), matchStr)) { 213 ++skipCount; 214 } else { 215 if (!test->run()) { 216 ++failCount; 217 } 218 } 219 SkDELETE(test); 220 index += 1; 221 } 222 223 SkDebugf("Finished %d tests, %d failures, %d skipped.\n", 224 count, failCount, skipCount); 225 int testCount = reporter.countTests(); 226 if (verboseOutput && testCount > 0) { 227 SkDebugf("Ran %d Internal tests.\n", testCount); 228 } 229#if SK_SUPPORT_GPU 230 231#if GR_CACHE_STATS 232 GrContext *gr = GpuTest::GetContext(); 233 234 gr->printCacheStats(); 235#endif 236 237#endif 238 239 SkGraphics::Term(); 240 GpuTest::DestroyContexts(); 241 242 return (failCount == 0) ? 0 : 1; 243} 244 245#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL) 246int main(int argc, char * const argv[]) { 247 return tool_main(argc, (char**) argv); 248} 249#endif 250