TabControlTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 = new[]
  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. var target = new TabControl
  48. {
  49. Template = TabControlTemplate(),
  50. };
  51. const string secondContent = "Second";
  52. var items = new AvaloniaList<object>
  53. {
  54. new TabItem { Header = "First"},
  55. new TabItem { Header = "Second", Content = secondContent, IsSelected = true }
  56. };
  57. target.Items = items;
  58. ApplyTemplate(target);
  59. Assert.Equal(secondContent, target.SelectedContent);
  60. }
  61. [Fact]
  62. public void Logical_Children_Should_Be_TabItems()
  63. {
  64. var target = new TabControl
  65. {
  66. Template = TabControlTemplate(),
  67. Items =
  68. {
  69. new TabItem
  70. {
  71. Content = "foo"
  72. },
  73. new TabItem
  74. {
  75. Content = "bar"
  76. },
  77. }
  78. };
  79. Assert.Equal(target.Items.Cast<ILogical>(), target.GetLogicalChildren());
  80. target.ApplyTemplate();
  81. Assert.Equal(target.Items.Cast<ILogical>(), target.GetLogicalChildren());
  82. }
  83. [Fact]
  84. public void Removal_Should_Set_First_Tab()
  85. {
  86. var collection = new ObservableCollection<TabItem>()
  87. {
  88. new TabItem
  89. {
  90. Name = "first",
  91. Content = "foo",
  92. },
  93. new TabItem
  94. {
  95. Name = "second",
  96. Content = "bar",
  97. },
  98. new TabItem
  99. {
  100. Name = "3rd",
  101. Content = "barf",
  102. },
  103. };
  104. var target = new TabControl
  105. {
  106. Template = TabControlTemplate(),
  107. Items = collection,
  108. };
  109. Prepare(target);
  110. target.SelectedItem = collection[1];
  111. Assert.Same(collection[1], target.SelectedItem);
  112. Assert.Equal(collection[1].Content, target.SelectedContent);
  113. collection.RemoveAt(1);
  114. Assert.Same(collection[0], target.SelectedItem);
  115. Assert.Equal(collection[0].Content, target.SelectedContent);
  116. }
  117. [Fact]
  118. public void Removal_Should_Set_New_Item0_When_Item0_Selected()
  119. {
  120. var collection = new ObservableCollection<TabItem>()
  121. {
  122. new TabItem
  123. {
  124. Name = "first",
  125. Content = "foo",
  126. },
  127. new TabItem
  128. {
  129. Name = "second",
  130. Content = "bar",
  131. },
  132. new TabItem
  133. {
  134. Name = "3rd",
  135. Content = "barf",
  136. },
  137. };
  138. var target = new TabControl
  139. {
  140. Template = TabControlTemplate(),
  141. Items = collection,
  142. };
  143. Prepare(target);
  144. target.SelectedItem = collection[0];
  145. Assert.Same(collection[0], target.SelectedItem);
  146. Assert.Equal(collection[0].Content, target.SelectedContent);
  147. collection.RemoveAt(0);
  148. Assert.Same(collection[0], target.SelectedItem);
  149. Assert.Equal(collection[0].Content, target.SelectedContent);
  150. }
  151. [Fact]
  152. public void Removal_Should_Set_New_Item0_When_Item0_Selected_With_DataTemplate()
  153. {
  154. using var app = UnitTestApplication.Start(TestServices.StyledWindow);
  155. var collection = new ObservableCollection<Item>()
  156. {
  157. new Item("first"),
  158. new Item("second"),
  159. new Item("3rd"),
  160. };
  161. var target = new TabControl
  162. {
  163. Template = TabControlTemplate(),
  164. Items = collection,
  165. };
  166. Prepare(target);
  167. target.SelectedItem = collection[0];
  168. Assert.Same(collection[0], target.SelectedItem);
  169. Assert.Equal(collection[0], target.SelectedContent);
  170. collection.RemoveAt(0);
  171. Assert.Same(collection[0], target.SelectedItem);
  172. Assert.Equal(collection[0], target.SelectedContent);
  173. }
  174. [Fact]
  175. public void TabItem_Templates_Should_Be_Set_Before_TabItem_ApplyTemplate()
  176. {
  177. var template = new FuncControlTemplate<TabItem>((x, __) => new Decorator());
  178. TabControl target;
  179. var root = new TestRoot
  180. {
  181. Styles =
  182. {
  183. new Style(x => x.OfType<TabItem>())
  184. {
  185. Setters =
  186. {
  187. new Setter(TemplatedControl.TemplateProperty, template)
  188. }
  189. }
  190. },
  191. Child = (target = new TabControl
  192. {
  193. Template = TabControlTemplate(),
  194. Items =
  195. {
  196. new TabItem
  197. {
  198. Name = "first",
  199. Content = "foo",
  200. },
  201. new TabItem
  202. {
  203. Name = "second",
  204. Content = "bar",
  205. },
  206. new TabItem
  207. {
  208. Name = "3rd",
  209. Content = "barf",
  210. },
  211. },
  212. })
  213. };
  214. var collection = target.Items.Cast<TabItem>().ToList();
  215. Assert.Same(collection[0].Template, template);
  216. Assert.Same(collection[1].Template, template);
  217. Assert.Same(collection[2].Template, template);
  218. }
  219. [Fact]
  220. public void DataContexts_Should_Be_Correctly_Set()
  221. {
  222. var items = new object[]
  223. {
  224. "Foo",
  225. new Item("Bar"),
  226. new TextBlock { Text = "Baz" },
  227. new TabItem { Content = "Qux" },
  228. new TabItem { Content = new TextBlock { Text = "Bob" } }
  229. };
  230. var target = new TabControl
  231. {
  232. Template = TabControlTemplate(),
  233. DataContext = "Base",
  234. DataTemplates =
  235. {
  236. new FuncDataTemplate<Item>((x, __) => new Button { Content = x })
  237. },
  238. Items = items,
  239. };
  240. ApplyTemplate(target);
  241. ((ContentPresenter)target.ContentPart).UpdateChild();
  242. var dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  243. Assert.Equal(items[0], dataContext);
  244. target.SelectedIndex = 1;
  245. ((ContentPresenter)target.ContentPart).UpdateChild();
  246. dataContext = ((Button)target.ContentPart.Child).DataContext;
  247. Assert.Equal(items[1], dataContext);
  248. target.SelectedIndex = 2;
  249. ((ContentPresenter)target.ContentPart).UpdateChild();
  250. dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  251. Assert.Equal("Base", dataContext);
  252. target.SelectedIndex = 3;
  253. ((ContentPresenter)target.ContentPart).UpdateChild();
  254. dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  255. Assert.Equal("Qux", dataContext);
  256. target.SelectedIndex = 4;
  257. ((ContentPresenter)target.ContentPart).UpdateChild();
  258. dataContext = ((Control)target.ContentPart).DataContext;
  259. Assert.Equal("Base", dataContext);
  260. }
  261. /// <summary>
  262. /// Non-headered control items should result in TabItems with empty header.
  263. /// </summary>
  264. /// <remarks>
  265. /// If a TabControl is created with non IHeadered controls as its items, don't try to
  266. /// display the control in the header: if the control is part of the header then
  267. /// *that* control would also end up in the content region, resulting in dual-parentage
  268. /// breakage.
  269. /// </remarks>
  270. [Fact]
  271. public void Non_IHeadered_Control_Items_Should_Be_Ignored()
  272. {
  273. var items = new[]
  274. {
  275. new TextBlock { Text = "foo" },
  276. new TextBlock { Text = "bar" },
  277. };
  278. var target = new TabControl
  279. {
  280. Template = TabControlTemplate(),
  281. Items = items,
  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 = new[]
  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. Items = 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 = new AvaloniaList<object> { 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' Items='{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.Items.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. }