ListBoxTests.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 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. private FuncControlTemplate ListBoxTemplate()
  145. {
  146. return new FuncControlTemplate<ListBox>(parent =>
  147. new ScrollViewer
  148. {
  149. Name = "PART_ScrollViewer",
  150. Template = ScrollViewerTemplate(),
  151. Content = new ItemsPresenter
  152. {
  153. Name = "PART_ItemsPresenter",
  154. [~ItemsPresenter.ItemsProperty] = parent.GetObservable(ItemsControl.ItemsProperty).ToBinding(),
  155. [~ItemsPresenter.ItemsPanelProperty] = parent.GetObservable(ItemsControl.ItemsPanelProperty).ToBinding(),
  156. [~ItemsPresenter.VirtualizationModeProperty] = parent.GetObservable(ListBox.VirtualizationModeProperty).ToBinding(),
  157. }
  158. });
  159. }
  160. private FuncControlTemplate ListBoxItemTemplate()
  161. {
  162. return new FuncControlTemplate<ListBoxItem>(parent =>
  163. new ContentPresenter
  164. {
  165. Name = "PART_ContentPresenter",
  166. [!ContentPresenter.ContentProperty] = parent[!ListBoxItem.ContentProperty],
  167. [!ContentPresenter.ContentTemplateProperty] = parent[!ListBoxItem.ContentTemplateProperty],
  168. });
  169. }
  170. private FuncControlTemplate ScrollViewerTemplate()
  171. {
  172. return new FuncControlTemplate<ScrollViewer>(parent =>
  173. new ScrollContentPresenter
  174. {
  175. Name = "PART_ContentPresenter",
  176. [~ScrollContentPresenter.ContentProperty] = parent.GetObservable(ScrollViewer.ContentProperty).ToBinding(),
  177. [~~ScrollContentPresenter.ExtentProperty] = parent[~~ScrollViewer.ExtentProperty],
  178. [~~ScrollContentPresenter.OffsetProperty] = parent[~~ScrollViewer.OffsetProperty],
  179. [~~ScrollContentPresenter.ViewportProperty] = parent[~~ScrollViewer.ViewportProperty],
  180. });
  181. }
  182. private void Prepare(ListBox target)
  183. {
  184. // The ListBox needs to be part of a rooted visual tree.
  185. var root = new TestRoot();
  186. root.Child = target;
  187. // Apply the template to the ListBox itself.
  188. target.ApplyTemplate();
  189. // Then to its inner ScrollViewer.
  190. var scrollViewer = (ScrollViewer)target.GetVisualChildren().Single();
  191. scrollViewer.ApplyTemplate();
  192. // Then make the ScrollViewer create its child.
  193. ((ContentPresenter)scrollViewer.Presenter).UpdateChild();
  194. // Now the ItemsPresenter should be reigstered, so apply its template.
  195. target.Presenter.ApplyTemplate();
  196. // Because ListBox items are virtualized we need to do a layout to make them appear.
  197. target.Measure(new Size(100, 100));
  198. target.Arrange(new Rect(0, 0, 100, 100));
  199. // Now set and apply the item templates.
  200. foreach (ListBoxItem item in target.Presenter.Panel.Children)
  201. {
  202. item.Template = ListBoxItemTemplate();
  203. item.ApplyTemplate();
  204. item.Presenter.ApplyTemplate();
  205. ((ContentPresenter)item.Presenter).UpdateChild();
  206. }
  207. // The items were created before the template was applied, so now we need to go back
  208. // and re-arrange everything.
  209. foreach (IControl i in target.GetSelfAndVisualDescendants())
  210. {
  211. i.InvalidateMeasure();
  212. }
  213. target.Measure(new Size(100, 100));
  214. target.Arrange(new Rect(0, 0, 100, 100));
  215. }
  216. private class Item
  217. {
  218. public Item(string value)
  219. {
  220. Value = value;
  221. }
  222. public string Value { get; }
  223. }
  224. }
  225. }