diff --git a/tests/Avalonia.Controls.UnitTests/ListBoxTests.cs b/tests/Avalonia.Controls.UnitTests/ListBoxTests.cs index 1debccd3c5..bc563c25e2 100644 --- a/tests/Avalonia.Controls.UnitTests/ListBoxTests.cs +++ b/tests/Avalonia.Controls.UnitTests/ListBoxTests.cs @@ -196,6 +196,41 @@ namespace Avalonia.Controls.UnitTests target.Presenter.Panel.Children.Cast().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(x => new TextBlock { Height = 11 }) + }; + + Prepare(target); + + var panel = target.Presenter.Panel as IVirtualizingPanel; + + var listBoxItems = panel.Children.OfType(); + + //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(parent =>