CompositorInvalidationClippingTests.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using Avalonia.Controls;
  2. using Avalonia.Media;
  3. using Xunit;
  4. namespace Avalonia.Base.UnitTests.Rendering;
  5. /// <summary>
  6. /// Test class that verifies how clipping influences rendering in the compositor
  7. /// </summary>
  8. public class CompositorInvalidationClippingTests : CompositorTestsBase
  9. {
  10. [Fact]
  11. // Test case: When the ClipToBounds is false, all visuals should be rendered
  12. public void Siblings_Should_Be_Rendered_On_Invalidate_Without_ClipToBounds()
  13. {
  14. AssertRenderedVisuals(clipToBounds: false, clipGeometry: false, expectedRenderedVisualsCount: 4);
  15. }
  16. [Fact]
  17. // Test case: When the ClipToBounds is true, only visuals within the clipped boundary should be rendered
  18. public void Siblings_Should_Not_Be_Rendered_On_Invalidate_With_ClipToBounds()
  19. {
  20. AssertRenderedVisuals(clipToBounds: true, clipGeometry: false, expectedRenderedVisualsCount: 3);
  21. }
  22. [Fact]
  23. // Test case: When the Clip is used, only visuals within the clip geometry should be rendered
  24. public void Siblings_Should_Not_Be_Rendered_On_Invalidate_With_Clip()
  25. {
  26. AssertRenderedVisuals(clipToBounds: false, clipGeometry: true, expectedRenderedVisualsCount: 3);
  27. }
  28. private void AssertRenderedVisuals(bool clipToBounds, bool clipGeometry, int expectedRenderedVisualsCount)
  29. {
  30. using (var s = new CompositorCanvas())
  31. {
  32. //#1 visual is top level
  33. //#2 visual is s.Canvas
  34. //#3 visual is border1
  35. s.Canvas.Children.Add(new Border()
  36. {
  37. [Canvas.LeftProperty] = 0, [Canvas.TopProperty] = 0,
  38. Width = 20, Height = 10,
  39. Background = Brushes.Red,
  40. ClipToBounds = clipToBounds,
  41. Clip = clipGeometry ? new RectangleGeometry(new Rect(new Size(20, 10))) : null
  42. });
  43. //#4 visual is border2
  44. s.Canvas.Children.Add(new Border()
  45. {
  46. [Canvas.LeftProperty] = 30, [Canvas.TopProperty] = 50,
  47. Width = 20, Height = 10,
  48. Background = Brushes.Red,
  49. ClipToBounds = clipToBounds,
  50. Clip = clipGeometry ? new RectangleGeometry(new Rect(new Size(20, 10))) : null
  51. });
  52. s.RunJobs();
  53. s.Events.Reset();
  54. //invalidate border1
  55. s.Canvas.Children[0].IsVisible = false;
  56. s.RunJobs();
  57. s.AssertRenderedVisuals(expectedRenderedVisualsCount);
  58. }
  59. }
  60. }