TestsInterface.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "TestsInterface.h"
  29. #include "TypesToString.h"
  30. #include <RmlUi/Core/Log.h>
  31. #include <RmlUi/Core/StringUtilities.h>
  32. #include <doctest.h>
  33. TestsSystemInterface::~TestsSystemInterface()
  34. {
  35. SetNumExpectedWarnings(0);
  36. }
  37. double TestsSystemInterface::GetElapsedTime()
  38. {
  39. if (manual_time)
  40. return elapsed_time;
  41. return Rml::SystemInterface::GetElapsedTime();
  42. }
  43. bool TestsSystemInterface::LogMessage(Rml::Log::Type type, const Rml::String& message)
  44. {
  45. static const char* message_type_str[Rml::Log::Type::LT_MAX] = {"Always", "Error", "Assert", "Warning", "Info", "Debug"};
  46. const bool result = Rml::SystemInterface::LogMessage(type, message);
  47. if (type <= Rml::Log::Type::LT_WARNING)
  48. {
  49. const Rml::String warning = "RmlUi " + Rml::String(message_type_str[type]) + ": " + message;
  50. if (num_expected_warnings > 0)
  51. {
  52. num_logged_warnings += 1;
  53. warnings.push_back(warning);
  54. }
  55. else
  56. {
  57. FAIL_CHECK(warning);
  58. }
  59. }
  60. return result;
  61. }
  62. void TestsSystemInterface::SetNumExpectedWarnings(int in_num_expected_warnings)
  63. {
  64. if (num_expected_warnings > 0)
  65. {
  66. // Check and clear previous warnings
  67. if (num_logged_warnings != num_expected_warnings)
  68. {
  69. Rml::String str = "Got unexpected number of warnings: \n";
  70. Rml::StringUtilities::JoinString(str, warnings, '\n');
  71. if (warnings.empty())
  72. str += "(no warnings logged)";
  73. CHECK_MESSAGE(num_logged_warnings == num_expected_warnings, str);
  74. }
  75. num_expected_warnings = 0;
  76. num_logged_warnings = 0;
  77. warnings.clear();
  78. }
  79. num_expected_warnings = in_num_expected_warnings;
  80. }
  81. void TestsSystemInterface::SetManualTime(double t)
  82. {
  83. manual_time = true;
  84. elapsed_time = t;
  85. }
  86. void TestsSystemInterface::Reset()
  87. {
  88. SetManualTime(0);
  89. manual_time = false;
  90. SetNumExpectedWarnings(0);
  91. }
  92. Rml::CompiledGeometryHandle TestsRenderInterface::CompileGeometry(Rml::Span<const Rml::Vertex> vertices, Rml::Span<const int> indices)
  93. {
  94. counters.compile_geometry += 1;
  95. if (meshes_set)
  96. {
  97. INFO("Got vertices:\n", vertices);
  98. INFO("Got indices:\n", indices);
  99. REQUIRE_MESSAGE(!meshes.empty(), "No CompileGeometry expected, but one was passed to us");
  100. Rml::Mesh mesh = std::move(meshes.front());
  101. meshes.erase(meshes.begin());
  102. INFO("Expected mesh:\n", mesh);
  103. CHECK(mesh.vertices.size() == vertices.size());
  104. CHECK(mesh.indices.size() == indices.size());
  105. for (size_t i = 0; i < mesh.vertices.size(); i++)
  106. {
  107. CHECK(mesh.vertices[i].position == vertices[i].position);
  108. CHECK(mesh.vertices[i].colour == vertices[i].colour);
  109. CHECK(mesh.vertices[i].tex_coord == vertices[i].tex_coord);
  110. }
  111. for (size_t i = 0; i < mesh.indices.size(); i++)
  112. {
  113. CHECK(mesh.indices[i] == indices[i]);
  114. }
  115. }
  116. return Rml::CompiledGeometryHandle(counters.compile_geometry);
  117. }
  118. void TestsRenderInterface::RenderGeometry(Rml::CompiledGeometryHandle /*geometry*/, Rml::Vector2f /*translation*/, Rml::TextureHandle /*texture*/)
  119. {
  120. counters.render_geometry += 1;
  121. }
  122. void TestsRenderInterface::ReleaseGeometry(Rml::CompiledGeometryHandle /*geometry*/)
  123. {
  124. counters.release_geometry += 1;
  125. }
  126. void TestsRenderInterface::EnableScissorRegion(bool /*enable*/)
  127. {
  128. counters.enable_scissor += 1;
  129. }
  130. void TestsRenderInterface::SetScissorRegion(Rml::Rectanglei /*region*/)
  131. {
  132. counters.set_scissor += 1;
  133. }
  134. void TestsRenderInterface::EnableClipMask(bool /*enable*/)
  135. {
  136. counters.enable_clip_mask += 1;
  137. }
  138. void TestsRenderInterface::RenderToClipMask(Rml::ClipMaskOperation /*mask_operation*/, Rml::CompiledGeometryHandle /*geometry*/,
  139. Rml::Vector2f /*translation*/)
  140. {
  141. counters.render_to_clip_mask += 1;
  142. }
  143. Rml::TextureHandle TestsRenderInterface::LoadTexture(Rml::Vector2i& texture_dimensions, const Rml::String& source)
  144. {
  145. counters.load_texture += 1;
  146. if (source.find("invalid") != Rml::String::npos)
  147. return 0;
  148. texture_dimensions.x = 512;
  149. texture_dimensions.y = 256;
  150. return 1;
  151. }
  152. Rml::TextureHandle TestsRenderInterface::GenerateTexture(Rml::Span<const Rml::byte> /*source*/, Rml::Vector2i /*source_dimensions*/)
  153. {
  154. counters.generate_texture += 1;
  155. return 1;
  156. }
  157. void TestsRenderInterface::ReleaseTexture(Rml::TextureHandle /*texture_handle*/)
  158. {
  159. counters.release_texture += 1;
  160. }
  161. void TestsRenderInterface::SetTransform(const Rml::Matrix4f* /*transform*/)
  162. {
  163. counters.set_transform += 1;
  164. }
  165. Rml::CompiledFilterHandle TestsRenderInterface::CompileFilter(const Rml::String& /*name*/, const Rml::Dictionary& /*parameters*/)
  166. {
  167. counters.compile_filter += 1;
  168. return 1;
  169. }
  170. void TestsRenderInterface::ReleaseFilter(Rml::CompiledFilterHandle /*filter*/)
  171. {
  172. counters.release_filter += 1;
  173. }
  174. Rml::CompiledShaderHandle TestsRenderInterface::CompileShader(const Rml::String& /*name*/, const Rml::Dictionary& /*parameters*/)
  175. {
  176. counters.compile_shader += 1;
  177. return 1;
  178. }
  179. void TestsRenderInterface::RenderShader(Rml::CompiledShaderHandle /*shader*/, Rml::CompiledGeometryHandle /*geometry*/, Rml::Vector2f /*translation*/,
  180. Rml::TextureHandle /*texture*/)
  181. {
  182. counters.render_shader += 1;
  183. }
  184. void TestsRenderInterface::ReleaseShader(Rml::CompiledShaderHandle /*shader*/)
  185. {
  186. counters.release_shader += 1;
  187. }
  188. void TestsRenderInterface::ResetCounters()
  189. {
  190. counters_from_previous_reset = std::exchange(counters, Counters());
  191. }
  192. void TestsRenderInterface::ExpectCompileGeometry(Rml::Vector<Rml::Mesh> in_meshes)
  193. {
  194. VerifyMeshes();
  195. meshes = std::move(in_meshes);
  196. meshes_set = true;
  197. }
  198. void TestsRenderInterface::Reset()
  199. {
  200. VerifyMeshes();
  201. meshes_set = false;
  202. ResetCounters();
  203. }
  204. void TestsRenderInterface::VerifyMeshes()
  205. {
  206. if (!meshes.empty())
  207. {
  208. // Use FAIL instead of REQUIRE here as this may be executed outside the context of a doctest test case.
  209. FAIL("CompileGeometry: Expected meshes not passed to us");
  210. }
  211. }