ItemsControlTests.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. using System.Collections.Specialized;
  2. using System.Linq;
  3. using Avalonia.Collections;
  4. using Avalonia.Controls.Presenters;
  5. using Avalonia.Controls.Templates;
  6. using Avalonia.LogicalTree;
  7. using Avalonia.VisualTree;
  8. using Xunit;
  9. using System.Collections.ObjectModel;
  10. using Avalonia.UnitTests;
  11. using Avalonia.Input;
  12. using System.Collections.Generic;
  13. namespace Avalonia.Controls.UnitTests
  14. {
  15. public class ItemsControlTests
  16. {
  17. [Fact]
  18. public void Should_Use_ItemTemplate_To_Create_Control()
  19. {
  20. var target = new ItemsControl
  21. {
  22. Template = GetTemplate(),
  23. ItemTemplate = new FuncDataTemplate<string>((_, __) => new Canvas()),
  24. };
  25. target.Items = new[] { "Foo" };
  26. target.ApplyTemplate();
  27. target.Presenter.ApplyTemplate();
  28. var container = (ContentPresenter)target.Presenter.Panel.Children[0];
  29. container.UpdateChild();
  30. Assert.IsType<Canvas>(container.Child);
  31. }
  32. [Fact]
  33. public void Panel_Should_Have_TemplatedParent_Set_To_ItemsControl()
  34. {
  35. var target = new ItemsControl();
  36. target.Template = GetTemplate();
  37. target.Items = new[] { "Foo" };
  38. target.ApplyTemplate();
  39. target.Presenter.ApplyTemplate();
  40. Assert.Equal(target, target.Presenter.Panel.TemplatedParent);
  41. }
  42. [Fact]
  43. public void Container_Should_Have_TemplatedParent_Set_To_Null()
  44. {
  45. var target = new ItemsControl();
  46. target.Template = GetTemplate();
  47. target.Items = new[] { "Foo" };
  48. target.ApplyTemplate();
  49. target.Presenter.ApplyTemplate();
  50. var container = (ContentPresenter)target.Presenter.Panel.Children[0];
  51. Assert.Null(container.TemplatedParent);
  52. }
  53. [Fact]
  54. public void Container_Should_Have_LogicalParent_Set_To_ItemsControl()
  55. {
  56. using (UnitTestApplication.Start(TestServices.StyledWindow))
  57. {
  58. var root = new Window();
  59. var target = new ItemsControl();
  60. root.Content = target;
  61. var templatedParent = new Button();
  62. target.SetValue(StyledElement.TemplatedParentProperty, templatedParent);
  63. target.Template = GetTemplate();
  64. target.Items = new[] { "Foo" };
  65. root.ApplyTemplate();
  66. target.ApplyTemplate();
  67. target.Presenter.ApplyTemplate();
  68. var container = (ContentPresenter)target.Presenter.Panel.Children[0];
  69. Assert.Equal(target, container.Parent);
  70. }
  71. }
  72. [Fact]
  73. public void Control_Item_Should_Be_Logical_Child_Before_ApplyTemplate()
  74. {
  75. var target = new ItemsControl();
  76. var child = new Control();
  77. target.Template = GetTemplate();
  78. target.Items = new[] { child };
  79. Assert.Equal(child.Parent, target);
  80. Assert.Equal(child.GetLogicalParent(), target);
  81. Assert.Equal(new[] { child }, target.GetLogicalChildren());
  82. }
  83. [Fact]
  84. public void Added_Container_Should_Have_LogicalParent_Set_To_ItemsControl()
  85. {
  86. var item = new Border();
  87. var items = new ObservableCollection<Border>();
  88. var target = new ItemsControl
  89. {
  90. Template = GetTemplate(),
  91. Items = items,
  92. };
  93. var root = new TestRoot(true, target);
  94. root.Measure(new Size(100, 100));
  95. root.Arrange(new Rect(0, 0, 100, 100));
  96. items.Add(item);
  97. Assert.Equal(target, item.Parent);
  98. }
  99. [Fact]
  100. public void Control_Item_Should_Be_Removed_From_Logical_Children_Before_ApplyTemplate()
  101. {
  102. var target = new ItemsControl();
  103. var child = new Control();
  104. var items = new AvaloniaList<Control>(child);
  105. target.Template = GetTemplate();
  106. target.Items = items;
  107. items.RemoveAt(0);
  108. Assert.Null(child.Parent);
  109. Assert.Null(child.GetLogicalParent());
  110. Assert.Empty(target.GetLogicalChildren());
  111. }
  112. [Fact]
  113. public void Clearing_Items_Should_Clear_Child_Controls_Parent_Before_ApplyTemplate()
  114. {
  115. var target = new ItemsControl();
  116. var child = new Control();
  117. target.Template = GetTemplate();
  118. target.Items = new[] { child };
  119. target.Items = null;
  120. Assert.Null(child.Parent);
  121. Assert.Null(((ILogical)child).LogicalParent);
  122. }
  123. [Fact]
  124. public void Clearing_Items_Should_Clear_Child_Controls_Parent()
  125. {
  126. var target = new ItemsControl();
  127. var child = new Control();
  128. target.Template = GetTemplate();
  129. target.Items = new[] { child };
  130. target.ApplyTemplate();
  131. target.Items = null;
  132. Assert.Null(child.Parent);
  133. Assert.Null(((ILogical)child).LogicalParent);
  134. }
  135. [Fact]
  136. public void Adding_Control_Item_Should_Make_Control_Appear_In_LogicalChildren()
  137. {
  138. var target = new ItemsControl();
  139. var child = new Control();
  140. target.Template = GetTemplate();
  141. target.Items = new[] { child };
  142. // Should appear both before and after applying template.
  143. Assert.Equal(new ILogical[] { child }, target.GetLogicalChildren());
  144. target.ApplyTemplate();
  145. Assert.Equal(new ILogical[] { child }, target.GetLogicalChildren());
  146. }
  147. [Fact]
  148. public void Adding_String_Item_Should_Make_ContentPresenter_Appear_In_LogicalChildren()
  149. {
  150. var target = new ItemsControl();
  151. var child = new Control();
  152. target.Template = GetTemplate();
  153. target.Items = new[] { "Foo" };
  154. target.ApplyTemplate();
  155. target.Presenter.ApplyTemplate();
  156. var logical = (ILogical)target;
  157. Assert.Equal(1, logical.LogicalChildren.Count);
  158. Assert.IsType<ContentPresenter>(logical.LogicalChildren[0]);
  159. }
  160. [Fact]
  161. public void Setting_Items_To_Null_Should_Remove_LogicalChildren()
  162. {
  163. var target = new ItemsControl();
  164. var child = new Control();
  165. target.Template = GetTemplate();
  166. target.Items = new[] { "Foo" };
  167. target.ApplyTemplate();
  168. target.Presenter.ApplyTemplate();
  169. Assert.NotEmpty(target.GetLogicalChildren());
  170. target.Items = null;
  171. Assert.Equal(new ILogical[0], target.GetLogicalChildren());
  172. }
  173. [Fact]
  174. public void Setting_Items_Should_Fire_LogicalChildren_CollectionChanged()
  175. {
  176. var target = new ItemsControl();
  177. var child = new Control();
  178. var called = false;
  179. target.Template = GetTemplate();
  180. target.ApplyTemplate();
  181. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
  182. called = e.Action == NotifyCollectionChangedAction.Add;
  183. target.Items = new[] { child };
  184. Assert.True(called);
  185. }
  186. [Fact]
  187. public void Setting_Items_To_Null_Should_Fire_LogicalChildren_CollectionChanged()
  188. {
  189. var target = new ItemsControl();
  190. var child = new Control();
  191. var called = false;
  192. target.Template = GetTemplate();
  193. target.Items = new[] { child };
  194. target.ApplyTemplate();
  195. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
  196. called = e.Action == NotifyCollectionChangedAction.Remove;
  197. target.Items = null;
  198. Assert.True(called);
  199. }
  200. [Fact]
  201. public void Changing_Items_Should_Fire_LogicalChildren_CollectionChanged()
  202. {
  203. var target = new ItemsControl();
  204. var child = new Control();
  205. var called = false;
  206. target.Template = GetTemplate();
  207. target.Items = new[] { child };
  208. target.ApplyTemplate();
  209. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true;
  210. target.Items = new[] { "Foo" };
  211. Assert.True(called);
  212. }
  213. [Fact]
  214. public void Adding_Items_Should_Fire_LogicalChildren_CollectionChanged()
  215. {
  216. var target = new ItemsControl();
  217. var items = new AvaloniaList<string> { "Foo" };
  218. var called = false;
  219. target.Template = GetTemplate();
  220. target.Items = items;
  221. target.ApplyTemplate();
  222. target.Presenter.ApplyTemplate();
  223. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
  224. called = e.Action == NotifyCollectionChangedAction.Add;
  225. items.Add("Bar");
  226. Assert.True(called);
  227. }
  228. [Fact]
  229. public void Removing_Items_Should_Fire_LogicalChildren_CollectionChanged()
  230. {
  231. var target = new ItemsControl();
  232. var items = new AvaloniaList<string> { "Foo", "Bar" };
  233. var called = false;
  234. target.Template = GetTemplate();
  235. target.Items = items;
  236. target.ApplyTemplate();
  237. target.Presenter.ApplyTemplate();
  238. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
  239. called = e.Action == NotifyCollectionChangedAction.Remove;
  240. items.Remove("Bar");
  241. Assert.True(called);
  242. }
  243. [Fact]
  244. public void LogicalChildren_Should_Not_Change_Instance_When_Template_Changed()
  245. {
  246. var target = new ItemsControl()
  247. {
  248. Template = GetTemplate(),
  249. };
  250. var before = ((ILogical)target).LogicalChildren;
  251. target.Template = null;
  252. target.Template = GetTemplate();
  253. var after = ((ILogical)target).LogicalChildren;
  254. Assert.NotNull(before);
  255. Assert.NotNull(after);
  256. Assert.Same(before, after);
  257. }
  258. [Fact]
  259. public void Should_Clear_Containers_When_ItemsPresenter_Changes()
  260. {
  261. var target = new ItemsControl
  262. {
  263. Items = new[] { "foo", "bar" },
  264. Template = GetTemplate(),
  265. };
  266. target.ApplyTemplate();
  267. target.Presenter.ApplyTemplate();
  268. Assert.Equal(2, target.ItemContainerGenerator.Containers.Count());
  269. target.Template = GetTemplate();
  270. target.ApplyTemplate();
  271. Assert.Empty(target.ItemContainerGenerator.Containers);
  272. }
  273. [Fact]
  274. public void Empty_Class_Should_Initially_Be_Applied()
  275. {
  276. var target = new ItemsControl()
  277. {
  278. Template = GetTemplate(),
  279. };
  280. Assert.Contains(":empty", target.Classes);
  281. }
  282. [Fact]
  283. public void Empty_Class_Should_Be_Cleared_When_Items_Added()
  284. {
  285. var target = new ItemsControl()
  286. {
  287. Template = GetTemplate(),
  288. Items = new[] { 1, 2, 3 },
  289. };
  290. Assert.DoesNotContain(":empty", target.Classes);
  291. }
  292. [Fact]
  293. public void Empty_Class_Should_Be_Set_When_Empty_Collection_Set()
  294. {
  295. var target = new ItemsControl()
  296. {
  297. Template = GetTemplate(),
  298. Items = new[] { 1, 2, 3 },
  299. };
  300. target.Items = new int[0];
  301. Assert.Contains(":empty", target.Classes);
  302. }
  303. [Fact]
  304. public void Item_Count_Should_Be_Set_When_Items_Added()
  305. {
  306. var target = new ItemsControl()
  307. {
  308. Template = GetTemplate(),
  309. Items = new[] { 1, 2, 3 },
  310. };
  311. Assert.Equal(3, target.ItemCount);
  312. }
  313. [Fact]
  314. public void Item_Count_Should_Be_Set_When_Items_Changed()
  315. {
  316. var items = new ObservableCollection<int>() { 1, 2, 3 };
  317. var target = new ItemsControl()
  318. {
  319. Template = GetTemplate(),
  320. Items = items,
  321. };
  322. items.Add(4);
  323. Assert.Equal(4, target.ItemCount);
  324. items.Clear();
  325. Assert.Equal(0, target.ItemCount);
  326. }
  327. [Fact]
  328. public void Empty_Class_Should_Be_Set_When_Items_Collection_Cleared()
  329. {
  330. var items = new ObservableCollection<int>() { 1, 2, 3 };
  331. var target = new ItemsControl()
  332. {
  333. Template = GetTemplate(),
  334. Items = items,
  335. };
  336. items.Clear();
  337. Assert.Contains(":empty", target.Classes);
  338. }
  339. [Fact]
  340. public void Empty_Class_Should_Not_Be_Set_When_Items_Collection_Count_Increases()
  341. {
  342. var items = new ObservableCollection<int>() {};
  343. var target = new ItemsControl()
  344. {
  345. Template = GetTemplate(),
  346. Items = items,
  347. };
  348. items.Add(1);
  349. Assert.DoesNotContain(":empty", target.Classes);
  350. }
  351. [Fact]
  352. public void Setting_Presenter_Explicitly_Should_Set_Item_Parent()
  353. {
  354. var target = new TestItemsControl();
  355. var child = new Control();
  356. var presenter = new ItemsPresenter
  357. {
  358. [StyledElement.TemplatedParentProperty] = target,
  359. [~ItemsPresenter.ItemsProperty] = target[~ItemsControl.ItemsProperty],
  360. };
  361. presenter.ApplyTemplate();
  362. target.Presenter = presenter;
  363. target.Items = new[] { child };
  364. target.ApplyTemplate();
  365. Assert.Equal(target, child.Parent);
  366. Assert.Equal(target, ((ILogical)child).LogicalParent);
  367. }
  368. [Fact]
  369. public void DataContexts_Should_Be_Correctly_Set()
  370. {
  371. var items = new object[]
  372. {
  373. "Foo",
  374. new Item("Bar"),
  375. new TextBlock { Text = "Baz" },
  376. new ListBoxItem { Content = "Qux" },
  377. };
  378. var target = new ItemsControl
  379. {
  380. Template = GetTemplate(),
  381. DataContext = "Base",
  382. DataTemplates =
  383. {
  384. new FuncDataTemplate<Item>((x, __) => new Button { Content = x })
  385. },
  386. Items = items,
  387. };
  388. target.ApplyTemplate();
  389. target.Presenter.ApplyTemplate();
  390. var dataContexts = target.Presenter.Panel.Children
  391. .Do(x => (x as ContentPresenter)?.UpdateChild())
  392. .Cast<Control>()
  393. .Select(x => x.DataContext)
  394. .ToList();
  395. Assert.Equal(
  396. new object[] { items[0], items[1], "Base", "Base" },
  397. dataContexts);
  398. }
  399. [Fact]
  400. public void Control_Item_Should_Not_Be_NameScope()
  401. {
  402. var items = new object[]
  403. {
  404. new TextBlock(),
  405. };
  406. var target = new ItemsControl
  407. {
  408. Template = GetTemplate(),
  409. Items = items,
  410. };
  411. target.ApplyTemplate();
  412. target.Presenter.ApplyTemplate();
  413. var item = target.Presenter.Panel.LogicalChildren[0];
  414. Assert.Null(NameScope.GetNameScope((TextBlock)item));
  415. }
  416. [Fact]
  417. public void Focuses_Next_Item_On_Key_Down()
  418. {
  419. using (UnitTestApplication.Start(TestServices.RealFocus))
  420. {
  421. var items = new object[]
  422. {
  423. new Button(),
  424. new Button(),
  425. };
  426. var target = new ItemsControl
  427. {
  428. Template = GetTemplate(),
  429. Items = items,
  430. };
  431. var root = new TestRoot { Child = target };
  432. target.ApplyTemplate();
  433. target.Presenter.ApplyTemplate();
  434. target.Presenter.Panel.Children[0].Focus();
  435. target.RaiseEvent(new KeyEventArgs
  436. {
  437. RoutedEvent = InputElement.KeyDownEvent,
  438. Key = Key.Down,
  439. });
  440. Assert.Equal(
  441. target.Presenter.Panel.Children[1],
  442. FocusManager.Instance.Current);
  443. }
  444. }
  445. [Fact]
  446. public void Does_Not_Focus_Non_Focusable_Item_On_Key_Down()
  447. {
  448. using (UnitTestApplication.Start(TestServices.RealFocus))
  449. {
  450. var items = new object[]
  451. {
  452. new Button(),
  453. new Button { Focusable = false },
  454. new Button(),
  455. };
  456. var target = new ItemsControl
  457. {
  458. Template = GetTemplate(),
  459. Items = items,
  460. };
  461. var root = new TestRoot { Child = target };
  462. target.ApplyTemplate();
  463. target.Presenter.ApplyTemplate();
  464. target.Presenter.Panel.Children[0].Focus();
  465. target.RaiseEvent(new KeyEventArgs
  466. {
  467. RoutedEvent = InputElement.KeyDownEvent,
  468. Key = Key.Down,
  469. });
  470. Assert.Equal(
  471. target.Presenter.Panel.Children[2],
  472. FocusManager.Instance.Current);
  473. }
  474. }
  475. [Fact]
  476. public void Presenter_Items_Should_Be_In_Sync()
  477. {
  478. var target = new ItemsControl
  479. {
  480. Template = GetTemplate(),
  481. Items = new object[]
  482. {
  483. new Button(),
  484. new Button(),
  485. },
  486. };
  487. var root = new TestRoot { Child = target };
  488. var otherPanel = new StackPanel();
  489. target.ApplyTemplate();
  490. target.Presenter.ApplyTemplate();
  491. target.ItemContainerGenerator.Materialized += (s, e) =>
  492. {
  493. Assert.IsType<Canvas>(e.Containers[0].Item);
  494. };
  495. target.Items = new[]
  496. {
  497. new Canvas()
  498. };
  499. }
  500. [Fact]
  501. public void Detaching_Then_Reattaching_To_Logical_Tree_Twice_Does_Not_Throw()
  502. {
  503. // # Issue 3487
  504. var target = new ItemsControl
  505. {
  506. Template = GetTemplate(),
  507. Items = new[] { "foo", "bar" },
  508. ItemTemplate = new FuncDataTemplate<string>((_, __) => new Canvas()),
  509. };
  510. var root = new TestRoot(target);
  511. root.Measure(Size.Infinity);
  512. root.Arrange(new Rect(root.DesiredSize));
  513. root.Child = null;
  514. root.Child = target;
  515. target.Measure(Size.Infinity);
  516. root.Child = null;
  517. root.Child = target;
  518. }
  519. private class Item
  520. {
  521. public Item(string value)
  522. {
  523. Value = value;
  524. }
  525. public string Value { get; }
  526. }
  527. private FuncControlTemplate GetTemplate()
  528. {
  529. return new FuncControlTemplate<ItemsControl>((parent, scope) =>
  530. {
  531. return new Border
  532. {
  533. Background = new Media.SolidColorBrush(0xffffffff),
  534. Child = new ItemsPresenter
  535. {
  536. Name = "PART_ItemsPresenter",
  537. [~ItemsPresenter.ItemsProperty] = parent[~ItemsControl.ItemsProperty],
  538. }.RegisterInNameScope(scope)
  539. };
  540. });
  541. }
  542. private class TestItemsControl : ItemsControl
  543. {
  544. public new IItemsPresenter Presenter
  545. {
  546. get { return base.Presenter; }
  547. set { base.Presenter = value; }
  548. }
  549. }
  550. }
  551. }