ListBoxTests.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.Controls.Presenters;
  5. using Avalonia.Controls.Templates;
  6. using Avalonia.Input;
  7. using Avalonia.LogicalTree;
  8. using Avalonia.Styling;
  9. using Avalonia.UnitTests;
  10. using Avalonia.VisualTree;
  11. using Xunit;
  12. using Avalonia.Collections;
  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 = new 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 ListBox_Virt_None_Should_Sync_Items_When_Inserted_Or_Removed()
  132. {
  133. var items = new AvaloniaList<string>(Enumerable.Range(0, 5).Select(x => $"Item {x}"));
  134. var toAdd = Enumerable.Range(0, 3).Select(x => $"Added Item {x}").ToArray();
  135. var target = new ListBox
  136. {
  137. Template = ListBoxTemplate(),
  138. VirtualizationMode = ItemVirtualizationMode.None,
  139. Items = items,
  140. ItemTemplate = new FuncDataTemplate<string>(x => new TextBlock { Height = 10 }),
  141. SelectedIndex = 0,
  142. };
  143. Prepare(target);
  144. Assert.Equal(items.Count, target.Presenter.Panel.Children.Count);
  145. int addIndex = 1;
  146. foreach(var item in toAdd)
  147. {
  148. items.Insert(addIndex++, item);
  149. }
  150. Assert.Equal(items.Count, target.Presenter.Panel.Children.Count);
  151. foreach (var item in toAdd)
  152. {
  153. items.Remove(item);
  154. }
  155. Assert.Equal(items.Count, target.Presenter.Panel.Children.Count);
  156. }
  157. private FuncControlTemplate ListBoxTemplate()
  158. {
  159. return new FuncControlTemplate<ListBox>(parent =>
  160. new ScrollViewer
  161. {
  162. Name = "PART_ScrollViewer",
  163. Template = ScrollViewerTemplate(),
  164. Content = new ItemsPresenter
  165. {
  166. Name = "PART_ItemsPresenter",
  167. [~ItemsPresenter.ItemsProperty] = parent.GetObservable(ItemsControl.ItemsProperty).AsBinding(),
  168. [~ItemsPresenter.ItemsPanelProperty] = parent.GetObservable(ItemsControl.ItemsPanelProperty).AsBinding(),
  169. [~ItemsPresenter.VirtualizationModeProperty] = parent.GetObservable(ListBox.VirtualizationModeProperty).AsBinding(),
  170. }
  171. });
  172. }
  173. private FuncControlTemplate ListBoxItemTemplate()
  174. {
  175. return new FuncControlTemplate<ListBoxItem>(parent =>
  176. new ContentPresenter
  177. {
  178. Name = "PART_ContentPresenter",
  179. [!ContentPresenter.ContentProperty] = parent[!ListBoxItem.ContentProperty],
  180. [!ContentPresenter.ContentTemplateProperty] = parent[!ListBoxItem.ContentTemplateProperty],
  181. });
  182. }
  183. private FuncControlTemplate ScrollViewerTemplate()
  184. {
  185. return new FuncControlTemplate<ScrollViewer>(parent =>
  186. new ScrollContentPresenter
  187. {
  188. Name = "PART_ContentPresenter",
  189. [~ScrollContentPresenter.ContentProperty] = parent.GetObservable(ScrollViewer.ContentProperty).AsBinding(),
  190. [~~ScrollContentPresenter.ExtentProperty] = parent[~~ScrollViewer.ExtentProperty],
  191. [~~ScrollContentPresenter.OffsetProperty] = parent[~~ScrollViewer.OffsetProperty],
  192. [~~ScrollContentPresenter.ViewportProperty] = parent[~~ScrollViewer.ViewportProperty],
  193. });
  194. }
  195. private void Prepare(ListBox target)
  196. {
  197. // The ListBox needs to be part of a rooted visual tree.
  198. var root = new TestRoot();
  199. root.Child = target;
  200. // Apply the template to the ListBox itself.
  201. target.ApplyTemplate();
  202. // Then to its inner ScrollViewer.
  203. var scrollViewer = (ScrollViewer)target.GetVisualChildren().Single();
  204. scrollViewer.ApplyTemplate();
  205. // Then make the ScrollViewer create its child.
  206. ((ContentPresenter)scrollViewer.Presenter).UpdateChild();
  207. // Now the ItemsPresenter should be reigstered, so apply its template.
  208. target.Presenter.ApplyTemplate();
  209. // Because ListBox items are virtualized we need to do a layout to make them appear.
  210. target.Measure(new Size(100, 100));
  211. target.Arrange(new Rect(0, 0, 100, 100));
  212. // Now set and apply the item templates.
  213. foreach (ListBoxItem item in target.Presenter.Panel.Children)
  214. {
  215. item.Template = ListBoxItemTemplate();
  216. item.ApplyTemplate();
  217. item.Presenter.ApplyTemplate();
  218. ((ContentPresenter)item.Presenter).UpdateChild();
  219. }
  220. // The items were created before the template was applied, so now we need to go back
  221. // and re-arrange everything.
  222. foreach (IControl i in target.GetSelfAndVisualDescendents())
  223. {
  224. i.InvalidateMeasure();
  225. }
  226. target.Arrange(new Rect(0, 0, 100, 100));
  227. }
  228. private class Item
  229. {
  230. public Item(string value)
  231. {
  232. Value = value;
  233. }
  234. public string Value { get; }
  235. }
  236. }
  237. }