TabControlTests.cs 11 KB

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