From e2ff454085da9d67aba8c180b508295d97c673c8 Mon Sep 17 00:00:00 2001 From: Benedikt Schroeder Date: Wed, 6 Nov 2019 07:52:17 +0100 Subject: [PATCH] Add failing tests --- .../Primitives/SelectingItemsControl.cs | 10 ++--- .../Primitives/SelectingItemsControlTests.cs | 37 +++++++++++++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs index 329b086a7c..b6ae567123 100644 --- a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs +++ b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs @@ -367,17 +367,17 @@ namespace Avalonia.Controls.Primitives { if ((container.ContainerControl as ISelectable)?.IsSelected == true) { - if (SelectionMode.HasFlag(SelectionMode.Multiple)) + if (SelectedIndex == -1) + { + SelectedIndex = container.Index; + } + else { if (_selection.Add(container.Index)) { resetSelectedItems = true; } } - else - { - SelectedIndex = container.Index; - } MarkContainerSelected(container.ContainerControl, true); } diff --git a/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs b/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs index 6df89ba444..d819581000 100644 --- a/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs +++ b/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs @@ -1185,6 +1185,33 @@ namespace Avalonia.Controls.UnitTests.Primitives target.MoveSelection(NavigationDirection.Next, true); } + [Fact] + public void Pre_Selecting_Item_Should_Set_Selection_After_It_Was_Added_When_AlwaysSelected() + { + var target = new TestSelector(SelectionMode.AlwaysSelected) + { + Template = Template() + }; + + var second = new Item { IsSelected = true }; + + var items = new AvaloniaList + { + new Item(), + second + }; + + target.Items = items; + + target.ApplyTemplate(); + + target.Presenter.ApplyTemplate(); + + Assert.Equal(second, target.SelectedItem); + + Assert.Equal(1, target.SelectedIndex); + } + private FuncControlTemplate Template() { return new FuncControlTemplate((control, scope) => @@ -1233,6 +1260,16 @@ namespace Avalonia.Controls.UnitTests.Primitives private class TestSelector : SelectingItemsControl { + public TestSelector() + { + + } + + public TestSelector(SelectionMode selectionMode) + { + SelectionMode = selectionMode; + } + public new bool MoveSelection(NavigationDirection direction, bool wrap) { return base.MoveSelection(direction, wrap);