CompositionAnimationTests.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using Avalonia.Animation.Easings;
  6. using Avalonia.Base.UnitTests.Rendering;
  7. using Avalonia.Rendering;
  8. using Avalonia.Rendering.Composition;
  9. using Avalonia.Rendering.Composition.Expressions;
  10. using Avalonia.Rendering.Composition.Server;
  11. using Avalonia.Threading;
  12. using Avalonia.UnitTests;
  13. using Xunit;
  14. using Xunit.Sdk;
  15. namespace Avalonia.Base.UnitTests.Composition;
  16. public class CompositionAnimationTests : ScopedTestBase
  17. {
  18. class AnimationDataProvider : DataAttribute
  19. {
  20. IEnumerable<AnimationData> Generate() =>
  21. new AnimationData[]
  22. {
  23. new("3 frames starting from 0")
  24. {
  25. Frames =
  26. {
  27. (0f, 10f),
  28. (0.5f, 30f),
  29. (1f, 20f)
  30. },
  31. Checks =
  32. {
  33. (0.25f, 20f),
  34. (0.5f, 30f),
  35. (0.75f, 25f),
  36. (1f, 20f)
  37. }
  38. },
  39. new("1 final frame")
  40. {
  41. Frames =
  42. {
  43. (1f, 10f)
  44. },
  45. Checks =
  46. {
  47. (0f, 0f),
  48. (0.5f, 5f),
  49. (1f, 10f)
  50. }
  51. }
  52. };
  53. public override IEnumerable<object[]> GetData(MethodInfo testMethod)
  54. {
  55. foreach (var ani in Generate())
  56. {
  57. yield return new Object[] { ani };
  58. }
  59. }
  60. }
  61. class DummyDispatcher : IDispatcher
  62. {
  63. public bool CheckAccess() => true;
  64. public void VerifyAccess()
  65. {
  66. }
  67. public void Post(Action action, DispatcherPriority priority = default) => throw new NotSupportedException();
  68. }
  69. [AnimationDataProvider]
  70. [Theory]
  71. public void GenericCheck(AnimationData data)
  72. {
  73. using var scope = AvaloniaLocator.EnterScope();
  74. var compositor =
  75. new Compositor(new RenderLoop(new CompositorTestServices.ManualRenderTimer()), null);
  76. var target = compositor.CreateSolidColorVisual();
  77. var ani = new ScalarKeyFrameAnimation(null);
  78. foreach (var frame in data.Frames)
  79. ani.InsertKeyFrame(frame.key, frame.value, new LinearEasing());
  80. ani.Duration = TimeSpan.FromSeconds(1);
  81. var instance = ani.CreateInstance(target.Server, null);
  82. instance.Initialize(TimeSpan.Zero, data.StartingValue, ServerCompositionVisual.s_IdOfRotationAngleProperty);
  83. var currentValue = ExpressionVariant.Create(data.StartingValue);
  84. foreach (var check in data.Checks)
  85. {
  86. currentValue = instance.Evaluate(TimeSpan.FromSeconds(check.time), currentValue);
  87. Assert.Equal(check.value, currentValue.Scalar);
  88. }
  89. }
  90. public class AnimationData
  91. {
  92. public AnimationData(string name)
  93. {
  94. Name = name;
  95. }
  96. public string Name { get; }
  97. public List<(float key, float value)> Frames { get; set; } = new();
  98. public List<(float time, float value)> Checks { get; set; } = new();
  99. public float StartingValue { get; set; }
  100. public float Duration { get; set; } = 1;
  101. public override string ToString()
  102. {
  103. return Name;
  104. }
  105. }
  106. }