AnimationIterationTests.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using Avalonia.Animation;
  6. using Avalonia.Controls;
  7. using Avalonia.Styling;
  8. using Avalonia.UnitTests;
  9. using Avalonia.Data;
  10. using Xunit;
  11. using Avalonia.Animation.Easings;
  12. namespace Avalonia.Animation.UnitTests
  13. {
  14. public class AnimationIterationTests
  15. {
  16. [Fact]
  17. public void Check_KeyTime_Correctly_Converted_To_Cue()
  18. {
  19. var keyframe1 = new KeyFrame()
  20. {
  21. Setters =
  22. {
  23. new Setter(Border.WidthProperty, 100d),
  24. },
  25. KeyTime = TimeSpan.FromSeconds(0.5)
  26. };
  27. var keyframe2 = new KeyFrame()
  28. {
  29. Setters =
  30. {
  31. new Setter(Border.WidthProperty, 0d),
  32. },
  33. KeyTime = TimeSpan.FromSeconds(0)
  34. };
  35. var animation = new Animation()
  36. {
  37. Duration = TimeSpan.FromSeconds(1),
  38. Children =
  39. {
  40. keyframe2,
  41. keyframe1
  42. }
  43. };
  44. var border = new Border()
  45. {
  46. Height = 100d,
  47. Width = 100d
  48. };
  49. var clock = new TestClock();
  50. var animationRun = animation.RunAsync(border, clock);
  51. clock.Step(TimeSpan.Zero);
  52. Assert.Equal(border.Width, 0d);
  53. clock.Step(TimeSpan.FromSeconds(1));
  54. Assert.Equal(border.Width, 100d);
  55. }
  56. [Fact]
  57. public void Check_Initial_Inter_and_Trailing_Delay_Values()
  58. {
  59. var keyframe1 = new KeyFrame()
  60. {
  61. Setters =
  62. {
  63. new Setter(Border.WidthProperty, 200d),
  64. },
  65. Cue = new Cue(1d)
  66. };
  67. var keyframe2 = new KeyFrame()
  68. {
  69. Setters =
  70. {
  71. new Setter(Border.WidthProperty, 100d),
  72. },
  73. Cue = new Cue(0d)
  74. };
  75. var animation = new Animation()
  76. {
  77. Duration = TimeSpan.FromSeconds(3),
  78. Delay = TimeSpan.FromSeconds(3),
  79. DelayBetweenIterations = TimeSpan.FromSeconds(3),
  80. IterationCount = new IterationCount(2),
  81. Children =
  82. {
  83. keyframe2,
  84. keyframe1
  85. }
  86. };
  87. var border = new Border()
  88. {
  89. Height = 100d,
  90. Width = 100d
  91. };
  92. var clock = new TestClock();
  93. var animationRun = animation.RunAsync(border, clock);
  94. clock.Step(TimeSpan.Zero);
  95. // Initial Delay.
  96. clock.Step(TimeSpan.FromSeconds(1));
  97. Assert.Equal(border.Width, 0d);
  98. clock.Step(TimeSpan.FromSeconds(6));
  99. // First Inter-Iteration delay.
  100. clock.Step(TimeSpan.FromSeconds(8));
  101. Assert.Equal(border.Width, 200d);
  102. // Trailing Delay should be non-existent.
  103. clock.Step(TimeSpan.FromSeconds(14));
  104. Assert.True(animationRun.Status == TaskStatus.RanToCompletion);
  105. Assert.Equal(border.Width, 100d);
  106. }
  107. [Fact]
  108. public void Check_FillModes_Start_and_End_Values_if_Retained()
  109. {
  110. var keyframe1 = new KeyFrame()
  111. {
  112. Setters =
  113. {
  114. new Setter(Border.WidthProperty, 0d),
  115. },
  116. Cue = new Cue(0.0d)
  117. };
  118. var keyframe2 = new KeyFrame()
  119. {
  120. Setters =
  121. {
  122. new Setter(Border.WidthProperty, 300d),
  123. },
  124. Cue = new Cue(1.0d)
  125. };
  126. var animation = new Animation()
  127. {
  128. Duration = TimeSpan.FromSeconds(0.05d),
  129. Delay = TimeSpan.FromSeconds(0.05d),
  130. Easing = new SineEaseInOut(),
  131. FillMode = FillMode.Both,
  132. Children =
  133. {
  134. keyframe1,
  135. keyframe2
  136. }
  137. };
  138. var border = new Border()
  139. {
  140. Height = 100d,
  141. Width = 100d,
  142. };
  143. var clock = new TestClock();
  144. var animationRun = animation.RunAsync(border, clock);
  145. clock.Step(TimeSpan.FromSeconds(0d));
  146. Assert.Equal(border.Width, 0d);
  147. clock.Step(TimeSpan.FromSeconds(0.050d));
  148. Assert.Equal(border.Width, 0d);
  149. clock.Step(TimeSpan.FromSeconds(0.100d));
  150. Assert.Equal(border.Width, 300d);
  151. }
  152. }
  153. }