Properties.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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/TestsInterface.h"
  29. #include <RmlUi/Core/Context.h>
  30. #include <RmlUi/Core/Core.h>
  31. #include <RmlUi/Core/DecorationTypes.h>
  32. #include <RmlUi/Core/Element.h>
  33. #include <RmlUi/Core/ElementDocument.h>
  34. #include <RmlUi/Core/PropertyDictionary.h>
  35. #include <RmlUi/Core/StyleSheetSpecification.h>
  36. #include <RmlUi/Core/StyleSheetTypes.h>
  37. #include <doctest.h>
  38. using namespace Rml;
  39. TEST_CASE("Properties")
  40. {
  41. const Vector2i window_size(1024, 768);
  42. TestsSystemInterface system_interface;
  43. TestsRenderInterface render_interface;
  44. SetRenderInterface(&render_interface);
  45. SetSystemInterface(&system_interface);
  46. Rml::Initialise();
  47. Context* context = Rml::CreateContext("main", window_size);
  48. ElementDocument* document = context->CreateDocument();
  49. SUBCASE("inset")
  50. {
  51. struct InsetTestCase {
  52. String inset_value;
  53. struct ExpectedValues {
  54. String top;
  55. String right;
  56. String bottom;
  57. String left;
  58. } expected;
  59. };
  60. InsetTestCase tests[] = {
  61. {"auto", {"auto", "auto", "auto", "auto"}},
  62. {"0", {"0px", "0px", "0px", "0px"}},
  63. {"1px", {"1px", "1px", "1px", "1px"}},
  64. {"1dp", {"1dp", "1dp", "1dp", "1dp"}},
  65. {"1%", {"1%", "1%", "1%", "1%"}},
  66. {"10px 20px", {"10px", "20px", "10px", "20px"}},
  67. {"10px 20px 30px", {"10px", "20px", "30px", "20px"}},
  68. {"10px 20px 30px 40px", {"10px", "20px", "30px", "40px"}},
  69. };
  70. for (const InsetTestCase& test : tests)
  71. {
  72. CHECK(document->SetProperty("inset", test.inset_value));
  73. CHECK(document->GetProperty("top")->ToString() == test.expected.top);
  74. CHECK(document->GetProperty("right")->ToString() == test.expected.right);
  75. CHECK(document->GetProperty("bottom")->ToString() == test.expected.bottom);
  76. CHECK(document->GetProperty("left")->ToString() == test.expected.left);
  77. }
  78. }
  79. SUBCASE("flex")
  80. {
  81. struct FlexTestCase {
  82. String flex_value;
  83. struct ExpectedValues {
  84. float flex_grow;
  85. float flex_shrink;
  86. String flex_basis;
  87. } expected;
  88. };
  89. FlexTestCase tests[] = {
  90. {"", {0.f, 1.f, "auto"}},
  91. {"none", {0.f, 0.f, "auto"}},
  92. {"auto", {1.f, 1.f, "auto"}},
  93. {"1", {1.f, 1.f, "0px"}},
  94. {"2", {2.f, 1.f, "0px"}},
  95. {"2 0", {2.f, 0.f, "0px"}},
  96. {"2 3", {2.f, 3.f, "0px"}},
  97. {"2 auto", {2.f, 1.f, "auto"}},
  98. {"2 0 auto", {2.f, 0.f, "auto"}},
  99. {"0 0 auto", {0.f, 0.f, "auto"}},
  100. {"0 0 50px", {0.f, 0.f, "50px"}},
  101. {"0 0 50px", {0.f, 0.f, "50px"}},
  102. {"0 0 0", {0.f, 0.f, "0px"}},
  103. };
  104. for (const FlexTestCase& test : tests)
  105. {
  106. if (!test.flex_value.empty())
  107. {
  108. CHECK(document->SetProperty("flex", test.flex_value));
  109. }
  110. CHECK(document->GetProperty<float>("flex-grow") == test.expected.flex_grow);
  111. CHECK(document->GetProperty<float>("flex-shrink") == test.expected.flex_shrink);
  112. CHECK(document->GetProperty("flex-basis")->ToString() == test.expected.flex_basis);
  113. }
  114. }
  115. SUBCASE("gradient")
  116. {
  117. auto ParseGradient = [&](const String& value) -> Property {
  118. document->SetProperty("decorator", "linear-gradient(" + value + ")");
  119. auto decorators = document->GetProperty<DecoratorsPtr>("decorator");
  120. if (!decorators || decorators->list.size() != 1)
  121. return {};
  122. for (auto& id_property : decorators->list.front().properties.GetProperties())
  123. {
  124. if (id_property.second.unit == Unit::COLORSTOPLIST)
  125. return id_property.second;
  126. }
  127. return {};
  128. };
  129. struct GradientTestCase {
  130. String value;
  131. String expected_parsed_string;
  132. ColorStopList expected_color_stops;
  133. };
  134. GradientTestCase test_cases[] = {
  135. {
  136. "red, blue",
  137. "#ff0000, #0000ff",
  138. {
  139. ColorStop{ColourbPremultiplied(255, 0, 0), NumericValue{}},
  140. ColorStop{ColourbPremultiplied(0, 0, 255), NumericValue{}},
  141. },
  142. },
  143. {
  144. "red 5px, blue 50%",
  145. "#ff0000 5px, #0000ff 50%",
  146. {
  147. ColorStop{ColourbPremultiplied(255, 0, 0), NumericValue{5.f, Unit::PX}},
  148. ColorStop{ColourbPremultiplied(0, 0, 255), NumericValue{50.f, Unit::PERCENT}},
  149. },
  150. },
  151. {
  152. "red, #00f 50%, rgba(0, 255,0, 150) 10dp",
  153. "#ff0000, #0000ff 50%, #00ff0096 10dp",
  154. {
  155. ColorStop{ColourbPremultiplied(255, 0, 0), NumericValue{}},
  156. ColorStop{ColourbPremultiplied(0, 0, 255), NumericValue{50.f, Unit::PERCENT}},
  157. ColorStop{ColourbPremultiplied(0, 150, 0, 150), NumericValue{10.f, Unit::DP}},
  158. },
  159. },
  160. {
  161. "hsl(10000, 0%, 50%), hsl(240, 100%, 50%) 50%, hsla(-240, 100%, 50%, 0.5) 10dp",
  162. "#7f7f7f, #0000ff 50%, #00ff007f 10dp",
  163. {
  164. ColorStop{ColourbPremultiplied(127, 127, 127), NumericValue{}},
  165. ColorStop{ColourbPremultiplied(0, 0, 255), NumericValue{50.f, Unit::PERCENT}},
  166. ColorStop{ColourbPremultiplied(0, 127, 0, 127), NumericValue{10.f, Unit::DP}},
  167. },
  168. },
  169. {
  170. "lab(55% none none), lab(30% 67% -110) 50%, lab(90% -90 80 / 0.5) 10dp",
  171. "#838383, #0000fb 50%, #00ff267f 10dp",
  172. {
  173. ColorStop{ColourbPremultiplied(131, 131, 131), NumericValue{}},
  174. ColorStop{ColourbPremultiplied(0, 0, 251), NumericValue{50.f, Unit::PERCENT}},
  175. ColorStop{ColourbPremultiplied(0, 127, 19, 127), NumericValue{10.f, Unit::DP}},
  176. },
  177. },
  178. {
  179. "lch(55% none 300.0), lch(30% 85% 180.0) 50%, lch(90% 90 100.0 / 50%) 10dp",
  180. "#838383, #006044 50%, #f0e6007f 10dp",
  181. {
  182. ColorStop{ColourbPremultiplied(131, 131, 131), NumericValue{}},
  183. ColorStop{ColourbPremultiplied(0, 96, 68), NumericValue{50.f, Unit::PERCENT}},
  184. ColorStop{ColourbPremultiplied(120, 115, 0, 127), NumericValue{10.f, Unit::DP}},
  185. },
  186. },
  187. {
  188. "oklab(85% -50% 70%), oklab(0.2 0.4 -0.4) 50%, oklab(100% none 0.4 / 0.25) 10dp",
  189. "#98ed00, #6600c1 50%, #ffde003f 10dp",
  190. {
  191. ColorStop{ColourbPremultiplied(152, 237, 0), NumericValue{}},
  192. ColorStop{ColourbPremultiplied(102, 0, 193), NumericValue{50.f, Unit::PERCENT}},
  193. ColorStop{ColourbPremultiplied(63, 55, 0, 63), NumericValue{10.f, Unit::DP}},
  194. },
  195. },
  196. {
  197. "oklch(75% 100% 30.0), oklch(0.5 0.2 270) 50%, oklch(1.0 0.1 none / 0.5) 10dp",
  198. "#ff0000, #3a50d2 50%, #ffe2fa7f 10dp",
  199. {
  200. ColorStop{ColourbPremultiplied(255, 0, 0), NumericValue{}},
  201. ColorStop{ColourbPremultiplied(58, 80, 210), NumericValue{50.f, Unit::PERCENT}},
  202. ColorStop{ColourbPremultiplied(127, 113, 125, 127), NumericValue{10.f, Unit::DP}},
  203. },
  204. },
  205. {
  206. "red 50px 20%, blue 10in",
  207. "#ff0000 50px, #ff0000 20%, #0000ff 10in",
  208. {
  209. ColorStop{ColourbPremultiplied(255, 0, 0), NumericValue{50.f, Unit::PX}},
  210. ColorStop{ColourbPremultiplied(255, 0, 0), NumericValue{20.f, Unit::PERCENT}},
  211. ColorStop{ColourbPremultiplied(0, 0, 255), NumericValue{10.f, Unit::INCH}},
  212. },
  213. },
  214. };
  215. for (const GradientTestCase& test_case : test_cases)
  216. {
  217. const Property result = ParseGradient(test_case.value);
  218. CHECK(result.ToString() == test_case.expected_parsed_string);
  219. CHECK(result.Get<ColorStopList>() == test_case.expected_color_stops);
  220. }
  221. }
  222. Rml::Shutdown();
  223. }
  224. TEST_CASE("Property.ToString")
  225. {
  226. TestsSystemInterface system_interface;
  227. TestsRenderInterface render_interface;
  228. SetRenderInterface(&render_interface);
  229. SetSystemInterface(&system_interface);
  230. Rml::Initialise();
  231. CHECK(Property(5.2f, Unit::CM).ToString() == "5.2cm");
  232. CHECK(Property(150, Unit::PERCENT).ToString() == "150%");
  233. CHECK(Property(Colourb{170, 187, 204, 255}, Unit::COLOUR).ToString() == "#aabbcc");
  234. auto ParsedValue = [](const String& name, const String& value) -> String {
  235. PropertyDictionary properties;
  236. StyleSheetSpecification::ParsePropertyDeclaration(properties, name, value);
  237. REQUIRE(properties.GetNumProperties() == 1);
  238. return properties.GetProperties().begin()->second.ToString();
  239. };
  240. CHECK(ParsedValue("width", "10px") == "10px");
  241. CHECK(ParsedValue("width", "10.00em") == "10em");
  242. CHECK(ParsedValue("width", "auto") == "auto");
  243. CHECK(ParsedValue("background-color", "#abc") == "#aabbcc");
  244. CHECK(ParsedValue("background-color", "red") == "#ff0000");
  245. CHECK(ParsedValue("transform", "translateX(10px)") == "translateX(10px)");
  246. CHECK(ParsedValue("transform", "translate(20in, 50em)") == "translate(20in, 50em)");
  247. CHECK(ParsedValue("box-shadow", "2px 2px 0px, rgba(0, 0, 255, 255) 4px 4px 2em") == "#000000 2px 2px 0px, #0000ff 4px 4px 2em");
  248. CHECK(ParsedValue("box-shadow", "2px 2px 0px, #00ff 4px 4px 2em") == "#000000 2px 2px 0px, #0000ff 4px 4px 2em");
  249. // Due to conversion to and from premultiplied alpha, some color information is lost.
  250. CHECK(ParsedValue("box-shadow", "#fff0 2px 2px 0px") == "#00000000 2px 2px 0px");
  251. CHECK(ParsedValue("decorator", "linear-gradient(110deg, #fff3, #fff 10%, #c33 250dp, #3c3, #33c, #000 90%, #0003) border-box") ==
  252. "linear-gradient(110deg, #fff3, #fff 10%, #c33 250dp, #3c3, #33c, #000 90%, #0003) border-box");
  253. CHECK(ParsedValue("filter", "drop-shadow(#000 30px 20px 5px) opacity(0.2) sepia(0.2)") ==
  254. "drop-shadow(#000 30px 20px 5px) opacity(0.2) sepia(0.2)");
  255. Rml::Shutdown();
  256. }