2
0

DataBinding.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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/DataModelHandle.h>
  31. #include <RmlUi/Core/Element.h>
  32. #include <RmlUi/Core/ElementDocument.h>
  33. #include <doctest.h>
  34. #include <map>
  35. using namespace Rml;
  36. namespace {
  37. static const String document_rml = R"(
  38. <rml>
  39. <head>
  40. <title>Test</title>
  41. <link type="text/template" href="/assets/window.rml"/>
  42. <style>
  43. body.window
  44. {
  45. left: 50px;
  46. right: 50px;
  47. top: 30px;
  48. bottom: 30px;
  49. max-width: none;
  50. max-height: none;
  51. }
  52. div#content
  53. {
  54. text-align: left;
  55. padding: 50px;
  56. box-sizing: border-box;
  57. }
  58. </style>
  59. </head>
  60. <body template="window">
  61. <div data-model="basics">
  62. <input type="text" data-value="i0"/>
  63. <h1>Globals</h1>
  64. <p>{{ i0 }}</p>
  65. <p>{{ i1 }}</p>
  66. <p>{{ i2 }}</p>
  67. <p>{{ i3 }}</p>
  68. <p>{{ s0 }}</p>
  69. <p>{{ s1 }}</p>
  70. <p>{{ s2.val }}</p>
  71. <p>{{ s3.val }}</p>
  72. <p>{{ s4.val }}</p>
  73. <p>{{ s5.val }}</p>
  74. <h1>Basic</h1>
  75. <p>{{ basic.a }}</p>
  76. <p>{{ basic.b }}</p>
  77. <p>{{ basic.c }}</p>
  78. <p>{{ basic.d }}</p>
  79. <p>{{ basic.e }}</p>
  80. <p>{{ basic.f }}</p>
  81. <h1>Wrapped</h1>
  82. <p>{{ wrapped.a.val }}</p>
  83. <p>{{ wrapped.b.val }}</p>
  84. <p>{{ wrapped.c.val }}</p>
  85. <p>{{ wrapped.d.val }}</p>
  86. <p>{{ wrapped.e.val }}</p>
  87. <h1>Pointed</h1>
  88. <p>{{ pointed.a.val }}</p>
  89. <p>{{ pointed.b.val }}</p>
  90. <p>{{ pointed.c.val }}</p>
  91. <h1>Arrays</h1>
  92. <p><span data-for="arrays.a">{{ it }} </span></p>
  93. <p><span data-for="arrays.b">{{ it }} </span></p>
  94. <p><span data-for="arrays.c">{{ it.val }} </span></p>
  95. <p><span data-for="arrays.d">{{ it.val }} </span></p>
  96. <p><span data-for="arrays.e">{{ it.val }} </span></p>
  97. </div>
  98. </body>
  99. </rml>
  100. )";
  101. static const String inside_string_rml = R"(
  102. <rml>
  103. <head>
  104. <title>Test</title>
  105. <link type="text/template" href="/assets/window.rml"/>
  106. <style>
  107. body.window
  108. {
  109. left: 50px;
  110. right: 50px;
  111. top: 30px;
  112. bottom: 30px;
  113. max-width: -1px;
  114. max-height: -1px;
  115. }
  116. </style>
  117. </head>
  118. <body template="window">
  119. <div data-model="basics">
  120. <p>{{ i0 }}</p>
  121. <p>{{ 'i0' }}</p>
  122. <p>{{ 'i{}23' }}</p>
  123. <p>before {{ 'i{{test}}23' }} test</p>
  124. <p>a {{ 'i' }} b {{ 'j' }} c</p>
  125. <p>{{i0}}</p>
  126. </div>
  127. </body>
  128. </rml>
  129. )";
  130. static const String aliasing_rml = R"(
  131. <rml>
  132. <head>
  133. <title>Test</title>
  134. <link type="text/rcss" href="/assets/rml.rcss"/>
  135. <link type="text/rcss" href="/assets/invader.rcss"/>
  136. <link type="text/template" href="/../Tests/Data/UnitTests/data-title.rml"/>
  137. <style>
  138. body {
  139. width: 600px;
  140. height: 400px;
  141. background: #ccc;
  142. color: #333;
  143. }
  144. .title-wrapper { border: 1dp red; }
  145. .icon { width: 64dp; height: 64dp; display: inline-block; }
  146. .icon[icon="a"] { decorator: image("/assets/high_scores_alien_1.tga"); }
  147. .icon[icon="b"] { decorator: image("/assets/high_scores_alien_2.tga"); }
  148. </style>
  149. </head>
  150. <body data-model="basics">
  151. <p>{{ i0 }}</p>
  152. <p data-alias-differentname="i0">{{ differentname }}</p>
  153. <div data-alias-title="s0" data-alias-icon="wrapped.a.val" id="w1">
  154. <template src="data-title"/>
  155. </div>
  156. <div data-alias-title="s1" data-alias-icon="wrapped.b.val" id="w2">
  157. <template src="data-title"/>
  158. </div>
  159. </body>
  160. </rml>
  161. )";
  162. struct StringWrap
  163. {
  164. StringWrap(String val = "wrap_default") : val(val) {}
  165. String val;
  166. };
  167. struct Globals
  168. {
  169. int i0 = 0;
  170. int* i1 = new int(1);
  171. UniquePtr<int> i2 = MakeUnique<int>(2);
  172. SharedPtr<int> i3 = MakeShared<int>(3);
  173. String s0 = "s0";
  174. String* s1 = new String("s1");
  175. StringWrap s2 = StringWrap("s2");
  176. StringWrap* s3 = new StringWrap("s3");
  177. UniquePtr<StringWrap> s4 = MakeUnique<StringWrap>("s4");
  178. SharedPtr<StringWrap> s5 = MakeShared<StringWrap>("s5");
  179. // Invalid
  180. const int x0 = 100; // Invalid: const variable
  181. const int* x1 = new int(101); // Invalid: const pointer
  182. UniquePtr<const int> x2 = MakeUnique<int>(102); // Invalid: const pointer
  183. const StringWrap* x3 = new StringWrap("x2"); // Invalid: const pointer
  184. UniquePtr<const StringWrap> x4 = MakeUnique<StringWrap>("x3"); // Invalid: const pointer
  185. } globals;
  186. struct Basic
  187. {
  188. int a = 1;
  189. int* b = new int(2);
  190. int GetC() {
  191. static int v = 5;
  192. return v;
  193. }
  194. int& GetD() {
  195. static int v = 5;
  196. return v;
  197. }
  198. int* GetE() {
  199. static int v = 6;
  200. return &v;
  201. }
  202. UniquePtr<int> GetF() {
  203. return MakeUnique<int>(7);
  204. }
  205. // Invalid: const member
  206. const int x0 = 2;
  207. // Invalid: const pointer
  208. const int* x1 = new int(3);
  209. // Invalid: const qualified member function
  210. int GetX2() const {
  211. return 4;
  212. }
  213. // Invalid: const reference return
  214. const int& GetX3() {
  215. static int g = 7;
  216. return g;
  217. }
  218. // Invalid: const pointer return
  219. const int* GetX4() {
  220. static int h = 8;
  221. return &h;
  222. }
  223. // Invalid: Illegal signature
  224. int GetX5(int) {
  225. return 9;
  226. }
  227. };
  228. struct Wrapped
  229. {
  230. StringWrap a = { "a" };
  231. StringWrap* b = new StringWrap("b");
  232. UniquePtr<StringWrap> c = MakeUnique<StringWrap>("c");
  233. StringWrap& GetD() {
  234. static StringWrap v = { "e" };
  235. return v;
  236. }
  237. StringWrap* GetE() {
  238. static StringWrap v = { "f" };
  239. return &v;
  240. }
  241. // Invalid: const pointer
  242. const StringWrap* x0 = new StringWrap("x0");
  243. // Invalid (run-time): Returning non-scalar variable by value.
  244. StringWrap GetX1() {
  245. return { "x1" };
  246. }
  247. // Invalid (run-time): Returning non-scalar variable by value.
  248. UniquePtr<StringWrap> GetX2() {
  249. return MakeUnique<StringWrap>("x2");
  250. }
  251. };
  252. using StringWrapPtr = UniquePtr<StringWrap>;
  253. struct Pointed
  254. {
  255. StringWrapPtr a = MakeUnique<StringWrap>("a");
  256. StringWrapPtr& GetB() {
  257. static StringWrapPtr v = MakeUnique<StringWrap>("b");
  258. return v;
  259. }
  260. StringWrapPtr* GetC() {
  261. static StringWrapPtr v = MakeUnique<StringWrap>("c");
  262. return &v;
  263. }
  264. // Invalid: We disallow recursive pointer types (pointer to pointer)
  265. StringWrapPtr* x0 = new StringWrapPtr(new StringWrap("x0"));
  266. // Invalid (run-time error): Only scalar data members can be returned by value
  267. StringWrapPtr GetX1() {
  268. return MakeUnique<StringWrap>("x1");
  269. }
  270. };
  271. struct Arrays
  272. {
  273. Vector<int> a = { 10, 11, 12 };
  274. Vector<int*> b = { new int(20), new int(21), new int(22) };
  275. Vector<StringWrap> c = { StringWrap("c1"), StringWrap("c2"), StringWrap("c3") };
  276. Vector<StringWrap*> d = { new StringWrap("d1"), new StringWrap("d2"), new StringWrap("d3") };
  277. Vector<StringWrapPtr> e;
  278. // Invalid: const pointer
  279. Vector<const int*> x0 = { new int(30), new int(31), new int(32) };
  280. // Invalid: const pointer
  281. Vector<UniquePtr<const StringWrap>> x1;
  282. Arrays() {
  283. e.emplace_back(MakeUnique<StringWrap>("e1"));
  284. e.emplace_back(MakeUnique<StringWrap>("e2"));
  285. e.emplace_back(MakeUnique<StringWrap>("e3"));
  286. x1.emplace_back(MakeUnique<StringWrap>("x1_1"));
  287. x1.emplace_back(MakeUnique<StringWrap>("x1_2"));
  288. x1.emplace_back(MakeUnique<StringWrap>("x1_3"));
  289. }
  290. };
  291. DataModelHandle model_handle;
  292. bool InitializeDataBindings(Context* context)
  293. {
  294. Rml::DataModelConstructor constructor = context->CreateDataModel("basics");
  295. if (!constructor)
  296. return false;
  297. if (auto handle = constructor.RegisterStruct<StringWrap>())
  298. {
  299. handle.RegisterMember("val", &StringWrap::val);
  300. }
  301. {
  302. // Globals
  303. constructor.Bind("i0", &globals.i0);
  304. constructor.Bind("i1", &globals.i1);
  305. constructor.Bind("i2", &globals.i2);
  306. constructor.Bind("i3", &globals.i3);
  307. constructor.Bind("s0", &globals.s0);
  308. constructor.Bind("s1", &globals.s1);
  309. constructor.Bind("s2", &globals.s2);
  310. constructor.Bind("s3", &globals.s3);
  311. constructor.Bind("s4", &globals.s4);
  312. constructor.Bind("s5", &globals.s5);
  313. // Invalid: Each of the following should give a compile-time failure.
  314. //constructor.Bind("x0", &globals.x0);
  315. //constructor.Bind("x1", &globals.x1);
  316. //constructor.Bind("x2", &globals.x2);
  317. //constructor.Bind("x3", &globals.x3);
  318. //constructor.Bind("x4", &globals.x4);
  319. }
  320. if (auto handle = constructor.RegisterStruct<Basic>())
  321. {
  322. handle.RegisterMember("a", &Basic::a);
  323. handle.RegisterMember("b", &Basic::b);
  324. handle.RegisterMember("c", &Basic::GetC);
  325. handle.RegisterMember("d", &Basic::GetD);
  326. handle.RegisterMember("e", &Basic::GetE);
  327. handle.RegisterMember("f", &Basic::GetF);
  328. //handle.RegisterMember("x0", &Basic::x0);
  329. //handle.RegisterMember("x1", &Basic::x1);
  330. //handle.RegisterMember("x2", &Basic::GetX2);
  331. //handle.RegisterMember("x3", &Basic::GetX3);
  332. //handle.RegisterMember("x4", &Basic::GetX4);
  333. //handle.RegisterMember("x5", &Basic::GetX5);
  334. }
  335. constructor.Bind("basic", new Basic);
  336. if (auto handle = constructor.RegisterStruct<Wrapped>())
  337. {
  338. handle.RegisterMember("a", &Wrapped::a);
  339. handle.RegisterMember("b", &Wrapped::b);
  340. handle.RegisterMember("c", &Wrapped::c);
  341. handle.RegisterMember("d", &Wrapped::GetD);
  342. handle.RegisterMember("e", &Wrapped::GetE);
  343. //handle.RegisterMember("x0", &Wrapped::x0);
  344. //handle.RegisterMember("x1", &Wrapped::GetX1);
  345. //handle.RegisterMember("x2", &Wrapped::GetX2);
  346. }
  347. constructor.Bind("wrapped", new Wrapped);
  348. if (auto handle = constructor.RegisterStruct<Pointed>())
  349. {
  350. handle.RegisterMember("a", &Pointed::a);
  351. handle.RegisterMember("b", &Pointed::GetB);
  352. handle.RegisterMember("c", &Pointed::GetC);
  353. //handle.RegisterMember("x0", &Pointed::x0);
  354. //handle.RegisterMember("x1", &Pointed::GetX1);
  355. }
  356. constructor.Bind("pointed", new Pointed);
  357. constructor.RegisterArray<decltype(Arrays::a)>();
  358. constructor.RegisterArray<decltype(Arrays::b)>();
  359. constructor.RegisterArray<decltype(Arrays::c)>();
  360. constructor.RegisterArray<decltype(Arrays::d)>();
  361. constructor.RegisterArray<decltype(Arrays::e)>();
  362. //constructor.RegisterArray<decltype(Arrays::x0)>();
  363. //constructor.RegisterArray<decltype(Arrays::x1)>();
  364. if (auto handle = constructor.RegisterStruct<Arrays>())
  365. {
  366. handle.RegisterMember("a", &Arrays::a);
  367. handle.RegisterMember("b", &Arrays::b);
  368. handle.RegisterMember("c", &Arrays::c);
  369. handle.RegisterMember("d", &Arrays::d);
  370. handle.RegisterMember("e", &Arrays::e);
  371. //handle.RegisterMember("x0", &Arrays::x0);
  372. //handle.RegisterMember("x1", &Arrays::x1);
  373. }
  374. constructor.Bind("arrays", new Arrays);
  375. model_handle = constructor.GetModelHandle();
  376. return true;
  377. }
  378. } // Anonymous namespace
  379. TEST_CASE("databinding")
  380. {
  381. Context* context = TestsShell::GetContext();
  382. REQUIRE(context);
  383. REQUIRE(InitializeDataBindings(context));
  384. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  385. REQUIRE(document);
  386. document->Show();
  387. TestsShell::RenderLoop();
  388. document->Close();
  389. TestsShell::ShutdownShell();
  390. }
  391. TEST_CASE("databinding.inside_string")
  392. {
  393. Context* context = TestsShell::GetContext();
  394. REQUIRE(context);
  395. REQUIRE(InitializeDataBindings(context));
  396. ElementDocument* document = context->LoadDocumentFromMemory(inside_string_rml);
  397. REQUIRE(document);
  398. document->Show();
  399. TestsShell::RenderLoop();
  400. CHECK(document->QuerySelector("p:nth-child(4)")->GetInnerRML() == "before i{{test}}23 test");
  401. CHECK(document->QuerySelector("p:nth-child(5)")->GetInnerRML() == "a i b j c");
  402. document->Close();
  403. TestsShell::ShutdownShell();
  404. }
  405. TEST_CASE("databinding.aliasing")
  406. {
  407. Context* context = TestsShell::GetContext();
  408. REQUIRE(context);
  409. REQUIRE(InitializeDataBindings(context));
  410. ElementDocument* document = context->LoadDocumentFromMemory(aliasing_rml);
  411. REQUIRE(document);
  412. document->Show();
  413. TestsShell::RenderLoop();
  414. CHECK(document->QuerySelector("p:nth-child(1)")->GetInnerRML() == document->QuerySelector("p:nth-child(2)")->GetInnerRML());
  415. CHECK(document->QuerySelector("#w1 .title")->GetInnerRML() == "s0");
  416. CHECK(document->QuerySelector("#w1 .icon")->GetAttribute("icon", String()) == "a");
  417. CHECK(document->QuerySelector("#w2 .title")->GetInnerRML() == "s1");
  418. CHECK(document->QuerySelector("#w2 .icon")->GetAttribute("icon", String()) == "b");
  419. document->Close();
  420. TestsShell::ShutdownShell();
  421. }