CompositionAnimationTests.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 Xunit;
  13. using Xunit.Sdk;
  14. namespace Avalonia.Base.UnitTests.Composition;
  15. public class CompositionAnimationTests
  16. {
  17. class AnimationDataProvider : DataAttribute
  18. {
  19. IEnumerable<AnimationData> Generate() =>
  20. new AnimationData[]
  21. {
  22. new("3 frames starting from 0")
  23. {
  24. Frames =
  25. {
  26. (0f, 10f),
  27. (0.5f, 30f),
  28. (1f, 20f)
  29. },
  30. Checks =
  31. {
  32. (0.25f, 20f),
  33. (0.5f, 30f),
  34. (0.75f, 25f),
  35. (1f, 20f)
  36. }
  37. },
  38. new("1 final frame")
  39. {
  40. Frames =
  41. {
  42. (1f, 10f)
  43. },
  44. Checks =
  45. {
  46. (0f, 0f),
  47. (0.5f, 5f),
  48. (1f, 10f)
  49. }
  50. }
  51. };
  52. public override IEnumerable<object[]> GetData(MethodInfo testMethod)
  53. {
  54. foreach (var ani in Generate())
  55. {
  56. yield return new Object[] { ani };
  57. }
  58. }
  59. }
  60. [AnimationDataProvider]
  61. [Theory]
  62. public void GenericCheck(AnimationData data)
  63. {
  64. var compositor =
  65. new Compositor(new RenderLoop(new CompositorTestsBase.ManualRenderTimer(), new Dispatcher(null)), null);
  66. var target = compositor.CreateSolidColorVisual();
  67. var ani = new ScalarKeyFrameAnimation(null);
  68. foreach (var frame in data.Frames)
  69. ani.InsertKeyFrame(frame.key, frame.value, new LinearEasing());
  70. ani.Duration = TimeSpan.FromSeconds(1);
  71. var instance = ani.CreateInstance(target.Server, null);
  72. instance.Initialize(TimeSpan.Zero, data.StartingValue, ServerCompositionVisual.s_IdOfRotationAngleProperty);
  73. var currentValue = ExpressionVariant.Create(data.StartingValue);
  74. foreach (var check in data.Checks)
  75. {
  76. currentValue = instance.Evaluate(TimeSpan.FromSeconds(check.time), currentValue);
  77. Assert.Equal(check.value, currentValue.Scalar);
  78. }
  79. }
  80. public class AnimationData
  81. {
  82. public AnimationData(string name)
  83. {
  84. Name = name;
  85. }
  86. public string Name { get; }
  87. public List<(float key, float value)> Frames { get; set; } = new();
  88. public List<(float time, float value)> Checks { get; set; } = new();
  89. public float StartingValue { get; set; }
  90. public float Duration { get; set; } = 1;
  91. public override string ToString()
  92. {
  93. return Name;
  94. }
  95. }
  96. }