ListBoxTests.cs 8.6 KB

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