TabControlTests.cs 12 KB

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