Specificity_MediaQuery.cpp 7.0 KB

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