TextBlockTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Threading.Tasks;
  5. using Avalonia.Controls;
  6. using Avalonia.Controls.Documents;
  7. using Avalonia.Layout;
  8. using Avalonia.Media;
  9. using Xunit;
  10. namespace Avalonia.Skia.RenderTests
  11. {
  12. public class TextBlockTests : TestBase
  13. {
  14. public TextBlockTests()
  15. : base(@"Controls\TextBlock")
  16. {
  17. }
  18. [Win32Fact("Has text")]
  19. public async Task Should_Draw_TextDecorations()
  20. {
  21. Border target = new Border
  22. {
  23. Padding = new Thickness(8),
  24. Width = 200,
  25. Height = 30,
  26. Background = Brushes.White,
  27. Child = new TextBlock
  28. {
  29. FontFamily = TestFontFamily,
  30. FontSize = 12,
  31. Foreground = Brushes.Black,
  32. Text = "Neque porro quisquam est qui dolorem",
  33. VerticalAlignment = VerticalAlignment.Top,
  34. TextWrapping = TextWrapping.NoWrap,
  35. TextDecorations = new TextDecorationCollection
  36. {
  37. new TextDecoration
  38. {
  39. Location = TextDecorationLocation.Overline,
  40. StrokeThickness= 1.5,
  41. StrokeThicknessUnit = TextDecorationUnit.Pixel,
  42. Stroke = new SolidColorBrush(Colors.Red)
  43. },
  44. new TextDecoration
  45. {
  46. Location = TextDecorationLocation.Baseline,
  47. StrokeThickness= 1.5,
  48. StrokeThicknessUnit = TextDecorationUnit.Pixel,
  49. Stroke = new SolidColorBrush(Colors.Green)
  50. },
  51. new TextDecoration
  52. {
  53. Location = TextDecorationLocation.Underline,
  54. StrokeThickness= 1.5,
  55. StrokeThicknessUnit = TextDecorationUnit.Pixel,
  56. Stroke = new SolidColorBrush(Colors.Blue),
  57. StrokeOffset = 2,
  58. StrokeOffsetUnit = TextDecorationUnit.Pixel
  59. }
  60. }
  61. }
  62. };
  63. await RenderToFile(target);
  64. CompareImages();
  65. }
  66. [Win32Fact("Has text")]
  67. public async Task Wrapping_NoWrap()
  68. {
  69. Decorator target = new Decorator
  70. {
  71. Padding = new Thickness(8),
  72. Width = 200,
  73. Height = 200,
  74. Child = new TextBlock
  75. {
  76. FontFamily = new FontFamily("Courier New"),
  77. Background = Brushes.Red,
  78. FontSize = 12,
  79. Foreground = Brushes.Black,
  80. Text = "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit",
  81. VerticalAlignment = VerticalAlignment.Top,
  82. TextWrapping = TextWrapping.NoWrap,
  83. }
  84. };
  85. await RenderToFile(target);
  86. CompareImages();
  87. }
  88. [Win32Fact("Has text")]
  89. public async Task RestrictedHeight_VerticalAlign()
  90. {
  91. Control text(VerticalAlignment verticalAlignment, bool clip = true, bool restrictHeight = true)
  92. {
  93. return new Border()
  94. {
  95. BorderBrush = Brushes.Blue,
  96. BorderThickness = new Thickness(1),
  97. VerticalAlignment = VerticalAlignment.Center,
  98. HorizontalAlignment = HorizontalAlignment.Center,
  99. Height = restrictHeight ? 20 : double.NaN,
  100. Margin = new Thickness(1),
  101. Child = new TextBlock
  102. {
  103. FontFamily = new FontFamily("Courier New"),
  104. Background = Brushes.Red,
  105. FontSize = 24,
  106. Foreground = Brushes.Black,
  107. Text = "L",
  108. VerticalAlignment = verticalAlignment,
  109. ClipToBounds = clip
  110. }
  111. };
  112. }
  113. Decorator target = new Decorator
  114. {
  115. Padding = new Thickness(8),
  116. Width = 190,
  117. Height = 80,
  118. Child = new StackPanel()
  119. {
  120. Orientation = Orientation.Horizontal,
  121. Children =
  122. {
  123. text(VerticalAlignment.Stretch, restrictHeight: false),
  124. text(VerticalAlignment.Center),
  125. text(VerticalAlignment.Stretch),
  126. text(VerticalAlignment.Top),
  127. text(VerticalAlignment.Bottom),
  128. text(VerticalAlignment.Center, clip:false),
  129. text(VerticalAlignment.Stretch, clip:false),
  130. text(VerticalAlignment.Top, clip:false),
  131. text(VerticalAlignment.Bottom, clip:false),
  132. }
  133. }
  134. };
  135. await RenderToFile(target);
  136. CompareImages();
  137. }
  138. [Win32Fact("Has text")]
  139. public async Task Should_Draw_Run_With_Background()
  140. {
  141. Decorator target = new Decorator
  142. {
  143. Padding = new Thickness(8),
  144. Width = 200,
  145. Height = 50,
  146. Child = new TextBlock
  147. {
  148. FontFamily = new FontFamily("Courier New"),
  149. FontSize = 12,
  150. Foreground = Brushes.Black,
  151. VerticalAlignment = VerticalAlignment.Top,
  152. TextWrapping = TextWrapping.NoWrap,
  153. Inlines = new InlineCollection
  154. {
  155. new Run
  156. {
  157. Text = "Neque porro quisquam",
  158. Background = Brushes.Red
  159. }
  160. }
  161. }
  162. };
  163. await RenderToFile(target);
  164. CompareImages();
  165. }
  166. [InlineData(150, 200, TextWrapping.NoWrap)]
  167. [InlineData(44, 200, TextWrapping.NoWrap)]
  168. [InlineData(44, 400, TextWrapping.Wrap)]
  169. [Win32Theory("Has text")]
  170. public async Task Should_Measure_Arrange_TextBlock(double width, double height, TextWrapping textWrapping)
  171. {
  172. var text = "Hello World";
  173. var target = new StackPanel { Width = 200, Height = height };
  174. target.Children.Add(new TextBlock
  175. {
  176. Text = text,
  177. Background = Brushes.Red,
  178. HorizontalAlignment = HorizontalAlignment.Left,
  179. TextAlignment = TextAlignment.Left,
  180. Width = width,
  181. TextWrapping = textWrapping
  182. });
  183. target.Children.Add(new TextBlock
  184. {
  185. Text = text,
  186. Background = Brushes.Red,
  187. HorizontalAlignment = HorizontalAlignment.Left,
  188. TextAlignment = TextAlignment.Center,
  189. Width = width,
  190. TextWrapping = textWrapping
  191. });
  192. target.Children.Add(new TextBlock
  193. {
  194. Text = text,
  195. Background = Brushes.Red,
  196. HorizontalAlignment = HorizontalAlignment.Left,
  197. TextAlignment = TextAlignment.Right,
  198. Width = width,
  199. TextWrapping = textWrapping
  200. });
  201. target.Children.Add(new TextBlock
  202. {
  203. Text = text,
  204. Background = Brushes.Red,
  205. HorizontalAlignment = HorizontalAlignment.Center,
  206. TextAlignment = TextAlignment.Left,
  207. Width = width,
  208. TextWrapping = textWrapping
  209. });
  210. target.Children.Add(new TextBlock
  211. {
  212. Text = text,
  213. Background = Brushes.Red,
  214. HorizontalAlignment = HorizontalAlignment.Center,
  215. TextAlignment = TextAlignment.Center,
  216. Width = width,
  217. TextWrapping = textWrapping
  218. });
  219. target.Children.Add(new TextBlock
  220. {
  221. Text = text,
  222. Background = Brushes.Red,
  223. HorizontalAlignment = HorizontalAlignment.Center,
  224. TextAlignment = TextAlignment.Right,
  225. Width = width,
  226. TextWrapping = textWrapping
  227. });
  228. target.Children.Add(new TextBlock
  229. {
  230. Text = text,
  231. Background = Brushes.Red,
  232. HorizontalAlignment = HorizontalAlignment.Right,
  233. TextAlignment = TextAlignment.Left,
  234. Width = width,
  235. TextWrapping = textWrapping
  236. });
  237. target.Children.Add(new TextBlock
  238. {
  239. Text = text,
  240. Background = Brushes.Red,
  241. HorizontalAlignment = HorizontalAlignment.Right,
  242. TextAlignment = TextAlignment.Center,
  243. Width = width,
  244. TextWrapping = textWrapping
  245. });
  246. target.Children.Add(new TextBlock
  247. {
  248. Text = text,
  249. Background = Brushes.Red,
  250. HorizontalAlignment = HorizontalAlignment.Right,
  251. TextAlignment = TextAlignment.Right,
  252. Width = width,
  253. TextWrapping = textWrapping
  254. });
  255. var testName = $"Should_Measure_Arrange_TextBlock_{width}_{textWrapping}";
  256. await RenderToFile(target, testName);
  257. CompareImages(testName);
  258. }
  259. [Win32Fact("Has text")]
  260. public async Task Should_Keep_TrailingWhiteSpace()
  261. {
  262. // <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
  263. // <TextBlock Margin="0 10 0 0" HorizontalAlignment="Center" VerticalAlignment="Center" Background="Magenta" FontSize="44" Text="aaaa" FontFamily="Courier New"/>
  264. // <TextBlock Margin="0 10 0 0" HorizontalAlignment="Center" VerticalAlignment="Center" Background="Magenta" FontSize="44" Text="a a " FontFamily="Courier New"/>
  265. // <TextBlock Margin="0 10 0 0" HorizontalAlignment="Center" VerticalAlignment="Center" Background="Magenta" FontSize="44" Text=" " FontFamily="Courier New"/>
  266. // <TextBlock Margin="0 10 0 0" HorizontalAlignment="Center" VerticalAlignment="Center" Background="Magenta" FontSize="44" Text="LLLL" FontFamily="Courier New"/>
  267. // </StackPanel>
  268. var target = new StackPanel
  269. {
  270. VerticalAlignment = VerticalAlignment.Center,
  271. HorizontalAlignment = HorizontalAlignment.Center,
  272. Width = 300,
  273. Height = 300,
  274. };
  275. target.Children.Add(CreateText("aaaa"));
  276. target.Children.Add(CreateText("a a "));
  277. target.Children.Add(CreateText(" "));
  278. target.Children.Add(CreateText("LLLL"));
  279. var testName = $"Should_Keep_TrailingWhiteSpace";
  280. await RenderToFile(target, testName);
  281. CompareImages(testName);
  282. static TextBlock CreateText(string text) => new TextBlock
  283. {
  284. Margin = new Thickness(0, 10, 0, 0),
  285. HorizontalAlignment = HorizontalAlignment.Center,
  286. VerticalAlignment = VerticalAlignment.Center,
  287. Background = Brushes.Magenta,
  288. FontSize = 44,
  289. Text = text,
  290. FontFamily = new FontFamily("Courier New")
  291. };
  292. }
  293. [Win32Fact("Has text")]
  294. public async Task Should_Account_For_Overhang_Leading_And_Trailing()
  295. {
  296. var target = new StackPanel
  297. {
  298. VerticalAlignment = VerticalAlignment.Center,
  299. HorizontalAlignment = HorizontalAlignment.Center,
  300. Width = 300,
  301. Height = 600,
  302. Background = new SolidColorBrush(Colors.White), // Required antialiasing to work for Overhang
  303. };
  304. target.Children.Add(CreateText("f"));
  305. target.Children.Add(CreateText("y"));
  306. target.Children.Add(CreateText("ff"));
  307. target.Children.Add(CreateText("yy"));
  308. target.Children.Add(CreateText("faaf"));
  309. target.Children.Add(CreateText("yaay"));
  310. target.Children.Add(CreateText("y y "));
  311. target.Children.Add(CreateText("f f "));
  312. var testName = $"Should_Account_For_Overhang_Leading_And_Trailing";
  313. await RenderToFile(target, testName);
  314. CompareImages(testName);
  315. const string symbolsFont = "resm:Avalonia.Skia.RenderTests.Assets?assembly=Avalonia.Skia.RenderTests#Source Serif 4 36pt";
  316. static TextBlock CreateText(string text) => new TextBlock
  317. {
  318. ClipToBounds = false,
  319. Margin = new Thickness(4),
  320. HorizontalAlignment = HorizontalAlignment.Center,
  321. VerticalAlignment = VerticalAlignment.Center,
  322. Background = Brushes.Magenta,
  323. FontStyle = FontStyle.Italic,
  324. FontSize = 44,
  325. Text = text,
  326. FontFamily = new FontFamily(symbolsFont)
  327. };
  328. }
  329. [Win32Fact("Has text")]
  330. public async Task Should_Draw_MultiLineText_WithOverHandLeadingTrailing()
  331. {
  332. var target = new StackPanel
  333. {
  334. VerticalAlignment = VerticalAlignment.Center,
  335. HorizontalAlignment = HorizontalAlignment.Center,
  336. Width = 600,
  337. Height = 200,
  338. Background = new SolidColorBrush(Colors.White), // Required antialiasing to work for Overhang
  339. };
  340. target.Children.Add(CreateText("fff Why this is a\nbig text yyy\nyyy with multiple lines fff"));
  341. var testName = $"Should_Draw_MultiLineText_WithOverHandLeadingTrailing";
  342. await RenderToFile(target, testName);
  343. CompareImages(testName);
  344. const string symbolsFont = "resm:Avalonia.Skia.RenderTests.Assets?assembly=Avalonia.Skia.RenderTests#Source Serif 4 36pt";
  345. static TextBlock CreateText(string text) => new TextBlock
  346. {
  347. HorizontalAlignment = HorizontalAlignment.Center,
  348. VerticalAlignment = VerticalAlignment.Center,
  349. TextAlignment = TextAlignment.Center,
  350. Background = Brushes.Magenta,
  351. FontStyle = FontStyle.Italic,
  352. FontSize = 44,
  353. Text = text,
  354. FontFamily = new FontFamily(symbolsFont)
  355. };
  356. }
  357. }
  358. }