TabControlTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using Avalonia.Collections;
  6. using Avalonia.Controls.Presenters;
  7. using Avalonia.Controls.Primitives;
  8. using Avalonia.Controls.Templates;
  9. using Avalonia.Controls.Utils;
  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_First_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. Assert.Same(collection[0], target.SelectedItem);
  113. }
  114. [Fact]
  115. public void TabItem_Templates_Should_Be_Set_Before_TabItem_ApplyTemplate()
  116. {
  117. var collection = new[]
  118. {
  119. new TabItem
  120. {
  121. Name = "first",
  122. Content = "foo",
  123. },
  124. new TabItem
  125. {
  126. Name = "second",
  127. Content = "bar",
  128. },
  129. new TabItem
  130. {
  131. Name = "3rd",
  132. Content = "barf",
  133. },
  134. };
  135. var template = new FuncControlTemplate<TabItem>((x, __) => new Decorator());
  136. using (UnitTestApplication.Start(TestServices.RealStyler))
  137. {
  138. var root = new TestRoot
  139. {
  140. Styles =
  141. {
  142. new Style(x => x.OfType<TabItem>())
  143. {
  144. Setters =
  145. {
  146. new Setter(TemplatedControl.TemplateProperty, template)
  147. }
  148. }
  149. },
  150. Child = new TabControl
  151. {
  152. Template = TabControlTemplate(),
  153. Items = collection,
  154. }
  155. };
  156. }
  157. Assert.Same(collection[0].Template, template);
  158. Assert.Same(collection[1].Template, template);
  159. Assert.Same(collection[2].Template, template);
  160. }
  161. [Fact]
  162. public void DataContexts_Should_Be_Correctly_Set()
  163. {
  164. var items = new object[]
  165. {
  166. "Foo",
  167. new Item("Bar"),
  168. new TextBlock { Text = "Baz" },
  169. new TabItem { Content = "Qux" },
  170. new TabItem { Content = new TextBlock { Text = "Bob" } }
  171. };
  172. var target = new TabControl
  173. {
  174. Template = TabControlTemplate(),
  175. DataContext = "Base",
  176. DataTemplates =
  177. {
  178. new FuncDataTemplate<Item>((x, __) => new Button { Content = x })
  179. },
  180. Items = items,
  181. };
  182. ApplyTemplate(target);
  183. ((ContentPresenter)target.ContentPart).UpdateChild();
  184. var dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  185. Assert.Equal(items[0], dataContext);
  186. target.SelectedIndex = 1;
  187. ((ContentPresenter)target.ContentPart).UpdateChild();
  188. dataContext = ((Button)target.ContentPart.Child).DataContext;
  189. Assert.Equal(items[1], dataContext);
  190. target.SelectedIndex = 2;
  191. ((ContentPresenter)target.ContentPart).UpdateChild();
  192. dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  193. Assert.Equal("Base", dataContext);
  194. target.SelectedIndex = 3;
  195. ((ContentPresenter)target.ContentPart).UpdateChild();
  196. dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  197. Assert.Equal("Qux", dataContext);
  198. target.SelectedIndex = 4;
  199. ((ContentPresenter)target.ContentPart).UpdateChild();
  200. dataContext = target.ContentPart.DataContext;
  201. Assert.Equal("Base", dataContext);
  202. }
  203. /// <summary>
  204. /// Non-headered control items should result in TabItems with empty header.
  205. /// </summary>
  206. /// <remarks>
  207. /// If a TabControl is created with non IHeadered controls as its items, don't try to
  208. /// display the control in the header: if the control is part of the header then
  209. /// *that* control would also end up in the content region, resulting in dual-parentage
  210. /// breakage.
  211. /// </remarks>
  212. [Fact]
  213. public void Non_IHeadered_Control_Items_Should_Be_Ignored()
  214. {
  215. var items = new[]
  216. {
  217. new TextBlock { Text = "foo" },
  218. new TextBlock { Text = "bar" },
  219. };
  220. var target = new TabControl
  221. {
  222. Template = TabControlTemplate(),
  223. Items = items,
  224. };
  225. ApplyTemplate(target);
  226. var logicalChildren = target.ItemsPresenterPart.Panel.GetLogicalChildren();
  227. var result = logicalChildren
  228. .OfType<TabItem>()
  229. .Select(x => x.Header)
  230. .ToList();
  231. Assert.Equal(new object[] { null, null }, result);
  232. }
  233. [Fact]
  234. public void Should_Handle_Changing_To_TabItem_With_Null_Content()
  235. {
  236. TabControl target = new TabControl
  237. {
  238. Template = TabControlTemplate(),
  239. Items = new[]
  240. {
  241. new TabItem { Header = "Foo" },
  242. new TabItem { Header = "Foo", Content = new Decorator() },
  243. new TabItem { Header = "Baz" },
  244. },
  245. };
  246. ApplyTemplate(target);
  247. target.SelectedIndex = 2;
  248. var page = (TabItem)target.SelectedItem;
  249. Assert.Null(page.Content);
  250. }
  251. [Fact]
  252. public void DataTemplate_Created_Content_Should_Be_Logical_Child_After_ApplyTemplate()
  253. {
  254. TabControl target = new TabControl
  255. {
  256. Template = TabControlTemplate(),
  257. ContentTemplate = new FuncDataTemplate<string>((x, _) =>
  258. new TextBlock { Tag = "bar", Text = x }),
  259. Items = new[] { "Foo" },
  260. };
  261. ApplyTemplate(target);
  262. ((ContentPresenter)target.ContentPart).UpdateChild();
  263. var content = Assert.IsType<TextBlock>(target.ContentPart.Child);
  264. Assert.Equal("bar", content.Tag);
  265. Assert.Same(target, content.GetLogicalParent());
  266. Assert.Single(target.GetLogicalChildren(), content);
  267. }
  268. [Fact]
  269. public void Should_Not_Propagate_DataContext_To_TabItem_Content()
  270. {
  271. var dataContext = "DataContext";
  272. var tabItem = new TabItem();
  273. var target = new TabControl
  274. {
  275. Template = TabControlTemplate(),
  276. DataContext = dataContext,
  277. Items = new AvaloniaList<object> { tabItem }
  278. };
  279. ApplyTemplate(target);
  280. Assert.NotEqual(dataContext, tabItem.Content);
  281. }
  282. [Fact]
  283. public void Can_Have_Empty_Tab_Control()
  284. {
  285. using (UnitTestApplication.Start(TestServices.StyledWindow))
  286. {
  287. var xaml = @"
  288. <Window xmlns='https://github.com/avaloniaui'
  289. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  290. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
  291. <TabControl Name='tabs' Items='{Binding Tabs}'/>
  292. </Window>";
  293. var loader = new Markup.Xaml.AvaloniaXamlLoader();
  294. var window = (Window)loader.Load(xaml);
  295. var tabControl = window.FindControl<TabControl>("tabs");
  296. tabControl.DataContext = new { Tabs = new List<string>() };
  297. window.ApplyTemplate();
  298. Assert.Equal(0, tabControl.Items.Count());
  299. }
  300. }
  301. private IControlTemplate TabControlTemplate()
  302. {
  303. return new FuncControlTemplate<TabControl>((parent, scope) =>
  304. new StackPanel
  305. {
  306. Children =
  307. {
  308. new ItemsPresenter
  309. {
  310. Name = "PART_ItemsPresenter",
  311. [!TabStrip.ItemsProperty] = parent[!TabControl.ItemsProperty],
  312. [!TabStrip.ItemTemplateProperty] = parent[!TabControl.ItemTemplateProperty],
  313. }.RegisterInNameScope(scope),
  314. new ContentPresenter
  315. {
  316. Name = "PART_SelectedContentHost",
  317. [!ContentPresenter.ContentProperty] = parent[!TabControl.SelectedContentProperty],
  318. [!ContentPresenter.ContentTemplateProperty] = parent[!TabControl.SelectedContentTemplateProperty],
  319. }.RegisterInNameScope(scope)
  320. }
  321. });
  322. }
  323. private IControlTemplate TabItemTemplate()
  324. {
  325. return new FuncControlTemplate<TabItem>((parent, scope) =>
  326. new ContentPresenter
  327. {
  328. Name = "PART_ContentPresenter",
  329. [!ContentPresenter.ContentProperty] = parent[!TabItem.HeaderProperty],
  330. [!ContentPresenter.ContentTemplateProperty] = parent[!TabItem.HeaderTemplateProperty]
  331. }.RegisterInNameScope(scope));
  332. }
  333. private void ApplyTemplate(TabControl target)
  334. {
  335. target.ApplyTemplate();
  336. target.Presenter.ApplyTemplate();
  337. foreach (var tabItem in target.GetLogicalChildren().OfType<TabItem>())
  338. {
  339. tabItem.Template = TabItemTemplate();
  340. tabItem.ApplyTemplate();
  341. ((ContentPresenter)tabItem.Presenter).UpdateChild();
  342. }
  343. target.ContentPart.ApplyTemplate();
  344. }
  345. private class Item
  346. {
  347. public Item(string value)
  348. {
  349. Value = value;
  350. }
  351. public string Value { get; }
  352. }
  353. }
  354. }