AnimatableTests.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. using System;
  2. using Avalonia.Animation;
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.Shapes;
  5. using Avalonia.Data;
  6. using Avalonia.Layout;
  7. using Avalonia.Media;
  8. using Avalonia.Styling;
  9. using Avalonia.UnitTests;
  10. using Moq;
  11. using Xunit;
  12. namespace Avalonia.Base.UnitTests.Animation
  13. {
  14. public class AnimatableTests
  15. {
  16. [Fact]
  17. public void Transition_Is_Not_Applied_When_Not_Attached_To_Visual_Tree()
  18. {
  19. var target = CreateTarget();
  20. var control = new Control { Transitions = new Transitions { target.Object }, };
  21. control.Opacity = 0.5;
  22. target.Verify(x => x.Apply(
  23. control,
  24. It.IsAny<IClock>(),
  25. 1.0,
  26. 0.5),
  27. Times.Never);
  28. }
  29. [Fact]
  30. public void Transition_Is_Not_Applied_To_Initial_Style()
  31. {
  32. using (Start())
  33. {
  34. var target = CreateTarget();
  35. var control = new Control { Transitions = new Transitions { target.Object }, };
  36. var root = new TestRoot
  37. {
  38. Styles =
  39. {
  40. new Style(x => x.OfType<Control>())
  41. {
  42. Setters = { new Setter(Visual.OpacityProperty, 0.8), }
  43. }
  44. }
  45. };
  46. root.Child = control;
  47. Assert.Equal(0.8, control.Opacity);
  48. target.Verify(x => x.Apply(
  49. It.IsAny<Control>(),
  50. It.IsAny<IClock>(),
  51. It.IsAny<object>(),
  52. It.IsAny<object>()),
  53. Times.Never);
  54. }
  55. }
  56. [Fact]
  57. public void Transition_Is_Applied_When_Local_Value_Changes()
  58. {
  59. using var app = Start();
  60. var target = CreateTarget();
  61. var control = CreateControl(target.Object);
  62. control.Opacity = 0.5;
  63. target.Verify(x => x.Apply(
  64. control,
  65. It.IsAny<IClock>(),
  66. 1.0,
  67. 0.5));
  68. }
  69. [Fact]
  70. public void Transition_Is_Not_Applied_When_Animated_Value_Changes()
  71. {
  72. var target = CreateTarget();
  73. var control = CreateControl(target.Object);
  74. control.SetValue(Visual.OpacityProperty, 0.5, BindingPriority.Animation);
  75. target.Verify(x => x.Apply(
  76. control,
  77. It.IsAny<IClock>(),
  78. 1.0,
  79. 0.5),
  80. Times.Never);
  81. }
  82. [Theory]
  83. [InlineData(null)] //null value
  84. [InlineData("stringValue")] //string value
  85. public void Invalid_Values_In_Animation_Should_Not_Crash_Animations(object invalidValue)
  86. {
  87. var keyframe1 = new KeyFrame()
  88. {
  89. Setters = { new Setter(Layoutable.WidthProperty, 1d), }, KeyTime = TimeSpan.FromSeconds(0)
  90. };
  91. var keyframe2 = new KeyFrame()
  92. {
  93. Setters = { new Setter(Layoutable.WidthProperty, 2d), }, KeyTime = TimeSpan.FromSeconds(2),
  94. };
  95. var keyframe3 = new KeyFrame()
  96. {
  97. Setters = { new Setter(Layoutable.WidthProperty, invalidValue), },
  98. KeyTime = TimeSpan.FromSeconds(3),
  99. };
  100. var animation = new Avalonia.Animation.Animation()
  101. {
  102. Duration = TimeSpan.FromSeconds(3),
  103. Children = { keyframe1, keyframe2, keyframe3 },
  104. IterationCount = new IterationCount(5),
  105. PlaybackDirection = PlaybackDirection.Alternate,
  106. };
  107. var rect = new Rectangle() { Width = 11, };
  108. var clock = new TestClock();
  109. animation.RunAsync(rect, clock);
  110. clock.Step(TimeSpan.Zero);
  111. Assert.Equal(1, rect.Width);
  112. clock.Step(TimeSpan.FromSeconds(2));
  113. Assert.Equal(2, rect.Width);
  114. clock.Step(TimeSpan.FromSeconds(3));
  115. //here we have invalid value so value should be expected and set to initial original value
  116. Assert.Equal(11, rect.Width);
  117. }
  118. [Fact]
  119. public void Transition_Is_Not_Applied_When_StyleTrigger_Changes_With_LocalValue_Present()
  120. {
  121. using var app = Start();
  122. var target = CreateTarget();
  123. var control = CreateControl(target.Object);
  124. control.SetValue(Visual.OpacityProperty, 0.5);
  125. target.Verify(x => x.Apply(
  126. control,
  127. It.IsAny<IClock>(),
  128. 1.0,
  129. 0.5));
  130. target.Invocations.Clear();
  131. control.SetValue(Visual.OpacityProperty, 0.8, BindingPriority.StyleTrigger);
  132. target.Verify(x => x.Apply(
  133. It.IsAny<Control>(),
  134. It.IsAny<IClock>(),
  135. It.IsAny<object>(),
  136. It.IsAny<object>()),
  137. Times.Never);
  138. }
  139. [Fact]
  140. public void Transition_Is_Disposed_When_Local_Value_Changes()
  141. {
  142. using var app = Start();
  143. var target = CreateTarget();
  144. var control = CreateControl(target.Object);
  145. var sub = new Mock<IDisposable>();
  146. target.Setup(x => x.Apply(control, It.IsAny<IClock>(), 1.0, 0.5)).Returns(sub.Object);
  147. control.Opacity = 0.5;
  148. sub.Invocations.Clear();
  149. control.Opacity = 0.4;
  150. sub.Verify(x => x.Dispose());
  151. }
  152. [Fact]
  153. public void New_Transition_Is_Applied_When_Local_Value_Changes()
  154. {
  155. using var app = Start();
  156. var target = CreateTarget();
  157. var control = CreateControl(target.Object);
  158. target.Setup(x => x.Property).Returns(Visual.OpacityProperty);
  159. target.Setup(x => x.Apply(control, It.IsAny<IClock>(), 1.0, 0.5))
  160. .Callback(() =>
  161. {
  162. control.SetValue(Visual.OpacityProperty, 0.9, BindingPriority.Animation);
  163. })
  164. .Returns(Mock.Of<IDisposable>());
  165. control.Opacity = 0.5;
  166. Assert.Equal(0.9, control.Opacity);
  167. target.Invocations.Clear();
  168. control.Opacity = 0.4;
  169. target.Verify(x => x.Apply(
  170. control,
  171. It.IsAny<IClock>(),
  172. 0.9,
  173. 0.4));
  174. }
  175. [Fact]
  176. public void Transition_Is_Not_Applied_When_Removed_From_Visual_Tree()
  177. {
  178. using var app = Start();
  179. var target = CreateTarget();
  180. var control = CreateControl(target.Object);
  181. control.Opacity = 0.5;
  182. target.Verify(x => x.Apply(
  183. control,
  184. It.IsAny<IClock>(),
  185. 1.0,
  186. 0.5));
  187. target.Invocations.Clear();
  188. var root = (TestRoot)control.Parent;
  189. Assert.NotNull(root);
  190. root.Child = null;
  191. control.Opacity = 0.8;
  192. target.Verify(x => x.Apply(
  193. It.IsAny<Control>(),
  194. It.IsAny<IClock>(),
  195. It.IsAny<object>(),
  196. It.IsAny<object>()),
  197. Times.Never);
  198. }
  199. [Fact]
  200. public void Animation_Is_Cancelled_When_Transition_Removed()
  201. {
  202. using var app = Start();
  203. var target = CreateTarget();
  204. var control = CreateControl(target.Object);
  205. var sub = new Mock<IDisposable>();
  206. target.Setup(x => x.Apply(
  207. It.IsAny<Animatable>(),
  208. It.IsAny<IClock>(),
  209. It.IsAny<object>(),
  210. It.IsAny<object>())).Returns(sub.Object);
  211. control.Opacity = 0.5;
  212. Assert.NotNull(control.Transitions);
  213. control.Transitions.RemoveAt(0);
  214. sub.Verify(x => x.Dispose());
  215. }
  216. [Fact]
  217. public void Animation_Is_Cancelled_When_New_Style_Activates()
  218. {
  219. using (Start())
  220. {
  221. var target = CreateTarget();
  222. var control = CreateStyledControl(target.Object);
  223. var sub = new Mock<IDisposable>();
  224. target.Setup(x => x.Apply(
  225. control,
  226. It.IsAny<IClock>(),
  227. 1.0,
  228. 0.5)).Returns(sub.Object);
  229. control.Opacity = 0.5;
  230. target.Verify(x => x.Apply(
  231. control,
  232. It.IsAny<IClock>(),
  233. 1.0,
  234. 0.5),
  235. Times.Once);
  236. control.Classes.Add("foo");
  237. sub.Verify(x => x.Dispose());
  238. }
  239. }
  240. [Fact]
  241. public void Transition_From_Style_Trigger_Is_Applied()
  242. {
  243. using (Start())
  244. {
  245. var target = CreateTransition(Layoutable.WidthProperty);
  246. var control = CreateStyledControl(transition2: target.Object);
  247. control.Classes.Add("foo");
  248. control.Width = 100;
  249. target.Verify(x => x.Apply(
  250. control,
  251. It.IsAny<IClock>(),
  252. double.NaN,
  253. 100.0),
  254. Times.Once);
  255. }
  256. }
  257. [Fact]
  258. public void Replacing_Transitions_During_Animation_Does_Not_Throw_KeyNotFound()
  259. {
  260. // Issue #4059
  261. using (Start())
  262. {
  263. Border target;
  264. var clock = new TestClock();
  265. var root = new TestRoot
  266. {
  267. Clock = clock,
  268. Styles =
  269. {
  270. new Style(x => x.OfType<Border>())
  271. {
  272. Setters =
  273. {
  274. new Setter(Animatable.TransitionsProperty,
  275. new Transitions
  276. {
  277. new DoubleTransition
  278. {
  279. Property = Visual.OpacityProperty,
  280. Duration = TimeSpan.FromSeconds(1),
  281. },
  282. }),
  283. },
  284. },
  285. new Style(x => x.OfType<Border>().Class("foo"))
  286. {
  287. Setters =
  288. {
  289. new Setter(Animatable.TransitionsProperty,
  290. new Transitions
  291. {
  292. new DoubleTransition
  293. {
  294. Property = Visual.OpacityProperty,
  295. Duration = TimeSpan.FromSeconds(1),
  296. },
  297. }),
  298. new Setter(Visual.OpacityProperty, 0.0),
  299. },
  300. },
  301. },
  302. Child = target = new Border { Background = Brushes.Red, }
  303. };
  304. root.Measure(Size.Infinity);
  305. root.Arrange(new Rect(root.DesiredSize));
  306. target.Classes.Add("foo");
  307. clock.Step(TimeSpan.FromSeconds(0));
  308. clock.Step(TimeSpan.FromSeconds(0.5));
  309. Assert.Equal(0.5, target.Opacity);
  310. target.Classes.Remove("foo");
  311. }
  312. }
  313. [Fact]
  314. public void Transitions_Can_Be_Changed_To_Collection_That_Contains_The_Same_Transitions()
  315. {
  316. var target = CreateTarget();
  317. var control = CreateControl(target.Object);
  318. control.Transitions = new Transitions { target.Object };
  319. }
  320. [Fact]
  321. public void Transitions_Can_Re_Set_During_Styling()
  322. {
  323. var target = CreateTarget();
  324. var control = CreateControl(target.Object);
  325. // Assigning and then clearing Transitions ensures we have a transition state
  326. // collection created.
  327. control.ClearValue(Animatable.TransitionsProperty);
  328. control.GetValueStore().BeginStyling();
  329. // Setting opacity then Transitions means that we receive the Transitions change
  330. // after the Opacity change when EndStyling is called.
  331. var style = new Style
  332. {
  333. Setters =
  334. {
  335. new Setter(Visual.OpacityProperty, 0.5),
  336. new Setter(Animatable.TransitionsProperty, new Transitions { target.Object }),
  337. }
  338. };
  339. StyleHelpers.TryAttach(style, control);
  340. // Which means that the transition state hasn't been initialized with the new
  341. // Transitions when the Opacity change notification gets raised here.
  342. control.GetValueStore().EndStyling();
  343. }
  344. [Fact]
  345. public void Transitions_Can_Be_Removed_While_Transition_In_Progress()
  346. {
  347. using var app = Start();
  348. var opacityTransition = new DoubleTransition
  349. {
  350. Property = Visual.OpacityProperty, Duration = TimeSpan.FromSeconds(1),
  351. };
  352. var transitions = new Transitions { opacityTransition };
  353. var borderTheme = new ControlTheme(typeof(Border))
  354. {
  355. Setters = { new Setter(Animatable.TransitionsProperty, transitions), }
  356. };
  357. var clock = new TestClock();
  358. var root = new TestRoot { Clock = clock, Resources = { { typeof(Border), borderTheme }, } };
  359. var border = new Border();
  360. root.Child = border;
  361. root.LayoutManager.ExecuteInitialLayoutPass();
  362. Assert.Same(transitions, border.Transitions);
  363. // First set property with a transition to a new value, and step the clock until
  364. // transition is complete.
  365. border.Opacity = 0;
  366. clock.Step(TimeSpan.FromSeconds(0));
  367. clock.Step(TimeSpan.FromSeconds(1));
  368. Assert.Equal(0, border.Opacity);
  369. // Now clear the property; a transition is now in progress but no local value is
  370. // set.
  371. border.ClearValue(Visual.OpacityProperty);
  372. // Remove the transition by removing the control from the logical tree. This was
  373. // causing an exception.
  374. root.Child = null;
  375. }
  376. [Fact]
  377. public void Run_Normal_Use_Case_Animation()
  378. {
  379. using (Start())
  380. {
  381. var keyframe1 = new KeyFrame()
  382. {
  383. Setters = { new Setter(Visual.OpacityProperty, 1d), }, KeyTime = TimeSpan.FromSeconds(0)
  384. };
  385. var keyframe2 = new KeyFrame()
  386. {
  387. Setters = { new Setter(Visual.OpacityProperty, 0.5d), }, KeyTime = TimeSpan.FromSeconds(1)
  388. };
  389. var animation = new Avalonia.Animation.Animation()
  390. {
  391. Duration = TimeSpan.FromSeconds(10), Children = { keyframe1, keyframe2 },
  392. };
  393. Border target;
  394. var clock = new TestClock();
  395. var root = new TestRoot
  396. {
  397. Clock = clock,
  398. Styles = { new Style(x => x.OfType<Border>()) { Animations = { animation }, } },
  399. Child = target = new Border { Background = Brushes.Red, }
  400. };
  401. root.Measure(Size.Infinity);
  402. root.Arrange(new Rect(root.DesiredSize));
  403. clock.Step(TimeSpan.FromSeconds(0));
  404. clock.Step(TimeSpan.FromSeconds(0.99));
  405. Assert.InRange(target.Opacity, 0.5d, 0.51d);
  406. }
  407. }
  408. [Fact]
  409. public void Run_Normal_Use_Case_Animation_With_Infinite_Iteration()
  410. {
  411. using (Start())
  412. {
  413. var keyframe1 = new KeyFrame()
  414. {
  415. Setters = { new Setter(Visual.OpacityProperty, 0d), }, KeyTime = TimeSpan.FromSeconds(0)
  416. };
  417. var keyframe2 = new KeyFrame()
  418. {
  419. Setters = { new Setter(Visual.OpacityProperty, 1d), }, KeyTime = TimeSpan.FromSeconds(1)
  420. };
  421. var animation = new Avalonia.Animation.Animation()
  422. {
  423. Duration = TimeSpan.FromSeconds(1),
  424. IterationCount = IterationCount.Infinite,
  425. Children = { keyframe1, keyframe2 },
  426. };
  427. Border target;
  428. var clock = new TestClock();
  429. var root = new TestRoot
  430. {
  431. Clock = clock,
  432. Styles = { new Style(x => x.OfType<Border>()) { Animations = { animation }, } },
  433. Child = target = new Border { Background = Brushes.Red, }
  434. };
  435. root.Measure(Size.Infinity);
  436. root.Arrange(new Rect(root.DesiredSize));
  437. clock.Step(TimeSpan.FromSeconds(0));
  438. clock.Step(TimeSpan.FromSeconds(0.5));
  439. Assert.Equal(0.5, target.Opacity);
  440. clock.Step(TimeSpan.FromSeconds(1));
  441. Assert.Equal(0, target.Opacity);
  442. clock.Step(TimeSpan.FromSeconds(1.5));
  443. Assert.Equal(0.5, target.Opacity);
  444. clock.Step(TimeSpan.FromSeconds(2));
  445. Assert.Equal(0, target.Opacity);
  446. }
  447. }
  448. [Fact]
  449. public void Zero_Duration_Should_Finish_Animation()
  450. {
  451. using (Start())
  452. {
  453. var keyframe1 = new KeyFrame()
  454. {
  455. Setters = { new Setter(Visual.OpacityProperty, 1d), }, KeyTime = TimeSpan.FromSeconds(0)
  456. };
  457. var keyframe2 = new KeyFrame()
  458. {
  459. Setters = { new Setter(Visual.OpacityProperty, 0.5d), }, KeyTime = TimeSpan.FromSeconds(2)
  460. };
  461. var animation = new Avalonia.Animation.Animation()
  462. {
  463. Duration = TimeSpan.FromSeconds(2),
  464. Children = { keyframe1, keyframe2 },
  465. FillMode = FillMode.Both
  466. };
  467. Border target;
  468. var clock = new TestClock();
  469. var root = new TestRoot
  470. {
  471. Clock = clock,
  472. Styles = { new Style(x => x.OfType<Border>()) { Animations = { animation }, } },
  473. Child = target = new Border { Background = Brushes.Red, }
  474. };
  475. root.Measure(Size.Infinity);
  476. root.Arrange(new Rect(root.DesiredSize));
  477. clock.Step(TimeSpan.FromSeconds(0));
  478. clock.Step(TimeSpan.FromSeconds(1));
  479. Assert.True(target.IsAnimating(Visual.OpacityProperty));
  480. Assert.Equal(0.75, target.Opacity);
  481. // This is not the normal way to access and set the animations
  482. // object's Duration property to zero that is defined in styles
  483. // but this is still valid for the RunAsync version.
  484. animation.Duration = TimeSpan.Zero;
  485. clock.Step(TimeSpan.FromSeconds(1.2));
  486. Assert.Equal(0.5, target.Opacity);
  487. Assert.False(target.IsAnimating(Visual.OpacityProperty));
  488. }
  489. }
  490. [Fact]
  491. public void Zero_Duration_Should_Finish_Animation_With_Infinite_Iteration()
  492. {
  493. using (Start())
  494. {
  495. var keyframe1 = new KeyFrame()
  496. {
  497. Setters = { new Setter(Visual.OpacityProperty, 0d), }, KeyTime = TimeSpan.FromSeconds(0)
  498. };
  499. var keyframe2 = new KeyFrame()
  500. {
  501. Setters = { new Setter(Visual.OpacityProperty, 1d), }, KeyTime = TimeSpan.FromSeconds(1)
  502. };
  503. var animation = new Avalonia.Animation.Animation()
  504. {
  505. Duration = TimeSpan.FromSeconds(1),
  506. IterationCount = IterationCount.Infinite,
  507. Children = { keyframe1, keyframe2 },
  508. };
  509. Border target;
  510. var clock = new TestClock();
  511. var root = new TestRoot
  512. {
  513. Clock = clock,
  514. Styles = { new Style(x => x.OfType<Border>()) { Animations = { animation }, } },
  515. Child = target = new Border { Background = Brushes.Red, }
  516. };
  517. root.Measure(Size.Infinity);
  518. root.Arrange(new Rect(root.DesiredSize));
  519. clock.Step(TimeSpan.FromSeconds(0));
  520. Assert.True(target.IsAnimating(Visual.OpacityProperty));
  521. clock.Step(TimeSpan.FromSeconds(0.5));
  522. Assert.Equal(0.5, target.Opacity);
  523. clock.Step(TimeSpan.FromSeconds(1));
  524. Assert.Equal(0, target.Opacity);
  525. clock.Step(TimeSpan.FromSeconds(1.5));
  526. Assert.Equal(0.5, target.Opacity);
  527. clock.Step(TimeSpan.FromSeconds(2));
  528. Assert.Equal(0, target.Opacity);
  529. // This is not the normal way to access and set the animations
  530. // object's Duration property to zero that is defined in styles
  531. // but this is still valid for the RunAsync version.
  532. animation.Duration = TimeSpan.Zero;
  533. clock.Step(TimeSpan.FromSeconds(1.2));
  534. Assert.Equal(1, target.Opacity);
  535. Assert.False(target.IsAnimating(Visual.OpacityProperty));
  536. }
  537. }
  538. private static IDisposable Start()
  539. {
  540. var clock = new MockGlobalClock();
  541. var services = new TestServices(globalClock: clock);
  542. return UnitTestApplication.Start(services);
  543. }
  544. private static Mock<ITransition> CreateTarget()
  545. {
  546. return CreateTransition(Visual.OpacityProperty);
  547. }
  548. private static Control CreateControl(ITransition transition)
  549. {
  550. var control = new Control { Transitions = new Transitions { transition }, };
  551. var _ = new TestRoot(control);
  552. return control;
  553. }
  554. private static Control CreateStyledControl(
  555. ITransition transition1 = null,
  556. ITransition transition2 = null)
  557. {
  558. transition1 = transition1 ?? CreateTarget().Object;
  559. transition2 = transition2 ?? CreateTransition(Layoutable.WidthProperty).Object;
  560. var control = new Control
  561. {
  562. Styles =
  563. {
  564. new Style(x => x.OfType<Control>())
  565. {
  566. Setters =
  567. {
  568. new Setter
  569. {
  570. Property = Animatable.TransitionsProperty,
  571. Value = new Transitions { transition1 },
  572. }
  573. }
  574. },
  575. new Style(x => x.OfType<Control>().Class("foo"))
  576. {
  577. Setters =
  578. {
  579. new Setter
  580. {
  581. Property = Animatable.TransitionsProperty,
  582. Value = new Transitions { transition2 },
  583. }
  584. }
  585. }
  586. }
  587. };
  588. var _ = new TestRoot(control);
  589. return control;
  590. }
  591. private static Mock<ITransition> CreateTransition(AvaloniaProperty property)
  592. {
  593. var target = new Mock<ITransition>();
  594. var sub = new Mock<IDisposable>();
  595. target.Setup(x => x.Property).Returns(property);
  596. target.Setup(x => x.Apply(
  597. It.IsAny<Animatable>(),
  598. It.IsAny<IClock>(),
  599. It.IsAny<object>(),
  600. It.IsAny<object>())).Returns(sub.Object);
  601. return target;
  602. }
  603. }
  604. }