Animation.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 { %s: %s; }
  51. to { %s: %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 #7f7f7f3f #7f7f7f3f)",
  83. },
  84. {
  85. "",
  86. "",
  87. "horizontal-gradient(transparent transparent) border-box",
  88. "horizontal-gradient(white white) border-box",
  89. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f) border-box",
  90. },
  91. {
  92. "",
  93. "",
  94. "none",
  95. "horizontal-gradient(transparent transparent)",
  96. "horizontal-gradient(horizontal #dcdcdcbf #dcdcdcbf)",
  97. },
  98. {
  99. "",
  100. "",
  101. "none",
  102. "horizontal-gradient(transparent transparent), horizontal-gradient(transparent transparent)",
  103. "horizontal-gradient(horizontal #dcdcdcbf #dcdcdcbf), horizontal-gradient(horizontal #dcdcdcbf #dcdcdcbf)",
  104. },
  105. {
  106. "",
  107. "",
  108. "horizontal-gradient(transparent transparent), horizontal-gradient(transparent transparent)",
  109. "none",
  110. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f), horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f)",
  111. },
  112. /// Only rule declaration
  113. {
  114. "start-color: transparent; stop-color: transparent;",
  115. "start-color: white; stop-color: white;",
  116. "from_rule",
  117. "to_rule",
  118. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f)",
  119. },
  120. {
  121. "",
  122. "start-color: transparent; stop-color: transparent;",
  123. "from_rule",
  124. "to_rule",
  125. "horizontal-gradient(horizontal #dcdcdcbf #dcdcdcbf)",
  126. },
  127. {
  128. "start-color: transparent; stop-color: transparent;",
  129. "",
  130. "from_rule",
  131. "to_rule",
  132. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f)",
  133. },
  134. /// Mix rule and standard declaration
  135. {
  136. "start-color: transparent; stop-color: transparent;",
  137. "",
  138. "from_rule",
  139. "horizontal-gradient(white white)",
  140. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f)",
  141. },
  142. {
  143. "",
  144. "start-color: transparent; stop-color: transparent;",
  145. "none",
  146. "to_rule",
  147. "horizontal-gradient(horizontal #dcdcdcbf #dcdcdcbf)",
  148. },
  149. {
  150. "start-color: transparent; stop-color: transparent;",
  151. "",
  152. "from_rule",
  153. "none",
  154. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f)",
  155. },
  156. {
  157. "",
  158. "",
  159. "from_rule, to_rule",
  160. "horizontal-gradient(transparent transparent), horizontal-gradient(transparent transparent)",
  161. "horizontal-gradient(horizontal #dcdcdcbf #dcdcdcbf), horizontal-gradient(horizontal #dcdcdcbf #dcdcdcbf)",
  162. },
  163. {
  164. "",
  165. "",
  166. "horizontal-gradient(transparent transparent), horizontal-gradient(transparent transparent)",
  167. "from_rule, to_rule",
  168. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f), horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f)",
  169. },
  170. };
  171. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  172. Context* context = TestsShell::GetContext();
  173. for (const char* property_str : {"decorator", "mask-image"})
  174. {
  175. for (const Test& test : tests)
  176. {
  177. const double t_final = 0.1;
  178. system_interface->SetTime(0.0);
  179. String document_rml = Rml::CreateString(document_decorator_rml.size() + 512, document_decorator_rml.c_str(), test.from_rule.c_str(),
  180. test.to_rule.c_str(), property_str, test.from.c_str(), property_str, test.to.c_str());
  181. ElementDocument* document = context->LoadDocumentFromMemory(document_rml, "assets/");
  182. Element* element = document->GetChild(0);
  183. document->Show();
  184. TestsShell::RenderLoop();
  185. system_interface->SetTime(0.25 * t_final);
  186. TestsShell::RenderLoop();
  187. CHECK_MESSAGE(element->GetProperty<String>(property_str) == test.expected_25p, property_str, " from: ", test.from, ", to: ", test.to);
  188. document->Close();
  189. }
  190. }
  191. system_interface->SetTime(0.0);
  192. TestsShell::ShutdownShell();
  193. }
  194. static const String document_filter_rml = R"(
  195. <rml>
  196. <head>
  197. <title>Test</title>
  198. <link type="text/rcss" href="/assets/rml.rcss"/>
  199. <style>
  200. body {
  201. left: 0;
  202. top: 0;
  203. right: 0;
  204. bottom: 0;
  205. }
  206. @keyframes mix {
  207. from { %s: %s; }
  208. to { %s: %s; }
  209. }
  210. div {
  211. background: #333;
  212. height: 64px;
  213. width: 64px;
  214. decorator: image(high_scores_alien_1.tga);
  215. animation: mix 0.1s;
  216. }
  217. </style>
  218. </head>
  219. <body>
  220. <div/>
  221. </body>
  222. </rml>
  223. )";
  224. TEST_CASE("animation.filter")
  225. {
  226. struct Test {
  227. String from;
  228. String to;
  229. String expected_25p; // expected interpolated value at 25% progression
  230. };
  231. Vector<Test> tests{
  232. {
  233. "blur( 0px)",
  234. "blur(40px)",
  235. "blur(10px)",
  236. },
  237. {
  238. "blur(10px)",
  239. "blur(25dp)", // assumes dp-ratio == 2
  240. "blur(20px)",
  241. },
  242. {
  243. "blur(40px)",
  244. "none",
  245. "blur(30px)",
  246. },
  247. {
  248. "none",
  249. "blur(40px)",
  250. "blur(10px)",
  251. },
  252. {
  253. "drop-shadow(#000 30px 20px 0px)",
  254. "drop-shadow(#f00 30px 20px 4px)", // colors interpolated in linear space
  255. "drop-shadow(#7f0000 30px 20px 1px)",
  256. },
  257. {
  258. "opacity(0) brightness(2)",
  259. "none",
  260. "opacity(0.25) brightness(1.75)",
  261. },
  262. {
  263. "opacity(0) brightness(0)",
  264. "opacity(0.5)",
  265. "opacity(0.125) brightness(0.25)",
  266. },
  267. {
  268. "opacity(0.5)",
  269. "opacity(0) brightness(0)",
  270. "opacity(0.375) brightness(0.75)",
  271. },
  272. {
  273. "opacity(0) brightness(0)",
  274. "brightness(1) opacity(0.5)", // discrete interpolation due to non-matching types
  275. "opacity(0) brightness(0)",
  276. },
  277. {
  278. "none", // Test initial values of various filters.
  279. "brightness(2.00) contrast(2.00) grayscale(1.00) hue-rotate(4rad) invert(1.00) opacity(0.00) sepia(1.00) saturate(2.00)",
  280. "brightness(1.25) contrast(1.25) grayscale(0.25) hue-rotate(1rad) invert(0.25) opacity(0.75) sepia(0.25) saturate(1.25)",
  281. },
  282. };
  283. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  284. Context* context = TestsShell::GetContext();
  285. context->SetDensityIndependentPixelRatio(2.0f);
  286. for (const char* property_str : {"filter", "backdrop-filter"})
  287. {
  288. for (const Test& test : tests)
  289. {
  290. const double t_final = 0.1;
  291. system_interface->SetTime(0.0);
  292. String document_rml = Rml::CreateString(document_filter_rml.size() + 512, document_filter_rml.c_str(), property_str, test.from.c_str(),
  293. property_str, test.to.c_str());
  294. ElementDocument* document = context->LoadDocumentFromMemory(document_rml, "assets/");
  295. Element* element = document->GetChild(0);
  296. document->Show();
  297. system_interface->SetTime(0.25 * t_final);
  298. TestsShell::RenderLoop();
  299. CHECK_MESSAGE(element->GetProperty<String>(property_str) == test.expected_25p, property_str, " from: ", test.from, ", to: ", test.to);
  300. document->Close();
  301. }
  302. }
  303. system_interface->SetTime(0.0);
  304. TestsShell::ShutdownShell();
  305. }