Core.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 <Shell.h>
  35. #include <algorithm>
  36. #include <doctest.h>
  37. using namespace Rml;
  38. static const String document_textures_rml = R"(
  39. <rml>
  40. <head>
  41. <title>Test</title>
  42. <link type="text/rcss" href="/assets/rml.rcss"/>
  43. <style>
  44. body {
  45. left: 0;
  46. top: 0;
  47. right: 0;
  48. bottom: 0;
  49. }
  50. div.file {
  51. height: 100px;
  52. decorator: image(/assets/high_scores_alien_2.tga);
  53. }
  54. @spritesheet aliens {
  55. src: /assets/high_scores_alien_3.tga;
  56. alien3: 0px 0px 64px 64px;
  57. }
  58. div.sprite {
  59. height: 100px;
  60. decorator: image(alien3);
  61. }
  62. progress {
  63. display: block;
  64. width: 50px;
  65. height: 50px;
  66. background: #333;
  67. fill-image: /assets/high_scores_defender.tga;
  68. }
  69. </style>
  70. </head>
  71. <body>
  72. <div style="display: none">
  73. <img src="/assets/high_scores_alien_1.tga"/>
  74. <div class="file"/>
  75. <div class="sprite"/>
  76. <progress direction="clockwise" start-edge="bottom" value="0.5"/>
  77. </div>
  78. </body>
  79. </rml>
  80. )";
  81. static const String document_basic_rml = R"(
  82. <rml>
  83. <head>
  84. <title>Test</title>
  85. <link type="text/rcss" href="/assets/rml.rcss"/>
  86. <style>
  87. body {
  88. left: 0;
  89. top: 0;
  90. right: 0;
  91. bottom: 0;
  92. font-family: LatoLatin;
  93. font-size: 16px;
  94. }
  95. @spritesheet aliens {
  96. src: /assets/high_scores_alien_3.tga;
  97. alien3: 0px 0px 64px 64px;
  98. }
  99. div.sprite {
  100. height: 100px;
  101. decorator: image(alien3);
  102. }
  103. </style>
  104. </head>
  105. <body>
  106. <img src="/assets/high_scores_alien_1.tga"/>
  107. <div class="sprite"/>
  108. abc
  109. </body>
  110. </rml>
  111. )";
  112. static inline StringList GetSortedTextureSourceList()
  113. {
  114. StringList list = Rml::GetTextureSourceList();
  115. std::sort(list.begin(), list.end());
  116. return list;
  117. }
  118. TEST_CASE("core.texture_source_list")
  119. {
  120. Context* context = TestsShell::GetContext();
  121. REQUIRE(context);
  122. REQUIRE(GetSortedTextureSourceList().size() == 0);
  123. // We should be able to detect all sources even though they are hidden.
  124. const StringList list_expected = {
  125. "assets/high_scores_alien_1.tga",
  126. "assets/high_scores_alien_2.tga",
  127. "assets/high_scores_alien_3.tga",
  128. "assets/high_scores_defender.tga",
  129. };
  130. ElementDocument* document = context->LoadDocumentFromMemory(document_textures_rml);
  131. REQUIRE(document);
  132. CHECK(GetSortedTextureSourceList() == list_expected);
  133. document->Show();
  134. CHECK(GetSortedTextureSourceList() == list_expected);
  135. context->Update();
  136. CHECK(GetSortedTextureSourceList() == list_expected);
  137. context->Render();
  138. CHECK(GetSortedTextureSourceList() == list_expected);
  139. TestsShell::RenderLoop();
  140. document->Close();
  141. TestsShell::ShutdownShell();
  142. }
  143. TEST_CASE("core.release_resources")
  144. {
  145. TestsRenderInterface* render_interface = TestsShell::GetTestsRenderInterface();
  146. // This test only works with the dummy renderer.
  147. if (!render_interface)
  148. return;
  149. const auto& counters = render_interface->GetCounters();
  150. Context* context = TestsShell::GetContext();
  151. REQUIRE(context);
  152. ElementDocument* document = context->LoadDocument("assets/demo.rml");
  153. document->Show();
  154. TestsShell::RenderLoop();
  155. Element* element = document->GetElementById("content");
  156. SUBCASE("ReleaseTextures")
  157. {
  158. const auto startup_counters = counters;
  159. REQUIRE(counters.load_texture > 0);
  160. REQUIRE(counters.generate_texture > 0);
  161. REQUIRE(counters.release_texture == 0);
  162. // Release all textures and verify that the render interface received the release call.
  163. Rml::ReleaseTextures();
  164. CHECK(counters.load_texture == startup_counters.load_texture);
  165. CHECK(counters.generate_texture == startup_counters.generate_texture);
  166. CHECK(counters.release_texture == startup_counters.generate_texture + startup_counters.load_texture);
  167. const size_t num_released_textures = counters.release_texture;
  168. // By doing a new context Update+Render the textures should be loaded again.
  169. TestsShell::RenderLoop();
  170. CHECK(counters.load_texture == 2 * startup_counters.load_texture);
  171. CHECK(counters.generate_texture == 2 * startup_counters.generate_texture);
  172. CHECK(counters.release_texture == num_released_textures);
  173. // Another loop should not affect the texture calls.
  174. TestsShell::RenderLoop();
  175. CHECK(counters.load_texture == 2 * startup_counters.load_texture);
  176. CHECK(counters.generate_texture == 2 * startup_counters.generate_texture);
  177. CHECK(counters.release_texture == num_released_textures);
  178. }
  179. SUBCASE("ReleaseTexture")
  180. {
  181. const auto startup_counters = counters;
  182. Rml::ReleaseTexture("assets/invader.tga");
  183. CHECK(counters.release_texture == startup_counters.release_texture + 1);
  184. TestsShell::RenderLoop();
  185. CHECK(counters.load_texture == startup_counters.load_texture + 1);
  186. }
  187. SUBCASE("ReleaseFontResources")
  188. {
  189. const auto counter_generate_before = counters.generate_texture;
  190. const auto counter_release_before = counters.release_texture;
  191. Rml::ReleaseFontResources();
  192. CHECK(counters.generate_texture == counter_generate_before);
  193. CHECK(counters.release_texture > counter_release_before);
  194. // Font texture is regenerated when rendered again.
  195. TestsShell::RenderLoop();
  196. CHECK(counters.generate_texture > counter_generate_before);
  197. }
  198. SUBCASE("FontGlyphCache")
  199. {
  200. const auto counter_generate_before = counters.generate_texture;
  201. const auto counter_release_before = counters.release_texture;
  202. // Verify that ASCII characters are cached during the first use of the font. Then the font texture should not be regenerated when adding ASCII
  203. // characters not previously shown.
  204. element->SetInnerRML("Abc!%&()");
  205. TestsShell::RenderLoop();
  206. CHECK(counters.generate_texture == counter_generate_before);
  207. // However, when we display a non-ASCII character not part of the initial cache, the font texture needs to be regenerated.
  208. element->SetInnerRML(reinterpret_cast<const char*>(u8"π"));
  209. TestsShell::RenderLoop();
  210. CHECK(counters.generate_texture == counter_generate_before + 1);
  211. CHECK(counters.release_texture == counter_release_before + 1);
  212. }
  213. SUBCASE("ReleaseGeometry")
  214. {
  215. CHECK(counters.compile_geometry > 0);
  216. CHECK(counters.release_geometry == 0);
  217. Rml::ReleaseCompiledGeometry();
  218. CHECK(counters.compile_geometry == counters.release_geometry);
  219. TestsShell::RenderLoop();
  220. CHECK(counters.compile_geometry > counters.release_geometry);
  221. }
  222. document->Close();
  223. TestsShell::ShutdownShell(false);
  224. // Counters are reset during shutdown.
  225. const auto& counters_at_shutdown = render_interface->GetCountersFromPreviousReset();
  226. // Finally, verify that all generated and loaded resources were released during shutdown.
  227. CHECK(counters_at_shutdown.generate_texture + counters_at_shutdown.load_texture == counters_at_shutdown.release_texture);
  228. CHECK(counters_at_shutdown.compile_geometry == counters_at_shutdown.release_geometry);
  229. TestsShell::ResetTestsRenderInterface();
  230. }
  231. TEST_CASE("core.initialize")
  232. {
  233. TestsRenderInterface* render_interface = TestsShell::GetTestsRenderInterface();
  234. // This test only works with the dummy renderer.
  235. if (!render_interface)
  236. return;
  237. const Vector2i window_size = {1280, 720};
  238. SUBCASE("GlobalRenderInterface")
  239. {
  240. Rml::SetRenderInterface(render_interface);
  241. REQUIRE(Rml::CreateContext("invalid_before_initialise", window_size) == nullptr);
  242. REQUIRE(Rml::Initialise());
  243. REQUIRE(Rml::CreateContext("main", window_size) != nullptr);
  244. }
  245. SUBCASE("ContextRenderInterface")
  246. {
  247. REQUIRE(Rml::CreateContext("invalid_before_initialise", window_size) == nullptr);
  248. // We should be able to initialize without setting any interfaces.
  249. REQUIRE(Rml::Initialise());
  250. // But then we must pass a render interface to new contexts (this will emit a warning).
  251. REQUIRE(Rml::CreateContext("invalid_no_render_interface", window_size) == nullptr);
  252. REQUIRE(Rml::CreateContext("main", window_size, render_interface) != nullptr);
  253. }
  254. Rml::Shutdown();
  255. }
  256. TEST_CASE("core.observer_ptr")
  257. {
  258. Context* context = TestsShell::GetContext();
  259. ElementDocument* document = context->LoadDocument("assets/demo.rml");
  260. ObserverPtr<Element> observer_ptr = document->GetObserverPtr();
  261. document->Close();
  262. // We expect a warning about the observer pointer being held in user space, preventing its memory pool from shutting down.
  263. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  264. system_interface->SetNumExpectedWarnings(1);
  265. TestsShell::ShutdownShell();
  266. }
  267. TEST_CASE("core.RemoveContext")
  268. {
  269. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  270. TestsRenderInterface* render_interface = TestsShell::GetTestsRenderInterface();
  271. // This test only works with the dummy renderer.
  272. if (!render_interface)
  273. return;
  274. const Vector2i window_size = {1280, 720};
  275. Shell::Initialize();
  276. Rml::SetSystemInterface(system_interface);
  277. REQUIRE(Rml::GetRenderInterface() == nullptr);
  278. REQUIRE(Rml::Initialise());
  279. Shell::LoadFonts();
  280. Context* context = Rml::CreateContext("main", window_size, render_interface);
  281. REQUIRE(context);
  282. ElementDocument* document = context->LoadDocumentFromMemory(document_basic_rml);
  283. document->Show();
  284. context->Update();
  285. context->Render();
  286. REQUIRE(Rml::RemoveContext(context->GetName()));
  287. SUBCASE("Normal shutdown")
  288. {
  289. Rml::Shutdown();
  290. TestsShell::ResetTestsRenderInterface();
  291. }
  292. SUBCASE("Destroy render interface before shutdown")
  293. {
  294. ReleaseRenderManagers();
  295. const auto counters = render_interface->GetCounters();
  296. CHECK(counters.release_texture == counters.generate_texture + counters.load_texture);
  297. CHECK(counters.release_geometry == counters.compile_geometry);
  298. TestsShell::ResetTestsRenderInterface();
  299. Rml::Shutdown();
  300. }
  301. Shell::Shutdown();
  302. }