CarouselTests.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using Avalonia.Controls.Presenters;
  6. using Avalonia.Controls.Primitives;
  7. using Avalonia.Controls.Templates;
  8. using Avalonia.LogicalTree;
  9. using Avalonia.VisualTree;
  10. using Xunit;
  11. namespace Avalonia.Controls.UnitTests
  12. {
  13. public class CarouselTests
  14. {
  15. [Fact]
  16. public void First_Item_Should_Be_Selected_By_Default()
  17. {
  18. var target = new Carousel
  19. {
  20. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  21. Items = new[]
  22. {
  23. "Foo",
  24. "Bar"
  25. }
  26. };
  27. target.ApplyTemplate();
  28. Assert.Equal(0, target.SelectedIndex);
  29. Assert.Equal("Foo", target.SelectedItem);
  30. }
  31. [Fact]
  32. public void LogicalChild_Should_Be_Selected_Item()
  33. {
  34. var target = new Carousel
  35. {
  36. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  37. Items = new[]
  38. {
  39. "Foo",
  40. "Bar"
  41. }
  42. };
  43. target.ApplyTemplate();
  44. target.Presenter.ApplyTemplate();
  45. Assert.Single(target.GetLogicalChildren());
  46. var child = GetContainerTextBlock(target.GetLogicalChildren().Single());
  47. Assert.Equal("Foo", child.Text);
  48. }
  49. [Fact]
  50. public void Should_Remove_NonCurrent_Page_When_IsVirtualized_True()
  51. {
  52. var target = new Carousel
  53. {
  54. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  55. Items = new[] { "foo", "bar" },
  56. IsVirtualized = true,
  57. SelectedIndex = 0,
  58. };
  59. target.ApplyTemplate();
  60. target.Presenter.ApplyTemplate();
  61. Assert.Single(target.ItemContainerGenerator.Containers);
  62. target.SelectedIndex = 1;
  63. Assert.Single(target.ItemContainerGenerator.Containers);
  64. }
  65. [Fact]
  66. public void Selected_Item_Changes_To_First_Item_When_Items_Property_Changes()
  67. {
  68. var items = new ObservableCollection<string>
  69. {
  70. "Foo",
  71. "Bar",
  72. "FooBar"
  73. };
  74. var target = new Carousel
  75. {
  76. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  77. Items = items,
  78. IsVirtualized = false
  79. };
  80. target.ApplyTemplate();
  81. target.Presenter.ApplyTemplate();
  82. Assert.Equal(3, target.GetLogicalChildren().Count());
  83. var child = GetContainerTextBlock(target.GetLogicalChildren().First());
  84. Assert.Equal("Foo", child.Text);
  85. var newItems = items.ToList();
  86. newItems.RemoveAt(0);
  87. target.Items = newItems;
  88. child = GetContainerTextBlock(target.GetLogicalChildren().First());
  89. Assert.Equal("Bar", child.Text);
  90. }
  91. [Fact]
  92. public void Selected_Item_Changes_To_First_Item_When_Items_Property_Changes_And_Virtualized()
  93. {
  94. var items = new ObservableCollection<string>
  95. {
  96. "Foo",
  97. "Bar",
  98. "FooBar"
  99. };
  100. var target = new Carousel
  101. {
  102. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  103. Items = items,
  104. IsVirtualized = true,
  105. };
  106. target.ApplyTemplate();
  107. target.Presenter.ApplyTemplate();
  108. Assert.Single(target.GetLogicalChildren());
  109. var child = GetContainerTextBlock(target.GetLogicalChildren().Single());
  110. Assert.Equal("Foo", child.Text);
  111. var newItems = items.ToList();
  112. newItems.RemoveAt(0);
  113. target.Items = newItems;
  114. child = GetContainerTextBlock(target.GetLogicalChildren().Single());
  115. Assert.Equal("Bar", child.Text);
  116. }
  117. [Fact]
  118. public void Selected_Item_Changes_To_First_Item_When_Item_Added()
  119. {
  120. var items = new ObservableCollection<string>();
  121. var target = new Carousel
  122. {
  123. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  124. Items = items,
  125. IsVirtualized = false
  126. };
  127. target.ApplyTemplate();
  128. target.Presenter.ApplyTemplate();
  129. Assert.Equal(-1, target.SelectedIndex);
  130. Assert.Empty(target.GetLogicalChildren());
  131. items.Add("Foo");
  132. Assert.Equal(0, target.SelectedIndex);
  133. Assert.Single(target.GetLogicalChildren());
  134. }
  135. [Fact]
  136. public void Selected_Index_Changes_To_When_Items_Assigned_Null()
  137. {
  138. var items = new ObservableCollection<string>
  139. {
  140. "Foo",
  141. "Bar",
  142. "FooBar"
  143. };
  144. var target = new Carousel
  145. {
  146. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  147. Items = items,
  148. IsVirtualized = false
  149. };
  150. target.ApplyTemplate();
  151. target.Presenter.ApplyTemplate();
  152. Assert.Equal(3, target.GetLogicalChildren().Count());
  153. var child = GetContainerTextBlock(target.GetLogicalChildren().First());
  154. Assert.Equal("Foo", child.Text);
  155. target.Items = null;
  156. var numChildren = target.GetLogicalChildren().Count();
  157. Assert.Equal(0, numChildren);
  158. Assert.Equal(-1, target.SelectedIndex);
  159. }
  160. [Fact]
  161. public void Selected_Index_Is_Maintained_Carousel_Created_With_Non_Zero_SelectedIndex()
  162. {
  163. var items = new ObservableCollection<string>
  164. {
  165. "Foo",
  166. "Bar",
  167. "FooBar"
  168. };
  169. var target = new Carousel
  170. {
  171. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  172. Items = items,
  173. IsVirtualized = false,
  174. SelectedIndex = 2
  175. };
  176. target.ApplyTemplate();
  177. target.Presenter.ApplyTemplate();
  178. Assert.Equal("FooBar", target.SelectedItem);
  179. var child = GetContainerTextBlock(target.GetVisualDescendants().LastOrDefault());
  180. Assert.IsType<TextBlock>(child);
  181. Assert.Equal("FooBar", ((TextBlock)child).Text);
  182. }
  183. [Fact]
  184. public void Selected_Item_Changes_To_Next_First_Item_When_Item_Removed_From_Beggining_Of_List()
  185. {
  186. var items = new ObservableCollection<string>
  187. {
  188. "Foo",
  189. "Bar",
  190. "FooBar"
  191. };
  192. var target = new Carousel
  193. {
  194. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  195. Items = items,
  196. IsVirtualized = false
  197. };
  198. target.ApplyTemplate();
  199. target.Presenter.ApplyTemplate();
  200. Assert.Equal(3, target.GetLogicalChildren().Count());
  201. var child = GetContainerTextBlock(target.GetLogicalChildren().First());
  202. Assert.Equal("Foo", child.Text);
  203. items.RemoveAt(0);
  204. child = GetContainerTextBlock(target.GetLogicalChildren().First());
  205. Assert.IsType<TextBlock>(child);
  206. Assert.Equal("Bar", ((TextBlock)child).Text);
  207. }
  208. [Fact]
  209. public void Selected_Item_Changes_To_NextAvailable_Item_If_SelectedItem_Is_Removed_From_Middle()
  210. {
  211. var items = new ObservableCollection<string>
  212. {
  213. "Foo",
  214. "Bar",
  215. "FooBar"
  216. };
  217. var target = new Carousel
  218. {
  219. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  220. Items = items,
  221. IsVirtualized = false
  222. };
  223. target.ApplyTemplate();
  224. target.Presenter.ApplyTemplate();
  225. target.SelectedIndex = 1;
  226. items.RemoveAt(1);
  227. Assert.Equal(1, target.SelectedIndex);
  228. Assert.Equal("FooBar", target.SelectedItem);
  229. }
  230. private Control CreateTemplate(Carousel control, INameScope scope)
  231. {
  232. return new CarouselPresenter
  233. {
  234. Name = "PART_ItemsPresenter",
  235. [~CarouselPresenter.IsVirtualizedProperty] = control[~Carousel.IsVirtualizedProperty],
  236. [~CarouselPresenter.ItemsProperty] = control[~Carousel.ItemsProperty],
  237. [~CarouselPresenter.ItemsPanelProperty] = control[~Carousel.ItemsPanelProperty],
  238. [~CarouselPresenter.SelectedIndexProperty] = control[~Carousel.SelectedIndexProperty],
  239. [~CarouselPresenter.PageTransitionProperty] = control[~Carousel.PageTransitionProperty],
  240. }.RegisterInNameScope(scope);
  241. }
  242. private static TextBlock GetContainerTextBlock(object control)
  243. {
  244. var contentPresenter = Assert.IsType<ContentPresenter>(control);
  245. contentPresenter.UpdateChild();
  246. return Assert.IsType<TextBlock>(contentPresenter.Child);
  247. }
  248. }
  249. }