1
0

RenderingTests.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System.Collections.ObjectModel;
  2. using System.Threading.Tasks;
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.Shapes;
  5. using Avalonia.Layout;
  6. using Avalonia.Media;
  7. using Avalonia.Rendering.Composition;
  8. using Avalonia.Threading;
  9. namespace Avalonia.Headless.UnitTests;
  10. public class RenderingTests
  11. {
  12. #if NUNIT
  13. [AvaloniaTest, Timeout(10000)]
  14. #elif XUNIT
  15. [AvaloniaFact]
  16. #endif
  17. public void Should_Render_Last_Frame_To_Bitmap()
  18. {
  19. var window = new Window
  20. {
  21. Content = new ContentControl
  22. {
  23. HorizontalAlignment = HorizontalAlignment.Stretch,
  24. VerticalAlignment = VerticalAlignment.Stretch,
  25. Padding = new Thickness(4),
  26. Content = new PathIcon
  27. {
  28. Data = StreamGeometry.Parse("M0,9 L10,0 20,9 19,10 10,2 1,10 z")
  29. }
  30. },
  31. SizeToContent = SizeToContent.WidthAndHeight
  32. };
  33. window.Show();
  34. var frame = window.CaptureRenderedFrame();
  35. Assert.NotNull(frame);
  36. }
  37. #if NUNIT
  38. [AvaloniaTest, Timeout(10000)]
  39. #elif XUNIT
  40. [AvaloniaFact]
  41. #endif
  42. public void Should_Not_Crash_On_GeometryGroup()
  43. {
  44. var window = new Window
  45. {
  46. Content = new ContentControl
  47. {
  48. HorizontalAlignment = HorizontalAlignment.Stretch,
  49. VerticalAlignment = VerticalAlignment.Stretch,
  50. Padding = new Thickness(4),
  51. Content = new PathIcon
  52. {
  53. Data = new GeometryGroup()
  54. {
  55. Children = new GeometryCollection(new []
  56. {
  57. new RectangleGeometry(new Rect(0, 0, 50, 50)),
  58. new RectangleGeometry(new Rect(50, 50, 100, 100))
  59. })
  60. }
  61. }
  62. },
  63. SizeToContent = SizeToContent.WidthAndHeight
  64. };
  65. window.Show();
  66. var frame = window.CaptureRenderedFrame();
  67. Assert.NotNull(frame);
  68. }
  69. #if NUNIT
  70. [AvaloniaTest, Timeout(10000)]
  71. #elif XUNIT
  72. [AvaloniaFact]
  73. #endif
  74. public void Should_Not_Crash_On_CombinedGeometry()
  75. {
  76. var window = new Window
  77. {
  78. Content = new ContentControl
  79. {
  80. HorizontalAlignment = HorizontalAlignment.Stretch,
  81. VerticalAlignment = VerticalAlignment.Stretch,
  82. Padding = new Thickness(4),
  83. Content = new PathIcon
  84. {
  85. Data = new CombinedGeometry(GeometryCombineMode.Union,
  86. new RectangleGeometry(new Rect(0, 0, 50, 50)),
  87. new RectangleGeometry(new Rect(50, 50, 100, 100)))
  88. }
  89. },
  90. SizeToContent = SizeToContent.WidthAndHeight
  91. };
  92. window.Show();
  93. var frame = window.CaptureRenderedFrame();
  94. Assert.NotNull(frame);
  95. }
  96. #if NUNIT
  97. [AvaloniaTest, Timeout(10000)]
  98. #elif XUNIT
  99. [AvaloniaFact]
  100. #endif
  101. public void Should_Not_Hang_With_Non_Trivial_Layout()
  102. {
  103. var window = new Window
  104. {
  105. Content = new ContentControl
  106. {
  107. HorizontalAlignment = HorizontalAlignment.Stretch,
  108. VerticalAlignment = VerticalAlignment.Stretch,
  109. Padding = new Thickness(1),
  110. Content = new ListBox
  111. {
  112. ItemsSource = new ObservableCollection<string>()
  113. {
  114. "Test 1",
  115. "Test 2"
  116. }
  117. }
  118. },
  119. SizeToContent = SizeToContent.WidthAndHeight
  120. };
  121. window.Show();
  122. var frame = window.CaptureRenderedFrame();
  123. Assert.NotNull(frame);
  124. }
  125. #if NUNIT
  126. [AvaloniaTest, Timeout(10000)]
  127. #elif XUNIT
  128. [AvaloniaFact(Timeout = 10000)]
  129. #endif
  130. public async Task Should_Render_To_A_Compositor_Snapshot_Capture()
  131. {
  132. var window = new Window
  133. {
  134. Content = new ContentControl
  135. {
  136. HorizontalAlignment = HorizontalAlignment.Stretch,
  137. VerticalAlignment = VerticalAlignment.Stretch,
  138. Width = 100,
  139. Height = 100,
  140. Background = Brushes.Green
  141. },
  142. SizeToContent = SizeToContent.WidthAndHeight
  143. };
  144. window.Show();
  145. var compositionVisual = ElementComposition.GetElementVisual(window)!;
  146. var snapshot = await compositionVisual.Compositor.CreateCompositionVisualSnapshot(compositionVisual, 1);
  147. Assert.NotNull(snapshot);
  148. // ReSharper disable CompareOfFloatsByEqualityOperator
  149. Assert.True(100 == snapshot.Size.Width);
  150. Assert.True(100 == snapshot.Size.Height);
  151. // ReSharper restore CompareOfFloatsByEqualityOperator
  152. }
  153. }