|
|
|
@ -8,6 +8,7 @@ using Avalonia.Controls.Presenters; |
|
|
|
using Avalonia.Controls.Templates; |
|
|
|
using Xunit; |
|
|
|
using System.Collections.ObjectModel; |
|
|
|
using System.Collections; |
|
|
|
|
|
|
|
namespace Avalonia.Controls.UnitTests.Presenters |
|
|
|
{ |
|
|
|
@ -49,148 +50,672 @@ namespace Avalonia.Controls.UnitTests.Presenters |
|
|
|
Assert.IsType<ItemContainerGenerator<TestItem>>(target.ItemContainerGenerator); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Setting_SelectedIndex_Should_Show_Page() |
|
|
|
public class Virtualized |
|
|
|
{ |
|
|
|
var target = new CarouselPresenter |
|
|
|
[Fact] |
|
|
|
public void Should_Initially_Materialize_Selected_Container() |
|
|
|
{ |
|
|
|
Items = new[] { "foo", "bar" }, |
|
|
|
SelectedIndex = 0, |
|
|
|
}; |
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = new[] { "foo", "bar" }, |
|
|
|
SelectedIndex = 0, |
|
|
|
IsVirtualized = true, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
target.ApplyTemplate(); |
|
|
|
|
|
|
|
Assert.IsType<ContentPresenter>(target.Panel.Children[0]); |
|
|
|
Assert.Equal("foo", ((ContentPresenter)target.Panel.Children[0]).Content); |
|
|
|
} |
|
|
|
AssertSingle(target); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Changing_SelectedIndex_Should_Show_Page() |
|
|
|
{ |
|
|
|
var target = new CarouselPresenter |
|
|
|
[Fact] |
|
|
|
public void Should_Initially_Materialize_Nothing_If_No_Selected_Container() |
|
|
|
{ |
|
|
|
Items = new[] { "foo", "bar" }, |
|
|
|
SelectedIndex = 0, |
|
|
|
}; |
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = new[] { "foo", "bar" }, |
|
|
|
IsVirtualized = true, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
target.SelectedIndex = 1; |
|
|
|
target.ApplyTemplate(); |
|
|
|
|
|
|
|
Assert.IsType<ContentPresenter>(target.Panel.Children[0]); |
|
|
|
Assert.Equal("bar", ((ContentPresenter)target.Panel.Children[0]).Content); |
|
|
|
} |
|
|
|
Assert.Empty(target.Panel.Children); |
|
|
|
Assert.Empty(target.ItemContainerGenerator.Containers); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_Remove_NonCurrent_Page_When_IsVirtualized_True() |
|
|
|
{ |
|
|
|
var target = new CarouselPresenter |
|
|
|
[Fact] |
|
|
|
public void Switching_To_Virtualized_Should_Reset_Containers() |
|
|
|
{ |
|
|
|
Items = new[] { "foo", "bar" }, |
|
|
|
IsVirtualized = true, |
|
|
|
SelectedIndex = 0, |
|
|
|
}; |
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = new[] { "foo", "bar" }, |
|
|
|
SelectedIndex = 0, |
|
|
|
IsVirtualized = false, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
Assert.Single(target.ItemContainerGenerator.Containers); |
|
|
|
target.SelectedIndex = 1; |
|
|
|
Assert.Single(target.ItemContainerGenerator.Containers); |
|
|
|
} |
|
|
|
target.ApplyTemplate(); |
|
|
|
target.IsVirtualized = true; |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_Not_Remove_NonCurrent_Page_When_IsVirtualized_False() |
|
|
|
{ |
|
|
|
var target = new CarouselPresenter |
|
|
|
AssertSingle(target); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Changing_SelectedIndex_Should_Show_Page() |
|
|
|
{ |
|
|
|
Items = new[] { "foo", "bar" }, |
|
|
|
IsVirtualized = false, |
|
|
|
SelectedIndex = 0, |
|
|
|
}; |
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = new[] { "foo", "bar" }, |
|
|
|
SelectedIndex = 0, |
|
|
|
IsVirtualized = true, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
AssertSingle(target); |
|
|
|
|
|
|
|
target.SelectedIndex = 1; |
|
|
|
AssertSingle(target); |
|
|
|
} |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
Assert.Single(target.ItemContainerGenerator.Containers); |
|
|
|
Assert.Single(target.Panel.Children); |
|
|
|
target.SelectedIndex = 1; |
|
|
|
Assert.Equal(2, target.ItemContainerGenerator.Containers.Count()); |
|
|
|
Assert.Equal(2, target.Panel.Children.Count); |
|
|
|
target.SelectedIndex = 0; |
|
|
|
Assert.Equal(2, target.ItemContainerGenerator.Containers.Count()); |
|
|
|
Assert.Equal(2, target.Panel.Children.Count); |
|
|
|
} |
|
|
|
[Fact] |
|
|
|
public void Should_Remove_NonCurrent_Page() |
|
|
|
{ |
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = new[] { "foo", "bar" }, |
|
|
|
IsVirtualized = true, |
|
|
|
SelectedIndex = 0, |
|
|
|
}; |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_Remove_Controls_When_IsVirtualized_Is_False() |
|
|
|
{ |
|
|
|
ObservableCollection<string> items = new ObservableCollection<string>(); |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
target.ApplyTemplate(); |
|
|
|
AssertSingle(target); |
|
|
|
|
|
|
|
target.SelectedIndex = 1; |
|
|
|
AssertSingle(target); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_Handle_Inserting_Item_At_SelectedItem() |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 0, |
|
|
|
IsVirtualized = false, |
|
|
|
}; |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"item0", |
|
|
|
"item1", |
|
|
|
"item2", |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 1, |
|
|
|
IsVirtualized = true, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
|
|
|
|
items.Insert(1, "item1a"); |
|
|
|
AssertSingle(target); |
|
|
|
} |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
target.SelectedIndex = 0; |
|
|
|
items.Add("foo"); |
|
|
|
target.SelectedIndex = 0; |
|
|
|
[Fact] |
|
|
|
public void Should_Handle_Inserting_Item_Before_SelectedItem() |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"item0", |
|
|
|
"item1", |
|
|
|
"item2", |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 2, |
|
|
|
IsVirtualized = true, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
|
|
|
|
items.Insert(1, "item1a"); |
|
|
|
AssertSingle(target); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_Do_Nothing_When_Inserting_Item_After_SelectedItem() |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"item0", |
|
|
|
"item1", |
|
|
|
"item2", |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 1, |
|
|
|
IsVirtualized = true, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
var child = AssertSingle(target); |
|
|
|
items.Insert(2, "after"); |
|
|
|
Assert.Same(child, AssertSingle(target)); |
|
|
|
} |
|
|
|
|
|
|
|
Assert.Single(target.ItemContainerGenerator.Containers); |
|
|
|
Assert.Single(target.Panel.Children); |
|
|
|
[Fact] |
|
|
|
public void Should_Handle_Removing_Item_At_SelectedItem() |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"item0", |
|
|
|
"item1", |
|
|
|
"item2", |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 1, |
|
|
|
IsVirtualized = true, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
|
|
|
|
items.RemoveAt(1); |
|
|
|
AssertSingle(target); |
|
|
|
} |
|
|
|
|
|
|
|
items.Add("bar"); |
|
|
|
Assert.Single(target.ItemContainerGenerator.Containers); |
|
|
|
Assert.Single(target.Panel.Children); |
|
|
|
[Fact] |
|
|
|
public void Should_Handle_Removing_Item_Before_SelectedItem() |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"item0", |
|
|
|
"item1", |
|
|
|
"item2", |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 1, |
|
|
|
IsVirtualized = true, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
|
|
|
|
items.RemoveAt(0); |
|
|
|
AssertSingle(target); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_Do_Nothing_When_Removing_Item_After_SelectedItem() |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"item0", |
|
|
|
"item1", |
|
|
|
"item2", |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 1, |
|
|
|
IsVirtualized = true, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
var child = AssertSingle(target); |
|
|
|
items.RemoveAt(2); |
|
|
|
Assert.Same(child, AssertSingle(target)); |
|
|
|
} |
|
|
|
|
|
|
|
target.SelectedIndex = 1; |
|
|
|
Assert.Equal(2, target.ItemContainerGenerator.Containers.Count()); |
|
|
|
Assert.Equal(2, target.Panel.Children.Count); |
|
|
|
[Fact] |
|
|
|
public void Should_Handle_Removing_SelectedItem_When_Its_Last() |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"item0", |
|
|
|
"item1", |
|
|
|
"item2", |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 2, |
|
|
|
IsVirtualized = true, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
|
|
|
|
items.RemoveAt(2); |
|
|
|
Assert.Equal(1, target.SelectedIndex); |
|
|
|
AssertSingle(target); |
|
|
|
} |
|
|
|
|
|
|
|
items.Remove(items[0]); |
|
|
|
Assert.Single(target.ItemContainerGenerator.Containers); |
|
|
|
Assert.Single(target.Panel.Children); |
|
|
|
[Fact] |
|
|
|
public void Should_Handle_Removing_Last_Item() |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"item0", |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 0, |
|
|
|
IsVirtualized = true, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
|
|
|
|
items.RemoveAt(0); |
|
|
|
Assert.Empty(target.Panel.Children); |
|
|
|
Assert.Empty(target.ItemContainerGenerator.Containers); |
|
|
|
} |
|
|
|
|
|
|
|
items.Remove(items[0]); |
|
|
|
Assert.Empty(target.ItemContainerGenerator.Containers); |
|
|
|
Assert.Empty(target.Panel.Children); |
|
|
|
[Fact] |
|
|
|
public void Should_Handle_Replacing_SelectedItem() |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"item0", |
|
|
|
"item1", |
|
|
|
"item2", |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 1, |
|
|
|
IsVirtualized = true, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
|
|
|
|
items[1] = "replaced"; |
|
|
|
AssertSingle(target); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_Do_Nothing_When_Replacing_Non_SelectedItem() |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"item0", |
|
|
|
"item1", |
|
|
|
"item2", |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 1, |
|
|
|
IsVirtualized = true, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
var child = AssertSingle(target); |
|
|
|
items[0] = "replaced"; |
|
|
|
Assert.Same(child, AssertSingle(target)); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_Handle_Moving_SelectedItem() |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"item0", |
|
|
|
"item1", |
|
|
|
"item2", |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 1, |
|
|
|
IsVirtualized = true, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
|
|
|
|
items.Move(1, 0); |
|
|
|
AssertSingle(target); |
|
|
|
} |
|
|
|
|
|
|
|
private static IControl AssertSingle(CarouselPresenter target) |
|
|
|
{ |
|
|
|
var items = (IList)target.Items; |
|
|
|
var index = target.SelectedIndex; |
|
|
|
var content = items[index]; |
|
|
|
var child = Assert.Single(target.Panel.Children); |
|
|
|
var presenter = Assert.IsType<ContentPresenter>(child); |
|
|
|
var container = Assert.Single(target.ItemContainerGenerator.Containers); |
|
|
|
var visible = Assert.Single(target.Panel.Children.Where(x => x.IsVisible)); |
|
|
|
|
|
|
|
Assert.Same(child, container.ContainerControl); |
|
|
|
Assert.Same(child, visible); |
|
|
|
Assert.Equal(content, presenter.Content); |
|
|
|
Assert.Equal(content, container.Item); |
|
|
|
Assert.Equal(index, container.Index); |
|
|
|
|
|
|
|
return child; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_Have_Correct_ItemsContainer_Index() |
|
|
|
public class NonVirtualized |
|
|
|
{ |
|
|
|
ObservableCollection<string> items = new ObservableCollection<string>(); |
|
|
|
[Fact] |
|
|
|
public void Should_Initially_Materialize_All_Containers() |
|
|
|
{ |
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = new[] { "foo", "bar" }, |
|
|
|
IsVirtualized = false, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
AssertAll(target); |
|
|
|
} |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
[Fact] |
|
|
|
public void Should_Initially_Show_Selected_Item() |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 0, |
|
|
|
IsVirtualized = false, |
|
|
|
}; |
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = new[] { "foo", "bar" }, |
|
|
|
SelectedIndex = 1, |
|
|
|
IsVirtualized = false, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
AssertAll(target); |
|
|
|
} |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
target.SelectedIndex = 0; |
|
|
|
items.Add("foo"); |
|
|
|
target.SelectedIndex = 0; |
|
|
|
|
|
|
|
Assert.Single(target.ItemContainerGenerator.Containers); |
|
|
|
Assert.Single(target.Panel.Children); |
|
|
|
|
|
|
|
items.Add("bar"); |
|
|
|
Assert.Single(target.ItemContainerGenerator.Containers); |
|
|
|
Assert.Single(target.Panel.Children); |
|
|
|
|
|
|
|
target.SelectedIndex = 1; |
|
|
|
Assert.Equal(2, target.ItemContainerGenerator.Containers.Count()); |
|
|
|
Assert.Equal(2, target.Panel.Children.Count); |
|
|
|
Assert.Equal(0, target.ItemContainerGenerator.Containers.First().Index); |
|
|
|
|
|
|
|
items.Remove(items[0]); |
|
|
|
Assert.Single(target.ItemContainerGenerator.Containers); |
|
|
|
Assert.Single(target.Panel.Children); |
|
|
|
Assert.Equal(0, target.ItemContainerGenerator.Containers.First().Index); |
|
|
|
|
|
|
|
items.Remove(items[0]); |
|
|
|
Assert.Empty(target.ItemContainerGenerator.Containers); |
|
|
|
Assert.Empty(target.Panel.Children); |
|
|
|
[Fact] |
|
|
|
public void Switching_To_Non_Virtualized_Should_Reset_Containers() |
|
|
|
{ |
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = new[] { "foo", "bar" }, |
|
|
|
SelectedIndex = 0, |
|
|
|
IsVirtualized = true, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
target.IsVirtualized = false; |
|
|
|
|
|
|
|
AssertAll(target); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Changing_SelectedIndex_Should_Show_Page() |
|
|
|
{ |
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = new[] { "foo", "bar" }, |
|
|
|
SelectedIndex = 0, |
|
|
|
IsVirtualized = false, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
AssertAll(target); |
|
|
|
|
|
|
|
target.SelectedIndex = 1; |
|
|
|
AssertAll(target); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_Handle_Inserting_Item_At_SelectedItem() |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"item0", |
|
|
|
"item1", |
|
|
|
"item2", |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 1, |
|
|
|
IsVirtualized = false, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
|
|
|
|
items.Insert(1, "item1a"); |
|
|
|
AssertAll(target); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_Handle_Inserting_Item_Before_SelectedItem() |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"item0", |
|
|
|
"item1", |
|
|
|
"item2", |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 2, |
|
|
|
IsVirtualized = false, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
|
|
|
|
items.Insert(1, "item1a"); |
|
|
|
AssertAll(target); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_Do_Handle_Inserting_Item_After_SelectedItem() |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"item0", |
|
|
|
"item1", |
|
|
|
"item2", |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 1, |
|
|
|
IsVirtualized = false, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
items.Insert(2, "after"); |
|
|
|
AssertAll(target); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_Handle_Removing_Item_At_SelectedItem() |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"item0", |
|
|
|
"item1", |
|
|
|
"item2", |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 1, |
|
|
|
IsVirtualized = false, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
|
|
|
|
items.RemoveAt(1); |
|
|
|
AssertAll(target); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_Handle_Removing_Item_Before_SelectedItem() |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"item0", |
|
|
|
"item1", |
|
|
|
"item2", |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 1, |
|
|
|
IsVirtualized = false, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
|
|
|
|
items.RemoveAt(0); |
|
|
|
AssertAll(target); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_Handle_Removing_Item_After_SelectedItem() |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"item0", |
|
|
|
"item1", |
|
|
|
"item2", |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 1, |
|
|
|
IsVirtualized = false, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
items.RemoveAt(2); |
|
|
|
AssertAll(target); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_Handle_Removing_SelectedItem_When_Its_Last() |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"item0", |
|
|
|
"item1", |
|
|
|
"item2", |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 2, |
|
|
|
IsVirtualized = false, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
|
|
|
|
items.RemoveAt(2); |
|
|
|
Assert.Equal(1, target.SelectedIndex); |
|
|
|
AssertAll(target); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_Handle_Removing_Last_Item() |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"item0", |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 0, |
|
|
|
IsVirtualized = false, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
|
|
|
|
items.RemoveAt(0); |
|
|
|
Assert.Empty(target.Panel.Children); |
|
|
|
Assert.Empty(target.ItemContainerGenerator.Containers); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_Handle_Replacing_SelectedItem() |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"item0", |
|
|
|
"item1", |
|
|
|
"item2", |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 1, |
|
|
|
IsVirtualized = false, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
|
|
|
|
items[1] = "replaced"; |
|
|
|
AssertAll(target); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_Handle_Moving_SelectedItem() |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"item0", |
|
|
|
"item1", |
|
|
|
"item2", |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new CarouselPresenter |
|
|
|
{ |
|
|
|
Items = items, |
|
|
|
SelectedIndex = 1, |
|
|
|
IsVirtualized = false, |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
|
|
|
|
items.Move(1, 0); |
|
|
|
AssertAll(target); |
|
|
|
} |
|
|
|
|
|
|
|
private static void AssertAll(CarouselPresenter target) |
|
|
|
{ |
|
|
|
var items = (IList)target.Items; |
|
|
|
|
|
|
|
Assert.Equal(items?.Count ?? 0, target.Panel.Children.Count); |
|
|
|
Assert.Equal(items?.Count ?? 0, target.ItemContainerGenerator.Containers.Count()); |
|
|
|
|
|
|
|
for (var i = 0; i < items?.Count; ++i) |
|
|
|
{ |
|
|
|
var content = items[i]; |
|
|
|
var child = target.Panel.Children[i]; |
|
|
|
var presenter = Assert.IsType<ContentPresenter>(child); |
|
|
|
var container = target.ItemContainerGenerator.ContainerFromIndex(i); |
|
|
|
|
|
|
|
Assert.Same(child, container); |
|
|
|
Assert.Equal(i == target.SelectedIndex, child.IsVisible); |
|
|
|
Assert.Equal(content, presenter.Content); |
|
|
|
Assert.Equal(i, target.ItemContainerGenerator.IndexFromContainer(container)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private class TestItem : ContentControl |
|
|
|
|