ItemsControlTests.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System.Collections.Specialized;
  4. using System.Linq;
  5. using Avalonia.Collections;
  6. using Avalonia.Controls.Presenters;
  7. using Avalonia.Controls.Templates;
  8. using Avalonia.LogicalTree;
  9. using Avalonia.VisualTree;
  10. using Xunit;
  11. using System.Collections.ObjectModel;
  12. using Avalonia.UnitTests;
  13. using Avalonia.Input;
  14. namespace Avalonia.Controls.UnitTests
  15. {
  16. public class ItemsControlTests
  17. {
  18. [Fact]
  19. public void Should_Use_ItemTemplate_To_Create_Control()
  20. {
  21. var target = new ItemsControl
  22. {
  23. Template = GetTemplate(),
  24. ItemTemplate = new FuncDataTemplate<string>((_, __) => new Canvas()),
  25. };
  26. target.Items = new[] { "Foo" };
  27. target.ApplyTemplate();
  28. target.Presenter.ApplyTemplate();
  29. var container = (ContentPresenter)target.Presenter.Panel.Children[0];
  30. container.UpdateChild();
  31. Assert.IsType<Canvas>(container.Child);
  32. }
  33. [Fact]
  34. public void Panel_Should_Have_TemplatedParent_Set_To_ItemsControl()
  35. {
  36. var target = new ItemsControl();
  37. target.Template = GetTemplate();
  38. target.Items = new[] { "Foo" };
  39. target.ApplyTemplate();
  40. target.Presenter.ApplyTemplate();
  41. Assert.Equal(target, target.Presenter.Panel.TemplatedParent);
  42. }
  43. [Fact]
  44. public void Container_Should_Have_TemplatedParent_Set_To_Null()
  45. {
  46. var target = new ItemsControl();
  47. target.Template = GetTemplate();
  48. target.Items = new[] { "Foo" };
  49. target.ApplyTemplate();
  50. target.Presenter.ApplyTemplate();
  51. var container = (ContentPresenter)target.Presenter.Panel.Children[0];
  52. Assert.Null(container.TemplatedParent);
  53. }
  54. [Fact]
  55. public void Container_Child_Should_Have_LogicalParent_Set_To_Container()
  56. {
  57. using (UnitTestApplication.Start(TestServices.StyledWindow))
  58. {
  59. var root = new Window();
  60. var target = new ItemsControl();
  61. root.Content = target;
  62. var templatedParent = new Button();
  63. target.SetValue(StyledElement.TemplatedParentProperty, templatedParent);
  64. target.Template = GetTemplate();
  65. target.Items = new[] { "Foo" };
  66. root.ApplyTemplate();
  67. target.ApplyTemplate();
  68. target.Presenter.ApplyTemplate();
  69. var container = (ContentPresenter)target.Presenter.Panel.Children[0];
  70. Assert.Equal(container, container.Child.Parent);
  71. }
  72. }
  73. [Fact]
  74. public void Control_Item_Should_Be_Logical_Child_Before_ApplyTemplate()
  75. {
  76. var target = new ItemsControl();
  77. var child = new Control();
  78. target.Template = GetTemplate();
  79. target.Items = new[] { child };
  80. Assert.Equal(child.Parent, target);
  81. Assert.Equal(child.GetLogicalParent(), target);
  82. Assert.Equal(new[] { child }, target.GetLogicalChildren());
  83. }
  84. [Fact]
  85. public void Control_Item_Should_Be_Removed_From_Logical_Children_Before_ApplyTemplate()
  86. {
  87. var target = new ItemsControl();
  88. var child = new Control();
  89. var items = new AvaloniaList<Control>(child);
  90. target.Template = GetTemplate();
  91. target.Items = items;
  92. items.RemoveAt(0);
  93. Assert.Null(child.Parent);
  94. Assert.Null(child.GetLogicalParent());
  95. Assert.Empty(target.GetLogicalChildren());
  96. }
  97. [Fact]
  98. public void Clearing_Items_Should_Clear_Child_Controls_Parent_Before_ApplyTemplate()
  99. {
  100. var target = new ItemsControl();
  101. var child = new Control();
  102. target.Template = GetTemplate();
  103. target.Items = new[] { child };
  104. target.Items = null;
  105. Assert.Null(child.Parent);
  106. Assert.Null(((ILogical)child).LogicalParent);
  107. }
  108. [Fact]
  109. public void Clearing_Items_Should_Clear_Child_Controls_Parent()
  110. {
  111. var target = new ItemsControl();
  112. var child = new Control();
  113. target.Template = GetTemplate();
  114. target.Items = new[] { child };
  115. target.ApplyTemplate();
  116. target.Items = null;
  117. Assert.Null(child.Parent);
  118. Assert.Null(((ILogical)child).LogicalParent);
  119. }
  120. [Fact]
  121. public void Adding_Control_Item_Should_Make_Control_Appear_In_LogicalChildren()
  122. {
  123. var target = new ItemsControl();
  124. var child = new Control();
  125. target.Template = GetTemplate();
  126. target.Items = new[] { child };
  127. // Should appear both before and after applying template.
  128. Assert.Equal(new ILogical[] { child }, target.GetLogicalChildren());
  129. target.ApplyTemplate();
  130. Assert.Equal(new ILogical[] { child }, target.GetLogicalChildren());
  131. }
  132. [Fact]
  133. public void Adding_String_Item_Should_Make_TextBlock_Appear_In_LogicalChildren()
  134. {
  135. var target = new ItemsControl();
  136. var child = new Control();
  137. target.Template = GetTemplate();
  138. target.Items = new[] { "Foo" };
  139. target.ApplyTemplate();
  140. target.Presenter.ApplyTemplate();
  141. var logical = (ILogical)target;
  142. Assert.Equal(1, logical.LogicalChildren.Count);
  143. Assert.IsType<TextBlock>(logical.LogicalChildren[0]);
  144. }
  145. [Fact]
  146. public void Setting_Items_To_Null_Should_Remove_LogicalChildren()
  147. {
  148. var target = new ItemsControl();
  149. var child = new Control();
  150. target.Template = GetTemplate();
  151. target.Items = new[] { "Foo" };
  152. target.ApplyTemplate();
  153. target.Presenter.ApplyTemplate();
  154. Assert.NotEmpty(target.GetLogicalChildren());
  155. target.Items = null;
  156. Assert.Equal(new ILogical[0], target.GetLogicalChildren());
  157. }
  158. [Fact]
  159. public void Setting_Items_Should_Fire_LogicalChildren_CollectionChanged()
  160. {
  161. var target = new ItemsControl();
  162. var child = new Control();
  163. var called = false;
  164. target.Template = GetTemplate();
  165. target.ApplyTemplate();
  166. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
  167. called = e.Action == NotifyCollectionChangedAction.Add;
  168. target.Items = new[] { child };
  169. Assert.True(called);
  170. }
  171. [Fact]
  172. public void Setting_Items_To_Null_Should_Fire_LogicalChildren_CollectionChanged()
  173. {
  174. var target = new ItemsControl();
  175. var child = new Control();
  176. var called = false;
  177. target.Template = GetTemplate();
  178. target.Items = new[] { child };
  179. target.ApplyTemplate();
  180. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
  181. called = e.Action == NotifyCollectionChangedAction.Remove;
  182. target.Items = null;
  183. Assert.True(called);
  184. }
  185. [Fact]
  186. public void Changing_Items_Should_Fire_LogicalChildren_CollectionChanged()
  187. {
  188. var target = new ItemsControl();
  189. var child = new Control();
  190. var called = false;
  191. target.Template = GetTemplate();
  192. target.Items = new[] { child };
  193. target.ApplyTemplate();
  194. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true;
  195. target.Items = new[] { "Foo" };
  196. Assert.True(called);
  197. }
  198. [Fact]
  199. public void Adding_Items_Should_Fire_LogicalChildren_CollectionChanged()
  200. {
  201. var target = new ItemsControl();
  202. var items = new AvaloniaList<string> { "Foo" };
  203. var called = false;
  204. target.Template = GetTemplate();
  205. target.Items = items;
  206. target.ApplyTemplate();
  207. target.Presenter.ApplyTemplate();
  208. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
  209. called = e.Action == NotifyCollectionChangedAction.Add;
  210. items.Add("Bar");
  211. Assert.True(called);
  212. }
  213. [Fact]
  214. public void Removing_Items_Should_Fire_LogicalChildren_CollectionChanged()
  215. {
  216. var target = new ItemsControl();
  217. var items = new AvaloniaList<string> { "Foo", "Bar" };
  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.Remove;
  225. items.Remove("Bar");
  226. Assert.True(called);
  227. }
  228. [Fact]
  229. public void LogicalChildren_Should_Not_Change_Instance_When_Template_Changed()
  230. {
  231. var target = new ItemsControl()
  232. {
  233. Template = GetTemplate(),
  234. };
  235. var before = ((ILogical)target).LogicalChildren;
  236. target.Template = null;
  237. target.Template = GetTemplate();
  238. var after = ((ILogical)target).LogicalChildren;
  239. Assert.NotNull(before);
  240. Assert.NotNull(after);
  241. Assert.Same(before, after);
  242. }
  243. [Fact]
  244. public void Should_Clear_Containers_When_ItemsPresenter_Changes()
  245. {
  246. var target = new ItemsControl
  247. {
  248. Items = new[] { "foo", "bar" },
  249. Template = GetTemplate(),
  250. };
  251. target.ApplyTemplate();
  252. target.Presenter.ApplyTemplate();
  253. Assert.Equal(2, target.ItemContainerGenerator.Containers.Count());
  254. target.Template = GetTemplate();
  255. target.ApplyTemplate();
  256. Assert.Empty(target.ItemContainerGenerator.Containers);
  257. }
  258. [Fact]
  259. public void Empty_Class_Should_Initially_Be_Applied()
  260. {
  261. var target = new ItemsControl()
  262. {
  263. Template = GetTemplate(),
  264. };
  265. Assert.Contains(":empty", target.Classes);
  266. }
  267. [Fact]
  268. public void Empty_Class_Should_Be_Cleared_When_Items_Added()
  269. {
  270. var target = new ItemsControl()
  271. {
  272. Template = GetTemplate(),
  273. Items = new[] { 1, 2, 3 },
  274. };
  275. Assert.DoesNotContain(":empty", target.Classes);
  276. }
  277. [Fact]
  278. public void Empty_Class_Should_Be_Set_When_Empty_Collection_Set()
  279. {
  280. var target = new ItemsControl()
  281. {
  282. Template = GetTemplate(),
  283. Items = new[] { 1, 2, 3 },
  284. };
  285. target.Items = new int[0];
  286. Assert.Contains(":empty", target.Classes);
  287. }
  288. [Fact]
  289. public void Setting_Presenter_Explicitly_Should_Set_Item_Parent()
  290. {
  291. var target = new TestItemsControl();
  292. var child = new Control();
  293. var presenter = new ItemsPresenter
  294. {
  295. [StyledElement.TemplatedParentProperty] = target,
  296. [~ItemsPresenter.ItemsProperty] = target[~ItemsControl.ItemsProperty],
  297. };
  298. presenter.ApplyTemplate();
  299. target.Presenter = presenter;
  300. target.Items = new[] { child };
  301. target.ApplyTemplate();
  302. Assert.Equal(target, child.Parent);
  303. Assert.Equal(target, ((ILogical)child).LogicalParent);
  304. }
  305. [Fact]
  306. public void DataContexts_Should_Be_Correctly_Set()
  307. {
  308. var items = new object[]
  309. {
  310. "Foo",
  311. new Item("Bar"),
  312. new TextBlock { Text = "Baz" },
  313. new ListBoxItem { Content = "Qux" },
  314. };
  315. var target = new ItemsControl
  316. {
  317. Template = GetTemplate(),
  318. DataContext = "Base",
  319. DataTemplates =
  320. {
  321. new FuncDataTemplate<Item>((x, __) => new Button { Content = x })
  322. },
  323. Items = items,
  324. };
  325. target.ApplyTemplate();
  326. target.Presenter.ApplyTemplate();
  327. var dataContexts = target.Presenter.Panel.Children
  328. .Do(x => (x as ContentPresenter)?.UpdateChild())
  329. .Cast<Control>()
  330. .Select(x => x.DataContext)
  331. .ToList();
  332. Assert.Equal(
  333. new object[] { items[0], items[1], "Base", "Base" },
  334. dataContexts);
  335. }
  336. [Fact]
  337. public void Control_Item_Should_Not_Be_NameScope()
  338. {
  339. var items = new object[]
  340. {
  341. new TextBlock(),
  342. };
  343. var target = new ItemsControl
  344. {
  345. Template = GetTemplate(),
  346. Items = items,
  347. };
  348. target.ApplyTemplate();
  349. target.Presenter.ApplyTemplate();
  350. var item = target.Presenter.Panel.LogicalChildren[0];
  351. Assert.Null(NameScope.GetNameScope((TextBlock)item));
  352. }
  353. [Fact]
  354. public void Focuses_Next_Item_On_Key_Down()
  355. {
  356. using (UnitTestApplication.Start(TestServices.RealFocus))
  357. {
  358. var items = new object[]
  359. {
  360. new Button(),
  361. new Button(),
  362. };
  363. var target = new ItemsControl
  364. {
  365. Template = GetTemplate(),
  366. Items = items,
  367. };
  368. var root = new TestRoot { Child = target };
  369. target.ApplyTemplate();
  370. target.Presenter.ApplyTemplate();
  371. target.Presenter.Panel.Children[0].Focus();
  372. target.RaiseEvent(new KeyEventArgs
  373. {
  374. RoutedEvent = InputElement.KeyDownEvent,
  375. Key = Key.Down,
  376. });
  377. Assert.Equal(
  378. target.Presenter.Panel.Children[1],
  379. FocusManager.Instance.Current);
  380. }
  381. }
  382. [Fact]
  383. public void Does_Not_Focus_Non_Focusable_Item_On_Key_Down()
  384. {
  385. using (UnitTestApplication.Start(TestServices.RealFocus))
  386. {
  387. var items = new object[]
  388. {
  389. new Button(),
  390. new Button { Focusable = false },
  391. new Button(),
  392. };
  393. var target = new ItemsControl
  394. {
  395. Template = GetTemplate(),
  396. Items = items,
  397. };
  398. var root = new TestRoot { Child = target };
  399. target.ApplyTemplate();
  400. target.Presenter.ApplyTemplate();
  401. target.Presenter.Panel.Children[0].Focus();
  402. target.RaiseEvent(new KeyEventArgs
  403. {
  404. RoutedEvent = InputElement.KeyDownEvent,
  405. Key = Key.Down,
  406. });
  407. Assert.Equal(
  408. target.Presenter.Panel.Children[2],
  409. FocusManager.Instance.Current);
  410. }
  411. }
  412. private class Item
  413. {
  414. public Item(string value)
  415. {
  416. Value = value;
  417. }
  418. public string Value { get; }
  419. }
  420. private FuncControlTemplate GetTemplate()
  421. {
  422. return new FuncControlTemplate<ItemsControl>((parent, scope) =>
  423. {
  424. return new Border
  425. {
  426. Background = new Media.SolidColorBrush(0xffffffff),
  427. Child = new ItemsPresenter
  428. {
  429. Name = "PART_ItemsPresenter",
  430. [~ItemsPresenter.ItemsProperty] = parent[~ItemsControl.ItemsProperty],
  431. }.RegisterInNameScope(scope)
  432. };
  433. });
  434. }
  435. private class TestItemsControl : ItemsControl
  436. {
  437. public new IItemsPresenter Presenter
  438. {
  439. get { return base.Presenter; }
  440. set { base.Presenter = value; }
  441. }
  442. }
  443. }
  444. }