TabControlTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 = new FuncControlTemplate<TabControl>(CreateTabControlTemplate),
  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 = new FuncControlTemplate<TabControl>(CreateTabControlTemplate),
  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 = new FuncControlTemplate<TabControl>(CreateTabControlTemplate),
  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 = new FuncControlTemplate<TabControl>(CreateTabControlTemplate),
  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 = new FuncControlTemplate<TabControl>(CreateTabControlTemplate),
  158. DataContext = "Base",
  159. DataTemplates =
  160. {
  161. new FuncDataTemplate<Item>(x => new Button { Content = x })
  162. },
  163. Items = items,
  164. };
  165. ApplyTemplate(target);
  166. var carousel = (Carousel)target.Pages;
  167. var container = (ContentPresenter)carousel.Presenter.Panel.Children.Single();
  168. container.UpdateChild();
  169. var dataContext = ((TextBlock)container.Child).DataContext;
  170. Assert.Equal(items[0], dataContext);
  171. target.SelectedIndex = 1;
  172. container = (ContentPresenter)carousel.Presenter.Panel.Children.Single();
  173. container.UpdateChild();
  174. dataContext = ((Button)container.Child).DataContext;
  175. Assert.Equal(items[1], dataContext);
  176. target.SelectedIndex = 2;
  177. dataContext = ((TextBlock)carousel.Presenter.Panel.Children.Single()).DataContext;
  178. Assert.Equal("Base", dataContext);
  179. target.SelectedIndex = 3;
  180. container = (ContentPresenter)carousel.Presenter.Panel.Children[0];
  181. container.UpdateChild();
  182. dataContext = ((TextBlock)container.Child).DataContext;
  183. Assert.Equal("Qux", dataContext);
  184. target.SelectedIndex = 4;
  185. dataContext = ((TextBlock)carousel.Presenter.Panel.Children.Single()).DataContext;
  186. Assert.Equal("Base", dataContext);
  187. }
  188. /// <summary>
  189. /// Non-headered control items should result in TabStripItems with empty content.
  190. /// </summary>
  191. /// <remarks>
  192. /// If a TabStrip is created with non IHeadered controls as its items, don't try to
  193. /// display the control in the TabStripItem: if the TabStrip is part of a TabControl
  194. /// then *that* will also try to display the control, resulting in dual-parentage
  195. /// breakage.
  196. /// </remarks>
  197. [Fact]
  198. public void Non_IHeadered_Control_Items_Should_Be_Ignored()
  199. {
  200. var items = new[]
  201. {
  202. new TextBlock { Text = "foo" },
  203. new TextBlock { Text = "bar" },
  204. };
  205. var target = new TabControl
  206. {
  207. Template = new FuncControlTemplate<TabControl>(CreateTabControlTemplate),
  208. Items = items,
  209. };
  210. ApplyTemplate(target);
  211. var result = target.TabStrip.GetLogicalChildren()
  212. .OfType<TabStripItem>()
  213. .Select(x => x.Content)
  214. .ToList();
  215. Assert.Equal(new object[] { string.Empty, string.Empty }, result);
  216. }
  217. [Fact]
  218. public void Should_Handle_Changing_To_TabItem_With_Null_Content()
  219. {
  220. TabControl target = new TabControl
  221. {
  222. Template = new FuncControlTemplate<TabControl>(CreateTabControlTemplate),
  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 carousel = (Carousel)target.Pages;
  233. var page = (TabItem)carousel.SelectedItem;
  234. Assert.Null(page.Content);
  235. }
  236. private Control CreateTabControlTemplate(TabControl parent)
  237. {
  238. return new StackPanel
  239. {
  240. Children =
  241. {
  242. new TabStrip
  243. {
  244. Name = "PART_TabStrip",
  245. Template = new FuncControlTemplate<TabStrip>(CreateTabStripTemplate),
  246. MemberSelector = TabControl.HeaderSelector,
  247. [!TabStrip.ItemsProperty] = parent[!TabControl.ItemsProperty],
  248. [!!TabStrip.SelectedIndexProperty] = parent[!!TabControl.SelectedIndexProperty]
  249. },
  250. new Carousel
  251. {
  252. Name = "PART_Content",
  253. Template = new FuncControlTemplate<Carousel>(CreateCarouselTemplate),
  254. MemberSelector = TabControl.ContentSelector,
  255. [!Carousel.ItemsProperty] = parent[!TabControl.ItemsProperty],
  256. [!Carousel.SelectedItemProperty] = parent[!TabControl.SelectedItemProperty],
  257. }
  258. }
  259. };
  260. }
  261. private Control CreateTabStripTemplate(TabStrip parent)
  262. {
  263. return new ItemsPresenter
  264. {
  265. Name = "PART_ItemsPresenter",
  266. [~ItemsPresenter.ItemsProperty] = parent[~ItemsControl.ItemsProperty],
  267. [!CarouselPresenter.MemberSelectorProperty] = parent[!ItemsControl.MemberSelectorProperty],
  268. };
  269. }
  270. private Control CreateCarouselTemplate(Carousel control)
  271. {
  272. return new CarouselPresenter
  273. {
  274. Name = "PART_ItemsPresenter",
  275. [!CarouselPresenter.ItemsProperty] = control[!ItemsControl.ItemsProperty],
  276. [!CarouselPresenter.ItemsPanelProperty] = control[!ItemsControl.ItemsPanelProperty],
  277. [!CarouselPresenter.MemberSelectorProperty] = control[!ItemsControl.MemberSelectorProperty],
  278. [!CarouselPresenter.SelectedIndexProperty] = control[!SelectingItemsControl.SelectedIndexProperty],
  279. [~CarouselPresenter.TransitionProperty] = control[~Carousel.TransitionProperty],
  280. };
  281. }
  282. private void ApplyTemplate(TabControl target)
  283. {
  284. target.ApplyTemplate();
  285. var carousel = (Carousel)target.Pages;
  286. carousel.ApplyTemplate();
  287. carousel.Presenter.ApplyTemplate();
  288. var tabStrip = (TabStrip)target.TabStrip;
  289. tabStrip.ApplyTemplate();
  290. tabStrip.Presenter.ApplyTemplate();
  291. }
  292. private class Item
  293. {
  294. public Item(string value)
  295. {
  296. Value = value;
  297. }
  298. public string Value { get; }
  299. }
  300. }
  301. }