ListBoxTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. private MouseTestHelper _mouse = new MouseTestHelper();
  18. [Fact]
  19. public void Should_Use_ItemTemplate_To_Create_Item_Content()
  20. {
  21. var target = new ListBox
  22. {
  23. Template = ListBoxTemplate(),
  24. Items = new[] { "Foo" },
  25. ItemTemplate = new FuncDataTemplate<string>(_ => new Canvas()),
  26. };
  27. Prepare(target);
  28. var container = (ListBoxItem)target.Presenter.Panel.Children[0];
  29. Assert.IsType<Canvas>(container.Presenter.Child);
  30. }
  31. [Fact]
  32. public void ListBox_Should_Find_ItemsPresenter_In_ScrollViewer()
  33. {
  34. var target = new ListBox
  35. {
  36. Template = ListBoxTemplate(),
  37. };
  38. Prepare(target);
  39. Assert.IsType<ItemsPresenter>(target.Presenter);
  40. }
  41. [Fact]
  42. public void ListBoxItem_Containers_Should_Be_Generated()
  43. {
  44. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  45. {
  46. var items = new[] { "Foo", "Bar", "Baz " };
  47. var target = new ListBox
  48. {
  49. Template = ListBoxTemplate(),
  50. Items = items,
  51. };
  52. Prepare(target);
  53. var text = target.Presenter.Panel.Children
  54. .OfType<ListBoxItem>()
  55. .Select(x => x.Presenter.Child)
  56. .OfType<TextBlock>()
  57. .Select(x => x.Text)
  58. .ToList();
  59. Assert.Equal(items, text);
  60. }
  61. }
  62. [Fact]
  63. public void LogicalChildren_Should_Be_Set_For_DataTemplate_Generated_Items()
  64. {
  65. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  66. {
  67. var target = new ListBox
  68. {
  69. Template = ListBoxTemplate(),
  70. Items = new[] { "Foo", "Bar", "Baz " },
  71. };
  72. Prepare(target);
  73. Assert.Equal(3, target.GetLogicalChildren().Count());
  74. foreach (var child in target.GetLogicalChildren())
  75. {
  76. Assert.IsType<ListBoxItem>(child);
  77. }
  78. }
  79. }
  80. [Fact]
  81. public void DataContexts_Should_Be_Correctly_Set()
  82. {
  83. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  84. {
  85. var items = new object[]
  86. {
  87. "Foo",
  88. new Item("Bar"),
  89. new TextBlock { Text = "Baz" },
  90. new ListBoxItem { Content = "Qux" },
  91. };
  92. var target = new ListBox
  93. {
  94. Template = ListBoxTemplate(),
  95. DataContext = "Base",
  96. DataTemplates =
  97. {
  98. new FuncDataTemplate<Item>(x => new Button { Content = x })
  99. },
  100. Items = items,
  101. };
  102. Prepare(target);
  103. var dataContexts = target.Presenter.Panel.Children
  104. .Cast<Control>()
  105. .Select(x => x.DataContext)
  106. .ToList();
  107. Assert.Equal(
  108. new object[] { items[0], items[1], "Base", "Base" },
  109. dataContexts);
  110. }
  111. }
  112. [Fact]
  113. public void Selection_Should_Be_Cleared_On_Recycled_Items()
  114. {
  115. var target = new ListBox
  116. {
  117. Template = ListBoxTemplate(),
  118. Items = Enumerable.Range(0, 20).Select(x => $"Item {x}").ToList(),
  119. ItemTemplate = new FuncDataTemplate<string>(x => new TextBlock { Height = 10 }),
  120. SelectedIndex = 0,
  121. };
  122. Prepare(target);
  123. // Make sure we're virtualized and first item is selected.
  124. Assert.Equal(10, target.Presenter.Panel.Children.Count);
  125. Assert.True(((ListBoxItem)target.Presenter.Panel.Children[0]).IsSelected);
  126. // Scroll down a page.
  127. target.Scroll.Offset = new Vector(0, 10);
  128. // Make sure recycled item isn't now selected.
  129. Assert.False(((ListBoxItem)target.Presenter.Panel.Children[0]).IsSelected);
  130. }
  131. [Fact]
  132. public void ScrollViewer_Should_Have_Correct_Extent_And_Viewport()
  133. {
  134. var target = new ListBox
  135. {
  136. Template = ListBoxTemplate(),
  137. Items = Enumerable.Range(0, 20).Select(x => $"Item {x}").ToList(),
  138. ItemTemplate = new FuncDataTemplate<string>(x => new TextBlock { Width = 20, Height = 10 }),
  139. SelectedIndex = 0,
  140. };
  141. Prepare(target);
  142. Assert.Equal(new Size(20, 20), target.Scroll.Extent);
  143. Assert.Equal(new Size(100, 10), target.Scroll.Viewport);
  144. }
  145. [Fact]
  146. public void Containers_Correct_After_Clear_Add_Remove()
  147. {
  148. // Issue #1936
  149. var items = new AvaloniaList<string>(Enumerable.Range(0, 11).Select(x => $"Item {x}"));
  150. var target = new ListBox
  151. {
  152. Template = ListBoxTemplate(),
  153. Items = items,
  154. ItemTemplate = new FuncDataTemplate<string>(x => new TextBlock { Width = 20, Height = 10 }),
  155. SelectedIndex = 0,
  156. };
  157. Prepare(target);
  158. items.Clear();
  159. items.AddRange(Enumerable.Range(0, 11).Select(x => $"Item {x}"));
  160. items.Remove("Item 2");
  161. Assert.Equal(
  162. items,
  163. target.Presenter.Panel.Children.Cast<ListBoxItem>().Select(x => (string)x.Content));
  164. }
  165. [Fact]
  166. public void Toggle_Selection_Should_Update_Containers()
  167. {
  168. var items = Enumerable.Range(0, 10).Select(x => $"Item {x}").ToArray();
  169. var target = new ListBox
  170. {
  171. Template = ListBoxTemplate(),
  172. Items = items,
  173. SelectionMode = SelectionMode.Toggle,
  174. ItemTemplate = new FuncDataTemplate<string>(x => new TextBlock { Height = 10 })
  175. };
  176. Prepare(target);
  177. var lbItems = target.GetLogicalChildren().OfType<ListBoxItem>().ToArray();
  178. var item = lbItems[0];
  179. Assert.Equal(false, item.IsSelected);
  180. RaisePressedEvent(target, item, MouseButton.Left);
  181. Assert.Equal(true, item.IsSelected);
  182. RaisePressedEvent(target, item, MouseButton.Left);
  183. Assert.Equal(false, item.IsSelected);
  184. }
  185. private void RaisePressedEvent(ListBox listBox, ListBoxItem item, MouseButton mouseButton)
  186. {
  187. _mouse.Click(listBox, item, mouseButton);
  188. }
  189. [Fact]
  190. public void ListBox_After_Scroll_IndexOutOfRangeException_Shouldnt_Be_Thrown()
  191. {
  192. var items = Enumerable.Range(0, 11).Select(x => $"{x}").ToArray();
  193. var target = new ListBox
  194. {
  195. Template = ListBoxTemplate(),
  196. Items = items,
  197. ItemTemplate = new FuncDataTemplate<string>(x => new TextBlock { Height = 11 })
  198. };
  199. Prepare(target);
  200. var panel = target.Presenter.Panel as IVirtualizingPanel;
  201. var listBoxItems = panel.Children.OfType<ListBoxItem>();
  202. //virtualization should have created exactly 10 items
  203. Assert.Equal(10, listBoxItems.Count());
  204. Assert.Equal("0", listBoxItems.First().DataContext);
  205. Assert.Equal("9", listBoxItems.Last().DataContext);
  206. //instead pixeloffset > 0 there could be pretty complex sequence for repro
  207. //it involves add/remove/scroll to end multiple actions
  208. //which i can't find so far :(, but this is the simplest way to add it to unit test
  209. panel.PixelOffset = 1;
  210. //here scroll to end -> IndexOutOfRangeException is thrown
  211. target.Scroll.Offset = new Vector(0, 2);
  212. Assert.True(true);
  213. }
  214. [Fact]
  215. public void LayoutManager_Should_Measure_Arrange_All()
  216. {
  217. var virtualizationMode = ItemVirtualizationMode.Simple;
  218. using (UnitTestApplication.Start(TestServices.StyledWindow))
  219. {
  220. var items = new AvaloniaList<string>(Enumerable.Range(1, 7).Select(v => v.ToString()));
  221. var wnd = new Window() { SizeToContent = SizeToContent.WidthAndHeight };
  222. wnd.IsVisible = true;
  223. var target = new ListBox();
  224. wnd.Content = target;
  225. var lm = wnd.LayoutManager;
  226. target.Height = 110;
  227. target.Width = 50;
  228. target.DataContext = items;
  229. target.VirtualizationMode = virtualizationMode;
  230. target.ItemTemplate = new FuncDataTemplate<object>(c =>
  231. {
  232. var tb = new TextBlock() { Height = 10, Width = 30 };
  233. tb.Bind(TextBlock.TextProperty, new Data.Binding());
  234. return tb;
  235. }, true);
  236. lm.ExecuteInitialLayoutPass(wnd);
  237. target.Items = items;
  238. lm.ExecuteLayoutPass();
  239. items.Insert(3, "3+");
  240. lm.ExecuteLayoutPass();
  241. items.Insert(4, "4+");
  242. lm.ExecuteLayoutPass();
  243. //RESET
  244. items.Clear();
  245. foreach (var i in Enumerable.Range(1, 7))
  246. {
  247. items.Add(i.ToString());
  248. }
  249. //working bit better with this line no outof memory or remaining to arrange/measure ???
  250. //lm.ExecuteLayoutPass();
  251. items.Insert(2, "2+");
  252. lm.ExecuteLayoutPass();
  253. //after few more layout cycles layoutmanager shouldn't hold any more visual for measure/arrange
  254. lm.ExecuteLayoutPass();
  255. lm.ExecuteLayoutPass();
  256. var flags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic;
  257. var toMeasure = lm.GetType().GetField("_toMeasure", flags).GetValue(lm) as System.Collections.Generic.IEnumerable<Layout.ILayoutable>;
  258. var toArrange = lm.GetType().GetField("_toArrange", flags).GetValue(lm) as System.Collections.Generic.IEnumerable<Layout.ILayoutable>;
  259. Assert.Equal(0, toMeasure.Count());
  260. Assert.Equal(0, toArrange.Count());
  261. }
  262. }
  263. private FuncControlTemplate ListBoxTemplate()
  264. {
  265. return new FuncControlTemplate<ListBox>(parent =>
  266. new ScrollViewer
  267. {
  268. Name = "PART_ScrollViewer",
  269. Template = ScrollViewerTemplate(),
  270. Content = new ItemsPresenter
  271. {
  272. Name = "PART_ItemsPresenter",
  273. [~ItemsPresenter.ItemsProperty] = parent.GetObservable(ItemsControl.ItemsProperty).ToBinding(),
  274. [~ItemsPresenter.ItemsPanelProperty] = parent.GetObservable(ItemsControl.ItemsPanelProperty).ToBinding(),
  275. [~ItemsPresenter.VirtualizationModeProperty] = parent.GetObservable(ListBox.VirtualizationModeProperty).ToBinding(),
  276. }
  277. });
  278. }
  279. private FuncControlTemplate ListBoxItemTemplate()
  280. {
  281. return new FuncControlTemplate<ListBoxItem>(parent =>
  282. new ContentPresenter
  283. {
  284. Name = "PART_ContentPresenter",
  285. [!ContentPresenter.ContentProperty] = parent[!ListBoxItem.ContentProperty],
  286. [!ContentPresenter.ContentTemplateProperty] = parent[!ListBoxItem.ContentTemplateProperty],
  287. });
  288. }
  289. private FuncControlTemplate ScrollViewerTemplate()
  290. {
  291. return new FuncControlTemplate<ScrollViewer>(parent =>
  292. new ScrollContentPresenter
  293. {
  294. Name = "PART_ContentPresenter",
  295. [~ScrollContentPresenter.ContentProperty] = parent.GetObservable(ScrollViewer.ContentProperty).ToBinding(),
  296. [~~ScrollContentPresenter.ExtentProperty] = parent[~~ScrollViewer.ExtentProperty],
  297. [~~ScrollContentPresenter.OffsetProperty] = parent[~~ScrollViewer.OffsetProperty],
  298. [~~ScrollContentPresenter.ViewportProperty] = parent[~~ScrollViewer.ViewportProperty],
  299. });
  300. }
  301. private void Prepare(ListBox target)
  302. {
  303. // The ListBox needs to be part of a rooted visual tree.
  304. var root = new TestRoot();
  305. root.Child = target;
  306. // Apply the template to the ListBox itself.
  307. target.ApplyTemplate();
  308. // Then to its inner ScrollViewer.
  309. var scrollViewer = (ScrollViewer)target.GetVisualChildren().Single();
  310. scrollViewer.ApplyTemplate();
  311. // Then make the ScrollViewer create its child.
  312. ((ContentPresenter)scrollViewer.Presenter).UpdateChild();
  313. // Now the ItemsPresenter should be reigstered, so apply its template.
  314. target.Presenter.ApplyTemplate();
  315. // Because ListBox items are virtualized we need to do a layout to make them appear.
  316. target.Measure(new Size(100, 100));
  317. target.Arrange(new Rect(0, 0, 100, 100));
  318. // Now set and apply the item templates.
  319. foreach (ListBoxItem item in target.Presenter.Panel.Children)
  320. {
  321. item.Template = ListBoxItemTemplate();
  322. item.ApplyTemplate();
  323. item.Presenter.ApplyTemplate();
  324. ((ContentPresenter)item.Presenter).UpdateChild();
  325. }
  326. // The items were created before the template was applied, so now we need to go back
  327. // and re-arrange everything.
  328. foreach (IControl i in target.GetSelfAndVisualDescendants())
  329. {
  330. i.InvalidateMeasure();
  331. }
  332. target.Measure(new Size(100, 100));
  333. target.Arrange(new Rect(0, 0, 100, 100));
  334. }
  335. private class Item
  336. {
  337. public Item(string value)
  338. {
  339. Value = value;
  340. }
  341. public string Value { get; }
  342. }
  343. }
  344. }