ComboBoxTests.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System.Linq;
  2. using System.Reactive.Subjects;
  3. using Avalonia.Controls.Presenters;
  4. using Avalonia.Controls.Primitives;
  5. using Avalonia.Controls.Shapes;
  6. using Avalonia.Controls.Templates;
  7. using Avalonia.Data;
  8. using Avalonia.Input;
  9. using Avalonia.LogicalTree;
  10. using Avalonia.Media;
  11. using Avalonia.Threading;
  12. using Avalonia.UnitTests;
  13. using Xunit;
  14. namespace Avalonia.Controls.UnitTests
  15. {
  16. public class ComboBoxTests
  17. {
  18. MouseTestHelper _helper = new MouseTestHelper();
  19. [Fact]
  20. public void Clicking_On_Control_Toggles_IsDropDownOpen()
  21. {
  22. var target = new ComboBox
  23. {
  24. Items = new[] { "Foo", "Bar" },
  25. };
  26. _helper.Down(target);
  27. _helper.Up(target);
  28. Assert.True(target.IsDropDownOpen);
  29. _helper.Down(target);
  30. _helper.Up(target);
  31. Assert.False(target.IsDropDownOpen);
  32. }
  33. [Fact]
  34. public void SelectionBoxItem_Is_Rectangle_With_VisualBrush_When_Selection_Is_Control()
  35. {
  36. var items = new[] { new Canvas() };
  37. var target = new ComboBox
  38. {
  39. Items = items,
  40. SelectedIndex = 0,
  41. };
  42. var root = new TestRoot(target);
  43. var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle;
  44. Assert.NotNull(rectangle);
  45. var brush = rectangle.Fill as VisualBrush;
  46. Assert.NotNull(brush);
  47. Assert.Same(items[0], brush.Visual);
  48. }
  49. [Fact]
  50. public void SelectionBoxItem_Rectangle_Is_Removed_From_Logical_Tree()
  51. {
  52. var target = new ComboBox
  53. {
  54. Items = new[] { new Canvas() },
  55. SelectedIndex = 0,
  56. Template = GetTemplate(),
  57. };
  58. var root = new TestRoot { Child = target };
  59. target.ApplyTemplate();
  60. target.Presenter.ApplyTemplate();
  61. var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle;
  62. Assert.True(((ILogical)target).IsAttachedToLogicalTree);
  63. Assert.True(((ILogical)rectangle).IsAttachedToLogicalTree);
  64. rectangle.DetachedFromLogicalTree += (s, e) => { };
  65. root.Child = null;
  66. Assert.False(((ILogical)target).IsAttachedToLogicalTree);
  67. Assert.False(((ILogical)rectangle).IsAttachedToLogicalTree);
  68. }
  69. private FuncControlTemplate GetTemplate()
  70. {
  71. return new FuncControlTemplate<ComboBox>((parent, scope) =>
  72. {
  73. return new Panel
  74. {
  75. Name = "container",
  76. Children =
  77. {
  78. new ContentControl
  79. {
  80. [!ContentControl.ContentProperty] = parent[!ComboBox.SelectionBoxItemProperty],
  81. },
  82. new ToggleButton
  83. {
  84. Name = "toggle",
  85. }.RegisterInNameScope(scope),
  86. new Popup
  87. {
  88. Name = "PART_Popup",
  89. Child = new ItemsPresenter
  90. {
  91. Name = "PART_ItemsPresenter",
  92. [!ItemsPresenter.ItemsProperty] = parent[!ComboBox.ItemsProperty],
  93. }.RegisterInNameScope(scope)
  94. }.RegisterInNameScope(scope)
  95. }
  96. };
  97. });
  98. }
  99. [Fact]
  100. public void Detaching_Closed_ComboBox_Keeps_Current_Focus()
  101. {
  102. using (UnitTestApplication.Start(TestServices.RealFocus))
  103. {
  104. var target = new ComboBox
  105. {
  106. Items = new[] { new Canvas() },
  107. SelectedIndex = 0,
  108. Template = GetTemplate(),
  109. };
  110. var other = new Control { Focusable = true };
  111. StackPanel panel;
  112. var root = new TestRoot { Child = panel = new StackPanel { Children = { target, other } } };
  113. target.ApplyTemplate();
  114. target.Presenter.ApplyTemplate();
  115. other.Focus();
  116. Assert.True(other.IsFocused);
  117. panel.Children.Remove(target);
  118. Assert.True(other.IsFocused);
  119. }
  120. }
  121. [Theory]
  122. [InlineData(-1, 2, "c", "A item", "B item", "C item")]
  123. [InlineData(0, 1, "b", "A item", "B item", "C item")]
  124. [InlineData(2, 2, "x", "A item", "B item", "C item")]
  125. public void TextSearch_Should_Have_Expected_SelectedIndex(
  126. int initialSelectedIndex,
  127. int expectedSelectedIndex,
  128. string searchTerm,
  129. params string[] items)
  130. {
  131. using (UnitTestApplication.Start(TestServices.MockThreadingInterface))
  132. {
  133. var target = new ComboBox
  134. {
  135. Template = GetTemplate(),
  136. Items = items.Select(x => new ComboBoxItem { Content = x })
  137. };
  138. target.ApplyTemplate();
  139. target.Presenter.ApplyTemplate();
  140. target.SelectedIndex = initialSelectedIndex;
  141. var args = new TextInputEventArgs
  142. {
  143. Text = searchTerm,
  144. RoutedEvent = InputElement.TextInputEvent
  145. };
  146. target.RaiseEvent(args);
  147. Assert.Equal(expectedSelectedIndex, target.SelectedIndex);
  148. }
  149. }
  150. [Fact]
  151. public void SelectedItem_Validation()
  152. {
  153. using (UnitTestApplication.Start(TestServices.MockThreadingInterface))
  154. {
  155. var target = new ComboBox
  156. {
  157. Template = GetTemplate(),
  158. VirtualizationMode = ItemVirtualizationMode.None
  159. };
  160. target.ApplyTemplate();
  161. target.Presenter.ApplyTemplate();
  162. var exception = new System.InvalidCastException("failed validation");
  163. var textObservable = new BehaviorSubject<BindingNotification>(new BindingNotification(exception, BindingErrorType.DataValidationError));
  164. target.Bind(ComboBox.SelectedItemProperty, textObservable);
  165. Assert.True(DataValidationErrors.GetHasErrors(target));
  166. Assert.True(DataValidationErrors.GetErrors(target).SequenceEqual(new[] { exception }));
  167. }
  168. }
  169. }
  170. }