TabControlTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using Avalonia.Collections;
  6. using Avalonia.Controls.Presenters;
  7. using Avalonia.Controls.Primitives;
  8. using Avalonia.Controls.Templates;
  9. using Avalonia.Controls.Utils;
  10. using Avalonia.LogicalTree;
  11. using Avalonia.Markup.Xaml;
  12. using Avalonia.Styling;
  13. using Avalonia.UnitTests;
  14. using Xunit;
  15. namespace Avalonia.Controls.UnitTests
  16. {
  17. public class TabControlTests
  18. {
  19. [Fact]
  20. public void First_Tab_Should_Be_Selected_By_Default()
  21. {
  22. TabItem selected;
  23. var target = new TabControl
  24. {
  25. Template = TabControlTemplate(),
  26. Items =
  27. {
  28. (selected = new TabItem
  29. {
  30. Name = "first",
  31. Content = "foo",
  32. }),
  33. new TabItem
  34. {
  35. Name = "second",
  36. Content = "bar",
  37. },
  38. }
  39. };
  40. target.ApplyTemplate();
  41. Assert.Equal(0, target.SelectedIndex);
  42. Assert.Equal(selected, target.SelectedItem);
  43. }
  44. [Fact]
  45. public void Pre_Selecting_TabItem_Should_Set_SelectedContent_After_It_Was_Added()
  46. {
  47. const string secondContent = "Second";
  48. var target = new TabControl
  49. {
  50. Template = TabControlTemplate(),
  51. Items =
  52. {
  53. new TabItem { Header = "First"},
  54. new TabItem { Header = "Second", Content = secondContent, IsSelected = true }
  55. },
  56. };
  57. ApplyTemplate(target);
  58. Assert.Equal(secondContent, target.SelectedContent);
  59. }
  60. [Fact]
  61. public void Logical_Children_Should_Be_TabItems()
  62. {
  63. var target = new TabControl
  64. {
  65. Template = TabControlTemplate(),
  66. Items =
  67. {
  68. new TabItem
  69. {
  70. Content = "foo"
  71. },
  72. new TabItem
  73. {
  74. Content = "bar"
  75. },
  76. }
  77. };
  78. Assert.Equal(target.Items, target.GetLogicalChildren().ToList());
  79. target.ApplyTemplate();
  80. Assert.Equal(target.Items, target.GetLogicalChildren().ToList());
  81. }
  82. [Fact]
  83. public void Removal_Should_Set_First_Tab()
  84. {
  85. var target = new TabControl
  86. {
  87. Template = TabControlTemplate(),
  88. Items =
  89. {
  90. new TabItem
  91. {
  92. Name = "first",
  93. Content = "foo",
  94. },
  95. new TabItem
  96. {
  97. Name = "second",
  98. Content = "bar",
  99. },
  100. new TabItem
  101. {
  102. Name = "3rd",
  103. Content = "barf",
  104. },
  105. }
  106. };
  107. Prepare(target);
  108. target.SelectedItem = target.Items[1];
  109. var item = Assert.IsType<TabItem>(target.Items[1]);
  110. Assert.Same(item, target.SelectedItem);
  111. Assert.Equal(item.Content, target.SelectedContent);
  112. target.Items.RemoveAt(1);
  113. item = Assert.IsType<TabItem>(target.Items[0]);
  114. Assert.Same(item, target.SelectedItem);
  115. Assert.Equal(item.Content, target.SelectedContent);
  116. }
  117. [Fact]
  118. public void Removal_Should_Set_New_Item0_When_Item0_Selected()
  119. {
  120. var target = new TabControl
  121. {
  122. Template = TabControlTemplate(),
  123. Items =
  124. {
  125. new TabItem
  126. {
  127. Name = "first",
  128. Content = "foo",
  129. },
  130. new TabItem
  131. {
  132. Name = "second",
  133. Content = "bar",
  134. },
  135. new TabItem
  136. {
  137. Name = "3rd",
  138. Content = "barf",
  139. },
  140. }
  141. };
  142. Prepare(target);
  143. target.SelectedItem = target.Items[0];
  144. var item = Assert.IsType<TabItem>(target.Items[0]);
  145. Assert.Same(item, target.SelectedItem);
  146. Assert.Equal(item.Content, target.SelectedContent);
  147. target.Items.RemoveAt(0);
  148. item = Assert.IsType<TabItem>(target.Items[0]);
  149. Assert.Same(item, target.SelectedItem);
  150. Assert.Equal(item.Content, target.SelectedContent);
  151. }
  152. [Fact]
  153. public void Removal_Should_Set_New_Item0_When_Item0_Selected_With_DataTemplate()
  154. {
  155. using var app = UnitTestApplication.Start(TestServices.StyledWindow);
  156. var collection = new ObservableCollection<Item>()
  157. {
  158. new Item("first"),
  159. new Item("second"),
  160. new Item("3rd"),
  161. };
  162. var target = new TabControl
  163. {
  164. Template = TabControlTemplate(),
  165. ItemsSource = collection,
  166. };
  167. Prepare(target);
  168. target.SelectedItem = collection[0];
  169. Assert.Same(collection[0], target.SelectedItem);
  170. Assert.Equal(collection[0], target.SelectedContent);
  171. collection.RemoveAt(0);
  172. Assert.Same(collection[0], target.SelectedItem);
  173. Assert.Equal(collection[0], target.SelectedContent);
  174. }
  175. [Fact]
  176. public void TabItem_Templates_Should_Be_Set_Before_TabItem_ApplyTemplate()
  177. {
  178. var template = new FuncControlTemplate<TabItem>((x, __) => new Decorator());
  179. TabControl target;
  180. var root = new TestRoot
  181. {
  182. Styles =
  183. {
  184. new Style(x => x.OfType<TabItem>())
  185. {
  186. Setters =
  187. {
  188. new Setter(TemplatedControl.TemplateProperty, template)
  189. }
  190. }
  191. },
  192. Child = (target = new TabControl
  193. {
  194. Template = TabControlTemplate(),
  195. Items =
  196. {
  197. new TabItem
  198. {
  199. Name = "first",
  200. Content = "foo",
  201. },
  202. new TabItem
  203. {
  204. Name = "second",
  205. Content = "bar",
  206. },
  207. new TabItem
  208. {
  209. Name = "3rd",
  210. Content = "barf",
  211. },
  212. },
  213. })
  214. };
  215. var collection = target.Items.Cast<TabItem>().ToList();
  216. Assert.Same(collection[0].Template, template);
  217. Assert.Same(collection[1].Template, template);
  218. Assert.Same(collection[2].Template, template);
  219. }
  220. [Fact]
  221. public void DataContexts_Should_Be_Correctly_Set()
  222. {
  223. var items = new object[]
  224. {
  225. "Foo",
  226. new Item("Bar"),
  227. new TextBlock { Text = "Baz" },
  228. new TabItem { Content = "Qux" },
  229. new TabItem { Content = new TextBlock { Text = "Bob" } }
  230. };
  231. var target = new TabControl
  232. {
  233. Template = TabControlTemplate(),
  234. DataContext = "Base",
  235. DataTemplates =
  236. {
  237. new FuncDataTemplate<Item>((x, __) => new Button { Content = x })
  238. },
  239. ItemsSource = items,
  240. };
  241. ApplyTemplate(target);
  242. ((ContentPresenter)target.ContentPart).UpdateChild();
  243. var dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  244. Assert.Equal(items[0], dataContext);
  245. target.SelectedIndex = 1;
  246. ((ContentPresenter)target.ContentPart).UpdateChild();
  247. dataContext = ((Button)target.ContentPart.Child).DataContext;
  248. Assert.Equal(items[1], dataContext);
  249. target.SelectedIndex = 2;
  250. ((ContentPresenter)target.ContentPart).UpdateChild();
  251. dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  252. Assert.Equal("Base", dataContext);
  253. target.SelectedIndex = 3;
  254. ((ContentPresenter)target.ContentPart).UpdateChild();
  255. dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  256. Assert.Equal("Qux", dataContext);
  257. target.SelectedIndex = 4;
  258. ((ContentPresenter)target.ContentPart).UpdateChild();
  259. dataContext = ((Control)target.ContentPart).DataContext;
  260. Assert.Equal("Base", dataContext);
  261. }
  262. /// <summary>
  263. /// Non-headered control items should result in TabItems with empty header.
  264. /// </summary>
  265. /// <remarks>
  266. /// If a TabControl is created with non IHeadered controls as its items, don't try to
  267. /// display the control in the header: if the control is part of the header then
  268. /// *that* control would also end up in the content region, resulting in dual-parentage
  269. /// breakage.
  270. /// </remarks>
  271. [Fact]
  272. public void Non_IHeadered_Control_Items_Should_Be_Ignored()
  273. {
  274. var target = new TabControl
  275. {
  276. Template = TabControlTemplate(),
  277. Items =
  278. {
  279. new TextBlock { Text = "foo" },
  280. new TextBlock { Text = "bar" },
  281. },
  282. };
  283. ApplyTemplate(target);
  284. var logicalChildren = target.GetLogicalChildren();
  285. var result = logicalChildren
  286. .OfType<TabItem>()
  287. .Select(x => x.Header)
  288. .ToList();
  289. Assert.Equal(new object[] { null, null }, result);
  290. }
  291. [Fact]
  292. public void Should_Handle_Changing_To_TabItem_With_Null_Content()
  293. {
  294. TabControl target = new TabControl
  295. {
  296. Template = TabControlTemplate(),
  297. Items =
  298. {
  299. new TabItem { Header = "Foo" },
  300. new TabItem { Header = "Foo", Content = new Decorator() },
  301. new TabItem { Header = "Baz" },
  302. },
  303. };
  304. ApplyTemplate(target);
  305. target.SelectedIndex = 2;
  306. var page = (TabItem)target.SelectedItem;
  307. Assert.Null(page.Content);
  308. }
  309. [Fact]
  310. public void DataTemplate_Created_Content_Should_Be_Logical_Child_After_ApplyTemplate()
  311. {
  312. TabControl target = new TabControl
  313. {
  314. Template = TabControlTemplate(),
  315. ContentTemplate = new FuncDataTemplate<string>((x, _) =>
  316. new TextBlock { Tag = "bar", Text = x }),
  317. ItemsSource = new[] { "Foo" },
  318. };
  319. var root = new TestRoot(target);
  320. ApplyTemplate(target);
  321. ((ContentPresenter)target.ContentPart).UpdateChild();
  322. var content = Assert.IsType<TextBlock>(target.ContentPart.Child);
  323. Assert.Equal("bar", content.Tag);
  324. Assert.Same(target, content.GetLogicalParent());
  325. Assert.Single(target.GetLogicalChildren(), content);
  326. }
  327. [Fact]
  328. public void Should_Not_Propagate_DataContext_To_TabItem_Content()
  329. {
  330. var dataContext = "DataContext";
  331. var tabItem = new TabItem();
  332. var target = new TabControl
  333. {
  334. Template = TabControlTemplate(),
  335. DataContext = dataContext,
  336. Items = { tabItem }
  337. };
  338. ApplyTemplate(target);
  339. Assert.NotEqual(dataContext, tabItem.Content);
  340. }
  341. [Fact]
  342. public void Can_Have_Empty_Tab_Control()
  343. {
  344. using (UnitTestApplication.Start(TestServices.StyledWindow))
  345. {
  346. var xaml = @"
  347. <Window xmlns='https://github.com/avaloniaui'
  348. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  349. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
  350. <TabControl Name='tabs' ItemsSource='{Binding Tabs}'/>
  351. </Window>";
  352. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  353. var tabControl = window.FindControl<TabControl>("tabs");
  354. tabControl.DataContext = new { Tabs = new List<string>() };
  355. window.ApplyTemplate();
  356. Assert.Equal(0, tabControl.ItemsSource.Count());
  357. }
  358. }
  359. private static IControlTemplate TabControlTemplate()
  360. {
  361. return new FuncControlTemplate<TabControl>((parent, scope) =>
  362. new StackPanel
  363. {
  364. Children =
  365. {
  366. new ItemsPresenter
  367. {
  368. Name = "PART_ItemsPresenter",
  369. }.RegisterInNameScope(scope),
  370. new ContentPresenter
  371. {
  372. Name = "PART_SelectedContentHost",
  373. [!ContentPresenter.ContentProperty] = parent[!TabControl.SelectedContentProperty],
  374. [!ContentPresenter.ContentTemplateProperty] = parent[!TabControl.SelectedContentTemplateProperty],
  375. }.RegisterInNameScope(scope)
  376. }
  377. });
  378. }
  379. private static IControlTemplate TabItemTemplate()
  380. {
  381. return new FuncControlTemplate<TabItem>((parent, scope) =>
  382. new ContentPresenter
  383. {
  384. Name = "PART_ContentPresenter",
  385. [!ContentPresenter.ContentProperty] = parent[!TabItem.HeaderProperty],
  386. [!ContentPresenter.ContentTemplateProperty] = parent[!TabItem.HeaderTemplateProperty]
  387. }.RegisterInNameScope(scope));
  388. }
  389. private static void Prepare(TabControl target)
  390. {
  391. ApplyTemplate(target);
  392. target.Measure(Size.Infinity);
  393. target.Arrange(new Rect(target.DesiredSize));
  394. }
  395. private static void ApplyTemplate(TabControl target)
  396. {
  397. target.ApplyTemplate();
  398. target.Presenter.ApplyTemplate();
  399. foreach (var tabItem in target.GetLogicalChildren().OfType<TabItem>())
  400. {
  401. tabItem.Template = TabItemTemplate();
  402. tabItem.ApplyTemplate();
  403. ((ContentPresenter)tabItem.Presenter).UpdateChild();
  404. }
  405. target.ContentPart.ApplyTemplate();
  406. }
  407. private class Item
  408. {
  409. public Item(string value)
  410. {
  411. Value = value;
  412. }
  413. public string Value { get; }
  414. }
  415. }
  416. }