Browse Source

Add failing tests

Benedikt Schroeder 6 years ago
parent
commit
e2ff454085

+ 5 - 5
src/Avalonia.Controls/Primitives/SelectingItemsControl.cs

@@ -367,17 +367,17 @@ namespace Avalonia.Controls.Primitives
             {
             {
                 if ((container.ContainerControl as ISelectable)?.IsSelected == true)
                 if ((container.ContainerControl as ISelectable)?.IsSelected == true)
                 {
                 {
-                    if (SelectionMode.HasFlag(SelectionMode.Multiple))
+                    if (SelectedIndex == -1)
+                    {
+                        SelectedIndex = container.Index;
+                    }
+                    else
                     {
                     {
                         if (_selection.Add(container.Index))
                         if (_selection.Add(container.Index))
                         {
                         {
                             resetSelectedItems = true;
                             resetSelectedItems = true;
                         }
                         }
                     }
                     }
-                    else
-                    {
-                        SelectedIndex = container.Index;
-                    }
 
 
                     MarkContainerSelected(container.ContainerControl, true);
                     MarkContainerSelected(container.ContainerControl, true);
                 }
                 }

+ 37 - 0
tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs

@@ -1185,6 +1185,33 @@ namespace Avalonia.Controls.UnitTests.Primitives
             target.MoveSelection(NavigationDirection.Next, true);
             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<object>
+            {
+                new Item(),
+                second
+            };
+
+            target.Items = items;
+
+            target.ApplyTemplate();
+
+            target.Presenter.ApplyTemplate();
+
+            Assert.Equal(second, target.SelectedItem);
+
+            Assert.Equal(1, target.SelectedIndex);
+        }
+
         private FuncControlTemplate Template()
         private FuncControlTemplate Template()
         {
         {
             return new FuncControlTemplate<SelectingItemsControl>((control, scope) =>
             return new FuncControlTemplate<SelectingItemsControl>((control, scope) =>
@@ -1233,6 +1260,16 @@ namespace Avalonia.Controls.UnitTests.Primitives
 
 
         private class TestSelector : SelectingItemsControl
         private class TestSelector : SelectingItemsControl
         {
         {
+            public TestSelector()
+            {
+                
+            }
+
+            public TestSelector(SelectionMode selectionMode)
+            {
+                SelectionMode = selectionMode;
+            }
+
             public new bool MoveSelection(NavigationDirection direction, bool wrap)
             public new bool MoveSelection(NavigationDirection direction, bool wrap)
             {
             {
                 return base.MoveSelection(direction, wrap);
                 return base.MoveSelection(direction, wrap);