EffectTests.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using Avalonia.Media;
  3. using Xunit;
  4. namespace Avalonia.Base.UnitTests.Media;
  5. public class EffectTests
  6. {
  7. [Fact]
  8. public void Parse_Parses_Blur()
  9. {
  10. var effect = (ImmutableBlurEffect)Effect.Parse("blur(123.34)");
  11. Assert.Equal(123.34, effect.Radius);
  12. }
  13. private const uint Black = 0xff000000;
  14. [Theory,
  15. InlineData("drop-shadow(10 20)", 10, 20, 0, Black),
  16. InlineData("drop-shadow( 10 20 ) ", 10, 20, 0, Black),
  17. InlineData("drop-shadow( 10 20 30 ) ", 10, 20, 30, Black),
  18. InlineData("drop-shadow(10 20 30)", 10, 20, 30, Black),
  19. InlineData("drop-shadow(-10 -20 30)", -10, -20, 30, Black),
  20. InlineData("drop-shadow(10 20 30 #ffff00ff)", 10, 20, 30, 0xffff00ff),
  21. InlineData("drop-shadow ( 10 20 30 #ffff00ff ) ", 10, 20, 30, 0xffff00ff),
  22. InlineData("drop-shadow(10 20 30 red)", 10, 20, 30, 0xffff0000),
  23. InlineData("drop-shadow ( 10 20 30 red ) ", 10, 20, 30, 0xffff0000),
  24. InlineData("drop-shadow(10 20 30 rgba(100, 30, 45, 90%))", 10, 20, 30, 0xe6641e2d),
  25. InlineData("drop-shadow(10 20 30 rgba(100, 30, 45, 90%) ) ", 10, 20, 30, 0xe6641e2d)
  26. ]
  27. public void Parse_Parses_DropShadow(string s, double x, double y, double r, uint color)
  28. {
  29. var effect = (ImmutableDropShadowEffect)Effect.Parse(s);
  30. Assert.Equal(x, effect.OffsetX);
  31. Assert.Equal(y, effect.OffsetY);
  32. Assert.Equal(r, effect.BlurRadius);
  33. Assert.Equal(1, effect.Opacity);
  34. Assert.Equal(color, effect.Color.ToUInt32());
  35. }
  36. [Theory,
  37. InlineData("blur"),
  38. InlineData("blur("),
  39. InlineData("blur()"),
  40. InlineData("blur(123"),
  41. InlineData("blur(aaab)"),
  42. InlineData("drop-shadow(-10 -20 -30)")
  43. ]
  44. public void Invalid_Effect_Parse_Fails(string b)
  45. {
  46. Assert.Throws<ArgumentException>(() => Effect.Parse(b));
  47. }
  48. [Theory,
  49. InlineData("blur(2.5)", 4, 4, 4, 4),
  50. InlineData("blur(0)", 0, 0, 0, 0),
  51. InlineData("drop-shadow(10 15)", 0, 0, 10, 15),
  52. InlineData("drop-shadow(10 15 5)", 0, 0, 16, 21),
  53. InlineData("drop-shadow(0 0 5)", 6, 6, 6, 6),
  54. InlineData("drop-shadow(3 3 5)", 3, 3, 9, 9)
  55. ]
  56. public static void PaddingIsCorrectlyCalculated(string effect, double left, double top, double right, double bottom)
  57. {
  58. var padding = Effect.Parse(effect).GetEffectOutputPadding();
  59. Assert.Equal(left, padding.Left);
  60. Assert.Equal(top, padding.Top);
  61. Assert.Equal(right, padding.Right);
  62. Assert.Equal(bottom, padding.Bottom);
  63. }
  64. }