ListBoxTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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.Linq;
  4. using Avalonia.Collections;
  5. using Avalonia.Controls.Presenters;
  6. using Avalonia.Controls.Templates;
  7. using Avalonia.Input;
  8. using Avalonia.LogicalTree;
  9. using Avalonia.Styling;
  10. using Avalonia.UnitTests;
  11. using Avalonia.VisualTree;
  12. using Xunit;
  13. namespace Avalonia.Controls.UnitTests
  14. {
  15. public class ListBoxTests
  16. {
  17. [Fact]
  18. public void Should_Use_ItemTemplate_To_Create_Item_Content()
  19. {
  20. var target = new ListBox
  21. {
  22. Template = ListBoxTemplate(),
  23. Items = new[] { "Foo" },
  24. ItemTemplate = new FuncDataTemplate<string>(_ => new Canvas()),
  25. };
  26. Prepare(target);
  27. var container = (ListBoxItem)target.Presenter.Panel.Children[0];
  28. Assert.IsType<Canvas>(container.Presenter.Child);
  29. }
  30. [Fact]
  31. public void ListBox_Should_Find_ItemsPresenter_In_ScrollViewer()
  32. {
  33. var target = new ListBox
  34. {
  35. Template = ListBoxTemplate(),
  36. };
  37. Prepare(target);
  38. Assert.IsType<ItemsPresenter>(target.Presenter);
  39. }
  40. [Fact]
  41. public void ListBoxItem_Containers_Should_Be_Generated()
  42. {
  43. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  44. {
  45. var items = new[] { "Foo", "Bar", "Baz " };
  46. var target = new ListBox
  47. {
  48. Template = ListBoxTemplate(),
  49. Items = items,
  50. };
  51. Prepare(target);
  52. var text = target.Presenter.Panel.Children
  53. .OfType<ListBoxItem>()
  54. .Select(x => x.Presenter.Child)
  55. .OfType<TextBlock>()
  56. .Select(x => x.Text)
  57. .ToList();
  58. Assert.Equal(items, text);
  59. }
  60. }
  61. [Fact]
  62. public void LogicalChildren_Should_Be_Set_For_DataTemplate_Generated_Items()
  63. {
  64. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  65. {
  66. var target = new ListBox
  67. {
  68. Template = ListBoxTemplate(),
  69. Items = new[] { "Foo", "Bar", "Baz " },
  70. };
  71. Prepare(target);
  72. Assert.Equal(3, target.GetLogicalChildren().Count());
  73. foreach (var child in target.GetLogicalChildren())
  74. {
  75. Assert.IsType<ListBoxItem>(child);
  76. }
  77. }
  78. }
  79. [Fact]
  80. public void DataContexts_Should_Be_Correctly_Set()
  81. {
  82. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  83. {
  84. var items = new object[]
  85. {
  86. "Foo",
  87. new Item("Bar"),
  88. new TextBlock { Text = "Baz" },
  89. new ListBoxItem { Content = "Qux" },
  90. };
  91. var target = new ListBox
  92. {
  93. Template = ListBoxTemplate(),
  94. DataContext = "Base",
  95. DataTemplates =
  96. {
  97. new FuncDataTemplate<Item>(x => new Button { Content = x })
  98. },
  99. Items = items,
  100. };
  101. Prepare(target);
  102. var dataContexts = target.Presenter.Panel.Children
  103. .Cast<Control>()
  104. .Select(x => x.DataContext)
  105. .ToList();
  106. Assert.Equal(
  107. new object[] { items[0], items[1], "Base", "Base" },
  108. dataContexts);
  109. }
  110. }
  111. [Fact]
  112. public void Selection_Should_Be_Cleared_On_Recycled_Items()
  113. {
  114. var target = new ListBox
  115. {
  116. Template = ListBoxTemplate(),
  117. Items = Enumerable.Range(0, 20).Select(x => $"Item {x}").ToList(),
  118. ItemTemplate = new FuncDataTemplate<string>(x => new TextBlock { Height = 10 }),
  119. SelectedIndex = 0,
  120. };
  121. Prepare(target);
  122. // Make sure we're virtualized and first item is selected.
  123. Assert.Equal(10, target.Presenter.Panel.Children.Count);
  124. Assert.True(((ListBoxItem)target.Presenter.Panel.Children[0]).IsSelected);
  125. // Scroll down a page.
  126. target.Scroll.Offset = new Vector(0, 10);
  127. // Make sure recycled item isn't now selected.
  128. Assert.False(((ListBoxItem)target.Presenter.Panel.Children[0]).IsSelected);
  129. }
  130. [Fact]
  131. public void ScrollViewer_Should_Have_Correct_Extent_And_Viewport()
  132. {
  133. var target = new ListBox
  134. {
  135. Template = ListBoxTemplate(),
  136. Items = Enumerable.Range(0, 20).Select(x => $"Item {x}").ToList(),
  137. ItemTemplate = new FuncDataTemplate<string>(x => new TextBlock { Width = 20, Height = 10 }),
  138. SelectedIndex = 0,
  139. };
  140. Prepare(target);
  141. Assert.Equal(new Size(20, 20), target.Scroll.Extent);
  142. Assert.Equal(new Size(100, 10), target.Scroll.Viewport);
  143. }
  144. [Fact]
  145. public void Containers_Correct_After_Clear_Add_Remove()
  146. {
  147. // Issue #1936
  148. var items = new AvaloniaList<string>(Enumerable.Range(0, 11).Select(x => $"Item {x}"));
  149. var target = new ListBox
  150. {
  151. Template = ListBoxTemplate(),
  152. Items = items,
  153. ItemTemplate = new FuncDataTemplate<string>(x => new TextBlock { Width = 20, Height = 10 }),
  154. SelectedIndex = 0,
  155. };
  156. Prepare(target);
  157. items.Clear();
  158. items.AddRange(Enumerable.Range(0, 11).Select(x => $"Item {x}"));
  159. items.Remove("Item 2");
  160. Assert.Equal(
  161. items,
  162. target.Presenter.Panel.Children.Cast<ListBoxItem>().Select(x => (string)x.Content));
  163. }
  164. [Fact]
  165. public void Toggle_Selection_Should_Update_Containers()
  166. {
  167. var items = Enumerable.Range(0, 10).Select(x => $"Item {x}").ToArray();
  168. var target = new ListBox
  169. {
  170. Template = ListBoxTemplate(),
  171. Items = items,
  172. SelectionMode = SelectionMode.Toggle,
  173. ItemTemplate = new FuncDataTemplate<string>(x => new TextBlock { Height = 10 })
  174. };
  175. Prepare(target);
  176. var lbItems = target.GetLogicalChildren().OfType<ListBoxItem>().ToArray();
  177. var item = lbItems[0];
  178. Assert.Equal(false, item.IsSelected);
  179. RaisePressedEvent(target, item, MouseButton.Left);
  180. Assert.Equal(true, item.IsSelected);
  181. RaisePressedEvent(target, item, MouseButton.Left);
  182. Assert.Equal(false, item.IsSelected);
  183. }
  184. private void RaisePressedEvent(ListBox listBox, ListBoxItem item, MouseButton mouseButton)
  185. {
  186. listBox.RaiseEvent(new PointerPressedEventArgs
  187. {
  188. Source = item,
  189. RoutedEvent = InputElement.PointerPressedEvent,
  190. MouseButton = mouseButton
  191. });
  192. }
  193. [Fact]
  194. public void ListBox_After_Scroll_IndexOutOfRangeException_Shouldnt_Be_Thrown()
  195. {
  196. var items = Enumerable.Range(0, 11).Select(x => $"{x}").ToArray();
  197. var target = new ListBox
  198. {
  199. Template = ListBoxTemplate(),
  200. Items = items,
  201. ItemTemplate = new FuncDataTemplate<string>(x => new TextBlock { Height = 11 })
  202. };
  203. Prepare(target);
  204. var panel = target.Presenter.Panel as IVirtualizingPanel;
  205. var listBoxItems = panel.Children.OfType<ListBoxItem>();
  206. //virtualization should have created exactly 10 items
  207. Assert.Equal(10, listBoxItems.Count());
  208. Assert.Equal("0", listBoxItems.First().DataContext);
  209. Assert.Equal("9", listBoxItems.Last().DataContext);
  210. //instead pixeloffset > 0 there could be pretty complex sequence for repro
  211. //it involves add/remove/scroll to end multiple actions
  212. //which i can't find so far :(, but this is the simplest way to add it to unit test
  213. panel.PixelOffset = 1;
  214. //here scroll to end -> IndexOutOfRangeException is thrown
  215. target.Scroll.Offset = new Vector(0, 2);
  216. Assert.True(true);
  217. }
  218. private FuncControlTemplate ListBoxTemplate()
  219. {
  220. return new FuncControlTemplate<ListBox>(parent =>
  221. new ScrollViewer
  222. {
  223. Name = "PART_ScrollViewer",
  224. Template = ScrollViewerTemplate(),
  225. Content = new ItemsPresenter
  226. {
  227. Name = "PART_ItemsPresenter",
  228. [~ItemsPresenter.ItemsProperty] = parent.GetObservable(ItemsControl.ItemsProperty).ToBinding(),
  229. [~ItemsPresenter.ItemsPanelProperty] = parent.GetObservable(ItemsControl.ItemsPanelProperty).ToBinding(),
  230. [~ItemsPresenter.VirtualizationModeProperty] = parent.GetObservable(ListBox.VirtualizationModeProperty).ToBinding(),
  231. }
  232. });
  233. }
  234. private FuncControlTemplate ListBoxItemTemplate()
  235. {
  236. return new FuncControlTemplate<ListBoxItem>(parent =>
  237. new ContentPresenter
  238. {
  239. Name = "PART_ContentPresenter",
  240. [!ContentPresenter.ContentProperty] = parent[!ListBoxItem.ContentProperty],
  241. [!ContentPresenter.ContentTemplateProperty] = parent[!ListBoxItem.ContentTemplateProperty],
  242. });
  243. }
  244. private FuncControlTemplate ScrollViewerTemplate()
  245. {
  246. return new FuncControlTemplate<ScrollViewer>(parent =>
  247. new ScrollContentPresenter
  248. {
  249. Name = "PART_ContentPresenter",
  250. [~ScrollContentPresenter.ContentProperty] = parent.GetObservable(ScrollViewer.ContentProperty).ToBinding(),
  251. [~~ScrollContentPresenter.ExtentProperty] = parent[~~ScrollViewer.ExtentProperty],
  252. [~~ScrollContentPresenter.OffsetProperty] = parent[~~ScrollViewer.OffsetProperty],
  253. [~~ScrollContentPresenter.ViewportProperty] = parent[~~ScrollViewer.ViewportProperty],
  254. });
  255. }
  256. private void Prepare(ListBox target)
  257. {
  258. // The ListBox needs to be part of a rooted visual tree.
  259. var root = new TestRoot();
  260. root.Child = target;
  261. // Apply the template to the ListBox itself.
  262. target.ApplyTemplate();
  263. // Then to its inner ScrollViewer.
  264. var scrollViewer = (ScrollViewer)target.GetVisualChildren().Single();
  265. scrollViewer.ApplyTemplate();
  266. // Then make the ScrollViewer create its child.
  267. ((ContentPresenter)scrollViewer.Presenter).UpdateChild();
  268. // Now the ItemsPresenter should be reigstered, so apply its template.
  269. target.Presenter.ApplyTemplate();
  270. // Because ListBox items are virtualized we need to do a layout to make them appear.
  271. target.Measure(new Size(100, 100));
  272. target.Arrange(new Rect(0, 0, 100, 100));
  273. // Now set and apply the item templates.
  274. foreach (ListBoxItem item in target.Presenter.Panel.Children)
  275. {
  276. item.Template = ListBoxItemTemplate();
  277. item.ApplyTemplate();
  278. item.Presenter.ApplyTemplate();
  279. ((ContentPresenter)item.Presenter).UpdateChild();
  280. }
  281. // The items were created before the template was applied, so now we need to go back
  282. // and re-arrange everything.
  283. foreach (IControl i in target.GetSelfAndVisualDescendants())
  284. {
  285. i.InvalidateMeasure();
  286. }
  287. target.Measure(new Size(100, 100));
  288. target.Arrange(new Rect(0, 0, 100, 100));
  289. }
  290. private class Item
  291. {
  292. public Item(string value)
  293. {
  294. Value = value;
  295. }
  296. public string Value { get; }
  297. }
  298. }
  299. }