TextBlockTests.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using Avalonia.Controls.Documents;
  3. using Avalonia.Controls.Templates;
  4. using Avalonia.Data;
  5. using Avalonia.Media;
  6. using Avalonia.Metadata;
  7. using Avalonia.Rendering;
  8. using Avalonia.UnitTests;
  9. using Moq;
  10. using Xunit;
  11. namespace Avalonia.Controls.UnitTests
  12. {
  13. public class TextBlockTests
  14. {
  15. [Fact]
  16. public void DefaultBindingMode_Should_Be_OneWay()
  17. {
  18. Assert.Equal(
  19. BindingMode.OneWay,
  20. TextBlock.TextProperty.GetMetadata(typeof(TextBlock)).DefaultBindingMode);
  21. }
  22. [Fact]
  23. public void Default_Text_Value_Should_Be_Null()
  24. {
  25. var textBlock = new TextBlock();
  26. Assert.Equal(null, textBlock.Text);
  27. }
  28. [Fact]
  29. public void Changing_Background_Brush_Color_Should_Invalidate_Visual()
  30. {
  31. var target = new TextBlock()
  32. {
  33. Background = new SolidColorBrush(Colors.Red),
  34. };
  35. var root = new TestRoot(target);
  36. var renderer = Mock.Get(root.Renderer);
  37. renderer.Invocations.Clear();
  38. ((SolidColorBrush)target.Background).Color = Colors.Green;
  39. renderer.Verify(x => x.AddDirty(target), Times.Once);
  40. }
  41. [Fact]
  42. public void Changing_Foreground_Brush_Color_Should_Invalidate_Visual()
  43. {
  44. var target = new TextBlock()
  45. {
  46. Foreground = new SolidColorBrush(Colors.Red),
  47. };
  48. var root = new TestRoot(target);
  49. var renderer = Mock.Get(root.Renderer);
  50. renderer.Invocations.Clear();
  51. ((SolidColorBrush)target.Foreground).Color = Colors.Green;
  52. renderer.Verify(x => x.AddDirty(target), Times.Once);
  53. }
  54. [Fact]
  55. public void Changing_InlinesCollection_Should_Invalidate_Measure()
  56. {
  57. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  58. {
  59. var target = new TextBlock();
  60. target.Measure(Size.Infinity);
  61. Assert.True(target.IsMeasureValid);
  62. target.Inlines.Add(new Run("Hello"));
  63. Assert.False(target.IsMeasureValid);
  64. target.Measure(Size.Infinity);
  65. Assert.True(target.IsMeasureValid);
  66. }
  67. }
  68. [Fact]
  69. public void Changing_Inlines_Properties_Should_Invalidate_Measure()
  70. {
  71. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  72. {
  73. var target = new TextBlock();
  74. var inline = new Run("Hello");
  75. target.Inlines.Add(inline);
  76. target.Measure(Size.Infinity);
  77. Assert.True(target.IsMeasureValid);
  78. inline.Foreground = Brushes.Green;
  79. Assert.False(target.IsMeasureValid);
  80. }
  81. }
  82. [Fact]
  83. public void Changing_Inlines_Should_Invalidate_Measure()
  84. {
  85. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  86. {
  87. var target = new TextBlock();
  88. var inlines = new InlineCollection { new Run("Hello") };
  89. target.Measure(Size.Infinity);
  90. Assert.True(target.IsMeasureValid);
  91. target.Inlines = inlines;
  92. Assert.False(target.IsMeasureValid);
  93. }
  94. }
  95. [Fact]
  96. public void Changing_Inlines_Should_Reset_Inlines_Parent()
  97. {
  98. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  99. {
  100. var target = new TextBlock();
  101. var run = new Run("Hello");
  102. target.Inlines.Add(run);
  103. target.Measure(Size.Infinity);
  104. Assert.True(target.IsMeasureValid);
  105. target.Inlines = null;
  106. Assert.Null(run.Parent);
  107. target.Inlines = new InlineCollection { run };
  108. Assert.Equal(target, run.Parent);
  109. }
  110. }
  111. [Fact]
  112. public void InlineUIContainer_Child_Schould_Be_Arranged()
  113. {
  114. using (UnitTestApplication.Start(TestServices.StyledWindow))
  115. {
  116. var target = new TextBlock();
  117. var button = new Button { Content = "12345678" };
  118. button.Template = new FuncControlTemplate<Button>((parent, scope) =>
  119. new TextBlock
  120. {
  121. Name = "PART_ContentPresenter",
  122. [!TextBlock.TextProperty] = parent[!ContentControl.ContentProperty],
  123. }.RegisterInNameScope(scope)
  124. );
  125. target.Inlines!.Add("123456");
  126. target.Inlines.Add(new InlineUIContainer(button));
  127. target.Inlines.Add("123456");
  128. target.Measure(Size.Infinity);
  129. Assert.True(button.IsMeasureValid);
  130. Assert.Equal(80, button.DesiredSize.Width);
  131. target.Arrange(new Rect(new Size(200, 50)));
  132. Assert.True(button.IsArrangeValid);
  133. Assert.Equal(60, button.Bounds.Left);
  134. }
  135. }
  136. [Fact]
  137. public void Setting_Text_Should_Reset_Inlines()
  138. {
  139. using (UnitTestApplication.Start(TestServices.StyledWindow))
  140. {
  141. var target = new TextBlock();
  142. target.Inlines.Add(new Run("Hello World"));
  143. Assert.Equal("Hello World", target.Text);
  144. Assert.Equal(1, target.Inlines.Count);
  145. target.Text = "1234";
  146. Assert.Equal("1234", target.Text);
  147. Assert.Equal(0, target.Inlines.Count);
  148. }
  149. }
  150. }
  151. }