FlexFormatting.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/Core.h>
  31. #include <RmlUi/Core/Element.h>
  32. #include <RmlUi/Core/ElementDocument.h>
  33. #include <doctest.h>
  34. using namespace Rml;
  35. static const String document_flex_rml = R"(
  36. <rml>
  37. <head>
  38. <link type="text/rcss" href="/assets/rml.rcss"/>
  39. <style>
  40. body {
  41. width: 500px;
  42. height: 300px;
  43. background: #333;
  44. }
  45. #flex {
  46. background: #666;
  47. display: flex;
  48. width: 50%;
  49. height: 30%;
  50. }
  51. input.checkbox {
  52. background: #fff;
  53. border: 1px #000;
  54. }
  55. </style>
  56. </head>
  57. <body>
  58. <div id="flex">
  59. <input type="checkbox" id="checkbox"/>
  60. </div>
  61. </body>
  62. </rml>
  63. )";
  64. TEST_CASE("FlexFormatting")
  65. {
  66. Context* context = TestsShell::GetContext();
  67. REQUIRE(context);
  68. ElementDocument* document = context->LoadDocumentFromMemory(document_flex_rml);
  69. REQUIRE(document);
  70. document->Show();
  71. Element* checkbox = document->GetElementById("checkbox");
  72. Element* flex = document->GetElementById("flex");
  73. struct TestCase {
  74. String flex_direction;
  75. String align_items;
  76. Vector2f expected_size;
  77. };
  78. TestCase test_cases[] = {
  79. {"column", "stretch", Vector2f(248, 16)},
  80. {"column", "center", Vector2f(16, 16)},
  81. {"row", "stretch", Vector2f(16, 88)},
  82. {"row", "center", Vector2f(16, 16)},
  83. };
  84. for (auto& test_case : test_cases)
  85. {
  86. flex->SetProperty("flex-direction", test_case.flex_direction);
  87. flex->SetProperty("align-items", test_case.align_items);
  88. TestsShell::RenderLoop();
  89. CAPTURE(test_case.align_items);
  90. CAPTURE(test_case.flex_direction);
  91. CHECK(checkbox->GetBox().GetSize().x == doctest::Approx(test_case.expected_size.x));
  92. CHECK(checkbox->GetBox().GetSize().y == doctest::Approx(test_case.expected_size.y));
  93. }
  94. document->Close();
  95. TestsShell::ShutdownShell();
  96. }
  97. static const String document_flex_dp_ratio_rml = R"(
  98. <rml>
  99. <head>
  100. <link type="text/rcss" href="/assets/rml.rcss"/>
  101. <style>
  102. body {
  103. width: 100%;
  104. height: 100%;
  105. }
  106. .window {
  107. width: 1920dp;
  108. height: 1080dp;
  109. margin: 0 auto;
  110. display: flex;
  111. flex-direction: column;
  112. }
  113. .inner {
  114. flex-grow: 1;
  115. display: flex;
  116. flex-direction: row;
  117. }
  118. .left {
  119. width: 30%;
  120. margin: 32dp;
  121. overflow: auto;
  122. background: #522;
  123. }
  124. .right {
  125. flex: 1;
  126. margin: 32dp;
  127. overflow: auto;
  128. background: #252;
  129. }
  130. </style>
  131. </head>
  132. <body>
  133. <div class="window">
  134. <div class="inner">
  135. <div class="left"/>
  136. <div class="right"/>
  137. </div>
  138. </div>
  139. </body>
  140. </rml>
  141. )";
  142. TEST_CASE("FlexFormatting.dp_ratio")
  143. {
  144. Context* context = TestsShell::GetContext();
  145. REQUIRE(context);
  146. ElementDocument* document = context->LoadDocumentFromMemory(document_flex_dp_ratio_rml);
  147. REQUIRE(document);
  148. document->Show();
  149. Element* window = document->GetChild(0);
  150. constexpr float native_width = 1920.f;
  151. constexpr float native_height = 1080.f;
  152. const float test_window_widths[] = {
  153. 3440.f,
  154. 2960.f,
  155. 2880.f,
  156. 2560.f,
  157. 2400.f,
  158. 2048.f,
  159. 1921.f,
  160. 1920.f,
  161. 1919.f,
  162. 1600.f,
  163. 1366.f,
  164. 1280.f,
  165. 1024.f,
  166. };
  167. for (float window_width : test_window_widths)
  168. {
  169. const float dp_ratio = window_width / native_width;
  170. CAPTURE(window_width);
  171. CAPTURE(dp_ratio);
  172. context->SetDensityIndependentPixelRatio(dp_ratio);
  173. TestsShell::RenderLoop();
  174. CHECK(window->GetBox().GetSize().x == window_width);
  175. CHECK(window->GetBox().GetSize().y == doctest::Approx((window_width / native_width) * native_height));
  176. }
  177. document->Close();
  178. TestsShell::ShutdownShell();
  179. }