ElementDocument.cpp 11 KB

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