GeometryTests.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using Avalonia.Media;
  3. using Avalonia.Platform;
  4. using Moq;
  5. using Xunit;
  6. namespace Avalonia.Base.UnitTests.Media
  7. {
  8. public class GeometryTests
  9. {
  10. [Fact]
  11. public void Changing_AffectsGeometry_Property_Causes_PlatformImpl_To_Be_Updated()
  12. {
  13. var target = new TestGeometry();
  14. var platformImpl = target.PlatformImpl;
  15. target.Foo = true;
  16. Assert.NotSame(platformImpl, target.PlatformImpl);
  17. }
  18. [Fact]
  19. public void Changing_AffectsGeometry_Property_Causes_Changed_To_Be_Raised()
  20. {
  21. var target = new TestGeometry();
  22. var raised = false;
  23. target.Changed += (s, e) => raised = true;
  24. target.Foo = true;
  25. Assert.True(raised);
  26. }
  27. [Fact]
  28. public void Setting_Transform_Causes_Changed_To_Be_Raised()
  29. {
  30. var target = new TestGeometry();
  31. var raised = false;
  32. target.Changed += (s, e) => raised = true;
  33. target.Transform = new RotateTransform(45);
  34. Assert.True(raised);
  35. }
  36. [Fact]
  37. public void Changing_Transform_Causes_Changed_To_Be_Raised()
  38. {
  39. var transform = new RotateTransform(45);
  40. var target = new TestGeometry { Transform = transform };
  41. var raised = false;
  42. target.Changed += (s, e) => raised = true;
  43. transform.Angle = 90;
  44. Assert.True(raised);
  45. }
  46. [Fact]
  47. public void Removing_Transform_Causes_Changed_To_Be_Raised()
  48. {
  49. var transform = new RotateTransform(45);
  50. var target = new TestGeometry { Transform = transform };
  51. var raised = false;
  52. target.Changed += (s, e) => raised = true;
  53. target.Transform = null;
  54. Assert.True(raised);
  55. }
  56. [Fact]
  57. public void Transform_Produces_Transformed_PlatformImpl()
  58. {
  59. var target = new TestGeometry();
  60. var rotate = new RotateTransform(45);
  61. Assert.False(target.PlatformImpl is ITransformedGeometryImpl);
  62. target.Transform = rotate;
  63. Assert.True(target.PlatformImpl is ITransformedGeometryImpl);
  64. rotate.Angle = 0;
  65. Assert.False(target.PlatformImpl is ITransformedGeometryImpl);
  66. }
  67. private class TestGeometry : Geometry
  68. {
  69. public static readonly StyledProperty<bool> FooProperty =
  70. AvaloniaProperty.Register<TestGeometry, bool>(nameof(Foo));
  71. static TestGeometry()
  72. {
  73. AffectsGeometry(FooProperty);
  74. }
  75. public bool Foo
  76. {
  77. get => GetValue(FooProperty);
  78. set => SetValue(FooProperty, value);
  79. }
  80. public override Geometry Clone()
  81. {
  82. throw new NotImplementedException();
  83. }
  84. private protected sealed override IGeometryImpl CreateDefiningGeometry()
  85. {
  86. return Mock.Of<IGeometryImpl>(
  87. x => x.WithTransform(It.IsAny<Matrix>()) ==
  88. Mock.Of<ITransformedGeometryImpl>(y =>
  89. y.SourceGeometry == x));
  90. }
  91. }
  92. }
  93. }