PropertySpecification.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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/Core.h>
  30. #include <RmlUi/Core/Element.h>
  31. #include <RmlUi/Core/Factory.h>
  32. #include <RmlUi/Core/PropertyDefinition.h>
  33. #include <RmlUi/Core/PropertyDictionary.h>
  34. #include <RmlUi/Core/PropertySpecification.h>
  35. #include <RmlUi/Core/StyleSheetSpecification.h>
  36. #include <doctest.h>
  37. #include <limits.h>
  38. using namespace Rml;
  39. TEST_CASE("PropertySpecification")
  40. {
  41. TestsSystemInterface system_interface;
  42. TestsRenderInterface render_interface;
  43. SetRenderInterface(&render_interface);
  44. SetSystemInterface(&system_interface);
  45. Rml::Initialise();
  46. PropertySpecification specification(1, 0);
  47. const PropertyId id = specification.RegisterProperty("name", "", false, false).AddParser("string").GetId();
  48. // Parse value into the <string> property.
  49. auto Parse = [&](const String& test_value, const String& expected) {
  50. PropertyDictionary properties;
  51. const bool parse_success = specification.ParsePropertyDeclaration(properties, id, test_value);
  52. const auto num_properties = properties.GetProperties().size();
  53. CHECK(parse_success);
  54. CHECK(num_properties == 1);
  55. CHECK(properties.GetProperty(id));
  56. if (const Property* property = properties.GetProperty(id))
  57. {
  58. const String parsed_value = property->Get<String>();
  59. CHECK_MESSAGE(parsed_value == expected, "Test value: ", test_value);
  60. }
  61. };
  62. Parse("a", "a");
  63. Parse(" a ", "a");
  64. Parse("green", "green");
  65. Parse("image(ress:///.ress#/images/a.png)", "image(ress:///.ress#/images/a.png)");
  66. Parse(R"(image("ress:///.ress#/images/a.png"))", R"(image("ress:///.ress#/images/a.png"))");
  67. Parse(R"("ress:///.ress#/images/a.png")", R"(ress:///.ress#/images/a.png)");
  68. Parse(R"("escaped\"quotes")", R"(escaped"quotes)");
  69. Parse(R"("escaped\\backslash")", R"(escaped\backslash)");
  70. Parse(R"("bad_\escape")", R"(bad_\escape)");
  71. Parse(R"(C:\Windows\test.png)", R"(C:\Windows\test.png)");
  72. Parse(R"("C:\Windows\test.png")", R"(C:\Windows\test.png)");
  73. Parse(R"(C:\\Windows\\test.png)", R"(C:\\Windows\\test.png)");
  74. Parse(R"("C:\\Windows\\test.png")", R"(C:\Windows\test.png)");
  75. Parse(R"(\\host\test.png)", R"(\\host\test.png)");
  76. Parse(R"(\\\host\test.png)", R"(\\\host\test.png)");
  77. Parse(R"("\\host\\test.png")", R"(\host\test.png)");
  78. Parse("image(a)", "image(a)");
  79. Parse(R"(image(a))", R"(image(a))");
  80. Parse(R"(image(a, "b"))", R"(image(a, "b"))");
  81. Parse(R"V("image(a, \"b\")")V", R"V(image(a, "b"))V");
  82. Parse(R"(image( ))", R"(image( ))");
  83. Parse(R"(image( a\)b ))", R"(image( a)b ))");
  84. Parse(R"(image("a\)b"))", R"(image("a)b"))");
  85. Parse(R"(image( a\\b ))", R"(image( a\b ))");
  86. Parse(R"(image( a\\\b ))", R"(image( a\\b ))");
  87. Parse(R"(image( a\\\\b ))", R"(image( a\\b ))");
  88. Rml::Shutdown();
  89. }
  90. TEST_CASE("PropertyParser.Keyword")
  91. {
  92. TestsSystemInterface system_interface;
  93. TestsRenderInterface render_interface;
  94. SetRenderInterface(&render_interface);
  95. SetSystemInterface(&system_interface);
  96. Rml::Initialise();
  97. PropertySpecification specification(20, 0);
  98. auto Parse = [&](const PropertyId id, const String& test_value, int expected_value) {
  99. PropertyDictionary properties;
  100. const bool parse_success = specification.ParsePropertyDeclaration(properties, id, test_value);
  101. if (expected_value == INT_MAX)
  102. {
  103. CHECK(!parse_success);
  104. }
  105. else
  106. {
  107. CHECK(parse_success);
  108. CHECK(properties.GetProperties().size() == 1);
  109. const int parsed_value = properties.GetProperty(id)->Get<int>();
  110. CHECK_MESSAGE(parsed_value == expected_value, "Test value: ", test_value);
  111. const String parsed_value_str = properties.GetProperty(id)->ToString();
  112. CHECK(parsed_value_str == test_value);
  113. }
  114. };
  115. const PropertyId simple = specification.RegisterProperty("simple", "", false, false).AddParser("keyword", "a, b, c").GetId();
  116. Parse(simple, "a", 0);
  117. Parse(simple, "b", 1);
  118. Parse(simple, "c", 2);
  119. Parse(simple, "d", INT_MAX);
  120. Parse(simple, "0", INT_MAX);
  121. Parse(simple, "2", INT_MAX);
  122. const PropertyId values = specification.RegisterProperty("values", "", false, false).AddParser("keyword", "a=50, b, c=-200").GetId();
  123. Parse(values, "a", 50);
  124. Parse(values, "b", 51);
  125. Parse(values, "c", -200);
  126. Parse(values, "d", INT_MAX);
  127. Parse(values, "0", INT_MAX);
  128. Parse(values, "2", INT_MAX);
  129. const PropertyId numbers =
  130. specification.RegisterProperty("numbers", "", false, false).AddParser("keyword", "a=10, b=20, c=30").AddParser("number").GetId();
  131. Parse(numbers, "a", 10);
  132. Parse(numbers, "b", 20);
  133. Parse(numbers, "c", 30);
  134. Parse(numbers, "d", INT_MAX);
  135. Parse(numbers, "0", 0);
  136. Parse(numbers, "2", 2);
  137. Parse(numbers, "20", 20);
  138. Rml::Shutdown();
  139. }
  140. TEST_CASE("PropertyParser.InvalidShorthands")
  141. {
  142. TestsSystemInterface system_interface;
  143. TestsRenderInterface render_interface;
  144. SetRenderInterface(&render_interface);
  145. SetSystemInterface(&system_interface);
  146. Rml::Initialise();
  147. ElementPtr element = Factory::InstanceElement(nullptr, "*", "div", {});
  148. struct TestCase {
  149. bool expected_result;
  150. String name;
  151. String value;
  152. };
  153. Vector<TestCase> tests = {
  154. {true, "margin", "10px "}, //
  155. {true, "margin", "10px 20px "}, //
  156. {true, "margin", "10px 20px 30px "}, //
  157. {true, "margin", "10px 20px 30px 40px"}, //
  158. {false, "margin", ""}, // Too few values
  159. {false, "margin", "10px 20px 30px 40px 50px"}, // Too many values
  160. {true, "flex", "1 2 3px"}, //
  161. {false, "flex", "1 2 3px 4"}, // Too many values
  162. {false, "flex", "1px 2 3 4"}, // Wrong order
  163. {true, "perspective-origin", "center center"}, //
  164. {true, "perspective-origin", "left top"}, //
  165. {false, "perspective-origin", "center center center"}, // Too many values
  166. {false, "perspective-origin", "left top 50px"}, // Too many values
  167. {false, "perspective-origin", "50px 50px left"}, // Too many values
  168. {false, "perspective-origin", "top left"}, // Wrong order
  169. {false, "perspective-origin", "50px left"}, // Wrong order
  170. {true, "font", "20px arial"}, //
  171. {false, "font", "arial 20px"}, // Wrong order
  172. {true, "decorator", "gradient(vertical blue red)"}, //
  173. {false, "decorator", "gradient(blue red vertical)"}, // Wrong order
  174. {false, "decorator", "gradient(blue vertical red)"}, // Wrong order
  175. {false, "decorator", "gradient(vertical blue red green)"}, // Too many values
  176. {true, "overflow", "hidden"}, //
  177. {true, "overflow", "scroll hidden"}, //
  178. {false, "overflow", ""}, // Too few values
  179. {false, "overflow", "scroll hidden scroll"}, // Too many values
  180. };
  181. for (const TestCase& test : tests)
  182. {
  183. PropertyDictionary properties;
  184. INFO(test.name, ": ", test.value);
  185. CHECK(StyleSheetSpecification::ParsePropertyDeclaration(properties, test.name, test.value) == test.expected_result);
  186. // Ensure we get a warning when trying to set the invalid property on an element.
  187. system_interface.SetNumExpectedWarnings(test.expected_result ? 0 : 1);
  188. CHECK(element->SetProperty(test.name, test.value) == test.expected_result);
  189. }
  190. element.reset();
  191. Rml::Shutdown();
  192. }