ListBoxTests.cs 17 KB

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