Specificity_Basic.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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/TestsShell.h"
  29. #include <RmlUi/Core/Context.h>
  30. #include <RmlUi/Core/Element.h>
  31. #include <RmlUi/Core/ElementDocument.h>
  32. #include <doctest.h>
  33. using namespace Rml;
  34. /*
  35. * Here we are testing that properties with the same specificty, but declared at
  36. * different locations, are merged such that the last declared property is used.
  37. */
  38. static const String simple_doc1_rml = R"(
  39. <rml>
  40. <head>
  41. <title>Test</title>
  42. <link type="text/rcss" href="/../Tests/Data/UnitTests/Specificity_Basic.rcss"/>
  43. <style>
  44. .b {
  45. width: 50px;
  46. }
  47. </style>
  48. </head>
  49. <body class="a b c"/>
  50. </rml>
  51. )";
  52. static const String simple_doc2_rml = R"(
  53. <rml>
  54. <head>
  55. <title>Test</title>
  56. <style>
  57. .b {
  58. width: 50px;
  59. }
  60. </style>
  61. <link type="text/rcss" href="/../Tests/Data/UnitTests/Specificity_Basic.rcss"/>
  62. </head>
  63. <body class="a b c"/>
  64. </rml>
  65. )";
  66. static const String simple_doc3_rml = R"(
  67. <rml>
  68. <head>
  69. <title>Test</title>
  70. <style>
  71. body.a {
  72. width: 50px;
  73. }
  74. </style>
  75. <link type="text/rcss" href="/../Tests/Data/UnitTests/Specificity_Basic.rcss"/>
  76. </head>
  77. <body class="a b c"/>
  78. </rml>
  79. )";
  80. static const String simple_doc4_rml = R"(
  81. <rml>
  82. <head>
  83. <title>Test</title>
  84. <link type="text/rcss" href="/../Tests/Data/UnitTests/Specificity_Basic.rcss"/>
  85. <style>
  86. .b { width: 200px; }
  87. .a { width: 50px; }
  88. </style>
  89. </head>
  90. <body class="a b c"/>
  91. </rml>
  92. )";
  93. static const String simple_doc5_rml = R"(
  94. <rml>
  95. <head>
  96. <title>Test</title>
  97. <link type="text/rcss" href="/../Tests/Data/UnitTests/Specificity_Basic.rcss"/>
  98. <style>
  99. .a { width: 50px; }
  100. .b { width: 200px; }
  101. </style>
  102. </head>
  103. <body class="a b c"/>
  104. </rml>
  105. )";
  106. static const String simple_doc6_rml = R"(
  107. <rml>
  108. <head>
  109. <title>Test</title>
  110. <style>
  111. .b { width: 200px; }
  112. .a { width: 50px; }
  113. </style>
  114. <link type="text/rcss" href="/../Tests/Data/UnitTests/Specificity_Basic.rcss"/>
  115. </head>
  116. <body class="a b c"/>
  117. </rml>
  118. )";
  119. static const String simple_doc7_rml = R"(
  120. <rml>
  121. <head>
  122. <title>Test</title>
  123. <style>
  124. .a { width: 50px; }
  125. .b { width: 200px; }
  126. </style>
  127. <link type="text/rcss" href="/../Tests/Data/UnitTests/Specificity_Basic.rcss"/>
  128. </head>
  129. <body class="a b c"/>
  130. </rml>
  131. )";
  132. static const String simple_doc8_rml = R"(
  133. <rml>
  134. <head>
  135. <title>Test</title>
  136. <link type="text/rcss" href="/../Tests/Data/UnitTests/Specificity_Basic.rcss"/>
  137. <style>
  138. .a { width: 50px; }
  139. .b { width: 200px; }
  140. </style>
  141. </head>
  142. <body class="a b c"/>
  143. </rml>
  144. )";
  145. static const String simple_doc9_rml = R"(
  146. <rml>
  147. <head>
  148. <title>Test</title>
  149. <link type="text/rcss" href="/../Tests/Data/UnitTests/Specificity_Basic.rcss"/>
  150. <style>
  151. .a { width: 50px; }
  152. .b { width: 200px; }
  153. </style>
  154. <style>
  155. .b { width: 300px; }
  156. .a { width: 400px; }
  157. </style>
  158. </head>
  159. <body class="a b c"/>
  160. </rml>
  161. )";
  162. static const String simple_doc10_rml = R"(
  163. <rml>
  164. <head>
  165. <title>Test</title>
  166. <link type="text/rcss" href="/../Tests/Data/UnitTests/Specificity_Basic.rcss"/>
  167. <style>
  168. .a { width: 50px; }
  169. .b { width: 200px; }
  170. </style>
  171. <style>
  172. .a { width: 400px; }
  173. .b { width: 300px; }
  174. </style>
  175. </head>
  176. <body class="a b c"/>
  177. </rml>
  178. )";
  179. static const String simple_doc11_rml = R"(
  180. <rml>
  181. <head>
  182. <title>Test</title>
  183. <link type="text/rcss" href="/../Tests/Data/UnitTests/Specificity_Basic.rcss"/>
  184. <style>
  185. .a { width: 50px; }
  186. .b { width: 200px; }
  187. </style>
  188. <style>
  189. .b { width: 300px; }
  190. .a { width: 400px; }
  191. </style>
  192. </head>
  193. <body class="a b c"/>
  194. </rml>
  195. )";
  196. static const String simple_doc12_rml = R"(
  197. <rml>
  198. <head>
  199. <title>Test</title>
  200. <link type="text/rcss" href="/../Tests/Data/UnitTests/Specificity_Basic.rcss"/>
  201. <style>
  202. .a { width: 50px; }
  203. .b { width: 200px; }
  204. </style>
  205. <style>
  206. .a { width: 400px; }
  207. .b { width: 300px; }
  208. </style>
  209. </head>
  210. <body class="a b c"/>
  211. </rml>
  212. )";
  213. static const String simple_doc13_rml = R"(
  214. <rml>
  215. <head>
  216. <title>Test</title>
  217. <style>
  218. .a { width: 50px; }
  219. .b { width: 200px; }
  220. </style>
  221. <link type="text/rcss" href="/../Tests/Data/UnitTests/Specificity_Basic.rcss"/>
  222. <style>
  223. .a { width: 400px; }
  224. .b { width: 300px; }
  225. </style>
  226. </head>
  227. <body class="a b c"/>
  228. </rml>
  229. )";
  230. static const String simple_doc14_rml = R"(
  231. <rml>
  232. <head>
  233. <title>Test</title>
  234. <style>
  235. .a { width: 50px; }
  236. .b { width: 200px; }
  237. </style>
  238. <style>
  239. .a { width: 400px; }
  240. .b { width: 300px; }
  241. </style>
  242. <link type="text/rcss" href="/../Tests/Data/UnitTests/Specificity_Basic.rcss"/>
  243. </head>
  244. <body class="a b c"/>
  245. </rml>
  246. )";
  247. TEST_CASE("specificity.basic")
  248. {
  249. Context* context = TestsShell::GetContext();
  250. REQUIRE(context);
  251. struct Test {
  252. const String* document_rml;
  253. float expected_width;
  254. };
  255. Test tests[] = {
  256. {&simple_doc1_rml, 50.f},
  257. {&simple_doc2_rml, 100.f},
  258. {&simple_doc3_rml, 50.f},
  259. {&simple_doc4_rml, 50.f},
  260. {&simple_doc5_rml, 200.f},
  261. {&simple_doc6_rml, 100.f},
  262. {&simple_doc7_rml, 100.f},
  263. {&simple_doc8_rml, 200.f},
  264. {&simple_doc9_rml, 400.f},
  265. {&simple_doc10_rml, 300.f},
  266. {&simple_doc11_rml, 400.f},
  267. {&simple_doc12_rml, 300.f},
  268. {&simple_doc13_rml, 300.f},
  269. {&simple_doc14_rml, 100.f},
  270. };
  271. int i = 1;
  272. for (const Test& test : tests)
  273. {
  274. ElementDocument* document = context->LoadDocumentFromMemory(*test.document_rml);
  275. REQUIRE(document);
  276. document->Show();
  277. context->Update();
  278. context->Render();
  279. TestsShell::RenderLoop();
  280. CHECK_MESSAGE(document->GetBox().GetSize().x == test.expected_width, "Document " << i);
  281. document->Close();
  282. i++;
  283. }
  284. TestsShell::ShutdownShell();
  285. }