ListBoxTests.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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(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. [Fact]
  325. public void Adding_And_Selecting_Item_With_AutoScrollToSelectedItem_Should_NotHide_FirstItem()
  326. {
  327. using (UnitTestApplication.Start(TestServices.StyledWindow))
  328. {
  329. var items = new AvaloniaList<string>();
  330. var wnd = new Window() { Width = 100, Height = 100, IsVisible = true };
  331. var target = new ListBox()
  332. {
  333. VerticalAlignment = Layout.VerticalAlignment.Top,
  334. AutoScrollToSelectedItem = true,
  335. Width = 50,
  336. VirtualizationMode = ItemVirtualizationMode.Simple,
  337. ItemTemplate = new FuncDataTemplate<object>((c, _) => new Border() { Height = 10 }),
  338. Items = items,
  339. };
  340. wnd.Content = target;
  341. var lm = wnd.LayoutManager;
  342. lm.ExecuteInitialLayoutPass();
  343. var panel = target.Presenter.Panel;
  344. items.Add("Item 1");
  345. target.Selection.Select(0);
  346. lm.ExecuteLayoutPass();
  347. Assert.Equal(1, panel.Children.Count);
  348. items.Add("Item 2");
  349. target.Selection.Select(1);
  350. lm.ExecuteLayoutPass();
  351. Assert.Equal(2, panel.Children.Count);
  352. //make sure we have enough space to show all items
  353. Assert.True(panel.Bounds.Height >= panel.Children.Sum(c => c.Bounds.Height));
  354. //make sure we show items and they completelly visible, not only partially
  355. Assert.True(panel.Children[0].Bounds.Top >= 0 && panel.Children[0].Bounds.Bottom <= panel.Bounds.Height, "first item is not completelly visible!");
  356. Assert.True(panel.Children[1].Bounds.Top >= 0 && panel.Children[1].Bounds.Bottom <= panel.Bounds.Height, "second item is not completelly visible!");
  357. }
  358. }
  359. private FuncControlTemplate ListBoxTemplate()
  360. {
  361. return new FuncControlTemplate<ListBox>((parent, scope) =>
  362. new ScrollViewer
  363. {
  364. Name = "PART_ScrollViewer",
  365. Template = ScrollViewerTemplate(),
  366. Content = new ItemsPresenter
  367. {
  368. Name = "PART_ItemsPresenter",
  369. [~ItemsPresenter.ItemsProperty] = parent.GetObservable(ItemsControl.ItemsProperty).ToBinding(),
  370. [~ItemsPresenter.ItemsPanelProperty] = parent.GetObservable(ItemsControl.ItemsPanelProperty).ToBinding(),
  371. [~ItemsPresenter.VirtualizationModeProperty] = parent.GetObservable(ListBox.VirtualizationModeProperty).ToBinding(),
  372. }.RegisterInNameScope(scope)
  373. }.RegisterInNameScope(scope));
  374. }
  375. private FuncControlTemplate ListBoxItemTemplate()
  376. {
  377. return new FuncControlTemplate<ListBoxItem>((parent, scope) =>
  378. new ContentPresenter
  379. {
  380. Name = "PART_ContentPresenter",
  381. [!ContentPresenter.ContentProperty] = parent[!ListBoxItem.ContentProperty],
  382. [!ContentPresenter.ContentTemplateProperty] = parent[!ListBoxItem.ContentTemplateProperty],
  383. }.RegisterInNameScope(scope));
  384. }
  385. private FuncControlTemplate ScrollViewerTemplate()
  386. {
  387. return new FuncControlTemplate<ScrollViewer>((parent, scope) =>
  388. new Panel
  389. {
  390. Children =
  391. {
  392. new ScrollContentPresenter
  393. {
  394. Name = "PART_ContentPresenter",
  395. [~ScrollContentPresenter.ContentProperty] = parent.GetObservable(ScrollViewer.ContentProperty).ToBinding(),
  396. [~~ScrollContentPresenter.ExtentProperty] = parent[~~ScrollViewer.ExtentProperty],
  397. [~~ScrollContentPresenter.OffsetProperty] = parent[~~ScrollViewer.OffsetProperty],
  398. [~~ScrollContentPresenter.ViewportProperty] = parent[~~ScrollViewer.ViewportProperty],
  399. }.RegisterInNameScope(scope),
  400. new ScrollBar
  401. {
  402. Name = "verticalScrollBar",
  403. [~ScrollBar.MaximumProperty] = parent[~ScrollViewer.VerticalScrollBarMaximumProperty],
  404. [~~ScrollBar.ValueProperty] = parent[~~ScrollViewer.VerticalScrollBarValueProperty],
  405. }
  406. }
  407. });
  408. }
  409. private void Prepare(ListBox target)
  410. {
  411. // The ListBox needs to be part of a rooted visual tree.
  412. var root = new TestRoot();
  413. root.Child = target;
  414. // Apply the template to the ListBox itself.
  415. target.ApplyTemplate();
  416. // Then to its inner ScrollViewer.
  417. var scrollViewer = (ScrollViewer)target.GetVisualChildren().Single();
  418. scrollViewer.ApplyTemplate();
  419. // Then make the ScrollViewer create its child.
  420. ((ContentPresenter)scrollViewer.Presenter).UpdateChild();
  421. // Now the ItemsPresenter should be reigstered, so apply its template.
  422. target.Presenter.ApplyTemplate();
  423. // Because ListBox items are virtualized we need to do a layout to make them appear.
  424. target.Measure(new Size(100, 100));
  425. target.Arrange(new Rect(0, 0, 100, 100));
  426. // Now set and apply the item templates.
  427. foreach (ListBoxItem item in target.Presenter.Panel.Children)
  428. {
  429. item.Template = ListBoxItemTemplate();
  430. item.ApplyTemplate();
  431. item.Presenter.ApplyTemplate();
  432. ((ContentPresenter)item.Presenter).UpdateChild();
  433. }
  434. // The items were created before the template was applied, so now we need to go back
  435. // and re-arrange everything.
  436. foreach (IControl i in target.GetSelfAndVisualDescendants())
  437. {
  438. i.InvalidateMeasure();
  439. }
  440. target.Measure(new Size(100, 100));
  441. target.Arrange(new Rect(0, 0, 100, 100));
  442. }
  443. private class Item
  444. {
  445. public Item(string value)
  446. {
  447. Value = value;
  448. }
  449. public string Value { get; }
  450. }
  451. [Fact]
  452. public void SelectedItem_Validation()
  453. {
  454. }
  455. }
  456. }