ElementDocument.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 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 <doctest.h>
  35. #include <algorithm>
  36. using namespace Rml;
  37. static const String document_focus_rml = R"(
  38. <rml>
  39. <head>
  40. <link type="text/rcss" href="/assets/rml.rcss"/>
  41. <link type="text/rcss" href="/assets/invader.rcss"/>
  42. <style>
  43. button {
  44. display: inline-block;
  45. tab-index: auto;
  46. }
  47. :focus {
  48. image-color: #af0;
  49. }
  50. :disabled {
  51. image-color: #666;
  52. }
  53. .hide {
  54. visibility: hidden;
  55. }
  56. .nodisplay {
  57. display: none;
  58. }
  59. </style>
  60. </head>
  61. <body id="body" onload="something">
  62. <input type="checkbox" id="p1"/> P1
  63. <label><input type="checkbox" id="p2"/> P2</label>
  64. <p>
  65. <input type="checkbox" id="p3"/><label for="p3"> P3</label>
  66. </p>
  67. <p class="nodisplay">
  68. <input type="checkbox" id="p4"/><label for="p4"> P4</label>
  69. </p>
  70. <input type="checkbox" id="p5"/> P5
  71. <p>
  72. <input type="checkbox" id="p6" disabled/><label for="p6"> P6</label>
  73. </p>
  74. <div>
  75. <label><input type="checkbox" id="p7"/> P7</label>
  76. <label class="hide"><input type="checkbox" id="p8"/> P8</label>
  77. <label><button id="p9"> P9</button></label>
  78. <label><input type="checkbox" id="p10"/> P10</label>
  79. </div>
  80. <div id="container">
  81. <p> Deeply nested
  82. <span>
  83. <input type="checkbox" id="p11"/> P11
  84. </span>
  85. <input type="checkbox" id="p12"/> P12
  86. </p>
  87. <input type="checkbox" id="p13"/> P13
  88. </div>
  89. </body>
  90. </rml>
  91. )";
  92. static const String focus_forward = "p1 p2 p3 p5 p7 p9 p10 p11 p12 p13";
  93. TEST_SUITE_BEGIN("ElementDocument");
  94. TEST_CASE("Focus")
  95. {
  96. Context* context = TestsShell::GetContext();
  97. REQUIRE(context);
  98. ElementDocument* document = context->LoadDocumentFromMemory(document_focus_rml);
  99. REQUIRE(document);
  100. document->Show();
  101. context->Update();
  102. context->Render();
  103. TestsShell::RenderLoop();
  104. SUBCASE("Tab order")
  105. {
  106. StringList ids;
  107. StringUtilities::ExpandString(ids, focus_forward, ' ');
  108. REQUIRE(!ids.empty());
  109. document->Focus();
  110. SUBCASE("Forward")
  111. {
  112. for(const String& id : ids)
  113. {
  114. context->ProcessKeyDown(Input::KI_TAB, 0);
  115. CHECK(context->GetFocusElement()->GetId() == id);
  116. }
  117. // Wrap around
  118. context->ProcessKeyDown(Input::KI_TAB, 0);
  119. CHECK(context->GetFocusElement()->GetId() == ids[0]);
  120. }
  121. SUBCASE("Reverse")
  122. {
  123. std::reverse(ids.begin(), ids.end());
  124. for (const String& id : ids)
  125. {
  126. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  127. CHECK(context->GetFocusElement()->GetId() == id);
  128. }
  129. // Wrap around (reverse)
  130. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  131. CHECK(context->GetFocusElement()->GetId() == ids[0]);
  132. }
  133. }
  134. SUBCASE("Tab to document")
  135. {
  136. Element* element = document->GetElementById("p13");
  137. REQUIRE(element);
  138. element->Focus();
  139. document->SetProperty("tab-index", "auto");
  140. document->UpdateDocument();
  141. context->ProcessKeyDown(Input::KI_TAB, 0);
  142. CHECK(context->GetFocusElement()->GetId() == "body");
  143. }
  144. SUBCASE("Tab from container element")
  145. {
  146. Element* container = document->GetElementById("container");
  147. REQUIRE(container);
  148. container->Focus();
  149. context->ProcessKeyDown(Input::KI_TAB, 0);
  150. CHECK(context->GetFocusElement()->GetId() == "p11");
  151. container->Focus();
  152. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  153. CHECK(context->GetFocusElement()->GetId() == "p10");
  154. }
  155. SUBCASE("Single element")
  156. {
  157. document->SetProperty("tab-index", "none");
  158. document->SetInnerRML(R"(<input type="checkbox" id="p1"/> P1)");
  159. document->UpdateDocument();
  160. document->Focus();
  161. context->ProcessKeyDown(Input::KI_TAB, 0);
  162. CHECK(context->GetFocusElement()->GetId() == "p1");
  163. context->ProcessKeyDown(Input::KI_TAB, 0);
  164. CHECK(context->GetFocusElement()->GetId() == "p1");
  165. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  166. CHECK(context->GetFocusElement()->GetId() == "p1");
  167. document->SetProperty("tab-index", "auto");
  168. document->UpdateDocument();
  169. document->Focus();
  170. context->ProcessKeyDown(Input::KI_TAB, 0);
  171. CHECK(context->GetFocusElement()->GetId() == "p1");
  172. context->ProcessKeyDown(Input::KI_TAB, 0);
  173. CHECK(context->GetFocusElement()->GetId() == "body");
  174. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  175. CHECK(context->GetFocusElement()->GetId() == "p1");
  176. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  177. CHECK(context->GetFocusElement()->GetId() == "body");
  178. }
  179. SUBCASE("Single, non-tabable element")
  180. {
  181. document->SetProperty("tab-index", "none");
  182. document->SetInnerRML(R"(<div id="child"/>)");
  183. document->UpdateDocument();
  184. Element* child = document->GetChild(0);
  185. document->Focus();
  186. context->ProcessKeyDown(Input::KI_TAB, 0);
  187. CHECK(context->GetFocusElement()->GetId() == "body");
  188. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  189. CHECK(context->GetFocusElement()->GetId() == "body");
  190. child->Focus();
  191. context->ProcessKeyDown(Input::KI_TAB, 0);
  192. CHECK(context->GetFocusElement()->GetId() == "child");
  193. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  194. CHECK(context->GetFocusElement()->GetId() == "child");
  195. document->SetProperty("tab-index", "auto");
  196. document->UpdateDocument();
  197. document->Focus();
  198. context->ProcessKeyDown(Input::KI_TAB, 0);
  199. CHECK(context->GetFocusElement()->GetId() == "body");
  200. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  201. CHECK(context->GetFocusElement()->GetId() == "body");
  202. child->Focus();
  203. context->ProcessKeyDown(Input::KI_TAB, 0);
  204. CHECK(context->GetFocusElement()->GetId() == "body");
  205. child->Focus();
  206. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  207. CHECK(context->GetFocusElement()->GetId() == "body");
  208. }
  209. document->Close();
  210. TestsShell::ShutdownShell();
  211. }
  212. TEST_CASE("Load")
  213. {
  214. namespace tl = trompeloeil;
  215. constexpr auto BODY_TAG = "body";
  216. Context* context = TestsShell::GetContext();
  217. REQUIRE(context);
  218. MockEventListener mockEventListener;
  219. MockEventListenerInstancer mockEventListenerInstancer;
  220. tl::sequence sequence;
  221. REQUIRE_CALL(mockEventListenerInstancer, InstanceEventListener("something", tl::_))
  222. .WITH(_2->GetTagName() == BODY_TAG)
  223. .IN_SEQUENCE(sequence)
  224. .LR_RETURN(&mockEventListener);
  225. ALLOW_CALL(mockEventListener, OnAttach(tl::_));
  226. ALLOW_CALL(mockEventListener, OnDetach(tl::_));
  227. REQUIRE_CALL(mockEventListener, ProcessEvent(tl::_))
  228. .WITH(_1.GetId() == EventId::Load && _1.GetTargetElement()->GetTagName() == BODY_TAG)
  229. .IN_SEQUENCE(sequence);
  230. Factory::RegisterEventListenerInstancer(&mockEventListenerInstancer);
  231. ElementDocument* document = context->LoadDocumentFromMemory(document_focus_rml);
  232. REQUIRE(document);
  233. document->Close();
  234. TestsShell::ShutdownShell();
  235. }
  236. TEST_SUITE_END();