BrushTransitionTests.cs 2.3 KB

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