|
@@ -17,76 +17,6 @@ namespace Avalonia.Controls.UnitTests
|
|
|
{
|
|
|
private MouseTestHelper _helper = new MouseTestHelper();
|
|
|
|
|
|
- [Fact]
|
|
|
- public void Focusing_Item_With_Shift_And_Arrow_Key_Should_Add_To_Selection()
|
|
|
- {
|
|
|
- var target = new ListBox
|
|
|
- {
|
|
|
- Template = new FuncControlTemplate(CreateListBoxTemplate),
|
|
|
- ItemsSource = new[] { "Foo", "Bar", "Baz " },
|
|
|
- SelectionMode = SelectionMode.Multiple
|
|
|
- };
|
|
|
-
|
|
|
- ApplyTemplate(target);
|
|
|
-
|
|
|
- target.SelectedItem = "Foo";
|
|
|
-
|
|
|
- target.Presenter.Panel.Children[1].RaiseEvent(new GotFocusEventArgs
|
|
|
- {
|
|
|
- NavigationMethod = NavigationMethod.Directional,
|
|
|
- KeyModifiers = KeyModifiers.Shift
|
|
|
- });
|
|
|
-
|
|
|
- Assert.Equal(new[] { "Foo", "Bar" }, target.SelectedItems);
|
|
|
- }
|
|
|
-
|
|
|
- [Fact]
|
|
|
- public void Focusing_Item_With_Ctrl_And_Arrow_Key_Should_Not_Add_To_Selection()
|
|
|
- {
|
|
|
- var target = new ListBox
|
|
|
- {
|
|
|
- Template = new FuncControlTemplate(CreateListBoxTemplate),
|
|
|
- ItemsSource = new[] { "Foo", "Bar", "Baz " },
|
|
|
- SelectionMode = SelectionMode.Multiple
|
|
|
- };
|
|
|
-
|
|
|
- ApplyTemplate(target);
|
|
|
-
|
|
|
- target.SelectedItem = "Foo";
|
|
|
-
|
|
|
- target.Presenter.Panel.Children[1].RaiseEvent(new GotFocusEventArgs
|
|
|
- {
|
|
|
- NavigationMethod = NavigationMethod.Directional,
|
|
|
- KeyModifiers = KeyModifiers.Control
|
|
|
- });
|
|
|
-
|
|
|
- Assert.Equal(new[] { "Foo" }, target.SelectedItems);
|
|
|
- }
|
|
|
-
|
|
|
- [Fact]
|
|
|
- public void Focusing_Selected_Item_With_Ctrl_And_Arrow_Key_Should_Not_Remove_From_Selection()
|
|
|
- {
|
|
|
- var target = new ListBox
|
|
|
- {
|
|
|
- Template = new FuncControlTemplate(CreateListBoxTemplate),
|
|
|
- ItemsSource = new[] { "Foo", "Bar", "Baz " },
|
|
|
- SelectionMode = SelectionMode.Multiple
|
|
|
- };
|
|
|
-
|
|
|
- ApplyTemplate(target);
|
|
|
-
|
|
|
- target.SelectedItems.Add("Foo");
|
|
|
- target.SelectedItems.Add("Bar");
|
|
|
-
|
|
|
- target.Presenter.Panel.Children[0].RaiseEvent(new GotFocusEventArgs
|
|
|
- {
|
|
|
- NavigationMethod = NavigationMethod.Directional,
|
|
|
- KeyModifiers = KeyModifiers.Control
|
|
|
- });
|
|
|
-
|
|
|
- Assert.Equal(new[] { "Foo", "Bar" }, target.SelectedItems);
|
|
|
- }
|
|
|
-
|
|
|
[Fact]
|
|
|
public void Shift_Selecting_From_No_Selection_Selects_From_Start()
|
|
|
{
|
|
@@ -549,6 +479,104 @@ namespace Avalonia.Controls.UnitTests
|
|
|
Assert.Equal(1, target.SelectedItems.Count);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ [Fact]
|
|
|
+ public void Shift_Arrow_Key_Selects_Range()
|
|
|
+ {
|
|
|
+ using var app = UnitTestApplication.Start(TestServices.RealFocus);
|
|
|
+ var target = new ListBox
|
|
|
+ {
|
|
|
+ Template = new FuncControlTemplate(CreateListBoxTemplate),
|
|
|
+ ItemsSource = new[] { "Foo", "Bar", "Baz" },
|
|
|
+ SelectionMode = SelectionMode.Multiple,
|
|
|
+ Width = 100,
|
|
|
+ Height = 100,
|
|
|
+ SelectedIndex = 0,
|
|
|
+ };
|
|
|
+
|
|
|
+ var root = new TestRoot(target);
|
|
|
+ root.LayoutManager.ExecuteInitialLayoutPass();
|
|
|
+
|
|
|
+ RaiseKeyEvent(target, Key.Down, KeyModifiers.Shift);
|
|
|
+
|
|
|
+ Assert.Equal(new[] { "Foo", "Bar", }, target.SelectedItems);
|
|
|
+ Assert.Equal(new[] { 0, 1 }, SelectedContainers(target));
|
|
|
+ Assert.True(target.ContainerFromIndex(1).IsFocused);
|
|
|
+
|
|
|
+ RaiseKeyEvent(target, Key.Down, KeyModifiers.Shift);
|
|
|
+
|
|
|
+ Assert.Equal(new[] { "Foo", "Bar", "Baz", }, target.SelectedItems);
|
|
|
+ Assert.Equal(new[] { 0, 1, 2 }, SelectedContainers(target));
|
|
|
+ Assert.True(target.ContainerFromIndex(2).IsFocused);
|
|
|
+
|
|
|
+ RaiseKeyEvent(target, Key.Up, KeyModifiers.Shift);
|
|
|
+
|
|
|
+ Assert.Equal(new[] { "Foo", "Bar", }, target.SelectedItems);
|
|
|
+ Assert.Equal(new[] { 0, 1 }, SelectedContainers(target));
|
|
|
+ Assert.True(target.ContainerFromIndex(1).IsFocused);
|
|
|
+ }
|
|
|
+
|
|
|
+ [Fact]
|
|
|
+ public void Shift_Down_Key_Selecting_Selects_Range_End_From_Focus()
|
|
|
+ {
|
|
|
+ using var app = UnitTestApplication.Start(TestServices.RealFocus);
|
|
|
+ var target = new ListBox
|
|
|
+ {
|
|
|
+ Template = new FuncControlTemplate(CreateListBoxTemplate),
|
|
|
+ ItemsSource = new[] { "Foo", "Bar", "Baz" },
|
|
|
+ SelectionMode = SelectionMode.Multiple,
|
|
|
+ Width = 100,
|
|
|
+ Height = 100,
|
|
|
+ SelectedIndex = 0,
|
|
|
+ };
|
|
|
+
|
|
|
+ var root = new TestRoot(target);
|
|
|
+ root.LayoutManager.ExecuteInitialLayoutPass();
|
|
|
+
|
|
|
+ target.ContainerFromIndex(1)!.Focus();
|
|
|
+ RaiseKeyEvent(target, Key.Down, KeyModifiers.Shift);
|
|
|
+
|
|
|
+ Assert.Equal(new[] { "Foo", "Bar", "Baz" }, target.SelectedItems);
|
|
|
+ Assert.Equal(new[] { 0, 1, 2 }, SelectedContainers(target));
|
|
|
+ Assert.True(target.ContainerFromIndex(2).IsFocused);
|
|
|
+ }
|
|
|
+
|
|
|
+ [Fact]
|
|
|
+ public void Shift_Down_Key_Selecting_Selects_Range_End_From_Focus_Moved_With_Ctrl_Key()
|
|
|
+ {
|
|
|
+ using var app = UnitTestApplication.Start(TestServices.RealFocus);
|
|
|
+ var target = new ListBox
|
|
|
+ {
|
|
|
+ Template = new FuncControlTemplate(CreateListBoxTemplate),
|
|
|
+ ItemsSource = new[] { "Foo", "Bar", "Baz", "Qux" },
|
|
|
+ SelectionMode = SelectionMode.Multiple,
|
|
|
+ Width = 100,
|
|
|
+ Height = 100,
|
|
|
+ SelectedIndex = 0,
|
|
|
+ };
|
|
|
+
|
|
|
+ var root = new TestRoot(target);
|
|
|
+ root.LayoutManager.ExecuteInitialLayoutPass();
|
|
|
+
|
|
|
+ RaiseKeyEvent(target, Key.Down, KeyModifiers.Shift);
|
|
|
+
|
|
|
+ Assert.Equal(new[] { "Foo", "Bar" }, target.SelectedItems);
|
|
|
+ Assert.Equal(new[] { 0, 1 }, SelectedContainers(target));
|
|
|
+ Assert.True(target.ContainerFromIndex(1).IsFocused);
|
|
|
+
|
|
|
+ RaiseKeyEvent(target, Key.Down, KeyModifiers.Control);
|
|
|
+
|
|
|
+ Assert.Equal(new[] { "Foo", "Bar" }, target.SelectedItems);
|
|
|
+ Assert.Equal(new[] { 0, 1 }, SelectedContainers(target));
|
|
|
+ Assert.True(target.ContainerFromIndex(2).IsFocused);
|
|
|
+
|
|
|
+ RaiseKeyEvent(target, Key.Down, KeyModifiers.Shift);
|
|
|
+
|
|
|
+ Assert.Equal(new[] { "Foo", "Bar", "Baz", "Qux" }, target.SelectedItems);
|
|
|
+ Assert.Equal(new[] { 0, 1, 2, 3 }, SelectedContainers(target));
|
|
|
+ Assert.True(target.ContainerFromIndex(3).IsFocused);
|
|
|
+ }
|
|
|
+
|
|
|
private Control CreateListBoxTemplate(TemplatedControl parent, INameScope scope)
|
|
|
{
|
|
|
return new ScrollViewer
|
|
@@ -571,20 +599,14 @@ namespace Avalonia.Controls.UnitTests
|
|
|
}.RegisterInNameScope(scope);
|
|
|
}
|
|
|
|
|
|
- private static void ApplyTemplate(ListBox target)
|
|
|
+ private static void RaiseKeyEvent(Control target, Key key, KeyModifiers inputModifiers = 0)
|
|
|
{
|
|
|
- // Apply the template to the ListBox itself.
|
|
|
- target.ApplyTemplate();
|
|
|
-
|
|
|
- // Then to its inner ScrollViewer.
|
|
|
- var scrollViewer = (ScrollViewer)target.GetVisualChildren().Single();
|
|
|
- scrollViewer.ApplyTemplate();
|
|
|
-
|
|
|
- // Then make the ScrollViewer create its child.
|
|
|
- ((ContentPresenter)scrollViewer.Presenter).UpdateChild();
|
|
|
-
|
|
|
- // Now the ItemsPresenter should be reigstered, so apply its template.
|
|
|
- ((Control)target.Presenter).ApplyTemplate();
|
|
|
+ target.RaiseEvent(new KeyEventArgs
|
|
|
+ {
|
|
|
+ RoutedEvent = InputElement.KeyDownEvent,
|
|
|
+ KeyModifiers = inputModifiers,
|
|
|
+ Key = key
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
private static IEnumerable<int> SelectedContainers(SelectingItemsControl target)
|