UnitTestFramework.cpp 4.7 KB

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