UnitTestFramework.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt.h>
  4. #include <cstdarg>
  5. #include <Core/FPException.h>
  6. #include <RegisterTypes.h>
  7. #ifdef JPH_PLATFORM_ANDROID
  8. #include <Core/Color.h>
  9. #include <android/log.h>
  10. #include <android_native_app_glue.h>
  11. #endif // JPH_PLATFORM_ANDROID
  12. using namespace JPH;
  13. // Emit everything needed for the main function
  14. #define DOCTEST_CONFIG_IMPLEMENT
  15. #define DOCTEST_CONFIG_NO_WINDOWS_SEH
  16. #include "doctest.h"
  17. using namespace doctest;
  18. // Callback for traces
  19. static void TraceImpl(const char *inFMT, ...)
  20. {
  21. // Format the message
  22. va_list list;
  23. va_start(list, inFMT);
  24. char buffer[1024];
  25. vsnprintf(buffer, sizeof(buffer), inFMT, list);
  26. // Forward to doctest
  27. MESSAGE(buffer);
  28. }
  29. #ifdef JPH_ENABLE_ASSERTS
  30. // Callback for asserts
  31. static bool AssertFailedImpl(const char *inExpression, const char *inMessage, const char *inFile, uint inLine)
  32. {
  33. // Format message
  34. char buffer[1024];
  35. snprintf(buffer, sizeof(buffer), "%s:%u: (%s) %s", inFile, inLine, inExpression, inMessage != nullptr? inMessage : "");
  36. // Forward to doctest
  37. FAIL_CHECK(buffer);
  38. // No breakpoint
  39. return false;
  40. };
  41. #endif // JPH_ENABLE_ASSERTS
  42. #ifndef JPH_PLATFORM_ANDROID
  43. // Generic entry point
  44. int main(int argc, char** argv)
  45. {
  46. // Install callbacks
  47. Trace = TraceImpl;
  48. JPH_IF_ENABLE_ASSERTS(AssertFailed = AssertFailedImpl;)
  49. #if defined(JPH_PLATFORM_WINDOWS) && defined(_DEBUG)
  50. // Enable leak detection
  51. _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  52. #endif
  53. // Enable floating point exceptions
  54. FPExceptionsEnable enable_exceptions;
  55. JPH_UNUSED(enable_exceptions);
  56. // Register physics types
  57. RegisterTypes();
  58. return Context(argc, argv).run();
  59. }
  60. #else // !JPH_PLATFORM_ANDROID
  61. // Reporter that writes logs to the Android log
  62. struct LogReporter : public ConsoleReporter
  63. {
  64. LogReporter(const ContextOptions &inOptions) :
  65. ConsoleReporter(inOptions, mStream)
  66. {
  67. }
  68. #define REPORTER_OVERRIDE(func, type, arg) \
  69. void func(type arg) override \
  70. { \
  71. ConsoleReporter::func(arg); \
  72. const char *str = mStream.str().c_str(); \
  73. if (str[0] != 0) \
  74. __android_log_write(ANDROID_LOG_INFO, "Jolt", str); \
  75. mStream.str(""); \
  76. }
  77. REPORTER_OVERRIDE(test_run_start, DOCTEST_EMPTY, DOCTEST_EMPTY)
  78. REPORTER_OVERRIDE(test_run_end, const TestRunStats &, in)
  79. REPORTER_OVERRIDE(test_case_start, const TestCaseData &, in)
  80. REPORTER_OVERRIDE(test_case_reenter, const TestCaseData &, in)
  81. REPORTER_OVERRIDE(test_case_end, const CurrentTestCaseStats &, in)
  82. REPORTER_OVERRIDE(test_case_exception, const TestCaseException &, in)
  83. REPORTER_OVERRIDE(subcase_start, const SubcaseSignature &, in)
  84. REPORTER_OVERRIDE(subcase_end, DOCTEST_EMPTY, DOCTEST_EMPTY)
  85. REPORTER_OVERRIDE(log_assert, const AssertData &, in)
  86. REPORTER_OVERRIDE(log_message, const MessageData &, in)
  87. REPORTER_OVERRIDE(test_case_skipped, const TestCaseData &, in)
  88. private:
  89. thread_local static std::ostringstream mStream;
  90. };
  91. thread_local std::ostringstream LogReporter::mStream;
  92. DOCTEST_REGISTER_REPORTER("android_log", 0, LogReporter);
  93. void AndroidInitialize(android_app *inApp)
  94. {
  95. // Install callbacks
  96. Trace = TraceImpl;
  97. JPH_IF_ENABLE_ASSERTS(AssertFailed = AssertFailedImpl;)
  98. // Enable floating point exceptions
  99. FPExceptionsEnable enable_exceptions;
  100. JPH_UNUSED(enable_exceptions);
  101. // Register physics types
  102. RegisterTypes();
  103. // Run all tests
  104. Context context;
  105. context.addFilter("reporters", "android_log");
  106. int return_value = context.run();
  107. // Color the screen according to the test result
  108. JPH::Color color = return_value == 0? JPH::Color::sGreen : JPH::Color::sRed;
  109. ANativeWindow_acquire(inApp->window);
  110. ANativeWindow_Buffer buffer;
  111. ARect bounds;
  112. ANativeWindow_lock(inApp->window, &buffer, &bounds);
  113. switch (buffer.format)
  114. {
  115. case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
  116. case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
  117. {
  118. uint32 color_u32 = color.GetUInt32();
  119. for (int y = 0; y < buffer.height; ++y)
  120. {
  121. uint32 *dest = (uint32 *)((uint8 *)buffer.bits + y * buffer.stride * sizeof(uint32));
  122. for (int x = 0; x < buffer.width; ++x)
  123. *dest++ = color_u32;
  124. }
  125. break;
  126. }
  127. default:
  128. // TODO implement
  129. break;
  130. }
  131. ANativeWindow_unlockAndPost(inApp->window);
  132. ANativeWindow_release(inApp->window);
  133. }
  134. // Handle callback from Android
  135. void AndroidHandleCommand(android_app *inApp, int32_t inCmd)
  136. {
  137. switch (inCmd)
  138. {
  139. case APP_CMD_INIT_WINDOW:
  140. AndroidInitialize(inApp);
  141. break;
  142. }
  143. }
  144. // Main entry point for android
  145. void android_main(struct android_app *ioApp)
  146. {
  147. ioApp->onAppCmd = AndroidHandleCommand;
  148. int events;
  149. android_poll_source *source;
  150. do
  151. {
  152. if (ALooper_pollAll(1, nullptr, &events, (void **)&source) >= 0 && source != nullptr)
  153. source->process(ioApp, source);
  154. } while (ioApp->destroyRequested == 0);
  155. }
  156. #endif // JPH_PLATFORM_ANDROID