Animation.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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->SetTime(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->SetTime(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. system_interface->SetTime(0.0);
  249. TestsShell::ShutdownShell();
  250. }
  251. static const String document_filter_rml = R"(
  252. <rml>
  253. <head>
  254. <title>Test</title>
  255. <link type="text/rcss" href="/assets/rml.rcss"/>
  256. <style>
  257. body {
  258. left: 0;
  259. top: 0;
  260. right: 0;
  261. bottom: 0;
  262. }
  263. @keyframes mix {
  264. from { %s: %s; }
  265. to { %s: %s; }
  266. }
  267. div {
  268. background: #333;
  269. height: 64px;
  270. width: 64px;
  271. decorator: image(high_scores_alien_1.tga);
  272. animation: mix 0.1s;
  273. }
  274. </style>
  275. </head>
  276. <body>
  277. <div/>
  278. </body>
  279. </rml>
  280. )";
  281. TEST_CASE("animation.filter")
  282. {
  283. struct Test {
  284. String from;
  285. String to;
  286. String expected_25p; // expected interpolated value at 25% progression
  287. };
  288. Vector<Test> tests{
  289. {
  290. "blur( 0px)",
  291. "blur(40px)",
  292. "blur(10px)",
  293. },
  294. {
  295. "blur(10px)",
  296. "blur(25dp)", // assumes dp-ratio == 2
  297. "blur(20px)",
  298. },
  299. {
  300. "blur(40px)",
  301. "none",
  302. "blur(30px)",
  303. },
  304. {
  305. "none",
  306. "blur(40px)",
  307. "blur(10px)",
  308. },
  309. {
  310. "drop-shadow(#000 30px 20px 0px)",
  311. "drop-shadow(#f00 30px 20px 4px)", // colors interpolated in linear space
  312. "drop-shadow(#7f0000 30px 20px 1px)",
  313. },
  314. {
  315. "opacity(0) brightness(2)",
  316. "none",
  317. "opacity(0.25) brightness(1.75)",
  318. },
  319. {
  320. "opacity(0) brightness(0)",
  321. "opacity(0.5)",
  322. "opacity(0.125) brightness(0.25)",
  323. },
  324. {
  325. "opacity(0.5)",
  326. "opacity(0) brightness(0)",
  327. "opacity(0.375) brightness(0.75)",
  328. },
  329. {
  330. "opacity(0) brightness(0)",
  331. "brightness(1) opacity(0.5)", // discrete interpolation due to non-matching types
  332. "opacity(0) brightness(0)",
  333. },
  334. {
  335. "none", // Test initial values of various filters.
  336. "brightness(2.00) contrast(2.00) grayscale(1.00) hue-rotate(4rad) invert(1.00) opacity(0.00) sepia(1.00) saturate(2.00)",
  337. "brightness(1.25) contrast(1.25) grayscale(0.25) hue-rotate(1rad) invert(0.25) opacity(0.75) sepia(0.25) saturate(1.25)",
  338. },
  339. };
  340. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  341. Context* context = TestsShell::GetContext();
  342. context->SetDensityIndependentPixelRatio(2.0f);
  343. for (const char* property_str : {"filter", "backdrop-filter"})
  344. {
  345. for (const Test& test : tests)
  346. {
  347. const double t_final = 0.1;
  348. system_interface->SetTime(0.0);
  349. String document_rml = Rml::CreateString(document_filter_rml.c_str(), property_str, test.from.c_str(), property_str, test.to.c_str());
  350. ElementDocument* document = context->LoadDocumentFromMemory(document_rml, "assets/");
  351. Element* element = document->GetChild(0);
  352. document->Show();
  353. system_interface->SetTime(0.25 * t_final);
  354. TestsShell::RenderLoop();
  355. CHECK_MESSAGE(element->GetProperty<String>(property_str) == test.expected_25p, property_str, " from: ", test.from, ", to: ", test.to);
  356. document->Close();
  357. }
  358. }
  359. system_interface->SetTime(0.0);
  360. TestsShell::ShutdownShell();
  361. }
  362. TEST_CASE("animation.case_sensitivity")
  363. {
  364. static const String document_rml_template = R"(
  365. <rml>
  366. <head>
  367. <title>Test</title>
  368. <link type="text/rcss" href="/assets/rml.rcss"/>
  369. <style>
  370. body {
  371. left: 0;
  372. top: 0;
  373. right: 0;
  374. bottom: 0;
  375. }
  376. @keyframes %s {
  377. to { visibility: visible; }
  378. }
  379. div {
  380. width: 300dp;
  381. height: 300dp;
  382. visibility: hidden;
  383. background-color: #666;
  384. animation: 1.5s %s;
  385. }
  386. </style>
  387. </head>
  388. <body>
  389. <div/>
  390. </body>
  391. </rml>
  392. )";
  393. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  394. Context* context = TestsShell::GetContext();
  395. struct TestCase {
  396. String keyframe_name;
  397. String animation_name;
  398. bool expected_visible_end;
  399. };
  400. const Vector<TestCase> test_cases = {
  401. // TODO: Make name-lookups case sensitive:
  402. {"mix", "Mix", false},
  403. {"Mix", "mix", false},
  404. {"show", "show", true},
  405. {"Show", "Show", true},
  406. };
  407. for (const auto& test_case : test_cases)
  408. {
  409. system_interface->SetTime(0.0);
  410. INFO("@keyframes: ", test_case.keyframe_name);
  411. INFO("animation: ", test_case.animation_name);
  412. const String document_rml = CreateString(document_rml_template.c_str(), test_case.keyframe_name.c_str(), test_case.animation_name.c_str());
  413. ElementDocument* document = context->LoadDocumentFromMemory(document_rml, "assets/");
  414. Element* element = document->GetChild(0);
  415. document->Show();
  416. REQUIRE(element->IsVisible() == false);
  417. constexpr double t_end = 1.0;
  418. constexpr double dt = 0.1;
  419. double t = 0;
  420. while (t < t_end)
  421. {
  422. t += dt;
  423. system_interface->SetTime(t);
  424. context->Update();
  425. }
  426. CHECK(element->IsVisible() == test_case.expected_visible_end);
  427. document->Close();
  428. }
  429. system_interface->SetTime(0.0);
  430. TestsShell::ShutdownShell();
  431. }
  432. TEST_CASE("animation.case_sensitive_distinct_keyframes")
  433. {
  434. static const String document_rml = R"(
  435. <rml>
  436. <head>
  437. <title>Test</title>
  438. <link type="text/rcss" href="/assets/rml.rcss"/>
  439. <style>
  440. body {
  441. left: 0;
  442. top: 0;
  443. right: 0;
  444. bottom: 0;
  445. }
  446. @keyframes show {
  447. 50%, 100% { opacity: 0.25; }
  448. }
  449. @keyframes Show {
  450. 50%, 100% { opacity: 0.5; }
  451. }
  452. @keyframes SHOW {
  453. 0%, 25% { opacity: 0.75; }
  454. 75%, 100% { opacity: 1.0; }
  455. }
  456. div {
  457. width: 300dp;
  458. height: 300dp;
  459. opacity: 0.0;
  460. background-color: #666;
  461. }
  462. .show-lower {
  463. animation: 2s show;
  464. }
  465. .show-upper {
  466. animation: 2s Show;
  467. }
  468. .show-infinite {
  469. animation: 0.5s infinite alternate SHOW;
  470. }
  471. .show-infinite-upper-keywords {
  472. animation: 0.5s INFINITE ALTERNATE SHOW;
  473. }
  474. </style>
  475. </head>
  476. <body>
  477. <div/>
  478. </body>
  479. </rml>
  480. )";
  481. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  482. Context* context = TestsShell::GetContext();
  483. struct TestCase {
  484. String class_name;
  485. float expected_opacity_end;
  486. };
  487. const Vector<TestCase> test_cases = {
  488. {"show-lower", 0.25f},
  489. {"show-upper", 0.5f},
  490. {"show-infinite", 0.75f},
  491. {"show-infinite-upper-keywords", 0.75f},
  492. };
  493. for (const auto& test_case : test_cases)
  494. {
  495. INFO("Class name: ", test_case.class_name);
  496. system_interface->SetTime(0.0);
  497. ElementDocument* document = context->LoadDocumentFromMemory(document_rml, "assets/");
  498. Element* element = document->GetChild(0);
  499. element->SetClass(test_case.class_name, true);
  500. document->Show();
  501. REQUIRE(element->GetProperty<float>("opacity") == 0.f);
  502. constexpr double t_end = 1.0;
  503. constexpr double dt = 0.1;
  504. double t = 0;
  505. while (t < t_end)
  506. {
  507. t += dt;
  508. system_interface->SetTime(t);
  509. context->Update();
  510. }
  511. CHECK(element->GetProperty<float>("opacity") == test_case.expected_opacity_end);
  512. document->Close();
  513. }
  514. system_interface->SetTime(0.0);
  515. TestsShell::ShutdownShell();
  516. }
  517. static const String document_multiple_values_rml = R"(
  518. <rml>
  519. <head>
  520. <title>Test</title>
  521. <link type="text/rcss" href="/assets/rml.rcss"/>
  522. <style>
  523. body {
  524. left: 0;
  525. top: 0;
  526. right: 0;
  527. bottom: 0;
  528. }
  529. @keyframes fade {
  530. from { opacity: 0; }
  531. to { opacity: 1; }
  532. }
  533. @keyframes colorize {
  534. from { background-color: #f00; }
  535. 25% { background-color: #0f0; }
  536. to { background-color: #00f; }
  537. }
  538. div {
  539. width: 300dp;
  540. height: 300dp;
  541. background-color: #000;
  542. animation: 2s fade, 4s 1s colorize;
  543. }
  544. </style>
  545. </head>
  546. <body>
  547. <div/>
  548. </body>
  549. </rml>
  550. )";
  551. TEST_CASE("animation.multiple_values")
  552. {
  553. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  554. Context* context = TestsShell::GetContext();
  555. const double dt = 0.1;
  556. const double t_fade_start = 0;
  557. const double t_colorize_start = 1;
  558. const double t_fade_final = 2;
  559. const double t_colorize_final = 5;
  560. struct TestCase {
  561. double time;
  562. float opacity_min;
  563. Colourb background_color;
  564. };
  565. const std::vector<TestCase> tests = {
  566. {t_fade_start, 0.f, {0, 0, 0}},
  567. {t_fade_start + dt, 0.01f, {0, 0, 0}},
  568. {t_colorize_start - dt, 0.4f, {0, 0, 0}},
  569. {t_colorize_start, 0.49f, {0, 0, 0}},
  570. {t_colorize_start + dt, 0.5f, {0xf1, 0x50, 0}},
  571. {t_fade_final - dt, 0.9f, {0x50, 0xf1, 0}},
  572. {t_fade_final, 0.99f, {0, 0xfe, 0x00}},
  573. {t_fade_final + dt, 1.0f, {0, 0xfa, 0x2e}},
  574. {t_colorize_final - dt, 1.0f, {0, 0x2e, 0xfa}},
  575. {t_colorize_final, 1.0f, {0, 0, 0xfe}},
  576. {t_colorize_final + dt, 1.0f, {0, 0, 0}},
  577. };
  578. {
  579. system_interface->SetTime(0.0);
  580. ElementDocument* document = context->LoadDocumentFromMemory(document_multiple_values_rml, "assets/");
  581. Element* element = document->GetChild(0);
  582. document->Show();
  583. double t = 0.f;
  584. for (const auto& test : tests)
  585. {
  586. // Simulate progression in small time steps since animations are constrained to 0.1s maximum step size.
  587. while (t < test.time)
  588. {
  589. t = Math::Min(t + dt, test.time);
  590. system_interface->SetTime(t);
  591. context->Update();
  592. }
  593. INFO("Time: ", test.time);
  594. CHECK(element->GetProperty<float>("opacity") >= test.opacity_min);
  595. CHECK(element->GetProperty<Colourb>("background-color") == test.background_color);
  596. }
  597. document->Close();
  598. }
  599. system_interface->SetTime(0.0);
  600. TestsShell::ShutdownShell();
  601. }
  602. static const String document_animation_multiple_values_rml = R"(
  603. <rml>
  604. <head>
  605. <title>Test</title>
  606. <link type="text/rcss" href="/assets/rml.rcss"/>
  607. <style>
  608. body {
  609. left: 0;
  610. top: 0;
  611. right: 0;
  612. bottom: 0;
  613. }
  614. @keyframes fadein {
  615. from {
  616. opacity: 0;
  617. visibility: hidden;
  618. }
  619. to {
  620. opacity: 1;
  621. visibility: visible;
  622. }
  623. }
  624. @keyframes fadeout {
  625. from {
  626. opacity: 1;
  627. visibility: visible;
  628. }
  629. to {
  630. opacity: 0;
  631. visibility: hidden;
  632. }
  633. }
  634. div {
  635. background-color: #c66;
  636. width: 300dp;
  637. height: 300dp;
  638. margin: auto;
  639. animation: 1s fadein, 1s 5s fadeout;
  640. }
  641. </style>
  642. </head>
  643. <body>
  644. <div id="div"/>
  645. </body>
  646. </rml>
  647. )";
  648. TEST_CASE("animation.multiple_overlapping")
  649. {
  650. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  651. Context* context = TestsShell::GetContext();
  652. const float greater_than_zero = FLT_MIN;
  653. const double dt = 1.0 / 60.0;
  654. const double t0 = 0;
  655. const double t_delta = 0.1;
  656. const double t_fadein_final = 1;
  657. const double t_fadeout_start = 5;
  658. const double t_fadeout_final = 6;
  659. struct TestCase {
  660. double time;
  661. float opacity_min;
  662. bool is_visible;
  663. };
  664. const std::vector<TestCase> tests = {
  665. {t0, 0.f, true},
  666. {t0 + t_delta, greater_than_zero, true},
  667. {t_fadein_final - t_delta, 0.8f, true},
  668. {t_fadein_final, 1.f, true},
  669. {t_fadein_final + t_delta, 1.f, true},
  670. {t_fadeout_start - t_delta, 1.f, true},
  671. {t_fadeout_start, 1.f, true},
  672. {t_fadeout_start + t_delta, 0.8f, true},
  673. {t_fadeout_final - t_delta, greater_than_zero, true},
  674. {t_fadeout_final, 0.f, false},
  675. {t_fadeout_final + t_delta, 0.f, false},
  676. };
  677. // Currently we don't support animating the same property from multiple animations on the same element, see #608.
  678. // This test only checks that we submit warnings to the user. Once we support this feature, we can enable more checks in this test.
  679. {
  680. system_interface->SetNumExpectedWarnings(2);
  681. system_interface->SetTime(0.0);
  682. ElementDocument* document = context->LoadDocumentFromMemory(document_animation_multiple_values_rml, "assets/");
  683. Element* element = document->GetChild(0);
  684. document->Show();
  685. TestsShell::RenderLoop();
  686. double t = 0.f;
  687. for (const auto& test : tests)
  688. {
  689. while (t < test.time)
  690. {
  691. t = Math::Min(t + dt, test.time);
  692. system_interface->SetTime(t);
  693. context->Update();
  694. }
  695. INFO("Time: ", test.time);
  696. (void)(element->GetProperty<float>("opacity") == test.opacity_min);
  697. (void)(element->IsVisible() == test.is_visible);
  698. }
  699. document->Close();
  700. }
  701. system_interface->SetTime(0.0);
  702. TestsShell::ShutdownShell();
  703. }
  704. TEST_CASE("transition.display_and_visibility")
  705. {
  706. // Display and visibility properties have special behavior that make them visible throughout the interpolation duration.
  707. const String document_rml_template = R"(
  708. <rml>
  709. <head>
  710. <title>Test</title>
  711. <link type="text/rcss" href="/assets/rml.rcss"/>
  712. <style>
  713. body {
  714. inset: 0;
  715. }
  716. div {
  717. background-color: #c66;
  718. width: 300dp;
  719. height: 300dp;
  720. margin: auto;
  721. transition: opacity display visibility 1s;
  722. }
  723. .hide {
  724. %s;
  725. opacity: 0;
  726. }
  727. </style>
  728. </head>
  729. <body>
  730. <div class="hide" id="div"/>
  731. </body>
  732. </rml>
  733. )";
  734. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  735. Context* context = TestsShell::GetContext();
  736. String document_rml;
  737. SUBCASE("display: none")
  738. {
  739. document_rml = CreateString(document_rml_template.c_str(), "display: none");
  740. }
  741. SUBCASE("visibility")
  742. {
  743. document_rml = CreateString(document_rml_template.c_str(), "visibility: hidden");
  744. }
  745. const double dt = 1.0 / 60.0;
  746. const double t_delta = 0.1;
  747. const double t_fadein0 = 1;
  748. const double t_fadein1 = 2;
  749. const double t_fadeout0 = 4;
  750. const double t_fadeout1 = 5;
  751. enum class Action { None, Show, Hide };
  752. struct TestCase {
  753. double time;
  754. float opacity;
  755. bool is_visible;
  756. Action action = Action::None;
  757. };
  758. const std::vector<TestCase> tests = {
  759. {t_fadein0 - t_delta, 0.f, false},
  760. {t_fadein0, 0.f, false, Action::Show},
  761. {t_fadein0 + t_delta, 0.1f, true},
  762. {t_fadein1 - t_delta, 0.9f, true},
  763. {t_fadein1, 1.f, true},
  764. {t_fadein1 + t_delta, 1.f, true},
  765. {t_fadeout0 - t_delta, 1.f, true},
  766. {t_fadeout0, 1.f, true, Action::Hide},
  767. {t_fadeout0 + t_delta, 0.9f, true},
  768. {t_fadeout1 - t_delta, 0.1f, true},
  769. {t_fadeout1 + dt, 0.f, false}, // Due to floating-point precision, the animation may end slightly after the exact endtime.
  770. {t_fadeout1 + t_delta, 0.f, false},
  771. };
  772. {
  773. system_interface->SetTime(0.0);
  774. ElementDocument* document = context->LoadDocumentFromMemory(document_rml, "assets/");
  775. Element* element = document->GetChild(0);
  776. document->Show();
  777. double t = 0.f;
  778. for (const auto& test : tests)
  779. {
  780. while (t < test.time)
  781. {
  782. t = Math::Min(t + dt, test.time);
  783. system_interface->SetTime(t);
  784. TestsShell::RenderLoop(false);
  785. }
  786. if (test.action == Action::Show)
  787. element->SetClass("hide", false);
  788. else if (test.action == Action::Hide)
  789. element->SetClass("hide", true);
  790. context->Update();
  791. INFO("Time: ", test.time);
  792. CHECK(element->GetProperty<float>("opacity") == doctest::Approx(test.opacity));
  793. CHECK(element->IsVisible() == test.is_visible);
  794. }
  795. document->Close();
  796. }
  797. system_interface->SetTime(0.0);
  798. TestsShell::ShutdownShell();
  799. }