Properties.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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("flex")
  50. {
  51. struct FlexTestCase {
  52. String flex_value;
  53. struct ExpectedValues {
  54. float flex_grow;
  55. float flex_shrink;
  56. String flex_basis;
  57. } expected;
  58. };
  59. FlexTestCase tests[] = {
  60. {"", {0.f, 1.f, "auto"}},
  61. {"none", {0.f, 0.f, "auto"}},
  62. {"auto", {1.f, 1.f, "auto"}},
  63. {"1", {1.f, 1.f, "0px"}},
  64. {"2", {2.f, 1.f, "0px"}},
  65. {"2 0", {2.f, 0.f, "0px"}},
  66. {"2 3", {2.f, 3.f, "0px"}},
  67. {"2 auto", {2.f, 1.f, "auto"}},
  68. {"2 0 auto", {2.f, 0.f, "auto"}},
  69. {"0 0 auto", {0.f, 0.f, "auto"}},
  70. {"0 0 50px", {0.f, 0.f, "50px"}},
  71. {"0 0 50px", {0.f, 0.f, "50px"}},
  72. {"0 0 0", {0.f, 0.f, "0px"}},
  73. };
  74. for (const FlexTestCase& test : tests)
  75. {
  76. if (!test.flex_value.empty())
  77. {
  78. CHECK(document->SetProperty("flex", test.flex_value));
  79. }
  80. CHECK(document->GetProperty<float>("flex-grow") == test.expected.flex_grow);
  81. CHECK(document->GetProperty<float>("flex-shrink") == test.expected.flex_shrink);
  82. CHECK(document->GetProperty("flex-basis")->ToString() == test.expected.flex_basis);
  83. }
  84. }
  85. SUBCASE("gradient")
  86. {
  87. auto ParseGradient = [&](const String& value) -> Property {
  88. document->SetProperty("decorator", "linear-gradient(" + value + ")");
  89. auto decorators = document->GetProperty<DecoratorsPtr>("decorator");
  90. if (!decorators || decorators->list.size() != 1)
  91. return {};
  92. for (auto& id_property : decorators->list.front().properties.GetProperties())
  93. {
  94. if (id_property.second.unit == Unit::COLORSTOPLIST)
  95. return id_property.second;
  96. }
  97. return {};
  98. };
  99. struct GradientTestCase {
  100. String value;
  101. String expected_parsed_string;
  102. ColorStopList expected_color_stops;
  103. };
  104. GradientTestCase test_cases[] = {
  105. {
  106. "red, blue",
  107. "#ff0000, #0000ff",
  108. {
  109. ColorStop{ColourbPremultiplied(255, 0, 0), NumericValue{}},
  110. ColorStop{ColourbPremultiplied(0, 0, 255), NumericValue{}},
  111. },
  112. },
  113. {
  114. "red 5px, blue 50%",
  115. "#ff0000 5px, #0000ff 50%",
  116. {
  117. ColorStop{ColourbPremultiplied(255, 0, 0), NumericValue{5.f, Unit::PX}},
  118. ColorStop{ColourbPremultiplied(0, 0, 255), NumericValue{50.f, Unit::PERCENT}},
  119. },
  120. },
  121. {
  122. "red, #00f 50%, rgba(0, 255,0, 150) 10dp",
  123. "#ff0000, #0000ff 50%, #00ff0096 10dp",
  124. {
  125. ColorStop{ColourbPremultiplied(255, 0, 0), NumericValue{}},
  126. ColorStop{ColourbPremultiplied(0, 0, 255), NumericValue{50.f, Unit::PERCENT}},
  127. ColorStop{ColourbPremultiplied(0, 150, 0, 150), NumericValue{10.f, Unit::DP}},
  128. },
  129. },
  130. {
  131. "hsl(10000, 0%, 50%), hsl(240, 100%, 50%) 50%, hsla(-240, 100%, 50%, 0.5) 10dp",
  132. "#7f7f7f, #0000ff 50%, #00ff007f 10dp",
  133. {
  134. ColorStop{ColourbPremultiplied(127, 127, 127), NumericValue{}},
  135. ColorStop{ColourbPremultiplied(0, 0, 255), NumericValue{50.f, Unit::PERCENT}},
  136. ColorStop{ColourbPremultiplied(0, 127, 0, 127), NumericValue{10.f, Unit::DP}},
  137. },
  138. },
  139. {
  140. "red 50px 20%, blue 10in",
  141. "#ff0000 50px, #ff0000 20%, #0000ff 10in",
  142. {
  143. ColorStop{ColourbPremultiplied(255, 0, 0), NumericValue{50.f, Unit::PX}},
  144. ColorStop{ColourbPremultiplied(255, 0, 0), NumericValue{20.f, Unit::PERCENT}},
  145. ColorStop{ColourbPremultiplied(0, 0, 255), NumericValue{10.f, Unit::INCH}},
  146. },
  147. },
  148. };
  149. for (const GradientTestCase& test_case : test_cases)
  150. {
  151. const Property result = ParseGradient(test_case.value);
  152. CHECK(result.ToString() == test_case.expected_parsed_string);
  153. CHECK(result.Get<ColorStopList>() == test_case.expected_color_stops);
  154. }
  155. }
  156. Rml::Shutdown();
  157. }
  158. TEST_CASE("Property.ToString")
  159. {
  160. TestsSystemInterface system_interface;
  161. TestsRenderInterface render_interface;
  162. SetRenderInterface(&render_interface);
  163. SetSystemInterface(&system_interface);
  164. Rml::Initialise();
  165. CHECK(Property(5.2f, Unit::CM).ToString() == "5.2cm");
  166. CHECK(Property(150, Unit::PERCENT).ToString() == "150%");
  167. CHECK(Property(Colourb{170, 187, 204, 255}, Unit::COLOUR).ToString() == "#aabbcc");
  168. auto ParsedValue = [](const String& name, const String& value) -> String {
  169. PropertyDictionary properties;
  170. StyleSheetSpecification::ParsePropertyDeclaration(properties, name, value);
  171. REQUIRE(properties.GetNumProperties() == 1);
  172. return properties.GetProperties().begin()->second.ToString();
  173. };
  174. CHECK(ParsedValue("width", "10px") == "10px");
  175. CHECK(ParsedValue("width", "10.00em") == "10em");
  176. CHECK(ParsedValue("width", "auto") == "auto");
  177. CHECK(ParsedValue("background-color", "#abc") == "#aabbcc");
  178. CHECK(ParsedValue("background-color", "red") == "#ff0000");
  179. CHECK(ParsedValue("transform", "translateX(10px)") == "translateX(10px)");
  180. CHECK(ParsedValue("transform", "translate(20in, 50em)") == "translate(20in, 50em)");
  181. CHECK(ParsedValue("box-shadow", "2px 2px 0px, rgba(0, 0, 255, 255) 4px 4px 2em") == "#000000 2px 2px 0px, #0000ff 4px 4px 2em");
  182. CHECK(ParsedValue("box-shadow", "2px 2px 0px, #00ff 4px 4px 2em") == "#000000 2px 2px 0px, #0000ff 4px 4px 2em");
  183. // Due to conversion to and from premultiplied alpha, some color information is lost.
  184. CHECK(ParsedValue("box-shadow", "#fff0 2px 2px 0px") == "#00000000 2px 2px 0px");
  185. CHECK(ParsedValue("decorator", "linear-gradient(110deg, #fff3, #fff 10%, #c33 250dp, #3c3, #33c, #000 90%, #0003) border-box") ==
  186. "linear-gradient(110deg, #fff3, #fff 10%, #c33 250dp, #3c3, #33c, #000 90%, #0003) border-box");
  187. CHECK(ParsedValue("filter", "drop-shadow(#000 30px 20px 5px) opacity(0.2) sepia(0.2)") ==
  188. "drop-shadow(#000 30px 20px 5px) opacity(0.2) sepia(0.2)");
  189. Rml::Shutdown();
  190. }