CarouselTests.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. using System.Collections.ObjectModel;
  2. using System.Linq;
  3. using System.Reactive.Subjects;
  4. using Avalonia.Controls.Presenters;
  5. using Avalonia.Controls.Primitives;
  6. using Avalonia.Controls.Templates;
  7. using Avalonia.Data;
  8. using Avalonia.LogicalTree;
  9. using Avalonia.Threading;
  10. using Avalonia.VisualTree;
  11. using Avalonia.UnitTests;
  12. using Xunit;
  13. namespace Avalonia.Controls.UnitTests
  14. {
  15. public class CarouselTests
  16. {
  17. [Fact]
  18. public void First_Item_Should_Be_Selected_By_Default()
  19. {
  20. var target = new Carousel
  21. {
  22. Template = new FuncControlTemplate<Carousel>(CreateTemplate), Items = new[] { "Foo", "Bar" }
  23. };
  24. target.ApplyTemplate();
  25. Assert.Equal(0, target.SelectedIndex);
  26. Assert.Equal("Foo", target.SelectedItem);
  27. }
  28. [Fact]
  29. public void LogicalChild_Should_Be_Selected_Item()
  30. {
  31. var target = new Carousel
  32. {
  33. Template = new FuncControlTemplate<Carousel>(CreateTemplate), Items = new[] { "Foo", "Bar" }
  34. };
  35. target.ApplyTemplate();
  36. target.Presenter.ApplyTemplate();
  37. Assert.Single(target.GetLogicalChildren());
  38. var child = GetContainerTextBlock(target.GetLogicalChildren().Single());
  39. Assert.Equal("Foo", child.Text);
  40. }
  41. [Fact]
  42. public void Should_Remove_NonCurrent_Page_When_IsVirtualized_True()
  43. {
  44. var target = new Carousel
  45. {
  46. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  47. Items = new[] { "foo", "bar" },
  48. IsVirtualized = true,
  49. SelectedIndex = 0,
  50. };
  51. target.ApplyTemplate();
  52. target.Presenter.ApplyTemplate();
  53. Assert.Single(target.ItemContainerGenerator.Containers);
  54. target.SelectedIndex = 1;
  55. Assert.Single(target.ItemContainerGenerator.Containers);
  56. }
  57. [Fact]
  58. public void Selected_Item_Changes_To_First_Item_When_Items_Property_Changes()
  59. {
  60. var items = new ObservableCollection<string> { "Foo", "Bar", "FooBar" };
  61. var target = new Carousel
  62. {
  63. Template = new FuncControlTemplate<Carousel>(CreateTemplate), Items = items, IsVirtualized = false
  64. };
  65. target.ApplyTemplate();
  66. target.Presenter.ApplyTemplate();
  67. Assert.Equal(3, target.GetLogicalChildren().Count());
  68. var child = GetContainerTextBlock(target.GetLogicalChildren().First());
  69. Assert.Equal("Foo", child.Text);
  70. var newItems = items.ToList();
  71. newItems.RemoveAt(0);
  72. target.Items = newItems;
  73. child = GetContainerTextBlock(target.GetLogicalChildren().First());
  74. Assert.Equal("Bar", child.Text);
  75. }
  76. [Fact]
  77. public void Selected_Item_Changes_To_First_Item_When_Items_Property_Changes_And_Virtualized()
  78. {
  79. var items = new ObservableCollection<string> { "Foo", "Bar", "FooBar" };
  80. var target = new Carousel
  81. {
  82. Template = new FuncControlTemplate<Carousel>(CreateTemplate), Items = items, IsVirtualized = true,
  83. };
  84. target.ApplyTemplate();
  85. target.Presenter.ApplyTemplate();
  86. Assert.Single(target.GetLogicalChildren());
  87. var child = GetContainerTextBlock(target.GetLogicalChildren().Single());
  88. Assert.Equal("Foo", child.Text);
  89. var newItems = items.ToList();
  90. newItems.RemoveAt(0);
  91. target.Items = newItems;
  92. child = GetContainerTextBlock(target.GetLogicalChildren().Single());
  93. Assert.Equal("Bar", child.Text);
  94. }
  95. [Fact]
  96. public void Selected_Item_Changes_To_First_Item_When_Item_Added()
  97. {
  98. var items = new ObservableCollection<string>();
  99. var target = new Carousel
  100. {
  101. Template = new FuncControlTemplate<Carousel>(CreateTemplate), Items = items, IsVirtualized = false
  102. };
  103. target.ApplyTemplate();
  104. target.Presenter.ApplyTemplate();
  105. Assert.Equal(-1, target.SelectedIndex);
  106. Assert.Empty(target.GetLogicalChildren());
  107. items.Add("Foo");
  108. Assert.Equal(0, target.SelectedIndex);
  109. Assert.Single(target.GetLogicalChildren());
  110. }
  111. [Fact]
  112. public void Selected_Index_Changes_To_None_When_Items_Assigned_Null()
  113. {
  114. var items = new ObservableCollection<string> { "Foo", "Bar", "FooBar" };
  115. var target = new Carousel
  116. {
  117. Template = new FuncControlTemplate<Carousel>(CreateTemplate), Items = items, IsVirtualized = false
  118. };
  119. target.ApplyTemplate();
  120. target.Presenter.ApplyTemplate();
  121. Assert.Equal(3, target.GetLogicalChildren().Count());
  122. var child = GetContainerTextBlock(target.GetLogicalChildren().First());
  123. Assert.Equal("Foo", child.Text);
  124. target.Items = null;
  125. var numChildren = target.GetLogicalChildren().Count();
  126. Assert.Equal(0, numChildren);
  127. Assert.Equal(-1, target.SelectedIndex);
  128. }
  129. [Fact]
  130. public void Selected_Index_Is_Maintained_Carousel_Created_With_Non_Zero_SelectedIndex()
  131. {
  132. var items = new ObservableCollection<string> { "Foo", "Bar", "FooBar" };
  133. var target = new Carousel
  134. {
  135. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  136. Items = items,
  137. IsVirtualized = false,
  138. SelectedIndex = 2
  139. };
  140. target.ApplyTemplate();
  141. target.Presenter.ApplyTemplate();
  142. Assert.Equal("FooBar", target.SelectedItem);
  143. var child = GetContainerTextBlock(target.GetVisualDescendants().LastOrDefault());
  144. Assert.IsType<TextBlock>(child);
  145. Assert.Equal("FooBar", ((TextBlock)child).Text);
  146. }
  147. [Fact]
  148. public void Selected_Item_Changes_To_Next_First_Item_When_Item_Removed_From_Beggining_Of_List()
  149. {
  150. var items = new ObservableCollection<string> { "Foo", "Bar", "FooBar" };
  151. var target = new Carousel
  152. {
  153. Template = new FuncControlTemplate<Carousel>(CreateTemplate), Items = items, IsVirtualized = false
  154. };
  155. target.ApplyTemplate();
  156. target.Presenter.ApplyTemplate();
  157. Assert.Equal(3, target.GetLogicalChildren().Count());
  158. var child = GetContainerTextBlock(target.GetLogicalChildren().First());
  159. Assert.Equal("Foo", child.Text);
  160. items.RemoveAt(0);
  161. child = GetContainerTextBlock(target.GetLogicalChildren().First());
  162. Assert.IsType<TextBlock>(child);
  163. Assert.Equal("Bar", ((TextBlock)child).Text);
  164. }
  165. [Fact]
  166. public void Selected_Item_Changes_To_First_Item_If_SelectedItem_Is_Removed_From_Middle()
  167. {
  168. var items = new ObservableCollection<string> { "Foo", "Bar", "FooBar" };
  169. var target = new Carousel
  170. {
  171. Template = new FuncControlTemplate<Carousel>(CreateTemplate), Items = items, IsVirtualized = false
  172. };
  173. target.ApplyTemplate();
  174. target.Presenter.ApplyTemplate();
  175. target.SelectedIndex = 1;
  176. items.RemoveAt(1);
  177. Assert.Equal(0, target.SelectedIndex);
  178. Assert.Equal("Foo", target.SelectedItem);
  179. }
  180. private Control CreateTemplate(Carousel control, INameScope scope)
  181. {
  182. return new CarouselPresenter
  183. {
  184. Name = "PART_ItemsPresenter",
  185. [~CarouselPresenter.IsVirtualizedProperty] = control[~Carousel.IsVirtualizedProperty],
  186. [~CarouselPresenter.ItemsProperty] = control[~Carousel.ItemsProperty],
  187. [~CarouselPresenter.ItemsPanelProperty] = control[~Carousel.ItemsPanelProperty],
  188. [~CarouselPresenter.SelectedIndexProperty] = control[~Carousel.SelectedIndexProperty],
  189. [~CarouselPresenter.PageTransitionProperty] = control[~Carousel.PageTransitionProperty],
  190. }.RegisterInNameScope(scope);
  191. }
  192. private static TextBlock GetContainerTextBlock(object control)
  193. {
  194. var contentPresenter = Assert.IsType<ContentPresenter>(control);
  195. contentPresenter.UpdateChild();
  196. return Assert.IsType<TextBlock>(contentPresenter.Child);
  197. }
  198. [Fact]
  199. public void SelectedItem_Validation()
  200. {
  201. using (UnitTestApplication.Start(TestServices.MockThreadingInterface))
  202. {
  203. var target = new Carousel
  204. {
  205. Template = new FuncControlTemplate<Carousel>(CreateTemplate), IsVirtualized = false
  206. };
  207. target.ApplyTemplate();
  208. target.Presenter.ApplyTemplate();
  209. var exception = new System.InvalidCastException("failed validation");
  210. var textObservable =
  211. new BehaviorSubject<BindingNotification>(new BindingNotification(exception,
  212. BindingErrorType.DataValidationError));
  213. target.Bind(ComboBox.SelectedItemProperty, textObservable);
  214. Dispatcher.UIThread.RunJobs();
  215. Assert.True(DataValidationErrors.GetHasErrors(target));
  216. Assert.True(DataValidationErrors.GetErrors(target).SequenceEqual(new[] { exception }));
  217. }
  218. }
  219. }
  220. }