2
0

MediaQuery.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 <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. TEST_CASE("mediaquery.basic")
  148. {
  149. Context* context = TestsShell::GetContext();
  150. REQUIRE(context);
  151. // There should be no warnings loading this document. There should be one div of 32px width & height
  152. ElementDocument* document = context->LoadDocumentFromMemory(document_media_query1_rml);
  153. REQUIRE(document);
  154. document->Show();
  155. context->Update();
  156. context->Render();
  157. TestsShell::RenderLoop();
  158. // Shell default window dimensions are 1500, 800
  159. ElementList elems;
  160. document->GetElementsByTagName(elems, "div");
  161. CHECK(elems.size() == 1);
  162. CHECK(elems[0]->GetBox() == Box(Vector2f(32.0f, 32.0f)));
  163. document->Close();
  164. TestsShell::ShutdownShell();
  165. }
  166. TEST_CASE("mediaquery.dynamic")
  167. {
  168. Context* context = TestsShell::GetContext();
  169. REQUIRE(context);
  170. // There should be no warnings loading this document. There should be one div of 32px width & height
  171. ElementDocument* document = context->LoadDocumentFromMemory(document_media_query1_rml);
  172. REQUIRE(document);
  173. document->Show();
  174. context->Update();
  175. context->Render();
  176. TestsShell::RenderLoop();
  177. // Shell default window dimensions are 1500, 800
  178. ElementList elems;
  179. document->GetElementsByTagName(elems, "div");
  180. CHECK(elems.size() == 1);
  181. CHECK(elems[0]->GetBox() == Box(Vector2f(32.0f, 32.0f)));
  182. context->SetDimensions(Vector2i(480, 320));
  183. context->Update();
  184. CHECK(elems[0]->GetBox() == Box(Vector2f(64.0f, 64.0f)));
  185. document->Close();
  186. TestsShell::ShutdownShell();
  187. }
  188. TEST_CASE("mediaquery.custom_properties")
  189. {
  190. Context* context = TestsShell::GetContext();
  191. REQUIRE(context);
  192. context->SetDensityIndependentPixelRatio(2.0f);
  193. // There should be no warnings loading this document. There should be one div of 32px width & height
  194. ElementDocument* document = context->LoadDocumentFromMemory(document_media_query2_rml);
  195. REQUIRE(document);
  196. document->Show();
  197. context->Update();
  198. context->Render();
  199. TestsShell::RenderLoop();
  200. // Shell default window dimensions are 1500, 800
  201. ElementList elems;
  202. document->GetElementsByTagName(elems, "div");
  203. CHECK(elems.size() == 1);
  204. CHECK(elems[0]->GetBox() == Box(Vector2f(32.0f, 128.0f)));
  205. document->Close();
  206. TestsShell::ShutdownShell();
  207. }
  208. TEST_CASE("mediaquery.composite")
  209. {
  210. Context* context = TestsShell::GetContext();
  211. REQUIRE(context);
  212. // There should be no warnings loading this document. There should be one div of 32px width & height
  213. ElementDocument* document = context->LoadDocumentFromMemory(document_media_query3_rml);
  214. REQUIRE(document);
  215. document->Show();
  216. context->Update();
  217. context->Render();
  218. TestsShell::RenderLoop();
  219. // Shell default window dimensions are 1500, 800
  220. ElementList elems;
  221. document->GetElementsByTagName(elems, "div");
  222. CHECK(elems.size() == 1);
  223. CHECK(elems[0]->GetBox() == Box(Vector2f(32.0f, 32.0f)));
  224. document->Close();
  225. TestsShell::ShutdownShell();
  226. }