ElementFormControlSelect.cpp 12 KB

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