StyleAnimationTests.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using Avalonia.Animation;
  3. using Avalonia.Controls;
  4. using Avalonia.Data;
  5. using Avalonia.Styling;
  6. using Avalonia.UnitTests;
  7. using Xunit;
  8. namespace Avalonia.Base.UnitTests.Animation;
  9. using Animation = global::Avalonia.Animation.Animation;
  10. public class StyleAnimationTests
  11. {
  12. [Fact]
  13. public void Application_ControlTheme_Applies_Animation()
  14. {
  15. using var app = new AnimationTestApplication
  16. {
  17. Resources =
  18. {
  19. { typeof(Button), CreateControlTheme<Button>(CreateOpacityAnimation()) },
  20. },
  21. };
  22. var target = new Button();
  23. var root = CreateRoot(target, app);
  24. app.Clock.Pulse(TimeSpan.Zero);
  25. Assert.Equal(1, target.Opacity);
  26. app.Clock.Pulse(TimeSpan.FromSeconds(0.5));
  27. Assert.Equal(0.5, target.Opacity);
  28. app.Clock.Pulse(TimeSpan.FromSeconds(1));
  29. Assert.Equal(0, target.Opacity);
  30. }
  31. [Fact]
  32. public void Application_ControlTheme_Applies_Animation_With_Bound_KeyFrame_Values()
  33. {
  34. var fromBinding = new Binding("From");
  35. var toBinding = new Binding("To");
  36. using var app = new AnimationTestApplication
  37. {
  38. Resources =
  39. {
  40. { typeof(Button), CreateControlTheme<Button>(CreateOpacityAnimation(fromBinding, toBinding)) },
  41. },
  42. };
  43. var target = new Button { DataContext = new KeyFrameValues(1.0, 0.0) };
  44. var root = CreateRoot(target, app);
  45. app.Clock.Pulse(TimeSpan.Zero);
  46. Assert.Equal(1, target.Opacity);
  47. app.Clock.Pulse(TimeSpan.FromSeconds(0.5));
  48. Assert.Equal(0.5, target.Opacity);
  49. app.Clock.Pulse(TimeSpan.FromSeconds(1));
  50. Assert.Equal(0, target.Opacity);
  51. }
  52. private static ControlTheme CreateControlTheme<T>(Animation animation)
  53. {
  54. return new ControlTheme(typeof(T))
  55. {
  56. Animations = { animation }
  57. };
  58. }
  59. private static object CreateRoot(Button child, Application app)
  60. {
  61. var root = new TestRoot{ StylingParent = app };
  62. root.Child = child;
  63. root.LayoutManager.ExecuteInitialLayoutPass();
  64. return root;
  65. }
  66. private static Animation CreateOpacityAnimation()
  67. {
  68. return CreateOpacityAnimation(1.0, 0.0);
  69. }
  70. private static Animation CreateOpacityAnimation(object from, object to)
  71. {
  72. return new Animation
  73. {
  74. Duration = TimeSpan.FromSeconds(1),
  75. FillMode = FillMode.Both,
  76. Children =
  77. {
  78. new KeyFrame
  79. {
  80. KeyTime = TimeSpan.FromSeconds(0),
  81. Setters = { new Setter(Button.OpacityProperty, from) }
  82. },
  83. new KeyFrame
  84. {
  85. KeyTime = TimeSpan.FromSeconds(1),
  86. Setters = {new Setter(Button.OpacityProperty, to) }
  87. },
  88. }
  89. };
  90. }
  91. private class AnimationTestApplication : Application, IDisposable
  92. {
  93. private readonly IDisposable _lifetime;
  94. public AnimationTestApplication()
  95. {
  96. var services = new TestServices(globalClock: Clock);
  97. _lifetime = UnitTestApplication.Start(services);
  98. }
  99. public MockGlobalClock Clock { get; } = new MockGlobalClock();
  100. public void Dispose() => _lifetime.Dispose();
  101. }
  102. private record KeyFrameValues(double From, double To);
  103. }