TextBoxTests.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System.Threading.Tasks;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.Presenters;
  4. using Avalonia.Controls.Templates;
  5. using Avalonia.Layout;
  6. using Avalonia.Media;
  7. using Xunit;
  8. namespace Avalonia.Skia.RenderTests
  9. {
  10. public class TextBoxTests : TestBase
  11. {
  12. public TextBoxTests()
  13. : base(@"Controls\TextBox")
  14. {
  15. }
  16. private static IControlTemplate CreateTextBoxTemplate()
  17. {
  18. return new FuncControlTemplate<TextBox>((textBox, scope) =>
  19. {
  20. var border = new Border
  21. {
  22. Background = textBox.Background,
  23. BorderBrush = Brushes.Gray,
  24. BorderThickness = new Thickness(1),
  25. Padding = new Thickness(4),
  26. };
  27. var panel = new Panel();
  28. // Use Antialias mode
  29. TextOptions.SetTextRenderingMode(panel, TextRenderingMode.Antialias);
  30. var placeholder = new TextBlock
  31. {
  32. Name = "PART_Placeholder",
  33. [!TextBlock.TextProperty] = textBox[!TextBox.PlaceholderTextProperty],
  34. [!TextBlock.ForegroundProperty] = textBox[!TextBox.PlaceholderForegroundProperty],
  35. FontFamily = textBox.FontFamily,
  36. FontSize = textBox.FontSize,
  37. VerticalAlignment = VerticalAlignment.Center,
  38. Opacity = 0.5,
  39. }.RegisterInNameScope(scope);
  40. var presenter = new TextPresenter
  41. {
  42. Name = "PART_TextPresenter",
  43. [!TextPresenter.TextProperty] = textBox[!TextBox.TextProperty],
  44. [!TextPresenter.CaretIndexProperty] = textBox[!TextBox.CaretIndexProperty],
  45. FontFamily = textBox.FontFamily,
  46. FontSize = textBox.FontSize,
  47. }.RegisterInNameScope(scope);
  48. panel.Children.Add(placeholder);
  49. panel.Children.Add(presenter);
  50. border.Child = panel;
  51. return border;
  52. });
  53. }
  54. [Fact]
  55. public async Task Placeholder_With_Red_Foreground()
  56. {
  57. var target = new Border
  58. {
  59. Padding = new Thickness(8),
  60. Width = 200,
  61. Height = 50,
  62. Background = Brushes.White,
  63. Child = new TextBox
  64. {
  65. Template = CreateTextBoxTemplate(),
  66. FontFamily = TestFontFamily,
  67. FontSize = 12,
  68. Background = Brushes.White,
  69. PlaceholderText = "Red placeholder",
  70. PlaceholderForeground = Brushes.Red,
  71. }
  72. };
  73. await RenderToFile(target);
  74. CompareImages();
  75. }
  76. [Fact]
  77. public async Task Placeholder_With_Blue_Foreground()
  78. {
  79. var target = new Border
  80. {
  81. Padding = new Thickness(8),
  82. Width = 200,
  83. Height = 50,
  84. Background = Brushes.White,
  85. Child = new TextBox
  86. {
  87. Template = CreateTextBoxTemplate(),
  88. FontFamily = TestFontFamily,
  89. FontSize = 12,
  90. Background = Brushes.White,
  91. PlaceholderText = "Blue placeholder",
  92. PlaceholderForeground = Brushes.Blue,
  93. }
  94. };
  95. await RenderToFile(target);
  96. CompareImages();
  97. }
  98. [Fact]
  99. public async Task Placeholder_With_Default_Foreground()
  100. {
  101. var target = new Border
  102. {
  103. Padding = new Thickness(8),
  104. Width = 200,
  105. Height = 50,
  106. Background = Brushes.White,
  107. Child = new TextBox
  108. {
  109. Template = CreateTextBoxTemplate(),
  110. FontFamily = TestFontFamily,
  111. FontSize = 12,
  112. Background = Brushes.White,
  113. PlaceholderText = "Default placeholder",
  114. PlaceholderForeground = Brushes.Gray,
  115. }
  116. };
  117. await RenderToFile(target);
  118. CompareImages();
  119. }
  120. }
  121. }