CompositionAnimationTests.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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
  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. [AnimationDataProvider]
  62. [Theory]
  63. public void GenericCheck(AnimationData data)
  64. {
  65. var compositor =
  66. new Compositor(new RenderLoop(new CompositorTestServices.ManualRenderTimer(), new Dispatcher(null)), null);
  67. var target = compositor.CreateSolidColorVisual();
  68. var ani = new ScalarKeyFrameAnimation(null);
  69. foreach (var frame in data.Frames)
  70. ani.InsertKeyFrame(frame.key, frame.value, new LinearEasing());
  71. ani.Duration = TimeSpan.FromSeconds(1);
  72. var instance = ani.CreateInstance(target.Server, null);
  73. instance.Initialize(TimeSpan.Zero, data.StartingValue, ServerCompositionVisual.s_IdOfRotationAngleProperty);
  74. var currentValue = ExpressionVariant.Create(data.StartingValue);
  75. foreach (var check in data.Checks)
  76. {
  77. currentValue = instance.Evaluate(TimeSpan.FromSeconds(check.time), currentValue);
  78. Assert.Equal(check.value, currentValue.Scalar);
  79. }
  80. }
  81. public class AnimationData
  82. {
  83. public AnimationData(string name)
  84. {
  85. Name = name;
  86. }
  87. public string Name { get; }
  88. public List<(float key, float value)> Frames { get; set; } = new();
  89. public List<(float time, float value)> Checks { get; set; } = new();
  90. public float StartingValue { get; set; }
  91. public float Duration { get; set; } = 1;
  92. public override string ToString()
  93. {
  94. return Name;
  95. }
  96. }
  97. }