TransitionsTests.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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),
  20. Property = Border.OpacityProperty,
  21. }
  22. }
  23. };
  24. border.Opacity = 0;
  25. clock.Pulse(TimeSpan.FromSeconds(0));
  26. clock.Pulse(TimeSpan.FromSeconds(-0.5));
  27. Assert.Equal(0, border.Opacity);
  28. }
  29. [Fact]
  30. public void Check_Transitions_Interpolation_Positive_Bounds_Clamp()
  31. {
  32. var clock = new TestClock();
  33. var border = new Border
  34. {
  35. Transitions = new Transitions
  36. {
  37. new DoubleTransition
  38. {
  39. Duration = TimeSpan.FromSeconds(1),
  40. Property = Border.OpacityProperty,
  41. }
  42. }
  43. };
  44. border.Opacity = 0;
  45. clock.Pulse(TimeSpan.FromSeconds(0));
  46. clock.Pulse(TimeSpan.FromMilliseconds(1001));
  47. Assert.Equal(0, border.Opacity);
  48. }
  49. [Fact]
  50. public void TransitionInstance_With_Zero_Duration_Is_Completed_On_First_Tick()
  51. {
  52. var clock = new TestClock();
  53. int i = 0;
  54. var inst = new TransitionInstance(clock, TimeSpan.Zero, TimeSpan.Zero).Subscribe(nextValue =>
  55. {
  56. switch (i++)
  57. {
  58. case 0: Assert.Equal(0, nextValue); break;
  59. case 1: Assert.Equal(1d, nextValue); break;
  60. }
  61. });
  62. clock.Pulse(TimeSpan.FromMilliseconds(10));
  63. }
  64. [Fact]
  65. public void TransitionInstance_Properly_Calculates_Delay_And_Duration_Values()
  66. {
  67. var clock = new TestClock();
  68. int i = -1;
  69. var inst = new TransitionInstance(clock, TimeSpan.FromMilliseconds(30), TimeSpan.FromMilliseconds(70)).Subscribe(nextValue =>
  70. {
  71. switch (i++)
  72. {
  73. case 0: Assert.Equal(0, nextValue); break;
  74. case 1: Assert.Equal(0, nextValue); break;
  75. case 2: Assert.Equal(0, nextValue); break;
  76. case 3: Assert.Equal(0, nextValue); break;
  77. case 4: Assert.Equal(Math.Round(10d / 70d, 4), Math.Round(nextValue, 4)); break;
  78. case 5: Assert.Equal(Math.Round(20d / 70d, 4), Math.Round(nextValue, 4)); break;
  79. case 6: Assert.Equal(Math.Round(30d / 70d, 4), Math.Round(nextValue, 4)); break;
  80. case 7: Assert.Equal(Math.Round(40d / 70d, 4), Math.Round(nextValue, 4)); break;
  81. case 8: Assert.Equal(Math.Round(50d / 70d, 4), Math.Round(nextValue, 4)); break;
  82. case 9: Assert.Equal(Math.Round(60d / 70d, 4), Math.Round(nextValue, 4)); break;
  83. case 10: Assert.Equal(1d, nextValue); break;
  84. }
  85. });
  86. for (int z = 0; z <= 10; z++)
  87. {
  88. clock.Pulse(TimeSpan.FromMilliseconds(10));
  89. }
  90. }
  91. }
  92. }