Test.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright 2011 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef skiatest_Test_DEFINED
  8. #define skiatest_Test_DEFINED
  9. #include "include/core/SkString.h"
  10. #include "include/core/SkTypes.h"
  11. #include "src/core/SkClipOpPriv.h"
  12. #include "src/core/SkTraceEvent.h"
  13. #include "tools/Registry.h"
  14. #include "tools/gpu/GrContextFactory.h"
  15. namespace skiatest {
  16. SkString GetTmpDir();
  17. struct Failure {
  18. Failure(const char* f, int l, const char* c, const SkString& m)
  19. : fileName(f), lineNo(l), condition(c), message(m) {}
  20. const char* fileName;
  21. int lineNo;
  22. const char* condition;
  23. SkString message;
  24. SkString toString() const;
  25. };
  26. class Reporter : SkNoncopyable {
  27. public:
  28. virtual ~Reporter() {}
  29. virtual void bumpTestCount();
  30. virtual void reportFailed(const skiatest::Failure&) = 0;
  31. virtual bool allowExtendedTest() const;
  32. virtual bool verbose() const;
  33. virtual void* stats() const { return nullptr; }
  34. void reportFailedWithContext(const skiatest::Failure&);
  35. void push(const SkString& message) {
  36. fContextStack.push_back(message);
  37. }
  38. void pop() {
  39. fContextStack.pop_back();
  40. }
  41. private:
  42. SkTArray<SkString> fContextStack;
  43. };
  44. #define REPORT_FAILURE(reporter, cond, message) \
  45. reporter->reportFailedWithContext(skiatest::Failure(__FILE__, __LINE__, cond, message))
  46. class ReporterContext : SkNoncopyable {
  47. public:
  48. ReporterContext(Reporter* reporter, const SkString& message) : fReporter(reporter) {
  49. fReporter->push(message);
  50. }
  51. ~ReporterContext() {
  52. fReporter->pop();
  53. }
  54. private:
  55. Reporter* fReporter;
  56. };
  57. typedef void (*TestProc)(skiatest::Reporter*, const GrContextOptions&);
  58. typedef void (*ContextOptionsProc)(GrContextOptions*);
  59. struct Test {
  60. Test(const char* n, bool g, TestProc p, ContextOptionsProc optionsProc = nullptr)
  61. : name(n), needsGpu(g), proc(p), fContextOptionsProc(optionsProc) {}
  62. const char* name;
  63. bool needsGpu;
  64. TestProc proc;
  65. ContextOptionsProc fContextOptionsProc;
  66. void modifyGrContextOptions(GrContextOptions* options) {
  67. if (fContextOptionsProc) {
  68. (*fContextOptionsProc)(options);
  69. }
  70. }
  71. void run(skiatest::Reporter* r, const GrContextOptions& options) const {
  72. TRACE_EVENT1("test", TRACE_FUNC, "name", this->name/*these are static*/);
  73. this->proc(r, options);
  74. }
  75. };
  76. typedef sk_tools::Registry<Test> TestRegistry;
  77. /*
  78. Use the following macros to make use of the skiatest classes, e.g.
  79. #include "tests/Test.h"
  80. DEF_TEST(TestName, reporter) {
  81. ...
  82. REPORTER_ASSERT(reporter, x == 15);
  83. ...
  84. REPORTER_ASSERT(reporter, x == 15, "x should be 15");
  85. ...
  86. if (x != 15) {
  87. ERRORF(reporter, "x should be 15, but is %d", x);
  88. return;
  89. }
  90. ...
  91. }
  92. */
  93. using GrContextFactoryContextType = sk_gpu_test::GrContextFactory::ContextType;
  94. typedef void GrContextTestFn(Reporter*, const sk_gpu_test::ContextInfo&);
  95. typedef bool GrContextTypeFilterFn(GrContextFactoryContextType);
  96. extern bool IsGLContextType(GrContextFactoryContextType);
  97. extern bool IsVulkanContextType(GrContextFactoryContextType);
  98. extern bool IsMetalContextType(GrContextFactoryContextType);
  99. extern bool IsRenderingGLContextType(GrContextFactoryContextType);
  100. extern bool IsMockContextType(GrContextFactoryContextType);
  101. void RunWithGPUTestContexts(GrContextTestFn*, GrContextTypeFilterFn*, Reporter*,
  102. const GrContextOptions&);
  103. /** Timer provides wall-clock duration since its creation. */
  104. class Timer {
  105. public:
  106. /** Starts the timer. */
  107. Timer();
  108. /** Nanoseconds since creation. */
  109. double elapsedNs() const;
  110. /** Milliseconds since creation. */
  111. double elapsedMs() const;
  112. /** Milliseconds since creation as an integer.
  113. Behavior is undefined for durations longer than SK_MSecMax.
  114. */
  115. SkMSec elapsedMsInt() const;
  116. private:
  117. double fStartNanos;
  118. };
  119. } // namespace skiatest
  120. #define REPORTER_ASSERT(r, cond, ...) \
  121. do { \
  122. if (!(cond)) { \
  123. REPORT_FAILURE(r, #cond, SkStringPrintf(__VA_ARGS__)); \
  124. } \
  125. } while (0)
  126. #define ERRORF(r, ...) \
  127. do { \
  128. REPORT_FAILURE(r, "", SkStringPrintf(__VA_ARGS__)); \
  129. } while (0)
  130. #define INFOF(REPORTER, ...) \
  131. do { \
  132. if ((REPORTER)->verbose()) { \
  133. SkDebugf(__VA_ARGS__); \
  134. } \
  135. } while (0)
  136. #define DEF_TEST(name, reporter) \
  137. static void test_##name(skiatest::Reporter*, const GrContextOptions&); \
  138. skiatest::TestRegistry name##TestRegistry(skiatest::Test(#name, false, test_##name)); \
  139. void test_##name(skiatest::Reporter* reporter, const GrContextOptions&)
  140. #define DEF_GPUTEST(name, reporter, options) \
  141. static void test_##name(skiatest::Reporter*, const GrContextOptions&); \
  142. skiatest::TestRegistry name##TestRegistry(skiatest::Test(#name, true, test_##name)); \
  143. void test_##name(skiatest::Reporter* reporter, const GrContextOptions& options)
  144. #define DEF_GPUTEST_FOR_CONTEXTS(name, context_filter, reporter, context_info, options_filter) \
  145. static void test_##name(skiatest::Reporter*, const sk_gpu_test::ContextInfo& context_info); \
  146. static void test_gpu_contexts_##name(skiatest::Reporter* reporter, \
  147. const GrContextOptions& options) { \
  148. skiatest::RunWithGPUTestContexts(test_##name, context_filter, reporter, options); \
  149. } \
  150. skiatest::TestRegistry name##TestRegistry( \
  151. skiatest::Test(#name, true, test_gpu_contexts_##name, options_filter)); \
  152. void test_##name(skiatest::Reporter* reporter, const sk_gpu_test::ContextInfo& context_info)
  153. #define DEF_GPUTEST_FOR_ALL_CONTEXTS(name, reporter, context_info) \
  154. DEF_GPUTEST_FOR_CONTEXTS(name, nullptr, reporter, context_info, nullptr)
  155. #define DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name, reporter, context_info) \
  156. DEF_GPUTEST_FOR_CONTEXTS(name, sk_gpu_test::GrContextFactory::IsRenderingContext, \
  157. reporter, context_info, nullptr)
  158. #define DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(name, reporter, context_info) \
  159. DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsGLContextType, \
  160. reporter, context_info, nullptr)
  161. #define DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(name, reporter, context_info) \
  162. DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsRenderingGLContextType, \
  163. reporter, context_info, nullptr)
  164. #define DEF_GPUTEST_FOR_MOCK_CONTEXT(name, reporter, context_info) \
  165. DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsMockContextType, \
  166. reporter, context_info, nullptr)
  167. #define DEF_GPUTEST_FOR_VULKAN_CONTEXT(name, reporter, context_info) \
  168. DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsVulkanContextType, \
  169. reporter, context_info, nullptr)
  170. #define DEF_GPUTEST_FOR_METAL_CONTEXT(name, reporter, context_info) \
  171. DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsMetalContextType, \
  172. reporter, context_info, nullptr)
  173. #define REQUIRE_PDF_DOCUMENT(TEST_NAME, REPORTER) \
  174. do { \
  175. SkNullWStream testStream; \
  176. auto testDoc = SkPDF::MakeDocument(&testStream); \
  177. if (!testDoc) { \
  178. INFOF(REPORTER, "PDF disabled; %s test skipped.", #TEST_NAME); \
  179. return; \
  180. } \
  181. } while (false)
  182. #endif