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.Markup.Xaml;
  12. using Avalonia.Styling;
  13. using Avalonia.UnitTests;
  14. using Xunit;
  15. namespace Avalonia.Controls.UnitTests
  16. {
  17. public class TabControlTests
  18. {
  19. [Fact]
  20. public void First_Tab_Should_Be_Selected_By_Default()
  21. {
  22. TabItem selected;
  23. var target = new TabControl
  24. {
  25. Template = TabControlTemplate(),
  26. Items = new[]
  27. {
  28. (selected = new TabItem
  29. {
  30. Name = "first",
  31. Content = "foo",
  32. }),
  33. new TabItem
  34. {
  35. Name = "second",
  36. Content = "bar",
  37. },
  38. }
  39. };
  40. target.ApplyTemplate();
  41. Assert.Equal(0, target.SelectedIndex);
  42. Assert.Equal(selected, target.SelectedItem);
  43. }
  44. [Fact]
  45. public void Pre_Selecting_TabItem_Should_Set_SelectedContent_After_It_Was_Added()
  46. {
  47. var target = new TabControl
  48. {
  49. Template = TabControlTemplate(),
  50. };
  51. const string secondContent = "Second";
  52. var items = new AvaloniaList<object>
  53. {
  54. new TabItem { Header = "First"},
  55. new TabItem { Header = "Second", Content = secondContent, IsSelected = true }
  56. };
  57. target.Items = items;
  58. ApplyTemplate(target);
  59. Assert.Equal(secondContent, target.SelectedContent);
  60. }
  61. [Fact]
  62. public void Logical_Children_Should_Be_TabItems()
  63. {
  64. var items = new[]
  65. {
  66. new TabItem
  67. {
  68. Content = "foo"
  69. },
  70. new TabItem
  71. {
  72. Content = "bar"
  73. },
  74. };
  75. var target = new TabControl
  76. {
  77. Template = TabControlTemplate(),
  78. Items = items,
  79. };
  80. Assert.Equal(items, target.GetLogicalChildren());
  81. target.ApplyTemplate();
  82. Assert.Equal(items, target.GetLogicalChildren());
  83. }
  84. [Fact]
  85. public void Removal_Should_Set_First_Tab()
  86. {
  87. var collection = new ObservableCollection<TabItem>()
  88. {
  89. new TabItem
  90. {
  91. Name = "first",
  92. Content = "foo",
  93. },
  94. new TabItem
  95. {
  96. Name = "second",
  97. Content = "bar",
  98. },
  99. new TabItem
  100. {
  101. Name = "3rd",
  102. Content = "barf",
  103. },
  104. };
  105. var target = new TabControl
  106. {
  107. Template = TabControlTemplate(),
  108. Items = collection,
  109. };
  110. target.ApplyTemplate();
  111. target.SelectedItem = collection[1];
  112. collection.RemoveAt(1);
  113. Assert.Same(collection[0], 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 =
  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. [Fact]
  284. public void Can_Have_Empty_Tab_Control()
  285. {
  286. using (UnitTestApplication.Start(TestServices.StyledWindow))
  287. {
  288. var xaml = @"
  289. <Window xmlns='https://github.com/avaloniaui'
  290. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  291. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
  292. <TabControl Name='tabs' Items='{Binding Tabs}'/>
  293. </Window>";
  294. var window = (Window)AvaloniaRuntimeXamlLoader.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. }