Properties.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/StyleSheetTypes.h>
  35. #include <doctest.h>
  36. using namespace Rml;
  37. TEST_CASE("Properties")
  38. {
  39. const Vector2i window_size(1024, 768);
  40. TestsSystemInterface system_interface;
  41. TestsRenderInterface render_interface;
  42. SetRenderInterface(&render_interface);
  43. SetSystemInterface(&system_interface);
  44. Rml::Initialise();
  45. Context* context = Rml::CreateContext("main", window_size);
  46. ElementDocument* document = context->CreateDocument();
  47. SUBCASE("flex")
  48. {
  49. struct FlexTestCase {
  50. String flex_value;
  51. struct ExpectedValues {
  52. float flex_grow;
  53. float flex_shrink;
  54. String flex_basis;
  55. } expected;
  56. };
  57. FlexTestCase tests[] = {
  58. {"", {0.f, 1.f, "auto"}},
  59. {"none", {0.f, 0.f, "auto"}},
  60. {"auto", {1.f, 1.f, "auto"}},
  61. {"1", {1.f, 1.f, "0px"}},
  62. {"2", {2.f, 1.f, "0px"}},
  63. {"2 0", {2.f, 0.f, "0px"}},
  64. {"2 3", {2.f, 3.f, "0px"}},
  65. {"2 auto", {2.f, 1.f, "auto"}},
  66. {"2 0 auto", {2.f, 0.f, "auto"}},
  67. {"0 0 auto", {0.f, 0.f, "auto"}},
  68. {"0 0 50px", {0.f, 0.f, "50px"}},
  69. {"0 0 50px", {0.f, 0.f, "50px"}},
  70. {"0 0 0", {0.f, 0.f, "0px"}},
  71. };
  72. for (const FlexTestCase& test : tests)
  73. {
  74. if (!test.flex_value.empty())
  75. {
  76. CHECK(document->SetProperty("flex", test.flex_value));
  77. }
  78. CHECK(document->GetProperty<float>("flex-grow") == test.expected.flex_grow);
  79. CHECK(document->GetProperty<float>("flex-shrink") == test.expected.flex_shrink);
  80. CHECK(document->GetProperty("flex-basis")->ToString() == test.expected.flex_basis);
  81. }
  82. }
  83. SUBCASE("gradient")
  84. {
  85. auto ParseGradient = [&](const String& value) -> ColorStopList {
  86. document->SetProperty("decorator", "linear-gradient(" + value + ")");
  87. auto decorators = document->GetProperty<DecoratorsPtr>("decorator");
  88. if (!decorators || decorators->list.size() != 1)
  89. return {};
  90. for (auto& id_property : decorators->list.front().properties.GetProperties())
  91. {
  92. if (id_property.second.unit == Unit::COLORSTOPLIST)
  93. return id_property.second.Get<ColorStopList>();
  94. }
  95. return {};
  96. };
  97. struct GradientTestCase {
  98. String value;
  99. ColorStopList expected_color_stops;
  100. };
  101. GradientTestCase test_cases[] = {
  102. {
  103. "red, blue",
  104. {
  105. ColorStop{Colourb(255, 0, 0), NumericValue{}},
  106. ColorStop{Colourb(0, 0, 255), NumericValue{}},
  107. },
  108. },
  109. {
  110. "red 5px, blue 50%",
  111. {
  112. ColorStop{Colourb(255, 0, 0), NumericValue{5.f, Unit::PX}},
  113. ColorStop{Colourb(0, 0, 255), NumericValue{50.f, Unit::PERCENT}},
  114. },
  115. },
  116. {
  117. "red, #00f 50%, rgba(0, 255,0, 150) 10dp",
  118. {
  119. ColorStop{Colourb(255, 0, 0), NumericValue{}},
  120. ColorStop{Colourb(0, 0, 255), NumericValue{50.f, Unit::PERCENT}},
  121. ColorStop{Colourb(0, 255, 0, 150), NumericValue{10.f, Unit::DP}},
  122. },
  123. },
  124. {
  125. "red 50px 20%, blue 10in",
  126. {
  127. ColorStop{Colourb(255, 0, 0), NumericValue{50.f, Unit::PX}},
  128. ColorStop{Colourb(255, 0, 0), NumericValue{20.f, Unit::PERCENT}},
  129. ColorStop{Colourb(0, 0, 255), NumericValue{10.f, Unit::INCH}},
  130. },
  131. },
  132. };
  133. for (const GradientTestCase& test_case : test_cases)
  134. {
  135. const ColorStopList result = ParseGradient(test_case.value);
  136. CHECK(result == test_case.expected_color_stops);
  137. }
  138. }
  139. Rml::Shutdown();
  140. }