1
0

TransitionsTests.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using Avalonia.Animation;
  3. using Avalonia.Controls;
  4. using Xunit;
  5. namespace Avalonia.Base.UnitTests.Animation
  6. {
  7. public class TransitionsTests
  8. {
  9. [Fact]
  10. public void Check_Transitions_Interpolation_Negative_Bounds_Clamp()
  11. {
  12. var clock = new TestClock();
  13. var border = new Border
  14. {
  15. Transitions = new Transitions
  16. {
  17. new DoubleTransition
  18. {
  19. Duration = TimeSpan.FromSeconds(1), Property = Visual.OpacityProperty,
  20. }
  21. }
  22. };
  23. border.Opacity = 0;
  24. clock.Pulse(TimeSpan.FromSeconds(0));
  25. clock.Pulse(TimeSpan.FromSeconds(-0.5));
  26. Assert.Equal(0, border.Opacity);
  27. }
  28. [Fact]
  29. public void Check_Transitions_Interpolation_Positive_Bounds_Clamp()
  30. {
  31. var clock = new TestClock();
  32. var border = new Border
  33. {
  34. Transitions = new Transitions
  35. {
  36. new DoubleTransition
  37. {
  38. Duration = TimeSpan.FromSeconds(1), Property = Visual.OpacityProperty,
  39. }
  40. }
  41. };
  42. border.Opacity = 0;
  43. clock.Pulse(TimeSpan.FromSeconds(0));
  44. clock.Pulse(TimeSpan.FromMilliseconds(1001));
  45. Assert.Equal(0, border.Opacity);
  46. }
  47. [Fact]
  48. public void TransitionInstance_With_Zero_Duration_Is_Completed_On_First_Tick()
  49. {
  50. var clock = new TestClock();
  51. var i = 0;
  52. new TransitionInstance(clock, TimeSpan.Zero, TimeSpan.Zero).Subscribe(nextValue =>
  53. {
  54. switch (i++)
  55. {
  56. case 0:
  57. Assert.Equal(0, nextValue);
  58. break;
  59. case 1:
  60. Assert.Equal(1d, nextValue);
  61. break;
  62. }
  63. });
  64. clock.Pulse(TimeSpan.FromMilliseconds(10));
  65. }
  66. [Fact]
  67. public void TransitionInstance_Properly_Calculates_Delay_And_Duration_Values()
  68. {
  69. var clock = new TestClock();
  70. var i = -1;
  71. var completed = false;
  72. new TransitionInstance(clock, TimeSpan.FromMilliseconds(30), TimeSpan.FromMilliseconds(70)).Subscribe(
  73. nextValue =>
  74. {
  75. switch (i++)
  76. {
  77. case 0:
  78. Assert.Equal(0, nextValue);
  79. break;
  80. case 1:
  81. Assert.Equal(0, nextValue);
  82. break;
  83. case 2:
  84. Assert.Equal(0, nextValue);
  85. break;
  86. case 3:
  87. Assert.Equal(0, nextValue);
  88. break;
  89. case 4:
  90. Assert.Equal(Math.Round(10d / 70d, 4), Math.Round(nextValue, 4));
  91. break;
  92. case 5:
  93. Assert.Equal(Math.Round(20d / 70d, 4), Math.Round(nextValue, 4));
  94. break;
  95. case 6:
  96. Assert.Equal(Math.Round(30d / 70d, 4), Math.Round(nextValue, 4));
  97. break;
  98. case 7:
  99. Assert.Equal(Math.Round(40d / 70d, 4), Math.Round(nextValue, 4));
  100. break;
  101. case 8:
  102. Assert.Equal(Math.Round(50d / 70d, 4), Math.Round(nextValue, 4));
  103. break;
  104. case 9:
  105. Assert.Equal(Math.Round(60d / 70d, 4), Math.Round(nextValue, 4));
  106. break;
  107. case 10:
  108. Assert.Equal(1d, nextValue);
  109. break;
  110. }
  111. }, () => completed = true);
  112. for (var z = 0; z <= 10; z++)
  113. {
  114. clock.Pulse(TimeSpan.FromMilliseconds(10));
  115. }
  116. Assert.True(completed);
  117. }
  118. [Fact]
  119. public void TransitionInstance_With_Delay_But_Zero_Duration_Is_Completed_After_Delay()
  120. {
  121. var clock = new TestClock();
  122. var i = -1;
  123. var completed = false;
  124. new TransitionInstance(clock, TimeSpan.FromMilliseconds(30), TimeSpan.Zero).Subscribe(
  125. nextValue =>
  126. {
  127. switch (i++)
  128. {
  129. case 0:
  130. Assert.Equal(0, nextValue);
  131. break;
  132. case 1:
  133. Assert.Equal(0, nextValue);
  134. break;
  135. case 2:
  136. Assert.Equal(0, nextValue);
  137. break;
  138. case 3: // one iteration sooner than the test above, because the start of the transition is also the end
  139. Assert.Equal(1, nextValue);
  140. break;
  141. }
  142. }, () => completed = true);
  143. for (var z = 0; z <= 4; z++)
  144. {
  145. clock.Pulse(TimeSpan.FromMilliseconds(10));
  146. }
  147. Assert.True(completed);
  148. }
  149. }
  150. }