BrushTransitionTests.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using Avalonia.Animation;
  3. using Avalonia.Controls;
  4. using Avalonia.Media;
  5. using Xunit;
  6. namespace Avalonia.Base.UnitTests.Animation
  7. {
  8. public class BrushTransitionTests
  9. {
  10. [Fact]
  11. public void SolidColorBrush_Opacity_IsInteroplated()
  12. {
  13. Test(0, new SolidColorBrush { Opacity = 0 }, new SolidColorBrush { Opacity = 0 });
  14. Test(0, new SolidColorBrush { Opacity = 0 }, new SolidColorBrush { Opacity = 1 });
  15. Test(0.5, new SolidColorBrush { Opacity = 0 }, new SolidColorBrush { Opacity = 1 });
  16. Test(0.5, new SolidColorBrush { Opacity = 0.5 }, new SolidColorBrush { Opacity = 0.5 });
  17. Test(1, new SolidColorBrush { Opacity = 1 }, new SolidColorBrush { Opacity = 1 });
  18. // TODO: investigate why this case fails.
  19. //Test2(1, new SolidColorBrush { Opacity = 0 }, new SolidColorBrush { Opacity = 1 });
  20. }
  21. [Fact]
  22. public void LinearGradientBrush_Opacity_IsInteroplated()
  23. {
  24. Test(0, new LinearGradientBrush { Opacity = 0 }, new LinearGradientBrush { Opacity = 0 });
  25. Test(0, new LinearGradientBrush { Opacity = 0 }, new LinearGradientBrush { Opacity = 1 });
  26. Test(0.5, new LinearGradientBrush { Opacity = 0 }, new LinearGradientBrush { Opacity = 1 });
  27. Test(0.5, new LinearGradientBrush { Opacity = 0.5 }, new LinearGradientBrush { Opacity = 0.5 });
  28. Test(1, new LinearGradientBrush { Opacity = 1 }, new LinearGradientBrush { Opacity = 1 });
  29. }
  30. private static void Test(double progress, IBrush oldBrush, IBrush newBrush)
  31. {
  32. var clock = new TestClock();
  33. var border = new Border() { Background = oldBrush };
  34. BrushTransition sut = new BrushTransition
  35. {
  36. Duration = TimeSpan.FromSeconds(1), Property = Border.BackgroundProperty
  37. };
  38. sut.Apply(border, clock, oldBrush, newBrush);
  39. clock.Pulse(TimeSpan.Zero);
  40. clock.Pulse(sut.Duration * progress);
  41. Assert.NotNull(border.Background);
  42. Assert.Equal(oldBrush.Opacity + (newBrush.Opacity - oldBrush.Opacity) * progress,
  43. border.Background.Opacity);
  44. }
  45. }
  46. }