ListBoxTests.cs 18 KB

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