ListBoxTests.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. namespace Avalonia.Controls.UnitTests
  13. {
  14. public class ListBoxTests
  15. {
  16. [Fact]
  17. public void Should_Use_ItemTemplate_To_Create_Item_Content()
  18. {
  19. var target = new ListBox
  20. {
  21. Template = ListBoxTemplate(),
  22. Items = new[] { "Foo" },
  23. ItemTemplate = new FuncDataTemplate<string>(_ => new Canvas()),
  24. };
  25. Prepare(target);
  26. var container = (ListBoxItem)target.Presenter.Panel.Children[0];
  27. Assert.IsType<Canvas>(container.Presenter.Child);
  28. }
  29. [Fact]
  30. public void ListBox_Should_Find_ItemsPresenter_In_ScrollViewer()
  31. {
  32. var target = new ListBox
  33. {
  34. Template = ListBoxTemplate(),
  35. };
  36. Prepare(target);
  37. Assert.IsType<ItemsPresenter>(target.Presenter);
  38. }
  39. [Fact]
  40. public void ListBoxItem_Containers_Should_Be_Generated()
  41. {
  42. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  43. {
  44. var items = new[] { "Foo", "Bar", "Baz " };
  45. var target = new ListBox
  46. {
  47. Template = ListBoxTemplate(),
  48. Items = items,
  49. };
  50. Prepare(target);
  51. var text = target.Presenter.Panel.Children
  52. .OfType<ListBoxItem>()
  53. .Select(x => x.Presenter.Child)
  54. .OfType<TextBlock>()
  55. .Select(x => x.Text)
  56. .ToList();
  57. Assert.Equal(items, text);
  58. }
  59. }
  60. [Fact]
  61. public void LogicalChildren_Should_Be_Set_For_DataTemplate_Generated_Items()
  62. {
  63. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  64. {
  65. var target = new ListBox
  66. {
  67. Template = ListBoxTemplate(),
  68. Items = new[] { "Foo", "Bar", "Baz " },
  69. };
  70. Prepare(target);
  71. Assert.Equal(3, target.GetLogicalChildren().Count());
  72. foreach (var child in target.GetLogicalChildren())
  73. {
  74. Assert.IsType<ListBoxItem>(child);
  75. }
  76. }
  77. }
  78. [Fact]
  79. public void DataContexts_Should_Be_Correctly_Set()
  80. {
  81. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  82. {
  83. var items = new object[]
  84. {
  85. "Foo",
  86. new Item("Bar"),
  87. new TextBlock { Text = "Baz" },
  88. new ListBoxItem { Content = "Qux" },
  89. };
  90. var target = new ListBox
  91. {
  92. Template = ListBoxTemplate(),
  93. DataContext = "Base",
  94. DataTemplates = new DataTemplates
  95. {
  96. new FuncDataTemplate<Item>(x => new Button { Content = x })
  97. },
  98. Items = items,
  99. };
  100. Prepare(target);
  101. var dataContexts = target.Presenter.Panel.Children
  102. .Cast<Control>()
  103. .Select(x => x.DataContext)
  104. .ToList();
  105. Assert.Equal(
  106. new object[] { items[0], items[1], "Base", "Base" },
  107. dataContexts);
  108. }
  109. }
  110. [Fact]
  111. public void Selection_Should_Be_Cleared_On_Recycled_Items()
  112. {
  113. var target = new ListBox
  114. {
  115. Template = ListBoxTemplate(),
  116. Items = Enumerable.Range(0, 20).Select(x => $"Item {x}").ToList(),
  117. ItemTemplate = new FuncDataTemplate<string>(x => new TextBlock { Height = 10 }),
  118. SelectedIndex = 0,
  119. };
  120. Prepare(target);
  121. // Make sure we're virtualized and first item is selected.
  122. Assert.Equal(10, target.Presenter.Panel.Children.Count);
  123. Assert.True(((ListBoxItem)target.Presenter.Panel.Children[0]).IsSelected);
  124. // Scroll down a page.
  125. target.Scroll.Offset = new Vector(0, 10);
  126. // Make sure recycled item isn't now selected.
  127. Assert.False(((ListBoxItem)target.Presenter.Panel.Children[0]).IsSelected);
  128. }
  129. private FuncControlTemplate ListBoxTemplate()
  130. {
  131. return new FuncControlTemplate<ListBox>(parent =>
  132. new ScrollViewer
  133. {
  134. Name = "PART_ScrollViewer",
  135. Template = ScrollViewerTemplate(),
  136. Content = new ItemsPresenter
  137. {
  138. Name = "PART_ItemsPresenter",
  139. [~ItemsPresenter.ItemsProperty] = parent.GetObservable(ItemsControl.ItemsProperty).AsBinding(),
  140. [~ItemsPresenter.ItemsPanelProperty] = parent.GetObservable(ItemsControl.ItemsPanelProperty).AsBinding(),
  141. [~ItemsPresenter.VirtualizationModeProperty] = parent.GetObservable(ListBox.VirtualizationModeProperty).AsBinding(),
  142. }
  143. });
  144. }
  145. private FuncControlTemplate ListBoxItemTemplate()
  146. {
  147. return new FuncControlTemplate<ListBoxItem>(parent =>
  148. new ContentPresenter
  149. {
  150. Name = "PART_ContentPresenter",
  151. [!ContentPresenter.ContentProperty] = parent[!ListBoxItem.ContentProperty],
  152. [!ContentPresenter.ContentTemplateProperty] = parent[!ListBoxItem.ContentTemplateProperty],
  153. });
  154. }
  155. private FuncControlTemplate ScrollViewerTemplate()
  156. {
  157. return new FuncControlTemplate<ScrollViewer>(parent =>
  158. new ScrollContentPresenter
  159. {
  160. Name = "PART_ContentPresenter",
  161. [~ScrollContentPresenter.ContentProperty] = parent.GetObservable(ScrollViewer.ContentProperty).AsBinding(),
  162. [~~ScrollContentPresenter.ExtentProperty] = parent[~~ScrollViewer.ExtentProperty],
  163. [~~ScrollContentPresenter.OffsetProperty] = parent[~~ScrollViewer.OffsetProperty],
  164. [~~ScrollContentPresenter.ViewportProperty] = parent[~~ScrollViewer.ViewportProperty],
  165. });
  166. }
  167. private void Prepare(ListBox target)
  168. {
  169. // The ListBox needs to be part of a rooted visual tree.
  170. var root = new TestRoot();
  171. root.Child = target;
  172. // Apply the template to the ListBox itself.
  173. target.ApplyTemplate();
  174. // Then to its inner ScrollViewer.
  175. var scrollViewer = (ScrollViewer)target.GetVisualChildren().Single();
  176. scrollViewer.ApplyTemplate();
  177. // Then make the ScrollViewer create its child.
  178. ((ContentPresenter)scrollViewer.Presenter).UpdateChild();
  179. // Now the ItemsPresenter should be reigstered, so apply its template.
  180. target.Presenter.ApplyTemplate();
  181. // Because ListBox items are virtualized we need to do a layout to make them appear.
  182. target.Measure(new Size(100, 100));
  183. target.Arrange(new Rect(0, 0, 100, 100));
  184. // Now set and apply the item templates.
  185. foreach (ListBoxItem item in target.Presenter.Panel.Children)
  186. {
  187. item.Template = ListBoxItemTemplate();
  188. item.ApplyTemplate();
  189. item.Presenter.ApplyTemplate();
  190. ((ContentPresenter)item.Presenter).UpdateChild();
  191. }
  192. // The items were created before the template was applied, so now we need to go back
  193. // and re-arrange everything.
  194. foreach (IControl i in target.GetSelfAndVisualDescendents())
  195. {
  196. i.InvalidateMeasure();
  197. }
  198. target.Arrange(new Rect(0, 0, 100, 100));
  199. }
  200. private class Item
  201. {
  202. public Item(string value)
  203. {
  204. Value = value;
  205. }
  206. public string Value { get; }
  207. }
  208. }
  209. }