| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- using System.Linq;
- using System.Reactive.Subjects;
- using Avalonia.Controls.Presenters;
- using Avalonia.Controls.Primitives;
- using Avalonia.Controls.Shapes;
- using Avalonia.Controls.Templates;
- using Avalonia.Data;
- using Avalonia.Input;
- using Avalonia.LogicalTree;
- using Avalonia.Media;
- using Avalonia.VisualTree;
- using Avalonia.UnitTests;
- using Xunit;
- namespace Avalonia.Controls.UnitTests
- {
- public class ComboBoxTests
- {
- MouseTestHelper _helper = new MouseTestHelper();
-
- [Fact]
- public void Clicking_On_Control_Toggles_IsDropDownOpen()
- {
- var target = new ComboBox
- {
- Items = new[] { "Foo", "Bar" },
- };
- _helper.Down(target);
- _helper.Up(target);
- Assert.True(target.IsDropDownOpen);
- _helper.Down(target);
- _helper.Up(target);
- Assert.False(target.IsDropDownOpen);
- }
- [Fact]
- public void WrapSelection_Should_Work()
- {
- using (UnitTestApplication.Start(TestServices.RealFocus))
- {
- var items = new[]
- {
- new ComboBoxItem() { Content = "bla" },
- new ComboBoxItem() { Content = "dd" },
- new ComboBoxItem() { Content = "sdf", IsEnabled = false }
- };
- var target = new ComboBox
- {
- Items = items,
- Template = GetTemplate(),
- WrapSelection = true
- };
- var root = new TestRoot(target);
- target.ApplyTemplate();
- target.Presenter.ApplyTemplate();
- target.Focus();
- Assert.Equal(target.SelectedIndex, -1);
- Assert.True(target.IsFocused);
- target.RaiseEvent(new KeyEventArgs
- {
- RoutedEvent = InputElement.KeyDownEvent,
- Key = Key.Up,
- });
- Assert.Equal(target.SelectedIndex, 1);
- target.RaiseEvent(new KeyEventArgs
- {
- RoutedEvent = InputElement.KeyDownEvent,
- Key = Key.Down,
- });
- Assert.Equal(target.SelectedIndex, 0);
- }
- }
- [Fact]
- public void Focuses_Next_Item_On_Key_Down()
- {
- using (UnitTestApplication.Start(TestServices.RealFocus))
- {
- var items = new[]
- {
- new ComboBoxItem() { Content = "bla" },
- new ComboBoxItem() { Content = "dd", IsEnabled = false },
- new ComboBoxItem() { Content = "sdf" }
- };
- var target = new ComboBox
- {
- Items = items,
- Template = GetTemplate()
- };
- var root = new TestRoot(target);
- target.ApplyTemplate();
- target.Presenter.ApplyTemplate();
- target.Focus();
- Assert.Equal(target.SelectedIndex, -1);
- Assert.True(target.IsFocused);
- target.RaiseEvent(new KeyEventArgs
- {
- RoutedEvent = InputElement.KeyDownEvent,
- Key = Key.Down,
- });
- Assert.Equal(target.SelectedIndex, 0);
- target.RaiseEvent(new KeyEventArgs
- {
- RoutedEvent = InputElement.KeyDownEvent,
- Key = Key.Down,
- });
- Assert.Equal(target.SelectedIndex, 2);
- }
- }
- [Fact]
- public void SelectionBoxItem_Is_Rectangle_With_VisualBrush_When_Selection_Is_Control()
- {
- var items = new[] { new Canvas() };
- var target = new ComboBox
- {
- Items = items,
- SelectedIndex = 0,
- };
- var root = new TestRoot(target);
- var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle;
- Assert.NotNull(rectangle);
- var brush = rectangle.Fill as VisualBrush;
- Assert.NotNull(brush);
- Assert.Same(items[0], brush.Visual);
- }
- [Fact]
- public void SelectionBoxItem_Rectangle_Is_Removed_From_Logical_Tree()
- {
- var target = new ComboBox
- {
- Items = new[] { new Canvas() },
- SelectedIndex = 0,
- Template = GetTemplate(),
- };
- var root = new TestRoot { Child = target };
- target.ApplyTemplate();
- target.Presenter.ApplyTemplate();
- var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle;
- Assert.True(((ILogical)target).IsAttachedToLogicalTree);
- Assert.True(((ILogical)rectangle).IsAttachedToLogicalTree);
- rectangle.DetachedFromLogicalTree += (s, e) => { };
- root.Child = null;
- Assert.False(((ILogical)target).IsAttachedToLogicalTree);
- Assert.False(((ILogical)rectangle).IsAttachedToLogicalTree);
- }
- private FuncControlTemplate GetTemplate()
- {
- return new FuncControlTemplate<ComboBox>((parent, scope) =>
- {
- return new Panel
- {
- Name = "container",
- Children =
- {
- new ContentControl
- {
- [!ContentControl.ContentProperty] = parent[!ComboBox.SelectionBoxItemProperty],
- },
- new ToggleButton
- {
- Name = "toggle",
- }.RegisterInNameScope(scope),
- new Popup
- {
- Name = "PART_Popup",
- Child = new ItemsPresenter
- {
- Name = "PART_ItemsPresenter",
- [!ItemsPresenter.ItemsProperty] = parent[!ComboBox.ItemsProperty],
- }.RegisterInNameScope(scope)
- }.RegisterInNameScope(scope)
- }
- };
- });
- }
- [Fact]
- public void Detaching_Closed_ComboBox_Keeps_Current_Focus()
- {
- using (UnitTestApplication.Start(TestServices.RealFocus))
- {
- var target = new ComboBox
- {
- Items = new[] { new Canvas() },
- SelectedIndex = 0,
- Template = GetTemplate(),
- };
- var other = new Control { Focusable = true };
- StackPanel panel;
- var root = new TestRoot { Child = panel = new StackPanel { Children = { target, other } } };
- target.ApplyTemplate();
- target.Presenter.ApplyTemplate();
- other.Focus();
- Assert.True(other.IsFocused);
- panel.Children.Remove(target);
- Assert.True(other.IsFocused);
- }
- }
- [Theory]
- [InlineData(-1, 2, "c", "A item", "B item", "C item")]
- [InlineData(0, 1, "b", "A item", "B item", "C item")]
- [InlineData(2, 2, "x", "A item", "B item", "C item")]
- public void TextSearch_Should_Have_Expected_SelectedIndex(
- int initialSelectedIndex,
- int expectedSelectedIndex,
- string searchTerm,
- params string[] items)
- {
- using (UnitTestApplication.Start(TestServices.MockThreadingInterface))
- {
- var target = new ComboBox
- {
- Template = GetTemplate(),
- Items = items.Select(x => new ComboBoxItem { Content = x })
- };
- target.ApplyTemplate();
- target.Presenter.ApplyTemplate();
- target.SelectedIndex = initialSelectedIndex;
- var args = new TextInputEventArgs
- {
- Text = searchTerm,
- RoutedEvent = InputElement.TextInputEvent
- };
- target.RaiseEvent(args);
- Assert.Equal(expectedSelectedIndex, target.SelectedIndex);
- }
- }
-
- [Fact]
- public void SelectedItem_Validation()
- {
- using (UnitTestApplication.Start(TestServices.MockThreadingInterface))
- {
- var target = new ComboBox
- {
- Template = GetTemplate(),
- VirtualizationMode = ItemVirtualizationMode.None
- };
- target.ApplyTemplate();
- target.Presenter.ApplyTemplate();
-
- var exception = new System.InvalidCastException("failed validation");
- var textObservable = new BehaviorSubject<BindingNotification>(new BindingNotification(exception, BindingErrorType.DataValidationError));
- target.Bind(ComboBox.SelectedItemProperty, textObservable);
- Assert.True(DataValidationErrors.GetHasErrors(target));
- Assert.True(DataValidationErrors.GetErrors(target).SequenceEqual(new[] { exception }));
-
- }
-
- }
- [Fact]
- public void Close_Window_On_Alt_F4_When_ComboBox_Is_Focus()
- {
- var inputManagerMock = new Moq.Mock<IInputManager>();
- var services = TestServices.StyledWindow.With(inputManager: inputManagerMock.Object);
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var window = new Window();
- window.KeyDown += (s, e) =>
- {
- if (e.Handled == false
- && e.KeyModifiers.HasAllFlags(KeyModifiers.Alt) == true
- && e.Key == Key.F4 )
- {
- e.Handled = true;
- window.Close();
- }
- };
- var count = 0;
- var target = new ComboBox
- {
- Items = new[] { new Canvas() },
- SelectedIndex = 0,
- Template = GetTemplate(),
- };
- window.Content = target;
- window.Closing +=
- (sender, e) =>
- {
- count++;
- };
- window.Show();
- target.Focus();
- _helper.Down(target);
- _helper.Up(target);
- Assert.True(target.IsDropDownOpen);
- target.RaiseEvent(new KeyEventArgs
- {
- RoutedEvent = InputElement.KeyDownEvent,
- KeyModifiers = KeyModifiers.Alt,
- Key = Key.F4
- });
- Assert.Equal(1, count);
- }
- }
- [Fact]
- public void FlowDirection_Of_RectangleContent_Shuold_Be_LeftToRight()
- {
- var items = new[]
- {
- new ComboBoxItem()
- {
- Content = new Control()
- }
- };
- var target = new ComboBox
- {
- FlowDirection = FlowDirection.RightToLeft,
- Items = items,
- Template = GetTemplate()
- };
- var root = new TestRoot(target);
- target.ApplyTemplate();
- target.SelectedIndex = 0;
- var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle;
- Assert.Equal(FlowDirection.LeftToRight, rectangle.FlowDirection);
- }
- [Fact]
- public void FlowDirection_Of_RectangleContent_Updated_After_InvalidateMirrorTransform()
- {
- var parentContent = new Decorator()
- {
- Child = new Control()
- };
- var items = new[]
- {
- new ComboBoxItem()
- {
- Content = parentContent.Child
- }
- };
- var target = new ComboBox
- {
- Items = items,
- Template = GetTemplate()
- };
- var root = new TestRoot(target);
- target.ApplyTemplate();
- target.SelectedIndex = 0;
- var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle;
- Assert.Equal(FlowDirection.LeftToRight, rectangle.FlowDirection);
- parentContent.FlowDirection = FlowDirection.RightToLeft;
- target.FlowDirection = FlowDirection.RightToLeft;
-
- Assert.Equal(FlowDirection.RightToLeft, rectangle.FlowDirection);
- }
- [Fact]
- public void FlowDirection_Of_RectangleContent_Updated_After_OpenPopup()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var parentContent = new Decorator()
- {
- Child = new Control()
- };
- var items = new[]
- {
- new ComboBoxItem()
- {
- Content = parentContent.Child
- }
- };
- var target = new ComboBox
- {
- FlowDirection = FlowDirection.RightToLeft,
- Items = items,
- Template = GetTemplate()
- };
- var root = new TestRoot(target);
- target.ApplyTemplate();
- target.SelectedIndex = 0;
- var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle;
- Assert.Equal(FlowDirection.LeftToRight, rectangle.FlowDirection);
- parentContent.FlowDirection = FlowDirection.RightToLeft;
- var popup = target.GetVisualDescendants().OfType<Popup>().First();
- popup.PlacementTarget = new Window();
- popup.Open();
-
- Assert.Equal(FlowDirection.RightToLeft, rectangle.FlowDirection);
- }
- }
- }
- }
|