Element.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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/TestsInterface.h"
  30. #include "../Common/TestsShell.h"
  31. #include "../Common/TypesToString.h"
  32. #include <RmlUi/Core/Context.h>
  33. #include <RmlUi/Core/Element.h>
  34. #include <RmlUi/Core/ElementDocument.h>
  35. #include <RmlUi/Core/Factory.h>
  36. #include <doctest.h>
  37. using namespace Rml;
  38. static const String document_clone_rml = R"(
  39. <rml>
  40. <head>
  41. <title>Test</title>
  42. <link type="text/rcss" href="/assets/rml.rcss"/>
  43. <link type="text/rcss" href="/assets/invader.rcss"/>
  44. <style>
  45. body
  46. {
  47. left: 0;
  48. top: 0;
  49. right: 0;
  50. bottom: 0;
  51. }
  52. div {
  53. drag: clone;
  54. background-color: #fff;
  55. height: 100px;
  56. }
  57. div.blue {
  58. background-color: #00f;
  59. }
  60. span {
  61. color: red;
  62. }
  63. </style>
  64. </head>
  65. <body>
  66. <div style="background-color: #f00">This is a <span>sample</span>.</div>
  67. </body>
  68. </rml>
  69. )";
  70. static const String document_scroll_rml = R"(
  71. <rml>
  72. <head>
  73. <title>Test</title>
  74. <style>
  75. body {
  76. left: 0;
  77. top: 0;
  78. width: 100px;
  79. height: 100px;
  80. padding: 0;
  81. margin: 0;
  82. overflow: scroll;
  83. }
  84. div {
  85. display: block;
  86. width: 200px;
  87. height: 50px;
  88. padding: 0;
  89. margin: 0;
  90. }
  91. #row0 { background: #3a3; }
  92. #row1 { background: #aa3; }
  93. #row2 { background: #3aa; }
  94. #row3 { background: #aaa; }
  95. span {
  96. display: inline-block;
  97. box-sizing: border-box;
  98. width: 50px;
  99. height: 50px;
  100. padding: 0;
  101. margin: 0;
  102. border: 1px #000;
  103. }
  104. span:nth-child(2) { border-color: #fff; }
  105. span:nth-child(3) { border-color: #f00; }
  106. span:nth-child(4) { border-color: #ff0; }
  107. scrollbarvertical,
  108. scrollbarhorizontal {
  109. width: 0;
  110. height: 0;
  111. }
  112. </style>
  113. </head>
  114. <body id="scrollable">
  115. <div id="row0">
  116. <span id="cell00"></span>
  117. <span id="cell01"></span>
  118. <span id="cell02"></span>
  119. <span id="cell03"></span>
  120. </div>
  121. <div id="row1">
  122. <span id="cell10"></span>
  123. <span id="cell11"></span>
  124. <span id="cell12"></span>
  125. <span id="cell13"></span>
  126. </div>
  127. <div id="row2">
  128. <span id="cell20"></span>
  129. <span id="cell21"></span>
  130. <span id="cell22"></span>
  131. <span id="cell23"></span>
  132. </div>
  133. <div id="row3">
  134. <span id="cell30"></span>
  135. <span id="cell31"></span>
  136. <span id="cell32"></span>
  137. <span id="cell33"></span>
  138. </div>
  139. </body>
  140. </rml>
  141. )";
  142. void Run(Context* context)
  143. {
  144. context->Update();
  145. context->Render();
  146. TestsShell::RenderLoop();
  147. }
  148. TEST_CASE("Element")
  149. {
  150. Context* context = TestsShell::GetContext();
  151. REQUIRE(context);
  152. ElementDocument* document = context->LoadDocumentFromMemory(document_clone_rml);
  153. REQUIRE(document);
  154. document->Show();
  155. context->Update();
  156. context->Render();
  157. TestsShell::RenderLoop();
  158. SUBCASE("Attribute")
  159. {
  160. auto* button = document->AppendChild(document->CreateElement("button"));
  161. SUBCASE("Event listener")
  162. {
  163. namespace tl = trompeloeil;
  164. static constexpr auto ON_CLICK_ATTRIBUTE = "onclick";
  165. static constexpr auto ON_CLICK_VALUE = "moo";
  166. std::vector<UniquePtr<tl::expectation>> expectations;
  167. UniquePtr<MockEventListener> mockEventListener;
  168. const auto configureMockEventListener = [&]() {
  169. mockEventListener.reset(new MockEventListener());
  170. expectations.emplace_back(NAMED_ALLOW_CALL(*mockEventListener, OnAttach(button)));
  171. expectations.emplace_back(NAMED_ALLOW_CALL(*mockEventListener, OnDetach(button)).LR_SIDE_EFFECT(mockEventListener.reset()));
  172. };
  173. MockEventListenerInstancer mockEventListenerInstancer;
  174. const auto configureMockEventListenerInstancer = [&](const auto value) {
  175. expectations.emplace_back(NAMED_REQUIRE_CALL(mockEventListenerInstancer, InstanceEventListener(value, button))
  176. .LR_SIDE_EFFECT(configureMockEventListener())
  177. .LR_RETURN(mockEventListener.get()));
  178. };
  179. Factory::RegisterEventListenerInstancer(&mockEventListenerInstancer);
  180. configureMockEventListenerInstancer(ON_CLICK_VALUE);
  181. button->SetAttribute(ON_CLICK_ATTRIBUTE, ON_CLICK_VALUE);
  182. SUBCASE("Replacement")
  183. {
  184. static constexpr auto REPLACEMENT_ON_CLICK_VALUE = "boo";
  185. configureMockEventListenerInstancer(REPLACEMENT_ON_CLICK_VALUE);
  186. button->SetAttribute(ON_CLICK_ATTRIBUTE, REPLACEMENT_ON_CLICK_VALUE);
  187. }
  188. button->RemoveAttribute(ON_CLICK_ATTRIBUTE);
  189. }
  190. SUBCASE("Simple")
  191. {
  192. static constexpr auto DISABLED_ATTRIBUTE = "disabled";
  193. REQUIRE_FALSE(button->HasAttribute(DISABLED_ATTRIBUTE));
  194. button->SetAttribute(DISABLED_ATTRIBUTE, "");
  195. REQUIRE(button->HasAttribute(DISABLED_ATTRIBUTE));
  196. SUBCASE("Replacement")
  197. {
  198. static constexpr auto VALUE = "something";
  199. button->SetAttribute(DISABLED_ATTRIBUTE, VALUE);
  200. const auto* attributeValue = button->GetAttribute(DISABLED_ATTRIBUTE);
  201. REQUIRE(attributeValue);
  202. REQUIRE(attributeValue->GetType() == Variant::Type::STRING);
  203. CHECK(attributeValue->Get<String>() == VALUE);
  204. }
  205. button->RemoveAttribute(DISABLED_ATTRIBUTE);
  206. CHECK_FALSE(button->HasAttribute(DISABLED_ATTRIBUTE));
  207. }
  208. }
  209. SUBCASE("CloneDrag")
  210. {
  211. // Simulate input for mouse click and drag
  212. context->ProcessMouseMove(10, 10, 0);
  213. context->Update();
  214. context->Render();
  215. context->ProcessMouseButtonDown(0, 0);
  216. context->Update();
  217. context->Render();
  218. // This should initiate a drag clone.
  219. context->ProcessMouseMove(10, 11, 0);
  220. context->Update();
  221. context->Render();
  222. context->ProcessMouseButtonUp(0, 0);
  223. }
  224. SUBCASE("CloneManual")
  225. {
  226. Element* element = document->GetFirstChild();
  227. REQUIRE(element->GetProperty<String>("background-color") == "#ff0000");
  228. CHECK(element->Clone()->GetProperty<String>("background-color") == "#ff0000");
  229. element->SetProperty("background-color", "#0f0");
  230. CHECK(element->Clone()->GetProperty<String>("background-color") == "#00ff00");
  231. element->RemoveProperty("background-color");
  232. Element* clone = document->AppendChild(element->Clone());
  233. context->Update();
  234. CHECK(clone->GetProperty<String>("background-color") == "#ffffff");
  235. element->SetClass("blue", true);
  236. clone = document->AppendChild(element->Clone());
  237. context->Update();
  238. CHECK(clone->GetProperty<String>("background-color") == "#0000ff");
  239. }
  240. SUBCASE("SetInnerRML")
  241. {
  242. Element* element = document->GetFirstChild();
  243. CHECK(element->GetInnerRML() == "This is a <span>sample</span>.");
  244. element->SetInnerRML("text");
  245. CHECK(element->GetInnerRML() == "text");
  246. const char* inner_rml = R"(before<div class="blue">child</div>after)";
  247. element->SetInnerRML(inner_rml);
  248. CHECK(element->GetInnerRML() == inner_rml);
  249. ElementPtr element_ptr = document->CreateElement("div");
  250. CHECK(element_ptr->GetInnerRML() == "");
  251. element_ptr->SetInnerRML("text");
  252. CHECK(element_ptr->GetInnerRML() == "text");
  253. }
  254. document->Close();
  255. TestsShell::ShutdownShell();
  256. }
  257. TEST_CASE("Element.ScrollIntoView")
  258. {
  259. Context* context = TestsShell::GetContext();
  260. REQUIRE(context);
  261. ElementDocument* document = context->LoadDocumentFromMemory(document_scroll_rml);
  262. REQUIRE(document);
  263. document->Show();
  264. Run(context);
  265. Element* scrollable = document->GetElementById("scrollable");
  266. REQUIRE(scrollable);
  267. Element* cells[4][4]{};
  268. for (int i = 0; i < 4; ++i)
  269. {
  270. for (int j = 0; j < 4; ++j)
  271. {
  272. cells[i][j] = document->GetElementById(CreateString("cell%d%d", i, j));
  273. REQUIRE(cells[i][j]);
  274. }
  275. }
  276. REQUIRE(cells[0][0]->GetAbsoluteOffset(Rml::BoxArea::Border) == Vector2f(0, 0));
  277. REQUIRE(cells[2][2]->GetAbsoluteOffset(Rml::BoxArea::Border) == Vector2f(100, 100));
  278. REQUIRE(cells[3][3]->GetAbsoluteOffset(Rml::BoxArea::Border) == Vector2f(150, 150));
  279. REQUIRE(scrollable->GetScrollLeft() == 0);
  280. REQUIRE(scrollable->GetScrollTop() == 0);
  281. SUBCASE("LegacyScroll")
  282. {
  283. cells[2][2]->ScrollIntoView(true);
  284. Run(context);
  285. CHECK(cells[0][0]->GetAbsoluteOffset(Rml::BoxArea::Border) == Vector2f(-50, -100));
  286. CHECK(cells[2][2]->GetAbsoluteOffset(Rml::BoxArea::Border) == Vector2f(50, 0));
  287. CHECK(cells[3][3]->GetAbsoluteOffset(Rml::BoxArea::Border) == Vector2f(100, 50));
  288. CHECK(scrollable->GetScrollLeft() == 50);
  289. CHECK(scrollable->GetScrollTop() == 100);
  290. cells[2][2]->ScrollIntoView(false);
  291. Run(context);
  292. CHECK(cells[0][0]->GetAbsoluteOffset(Rml::BoxArea::Border) == Vector2f(-50, -50));
  293. CHECK(cells[2][2]->GetAbsoluteOffset(Rml::BoxArea::Border) == Vector2f(50, 50));
  294. CHECK(cells[3][3]->GetAbsoluteOffset(Rml::BoxArea::Border) == Vector2f(100, 100));
  295. CHECK(scrollable->GetScrollLeft() == 50);
  296. CHECK(scrollable->GetScrollTop() == 50);
  297. }
  298. SUBCASE("AdvancedScroll")
  299. {
  300. cells[2][2]->ScrollIntoView({ScrollAlignment::Center, ScrollAlignment::Center});
  301. Run(context);
  302. CHECK(cells[0][0]->GetAbsoluteOffset(Rml::BoxArea::Border) == Vector2f(-75, -75));
  303. CHECK(cells[2][2]->GetAbsoluteOffset(Rml::BoxArea::Border) == Vector2f(25, 25));
  304. CHECK(cells[3][3]->GetAbsoluteOffset(Rml::BoxArea::Border) == Vector2f(75, 75));
  305. SUBCASE("NearestAlready")
  306. {
  307. cells[2][2]->ScrollIntoView(ScrollAlignment::Nearest);
  308. Run(context);
  309. CHECK(cells[0][0]->GetAbsoluteOffset(Rml::BoxArea::Border) == Vector2f(-75, -75));
  310. CHECK(cells[2][2]->GetAbsoluteOffset(Rml::BoxArea::Border) == Vector2f(25, 25));
  311. CHECK(cells[3][3]->GetAbsoluteOffset(Rml::BoxArea::Border) == Vector2f(75, 75));
  312. }
  313. SUBCASE("NearestBefore")
  314. {
  315. cells[1][1]->ScrollIntoView(ScrollAlignment::Nearest);
  316. Run(context);
  317. CHECK(cells[0][0]->GetAbsoluteOffset(Rml::BoxArea::Border) == Vector2f(-50, -50));
  318. CHECK(cells[1][1]->GetAbsoluteOffset(Rml::BoxArea::Border) == Vector2f(0, 0));
  319. CHECK(cells[2][2]->GetAbsoluteOffset(Rml::BoxArea::Border) == Vector2f(50, 50));
  320. }
  321. SUBCASE("NearestAfter")
  322. {
  323. cells[3][3]->ScrollIntoView(ScrollAlignment::Nearest);
  324. Run(context);
  325. CHECK(cells[1][1]->GetAbsoluteOffset(Rml::BoxArea::Border) == Vector2f(-50, -50));
  326. CHECK(cells[2][2]->GetAbsoluteOffset(Rml::BoxArea::Border) == Vector2f(0, 0));
  327. CHECK(cells[3][3]->GetAbsoluteOffset(Rml::BoxArea::Border) == Vector2f(50, 50));
  328. }
  329. SUBCASE("Smoothscroll")
  330. {
  331. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  332. system_interface->SetTime(0);
  333. cells[3][3]->ScrollIntoView({ScrollAlignment::Nearest, ScrollAlignment::Nearest, ScrollBehavior::Smooth});
  334. constexpr double dt = 1.0 / 15.0;
  335. system_interface->SetTime(dt);
  336. Run(context);
  337. // We don't define the exact offset at this time step, but it should be somewhere between the start and end offsets.
  338. Vector2f offset = cells[3][3]->GetAbsoluteOffset(Rml::BoxArea::Border);
  339. CHECK(offset.x > 50.f);
  340. CHECK(offset.y > 50.f);
  341. CHECK(offset.x < 75.f);
  342. CHECK(offset.y < 75.f);
  343. // After one second it should be at the destination offset.
  344. for (double t = 2.0 * dt; t < 1.0; t += dt)
  345. {
  346. system_interface->SetTime(t);
  347. Run(context);
  348. }
  349. CHECK(cells[3][3]->GetAbsoluteOffset(Rml::BoxArea::Border) == Vector2f(50, 50));
  350. }
  351. }
  352. document->Close();
  353. TestsShell::ShutdownShell();
  354. }