ElementDocument.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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/Mocks.h"
  29. #include "../Common/TestsShell.h"
  30. #include <RmlUi/Core/Context.h>
  31. #include <RmlUi/Core/Element.h>
  32. #include <RmlUi/Core/ElementDocument.h>
  33. #include <RmlUi/Core/Factory.h>
  34. #include <algorithm>
  35. #include <doctest.h>
  36. using namespace Rml;
  37. static void SimulateClick(Context* context, Vector2i position)
  38. {
  39. context->Update();
  40. context->ProcessMouseMove(position.x, position.y, 0);
  41. context->Update();
  42. context->ProcessMouseButtonDown(0, 0);
  43. context->Update();
  44. context->ProcessMouseButtonUp(0, 0);
  45. context->Update();
  46. }
  47. static const String document_focus_rml = R"(
  48. <rml>
  49. <head>
  50. <link type="text/rcss" href="/assets/rml.rcss"/>
  51. <link type="text/rcss" href="/assets/invader.rcss"/>
  52. <style>
  53. button {
  54. display: inline-block;
  55. tab-index: auto;
  56. }
  57. :focus {
  58. image-color: #af0;
  59. }
  60. :disabled {
  61. image-color: #666;
  62. }
  63. .hide {
  64. visibility: hidden;
  65. }
  66. .nodisplay {
  67. display: none;
  68. }
  69. </style>
  70. </head>
  71. <body id="body" onload="something">
  72. <input type="checkbox" id="p1"/> P1
  73. <label><input type="checkbox" id="p2"/> P2</label>
  74. <p>
  75. <input type="checkbox" id="p3"/><label for="p3"> P3</label>
  76. </p>
  77. <p class="nodisplay">
  78. <input type="checkbox" id="p4"/><label for="p4"> P4</label>
  79. </p>
  80. <input type="checkbox" id="p5"/> P5
  81. <p>
  82. <input type="checkbox" id="p6" disabled/><label for="p6"> P6</label>
  83. </p>
  84. <div>
  85. <label><input type="checkbox" id="p7"/> P7</label>
  86. <label class="hide"><input type="checkbox" id="p8"/> P8</label>
  87. <label><button id="p9"> P9</button></label>
  88. <label><input type="checkbox" id="p10"/> P10</label>
  89. </div>
  90. <div id="container">
  91. <p> Deeply nested
  92. <span>
  93. <input type="checkbox" id="p11"/> P11
  94. </span>
  95. <input type="checkbox" id="p12"/> P12
  96. </p>
  97. <input type="checkbox" id="p13"/> P13
  98. </div>
  99. </body>
  100. </rml>
  101. )";
  102. static const String focus_forward = "p1 p2 p3 p5 p7 p9 p10 p11 p12 p13";
  103. TEST_SUITE_BEGIN("ElementDocument");
  104. TEST_CASE("Focus")
  105. {
  106. Context* context = TestsShell::GetContext();
  107. REQUIRE(context);
  108. ElementDocument* document = context->LoadDocumentFromMemory(document_focus_rml);
  109. REQUIRE(document);
  110. document->Show();
  111. context->Update();
  112. context->Render();
  113. TestsShell::RenderLoop();
  114. SUBCASE("Tab order")
  115. {
  116. StringList ids;
  117. StringUtilities::ExpandString(ids, focus_forward, ' ');
  118. REQUIRE(!ids.empty());
  119. document->Focus();
  120. SUBCASE("Forward")
  121. {
  122. for (const String& id : ids)
  123. {
  124. context->ProcessKeyDown(Input::KI_TAB, 0);
  125. CHECK(context->GetFocusElement()->GetId() == id);
  126. }
  127. // Wrap around
  128. context->ProcessKeyDown(Input::KI_TAB, 0);
  129. CHECK(context->GetFocusElement()->GetId() == ids[0]);
  130. }
  131. SUBCASE("Reverse")
  132. {
  133. std::reverse(ids.begin(), ids.end());
  134. for (const String& id : ids)
  135. {
  136. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  137. CHECK(context->GetFocusElement()->GetId() == id);
  138. }
  139. // Wrap around (reverse)
  140. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  141. CHECK(context->GetFocusElement()->GetId() == ids[0]);
  142. }
  143. }
  144. SUBCASE("Tab to document")
  145. {
  146. Element* element = document->GetElementById("p13");
  147. REQUIRE(element);
  148. element->Focus();
  149. document->SetProperty("tab-index", "auto");
  150. document->UpdateDocument();
  151. context->ProcessKeyDown(Input::KI_TAB, 0);
  152. CHECK(context->GetFocusElement()->GetId() == "body");
  153. }
  154. SUBCASE("Tab from container element")
  155. {
  156. Element* container = document->GetElementById("container");
  157. REQUIRE(container);
  158. container->Focus();
  159. context->ProcessKeyDown(Input::KI_TAB, 0);
  160. CHECK(context->GetFocusElement()->GetId() == "p11");
  161. container->Focus();
  162. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  163. CHECK(context->GetFocusElement()->GetId() == "p10");
  164. }
  165. SUBCASE("Single element")
  166. {
  167. document->SetProperty("tab-index", "none");
  168. document->SetInnerRML(R"(<input type="checkbox" id="p1"/> P1)");
  169. document->UpdateDocument();
  170. document->Focus();
  171. context->ProcessKeyDown(Input::KI_TAB, 0);
  172. CHECK(context->GetFocusElement()->GetId() == "p1");
  173. context->ProcessKeyDown(Input::KI_TAB, 0);
  174. CHECK(context->GetFocusElement()->GetId() == "p1");
  175. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  176. CHECK(context->GetFocusElement()->GetId() == "p1");
  177. document->SetProperty("tab-index", "auto");
  178. document->UpdateDocument();
  179. document->Focus();
  180. context->ProcessKeyDown(Input::KI_TAB, 0);
  181. CHECK(context->GetFocusElement()->GetId() == "p1");
  182. context->ProcessKeyDown(Input::KI_TAB, 0);
  183. CHECK(context->GetFocusElement()->GetId() == "body");
  184. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  185. CHECK(context->GetFocusElement()->GetId() == "p1");
  186. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  187. CHECK(context->GetFocusElement()->GetId() == "body");
  188. }
  189. SUBCASE("Single, non-tabable element")
  190. {
  191. document->SetProperty("tab-index", "none");
  192. document->SetInnerRML(R"(<div id="child"/>)");
  193. document->UpdateDocument();
  194. Element* child = document->GetChild(0);
  195. document->Focus();
  196. context->ProcessKeyDown(Input::KI_TAB, 0);
  197. CHECK(context->GetFocusElement()->GetId() == "body");
  198. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  199. CHECK(context->GetFocusElement()->GetId() == "body");
  200. child->Focus();
  201. context->ProcessKeyDown(Input::KI_TAB, 0);
  202. CHECK(context->GetFocusElement()->GetId() == "child");
  203. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  204. CHECK(context->GetFocusElement()->GetId() == "child");
  205. document->SetProperty("tab-index", "auto");
  206. document->UpdateDocument();
  207. document->Focus();
  208. context->ProcessKeyDown(Input::KI_TAB, 0);
  209. CHECK(context->GetFocusElement()->GetId() == "body");
  210. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  211. CHECK(context->GetFocusElement()->GetId() == "body");
  212. child->Focus();
  213. context->ProcessKeyDown(Input::KI_TAB, 0);
  214. CHECK(context->GetFocusElement()->GetId() == "body");
  215. child->Focus();
  216. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  217. CHECK(context->GetFocusElement()->GetId() == "body");
  218. }
  219. document->Close();
  220. TestsShell::ShutdownShell();
  221. }
  222. TEST_CASE("Load")
  223. {
  224. namespace tl = trompeloeil;
  225. constexpr auto BODY_TAG = "body";
  226. Context* context = TestsShell::GetContext();
  227. REQUIRE(context);
  228. MockEventListener mockEventListener;
  229. MockEventListenerInstancer mockEventListenerInstancer;
  230. tl::sequence sequence;
  231. REQUIRE_CALL(mockEventListenerInstancer, InstanceEventListener("something", tl::_))
  232. .WITH(_2->GetTagName() == BODY_TAG)
  233. .IN_SEQUENCE(sequence)
  234. .LR_RETURN(&mockEventListener);
  235. ALLOW_CALL(mockEventListener, OnAttach(tl::_));
  236. ALLOW_CALL(mockEventListener, OnDetach(tl::_));
  237. REQUIRE_CALL(mockEventListener, ProcessEvent(tl::_))
  238. .WITH(_1.GetId() == EventId::Load && _1.GetTargetElement()->GetTagName() == BODY_TAG)
  239. .IN_SEQUENCE(sequence);
  240. Factory::RegisterEventListenerInstancer(&mockEventListenerInstancer);
  241. ElementDocument* document = context->LoadDocumentFromMemory(document_focus_rml);
  242. REQUIRE(document);
  243. document->Close();
  244. TestsShell::ShutdownShell();
  245. }
  246. TEST_CASE("ReloadStyleSheet")
  247. {
  248. Context* context = TestsShell::GetContext();
  249. ElementDocument* document = context->LoadDocument("basic/demo/data/demo.rml");
  250. // There should be no warnings when reloading style sheets.
  251. document->ReloadStyleSheet();
  252. document->Close();
  253. TestsShell::ShutdownShell();
  254. }
  255. TEST_CASE("Modal.MultipleDocuments")
  256. {
  257. Context* context = TestsShell::GetContext();
  258. constexpr float halfwidth = 150;
  259. ElementDocument* document1 = context->LoadDocument("assets/demo.rml");
  260. document1->Show(Rml::ModalFlag::Modal);
  261. document1->GetElementById("title")->SetInnerRML("Modal 1");
  262. constexpr float margin1 = 50;
  263. document1->SetProperty(PropertyId::MarginLeft, Property{margin1, Unit::PX});
  264. Rml::ElementDocument* document2 = context->LoadDocument("assets/demo.rml");
  265. document2->Show(Rml::ModalFlag::Modal);
  266. document2->GetElementById("title")->SetInnerRML("Modal 2");
  267. constexpr float margin2 = 350;
  268. document2->SetProperty(PropertyId::MarginLeft, Property{margin2, Unit::PX});
  269. Rml::ElementDocument* document3 = context->LoadDocument("assets/demo.rml");
  270. document3->Show(Rml::ModalFlag::None);
  271. document3->GetElementById("title")->SetInnerRML("Non-modal");
  272. constexpr float margin3 = 650;
  273. document3->SetProperty(PropertyId::MarginLeft, Property{margin3, Unit::PX});
  274. context->Update();
  275. TestsShell::RenderLoop();
  276. REQUIRE(context->GetFocusElement() == document2);
  277. SUBCASE("FocusFromModal")
  278. {
  279. document1->Focus();
  280. REQUIRE(context->GetFocusElement() == document1);
  281. document3->Focus();
  282. REQUIRE(context->GetFocusElement() == document1);
  283. }
  284. SUBCASE("ClickFromModal")
  285. {
  286. SimulateClick(context, Vector2i(int(margin1 + halfwidth), context->GetDimensions().y / 2));
  287. REQUIRE(context->GetFocusElement() == document2);
  288. SimulateClick(context, Vector2i(int(margin3 + halfwidth), context->GetDimensions().y / 2));
  289. REQUIRE(context->GetFocusElement() == document2);
  290. }
  291. SUBCASE("ModalFlag")
  292. {
  293. document1->Show(ModalFlag::None, FocusFlag::Document);
  294. REQUIRE(context->GetFocusElement() == document2);
  295. document3->Show(ModalFlag::None, FocusFlag::Document);
  296. REQUIRE(context->GetFocusElement() == document2);
  297. document3->Show(ModalFlag::Modal, FocusFlag::Document);
  298. REQUIRE(context->GetFocusElement() == document3);
  299. }
  300. document1->Close();
  301. document2->Close();
  302. document3->Close();
  303. TestsShell::ShutdownShell();
  304. }
  305. TEST_CASE("Modal.FocusWithin")
  306. {
  307. Context* context = TestsShell::GetContext();
  308. Rml::ElementDocument* document = context->LoadDocument("assets/demo.rml");
  309. document->GetElementById("content")->SetInnerRML("<input type='text' id='input'/>");
  310. document->Show(Rml::ModalFlag::Modal);
  311. REQUIRE(context->GetFocusElement() == document);
  312. Element* input = document->GetElementById("input");
  313. input->Focus();
  314. REQUIRE(context->GetFocusElement() == input);
  315. document->Close();
  316. TestsShell::ShutdownShell();
  317. }
  318. TEST_SUITE_END();