Element.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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/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. {
  170. mockEventListener.reset(new MockEventListener());
  171. expectations.emplace_back(NAMED_ALLOW_CALL(*mockEventListener, OnAttach(button)));
  172. expectations.emplace_back(NAMED_ALLOW_CALL(*mockEventListener, OnDetach(button))
  173. .LR_SIDE_EFFECT(mockEventListener.reset()));
  174. };
  175. MockEventListenerInstancer mockEventListenerInstancer;
  176. const auto configureMockEventListenerInstancer = [&](const auto value)
  177. {
  178. expectations.emplace_back(NAMED_REQUIRE_CALL(mockEventListenerInstancer, InstanceEventListener(value, button))
  179. .LR_SIDE_EFFECT(configureMockEventListener())
  180. .LR_RETURN(mockEventListener.get()));
  181. };
  182. Factory::RegisterEventListenerInstancer(&mockEventListenerInstancer);
  183. configureMockEventListenerInstancer(ON_CLICK_VALUE);
  184. button->SetAttribute(ON_CLICK_ATTRIBUTE, ON_CLICK_VALUE);
  185. SUBCASE("Replacement")
  186. {
  187. static constexpr auto REPLACEMENT_ON_CLICK_VALUE = "boo";
  188. configureMockEventListenerInstancer(REPLACEMENT_ON_CLICK_VALUE);
  189. button->SetAttribute(ON_CLICK_ATTRIBUTE, REPLACEMENT_ON_CLICK_VALUE);
  190. }
  191. button->RemoveAttribute(ON_CLICK_ATTRIBUTE);
  192. }
  193. SUBCASE("Simple")
  194. {
  195. static constexpr auto DISABLED_ATTRIBUTE = "disabled";
  196. REQUIRE_FALSE(button->HasAttribute(DISABLED_ATTRIBUTE));
  197. button->SetAttribute(DISABLED_ATTRIBUTE, "");
  198. REQUIRE(button->HasAttribute(DISABLED_ATTRIBUTE));
  199. SUBCASE("Replacement")
  200. {
  201. static constexpr auto VALUE = "something";
  202. button->SetAttribute(DISABLED_ATTRIBUTE, VALUE);
  203. const auto* attributeValue = button->GetAttribute(DISABLED_ATTRIBUTE);
  204. REQUIRE(attributeValue);
  205. REQUIRE(attributeValue->GetType() == Variant::Type::STRING);
  206. CHECK(attributeValue->Get<String>() == VALUE);
  207. }
  208. button->RemoveAttribute(DISABLED_ATTRIBUTE);
  209. CHECK_FALSE(button->HasAttribute(DISABLED_ATTRIBUTE));
  210. }
  211. }
  212. SUBCASE("CloneDrag")
  213. {
  214. // Simulate input for mouse click and drag
  215. context->ProcessMouseMove(10, 10, 0);
  216. context->Update();
  217. context->Render();
  218. context->ProcessMouseButtonDown(0, 0);
  219. context->Update();
  220. context->Render();
  221. // This should initiate a drag clone.
  222. context->ProcessMouseMove(10, 11, 0);
  223. context->Update();
  224. context->Render();
  225. context->ProcessMouseButtonUp(0, 0);
  226. }
  227. SUBCASE("CloneManual")
  228. {
  229. Element* element = document->GetFirstChild();
  230. REQUIRE(element->GetProperty<String>("background-color") == "255, 0, 0, 255");
  231. CHECK(element->Clone()->GetProperty<String>("background-color") == "255, 0, 0, 255");
  232. element->SetProperty("background-color", "#0f0");
  233. CHECK(element->Clone()->GetProperty<String>("background-color") == "0, 255, 0, 255");
  234. element->RemoveProperty("background-color");
  235. Element* clone = document->AppendChild(element->Clone());
  236. context->Update();
  237. CHECK(clone->GetProperty<String>("background-color") == "255, 255, 255, 255");
  238. element->SetClass("blue", true);
  239. clone = document->AppendChild(element->Clone());
  240. context->Update();
  241. CHECK(clone->GetProperty<String>("background-color") == "0, 0, 255, 255");
  242. }
  243. SUBCASE("SetInnerRML")
  244. {
  245. Element* element = document->GetFirstChild();
  246. CHECK(element->GetInnerRML() == "This is a <span>sample</span>.");
  247. element->SetInnerRML("text");
  248. CHECK(element->GetInnerRML() == "text");
  249. const char* inner_rml = R"(before<div class="blue">child</div>after)";
  250. element->SetInnerRML(inner_rml);
  251. CHECK(element->GetInnerRML() == inner_rml);
  252. ElementPtr element_ptr = document->CreateElement("div");
  253. CHECK(element_ptr->GetInnerRML() == "");
  254. element_ptr->SetInnerRML("text");
  255. CHECK(element_ptr->GetInnerRML() == "text");
  256. }
  257. document->Close();
  258. TestsShell::ShutdownShell();
  259. }
  260. TEST_CASE("Element.ScrollIntoView")
  261. {
  262. Context* context = TestsShell::GetContext();
  263. REQUIRE(context);
  264. ElementDocument* document = context->LoadDocumentFromMemory(document_scroll_rml);
  265. REQUIRE(document);
  266. document->Show();
  267. Run(context);
  268. Element* scrollable = document->GetElementById("scrollable");
  269. REQUIRE(scrollable);
  270. Element* cells[4][4]{};
  271. for (int i = 0; i < 4; ++i)
  272. {
  273. for (int j = 0; j < 4; ++j)
  274. {
  275. cells[i][j] = document->GetElementById(CreateString(8, "cell%d%d", i, j));
  276. REQUIRE(cells[i][j]);
  277. }
  278. }
  279. REQUIRE(cells[0][0]->GetAbsoluteOffset(Rml::Box::Area::BORDER) == Vector2f(0, 0));
  280. REQUIRE(cells[2][2]->GetAbsoluteOffset(Rml::Box::Area::BORDER) == Vector2f(100, 100));
  281. REQUIRE(cells[3][3]->GetAbsoluteOffset(Rml::Box::Area::BORDER) == Vector2f(150, 150));
  282. REQUIRE(scrollable->GetScrollLeft() == 0);
  283. REQUIRE(scrollable->GetScrollTop() == 0);
  284. SUBCASE("LegacyScroll")
  285. {
  286. cells[2][2]->ScrollIntoView(true);
  287. Run(context);
  288. CHECK(cells[0][0]->GetAbsoluteOffset(Rml::Box::Area::BORDER) == Vector2f(-50, -100));
  289. CHECK(cells[2][2]->GetAbsoluteOffset(Rml::Box::Area::BORDER) == Vector2f(50, 0));
  290. CHECK(cells[3][3]->GetAbsoluteOffset(Rml::Box::Area::BORDER) == Vector2f(100, 50));
  291. CHECK(scrollable->GetScrollLeft() == 50);
  292. CHECK(scrollable->GetScrollTop() == 100);
  293. cells[2][2]->ScrollIntoView(false);
  294. Run(context);
  295. CHECK(cells[0][0]->GetAbsoluteOffset(Rml::Box::Area::BORDER) == Vector2f(-50, -50));
  296. CHECK(cells[2][2]->GetAbsoluteOffset(Rml::Box::Area::BORDER) == Vector2f(50, 50));
  297. CHECK(cells[3][3]->GetAbsoluteOffset(Rml::Box::Area::BORDER) == Vector2f(100, 100));
  298. CHECK(scrollable->GetScrollLeft() == 50);
  299. CHECK(scrollable->GetScrollTop() == 50);
  300. }
  301. SUBCASE("AdvancedScroll")
  302. {
  303. cells[2][2]->ScrollIntoView({ScrollAlignment::Center, ScrollAlignment::Center});
  304. Run(context);
  305. CHECK(cells[0][0]->GetAbsoluteOffset(Rml::Box::Area::BORDER) == Vector2f(-75, -75));
  306. CHECK(cells[2][2]->GetAbsoluteOffset(Rml::Box::Area::BORDER) == Vector2f(25, 25));
  307. CHECK(cells[3][3]->GetAbsoluteOffset(Rml::Box::Area::BORDER) == Vector2f(75, 75));
  308. SUBCASE("NearestAlready")
  309. {
  310. cells[2][2]->ScrollIntoView(ScrollAlignment::Nearest);
  311. Run(context);
  312. CHECK(cells[0][0]->GetAbsoluteOffset(Rml::Box::Area::BORDER) == Vector2f(-75, -75));
  313. CHECK(cells[2][2]->GetAbsoluteOffset(Rml::Box::Area::BORDER) == Vector2f(25, 25));
  314. CHECK(cells[3][3]->GetAbsoluteOffset(Rml::Box::Area::BORDER) == Vector2f(75, 75));
  315. }
  316. SUBCASE("NearestBefore")
  317. {
  318. cells[1][1]->ScrollIntoView(ScrollAlignment::Nearest);
  319. Run(context);
  320. CHECK(cells[0][0]->GetAbsoluteOffset(Rml::Box::Area::BORDER) == Vector2f(-50, -50));
  321. CHECK(cells[1][1]->GetAbsoluteOffset(Rml::Box::Area::BORDER) == Vector2f(0, 0));
  322. CHECK(cells[2][2]->GetAbsoluteOffset(Rml::Box::Area::BORDER) == Vector2f(50, 50));
  323. }
  324. SUBCASE("NearestAfter")
  325. {
  326. cells[3][3]->ScrollIntoView(ScrollAlignment::Nearest);
  327. Run(context);
  328. CHECK(cells[1][1]->GetAbsoluteOffset(Rml::Box::Area::BORDER) == Vector2f(-50, -50));
  329. CHECK(cells[2][2]->GetAbsoluteOffset(Rml::Box::Area::BORDER) == Vector2f(0, 0));
  330. CHECK(cells[3][3]->GetAbsoluteOffset(Rml::Box::Area::BORDER) == Vector2f(50, 50));
  331. }
  332. SUBCASE("Smoothscroll")
  333. {
  334. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  335. system_interface->SetTime(0);
  336. cells[3][3]->ScrollIntoView({ScrollAlignment::Nearest, ScrollAlignment::Nearest, ScrollBehavior::Smooth});
  337. constexpr double dt = 1.0 / 15.0;
  338. system_interface->SetTime(dt);
  339. Run(context);
  340. // We don't define the exact offset at this time step, but it should be somewhere between the start and end offsets.
  341. Vector2f offset = cells[3][3]->GetAbsoluteOffset(Rml::Box::Area::BORDER);
  342. CHECK(offset.x > 50.f);
  343. CHECK(offset.y > 50.f);
  344. CHECK(offset.x < 75.f);
  345. CHECK(offset.y < 75.f);
  346. // After one second it should be at the destination offset.
  347. for (double t = 2.0 * dt; t < 1.0; t += dt)
  348. {
  349. system_interface->SetTime(t);
  350. Run(context);
  351. }
  352. CHECK(cells[3][3]->GetAbsoluteOffset(Rml::Box::Area::BORDER) == Vector2f(50, 50));
  353. }
  354. }
  355. document->Close();
  356. TestsShell::ShutdownShell();
  357. }