ElementFormControlSelect.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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 <RmlUi/Core/DataModelHandle.h>
  33. #include <RmlUi/Core/Elements/ElementFormControlSelect.h>
  34. #include <RmlUi/Core/EventListener.h>
  35. #include <doctest.h>
  36. using namespace Rml;
  37. static const String basic_doc_rml = R"(
  38. <rml>
  39. <head>
  40. <title>Test</title>
  41. <link type="text/rcss" href="/assets/rml.rcss"/>
  42. <link type="text/rcss" href="/assets/invader.rcss"/>
  43. </head>
  44. <body/>
  45. </rml>
  46. )";
  47. static const String data_model_doc_rml_pre = R"(
  48. <rml>
  49. <head>
  50. <title>Test</title>
  51. <link type="text/template" href="/assets/window.rml"/>
  52. <style>
  53. body.window
  54. {
  55. top: 100px;
  56. left: 200px;
  57. width: 600px;
  58. height: 450px;
  59. }
  60. </style>
  61. </head>
  62. <body template="window">
  63. <div data-model="select-test" id="select_wrap">
  64. )";
  65. static const String data_model_doc_rml_post = R"(
  66. </div>
  67. </body>
  68. </rml>
  69. )";
  70. static String GetSelectValueRml(ElementFormControlSelect* select_element)
  71. {
  72. for (int child_index = 0; child_index < select_element->GetNumChildren(true); child_index++)
  73. {
  74. Element* child = select_element->GetChild(child_index);
  75. if (child->GetTagName() == "selectvalue")
  76. return child->GetInnerRML();
  77. }
  78. return String();
  79. }
  80. TEST_CASE("form.select.value")
  81. {
  82. Context* context = TestsShell::GetContext();
  83. REQUIRE(context);
  84. struct Test {
  85. const String rml;
  86. const String expected_value;
  87. const String expected_value_rml;
  88. };
  89. Test tests[] = {
  90. {
  91. R"(
  92. <select>
  93. <option>A</option>
  94. <option>B</option>
  95. <option>C</option>
  96. <option>D</option>
  97. </select>
  98. )", "", "A"
  99. },
  100. {
  101. R"(
  102. <select>
  103. <option value="a">A</option>
  104. <option>B</option>
  105. <option>C</option>
  106. <option>D</option>
  107. </select>
  108. )", "a", "A"
  109. },
  110. {
  111. R"(
  112. <select value="z">
  113. <option>A</option>
  114. <option>B</option>
  115. <option>C</option>
  116. <option>D</option>
  117. </select>
  118. )", "", "A"
  119. },
  120. {
  121. R"(
  122. <select value="c">
  123. <option value="a">A</option>
  124. <option value="b">B</option>
  125. <option value="c">C</option>
  126. <option value="d">D</option>
  127. </select>
  128. )", "c", "C"
  129. },
  130. {
  131. R"(
  132. <select value="c">
  133. <option value="a">A</option>
  134. <option value="b">B</option>
  135. <option value="c">C</option>
  136. <option value="d" selected>D</option>
  137. </select>
  138. )", "d", "D"
  139. },
  140. {
  141. R"(
  142. <select value="c">
  143. <option value="a" selected>A</option>
  144. <option value="b">B</option>
  145. <option value="c">C</option>
  146. <option value="d">D</option>
  147. </select>
  148. )", "a", "A"
  149. },
  150. {
  151. R"(
  152. <select value="c">
  153. <option value="a" selected>A</option>
  154. <option value="b">B</option>
  155. <option value="c">C</option>
  156. <option value="d" selected>D</option>
  157. </select>
  158. )", "d", "D"
  159. },
  160. {
  161. R"(
  162. <select>
  163. <option value="a">A</option>
  164. <option value="b">B</option>
  165. <option value="c" selected>C</option>
  166. <option value="d">D</option>
  167. </select>
  168. )", "c", "C"
  169. },
  170. };
  171. ElementDocument* document = context->LoadDocumentFromMemory(basic_doc_rml);
  172. REQUIRE(document);
  173. document->Show();
  174. context->Update();
  175. context->Render();
  176. int i = 0;
  177. for (const Test& test : tests)
  178. {
  179. document->SetInnerRML(test.rml);
  180. context->Update();
  181. REQUIRE(document->GetNumChildren() == 1);
  182. ElementFormControlSelect* select_element = rmlui_dynamic_cast<ElementFormControlSelect*>(document->GetFirstChild());
  183. REQUIRE(select_element);
  184. const String value = select_element->GetValue();
  185. const String selectvalue_rml = GetSelectValueRml(select_element);
  186. CHECK_MESSAGE(value == test.expected_value, "Document " << i);
  187. CHECK_MESSAGE(selectvalue_rml == test.expected_value_rml, "Document " << i);
  188. i++;
  189. }
  190. document->Close();
  191. TestsShell::ShutdownShell();
  192. }
  193. TEST_CASE("form.select.databinding")
  194. {
  195. Context* context = TestsShell::GetContext();
  196. REQUIRE(context);
  197. struct Test {
  198. const String rml;
  199. const String expected_value;
  200. const String expected_value_rml;
  201. };
  202. int selected_index = 2;
  203. String selected_value = "b";
  204. Vector<String> values = { "a", "b", "c", "d" };
  205. Test tests[] = {
  206. {
  207. R"(
  208. <select data-value="selected_index">
  209. <option value="0">A</option>
  210. <option value="1">B</option>
  211. <option value="2">C</option>
  212. <option value="3">D</option>
  213. </select>
  214. )", "2", "C"
  215. },
  216. {
  217. R"(
  218. <select data-value="selected_value">
  219. <option value="a">A</option>
  220. <option value="b">B</option>
  221. <option value="c">C</option>
  222. <option value="d">D</option>
  223. </select>
  224. )", "b", "B"
  225. },
  226. {
  227. R"(
  228. <select>
  229. <option value="a" data-attrif-selected="selected_value == 'a'">A</option>
  230. <option value="b" data-attrif-selected="selected_value == 'b'">B</option>
  231. <option value="c" data-attrif-selected="selected_value == 'c'">C</option>
  232. <option value="d" data-attrif-selected="selected_value == 'd'">D</option>
  233. </select>
  234. )", "b", "B"
  235. },
  236. {
  237. R"(
  238. <select>
  239. <option data-for="s : subjects" data-attr-value="s" data-attrif-selected="s == selected_value">{{ s | to_upper }}</option>
  240. </select>
  241. )", "b", "B"
  242. },
  243. {
  244. R"(
  245. <select>
  246. <option data-for="s : subjects" data-attr-value="it_index" data-attrif-selected="it_index == selected_index">{{ s | to_upper }}</option>
  247. </select>
  248. )", "2", "C"
  249. },
  250. {
  251. R"(
  252. <select data-value="selected_value">
  253. <option data-for="s : subjects" data-attr-value="s">{{ s | to_upper }}</option>
  254. </select>
  255. )", "b", "B"
  256. },
  257. {
  258. R"(
  259. <select data-value="selected_index">
  260. <option data-for="s : subjects" data-attr-value="it_index">{{ s | to_upper }}</option>
  261. </select>
  262. )", "2", "C"
  263. },
  264. };
  265. DataModelConstructor constructor = context->CreateDataModel("select-test");
  266. constructor.RegisterArray<Vector<String>>();
  267. constructor.Bind("selected_index", &selected_index);
  268. constructor.Bind("selected_value", &selected_value);
  269. constructor.Bind("subjects", &values);
  270. int i = 0;
  271. for (const Test& test : tests)
  272. {
  273. const String document_rml = data_model_doc_rml_pre + test.rml + data_model_doc_rml_post;
  274. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  275. REQUIRE(document);
  276. document->Show();
  277. TestsShell::RenderLoop();
  278. Element* wrapper_element = document->GetElementById("select_wrap");
  279. REQUIRE(wrapper_element);
  280. REQUIRE(wrapper_element->GetNumChildren() == 1);
  281. ElementFormControlSelect* select_element = rmlui_dynamic_cast<ElementFormControlSelect*>(wrapper_element->GetFirstChild());
  282. REQUIRE(select_element);
  283. const String value = select_element->GetValue();
  284. const String selectvalue_rml = GetSelectValueRml(select_element);
  285. CHECK_MESSAGE(value == test.expected_value, "Document " << i);
  286. CHECK_MESSAGE(selectvalue_rml == test.expected_value_rml, "Document " << i);
  287. i++;
  288. document->Close();
  289. }
  290. TestsShell::ShutdownShell();
  291. }
  292. TEST_CASE("form.select.data-for")
  293. {
  294. Context* context = TestsShell::GetContext();
  295. REQUIRE(context);
  296. struct Test {
  297. const String rml;
  298. const String expected_value;
  299. const String expected_value_rml;
  300. };
  301. String selected_value = "b";
  302. Vector<String> values = { "a", "b", "c", "d" };
  303. DataModelConstructor constructor = context->CreateDataModel("select-test");
  304. constructor.RegisterArray<Vector<String>>();
  305. constructor.Bind("selected_value", &selected_value);
  306. constructor.Bind("subjects", &values);
  307. DataModelHandle handle = constructor.GetModelHandle();
  308. {
  309. const String select_rml = R"(
  310. <select data-value="selected_value">
  311. <option data-for="s : subjects" data-attr-value="s">{{ s | to_upper }}</option>
  312. </select>
  313. )";
  314. const String document_rml = data_model_doc_rml_pre + select_rml + data_model_doc_rml_post;
  315. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  316. REQUIRE(document);
  317. document->Show();
  318. Element* wrapper_element = document->GetElementById("select_wrap");
  319. REQUIRE(wrapper_element);
  320. REQUIRE(wrapper_element->GetNumChildren() == 1);
  321. ElementFormControlSelect* select_element = rmlui_dynamic_cast<ElementFormControlSelect*>(wrapper_element->GetFirstChild());
  322. REQUIRE(select_element);
  323. {
  324. //TestsShell::RenderLoop();
  325. const String value = select_element->GetValue();
  326. const int selected_index = select_element->GetSelection();
  327. const String selectvalue_rml = GetSelectValueRml(select_element);
  328. CHECK(value == "b");
  329. CHECK(selected_index == 1);
  330. CHECK(selectvalue_rml == "B");
  331. }
  332. {
  333. selected_value = "d";
  334. handle.DirtyVariable("selected_value");
  335. context->Update();
  336. //TestsShell::RenderLoop();
  337. const String value = select_element->GetValue();
  338. const int selected_index = select_element->GetSelection();
  339. const String selectvalue_rml = GetSelectValueRml(select_element);
  340. CHECK(value == "d");
  341. CHECK(selected_index == 3);
  342. CHECK(selectvalue_rml == "D");
  343. }
  344. {
  345. select_element->SetValue("a");
  346. context->Update();
  347. TestsShell::RenderLoop();
  348. const String value = select_element->GetValue();
  349. const int selected_index = select_element->GetSelection();
  350. const String selectvalue_rml = GetSelectValueRml(select_element);
  351. CHECK(selected_value == "a");
  352. CHECK(value == "a");
  353. CHECK(selected_index == 0);
  354. CHECK(selectvalue_rml == "A");
  355. }
  356. {
  357. select_element->SetSelection(1);
  358. context->Update();
  359. TestsShell::RenderLoop();
  360. const String value = select_element->GetValue();
  361. const int selected_index = select_element->GetSelection();
  362. const String selectvalue_rml = GetSelectValueRml(select_element);
  363. CHECK(selected_value == "b");
  364. CHECK(value == "b");
  365. CHECK(selected_index == 1);
  366. CHECK(selectvalue_rml == "B");
  367. }
  368. #if 0
  369. // These are not supported now, we may want to add support later.
  370. {
  371. // Values: a c b d
  372. std::swap(values[1], values[2]);
  373. handle.DirtyVariable("subjects");
  374. context->Update();
  375. TestsShell::RenderLoop();
  376. const String value = select_element->GetValue();
  377. const int selected_index = select_element->GetSelection();
  378. const String selectvalue_rml = GetSelectValueRml(select_element);
  379. CHECK(value == "b");
  380. CHECK(selected_index == 2);
  381. CHECK(selectvalue_rml == "B");
  382. }
  383. {
  384. // Values: c b d
  385. values.erase(values.begin());
  386. handle.DirtyVariable("subjects");
  387. context->Update();
  388. TestsShell::RenderLoop();
  389. const String value = select_element->GetValue();
  390. const int selected_index = select_element->GetSelection();
  391. const String selectvalue_rml = GetSelectValueRml(select_element);
  392. CHECK(value == "b");
  393. CHECK(selected_index == 1);
  394. CHECK(selectvalue_rml == "B");
  395. }
  396. #endif
  397. document->Close();
  398. }
  399. TestsShell::ShutdownShell();
  400. }
  401. static const String event_doc_rml = R"(
  402. <rml>
  403. <head>
  404. <link type="text/rcss" href="/assets/rml.rcss"/>
  405. <link type="text/rcss" href="/assets/invader.rcss"/>
  406. </head>
  407. <body>
  408. <select id="sel">
  409. <option selected>Cube</option>
  410. <option>Cone</option>
  411. <option value="c">Cylinder</option>
  412. <option value="d">Sphere</option>
  413. </select>
  414. </body>
  415. </rml>
  416. )";
  417. static void ClickAt(Context* context, int x, int y)
  418. {
  419. context->Update();
  420. context->Render();
  421. context->ProcessMouseMove(x, y, 0);
  422. context->Update();
  423. context->Render();
  424. context->ProcessMouseButtonDown(0, 0);
  425. context->ProcessMouseButtonUp(0, 0);
  426. context->Update();
  427. context->Render();
  428. }
  429. TEST_CASE("form.select.event.change")
  430. {
  431. Context* context = TestsShell::GetContext();
  432. REQUIRE(context);
  433. ElementDocument* document = context->LoadDocumentFromMemory(event_doc_rml);
  434. REQUIRE(document);
  435. document->Show();
  436. struct SelectionEventListener : public Rml::EventListener {
  437. void ProcessEvent(Rml::Event& ev) override
  438. {
  439. num_events_processed += 1;
  440. value = ev.GetParameter<Rml::String>("value", "*empty*");
  441. }
  442. int num_events_processed = 0;
  443. String value;
  444. };
  445. auto listener = MakeUnique<SelectionEventListener>();
  446. document->GetElementById("sel")->AddEventListener(Rml::EventId::Change, listener.get());
  447. ClickAt(context, 100, 20); // Open select
  448. ClickAt(context, 100, 40); // Click option 'Cube'
  449. CHECK(listener->num_events_processed == 0);
  450. ClickAt(context, 100, 20); // Open select
  451. ClickAt(context, 100, 63); // Click option 'Cone'
  452. CHECK(listener->num_events_processed == 1);
  453. CHECK(listener->value == "");
  454. ClickAt(context, 100, 20); // Open select again
  455. ClickAt(context, 100, 85); // Click option 'Cylinder'
  456. CHECK(listener->num_events_processed == 2);
  457. CHECK(listener->value == "c");
  458. ClickAt(context, 100, 20); // Open select again
  459. context->ProcessMouseMove(100, 85, 0); // Hover option 'Cylinder'
  460. context->Update();
  461. context->Render();
  462. context->ProcessKeyDown(Rml::Input::KI_DOWN, 0);
  463. context->Update();
  464. context->Render();
  465. CHECK(listener->num_events_processed == 3);
  466. CHECK(listener->value == "d");
  467. context->ProcessKeyDown(Rml::Input::KI_DOWN, 0);
  468. context->Update();
  469. context->Render();
  470. CHECK(listener->num_events_processed == 3);
  471. document->Close();
  472. TestsShell::ShutdownShell();
  473. }