TabControlTests.cs 11 KB

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