Core.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 "../Common/TestsInterface.h"
  29. #include "../Common/TestsShell.h"
  30. #include <RmlUi/Core/Context.h>
  31. #include <RmlUi/Core/Core.h>
  32. #include <RmlUi/Core/Element.h>
  33. #include <RmlUi/Core/ElementDocument.h>
  34. #include <algorithm>
  35. #include <doctest.h>
  36. using namespace Rml;
  37. static const String document_textures_rml = R"(
  38. <rml>
  39. <head>
  40. <title>Test</title>
  41. <link type="text/rcss" href="/assets/rml.rcss"/>
  42. <style>
  43. body {
  44. left: 0;
  45. top: 0;
  46. right: 0;
  47. bottom: 0;
  48. }
  49. div.file {
  50. height: 100px;
  51. decorator: image(/assets/high_scores_alien_2.tga);
  52. }
  53. @spritesheet aliens {
  54. src: /assets/high_scores_alien_3.tga;
  55. alien3: 0px 0px 64px 64px;
  56. }
  57. div.sprite {
  58. height: 100px;
  59. decorator: image(alien3);
  60. }
  61. progress {
  62. display: block;
  63. width: 50px;
  64. height: 50px;
  65. background: #333;
  66. fill-image: /assets/high_scores_defender.tga;
  67. }
  68. </style>
  69. </head>
  70. <body>
  71. <div style="display: none">
  72. <img src="/assets/high_scores_alien_1.tga"/>
  73. <div class="file"/>
  74. <div class="sprite"/>
  75. <progress direction="clockwise" start-edge="bottom" value="0.5"/>
  76. </div>
  77. </body>
  78. </rml>
  79. )";
  80. static inline StringList GetSortedTextureSourceList()
  81. {
  82. StringList list = Rml::GetTextureSourceList();
  83. std::sort(list.begin(), list.end());
  84. return list;
  85. }
  86. TEST_CASE("core.texture_source_list")
  87. {
  88. Context* context = TestsShell::GetContext();
  89. REQUIRE(context);
  90. REQUIRE(GetSortedTextureSourceList().size() == 0);
  91. // We should be able to detect all sources even though they are hidden.
  92. const StringList list_expected = {
  93. "assets/high_scores_alien_1.tga",
  94. "assets/high_scores_alien_2.tga",
  95. "assets/high_scores_alien_3.tga",
  96. "assets/high_scores_defender.tga",
  97. };
  98. ElementDocument* document = context->LoadDocumentFromMemory(document_textures_rml);
  99. REQUIRE(document);
  100. CHECK(GetSortedTextureSourceList() == list_expected);
  101. document->Show();
  102. CHECK(GetSortedTextureSourceList() == list_expected);
  103. context->Update();
  104. CHECK(GetSortedTextureSourceList() == list_expected);
  105. context->Render();
  106. CHECK(GetSortedTextureSourceList() == list_expected);
  107. TestsShell::RenderLoop();
  108. document->Close();
  109. TestsShell::ShutdownShell();
  110. }
  111. TEST_CASE("core.release_resources")
  112. {
  113. TestsRenderInterface* render_interface = TestsShell::GetTestsRenderInterface();
  114. // This test only works with the dummy renderer.
  115. if (!render_interface)
  116. return;
  117. const auto& counters = render_interface->GetCounters();
  118. Context* context = TestsShell::GetContext();
  119. REQUIRE(context);
  120. ElementDocument* document = context->LoadDocument("assets/demo.rml");
  121. document->Show();
  122. TestsShell::RenderLoop();
  123. Element* element = document->GetElementById("content");
  124. SUBCASE("ReleaseTextures")
  125. {
  126. const auto startup_counters = counters;
  127. REQUIRE(counters.load_texture > 0);
  128. REQUIRE(counters.generate_texture > 0);
  129. REQUIRE(counters.release_texture == 0);
  130. // Release all textures and verify that the render interface received the release call.
  131. Rml::ReleaseTextures();
  132. CHECK(counters.load_texture == startup_counters.load_texture);
  133. CHECK(counters.generate_texture == startup_counters.generate_texture);
  134. CHECK(counters.release_texture == startup_counters.generate_texture + startup_counters.load_texture);
  135. const size_t num_released_textures = counters.release_texture;
  136. // By doing a new context Update+Render the textures should be loaded again.
  137. TestsShell::RenderLoop();
  138. CHECK(counters.load_texture == 2 * startup_counters.load_texture);
  139. CHECK(counters.generate_texture == 2 * startup_counters.generate_texture);
  140. CHECK(counters.release_texture == num_released_textures);
  141. // Another loop should not affect the texture calls.
  142. TestsShell::RenderLoop();
  143. CHECK(counters.load_texture == 2 * startup_counters.load_texture);
  144. CHECK(counters.generate_texture == 2 * startup_counters.generate_texture);
  145. CHECK(counters.release_texture == num_released_textures);
  146. }
  147. SUBCASE("ReleaseFontResources")
  148. {
  149. const auto counter_generate_before = counters.generate_texture;
  150. const auto counter_release_before = counters.release_texture;
  151. Rml::ReleaseFontResources();
  152. CHECK(counters.generate_texture == counter_generate_before);
  153. CHECK(counters.release_texture > counter_release_before);
  154. // Font texture is regenerated when rendered again.
  155. TestsShell::RenderLoop();
  156. CHECK(counters.generate_texture > counter_generate_before);
  157. }
  158. SUBCASE("FontGlyphCache")
  159. {
  160. const auto counter_generate_before = counters.generate_texture;
  161. const auto counter_release_before = counters.release_texture;
  162. // Verify that ASCII characters are cached during the first use of the font. Then the font texture should not be regenerated when adding ASCII
  163. // characters not previously shown.
  164. element->SetInnerRML("Abc!%&()");
  165. TestsShell::RenderLoop();
  166. CHECK(counters.generate_texture == counter_generate_before);
  167. // However, when we display a non-ASCII character not part of the initial cache, the font texture needs to be regenerated.
  168. element->SetInnerRML(reinterpret_cast<const char*>(u8"π"));
  169. TestsShell::RenderLoop();
  170. CHECK(counters.generate_texture == counter_generate_before + 1);
  171. CHECK(counters.release_texture == counter_release_before + 1);
  172. }
  173. SUBCASE("ReleaseGeometry")
  174. {
  175. CHECK(counters.compile_geometry > 0);
  176. CHECK(counters.release_geometry == 0);
  177. Rml::ReleaseCompiledGeometry();
  178. CHECK(counters.compile_geometry == counters.release_geometry);
  179. TestsShell::RenderLoop();
  180. CHECK(counters.compile_geometry > counters.release_geometry);
  181. }
  182. document->Close();
  183. TestsShell::ShutdownShell();
  184. // Counters are reset during shutdown.
  185. const auto& counters_at_shutdown = render_interface->GetCountersFromPreviousReset();
  186. // Finally, verify that all generated and loaded resources were released during shutdown.
  187. CHECK(counters_at_shutdown.generate_texture + counters_at_shutdown.load_texture == counters_at_shutdown.release_texture);
  188. CHECK(counters_at_shutdown.compile_geometry == counters_at_shutdown.release_geometry);
  189. }
  190. TEST_CASE("core.initialize")
  191. {
  192. TestsRenderInterface* render_interface = TestsShell::GetTestsRenderInterface();
  193. // This test only works with the dummy renderer.
  194. if (!render_interface)
  195. return;
  196. const Vector2i window_size = {1280, 720};
  197. SUBCASE("GlobalRenderInterface")
  198. {
  199. Rml::SetRenderInterface(render_interface);
  200. REQUIRE(Rml::CreateContext("invalid_before_initialise", window_size) == nullptr);
  201. REQUIRE(Rml::Initialise());
  202. REQUIRE(Rml::CreateContext("main", window_size) != nullptr);
  203. }
  204. SUBCASE("ContextRenderInterface")
  205. {
  206. REQUIRE(Rml::CreateContext("invalid_before_initialise", window_size) == nullptr);
  207. // We should be able to initialize without setting any interfaces.
  208. REQUIRE(Rml::Initialise());
  209. // But then we must pass a render interface to new contexts (this will emit a warning).
  210. REQUIRE(Rml::CreateContext("invalid_no_render_interface", window_size) == nullptr);
  211. REQUIRE(Rml::CreateContext("main", window_size, render_interface) != nullptr);
  212. }
  213. Rml::Shutdown();
  214. }