TestsInterface.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. return elapsed_time;
  40. }
  41. bool TestsSystemInterface::LogMessage(Rml::Log::Type type, const Rml::String& message)
  42. {
  43. static const char* message_type_str[Rml::Log::Type::LT_MAX] = {"Always", "Error", "Assert", "Warning", "Info", "Debug"};
  44. const bool result = Rml::SystemInterface::LogMessage(type, message);
  45. if (type <= Rml::Log::Type::LT_WARNING)
  46. {
  47. const Rml::String warning = "RmlUi " + Rml::String(message_type_str[type]) + ": " + message;
  48. if (num_expected_warnings > 0)
  49. {
  50. num_logged_warnings += 1;
  51. warnings.push_back(warning);
  52. }
  53. else
  54. {
  55. FAIL_CHECK(warning);
  56. }
  57. }
  58. return result;
  59. }
  60. void TestsSystemInterface::SetNumExpectedWarnings(int in_num_expected_warnings)
  61. {
  62. if (num_expected_warnings > 0)
  63. {
  64. // Check and clear previous warnings
  65. if (num_logged_warnings != num_expected_warnings)
  66. {
  67. Rml::String str = "Got unexpected number of warnings: \n";
  68. Rml::StringUtilities::JoinString(str, warnings, '\n');
  69. if (warnings.empty())
  70. str += "(no warnings logged)";
  71. CHECK_MESSAGE(num_logged_warnings == num_expected_warnings, str);
  72. }
  73. num_expected_warnings = 0;
  74. num_logged_warnings = 0;
  75. warnings.clear();
  76. }
  77. num_expected_warnings = in_num_expected_warnings;
  78. }
  79. void TestsSystemInterface::SetTime(double t)
  80. {
  81. elapsed_time = t;
  82. }
  83. Rml::CompiledGeometryHandle TestsRenderInterface::CompileGeometry(Rml::Span<const Rml::Vertex> vertices, Rml::Span<const int> indices)
  84. {
  85. counters.compile_geometry += 1;
  86. if (meshes_set)
  87. {
  88. INFO("Got vertices:\n", vertices);
  89. INFO("Got indices:\n", indices);
  90. REQUIRE_MESSAGE(!meshes.empty(), "No CompileGeometry expected, but one was passed to us");
  91. Rml::Mesh mesh = std::move(meshes.front());
  92. meshes.erase(meshes.begin());
  93. INFO("Expected mesh:\n", mesh);
  94. CHECK(mesh.vertices.size() == vertices.size());
  95. CHECK(mesh.indices.size() == indices.size());
  96. for (size_t i = 0; i < mesh.vertices.size(); i++)
  97. {
  98. CHECK(mesh.vertices[i].position == vertices[i].position);
  99. CHECK(mesh.vertices[i].colour == vertices[i].colour);
  100. CHECK(mesh.vertices[i].tex_coord == vertices[i].tex_coord);
  101. }
  102. for (size_t i = 0; i < mesh.indices.size(); i++)
  103. {
  104. CHECK(mesh.indices[i] == indices[i]);
  105. }
  106. }
  107. return Rml::CompiledGeometryHandle(counters.compile_geometry);
  108. }
  109. void TestsRenderInterface::RenderGeometry(Rml::CompiledGeometryHandle /*geometry*/, Rml::Vector2f /*translation*/, Rml::TextureHandle /*texture*/)
  110. {
  111. counters.render_geometry += 1;
  112. }
  113. void TestsRenderInterface::ReleaseGeometry(Rml::CompiledGeometryHandle /*geometry*/)
  114. {
  115. counters.release_geometry += 1;
  116. }
  117. void TestsRenderInterface::EnableScissorRegion(bool /*enable*/)
  118. {
  119. counters.enable_scissor += 1;
  120. }
  121. void TestsRenderInterface::SetScissorRegion(Rml::Rectanglei /*region*/)
  122. {
  123. counters.set_scissor += 1;
  124. }
  125. void TestsRenderInterface::EnableClipMask(bool /*enable*/)
  126. {
  127. counters.enable_clip_mask += 1;
  128. }
  129. void TestsRenderInterface::RenderToClipMask(Rml::ClipMaskOperation /*mask_operation*/, Rml::CompiledGeometryHandle /*geometry*/,
  130. Rml::Vector2f /*translation*/)
  131. {
  132. counters.render_to_clip_mask += 1;
  133. }
  134. Rml::TextureHandle TestsRenderInterface::LoadTexture(Rml::Vector2i& texture_dimensions, const Rml::String& /*source*/)
  135. {
  136. counters.load_texture += 1;
  137. texture_dimensions.x = 512;
  138. texture_dimensions.y = 256;
  139. return 1;
  140. }
  141. Rml::TextureHandle TestsRenderInterface::GenerateTexture(Rml::Span<const Rml::byte> /*source*/, Rml::Vector2i /*source_dimensions*/)
  142. {
  143. counters.generate_texture += 1;
  144. return 1;
  145. }
  146. void TestsRenderInterface::ReleaseTexture(Rml::TextureHandle /*texture_handle*/)
  147. {
  148. counters.release_texture += 1;
  149. }
  150. void TestsRenderInterface::SetTransform(const Rml::Matrix4f* /*transform*/)
  151. {
  152. counters.set_transform += 1;
  153. }
  154. Rml::CompiledFilterHandle TestsRenderInterface::CompileFilter(const Rml::String& /*name*/, const Rml::Dictionary& /*parameters*/)
  155. {
  156. counters.compile_filter += 1;
  157. return 1;
  158. }
  159. void TestsRenderInterface::ReleaseFilter(Rml::CompiledFilterHandle /*filter*/)
  160. {
  161. counters.release_filter += 1;
  162. }
  163. Rml::CompiledShaderHandle TestsRenderInterface::CompileShader(const Rml::String& /*name*/, const Rml::Dictionary& /*parameters*/)
  164. {
  165. counters.compile_shader += 1;
  166. return 1;
  167. }
  168. void TestsRenderInterface::RenderShader(Rml::CompiledShaderHandle /*shader*/, Rml::CompiledGeometryHandle /*geometry*/, Rml::Vector2f /*translation*/,
  169. Rml::TextureHandle /*texture*/)
  170. {
  171. counters.render_shader += 1;
  172. }
  173. void TestsRenderInterface::ReleaseShader(Rml::CompiledShaderHandle /*shader*/)
  174. {
  175. counters.release_shader += 1;
  176. }
  177. void TestsRenderInterface::ResetCounters()
  178. {
  179. counters_from_previous_reset = std::exchange(counters, Counters());
  180. }
  181. void TestsRenderInterface::ExpectCompileGeometry(Rml::Vector<Rml::Mesh> in_meshes)
  182. {
  183. VerifyMeshes();
  184. meshes = std::move(in_meshes);
  185. meshes_set = true;
  186. }
  187. void TestsRenderInterface::Reset()
  188. {
  189. VerifyMeshes();
  190. meshes_set = false;
  191. ResetCounters();
  192. }
  193. void TestsRenderInterface::VerifyMeshes()
  194. {
  195. REQUIRE_MESSAGE(meshes.empty(), "CompileGeometry: Expected meshes not passed to us");
  196. }