ElementDocument.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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/TestsShell.h"
  29. #include <RmlUi/Core/Context.h>
  30. #include <RmlUi/Core/Element.h>
  31. #include <RmlUi/Core/ElementDocument.h>
  32. #include <doctest.h>
  33. #include <algorithm>
  34. using namespace Rml;
  35. static const String document_focus_rml = R"(
  36. <rml>
  37. <head>
  38. <link type="text/rcss" href="/assets/rml.rcss"/>
  39. <link type="text/rcss" href="/assets/invader.rcss"/>
  40. <style>
  41. button {
  42. display: inline-block;
  43. tab-index: auto;
  44. }
  45. :focus {
  46. image-color: #af0;
  47. }
  48. :disabled {
  49. image-color: #666;
  50. }
  51. .hide {
  52. visibility: hidden;
  53. }
  54. .nodisplay {
  55. display: none;
  56. }
  57. </style>
  58. </head>
  59. <body id="body">
  60. <input type="checkbox" id="p1"/> P1
  61. <label><input type="checkbox" id="p2"/> P2</label>
  62. <p>
  63. <input type="checkbox" id="p3"/><label for="p3"> P3</label>
  64. </p>
  65. <p class="nodisplay">
  66. <input type="checkbox" id="p4"/><label for="p4"> P4</label>
  67. </p>
  68. <input type="checkbox" id="p5"/> P5
  69. <p>
  70. <input type="checkbox" id="p6" disabled/><label for="p6"> P6</label>
  71. </p>
  72. <div>
  73. <label><input type="checkbox" id="p7"/> P7</label>
  74. <label class="hide"><input type="checkbox" id="p8"/> P8</label>
  75. <label><button id="p9"> P9</button></label>
  76. <label><input type="checkbox" id="p10"/> P10</label>
  77. </div>
  78. <div id="container">
  79. <p> Deeply nested
  80. <span>
  81. <input type="checkbox" id="p11"/> P11
  82. </span>
  83. <input type="checkbox" id="p12"/> P12
  84. </p>
  85. <input type="checkbox" id="p13"/> P13
  86. </div>
  87. </body>
  88. </rml>
  89. )";
  90. static const String focus_forward = "p1 p2 p3 p5 p7 p9 p10 p11 p12 p13";
  91. TEST_CASE("elementdocument.focus")
  92. {
  93. Context* context = TestsShell::GetContext();
  94. REQUIRE(context);
  95. ElementDocument* document = context->LoadDocumentFromMemory(document_focus_rml);
  96. REQUIRE(document);
  97. document->Show();
  98. context->Update();
  99. context->Render();
  100. TestsShell::RenderLoop();
  101. document->Focus();
  102. StringList ids;
  103. StringUtilities::ExpandString(ids, focus_forward, ' ');
  104. REQUIRE(!ids.empty());
  105. // Forward direction
  106. for(const String& id : ids)
  107. {
  108. context->ProcessKeyDown(Input::KI_TAB, 0);
  109. CHECK(context->GetFocusElement()->GetId() == id);
  110. }
  111. // Wrap around
  112. context->ProcessKeyDown(Input::KI_TAB, 0);
  113. CHECK(context->GetFocusElement()->GetId() == ids[0]);
  114. document->Focus();
  115. std::reverse(ids.begin(), ids.end());
  116. // Reverse direction
  117. for (const String& id : ids)
  118. {
  119. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  120. CHECK(context->GetFocusElement()->GetId() == id);
  121. }
  122. // Wrap around (reverse)
  123. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  124. CHECK(context->GetFocusElement()->GetId() == ids[0]);
  125. // Tab to document
  126. {
  127. document->SetProperty("tab-index", "auto");
  128. document->UpdateDocument();
  129. context->ProcessKeyDown(Input::KI_TAB, 0);
  130. CHECK(context->GetFocusElement()->GetId() == "body");
  131. }
  132. // Tab from container element
  133. {
  134. Element* container = document->GetElementById("container");
  135. REQUIRE(container);
  136. container->Focus();
  137. context->ProcessKeyDown(Input::KI_TAB, 0);
  138. CHECK(context->GetFocusElement()->GetId() == "p11");
  139. container->Focus();
  140. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  141. CHECK(context->GetFocusElement()->GetId() == "p10");
  142. }
  143. // Single element
  144. {
  145. document->SetProperty("tab-index", "none");
  146. document->SetInnerRML(R"(<input type="checkbox" id="p1"/> P1)");
  147. document->UpdateDocument();
  148. document->Focus();
  149. context->ProcessKeyDown(Input::KI_TAB, 0);
  150. CHECK(context->GetFocusElement()->GetId() == "p1");
  151. context->ProcessKeyDown(Input::KI_TAB, 0);
  152. CHECK(context->GetFocusElement()->GetId() == "p1");
  153. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  154. CHECK(context->GetFocusElement()->GetId() == "p1");
  155. document->SetProperty("tab-index", "auto");
  156. document->UpdateDocument();
  157. document->Focus();
  158. context->ProcessKeyDown(Input::KI_TAB, 0);
  159. CHECK(context->GetFocusElement()->GetId() == "p1");
  160. context->ProcessKeyDown(Input::KI_TAB, 0);
  161. CHECK(context->GetFocusElement()->GetId() == "body");
  162. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  163. CHECK(context->GetFocusElement()->GetId() == "p1");
  164. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  165. CHECK(context->GetFocusElement()->GetId() == "body");
  166. }
  167. // Single, non-tabable element
  168. {
  169. document->SetProperty("tab-index", "none");
  170. document->SetInnerRML(R"(<div id="child"/>)");
  171. document->UpdateDocument();
  172. Element* child = document->GetChild(0);
  173. document->Focus();
  174. context->ProcessKeyDown(Input::KI_TAB, 0);
  175. CHECK(context->GetFocusElement()->GetId() == "body");
  176. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  177. CHECK(context->GetFocusElement()->GetId() == "body");
  178. child->Focus();
  179. context->ProcessKeyDown(Input::KI_TAB, 0);
  180. CHECK(context->GetFocusElement()->GetId() == "child");
  181. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  182. CHECK(context->GetFocusElement()->GetId() == "child");
  183. document->SetProperty("tab-index", "auto");
  184. document->UpdateDocument();
  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() == "body");
  193. child->Focus();
  194. context->ProcessKeyDown(Input::KI_TAB, Input::KM_SHIFT);
  195. CHECK(context->GetFocusElement()->GetId() == "body");
  196. }
  197. document->Close();
  198. TestsShell::ShutdownShell();
  199. }