TabControlTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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;
  4. using System.Collections.ObjectModel;
  5. using System.Linq;
  6. using Avalonia.Collections;
  7. using Avalonia.Controls.Presenters;
  8. using Avalonia.Controls.Primitives;
  9. using Avalonia.Controls.Templates;
  10. using Avalonia.LogicalTree;
  11. using Avalonia.Styling;
  12. using Avalonia.UnitTests;
  13. using Xunit;
  14. namespace Avalonia.Controls.UnitTests
  15. {
  16. public class TabControlTests
  17. {
  18. [Fact]
  19. public void First_Tab_Should_Be_Selected_By_Default()
  20. {
  21. TabItem selected;
  22. var target = new TabControl
  23. {
  24. Template = TabControlTemplate(),
  25. Items = new[]
  26. {
  27. (selected = new TabItem
  28. {
  29. Name = "first",
  30. Content = "foo",
  31. }),
  32. new TabItem
  33. {
  34. Name = "second",
  35. Content = "bar",
  36. },
  37. }
  38. };
  39. target.ApplyTemplate();
  40. Assert.Equal(0, target.SelectedIndex);
  41. Assert.Equal(selected, target.SelectedItem);
  42. }
  43. [Fact]
  44. public void Pre_Selecting_TabItem_Should_Set_SelectedContent_After_It_Was_Added()
  45. {
  46. var target = new TabControl
  47. {
  48. Template = TabControlTemplate(),
  49. };
  50. const string secondContent = "Second";
  51. var items = new AvaloniaList<object>
  52. {
  53. new TabItem { Header = "First"},
  54. new TabItem { Header = "Second", Content = secondContent, IsSelected = true }
  55. };
  56. target.Items = items;
  57. ApplyTemplate(target);
  58. Assert.Equal(secondContent, target.SelectedContent);
  59. }
  60. [Fact]
  61. public void Logical_Children_Should_Be_TabItems()
  62. {
  63. var items = new[]
  64. {
  65. new TabItem
  66. {
  67. Content = "foo"
  68. },
  69. new TabItem
  70. {
  71. Content = "bar"
  72. },
  73. };
  74. var target = new TabControl
  75. {
  76. Template = TabControlTemplate(),
  77. Items = items,
  78. };
  79. Assert.Equal(items, target.GetLogicalChildren());
  80. target.ApplyTemplate();
  81. Assert.Equal(items, target.GetLogicalChildren());
  82. }
  83. [Fact]
  84. public void Removal_Should_Set_Next_Tab()
  85. {
  86. var collection = new ObservableCollection<TabItem>()
  87. {
  88. new TabItem
  89. {
  90. Name = "first",
  91. Content = "foo",
  92. },
  93. new TabItem
  94. {
  95. Name = "second",
  96. Content = "bar",
  97. },
  98. new TabItem
  99. {
  100. Name = "3rd",
  101. Content = "barf",
  102. },
  103. };
  104. var target = new TabControl
  105. {
  106. Template = TabControlTemplate(),
  107. Items = collection,
  108. };
  109. target.ApplyTemplate();
  110. target.SelectedItem = collection[1];
  111. collection.RemoveAt(1);
  112. // compare with former [2] now [1] == "3rd"
  113. Assert.Same(collection[1], target.SelectedItem);
  114. }
  115. [Fact]
  116. public void TabItem_Templates_Should_Be_Set_Before_TabItem_ApplyTemplate()
  117. {
  118. var collection = new[]
  119. {
  120. new TabItem
  121. {
  122. Name = "first",
  123. Content = "foo",
  124. },
  125. new TabItem
  126. {
  127. Name = "second",
  128. Content = "bar",
  129. },
  130. new TabItem
  131. {
  132. Name = "3rd",
  133. Content = "barf",
  134. },
  135. };
  136. var template = new FuncControlTemplate<TabItem>((x, __) => new Decorator());
  137. using (UnitTestApplication.Start(TestServices.RealStyler))
  138. {
  139. var root = new TestRoot
  140. {
  141. Styles =
  142. {
  143. new Style(x => x.OfType<TabItem>())
  144. {
  145. Setters = new[]
  146. {
  147. new Setter(TemplatedControl.TemplateProperty, template)
  148. }
  149. }
  150. },
  151. Child = new TabControl
  152. {
  153. Template = TabControlTemplate(),
  154. Items = collection,
  155. }
  156. };
  157. }
  158. Assert.Same(collection[0].Template, template);
  159. Assert.Same(collection[1].Template, template);
  160. Assert.Same(collection[2].Template, template);
  161. }
  162. [Fact]
  163. public void DataContexts_Should_Be_Correctly_Set()
  164. {
  165. var items = new object[]
  166. {
  167. "Foo",
  168. new Item("Bar"),
  169. new TextBlock { Text = "Baz" },
  170. new TabItem { Content = "Qux" },
  171. new TabItem { Content = new TextBlock { Text = "Bob" } }
  172. };
  173. var target = new TabControl
  174. {
  175. Template = TabControlTemplate(),
  176. DataContext = "Base",
  177. DataTemplates =
  178. {
  179. new FuncDataTemplate<Item>((x, __) => new Button { Content = x })
  180. },
  181. Items = items,
  182. };
  183. ApplyTemplate(target);
  184. ((ContentPresenter)target.ContentPart).UpdateChild();
  185. var dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  186. Assert.Equal(items[0], dataContext);
  187. target.SelectedIndex = 1;
  188. ((ContentPresenter)target.ContentPart).UpdateChild();
  189. dataContext = ((Button)target.ContentPart.Child).DataContext;
  190. Assert.Equal(items[1], dataContext);
  191. target.SelectedIndex = 2;
  192. ((ContentPresenter)target.ContentPart).UpdateChild();
  193. dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  194. Assert.Equal("Base", dataContext);
  195. target.SelectedIndex = 3;
  196. ((ContentPresenter)target.ContentPart).UpdateChild();
  197. dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  198. Assert.Equal("Qux", dataContext);
  199. target.SelectedIndex = 4;
  200. ((ContentPresenter)target.ContentPart).UpdateChild();
  201. dataContext = target.ContentPart.DataContext;
  202. Assert.Equal("Base", dataContext);
  203. }
  204. /// <summary>
  205. /// Non-headered control items should result in TabItems with empty header.
  206. /// </summary>
  207. /// <remarks>
  208. /// If a TabControl is created with non IHeadered controls as its items, don't try to
  209. /// display the control in the header: if the control is part of the header then
  210. /// *that* control would also end up in the content region, resulting in dual-parentage
  211. /// breakage.
  212. /// </remarks>
  213. [Fact]
  214. public void Non_IHeadered_Control_Items_Should_Be_Ignored()
  215. {
  216. var items = new[]
  217. {
  218. new TextBlock { Text = "foo" },
  219. new TextBlock { Text = "bar" },
  220. };
  221. var target = new TabControl
  222. {
  223. Template = TabControlTemplate(),
  224. Items = items,
  225. };
  226. ApplyTemplate(target);
  227. var logicalChildren = target.ItemsPresenterPart.Panel.GetLogicalChildren();
  228. var result = logicalChildren
  229. .OfType<TabItem>()
  230. .Select(x => x.Header)
  231. .ToList();
  232. Assert.Equal(new object[] { null, null }, result);
  233. }
  234. [Fact]
  235. public void Should_Handle_Changing_To_TabItem_With_Null_Content()
  236. {
  237. TabControl target = new TabControl
  238. {
  239. Template = TabControlTemplate(),
  240. Items = new[]
  241. {
  242. new TabItem { Header = "Foo" },
  243. new TabItem { Header = "Foo", Content = new Decorator() },
  244. new TabItem { Header = "Baz" },
  245. },
  246. };
  247. ApplyTemplate(target);
  248. target.SelectedIndex = 2;
  249. var page = (TabItem)target.SelectedItem;
  250. Assert.Null(page.Content);
  251. }
  252. [Fact]
  253. public void DataTemplate_Created_Content_Should_Be_Logical_Child_After_ApplyTemplate()
  254. {
  255. TabControl target = new TabControl
  256. {
  257. Template = TabControlTemplate(),
  258. ContentTemplate = new FuncDataTemplate<string>((x, _) =>
  259. new TextBlock { Tag = "bar", Text = x }),
  260. Items = new[] { "Foo" },
  261. };
  262. ApplyTemplate(target);
  263. ((ContentPresenter)target.ContentPart).UpdateChild();
  264. var content = Assert.IsType<TextBlock>(target.ContentPart.Child);
  265. Assert.Equal("bar", content.Tag);
  266. Assert.Same(target, content.GetLogicalParent());
  267. Assert.Single(target.GetLogicalChildren(), content);
  268. }
  269. [Fact]
  270. public void Should_Not_Propagate_DataContext_To_TabItem_Content()
  271. {
  272. var dataContext = "DataContext";
  273. var tabItem = new TabItem();
  274. var target = new TabControl
  275. {
  276. Template = TabControlTemplate(),
  277. DataContext = dataContext,
  278. Items = new AvaloniaList<object> { tabItem }
  279. };
  280. ApplyTemplate(target);
  281. Assert.NotEqual(dataContext, tabItem.Content);
  282. }
  283. private IControlTemplate TabControlTemplate()
  284. {
  285. return new FuncControlTemplate<TabControl>((parent, scope) =>
  286. new StackPanel
  287. {
  288. Children =
  289. {
  290. new ItemsPresenter
  291. {
  292. Name = "PART_ItemsPresenter",
  293. [!TabStrip.ItemsProperty] = parent[!TabControl.ItemsProperty],
  294. [!TabStrip.ItemTemplateProperty] = parent[!TabControl.ItemTemplateProperty],
  295. }.RegisterInNameScope(scope),
  296. new ContentPresenter
  297. {
  298. Name = "PART_SelectedContentHost",
  299. [!ContentPresenter.ContentProperty] = parent[!TabControl.SelectedContentProperty],
  300. [!ContentPresenter.ContentTemplateProperty] = parent[!TabControl.SelectedContentTemplateProperty],
  301. }.RegisterInNameScope(scope)
  302. }
  303. });
  304. }
  305. private IControlTemplate TabItemTemplate()
  306. {
  307. return new FuncControlTemplate<TabItem>((parent, scope) =>
  308. new ContentPresenter
  309. {
  310. Name = "PART_ContentPresenter",
  311. [!ContentPresenter.ContentProperty] = parent[!TabItem.HeaderProperty],
  312. [!ContentPresenter.ContentTemplateProperty] = parent[!TabItem.HeaderTemplateProperty]
  313. }.RegisterInNameScope(scope));
  314. }
  315. private void ApplyTemplate(TabControl target)
  316. {
  317. target.ApplyTemplate();
  318. target.Presenter.ApplyTemplate();
  319. foreach (var tabItem in target.GetLogicalChildren().OfType<TabItem>())
  320. {
  321. tabItem.Template = TabItemTemplate();
  322. tabItem.ApplyTemplate();
  323. ((ContentPresenter)tabItem.Presenter).UpdateChild();
  324. }
  325. target.ContentPart.ApplyTemplate();
  326. }
  327. private class Item
  328. {
  329. public Item(string value)
  330. {
  331. Value = value;
  332. }
  333. public string Value { get; }
  334. }
  335. }
  336. }