DrawOperationTests.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Avalonia.Media;
  2. using Avalonia.Platform;
  3. using Avalonia.Rendering.SceneGraph;
  4. using Avalonia.Utilities;
  5. using Avalonia.Visuals.Media.Imaging;
  6. using Moq;
  7. using Xunit;
  8. namespace Avalonia.Visuals.UnitTests.Rendering.SceneGraph
  9. {
  10. public class DrawOperationTests
  11. {
  12. [Fact]
  13. public void Empty_Bounds_Remain_Empty()
  14. {
  15. var target = new TestDrawOperation(Rect.Empty, Matrix.Identity, null);
  16. Assert.Equal(Rect.Empty, 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 TestDrawOperation(
  36. new Rect(x, y, width, height),
  37. Matrix.CreateScale(scaleX, scaleY),
  38. penThickness.HasValue ? new Pen(Brushes.Black, penThickness.Value) : null);
  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. private class TestDrawOperation : DrawOperation
  57. {
  58. public TestDrawOperation(Rect bounds, Matrix transform, Pen pen)
  59. :base(bounds, transform, pen)
  60. {
  61. }
  62. public override bool HitTest(Point p) => false;
  63. public override void Render(IDrawingContextImpl context) { }
  64. }
  65. }
  66. }