Browse Source

unit test for Listbox OutOfRangeException issue #1395

Andrey Kunchev 7 years ago
parent
commit
be8d4516b9
1 changed files with 35 additions and 0 deletions
  1. 35 0
      tests/Avalonia.Controls.UnitTests/ListBoxTests.cs

+ 35 - 0
tests/Avalonia.Controls.UnitTests/ListBoxTests.cs

@@ -196,6 +196,41 @@ namespace Avalonia.Controls.UnitTests
                 target.Presenter.Panel.Children.Cast<ListBoxItem>().Select(x => (string)x.Content));
         }
 
+
+        [Fact]
+        public void ListBox_After_Scroll_IndexOutOfRangeException_Shouldnt_Be_Thrown()
+        {
+            var items = Enumerable.Range(0, 11).Select(x => $"{x}").ToArray();
+
+            var target = new ListBox
+            {
+                Template = ListBoxTemplate(),
+                Items = items,
+                ItemTemplate = new FuncDataTemplate<string>(x => new TextBlock { Height = 11 })
+            };
+
+            Prepare(target);
+
+            var panel = target.Presenter.Panel as IVirtualizingPanel;
+
+            var listBoxItems = panel.Children.OfType<ListBoxItem>();
+
+            //virtualization should have created exactly 10 items
+            Assert.Equal(10, listBoxItems.Count());
+            Assert.Equal("0", listBoxItems.First().DataContext);
+            Assert.Equal("9", listBoxItems.Last().DataContext);
+
+            //instead pixeloffset > 0 there could be pretty complex sequence for repro
+            //it involves add/remove/scroll to end multiple actions
+            //which i can't find so far :(, but this is the simplest way to add it to unit test
+            panel.PixelOffset = 1;
+
+            //here scroll to end -> IndexOutOfRangeException is thrown
+            target.Scroll.Offset = new Vector(0, 2);
+
+            Assert.True(true);
+        }
+
         private FuncControlTemplate ListBoxTemplate()
         {
             return new FuncControlTemplate<ListBox>(parent =>