ItemsControlTests.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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_Items_Not_Set()
  294. {
  295. var target = new ItemsControl()
  296. {
  297. Template = GetTemplate(),
  298. };
  299. Assert.Contains(":empty", target.Classes);
  300. }
  301. [Fact]
  302. public void Empty_Class_Should_Be_Set_When_Empty_Collection_Set()
  303. {
  304. var target = new ItemsControl()
  305. {
  306. Template = GetTemplate(),
  307. Items = new[] { 1, 2, 3 },
  308. };
  309. target.Items = new int[0];
  310. Assert.Contains(":empty", target.Classes);
  311. }
  312. [Fact]
  313. public void Item_Count_Should_Be_Set_When_Items_Added()
  314. {
  315. var target = new ItemsControl()
  316. {
  317. Template = GetTemplate(),
  318. Items = new[] { 1, 2, 3 },
  319. };
  320. Assert.Equal(3, target.ItemCount);
  321. }
  322. [Fact]
  323. public void Item_Count_Should_Be_Set_When_Items_Changed()
  324. {
  325. var items = new ObservableCollection<int>() { 1, 2, 3 };
  326. var target = new ItemsControl()
  327. {
  328. Template = GetTemplate(),
  329. Items = items,
  330. };
  331. items.Add(4);
  332. Assert.Equal(4, target.ItemCount);
  333. items.Clear();
  334. Assert.Equal(0, target.ItemCount);
  335. }
  336. [Fact]
  337. public void Empty_Class_Should_Be_Set_When_Items_Collection_Cleared()
  338. {
  339. var items = new ObservableCollection<int>() { 1, 2, 3 };
  340. var target = new ItemsControl()
  341. {
  342. Template = GetTemplate(),
  343. Items = items,
  344. };
  345. items.Clear();
  346. Assert.Contains(":empty", target.Classes);
  347. }
  348. [Fact]
  349. public void Empty_Class_Should_Not_Be_Set_When_Items_Collection_Count_Increases()
  350. {
  351. var items = new ObservableCollection<int>() { };
  352. var target = new ItemsControl()
  353. {
  354. Template = GetTemplate(),
  355. Items = items,
  356. };
  357. items.Add(1);
  358. Assert.DoesNotContain(":empty", target.Classes);
  359. }
  360. [Fact]
  361. public void Single_Item_Class_Should_Be_Set_When_Items_Collection_Count_Increases_To_One()
  362. {
  363. var items = new ObservableCollection<int>() { };
  364. var target = new ItemsControl()
  365. {
  366. Template = GetTemplate(),
  367. Items = items,
  368. };
  369. items.Add(1);
  370. Assert.Contains(":singleitem", target.Classes);
  371. }
  372. [Fact]
  373. public void Empty_Class_Should_Not_Be_Set_When_Items_Collection_Cleared()
  374. {
  375. var items = new ObservableCollection<int>() { 1, 2, 3 };
  376. var target = new ItemsControl()
  377. {
  378. Template = GetTemplate(),
  379. Items = items,
  380. };
  381. items.Clear();
  382. Assert.DoesNotContain(":singleitem", target.Classes);
  383. }
  384. [Fact]
  385. public void Single_Item_Class_Should_Not_Be_Set_When_Items_Collection_Count_Increases_Beyond_One()
  386. {
  387. var items = new ObservableCollection<int>() { 1 };
  388. var target = new ItemsControl()
  389. {
  390. Template = GetTemplate(),
  391. Items = items,
  392. };
  393. items.Add(2);
  394. Assert.DoesNotContain(":singleitem", target.Classes);
  395. }
  396. [Fact]
  397. public void Setting_Presenter_Explicitly_Should_Set_Item_Parent()
  398. {
  399. var target = new TestItemsControl();
  400. var child = new Control();
  401. var presenter = new ItemsPresenter
  402. {
  403. [StyledElement.TemplatedParentProperty] = target,
  404. [~ItemsPresenter.ItemsProperty] = target[~ItemsControl.ItemsProperty],
  405. };
  406. presenter.ApplyTemplate();
  407. target.Presenter = presenter;
  408. target.Items = new[] { child };
  409. target.ApplyTemplate();
  410. Assert.Equal(target, child.Parent);
  411. Assert.Equal(target, ((ILogical)child).LogicalParent);
  412. }
  413. [Fact]
  414. public void DataContexts_Should_Be_Correctly_Set()
  415. {
  416. var items = new object[]
  417. {
  418. "Foo",
  419. new Item("Bar"),
  420. new TextBlock { Text = "Baz" },
  421. new ListBoxItem { Content = "Qux" },
  422. };
  423. var target = new ItemsControl
  424. {
  425. Template = GetTemplate(),
  426. DataContext = "Base",
  427. DataTemplates =
  428. {
  429. new FuncDataTemplate<Item>((x, __) => new Button { Content = x })
  430. },
  431. Items = items,
  432. };
  433. target.ApplyTemplate();
  434. target.Presenter.ApplyTemplate();
  435. var dataContexts = target.Presenter.Panel.Children
  436. .Do(x => (x as ContentPresenter)?.UpdateChild())
  437. .Cast<Control>()
  438. .Select(x => x.DataContext)
  439. .ToList();
  440. Assert.Equal(
  441. new object[] { items[0], items[1], "Base", "Base" },
  442. dataContexts);
  443. }
  444. [Fact]
  445. public void Control_Item_Should_Not_Be_NameScope()
  446. {
  447. var items = new object[]
  448. {
  449. new TextBlock(),
  450. };
  451. var target = new ItemsControl
  452. {
  453. Template = GetTemplate(),
  454. Items = items,
  455. };
  456. target.ApplyTemplate();
  457. target.Presenter.ApplyTemplate();
  458. var item = target.Presenter.Panel.LogicalChildren[0];
  459. Assert.Null(NameScope.GetNameScope((TextBlock)item));
  460. }
  461. [Fact]
  462. public void Focuses_Next_Item_On_Key_Down()
  463. {
  464. using (UnitTestApplication.Start(TestServices.RealFocus))
  465. {
  466. var items = new object[]
  467. {
  468. new Button(),
  469. new Button(),
  470. };
  471. var target = new ItemsControl
  472. {
  473. Template = GetTemplate(),
  474. Items = items,
  475. };
  476. var root = new TestRoot { Child = target };
  477. target.ApplyTemplate();
  478. target.Presenter.ApplyTemplate();
  479. target.Presenter.Panel.Children[0].Focus();
  480. target.RaiseEvent(new KeyEventArgs
  481. {
  482. RoutedEvent = InputElement.KeyDownEvent,
  483. Key = Key.Down,
  484. });
  485. Assert.Equal(
  486. target.Presenter.Panel.Children[1],
  487. FocusManager.Instance.Current);
  488. }
  489. }
  490. [Fact]
  491. public void Does_Not_Focus_Non_Focusable_Item_On_Key_Down()
  492. {
  493. using (UnitTestApplication.Start(TestServices.RealFocus))
  494. {
  495. var items = new object[]
  496. {
  497. new Button(),
  498. new Button { Focusable = false },
  499. new Button(),
  500. };
  501. var target = new ItemsControl
  502. {
  503. Template = GetTemplate(),
  504. Items = items,
  505. };
  506. var root = new TestRoot { Child = target };
  507. target.ApplyTemplate();
  508. target.Presenter.ApplyTemplate();
  509. target.Presenter.Panel.Children[0].Focus();
  510. target.RaiseEvent(new KeyEventArgs
  511. {
  512. RoutedEvent = InputElement.KeyDownEvent,
  513. Key = Key.Down,
  514. });
  515. Assert.Equal(
  516. target.Presenter.Panel.Children[2],
  517. FocusManager.Instance.Current);
  518. }
  519. }
  520. [Fact]
  521. public void Presenter_Items_Should_Be_In_Sync()
  522. {
  523. var target = new ItemsControl
  524. {
  525. Template = GetTemplate(),
  526. Items = new object[]
  527. {
  528. new Button(),
  529. new Button(),
  530. },
  531. };
  532. var root = new TestRoot { Child = target };
  533. var otherPanel = new StackPanel();
  534. target.ApplyTemplate();
  535. target.Presenter.ApplyTemplate();
  536. target.ItemContainerGenerator.Materialized += (s, e) =>
  537. {
  538. Assert.IsType<Canvas>(e.Containers[0].Item);
  539. };
  540. target.Items = new[]
  541. {
  542. new Canvas()
  543. };
  544. }
  545. [Fact]
  546. public void Detaching_Then_Reattaching_To_Logical_Tree_Twice_Does_Not_Throw()
  547. {
  548. // # Issue 3487
  549. var target = new ItemsControl
  550. {
  551. Template = GetTemplate(),
  552. Items = new[] { "foo", "bar" },
  553. ItemTemplate = new FuncDataTemplate<string>((_, __) => new Canvas()),
  554. };
  555. var root = new TestRoot(target);
  556. root.Measure(Size.Infinity);
  557. root.Arrange(new Rect(root.DesiredSize));
  558. root.Child = null;
  559. root.Child = target;
  560. target.Measure(Size.Infinity);
  561. root.Child = null;
  562. root.Child = target;
  563. }
  564. private class Item
  565. {
  566. public Item(string value)
  567. {
  568. Value = value;
  569. }
  570. public string Value { get; }
  571. }
  572. private FuncControlTemplate GetTemplate()
  573. {
  574. return new FuncControlTemplate<ItemsControl>((parent, scope) =>
  575. {
  576. return new Border
  577. {
  578. Background = new Media.SolidColorBrush(0xffffffff),
  579. Child = new ItemsPresenter
  580. {
  581. Name = "PART_ItemsPresenter",
  582. [~ItemsPresenter.ItemsProperty] = parent[~ItemsControl.ItemsProperty],
  583. }.RegisterInNameScope(scope)
  584. };
  585. });
  586. }
  587. private class TestItemsControl : ItemsControl
  588. {
  589. public new IItemsPresenter Presenter
  590. {
  591. get { return base.Presenter; }
  592. set { base.Presenter = value; }
  593. }
  594. }
  595. }
  596. }