From 96187d76e193e5ed3264ed65354ef8ee4dececd7 Mon Sep 17 00:00:00 2001 From: Takoooooo Date: Mon, 15 Aug 2022 15:20:34 +0300 Subject: [PATCH] Fix ListBoxTests. --- .../ListBoxTests.cs | 98 ++++++++++--------- 1 file changed, 53 insertions(+), 45 deletions(-) diff --git a/tests/Avalonia.Controls.UnitTests/ListBoxTests.cs b/tests/Avalonia.Controls.UnitTests/ListBoxTests.cs index 7c57e22933b..f1486ab7e75 100644 --- a/tests/Avalonia.Controls.UnitTests/ListBoxTests.cs +++ b/tests/Avalonia.Controls.UnitTests/ListBoxTests.cs @@ -7,11 +7,13 @@ using Avalonia.Controls.Templates; using Avalonia.Data; using Avalonia.Input; +using Avalonia.Input.Platform; using Avalonia.LogicalTree; using Avalonia.Styling; using Avalonia.Threading; using Avalonia.UnitTests; using Avalonia.VisualTree; +using Moq; using Xunit; namespace Avalonia.Controls.UnitTests @@ -221,30 +223,33 @@ public void Containers_Correct_After_Clear_Add_Remove() [Fact] public void Toggle_Selection_Should_Update_Containers() { - var items = Enumerable.Range(0, 10).Select(x => $"Item {x}").ToArray(); - var target = new ListBox + using (UnitTestApplication.Start()) { - Template = ListBoxTemplate(), - Items = items, - SelectionMode = SelectionMode.Toggle, - ItemTemplate = new FuncDataTemplate((x, _) => new TextBlock { Height = 10 }) - }; - - Prepare(target); + var items = Enumerable.Range(0, 10).Select(x => $"Item {x}").ToArray(); + var target = new ListBox + { + Template = ListBoxTemplate(), + Items = items, + SelectionMode = SelectionMode.Toggle, + ItemTemplate = new FuncDataTemplate((x, _) => new TextBlock { Height = 10 }) + }; + AvaloniaLocator.CurrentMutable.Bind().ToConstant(new Mock().Object); + Prepare(target); - var lbItems = target.GetLogicalChildren().OfType().ToArray(); + var lbItems = target.GetLogicalChildren().OfType().ToArray(); - var item = lbItems[0]; + var item = lbItems[0]; - Assert.Equal(false, item.IsSelected); + Assert.Equal(false, item.IsSelected); - RaisePressedEvent(target, item, MouseButton.Left); + RaisePressedEvent(target, item, MouseButton.Left); - Assert.Equal(true, item.IsSelected); + Assert.Equal(true, item.IsSelected); - RaisePressedEvent(target, item, MouseButton.Left); + RaisePressedEvent(target, item, MouseButton.Left); - Assert.Equal(false, item.IsSelected); + Assert.Equal(false, item.IsSelected); + } } [Fact] @@ -374,41 +379,44 @@ public void LayoutManager_Should_Measure_Arrange_All() [Fact] public void Clicking_Item_Should_Raise_BringIntoView_For_Correct_Control() { - // Issue #3934 - var items = Enumerable.Range(0, 10).Select(x => $"Item {x}").ToArray(); - var target = new ListBox + using (UnitTestApplication.Start()) { - Template = ListBoxTemplate(), - Items = items, - ItemTemplate = new FuncDataTemplate((x, _) => new TextBlock { Height = 10 }), - SelectionMode = SelectionMode.AlwaysSelected, - VirtualizationMode = ItemVirtualizationMode.None, - }; - - Prepare(target); + // Issue #3934 + var items = Enumerable.Range(0, 10).Select(x => $"Item {x}").ToArray(); + var target = new ListBox + { + Template = ListBoxTemplate(), + Items = items, + ItemTemplate = new FuncDataTemplate((x, _) => new TextBlock { Height = 10 }), + SelectionMode = SelectionMode.AlwaysSelected, + VirtualizationMode = ItemVirtualizationMode.None, + }; + AvaloniaLocator.CurrentMutable.Bind().ToConstant(new Mock().Object); + Prepare(target); - // First an item that is not index 0 must be selected. - _mouse.Click(target.Presenter.Panel.Children[1]); - Assert.Equal(1, target.Selection.AnchorIndex); + // First an item that is not index 0 must be selected. + _mouse.Click(target.Presenter.Panel.Children[1]); + Assert.Equal(1, target.Selection.AnchorIndex); - // We're going to be clicking on item 9. - var item = (ListBoxItem)target.Presenter.Panel.Children[9]; - var raised = 0; + // We're going to be clicking on item 9. + var item = (ListBoxItem)target.Presenter.Panel.Children[9]; + var raised = 0; - // Make sure a RequestBringIntoView event is raised for item 9. It won't be handled - // by the ScrollContentPresenter as the item is already visible, so we don't need - // handledEventsToo: true. Issue #3934 failed here because item 0 was being scrolled - // into view due to SelectionMode.AlwaysSelected. - target.AddHandler(Control.RequestBringIntoViewEvent, (s, e) => - { - Assert.Same(item, e.TargetObject); - ++raised; - }); + // Make sure a RequestBringIntoView event is raised for item 9. It won't be handled + // by the ScrollContentPresenter as the item is already visible, so we don't need + // handledEventsToo: true. Issue #3934 failed here because item 0 was being scrolled + // into view due to SelectionMode.AlwaysSelected. + target.AddHandler(Control.RequestBringIntoViewEvent, (s, e) => + { + Assert.Same(item, e.TargetObject); + ++raised; + }); - // Click item 9. - _mouse.Click(item); + // Click item 9. + _mouse.Click(item); - Assert.Equal(1, raised); + Assert.Equal(1, raised); + } } [Fact]