Animation.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 "../Common/TestsShell.h"
  30. #include <RmlUi/Core/Context.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_decorator_rml = R"(
  36. <rml>
  37. <head>
  38. <title>Test</title>
  39. <link type="text/rcss" href="/assets/rml.rcss"/>
  40. <style>
  41. body {
  42. left: 0;
  43. top: 0;
  44. right: 0;
  45. bottom: 0;
  46. }
  47. @decorator from_rule : horizontal-gradient { %s }
  48. @decorator to_rule: horizontal-gradient{ %s }
  49. @keyframes mix {
  50. from { decorator: %s; }
  51. to { decorator: %s; }
  52. }
  53. div {
  54. background: #333;
  55. height: 64px;
  56. width: 64px;
  57. animation: mix 0.1s;
  58. }
  59. </style>
  60. </head>
  61. <body>
  62. <div/>
  63. </body>
  64. </rml>
  65. )";
  66. TEST_CASE("animation.decorator")
  67. {
  68. struct Test {
  69. String from_rule;
  70. String to_rule;
  71. String from;
  72. String to;
  73. String expected_25p; // expected interpolated value at 25% progression
  74. };
  75. Vector<Test> tests{
  76. // Only standard declaration
  77. {
  78. "",
  79. "",
  80. "horizontal-gradient(transparent transparent)",
  81. "horizontal-gradient(white white)",
  82. "horizontal-gradient(horizontal rgba(127,127,127,63) rgba(127,127,127,63))",
  83. },
  84. {
  85. "",
  86. "",
  87. "none",
  88. "horizontal-gradient(transparent transparent)",
  89. "horizontal-gradient(horizontal rgba(220,220,220,191) rgba(220,220,220,191))",
  90. },
  91. {
  92. "",
  93. "",
  94. "none",
  95. "horizontal-gradient(transparent transparent), horizontal-gradient(transparent transparent)",
  96. "horizontal-gradient(horizontal rgba(220,220,220,191) rgba(220,220,220,191)), horizontal-gradient(horizontal rgba(220,220,220,191) "
  97. "rgba(220,220,220,191))",
  98. },
  99. {
  100. "",
  101. "",
  102. "horizontal-gradient(transparent transparent), horizontal-gradient(transparent transparent)",
  103. "none",
  104. "horizontal-gradient(horizontal rgba(127,127,127,63) rgba(127,127,127,63)), horizontal-gradient(horizontal rgba(127,127,127,63) "
  105. "rgba(127,127,127,63))",
  106. },
  107. /// Only rule declaration
  108. {
  109. "start-color: transparent; stop-color: transparent;",
  110. "start-color: white; stop-color: white;",
  111. "from_rule",
  112. "to_rule",
  113. "horizontal-gradient(horizontal rgba(127,127,127,63) rgba(127,127,127,63))",
  114. },
  115. {
  116. "",
  117. "start-color: transparent; stop-color: transparent;",
  118. "from_rule",
  119. "to_rule",
  120. "horizontal-gradient(horizontal rgba(220,220,220,191) rgba(220,220,220,191))",
  121. },
  122. {
  123. "start-color: transparent; stop-color: transparent;",
  124. "",
  125. "from_rule",
  126. "to_rule",
  127. "horizontal-gradient(horizontal rgba(127,127,127,63) rgba(127,127,127,63))",
  128. },
  129. /// Mix rule and standard declaration
  130. {
  131. "start-color: transparent; stop-color: transparent;",
  132. "",
  133. "from_rule",
  134. "horizontal-gradient(white white)",
  135. "horizontal-gradient(horizontal rgba(127,127,127,63) rgba(127,127,127,63))",
  136. },
  137. {
  138. "",
  139. "start-color: transparent; stop-color: transparent;",
  140. "none",
  141. "to_rule",
  142. "horizontal-gradient(horizontal rgba(220,220,220,191) rgba(220,220,220,191))",
  143. },
  144. {
  145. "start-color: transparent; stop-color: transparent;",
  146. "",
  147. "from_rule",
  148. "none",
  149. "horizontal-gradient(horizontal rgba(127,127,127,63) rgba(127,127,127,63))",
  150. },
  151. {
  152. "",
  153. "",
  154. "from_rule, to_rule",
  155. "horizontal-gradient(transparent transparent), horizontal-gradient(transparent transparent)",
  156. "horizontal-gradient(horizontal rgba(220,220,220,191) rgba(220,220,220,191)), horizontal-gradient(horizontal rgba(220,220,220,191) "
  157. "rgba(220,220,220,191))",
  158. },
  159. {
  160. "",
  161. "",
  162. "horizontal-gradient(transparent transparent), horizontal-gradient(transparent transparent)",
  163. "from_rule, to_rule",
  164. "horizontal-gradient(horizontal rgba(127,127,127,63) rgba(127,127,127,63)), horizontal-gradient(horizontal rgba(127,127,127,63) "
  165. "rgba(127,127,127,63))",
  166. },
  167. };
  168. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  169. Context* context = TestsShell::GetContext();
  170. context->SetDensityIndependentPixelRatio(2.0f);
  171. for (const Test& test : tests)
  172. {
  173. const double t_final = 0.1;
  174. system_interface->SetTime(0.0);
  175. String document_rml = Rml::CreateString(document_decorator_rml.size() + 512, document_decorator_rml.c_str(), test.from_rule.c_str(),
  176. test.to_rule.c_str(), test.from.c_str(), test.to.c_str());
  177. ElementDocument* document = context->LoadDocumentFromMemory(document_rml, "assets/");
  178. Element* element = document->GetChild(0);
  179. document->Show();
  180. TestsShell::RenderLoop();
  181. system_interface->SetTime(0.25 * t_final);
  182. TestsShell::RenderLoop();
  183. CHECK_MESSAGE(element->GetProperty<String>("decorator") == test.expected_25p, "from: ", test.from, ", to: ", test.to);
  184. document->Close();
  185. }
  186. system_interface->SetTime(0.0);
  187. TestsShell::ShutdownShell();
  188. }