TextBlockTests.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System.Net;
  2. using System.Threading.Tasks;
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.Documents;
  5. using Avalonia.Layout;
  6. using Avalonia.Media;
  7. using Xunit;
  8. #if AVALONIA_SKIA
  9. namespace Avalonia.Skia.RenderTests
  10. #else
  11. namespace Avalonia.Direct2D1.RenderTests.Controls
  12. #endif
  13. {
  14. public class TextBlockTests : TestBase
  15. {
  16. public TextBlockTests()
  17. : base(@"Controls\TextBlock")
  18. {
  19. }
  20. [Win32Fact("Has text")]
  21. public async Task Should_Draw_TextDecorations()
  22. {
  23. Border target = new Border
  24. {
  25. Padding = new Thickness(8),
  26. Width = 200,
  27. Height = 30,
  28. Background = Brushes.White,
  29. Child = new TextBlock
  30. {
  31. FontFamily = TestFontFamily,
  32. FontSize = 12,
  33. Foreground = Brushes.Black,
  34. Text = "Neque porro quisquam est qui dolorem",
  35. VerticalAlignment = VerticalAlignment.Top,
  36. TextWrapping = TextWrapping.NoWrap,
  37. TextDecorations = new TextDecorationCollection
  38. {
  39. new TextDecoration
  40. {
  41. Location = TextDecorationLocation.Overline,
  42. StrokeThickness= 1.5,
  43. StrokeThicknessUnit = TextDecorationUnit.Pixel,
  44. Stroke = new SolidColorBrush(Colors.Red)
  45. },
  46. new TextDecoration
  47. {
  48. Location = TextDecorationLocation.Baseline,
  49. StrokeThickness= 1.5,
  50. StrokeThicknessUnit = TextDecorationUnit.Pixel,
  51. Stroke = new SolidColorBrush(Colors.Green)
  52. },
  53. new TextDecoration
  54. {
  55. Location = TextDecorationLocation.Underline,
  56. StrokeThickness= 1.5,
  57. StrokeThicknessUnit = TextDecorationUnit.Pixel,
  58. Stroke = new SolidColorBrush(Colors.Blue),
  59. StrokeOffset = 2,
  60. StrokeOffsetUnit = TextDecorationUnit.Pixel
  61. }
  62. }
  63. }
  64. };
  65. await RenderToFile(target);
  66. CompareImages();
  67. }
  68. [Win32Fact("Has text")]
  69. public async Task Wrapping_NoWrap()
  70. {
  71. Decorator target = new Decorator
  72. {
  73. Padding = new Thickness(8),
  74. Width = 200,
  75. Height = 200,
  76. Child = new TextBlock
  77. {
  78. FontFamily = new FontFamily("Courier New"),
  79. Background = Brushes.Red,
  80. FontSize = 12,
  81. Foreground = Brushes.Black,
  82. Text = "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit",
  83. VerticalAlignment = VerticalAlignment.Top,
  84. TextWrapping = TextWrapping.NoWrap,
  85. }
  86. };
  87. await RenderToFile(target);
  88. CompareImages();
  89. }
  90. [Win32Fact("Has text")]
  91. public async Task RestrictedHeight_VerticalAlign()
  92. {
  93. Control text(VerticalAlignment verticalAlignment, bool clip = true, bool restrictHeight = true)
  94. {
  95. return new Border()
  96. {
  97. BorderBrush = Brushes.Blue,
  98. BorderThickness = new Thickness(1),
  99. VerticalAlignment = VerticalAlignment.Center,
  100. HorizontalAlignment = HorizontalAlignment.Center,
  101. Height = restrictHeight ? 20 : double.NaN,
  102. Margin = new Thickness(1),
  103. Child = new TextBlock
  104. {
  105. FontFamily = new FontFamily("Courier New"),
  106. Background = Brushes.Red,
  107. FontSize = 24,
  108. Foreground = Brushes.Black,
  109. Text = "L",
  110. VerticalAlignment = verticalAlignment,
  111. ClipToBounds = clip
  112. }
  113. };
  114. }
  115. Decorator target = new Decorator
  116. {
  117. Padding = new Thickness(8),
  118. Width = 190,
  119. Height = 80,
  120. Child = new StackPanel()
  121. {
  122. Orientation = Orientation.Horizontal,
  123. Children =
  124. {
  125. text(VerticalAlignment.Stretch, restrictHeight: false),
  126. text(VerticalAlignment.Center),
  127. text(VerticalAlignment.Stretch),
  128. text(VerticalAlignment.Top),
  129. text(VerticalAlignment.Bottom),
  130. text(VerticalAlignment.Center, clip:false),
  131. text(VerticalAlignment.Stretch, clip:false),
  132. text(VerticalAlignment.Top, clip:false),
  133. text(VerticalAlignment.Bottom, clip:false),
  134. }
  135. }
  136. };
  137. await RenderToFile(target);
  138. CompareImages();
  139. }
  140. [Win32Fact("Has text")]
  141. public async Task Should_Draw_Run_With_Background()
  142. {
  143. Decorator target = new Decorator
  144. {
  145. Padding = new Thickness(8),
  146. Width = 200,
  147. Height = 50,
  148. Child = new TextBlock
  149. {
  150. FontFamily = new FontFamily("Courier New"),
  151. FontSize = 12,
  152. Foreground = Brushes.Black,
  153. VerticalAlignment = VerticalAlignment.Top,
  154. TextWrapping = TextWrapping.NoWrap,
  155. Inlines = new InlineCollection
  156. {
  157. new Run
  158. {
  159. Text = "Neque porro quisquam",
  160. Background = Brushes.Red
  161. }
  162. }
  163. }
  164. };
  165. await RenderToFile(target);
  166. CompareImages();
  167. }
  168. }
  169. }