PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Avalonia.Controls.UnitTests/TabControlTests.cs

https://gitlab.com/kush/Avalonia
C# | 332 lines | 274 code | 46 blank | 12 comment | 0 complexity | f58ce9d91951df103da9501a3e6cf3b6 MD5 | raw file
  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. target.ContentPart.UpdateChild();
  167. var dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  168. Assert.Equal(items[0], dataContext);
  169. target.SelectedIndex = 1;
  170. target.ContentPart.UpdateChild();
  171. dataContext = ((Button)target.ContentPart.Child).DataContext;
  172. Assert.Equal(items[1], dataContext);
  173. target.SelectedIndex = 2;
  174. target.ContentPart.UpdateChild();
  175. dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  176. Assert.Equal("Base", dataContext);
  177. target.SelectedIndex = 3;
  178. target.ContentPart.UpdateChild();
  179. dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  180. Assert.Equal("Qux", dataContext);
  181. target.SelectedIndex = 4;
  182. 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. private IControlTemplate TabControlTemplate()
  235. {
  236. return new FuncControlTemplate<TabControl>(parent =>
  237. new StackPanel
  238. {
  239. Children = {
  240. new ItemsPresenter
  241. {
  242. Name = "PART_ItemsPresenter",
  243. [!TabStrip.ItemsProperty] = parent[!TabControl.ItemsProperty],
  244. [!TabStrip.ItemTemplateProperty] = parent[!TabControl.ItemTemplateProperty],
  245. },
  246. new ContentPresenter
  247. {
  248. Name = "PART_Content",
  249. [!ContentPresenter.ContentProperty] = parent[!TabControl.SelectedContentProperty],
  250. [!ContentPresenter.ContentTemplateProperty] = parent[!TabControl.SelectedContentTemplateProperty],
  251. }
  252. }
  253. });
  254. }
  255. private IControlTemplate TabItemTemplate()
  256. {
  257. return new FuncControlTemplate<TabItem>(parent =>
  258. new ContentPresenter
  259. {
  260. Name = "PART_ContentPresenter",
  261. [!ContentPresenter.ContentProperty] = parent[!TabItem.HeaderProperty],
  262. [!ContentPresenter.ContentTemplateProperty] = parent[!TabItem.HeaderTemplateProperty]
  263. });
  264. }
  265. private void ApplyTemplate(TabControl target)
  266. {
  267. target.ApplyTemplate();
  268. target.Presenter.ApplyTemplate();
  269. foreach (var tabItem in target.GetLogicalChildren().OfType<TabItem>())
  270. {
  271. tabItem.Template = TabItemTemplate();
  272. tabItem.ApplyTemplate();
  273. ((ContentPresenter)tabItem.Presenter).UpdateChild();
  274. }
  275. target.ContentPart.ApplyTemplate();
  276. }
  277. private class Item
  278. {
  279. public Item(string value)
  280. {
  281. Value = value;
  282. }
  283. public string Value { get; }
  284. }
  285. }
  286. }