CarouselTests.cs 9.3 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 = target.GetLogicalChildren().Single();
  47. Assert.IsType<TextBlock>(child);
  48. Assert.Equal("Foo", ((TextBlock)child).Text);
  49. }
  50. [Fact]
  51. public void Should_Remove_NonCurrent_Page_When_IsVirtualized_True()
  52. {
  53. var target = new Carousel
  54. {
  55. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  56. Items = new[] { "foo", "bar" },
  57. IsVirtualized = true,
  58. SelectedIndex = 0,
  59. };
  60. target.ApplyTemplate();
  61. target.Presenter.ApplyTemplate();
  62. Assert.Single(target.ItemContainerGenerator.Containers);
  63. target.SelectedIndex = 1;
  64. Assert.Single(target.ItemContainerGenerator.Containers);
  65. }
  66. [Fact]
  67. public void Selected_Item_Changes_To_First_Item_When_Items_Property_Changes()
  68. {
  69. var items = new ObservableCollection<string>
  70. {
  71. "Foo",
  72. "Bar",
  73. "FooBar"
  74. };
  75. var target = new Carousel
  76. {
  77. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  78. Items = items,
  79. IsVirtualized = false
  80. };
  81. target.ApplyTemplate();
  82. target.Presenter.ApplyTemplate();
  83. Assert.Equal(3, target.GetLogicalChildren().Count());
  84. var child = target.GetLogicalChildren().First();
  85. Assert.IsType<TextBlock>(child);
  86. Assert.Equal("Foo", ((TextBlock)child).Text);
  87. var newItems = items.ToList();
  88. newItems.RemoveAt(0);
  89. target.Items = newItems;
  90. child = target.GetLogicalChildren().First();
  91. Assert.IsType<TextBlock>(child);
  92. Assert.Equal("Bar", ((TextBlock)child).Text);
  93. }
  94. [Fact]
  95. public void Selected_Item_Changes_To_First_Item_When_Items_Property_Changes_And_Virtualized()
  96. {
  97. var items = new ObservableCollection<string>
  98. {
  99. "Foo",
  100. "Bar",
  101. "FooBar"
  102. };
  103. var target = new Carousel
  104. {
  105. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  106. Items = items,
  107. IsVirtualized = true,
  108. };
  109. target.ApplyTemplate();
  110. target.Presenter.ApplyTemplate();
  111. Assert.Single(target.GetLogicalChildren());
  112. var child = target.GetLogicalChildren().Single();
  113. Assert.IsType<TextBlock>(child);
  114. Assert.Equal("Foo", ((TextBlock)child).Text);
  115. var newItems = items.ToList();
  116. newItems.RemoveAt(0);
  117. target.Items = newItems;
  118. child = target.GetLogicalChildren().Single();
  119. Assert.IsType<TextBlock>(child);
  120. Assert.Equal("Bar", ((TextBlock)child).Text);
  121. }
  122. [Fact]
  123. public void Selected_Item_Changes_To_First_Item_When_Item_Added()
  124. {
  125. var items = new ObservableCollection<string>();
  126. var target = new Carousel
  127. {
  128. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  129. Items = items,
  130. IsVirtualized = false
  131. };
  132. target.ApplyTemplate();
  133. target.Presenter.ApplyTemplate();
  134. Assert.Equal(-1, target.SelectedIndex);
  135. Assert.Empty(target.GetLogicalChildren());
  136. items.Add("Foo");
  137. Assert.Equal(0, target.SelectedIndex);
  138. Assert.Single(target.GetLogicalChildren());
  139. }
  140. [Fact]
  141. public void Selected_Index_Changes_To_When_Items_Assigned_Null()
  142. {
  143. var items = new ObservableCollection<string>
  144. {
  145. "Foo",
  146. "Bar",
  147. "FooBar"
  148. };
  149. var target = new Carousel
  150. {
  151. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  152. Items = items,
  153. IsVirtualized = false
  154. };
  155. target.ApplyTemplate();
  156. target.Presenter.ApplyTemplate();
  157. Assert.Equal(3, target.GetLogicalChildren().Count());
  158. var child = target.GetLogicalChildren().First();
  159. Assert.IsType<TextBlock>(child);
  160. Assert.Equal("Foo", ((TextBlock)child).Text);
  161. target.Items = null;
  162. var numChildren = target.GetLogicalChildren().Count();
  163. Assert.Equal(0, numChildren);
  164. Assert.Equal(-1, target.SelectedIndex);
  165. }
  166. [Fact]
  167. public void Selected_Index_Is_Maintained_Carousel_Created_With_Non_Zero_SelectedIndex()
  168. {
  169. var items = new ObservableCollection<string>
  170. {
  171. "Foo",
  172. "Bar",
  173. "FooBar"
  174. };
  175. var target = new Carousel
  176. {
  177. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  178. Items = items,
  179. IsVirtualized = false,
  180. SelectedIndex = 2
  181. };
  182. target.ApplyTemplate();
  183. target.Presenter.ApplyTemplate();
  184. Assert.Equal("FooBar", target.SelectedItem);
  185. var child = target.GetVisualDescendants().LastOrDefault();
  186. Assert.IsType<TextBlock>(child);
  187. Assert.Equal("FooBar", ((TextBlock)child).Text);
  188. }
  189. [Fact]
  190. public void Selected_Item_Changes_To_Next_First_Item_When_Item_Removed_From_Beggining_Of_List()
  191. {
  192. var items = new ObservableCollection<string>
  193. {
  194. "Foo",
  195. "Bar",
  196. "FooBar"
  197. };
  198. var target = new Carousel
  199. {
  200. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  201. Items = items,
  202. IsVirtualized = false
  203. };
  204. target.ApplyTemplate();
  205. target.Presenter.ApplyTemplate();
  206. Assert.Equal(3, target.GetLogicalChildren().Count());
  207. var child = target.GetLogicalChildren().First();
  208. Assert.IsType<TextBlock>(child);
  209. Assert.Equal("Foo", ((TextBlock)child).Text);
  210. items.RemoveAt(0);
  211. child = target.GetLogicalChildren().First();
  212. Assert.IsType<TextBlock>(child);
  213. Assert.Equal("Bar", ((TextBlock)child).Text);
  214. }
  215. [Fact]
  216. public void Selected_Item_Changes_To_NextAvailable_Item_If_SelectedItem_Is_Removed_From_Middle()
  217. {
  218. var items = new ObservableCollection<string>
  219. {
  220. "Foo",
  221. "Bar",
  222. "FooBar"
  223. };
  224. var target = new Carousel
  225. {
  226. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  227. Items = items,
  228. IsVirtualized = false
  229. };
  230. target.ApplyTemplate();
  231. target.Presenter.ApplyTemplate();
  232. target.SelectedIndex = 1;
  233. items.RemoveAt(1);
  234. Assert.Equal(1, target.SelectedIndex);
  235. Assert.Equal("FooBar", target.SelectedItem);
  236. }
  237. private Control CreateTemplate(Carousel control, INameScope scope)
  238. {
  239. return new CarouselPresenter
  240. {
  241. Name = "PART_ItemsPresenter",
  242. [~CarouselPresenter.IsVirtualizedProperty] = control[~Carousel.IsVirtualizedProperty],
  243. [~CarouselPresenter.ItemsProperty] = control[~Carousel.ItemsProperty],
  244. [~CarouselPresenter.ItemsPanelProperty] = control[~Carousel.ItemsPanelProperty],
  245. [~CarouselPresenter.SelectedIndexProperty] = control[~Carousel.SelectedIndexProperty],
  246. [~CarouselPresenter.PageTransitionProperty] = control[~Carousel.PageTransitionProperty],
  247. }.RegisterInNameScope(scope);
  248. }
  249. }
  250. }