Animation.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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 "../Common/TypesToString.h"
  31. #include <RmlUi/Core/Context.h>
  32. #include <RmlUi/Core/Element.h>
  33. #include <RmlUi/Core/ElementDocument.h>
  34. #include <doctest.h>
  35. #include <float.h>
  36. using namespace Rml;
  37. static const String document_decorator_rml = R"(
  38. <rml>
  39. <head>
  40. <title>Test</title>
  41. <link type="text/rcss" href="/assets/rml.rcss"/>
  42. <style>
  43. body {
  44. left: 0;
  45. top: 0;
  46. right: 0;
  47. bottom: 0;
  48. }
  49. @decorator from_rule : horizontal-gradient { %s }
  50. @decorator to_rule: horizontal-gradient{ %s }
  51. @keyframes mix {
  52. from { %s: %s; }
  53. to { %s: %s; }
  54. }
  55. div {
  56. background: #333;
  57. height: 64px;
  58. width: 64px;
  59. animation: mix 0.1s;
  60. }
  61. </style>
  62. </head>
  63. <body>
  64. <div/>
  65. </body>
  66. </rml>
  67. )";
  68. TEST_CASE("animation.decorator")
  69. {
  70. struct Test {
  71. String from_rule;
  72. String to_rule;
  73. String from;
  74. String to;
  75. String expected_25p; // expected interpolated value at 25% progression
  76. };
  77. Vector<Test> tests{
  78. // Only standard declaration
  79. {
  80. "",
  81. "",
  82. "horizontal-gradient(transparent transparent)",
  83. "horizontal-gradient(white white)",
  84. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f)",
  85. },
  86. {
  87. "",
  88. "",
  89. "horizontal-gradient(transparent transparent) border-box",
  90. "horizontal-gradient(white white) border-box",
  91. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f) border-box",
  92. },
  93. {
  94. "",
  95. "",
  96. "none",
  97. "horizontal-gradient(transparent transparent)",
  98. "horizontal-gradient(horizontal #dcdcdcbf #dcdcdcbf)",
  99. },
  100. {
  101. "",
  102. "",
  103. "none",
  104. "horizontal-gradient(transparent transparent), horizontal-gradient(transparent transparent)",
  105. "horizontal-gradient(horizontal #dcdcdcbf #dcdcdcbf), horizontal-gradient(horizontal #dcdcdcbf #dcdcdcbf)",
  106. },
  107. {
  108. "",
  109. "",
  110. "horizontal-gradient(transparent transparent), horizontal-gradient(transparent transparent)",
  111. "none",
  112. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f), horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f)",
  113. },
  114. /// Only rule declaration
  115. {
  116. "start-color: transparent; stop-color: transparent;",
  117. "start-color: white; stop-color: white;",
  118. "from_rule",
  119. "to_rule",
  120. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f)",
  121. },
  122. {
  123. "",
  124. "start-color: transparent; stop-color: transparent;",
  125. "from_rule",
  126. "to_rule",
  127. "horizontal-gradient(horizontal #dcdcdcbf #dcdcdcbf)",
  128. },
  129. {
  130. "start-color: transparent; stop-color: transparent;",
  131. "",
  132. "from_rule",
  133. "to_rule",
  134. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f)",
  135. },
  136. /// Mix rule and standard declaration
  137. {
  138. "start-color: transparent; stop-color: transparent;",
  139. "",
  140. "from_rule",
  141. "horizontal-gradient(white white)",
  142. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f)",
  143. },
  144. {
  145. "",
  146. "start-color: transparent; stop-color: transparent;",
  147. "none",
  148. "to_rule",
  149. "horizontal-gradient(horizontal #dcdcdcbf #dcdcdcbf)",
  150. },
  151. {
  152. "start-color: transparent; stop-color: transparent;",
  153. "",
  154. "from_rule",
  155. "none",
  156. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f)",
  157. },
  158. {
  159. "",
  160. "",
  161. "from_rule, to_rule",
  162. "horizontal-gradient(transparent transparent), horizontal-gradient(transparent transparent)",
  163. "horizontal-gradient(horizontal #dcdcdcbf #dcdcdcbf), horizontal-gradient(horizontal #dcdcdcbf #dcdcdcbf)",
  164. },
  165. {
  166. "",
  167. "",
  168. "horizontal-gradient(transparent transparent), horizontal-gradient(transparent transparent)",
  169. "from_rule, to_rule",
  170. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f), horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f)",
  171. },
  172. // Standard declaration of linear gradients (consider string conversion a best-effort for now)
  173. {
  174. "",
  175. "",
  176. "linear-gradient(transparent, transparent)",
  177. "linear-gradient(white, white)",
  178. "linear-gradient(180deg unspecified unspecified unspecified #7d7d7d3f, #7d7d7d3f)",
  179. },
  180. {
  181. "",
  182. "",
  183. "linear-gradient(0deg, transparent, transparent)",
  184. "linear-gradient(180deg, white, white)",
  185. "linear-gradient(45deg unspecified unspecified unspecified #7d7d7d3f, #7d7d7d3f)",
  186. },
  187. {
  188. "",
  189. "",
  190. "linear-gradient(105deg, #000000 0%, #ff0000 100%)",
  191. "linear-gradient(105deg, #ffffff 20%, #00ff00 60%)",
  192. "linear-gradient(105deg unspecified unspecified unspecified #7f7f7f 5%, #dc7f00 90%)",
  193. },
  194. {
  195. "",
  196. "",
  197. "linear-gradient(105deg, #000000 0%, #ff0000 50%, #ff00ff 100%)",
  198. "linear-gradient(105deg, #ffffff 0%, #ffffff 10%, #ffffff 100%)",
  199. "linear-gradient(105deg unspecified unspecified unspecified #7f7f7f 0%, #ff7f7f 40%, #ff7fff 100%)",
  200. },
  201. {
  202. "",
  203. "",
  204. "linear-gradient(to right, transparent, transparent)",
  205. "linear-gradient(270deg, transparent, transparent)",
  206. // We don't really handle mixing direction keywords and angles here, the output will not be what one might expect.
  207. "linear-gradient(202.5deg to right unspecified #00000000, #00000000)",
  208. },
  209. {
  210. "",
  211. "",
  212. "linear-gradient(to right, transparent, transparent)",
  213. "linear-gradient(to left, transparent, transparent)",
  214. // This will effectively evaluate to "to right" with angle being ignored, resulting in discrete interpolation. Not ideal.
  215. "linear-gradient(180deg to right unspecified #00000000, #00000000)",
  216. },
  217. {
  218. "",
  219. "",
  220. "linear-gradient(#000000 0%, #ffffff 100%)",
  221. "repeating-linear-gradient(#000000 0%, #ffffff 100%)",
  222. "linear-gradient(#000000 0%, #ffffff 100%)",
  223. },
  224. };
  225. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  226. Context* context = TestsShell::GetContext();
  227. for (const String property_str : {"decorator", "mask-image"})
  228. {
  229. for (const Test& test : tests)
  230. {
  231. const double t_final = 0.1;
  232. system_interface->SetManualTime(0.0);
  233. String document_rml = Rml::CreateString(document_decorator_rml.c_str(), test.from_rule.c_str(), test.to_rule.c_str(),
  234. property_str.c_str(), test.from.c_str(), property_str.c_str(), test.to.c_str());
  235. ElementDocument* document = context->LoadDocumentFromMemory(document_rml, "assets/");
  236. Element* element = document->GetChild(0);
  237. document->Show();
  238. TestsShell::RenderLoop();
  239. system_interface->SetManualTime(0.25 * t_final);
  240. TestsShell::RenderLoop();
  241. CAPTURE(property_str);
  242. CAPTURE(test.from);
  243. CAPTURE(test.to);
  244. CHECK(element->GetProperty<String>(property_str) == test.expected_25p);
  245. document->Close();
  246. }
  247. }
  248. TestsShell::ShutdownShell();
  249. }
  250. static const String document_filter_rml = R"(
  251. <rml>
  252. <head>
  253. <title>Test</title>
  254. <link type="text/rcss" href="/assets/rml.rcss"/>
  255. <style>
  256. body {
  257. left: 0;
  258. top: 0;
  259. right: 0;
  260. bottom: 0;
  261. }
  262. @keyframes mix {
  263. from { %s: %s; }
  264. to { %s: %s; }
  265. }
  266. div {
  267. background: #333;
  268. height: 64px;
  269. width: 64px;
  270. decorator: image(high_scores_alien_1.tga);
  271. animation: mix 0.1s;
  272. }
  273. </style>
  274. </head>
  275. <body>
  276. <div/>
  277. </body>
  278. </rml>
  279. )";
  280. TEST_CASE("animation.filter")
  281. {
  282. struct Test {
  283. String from;
  284. String to;
  285. String expected_25p; // expected interpolated value at 25% progression
  286. };
  287. Vector<Test> tests{
  288. {
  289. "blur( 0px)",
  290. "blur(40px)",
  291. "blur(10px)",
  292. },
  293. {
  294. "blur(10px)",
  295. "blur(25dp)", // assumes dp-ratio == 2
  296. "blur(20px)",
  297. },
  298. {
  299. "blur(40px)",
  300. "none",
  301. "blur(30px)",
  302. },
  303. {
  304. "none",
  305. "blur(40px)",
  306. "blur(10px)",
  307. },
  308. {
  309. "drop-shadow(#000 30px 20px 0px)",
  310. "drop-shadow(#f00 30px 20px 4px)", // colors interpolated in linear space
  311. "drop-shadow(#7f0000 30px 20px 1px)",
  312. },
  313. {
  314. "opacity(0) brightness(2)",
  315. "none",
  316. "opacity(0.25) brightness(1.75)",
  317. },
  318. {
  319. "opacity(0) brightness(0)",
  320. "opacity(0.5)",
  321. "opacity(0.125) brightness(0.25)",
  322. },
  323. {
  324. "opacity(0.5)",
  325. "opacity(0) brightness(0)",
  326. "opacity(0.375) brightness(0.75)",
  327. },
  328. {
  329. "opacity(0) brightness(0)",
  330. "brightness(1) opacity(0.5)", // discrete interpolation due to non-matching types
  331. "opacity(0) brightness(0)",
  332. },
  333. {
  334. "none", // Test initial values of various filters.
  335. "brightness(2.00) contrast(2.00) grayscale(1.00) hue-rotate(4rad) invert(1.00) opacity(0.00) sepia(1.00) saturate(2.00)",
  336. "brightness(1.25) contrast(1.25) grayscale(0.25) hue-rotate(1rad) invert(0.25) opacity(0.75) sepia(0.25) saturate(1.25)",
  337. },
  338. };
  339. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  340. Context* context = TestsShell::GetContext();
  341. context->SetDensityIndependentPixelRatio(2.0f);
  342. for (const char* property_str : {"filter", "backdrop-filter"})
  343. {
  344. for (const Test& test : tests)
  345. {
  346. const double t_final = 0.1;
  347. system_interface->SetManualTime(0.0);
  348. String document_rml = Rml::CreateString(document_filter_rml.c_str(), property_str, test.from.c_str(), property_str, test.to.c_str());
  349. ElementDocument* document = context->LoadDocumentFromMemory(document_rml, "assets/");
  350. Element* element = document->GetChild(0);
  351. document->Show();
  352. system_interface->SetManualTime(0.25 * t_final);
  353. TestsShell::RenderLoop();
  354. CHECK_MESSAGE(element->GetProperty<String>(property_str) == test.expected_25p, property_str, " from: ", test.from, ", to: ", test.to);
  355. document->Close();
  356. }
  357. }
  358. TestsShell::ShutdownShell();
  359. }
  360. TEST_CASE("animation.case_sensitivity")
  361. {
  362. static const String document_rml_template = R"(
  363. <rml>
  364. <head>
  365. <title>Test</title>
  366. <link type="text/rcss" href="/assets/rml.rcss"/>
  367. <style>
  368. body {
  369. left: 0;
  370. top: 0;
  371. right: 0;
  372. bottom: 0;
  373. }
  374. @keyframes %s {
  375. to { visibility: visible; }
  376. }
  377. div {
  378. width: 300dp;
  379. height: 300dp;
  380. visibility: hidden;
  381. background-color: #666;
  382. animation: 1.5s %s;
  383. }
  384. </style>
  385. </head>
  386. <body>
  387. <div/>
  388. </body>
  389. </rml>
  390. )";
  391. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  392. Context* context = TestsShell::GetContext();
  393. struct TestCase {
  394. String keyframe_name;
  395. String animation_name;
  396. bool expected_visible_end;
  397. };
  398. const Vector<TestCase> test_cases = {
  399. // TODO: Make name-lookups case sensitive:
  400. {"mix", "Mix", false},
  401. {"Mix", "mix", false},
  402. {"show", "show", true},
  403. {"Show", "Show", true},
  404. };
  405. for (const auto& test_case : test_cases)
  406. {
  407. system_interface->SetManualTime(0.0);
  408. INFO("@keyframes: ", test_case.keyframe_name);
  409. INFO("animation: ", test_case.animation_name);
  410. const String document_rml = CreateString(document_rml_template.c_str(), test_case.keyframe_name.c_str(), test_case.animation_name.c_str());
  411. ElementDocument* document = context->LoadDocumentFromMemory(document_rml, "assets/");
  412. Element* element = document->GetChild(0);
  413. document->Show();
  414. REQUIRE(element->IsVisible() == false);
  415. constexpr double t_end = 1.0;
  416. constexpr double dt = 0.1;
  417. double t = 0;
  418. while (t < t_end)
  419. {
  420. t += dt;
  421. system_interface->SetManualTime(t);
  422. context->Update();
  423. }
  424. CHECK(element->IsVisible() == test_case.expected_visible_end);
  425. document->Close();
  426. }
  427. TestsShell::ShutdownShell();
  428. }
  429. TEST_CASE("animation.case_sensitive_distinct_keyframes")
  430. {
  431. static const String document_rml = R"(
  432. <rml>
  433. <head>
  434. <title>Test</title>
  435. <link type="text/rcss" href="/assets/rml.rcss"/>
  436. <style>
  437. body {
  438. left: 0;
  439. top: 0;
  440. right: 0;
  441. bottom: 0;
  442. }
  443. @keyframes show {
  444. 50%, 100% { opacity: 0.25; }
  445. }
  446. @keyframes Show {
  447. 50%, 100% { opacity: 0.5; }
  448. }
  449. @keyframes SHOW {
  450. 0%, 25% { opacity: 0.75; }
  451. 75%, 100% { opacity: 1.0; }
  452. }
  453. div {
  454. width: 300dp;
  455. height: 300dp;
  456. opacity: 0.0;
  457. background-color: #666;
  458. }
  459. .show-lower {
  460. animation: 2s show;
  461. }
  462. .show-upper {
  463. animation: 2s Show;
  464. }
  465. .show-infinite {
  466. animation: 0.5s infinite alternate SHOW;
  467. }
  468. .show-infinite-upper-keywords {
  469. animation: 0.5s INFINITE ALTERNATE SHOW;
  470. }
  471. </style>
  472. </head>
  473. <body>
  474. <div/>
  475. </body>
  476. </rml>
  477. )";
  478. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  479. Context* context = TestsShell::GetContext();
  480. struct TestCase {
  481. String class_name;
  482. float expected_opacity_end;
  483. };
  484. const Vector<TestCase> test_cases = {
  485. {"show-lower", 0.25f},
  486. {"show-upper", 0.5f},
  487. {"show-infinite", 0.75f},
  488. {"show-infinite-upper-keywords", 0.75f},
  489. };
  490. for (const auto& test_case : test_cases)
  491. {
  492. INFO("Class name: ", test_case.class_name);
  493. system_interface->SetManualTime(0.0);
  494. ElementDocument* document = context->LoadDocumentFromMemory(document_rml, "assets/");
  495. Element* element = document->GetChild(0);
  496. element->SetClass(test_case.class_name, true);
  497. document->Show();
  498. REQUIRE(element->GetProperty<float>("opacity") == 0.f);
  499. constexpr double t_end = 1.0;
  500. constexpr double dt = 0.1;
  501. double t = 0;
  502. while (t < t_end)
  503. {
  504. t += dt;
  505. system_interface->SetManualTime(t);
  506. context->Update();
  507. }
  508. CHECK(element->GetProperty<float>("opacity") == test_case.expected_opacity_end);
  509. document->Close();
  510. }
  511. TestsShell::ShutdownShell();
  512. }
  513. static const String document_multiple_values_rml = R"(
  514. <rml>
  515. <head>
  516. <title>Test</title>
  517. <link type="text/rcss" href="/assets/rml.rcss"/>
  518. <style>
  519. body {
  520. left: 0;
  521. top: 0;
  522. right: 0;
  523. bottom: 0;
  524. }
  525. @keyframes fade {
  526. from { opacity: 0; }
  527. to { opacity: 1; }
  528. }
  529. @keyframes colorize {
  530. from { background-color: #f00; }
  531. 25% { background-color: #0f0; }
  532. to { background-color: #00f; }
  533. }
  534. div {
  535. width: 300dp;
  536. height: 300dp;
  537. background-color: #000;
  538. animation: 2s fade, 4s 1s colorize;
  539. }
  540. </style>
  541. </head>
  542. <body>
  543. <div/>
  544. </body>
  545. </rml>
  546. )";
  547. TEST_CASE("animation.multiple_values")
  548. {
  549. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  550. Context* context = TestsShell::GetContext();
  551. const double dt = 0.1;
  552. const double t_fade_start = 0;
  553. const double t_colorize_start = 1;
  554. const double t_fade_final = 2;
  555. const double t_colorize_final = 5;
  556. struct TestCase {
  557. double time;
  558. float opacity_min;
  559. Colourb background_color;
  560. };
  561. const std::vector<TestCase> tests = {
  562. {t_fade_start, 0.f, {0, 0, 0}},
  563. {t_fade_start + dt, 0.01f, {0, 0, 0}},
  564. {t_colorize_start - dt, 0.4f, {0, 0, 0}},
  565. {t_colorize_start, 0.49f, {0, 0, 0}},
  566. {t_colorize_start + dt, 0.5f, {0xf1, 0x50, 0}},
  567. {t_fade_final - dt, 0.9f, {0x50, 0xf1, 0}},
  568. {t_fade_final, 0.99f, {0, 0xfe, 0x00}},
  569. {t_fade_final + dt, 1.0f, {0, 0xfa, 0x2e}},
  570. {t_colorize_final - dt, 1.0f, {0, 0x2e, 0xfa}},
  571. {t_colorize_final, 1.0f, {0, 0, 0xfe}},
  572. {t_colorize_final + dt, 1.0f, {0, 0, 0}},
  573. };
  574. {
  575. system_interface->SetManualTime(0.0);
  576. ElementDocument* document = context->LoadDocumentFromMemory(document_multiple_values_rml, "assets/");
  577. Element* element = document->GetChild(0);
  578. document->Show();
  579. double t = 0.f;
  580. for (const auto& test : tests)
  581. {
  582. // Simulate progression in small time steps since animations are constrained to 0.1s maximum step size.
  583. while (t < test.time)
  584. {
  585. t = Math::Min(t + dt, test.time);
  586. system_interface->SetManualTime(t);
  587. context->Update();
  588. }
  589. INFO("Time: ", test.time);
  590. CHECK(element->GetProperty<float>("opacity") >= test.opacity_min);
  591. CHECK(element->GetProperty<Colourb>("background-color") == test.background_color);
  592. }
  593. document->Close();
  594. }
  595. TestsShell::ShutdownShell();
  596. }
  597. static const String document_animation_multiple_values_rml = R"(
  598. <rml>
  599. <head>
  600. <title>Test</title>
  601. <link type="text/rcss" href="/assets/rml.rcss"/>
  602. <style>
  603. body {
  604. left: 0;
  605. top: 0;
  606. right: 0;
  607. bottom: 0;
  608. }
  609. @keyframes fadein {
  610. from {
  611. opacity: 0;
  612. visibility: hidden;
  613. }
  614. to {
  615. opacity: 1;
  616. visibility: visible;
  617. }
  618. }
  619. @keyframes fadeout {
  620. from {
  621. opacity: 1;
  622. visibility: visible;
  623. }
  624. to {
  625. opacity: 0;
  626. visibility: hidden;
  627. }
  628. }
  629. div {
  630. background-color: #c66;
  631. width: 300dp;
  632. height: 300dp;
  633. margin: auto;
  634. animation: 1s fadein, 1s 5s fadeout;
  635. }
  636. </style>
  637. </head>
  638. <body>
  639. <div id="div"/>
  640. </body>
  641. </rml>
  642. )";
  643. TEST_CASE("animation.multiple_overlapping")
  644. {
  645. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  646. Context* context = TestsShell::GetContext();
  647. const float greater_than_zero = FLT_MIN;
  648. const double dt = 1.0 / 60.0;
  649. const double t0 = 0;
  650. const double t_delta = 0.1;
  651. const double t_fadein_final = 1;
  652. const double t_fadeout_start = 5;
  653. const double t_fadeout_final = 6;
  654. struct TestCase {
  655. double time;
  656. float opacity_min;
  657. bool is_visible;
  658. };
  659. const std::vector<TestCase> tests = {
  660. {t0, 0.f, true},
  661. {t0 + t_delta, greater_than_zero, true},
  662. {t_fadein_final - t_delta, 0.8f, true},
  663. {t_fadein_final, 1.f, true},
  664. {t_fadein_final + t_delta, 1.f, true},
  665. {t_fadeout_start - t_delta, 1.f, true},
  666. {t_fadeout_start, 1.f, true},
  667. {t_fadeout_start + t_delta, 0.8f, true},
  668. {t_fadeout_final - t_delta, greater_than_zero, true},
  669. {t_fadeout_final, 0.f, false},
  670. {t_fadeout_final + t_delta, 0.f, false},
  671. };
  672. // Currently we don't support animating the same property from multiple animations on the same element, see #608.
  673. // This test only checks that we submit warnings to the user. Once we support this feature, we can enable more checks in this test.
  674. {
  675. system_interface->SetNumExpectedWarnings(2);
  676. system_interface->SetManualTime(0.0);
  677. ElementDocument* document = context->LoadDocumentFromMemory(document_animation_multiple_values_rml, "assets/");
  678. Element* element = document->GetChild(0);
  679. document->Show();
  680. TestsShell::RenderLoop();
  681. double t = 0.f;
  682. for (const auto& test : tests)
  683. {
  684. while (t < test.time)
  685. {
  686. t = Math::Min(t + dt, test.time);
  687. system_interface->SetManualTime(t);
  688. context->Update();
  689. }
  690. INFO("Time: ", test.time);
  691. (void)(element->GetProperty<float>("opacity") == test.opacity_min);
  692. (void)(element->IsVisible() == test.is_visible);
  693. }
  694. document->Close();
  695. }
  696. TestsShell::ShutdownShell();
  697. }
  698. TEST_CASE("transition.display_and_visibility")
  699. {
  700. // Display and visibility properties have special behavior that make them visible throughout the interpolation duration.
  701. const String document_rml_template = R"(
  702. <rml>
  703. <head>
  704. <title>Test</title>
  705. <link type="text/rcss" href="/assets/rml.rcss"/>
  706. <style>
  707. body {
  708. inset: 0;
  709. }
  710. div {
  711. background-color: #c66;
  712. width: 300dp;
  713. height: 300dp;
  714. margin: auto;
  715. transition: opacity display visibility 1s;
  716. }
  717. .hide {
  718. %s;
  719. opacity: 0;
  720. }
  721. </style>
  722. </head>
  723. <body>
  724. <div class="hide" id="div"/>
  725. </body>
  726. </rml>
  727. )";
  728. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  729. Context* context = TestsShell::GetContext();
  730. String document_rml;
  731. SUBCASE("display: none")
  732. {
  733. document_rml = CreateString(document_rml_template.c_str(), "display: none");
  734. }
  735. SUBCASE("visibility")
  736. {
  737. document_rml = CreateString(document_rml_template.c_str(), "visibility: hidden");
  738. }
  739. const double dt = 1.0 / 60.0;
  740. const double t_delta = 0.1;
  741. const double t_fadein0 = 1;
  742. const double t_fadein1 = 2;
  743. const double t_fadeout0 = 4;
  744. const double t_fadeout1 = 5;
  745. enum class Action { None, Show, Hide };
  746. struct TestCase {
  747. double time;
  748. float opacity;
  749. bool is_visible;
  750. Action action = Action::None;
  751. };
  752. const std::vector<TestCase> tests = {
  753. {t_fadein0 - t_delta, 0.f, false},
  754. {t_fadein0, 0.f, false, Action::Show},
  755. {t_fadein0 + t_delta, 0.1f, true},
  756. {t_fadein1 - t_delta, 0.9f, true},
  757. {t_fadein1, 1.f, true},
  758. {t_fadein1 + t_delta, 1.f, true},
  759. {t_fadeout0 - t_delta, 1.f, true},
  760. {t_fadeout0, 1.f, true, Action::Hide},
  761. {t_fadeout0 + t_delta, 0.9f, true},
  762. {t_fadeout1 - t_delta, 0.1f, true},
  763. {t_fadeout1 + dt, 0.f, false}, // Due to floating-point precision, the animation may end slightly after the exact endtime.
  764. {t_fadeout1 + t_delta, 0.f, false},
  765. };
  766. {
  767. system_interface->SetManualTime(0.0);
  768. ElementDocument* document = context->LoadDocumentFromMemory(document_rml, "assets/");
  769. Element* element = document->GetChild(0);
  770. document->Show();
  771. double t = 0.f;
  772. for (const auto& test : tests)
  773. {
  774. while (t < test.time)
  775. {
  776. t = Math::Min(t + dt, test.time);
  777. system_interface->SetManualTime(t);
  778. TestsShell::RenderLoop(false);
  779. }
  780. if (test.action == Action::Show)
  781. element->SetClass("hide", false);
  782. else if (test.action == Action::Hide)
  783. element->SetClass("hide", true);
  784. context->Update();
  785. INFO("Time: ", test.time);
  786. CHECK(element->GetProperty<float>("opacity") == doctest::Approx(test.opacity));
  787. CHECK(element->IsVisible() == test.is_visible);
  788. }
  789. document->Close();
  790. }
  791. TestsShell::ShutdownShell();
  792. }