TextBlockTests.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. using System;
  2. using Avalonia.Controls.Documents;
  3. using Avalonia.Controls.Templates;
  4. using Avalonia.Data;
  5. using Avalonia.Layout;
  6. using Avalonia.Media;
  7. using Avalonia.UnitTests;
  8. using Xunit;
  9. using static System.Net.Mime.MediaTypeNames;
  10. namespace Avalonia.Controls.UnitTests
  11. {
  12. public class TextBlockTests : ScopedTestBase
  13. {
  14. [Fact]
  15. public void DefaultBindingMode_Should_Be_OneWay()
  16. {
  17. Assert.Equal(
  18. BindingMode.OneWay,
  19. TextBlock.TextProperty.GetMetadata(typeof(TextBlock)).DefaultBindingMode);
  20. }
  21. [Fact]
  22. public void Default_Text_Value_Should_Be_Null()
  23. {
  24. var textBlock = new TextBlock();
  25. Assert.Equal(null, textBlock.Text);
  26. }
  27. [Fact]
  28. public void Calling_Measure_Should_Update_TextLayout()
  29. {
  30. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  31. {
  32. var textBlock = new TestTextBlock { Text = "Hello World" };
  33. var constraint = textBlock.Constraint;
  34. Assert.True(double.IsNaN(constraint.Width));
  35. Assert.True(double.IsNaN(constraint.Height));
  36. textBlock.Measure(new Size(100, 100));
  37. var textLayout = textBlock.TextLayout;
  38. textBlock.Measure(new Size(50, 100));
  39. Assert.NotEqual(textLayout, textBlock.TextLayout);
  40. }
  41. }
  42. [Fact]
  43. public void Should_Measure_MinTextWith()
  44. {
  45. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  46. {
  47. var textBlock = new TextBlock
  48. {
  49. Text = "Hello
שלום
Really really really really long line",
  50. HorizontalAlignment = HorizontalAlignment.Center,
  51. TextAlignment = TextAlignment.DetectFromContent,
  52. TextWrapping = TextWrapping.Wrap
  53. };
  54. textBlock.Measure(new Size(1920, 1080));
  55. var textLayout = textBlock.TextLayout;
  56. var constraint = LayoutHelper.RoundLayoutSizeUp(new Size(textLayout.Width, textLayout.Height), 1);
  57. Assert.Equal(textBlock.DesiredSize, constraint);
  58. }
  59. }
  60. [Fact]
  61. public void Calling_Arrange_With_Different_Size_Should_Update_Constraint_And_TextLayout()
  62. {
  63. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  64. {
  65. var textBlock = new TestTextBlock { Text = "Hello World" };
  66. textBlock.Measure(Size.Infinity);
  67. var textLayout = textBlock.TextLayout;
  68. var constraint = LayoutHelper.RoundLayoutSizeUp(new Size(textLayout.WidthIncludingTrailingWhitespace, textLayout.Height), 1);
  69. textBlock.Arrange(new Rect(constraint));
  70. //TextLayout is recreated after arrange
  71. textLayout = textBlock.TextLayout;
  72. Assert.Equal(constraint, textBlock.Constraint);
  73. textBlock.Measure(constraint);
  74. Assert.Equal(textLayout, textBlock.TextLayout);
  75. constraint += new Size(50, 0);
  76. textBlock.Arrange(new Rect(constraint));
  77. Assert.Equal(constraint, textBlock.Constraint);
  78. //TextLayout is recreated after arrange
  79. Assert.NotEqual(textLayout, textBlock.TextLayout);
  80. }
  81. }
  82. [Fact]
  83. public void Calling_Measure_With_Infinite_Space_Should_Set_DesiredSize()
  84. {
  85. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  86. {
  87. var textBlock = new TestTextBlock { Text = "Hello World" };
  88. textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  89. var textLayout = textBlock.TextLayout;
  90. var constraint = LayoutHelper.RoundLayoutSizeUp(new Size(textLayout.WidthIncludingTrailingWhitespace, textLayout.Height), 1);
  91. Assert.Equal(constraint, textBlock.DesiredSize);
  92. }
  93. }
  94. [Fact]
  95. public void Changing_InlinesCollection_Should_Invalidate_Measure()
  96. {
  97. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  98. {
  99. var target = new TextBlock();
  100. target.Measure(Size.Infinity);
  101. Assert.True(target.IsMeasureValid);
  102. target.Inlines.Add(new Run("Hello"));
  103. Assert.False(target.IsMeasureValid);
  104. target.Measure(Size.Infinity);
  105. Assert.True(target.IsMeasureValid);
  106. }
  107. }
  108. [Fact]
  109. public void Changing_Inlines_Should_Attach_Embedded_Controls_To_Parents()
  110. {
  111. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  112. {
  113. var target = new TextBlock();
  114. var control = new Border();
  115. var inlineUIContainer = new InlineUIContainer { Child = control };
  116. target.Inlines = new InlineCollection { inlineUIContainer };
  117. Assert.Equal(inlineUIContainer, control.Parent);
  118. Assert.Equal(target, control.VisualParent);
  119. }
  120. }
  121. [Fact]
  122. public void Can_Call_Measure_Without_InvalidateTextLayout()
  123. {
  124. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  125. {
  126. var target = new TextBlock();
  127. target.Inlines.Add(new TextBox { Text = "Hello"});
  128. target.Measure(Size.Infinity);
  129. target.InvalidateMeasure();
  130. target.Measure(Size.Infinity);
  131. }
  132. }
  133. [Fact]
  134. public void Embedded_Control_Should_Keep_Focus()
  135. {
  136. using (UnitTestApplication.Start(TestServices.RealFocus))
  137. {
  138. var target = new TextBlock();
  139. var root = new TestRoot
  140. {
  141. Child = target
  142. };
  143. var textBox = new TextBox { Text = "Hello", Template = TextBoxTests.CreateTemplate() };
  144. target.Inlines.Add(textBox);
  145. target.Measure(Size.Infinity);
  146. textBox.Focus();
  147. Assert.Same(textBox, root.FocusManager.GetFocusedElement());
  148. target.InvalidateMeasure();
  149. Assert.Same(textBox, root.FocusManager.GetFocusedElement());
  150. target.Measure(Size.Infinity);
  151. Assert.Same(textBox, root.FocusManager.GetFocusedElement());
  152. }
  153. }
  154. [Fact]
  155. public void Changing_Inlines_Properties_Should_Invalidate_Measure()
  156. {
  157. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  158. {
  159. var target = new TextBlock();
  160. var inline = new Run("Hello");
  161. target.Inlines.Add(inline);
  162. target.Measure(Size.Infinity);
  163. Assert.True(target.IsMeasureValid);
  164. inline.Foreground = Brushes.Green;
  165. Assert.False(target.IsMeasureValid);
  166. }
  167. }
  168. [Fact]
  169. public void Changing_Inlines_Should_Invalidate_Measure()
  170. {
  171. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  172. {
  173. var target = new TextBlock();
  174. var inlines = new InlineCollection { new Run("Hello") };
  175. target.Measure(Size.Infinity);
  176. Assert.True(target.IsMeasureValid);
  177. target.Inlines = inlines;
  178. Assert.False(target.IsMeasureValid);
  179. }
  180. }
  181. [Fact]
  182. public void Changing_Inlines_Should_Reset_Inlines_Parent()
  183. {
  184. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  185. {
  186. var target = new TextBlock();
  187. var run = new Run("Hello");
  188. target.Inlines.Add(run);
  189. target.Measure(Size.Infinity);
  190. Assert.True(target.IsMeasureValid);
  191. target.Inlines = null;
  192. Assert.Null(run.Parent);
  193. target.Inlines = new InlineCollection { run };
  194. Assert.Equal(target, run.Parent);
  195. }
  196. }
  197. [Fact]
  198. public void Changing_InlineHost_Should_Propagate_To_Nested_Inlines()
  199. {
  200. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  201. {
  202. var target = new TextBlock();
  203. var span = new Span { Inlines = new InlineCollection { new Run { Text = "World" } } };
  204. var inlines = new InlineCollection{ new Run{Text = "Hello "}, span };
  205. target.Inlines = inlines;
  206. Assert.Equal(target, span.InlineHost);
  207. }
  208. }
  209. [Fact]
  210. public void Changing_Inlines_Should_Reset_VisualChildren()
  211. {
  212. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  213. {
  214. var target = new TextBlock();
  215. target.Inlines.Add(new Border());
  216. target.Measure(Size.Infinity);
  217. Assert.NotEmpty(target.VisualChildren);
  218. target.Inlines = null;
  219. Assert.Empty(target.VisualChildren);
  220. }
  221. }
  222. [Fact]
  223. public void Changing_Inlines_Should_Reset_InlineUIContainer_VisualParent_On_Measure()
  224. {
  225. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  226. {
  227. var target = new TextBlock();
  228. var control = new Control();
  229. var run = new InlineUIContainer(control);
  230. target.Inlines.Add(run);
  231. target.Measure(Size.Infinity);
  232. Assert.True(target.IsMeasureValid);
  233. Assert.Equal(target, control.VisualParent);
  234. target.Inlines = null;
  235. Assert.Null(run.Parent);
  236. target.Inlines = new InlineCollection { new Run("Hello World") };
  237. Assert.Null(run.Parent);
  238. target.Measure(Size.Infinity);
  239. Assert.Null(control.VisualParent);
  240. }
  241. }
  242. [Fact]
  243. public void InlineUIContainer_Child_Should_Be_Arranged()
  244. {
  245. using (UnitTestApplication.Start(TestServices.StyledWindow))
  246. {
  247. var target = new TextBlock();
  248. var button = new Button { Content = "12345678" };
  249. button.Template = new FuncControlTemplate<Button>((parent, scope) =>
  250. new TextBlock
  251. {
  252. Name = "PART_ContentPresenter",
  253. [!TextBlock.TextProperty] = parent[!ContentControl.ContentProperty],
  254. }.RegisterInNameScope(scope)
  255. );
  256. target.Inlines!.Add("123456");
  257. target.Inlines.Add(new InlineUIContainer(button));
  258. target.Inlines.Add("123456");
  259. target.Measure(Size.Infinity);
  260. target.Arrange(new Rect(target.DesiredSize));
  261. Assert.True(button.IsMeasureValid);
  262. Assert.Equal(80, button.DesiredSize.Width);
  263. target.Arrange(new Rect(new Size(200, 50)));
  264. Assert.True(button.IsArrangeValid);
  265. Assert.Equal(60, button.Bounds.Left);
  266. }
  267. }
  268. [Fact]
  269. public void InlineUIContainer_Child_Should_Be_Constrained()
  270. {
  271. using (UnitTestApplication.Start(TestServices.StyledWindow))
  272. {
  273. var target = new TextBlock();
  274. GeometryDrawing drawing = new GeometryDrawing();
  275. drawing.Geometry = new RectangleGeometry(new Rect(0, 0, 500, 500));
  276. DrawingImage image = new DrawingImage(drawing);
  277. Image imageControl = new Image { Source = image };
  278. InlineUIContainer container = new InlineUIContainer(imageControl);
  279. target.Inlines.Add(new Run("The child should not be limited by position on line."));
  280. target.Inlines.Add(container);
  281. target.Measure(new Size(100, 100));
  282. target.Arrange(new Rect(target.DesiredSize));
  283. Assert.True(imageControl.IsMeasureValid);
  284. Assert.Equal(100, imageControl.Bounds.Width);
  285. }
  286. }
  287. [Fact]
  288. public void Setting_Text_Should_Reset_Inlines()
  289. {
  290. using (UnitTestApplication.Start(TestServices.StyledWindow))
  291. {
  292. var target = new TextBlock();
  293. target.Inlines.Add(new Run("Hello World"));
  294. Assert.Equal(null, target.Text);
  295. Assert.Equal(1, target.Inlines.Count);
  296. target.Text = "1234";
  297. Assert.Equal("1234", target.Text);
  298. Assert.Equal(0, target.Inlines.Count);
  299. }
  300. }
  301. [Fact]
  302. public void Setting_TextDecorations_Should_Update_Inlines()
  303. {
  304. using (UnitTestApplication.Start(TestServices.StyledWindow))
  305. {
  306. var target = new TextBlock();
  307. target.Inlines.Add(new Run("Hello World"));
  308. Assert.Equal(1, target.Inlines.Count);
  309. Assert.Null(target.Inlines[0].TextDecorations);
  310. var underline = TextDecorations.Underline;
  311. target.TextDecorations = underline;
  312. Assert.Equal(underline, target.Inlines[0].TextDecorations);
  313. }
  314. }
  315. [Fact]
  316. public void TextBlock_TextLines_Should_Be_Empty()
  317. {
  318. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  319. {
  320. var textblock = new TextBlock();
  321. textblock.Inlines?.Add(new Run("123"));
  322. textblock.Measure(new Size(200, 200));
  323. int count = textblock.TextLayout.TextLines[0].TextRuns.Count;
  324. textblock.Inlines?.Clear();
  325. textblock.Measure(new Size(200, 200));
  326. int count1 = textblock.TextLayout.TextLines[0].TextRuns.Count;
  327. Assert.NotEqual(count, count1);
  328. }
  329. }
  330. [Fact]
  331. public void TextBlock_With_Infinite_Size_Should_Be_Remeasured_After_TextLayout_Created()
  332. {
  333. using var app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
  334. var target = new TextBlock { Text = "" };
  335. var layout = target.TextLayout;
  336. Assert.Equal(0.0, layout.MaxWidth);
  337. Assert.Equal(0.0, layout.MaxHeight);
  338. target.Text = "foo";
  339. target.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  340. Assert.True(target.DesiredSize.Width > 0);
  341. Assert.True(target.DesiredSize.Height > 0);
  342. }
  343. [Fact]
  344. public void TextBlock_With_UseLayoutRounding_True_Should_Round_DesiredSize()
  345. {
  346. using var app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
  347. var target = new TextBlock { Text = "1980" };
  348. target.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  349. Assert.Equal(target.DesiredSize, new Size(40, 10));
  350. }
  351. [Fact]
  352. public void TextBlock_With_UseLayoutRounding_True_Should_Round_Padding_And_DesiredSize()
  353. {
  354. using var app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
  355. var target = new TextBlock { Text = "1980", Padding = new(2.25) };
  356. target.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  357. Assert.Equal(target.DesiredSize, new Size(44, 14));
  358. }
  359. [Fact]
  360. public void TextBlock_With_UseLayoutRounding_False_Should_Not_Round_DesiredSize()
  361. {
  362. using var app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
  363. var target = new TextBlock { Text = "1980", UseLayoutRounding = false };
  364. target.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  365. Assert.Equal(target.DesiredSize, new Size(40, 9.6));
  366. }
  367. [Fact]
  368. public void TextBlock_With_UseLayoutRounding_False_Should_Not_Round_Bounds()
  369. {
  370. using var app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
  371. var target = new TextBlock { Text = "1980", UseLayoutRounding = false };
  372. target.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  373. target.Arrange(new Rect(default, target.DesiredSize));
  374. Assert.Equal(target.Bounds, new Rect(0, 0, 40, 9.6));
  375. }
  376. [Fact]
  377. public void TextBlock_With_UseLayoutRounding_False_Should_Not_Round_Padding_In_MeasureOverride()
  378. {
  379. using var app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
  380. var target = new TextBlock { Text = "1980", UseLayoutRounding = false, Padding = new(2.25) };
  381. target.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  382. Assert.Equal(target.DesiredSize, new Size(44.5, 14.1));
  383. }
  384. [Fact]
  385. public void TextBlock_With_UseLayoutRounding_False_Should_Not_Round_Padding_In_ArrangeOverride()
  386. {
  387. using var app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
  388. var target = new TextBlock { Text = "1980", UseLayoutRounding = false, Padding = new(2.25) };
  389. target.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  390. target.Arrange(new Rect(default, target.DesiredSize));
  391. Assert.Equal(target.Bounds, new Rect(0, 0, 44.5, 14.1));
  392. }
  393. private class TestTextBlock : TextBlock
  394. {
  395. public Size Constraint => _constraint;
  396. }
  397. }
  398. }