From 65d57262b2eaa90022c4b42fe71429d1e59631db Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Fri, 1 Jun 2018 11:45:59 +0100 Subject: [PATCH] Add unit tests to demonstrate the issues with Carousel --- .../CarouselTests.cs | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/tests/Avalonia.Controls.UnitTests/CarouselTests.cs b/tests/Avalonia.Controls.UnitTests/CarouselTests.cs index 4d38ec9856..79d573bd1b 100644 --- a/tests/Avalonia.Controls.UnitTests/CarouselTests.cs +++ b/tests/Avalonia.Controls.UnitTests/CarouselTests.cs @@ -1,6 +1,7 @@ // Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. +using System.Collections.ObjectModel; using System.Linq; using Avalonia.Controls.Presenters; using Avalonia.Controls.Primitives; @@ -93,6 +94,79 @@ namespace Avalonia.Controls.UnitTests Assert.Equal(2, target.ItemContainerGenerator.Containers.Count()); } + [Fact] + public void Selected_Item_Changes_To_First_Item_When_Items_Property_Changes() + { + var items = new ObservableCollection + { + "Foo", + "Bar", + "FooBar" + }; + + var target = new Carousel + { + Template = new FuncControlTemplate(CreateTemplate), + Items = items, + IsVirtualized = false + }; + + target.ApplyTemplate(); + target.Presenter.ApplyTemplate(); + + Assert.Single(target.GetLogicalChildren()); + + var child = target.GetLogicalChildren().Single(); + + Assert.IsType(child); + Assert.Equal("Foo", ((TextBlock)child).Text); + + var newItems = items.ToList(); + newItems.RemoveAt(0); + + target.Items = newItems; + + child = target.GetLogicalChildren().Single(); + + Assert.IsType(child); + Assert.Equal("Bar", ((TextBlock)child).Text); + } + + [Fact] + public void Selected_Item_Changes_To_Next_First_Item_When_Item_Removed_From_Beggining_Of_List() + { + var items = new ObservableCollection + { + "Foo", + "Bar", + "FooBar" + }; + + var target = new Carousel + { + Template = new FuncControlTemplate(CreateTemplate), + Items = items, + IsVirtualized = false + }; + + target.ApplyTemplate(); + target.Presenter.ApplyTemplate(); + + Assert.Single(target.GetLogicalChildren()); + + var child = target.GetLogicalChildren().Single(); + + Assert.IsType(child); + Assert.Equal("Foo", ((TextBlock)child).Text); + + items.RemoveAt(0); + + child = target.GetLogicalChildren().Single(); + + Assert.IsType(child); + Assert.Equal("Bar", ((TextBlock)child).Text); + } + private Control CreateTemplate(Carousel control) { return new CarouselPresenter