ItemsControlTests.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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 Setting_Presenter_Explicitly_Should_Set_Item_Parent()
  305. {
  306. var target = new TestItemsControl();
  307. var child = new Control();
  308. var presenter = new ItemsPresenter
  309. {
  310. [StyledElement.TemplatedParentProperty] = target,
  311. [~ItemsPresenter.ItemsProperty] = target[~ItemsControl.ItemsProperty],
  312. };
  313. presenter.ApplyTemplate();
  314. target.Presenter = presenter;
  315. target.Items = new[] { child };
  316. target.ApplyTemplate();
  317. Assert.Equal(target, child.Parent);
  318. Assert.Equal(target, ((ILogical)child).LogicalParent);
  319. }
  320. [Fact]
  321. public void DataContexts_Should_Be_Correctly_Set()
  322. {
  323. var items = new object[]
  324. {
  325. "Foo",
  326. new Item("Bar"),
  327. new TextBlock { Text = "Baz" },
  328. new ListBoxItem { Content = "Qux" },
  329. };
  330. var target = new ItemsControl
  331. {
  332. Template = GetTemplate(),
  333. DataContext = "Base",
  334. DataTemplates =
  335. {
  336. new FuncDataTemplate<Item>((x, __) => new Button { Content = x })
  337. },
  338. Items = items,
  339. };
  340. target.ApplyTemplate();
  341. target.Presenter.ApplyTemplate();
  342. var dataContexts = target.Presenter.Panel.Children
  343. .Do(x => (x as ContentPresenter)?.UpdateChild())
  344. .Cast<Control>()
  345. .Select(x => x.DataContext)
  346. .ToList();
  347. Assert.Equal(
  348. new object[] { items[0], items[1], "Base", "Base" },
  349. dataContexts);
  350. }
  351. [Fact]
  352. public void Control_Item_Should_Not_Be_NameScope()
  353. {
  354. var items = new object[]
  355. {
  356. new TextBlock(),
  357. };
  358. var target = new ItemsControl
  359. {
  360. Template = GetTemplate(),
  361. Items = items,
  362. };
  363. target.ApplyTemplate();
  364. target.Presenter.ApplyTemplate();
  365. var item = target.Presenter.Panel.LogicalChildren[0];
  366. Assert.Null(NameScope.GetNameScope((TextBlock)item));
  367. }
  368. [Fact]
  369. public void Focuses_Next_Item_On_Key_Down()
  370. {
  371. using (UnitTestApplication.Start(TestServices.RealFocus))
  372. {
  373. var items = new object[]
  374. {
  375. new Button(),
  376. new Button(),
  377. };
  378. var target = new ItemsControl
  379. {
  380. Template = GetTemplate(),
  381. Items = items,
  382. };
  383. var root = new TestRoot { Child = target };
  384. target.ApplyTemplate();
  385. target.Presenter.ApplyTemplate();
  386. target.Presenter.Panel.Children[0].Focus();
  387. target.RaiseEvent(new KeyEventArgs
  388. {
  389. RoutedEvent = InputElement.KeyDownEvent,
  390. Key = Key.Down,
  391. });
  392. Assert.Equal(
  393. target.Presenter.Panel.Children[1],
  394. FocusManager.Instance.Current);
  395. }
  396. }
  397. [Fact]
  398. public void Does_Not_Focus_Non_Focusable_Item_On_Key_Down()
  399. {
  400. using (UnitTestApplication.Start(TestServices.RealFocus))
  401. {
  402. var items = new object[]
  403. {
  404. new Button(),
  405. new Button { Focusable = false },
  406. new Button(),
  407. };
  408. var target = new ItemsControl
  409. {
  410. Template = GetTemplate(),
  411. Items = items,
  412. };
  413. var root = new TestRoot { Child = target };
  414. target.ApplyTemplate();
  415. target.Presenter.ApplyTemplate();
  416. target.Presenter.Panel.Children[0].Focus();
  417. target.RaiseEvent(new KeyEventArgs
  418. {
  419. RoutedEvent = InputElement.KeyDownEvent,
  420. Key = Key.Down,
  421. });
  422. Assert.Equal(
  423. target.Presenter.Panel.Children[2],
  424. FocusManager.Instance.Current);
  425. }
  426. }
  427. [Fact]
  428. public void Presenter_Items_Should_Be_In_Sync()
  429. {
  430. var target = new ItemsControl
  431. {
  432. Template = GetTemplate(),
  433. Items = new object[]
  434. {
  435. new Button(),
  436. new Button(),
  437. },
  438. };
  439. var root = new TestRoot { Child = target };
  440. var otherPanel = new StackPanel();
  441. target.ApplyTemplate();
  442. target.Presenter.ApplyTemplate();
  443. target.ItemContainerGenerator.Materialized += (s, e) =>
  444. {
  445. Assert.IsType<Canvas>(e.Containers[0].Item);
  446. };
  447. target.Items = new[]
  448. {
  449. new Canvas()
  450. };
  451. }
  452. [Fact]
  453. public void Detaching_Then_Reattaching_To_Logical_Tree_Twice_Does_Not_Throw()
  454. {
  455. // # Issue 3487
  456. var target = new ItemsControl
  457. {
  458. Template = GetTemplate(),
  459. Items = new[] { "foo", "bar" },
  460. ItemTemplate = new FuncDataTemplate<string>((_, __) => new Canvas()),
  461. };
  462. var root = new TestRoot(target);
  463. root.Measure(Size.Infinity);
  464. root.Arrange(new Rect(root.DesiredSize));
  465. root.Child = null;
  466. root.Child = target;
  467. target.Measure(Size.Infinity);
  468. root.Child = null;
  469. root.Child = target;
  470. }
  471. private class Item
  472. {
  473. public Item(string value)
  474. {
  475. Value = value;
  476. }
  477. public string Value { get; }
  478. }
  479. private FuncControlTemplate GetTemplate()
  480. {
  481. return new FuncControlTemplate<ItemsControl>((parent, scope) =>
  482. {
  483. return new Border
  484. {
  485. Background = new Media.SolidColorBrush(0xffffffff),
  486. Child = new ItemsPresenter
  487. {
  488. Name = "PART_ItemsPresenter",
  489. [~ItemsPresenter.ItemsProperty] = parent[~ItemsControl.ItemsProperty],
  490. }.RegisterInNameScope(scope)
  491. };
  492. });
  493. }
  494. private class TestItemsControl : ItemsControl
  495. {
  496. public new IItemsPresenter Presenter
  497. {
  498. get { return base.Presenter; }
  499. set { base.Presenter = value; }
  500. }
  501. }
  502. }
  503. }