A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

173 lines
4.9 KiB

using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Layout;
using Avalonia.Media;
using Avalonia.Rendering.Composition;
using Avalonia.Threading;
namespace Avalonia.Headless.UnitTests;
public class RenderingTests
{
#if NUNIT
[AvaloniaTest, Timeout(10000)]
#elif XUNIT
[AvaloniaFact(Timeout = 10000)]
#endif
public void Should_Render_Last_Frame_To_Bitmap()
{
var window = new Window
{
Content = new ContentControl
{
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Padding = new Thickness(4),
Content = new PathIcon
{
Data = StreamGeometry.Parse("M0,9 L10,0 20,9 19,10 10,2 1,10 z")
}
},
SizeToContent = SizeToContent.WidthAndHeight
};
window.Show();
var frame = window.CaptureRenderedFrame();
Assert.NotNull(frame);
}
#if NUNIT
[AvaloniaTest, Timeout(10000)]
#elif XUNIT
[AvaloniaFact(Timeout = 10000)]
#endif
public void Should_Not_Crash_On_GeometryGroup()
{
var window = new Window
{
Content = new ContentControl
{
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Padding = new Thickness(4),
Content = new PathIcon
{
Data = new GeometryGroup()
{
Children = new GeometryCollection(new []
{
new RectangleGeometry(new Rect(0, 0, 50, 50)),
new RectangleGeometry(new Rect(50, 50, 100, 100))
})
}
}
},
SizeToContent = SizeToContent.WidthAndHeight
};
window.Show();
var frame = window.CaptureRenderedFrame();
Assert.NotNull(frame);
}
#if NUNIT
[AvaloniaTest, Timeout(10000)]
#elif XUNIT
[AvaloniaFact(Timeout = 10000)]
#endif
public void Should_Not_Crash_On_CombinedGeometry()
{
var window = new Window
{
Content = new ContentControl
{
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Padding = new Thickness(4),
Content = new PathIcon
{
Data = new CombinedGeometry(GeometryCombineMode.Union,
new RectangleGeometry(new Rect(0, 0, 50, 50)),
new RectangleGeometry(new Rect(50, 50, 100, 100)))
}
},
SizeToContent = SizeToContent.WidthAndHeight
};
window.Show();
var frame = window.CaptureRenderedFrame();
Assert.NotNull(frame);
}
#if NUNIT
[AvaloniaTest, Timeout(10000)]
#elif XUNIT
[AvaloniaFact(Timeout = 10000)]
#endif
public void Should_Not_Hang_With_Non_Trivial_Layout()
{
var window = new Window
{
Content = new ContentControl
{
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Padding = new Thickness(1),
Content = new ListBox
{
ItemsSource = new ObservableCollection<string>()
{
"Test 1",
"Test 2"
}
}
},
SizeToContent = SizeToContent.WidthAndHeight
};
window.Show();
var frame = window.CaptureRenderedFrame();
Assert.NotNull(frame);
}
#if NUNIT
[AvaloniaTest, Timeout(10000)]
#elif XUNIT
[AvaloniaFact(Timeout = 10000)]
#endif
public async Task Should_Render_To_A_Compositor_Snapshot_Capture()
{
var window = new Window
{
Content = new ContentControl
{
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Width = 100,
Height = 100,
Background = Brushes.Green
},
SizeToContent = SizeToContent.WidthAndHeight
};
window.Show();
var compositionVisual = ElementComposition.GetElementVisual(window)!;
var snapshot = await compositionVisual.Compositor.CreateCompositionVisualSnapshot(compositionVisual, 1);
Assert.NotNull(snapshot);
// ReSharper disable CompareOfFloatsByEqualityOperator
Assert.True(100 == snapshot.Size.Width);
Assert.True(100 == snapshot.Size.Height);
// ReSharper restore CompareOfFloatsByEqualityOperator
}
}