MediaQuery.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 document_media_query1_rml = R"(
  35. <rml>
  36. <head>
  37. <title>Test</title>
  38. <link type="text/rcss" href="/assets/rml.rcss"/>
  39. <style>
  40. body {
  41. left: 0;
  42. top: 0;
  43. right: 0;
  44. bottom: 0;
  45. }
  46. div {
  47. height: 48px;
  48. width: 48px;
  49. background: white;
  50. }
  51. @media (min-width: 640px) {
  52. div {
  53. height: 32px;
  54. width: 32px;
  55. }
  56. }
  57. @media (max-width: 639px) {
  58. div {
  59. height: 64px;
  60. width: 64px;
  61. }
  62. }
  63. </style>
  64. </head>
  65. <body>
  66. <div/>
  67. </body>
  68. </rml>
  69. )";
  70. static const String document_media_query2_rml = R"(
  71. <rml>
  72. <head>
  73. <title>Test</title>
  74. <link type="text/rcss" href="/assets/rml.rcss"/>
  75. <style>
  76. body {
  77. left: 0;
  78. top: 0;
  79. right: 0;
  80. bottom: 0;
  81. }
  82. div {
  83. height: 48px;
  84. width: 48px;
  85. background: white;
  86. }
  87. @media (orientation: landscape) {
  88. div {
  89. height: 32px;
  90. width: 32px;
  91. }
  92. }
  93. @media (max-aspect-ratio: 3/4) {
  94. div {
  95. height: 64px;
  96. width: 64px;
  97. }
  98. }
  99. @media (min-resolution: 2x) {
  100. div {
  101. height: 128px;
  102. }
  103. }
  104. </style>
  105. </head>
  106. <body>
  107. <div/>
  108. </body>
  109. </rml>
  110. )";
  111. static const String document_media_query3_rml = R"(
  112. <rml>
  113. <head>
  114. <title>Test</title>
  115. <link type="text/rcss" href="/assets/rml.rcss"/>
  116. <style>
  117. body {
  118. left: 0;
  119. top: 0;
  120. right: 0;
  121. bottom: 0;
  122. }
  123. div {
  124. height: 48px;
  125. width: 48px;
  126. background: white;
  127. }
  128. @media (orientation: landscape) and (min-width: 640px) {
  129. div {
  130. height: 32px;
  131. width: 32px;
  132. }
  133. }
  134. @media (max-aspect-ratio: 4/3) {
  135. div {
  136. height: 64px;
  137. width: 64px;
  138. }
  139. }
  140. </style>
  141. </head>
  142. <body>
  143. <div/>
  144. </body>
  145. </rml>
  146. )";
  147. static const String document_media_query4_rml = R"(
  148. <rml>
  149. <head>
  150. <title>Test</title>
  151. <link type="text/rcss" href="/assets/rml.rcss"/>
  152. <style>
  153. body {
  154. left: 0;
  155. top: 0;
  156. right: 0;
  157. bottom: 0;
  158. }
  159. div {
  160. height: 48px;
  161. width: 48px;
  162. background: white;
  163. }
  164. @media (theme: big) {
  165. div {
  166. height: 96px;
  167. width: 96px;
  168. }
  169. }
  170. @media (theme: tiny) {
  171. div {
  172. height: 32px;
  173. width: 32px;
  174. }
  175. }
  176. </style>
  177. </head>
  178. <body>
  179. <div/>
  180. </body>
  181. </rml>
  182. )";
  183. static const String document_media_query5_rml = R"(
  184. <rml>
  185. <head>
  186. <title>Test</title>
  187. <link type="text/rcss" href="/assets/rml.rcss"/>
  188. <style>
  189. body {
  190. left: 0;
  191. top: 0;
  192. right: 0;
  193. bottom: 0;
  194. }
  195. div {
  196. height: 48px;
  197. width: 48px;
  198. background: white;
  199. }
  200. @media not (theme: tiny) {
  201. div {
  202. height: 32px;
  203. width: 32px;
  204. }
  205. }
  206. @media (theme: big) {
  207. div {
  208. height: 96px;
  209. width: 96px;
  210. }
  211. }
  212. </style>
  213. </head>
  214. <body>
  215. <div/>
  216. </body>
  217. </rml>
  218. )";
  219. static const String document_media_query6_rml = R"(
  220. <rml>
  221. <head>
  222. <title>Test</title>
  223. <link type="text/rcss" href="/assets/rml.rcss"/>
  224. <style>
  225. body {
  226. left: 0;
  227. top: 0;
  228. right: 0;
  229. bottom: 0;
  230. }
  231. div {
  232. height: 48px;
  233. width: 48px;
  234. background: white;
  235. }
  236. @media not not (theme: big) {
  237. div {
  238. height: 32px;
  239. width: 32px;
  240. }
  241. }
  242. </style>
  243. </head>
  244. <body>
  245. <div/>
  246. </body>
  247. </rml>
  248. )";
  249. static const String document_media_query7_rml = R"(
  250. <rml>
  251. <head>
  252. <title>Test</title>
  253. <link type="text/rcss" href="/assets/rml.rcss"/>
  254. <style>
  255. body {
  256. left: 0;
  257. top: 0;
  258. right: 0;
  259. bottom: 0;
  260. }
  261. div {
  262. height: 48px;
  263. width: 48px;
  264. background: white;
  265. }
  266. @media (theme: big) (theme: small) {
  267. div {
  268. height: 32px;
  269. width: 32px;
  270. }
  271. }
  272. @media not (theme: big) (theme: small) {
  273. div {
  274. height: 32px;
  275. width: 32px;
  276. }
  277. }
  278. </style>
  279. </head>
  280. <body>
  281. <div/>
  282. </body>
  283. </rml>
  284. )";
  285. TEST_CASE("mediaquery.basic")
  286. {
  287. Context* context = TestsShell::GetContext();
  288. REQUIRE(context);
  289. // There should be no warnings loading this document. There should be one div of 32px width & height
  290. ElementDocument* document = context->LoadDocumentFromMemory(document_media_query1_rml);
  291. REQUIRE(document);
  292. document->Show();
  293. context->Update();
  294. context->Render();
  295. TestsShell::RenderLoop();
  296. // Shell default window dimensions are 1500, 800
  297. ElementList elems;
  298. document->GetElementsByTagName(elems, "div");
  299. CHECK(elems.size() == 1);
  300. CHECK(elems[0]->GetBox() == Box(Vector2f(32.0f, 32.0f)));
  301. document->Close();
  302. TestsShell::ShutdownShell();
  303. }
  304. TEST_CASE("mediaquery.dynamic")
  305. {
  306. Context* context = TestsShell::GetContext();
  307. REQUIRE(context);
  308. // There should be no warnings loading this document. There should be one div of 32px width & height
  309. ElementDocument* document = context->LoadDocumentFromMemory(document_media_query1_rml);
  310. REQUIRE(document);
  311. document->Show();
  312. context->Update();
  313. context->Render();
  314. TestsShell::RenderLoop();
  315. // Shell default window dimensions are 1500, 800
  316. ElementList elems;
  317. document->GetElementsByTagName(elems, "div");
  318. CHECK(elems.size() == 1);
  319. CHECK(elems[0]->GetBox() == Box(Vector2f(32.0f, 32.0f)));
  320. context->SetDimensions(Vector2i(480, 320));
  321. context->Update();
  322. CHECK(elems[0]->GetBox() == Box(Vector2f(64.0f, 64.0f)));
  323. document->Close();
  324. TestsShell::ShutdownShell();
  325. }
  326. TEST_CASE("mediaquery.custom_properties")
  327. {
  328. Context* context = TestsShell::GetContext();
  329. REQUIRE(context);
  330. context->SetDensityIndependentPixelRatio(2.0f);
  331. // There should be no warnings loading this document. There should be one div of 32px width & height
  332. ElementDocument* document = context->LoadDocumentFromMemory(document_media_query2_rml);
  333. REQUIRE(document);
  334. document->Show();
  335. context->Update();
  336. context->Render();
  337. TestsShell::RenderLoop();
  338. // Shell default window dimensions are 1500, 800
  339. ElementList elems;
  340. document->GetElementsByTagName(elems, "div");
  341. CHECK(elems.size() == 1);
  342. CHECK(elems[0]->GetBox() == Box(Vector2f(32.0f, 128.0f)));
  343. document->Close();
  344. TestsShell::ShutdownShell();
  345. }
  346. TEST_CASE("mediaquery.composite")
  347. {
  348. Context* context = TestsShell::GetContext();
  349. REQUIRE(context);
  350. // There should be no warnings loading this document. There should be one div of 32px width & height
  351. ElementDocument* document = context->LoadDocumentFromMemory(document_media_query3_rml);
  352. REQUIRE(document);
  353. document->Show();
  354. context->Update();
  355. context->Render();
  356. TestsShell::RenderLoop();
  357. // Shell default window dimensions are 1500, 800
  358. ElementList elems;
  359. document->GetElementsByTagName(elems, "div");
  360. CHECK(elems.size() == 1);
  361. CHECK(elems[0]->GetBox() == Box(Vector2f(32.0f, 32.0f)));
  362. document->Close();
  363. TestsShell::ShutdownShell();
  364. }
  365. TEST_CASE("mediaquery.theme")
  366. {
  367. Context* context = TestsShell::GetContext();
  368. REQUIRE(context);
  369. ElementDocument* document = context->LoadDocumentFromMemory(document_media_query4_rml);
  370. REQUIRE(document);
  371. document->Show();
  372. context->Update();
  373. context->Render();
  374. TestsShell::RenderLoop();
  375. ElementList elems;
  376. document->GetElementsByTagName(elems, "div");
  377. CHECK(elems.size() == 1);
  378. CHECK(elems[0]->GetBox().GetSize().x == 48.0f);
  379. context->ActivateTheme("big", true);
  380. context->Update();
  381. context->Render();
  382. CHECK(elems[0]->GetBox().GetSize().x == 96.0f);
  383. context->ActivateTheme("big", false);
  384. context->Update();
  385. context->Render();
  386. CHECK(elems[0]->GetBox().GetSize().x == 48.0f);
  387. context->ActivateTheme("tiny", true);
  388. context->Update();
  389. context->Render();
  390. CHECK(elems[0]->GetBox().GetSize().x == 32.0f);
  391. context->ActivateTheme("big", true);
  392. context->Update();
  393. context->Render();
  394. CHECK(elems[0]->GetBox().GetSize().x == 32.0f);
  395. document->Close();
  396. TestsShell::ShutdownShell();
  397. }
  398. // test of `not`; `not` should be an inverted case
  399. TEST_CASE("mediaquery.theme.notonly")
  400. {
  401. Context* context = TestsShell::GetContext();
  402. REQUIRE(context);
  403. ElementDocument* document = context->LoadDocumentFromMemory(document_media_query5_rml);
  404. REQUIRE(document);
  405. document->Show();
  406. context->Update();
  407. context->Render();
  408. TestsShell::RenderLoop();
  409. ElementList elems;
  410. document->GetElementsByTagName(elems, "div");
  411. CHECK(elems.size() == 1);
  412. CHECK(elems[0]->GetBox().GetSize().x == 32.0f);
  413. context->ActivateTheme("big", true);
  414. context->Update();
  415. context->Render();
  416. CHECK(elems[0]->GetBox().GetSize().x == 96.0f);
  417. context->ActivateTheme("big", false);
  418. context->Update();
  419. context->Render();
  420. CHECK(elems[0]->GetBox().GetSize().x == 32.0f);
  421. context->ActivateTheme("tiny", true);
  422. context->Update();
  423. context->Render();
  424. CHECK(elems[0]->GetBox().GetSize().x == 48.0f);
  425. context->ActivateTheme("big", true);
  426. context->Update();
  427. context->Render();
  428. CHECK(elems[0]->GetBox().GetSize().x == 96.0f);
  429. document->Close();
  430. TestsShell::ShutdownShell();
  431. }
  432. // test that `not` cannot appear multiple times
  433. TEST_CASE("mediaquery.theme.notonly_one")
  434. {
  435. Context* context = TestsShell::GetContext();
  436. REQUIRE(context);
  437. INFO("Expected warnings: unexpected 'not'.");
  438. TestsShell::SetNumExpectedWarnings(1);
  439. ElementDocument* document = context->LoadDocumentFromMemory(document_media_query6_rml);
  440. REQUIRE(document);
  441. document->Close();
  442. TestsShell::ShutdownShell();
  443. }
  444. // test that an `and` must be between multiple conditions.
  445. TEST_CASE("mediaquery.theme.condition_checks")
  446. {
  447. Context* context = TestsShell::GetContext();
  448. REQUIRE(context);
  449. INFO("Expected warnings: expected 'and'.");
  450. TestsShell::SetNumExpectedWarnings(2);
  451. ElementDocument* document = context->LoadDocumentFromMemory(document_media_query7_rml);
  452. REQUIRE(document);
  453. document->Close();
  454. TestsShell::ShutdownShell();
  455. }