ListBoxTests.cs 22 KB

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