DrawOperationTests.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using Avalonia.Media;
  2. using Avalonia.Platform;
  3. using Avalonia.Rendering.SceneGraph;
  4. using Avalonia.Utilities;
  5. using Avalonia.Media.Imaging;
  6. using Moq;
  7. using Xunit;
  8. namespace Avalonia.Base.UnitTests.Rendering.SceneGraph
  9. {
  10. public class DrawOperationTests
  11. {
  12. [Fact]
  13. public void Empty_Bounds_Remain_Empty()
  14. {
  15. var target = new TestDrawOperation(default, Matrix.Identity, null);
  16. Assert.Equal(default, target.Bounds);
  17. }
  18. [Theory]
  19. [InlineData(10, 10, 10, 10, 1, 1, 1, 9, 9, 12, 12)]
  20. [InlineData(10, 10, 10, 10, 1, 1, 2, 9, 9, 12, 12)]
  21. [InlineData(10, 10, 10, 10, 1.5, 1.5, 1, 14, 14, 17, 17)]
  22. public void Rectangle_Bounds_Are_Snapped_To_Pixels(
  23. double x,
  24. double y,
  25. double width,
  26. double height,
  27. double scaleX,
  28. double scaleY,
  29. double penThickness,
  30. double expectedX,
  31. double expectedY,
  32. double expectedWidth,
  33. double expectedHeight)
  34. {
  35. var target = new TestRectangleDrawOperation(
  36. new Rect(x, y, width, height),
  37. Matrix.CreateScale(scaleX, scaleY),
  38. new Pen(Brushes.Black, penThickness));
  39. Assert.Equal(new Rect(expectedX, expectedY, expectedWidth, expectedHeight), target.Bounds);
  40. }
  41. [Fact]
  42. public void Image_Node_Releases_Reference_To_Bitmap_On_Dispose()
  43. {
  44. var bitmap = RefCountable.Create(Mock.Of<IBitmapImpl>());
  45. var imageNode = new ImageNode(
  46. Matrix.Identity,
  47. bitmap,
  48. 1,
  49. new Rect(1, 1, 1, 1),
  50. new Rect(1, 1, 1, 1),
  51. BitmapInterpolationMode.Default);
  52. Assert.Equal(2, bitmap.RefCount);
  53. imageNode.Dispose();
  54. Assert.Equal(1, bitmap.RefCount);
  55. }
  56. [Fact]
  57. public void HitTest_On_Geometry_Node_With_Zero_Transform_Does_Not_Throw()
  58. {
  59. var geometry = Mock.Of<IGeometryImpl>();
  60. var geometryNode = new GeometryNode(
  61. new Matrix(),
  62. Brushes.Black,
  63. null,
  64. geometry);
  65. geometryNode.HitTest(new Point());
  66. }
  67. private class TestRectangleDrawOperation : RectangleNode
  68. {
  69. public TestRectangleDrawOperation(Rect bounds, Matrix transform, Pen pen)
  70. : base(transform, pen.Brush?.ToImmutable(), pen, bounds, new BoxShadows())
  71. {
  72. }
  73. public override bool HitTest(Point p) => false;
  74. public override void Render(IDrawingContextImpl context) { }
  75. }
  76. private class TestDrawOperation : DrawOperation
  77. {
  78. public TestDrawOperation(Rect bounds, Matrix transform, Pen pen)
  79. :base(bounds, transform)
  80. {
  81. }
  82. public override bool HitTest(Point p) => false;
  83. public override void Render(IDrawingContextImpl context) { }
  84. }
  85. }
  86. }