| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Reactive.Subjects;
- using Avalonia.Controls.Presenters;
- using Avalonia.Controls.Primitives;
- using Avalonia.Controls.Templates;
- using Avalonia.Data;
- using Avalonia.LogicalTree;
- using Avalonia.Threading;
- using Avalonia.VisualTree;
- using Avalonia.UnitTests;
- using Xunit;
- namespace Avalonia.Controls.UnitTests
- {
- public class CarouselTests
- {
- [Fact]
- public void First_Item_Should_Be_Selected_By_Default()
- {
- var target = new Carousel
- {
- Template = new FuncControlTemplate<Carousel>(CreateTemplate), Items = new[] { "Foo", "Bar" }
- };
- target.ApplyTemplate();
- Assert.Equal(0, target.SelectedIndex);
- Assert.Equal("Foo", target.SelectedItem);
- }
- [Fact]
- public void LogicalChild_Should_Be_Selected_Item()
- {
- var target = new Carousel
- {
- Template = new FuncControlTemplate<Carousel>(CreateTemplate), Items = new[] { "Foo", "Bar" }
- };
- target.ApplyTemplate();
- target.Presenter.ApplyTemplate();
- Assert.Single(target.GetLogicalChildren());
- var child = GetContainerTextBlock(target.GetLogicalChildren().Single());
- Assert.Equal("Foo", child.Text);
- }
- [Fact]
- public void Should_Remove_NonCurrent_Page_When_IsVirtualized_True()
- {
- var target = new Carousel
- {
- Template = new FuncControlTemplate<Carousel>(CreateTemplate),
- Items = new[] { "foo", "bar" },
- IsVirtualized = true,
- SelectedIndex = 0,
- };
- target.ApplyTemplate();
- target.Presenter.ApplyTemplate();
- Assert.Single(target.ItemContainerGenerator.Containers);
- target.SelectedIndex = 1;
- Assert.Single(target.ItemContainerGenerator.Containers);
- }
- [Fact]
- public void Selected_Item_Changes_To_First_Item_When_Items_Property_Changes()
- {
- var items = new ObservableCollection<string> { "Foo", "Bar", "FooBar" };
- var target = new Carousel
- {
- Template = new FuncControlTemplate<Carousel>(CreateTemplate), Items = items, IsVirtualized = false
- };
- target.ApplyTemplate();
- target.Presenter.ApplyTemplate();
- Assert.Equal(3, target.GetLogicalChildren().Count());
- var child = GetContainerTextBlock(target.GetLogicalChildren().First());
- Assert.Equal("Foo", child.Text);
- var newItems = items.ToList();
- newItems.RemoveAt(0);
- target.Items = newItems;
- child = GetContainerTextBlock(target.GetLogicalChildren().First());
- Assert.Equal("Bar", child.Text);
- }
- [Fact]
- public void Selected_Item_Changes_To_First_Item_When_Items_Property_Changes_And_Virtualized()
- {
- var items = new ObservableCollection<string> { "Foo", "Bar", "FooBar" };
- var target = new Carousel
- {
- Template = new FuncControlTemplate<Carousel>(CreateTemplate), Items = items, IsVirtualized = true,
- };
- target.ApplyTemplate();
- target.Presenter.ApplyTemplate();
- Assert.Single(target.GetLogicalChildren());
- var child = GetContainerTextBlock(target.GetLogicalChildren().Single());
- Assert.Equal("Foo", child.Text);
- var newItems = items.ToList();
- newItems.RemoveAt(0);
- target.Items = newItems;
- child = GetContainerTextBlock(target.GetLogicalChildren().Single());
- Assert.Equal("Bar", child.Text);
- }
- [Fact]
- public void Selected_Item_Changes_To_First_Item_When_Item_Added()
- {
- var items = new ObservableCollection<string>();
- var target = new Carousel
- {
- Template = new FuncControlTemplate<Carousel>(CreateTemplate), Items = items, IsVirtualized = false
- };
- target.ApplyTemplate();
- target.Presenter.ApplyTemplate();
- Assert.Equal(-1, target.SelectedIndex);
- Assert.Empty(target.GetLogicalChildren());
- items.Add("Foo");
- Assert.Equal(0, target.SelectedIndex);
- Assert.Single(target.GetLogicalChildren());
- }
- [Fact]
- public void Selected_Index_Changes_To_None_When_Items_Assigned_Null()
- {
- var items = new ObservableCollection<string> { "Foo", "Bar", "FooBar" };
- var target = new Carousel
- {
- Template = new FuncControlTemplate<Carousel>(CreateTemplate), Items = items, IsVirtualized = false
- };
- target.ApplyTemplate();
- target.Presenter.ApplyTemplate();
- Assert.Equal(3, target.GetLogicalChildren().Count());
- var child = GetContainerTextBlock(target.GetLogicalChildren().First());
- Assert.Equal("Foo", child.Text);
- target.Items = null;
- var numChildren = target.GetLogicalChildren().Count();
- Assert.Equal(0, numChildren);
- Assert.Equal(-1, target.SelectedIndex);
- }
- [Fact]
- public void Selected_Index_Is_Maintained_Carousel_Created_With_Non_Zero_SelectedIndex()
- {
- var items = new ObservableCollection<string> { "Foo", "Bar", "FooBar" };
- var target = new Carousel
- {
- Template = new FuncControlTemplate<Carousel>(CreateTemplate),
- Items = items,
- IsVirtualized = false,
- SelectedIndex = 2
- };
- target.ApplyTemplate();
- target.Presenter.ApplyTemplate();
- Assert.Equal("FooBar", target.SelectedItem);
- var child = GetContainerTextBlock(target.GetVisualDescendants().LastOrDefault());
- Assert.IsType<TextBlock>(child);
- Assert.Equal("FooBar", ((TextBlock)child).Text);
- }
- [Fact]
- public void Selected_Item_Changes_To_Next_First_Item_When_Item_Removed_From_Beggining_Of_List()
- {
- var items = new ObservableCollection<string> { "Foo", "Bar", "FooBar" };
- var target = new Carousel
- {
- Template = new FuncControlTemplate<Carousel>(CreateTemplate), Items = items, IsVirtualized = false
- };
- target.ApplyTemplate();
- target.Presenter.ApplyTemplate();
- Assert.Equal(3, target.GetLogicalChildren().Count());
- var child = GetContainerTextBlock(target.GetLogicalChildren().First());
- Assert.Equal("Foo", child.Text);
- items.RemoveAt(0);
- child = GetContainerTextBlock(target.GetLogicalChildren().First());
- Assert.IsType<TextBlock>(child);
- Assert.Equal("Bar", ((TextBlock)child).Text);
- }
- [Fact]
- public void Selected_Item_Changes_To_First_Item_If_SelectedItem_Is_Removed_From_Middle()
- {
- var items = new ObservableCollection<string> { "Foo", "Bar", "FooBar" };
- var target = new Carousel
- {
- Template = new FuncControlTemplate<Carousel>(CreateTemplate), Items = items, IsVirtualized = false
- };
- target.ApplyTemplate();
- target.Presenter.ApplyTemplate();
- target.SelectedIndex = 1;
- items.RemoveAt(1);
- Assert.Equal(0, target.SelectedIndex);
- Assert.Equal("Foo", target.SelectedItem);
- }
- private Control CreateTemplate(Carousel control, INameScope scope)
- {
- return new CarouselPresenter
- {
- Name = "PART_ItemsPresenter",
- [~CarouselPresenter.IsVirtualizedProperty] = control[~Carousel.IsVirtualizedProperty],
- [~CarouselPresenter.ItemsProperty] = control[~Carousel.ItemsProperty],
- [~CarouselPresenter.ItemsPanelProperty] = control[~Carousel.ItemsPanelProperty],
- [~CarouselPresenter.SelectedIndexProperty] = control[~Carousel.SelectedIndexProperty],
- [~CarouselPresenter.PageTransitionProperty] = control[~Carousel.PageTransitionProperty],
- }.RegisterInNameScope(scope);
- }
- private static TextBlock GetContainerTextBlock(object control)
- {
- var contentPresenter = Assert.IsType<ContentPresenter>(control);
- contentPresenter.UpdateChild();
- return Assert.IsType<TextBlock>(contentPresenter.Child);
- }
- [Fact]
- public void SelectedItem_Validation()
- {
- using (UnitTestApplication.Start(TestServices.MockThreadingInterface))
- {
- var target = new Carousel
- {
- Template = new FuncControlTemplate<Carousel>(CreateTemplate), IsVirtualized = false
- };
- target.ApplyTemplate();
- target.Presenter.ApplyTemplate();
- var exception = new System.InvalidCastException("failed validation");
- var textObservable =
- new BehaviorSubject<BindingNotification>(new BindingNotification(exception,
- BindingErrorType.DataValidationError));
- target.Bind(ComboBox.SelectedItemProperty, textObservable);
- Dispatcher.UIThread.RunJobs();
- Assert.True(DataValidationErrors.GetHasErrors(target));
- Assert.True(DataValidationErrors.GetErrors(target).SequenceEqual(new[] { exception }));
- }
- }
- }
- }
|