From 4f36cf41a147d5ff8a9b38c4b59eb041fe954c63 Mon Sep 17 00:00:00 2001 From: timunie Date: Tue, 3 Mar 2026 08:35:50 +0100 Subject: [PATCH] Add another unit test to ensure the recent changes don't get lost at some point in time --- .../Media/DrawingGroupTests.cs | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 tests/Avalonia.Base.UnitTests/Media/DrawingGroupTests.cs diff --git a/tests/Avalonia.Base.UnitTests/Media/DrawingGroupTests.cs b/tests/Avalonia.Base.UnitTests/Media/DrawingGroupTests.cs new file mode 100644 index 0000000000..795821b0f0 --- /dev/null +++ b/tests/Avalonia.Base.UnitTests/Media/DrawingGroupTests.cs @@ -0,0 +1,99 @@ +using Avalonia.Media; +using Avalonia.Platform; +using Avalonia.Rendering.SceneGraph; +using Avalonia.UnitTests; +using Avalonia.Utilities; +using Moq; +using Xunit; + +namespace Avalonia.Base.UnitTests.Media; + +public class DrawingGroupTests +{ + [Fact] + public void PushEffect_Should_Store_Provided_Bounds() + { + using (UnitTestApplication.Start(new TestServices(renderInterface: Mock.Of()))) + { + var group = new DrawingGroup(); + var effect = new BlurEffect { Radius = 10 }; + var bounds = new Rect(10, 10, 100, 100); + + using (var context = group.Open()) + { + using (context.PushEffect(effect, bounds)) + { + context.DrawRectangle(Brushes.Red, null, new Rect(20, 20, 50, 50)); + } + } + + // The Open() call adds a child DrawingGroup to the root group when PushEffect is called. + Assert.Single(group.Children); + var childGroup = Assert.IsType(group.Children[0]); + Assert.Equal(effect, childGroup.Effect); + Assert.Equal(bounds, childGroup.EffectBounds); + } + } + + [Fact] + public void Drawing_With_Effect_Should_Use_Stored_Bounds() + { + using (UnitTestApplication.Start(new TestServices(renderInterface: Mock.Of()))) + { + var effect = new BlurEffect { Radius = 10 }; + var bounds = new Rect(10, 10, 100, 100); + var group = new DrawingGroup + { + Effect = effect, + EffectBounds = bounds + }; + group.Children.Add(new GeometryDrawing { Brush = Brushes.Red, Geometry = new RectangleGeometry { Rect = new Rect(20, 20, 50, 50) } }); + + var mockContext = new MockDrawingContext(); + group.Draw(mockContext); + + Assert.Equal(effect, mockContext.Effect); + Assert.Equal(bounds, mockContext.Bounds); + } + } + + private class MockDrawingContext : DrawingContext + { + public IEffect? Effect { get; private set; } + public Rect Bounds { get; private set; } + + protected override void PushEffectCore(IEffect effect, Rect bounds) + { + Effect = effect; + Bounds = bounds; + } + + protected override void PopEffectCore() { } + + // Implementing required abstract members + protected override void DrawEllipseCore(IBrush? brush, IPen? pen, Rect rect) { } + protected override void DrawGeometryCore(IBrush? brush, IPen? pen, IGeometryImpl geometry) { } + protected override void DrawLineCore(IPen pen, Point p1, Point p2) { } + protected override void DrawRectangleCore(IBrush? brush, IPen? pen, RoundedRect rrect, BoxShadows boxShadows = default) { } + protected override void PushClipCore(Rect rect) { } + protected override void PushClipCore(RoundedRect rect) { } + protected override void PushGeometryClipCore(Geometry clip) { } + protected override void PushOpacityCore(double opacity) { } + protected override void PushOpacityMaskCore(IBrush mask, Rect bounds) { } + protected override void PushTransformCore(Matrix matrix) { } + protected override void PopClipCore() { } + protected override void PopGeometryClipCore() { } + protected override void PopOpacityCore() { } + protected override void PopOpacityMaskCore() { } + protected override void PopTransformCore() { } + + protected override void DisposeCore() { } + internal override void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect) { } + public override void Custom(ICustomDrawOperation custom) { } + public override void DrawGlyphRun(IBrush? foreground, GlyphRun glyphRun) { } + protected override void PushRenderOptionsCore(RenderOptions renderOptions) { } + protected override void PushTextOptionsCore(TextOptions textOptions) { } + protected override void PopRenderOptionsCore() { } + protected override void PopTextOptionsCore() { } + } +}