TabControlTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 items = new[]
  65. {
  66. new TabItem
  67. {
  68. Content = "foo"
  69. },
  70. new TabItem
  71. {
  72. Content = "bar"
  73. },
  74. };
  75. var target = new TabControl
  76. {
  77. Template = TabControlTemplate(),
  78. Items = items,
  79. };
  80. Assert.Equal(items, target.GetLogicalChildren());
  81. target.ApplyTemplate();
  82. Assert.Equal(items, target.GetLogicalChildren());
  83. }
  84. [Fact]
  85. public void Removal_Should_Set_First_Tab()
  86. {
  87. var collection = new ObservableCollection<TabItem>()
  88. {
  89. new TabItem
  90. {
  91. Name = "first",
  92. Content = "foo",
  93. },
  94. new TabItem
  95. {
  96. Name = "second",
  97. Content = "bar",
  98. },
  99. new TabItem
  100. {
  101. Name = "3rd",
  102. Content = "barf",
  103. },
  104. };
  105. var target = new TabControl
  106. {
  107. Template = TabControlTemplate(),
  108. Items = collection,
  109. };
  110. Prepare(target);
  111. target.SelectedItem = collection[1];
  112. Assert.Same(collection[1], target.SelectedItem);
  113. Assert.Equal(collection[1].Content, target.SelectedContent);
  114. collection.RemoveAt(1);
  115. Assert.Same(collection[0], target.SelectedItem);
  116. Assert.Equal(collection[0].Content, target.SelectedContent);
  117. }
  118. [Fact]
  119. public void Removal_Should_Set_New_Item0_When_Item0_Selected()
  120. {
  121. var collection = new ObservableCollection<TabItem>()
  122. {
  123. new TabItem
  124. {
  125. Name = "first",
  126. Content = "foo",
  127. },
  128. new TabItem
  129. {
  130. Name = "second",
  131. Content = "bar",
  132. },
  133. new TabItem
  134. {
  135. Name = "3rd",
  136. Content = "barf",
  137. },
  138. };
  139. var target = new TabControl
  140. {
  141. Template = TabControlTemplate(),
  142. Items = collection,
  143. };
  144. Prepare(target);
  145. target.SelectedItem = collection[0];
  146. Assert.Same(collection[0], target.SelectedItem);
  147. Assert.Equal(collection[0].Content, target.SelectedContent);
  148. collection.RemoveAt(0);
  149. Assert.Same(collection[0], target.SelectedItem);
  150. Assert.Equal(collection[0].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. Items = 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 collection = new[]
  179. {
  180. new TabItem
  181. {
  182. Name = "first",
  183. Content = "foo",
  184. },
  185. new TabItem
  186. {
  187. Name = "second",
  188. Content = "bar",
  189. },
  190. new TabItem
  191. {
  192. Name = "3rd",
  193. Content = "barf",
  194. },
  195. };
  196. var template = new FuncControlTemplate<TabItem>((x, __) => new Decorator());
  197. using (UnitTestApplication.Start(TestServices.RealStyler))
  198. {
  199. var root = new TestRoot
  200. {
  201. Styles =
  202. {
  203. new Style(x => x.OfType<TabItem>())
  204. {
  205. Setters =
  206. {
  207. new Setter(TemplatedControl.TemplateProperty, template)
  208. }
  209. }
  210. },
  211. Child = new TabControl
  212. {
  213. Template = TabControlTemplate(),
  214. Items = collection,
  215. }
  216. };
  217. }
  218. Assert.Same(collection[0].Template, template);
  219. Assert.Same(collection[1].Template, template);
  220. Assert.Same(collection[2].Template, template);
  221. }
  222. [Fact]
  223. public void DataContexts_Should_Be_Correctly_Set()
  224. {
  225. var items = new object[]
  226. {
  227. "Foo",
  228. new Item("Bar"),
  229. new TextBlock { Text = "Baz" },
  230. new TabItem { Content = "Qux" },
  231. new TabItem { Content = new TextBlock { Text = "Bob" } }
  232. };
  233. var target = new TabControl
  234. {
  235. Template = TabControlTemplate(),
  236. DataContext = "Base",
  237. DataTemplates =
  238. {
  239. new FuncDataTemplate<Item>((x, __) => new Button { Content = x })
  240. },
  241. Items = items,
  242. };
  243. ApplyTemplate(target);
  244. ((ContentPresenter)target.ContentPart).UpdateChild();
  245. var dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  246. Assert.Equal(items[0], dataContext);
  247. target.SelectedIndex = 1;
  248. ((ContentPresenter)target.ContentPart).UpdateChild();
  249. dataContext = ((Button)target.ContentPart.Child).DataContext;
  250. Assert.Equal(items[1], dataContext);
  251. target.SelectedIndex = 2;
  252. ((ContentPresenter)target.ContentPart).UpdateChild();
  253. dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  254. Assert.Equal("Base", dataContext);
  255. target.SelectedIndex = 3;
  256. ((ContentPresenter)target.ContentPart).UpdateChild();
  257. dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  258. Assert.Equal("Qux", dataContext);
  259. target.SelectedIndex = 4;
  260. ((ContentPresenter)target.ContentPart).UpdateChild();
  261. dataContext = target.ContentPart.DataContext;
  262. Assert.Equal("Base", dataContext);
  263. }
  264. /// <summary>
  265. /// Non-headered control items should result in TabItems with empty header.
  266. /// </summary>
  267. /// <remarks>
  268. /// If a TabControl is created with non IHeadered controls as its items, don't try to
  269. /// display the control in the header: if the control is part of the header then
  270. /// *that* control would also end up in the content region, resulting in dual-parentage
  271. /// breakage.
  272. /// </remarks>
  273. [Fact]
  274. public void Non_IHeadered_Control_Items_Should_Be_Ignored()
  275. {
  276. var items = new[]
  277. {
  278. new TextBlock { Text = "foo" },
  279. new TextBlock { Text = "bar" },
  280. };
  281. var target = new TabControl
  282. {
  283. Template = TabControlTemplate(),
  284. Items = items,
  285. };
  286. ApplyTemplate(target);
  287. var logicalChildren = target.ItemsPresenterPart.Panel.GetLogicalChildren();
  288. var result = logicalChildren
  289. .OfType<TabItem>()
  290. .Select(x => x.Header)
  291. .ToList();
  292. Assert.Equal(new object[] { null, null }, result);
  293. }
  294. [Fact]
  295. public void Should_Handle_Changing_To_TabItem_With_Null_Content()
  296. {
  297. TabControl target = new TabControl
  298. {
  299. Template = TabControlTemplate(),
  300. Items = new[]
  301. {
  302. new TabItem { Header = "Foo" },
  303. new TabItem { Header = "Foo", Content = new Decorator() },
  304. new TabItem { Header = "Baz" },
  305. },
  306. };
  307. ApplyTemplate(target);
  308. target.SelectedIndex = 2;
  309. var page = (TabItem)target.SelectedItem;
  310. Assert.Null(page.Content);
  311. }
  312. [Fact]
  313. public void DataTemplate_Created_Content_Should_Be_Logical_Child_After_ApplyTemplate()
  314. {
  315. TabControl target = new TabControl
  316. {
  317. Template = TabControlTemplate(),
  318. ContentTemplate = new FuncDataTemplate<string>((x, _) =>
  319. new TextBlock { Tag = "bar", Text = x }),
  320. Items = new[] { "Foo" },
  321. };
  322. var root = new TestRoot(target);
  323. ApplyTemplate(target);
  324. ((ContentPresenter)target.ContentPart).UpdateChild();
  325. var content = Assert.IsType<TextBlock>(target.ContentPart.Child);
  326. Assert.Equal("bar", content.Tag);
  327. Assert.Same(target, content.GetLogicalParent());
  328. Assert.Single(target.GetLogicalChildren(), content);
  329. }
  330. [Fact]
  331. public void Should_Not_Propagate_DataContext_To_TabItem_Content()
  332. {
  333. var dataContext = "DataContext";
  334. var tabItem = new TabItem();
  335. var target = new TabControl
  336. {
  337. Template = TabControlTemplate(),
  338. DataContext = dataContext,
  339. Items = new AvaloniaList<object> { tabItem }
  340. };
  341. ApplyTemplate(target);
  342. Assert.NotEqual(dataContext, tabItem.Content);
  343. }
  344. [Fact]
  345. public void Can_Have_Empty_Tab_Control()
  346. {
  347. using (UnitTestApplication.Start(TestServices.StyledWindow))
  348. {
  349. var xaml = @"
  350. <Window xmlns='https://github.com/avaloniaui'
  351. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  352. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
  353. <TabControl Name='tabs' Items='{Binding Tabs}'/>
  354. </Window>";
  355. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  356. var tabControl = window.FindControl<TabControl>("tabs");
  357. tabControl.DataContext = new { Tabs = new List<string>() };
  358. window.ApplyTemplate();
  359. Assert.Equal(0, tabControl.Items.Count());
  360. }
  361. }
  362. private IControlTemplate TabControlTemplate()
  363. {
  364. return new FuncControlTemplate<TabControl>((parent, scope) =>
  365. new StackPanel
  366. {
  367. Children =
  368. {
  369. new ItemsPresenter
  370. {
  371. Name = "PART_ItemsPresenter",
  372. [!TabStrip.ItemsProperty] = parent[!TabControl.ItemsProperty],
  373. [!TabStrip.ItemTemplateProperty] = parent[!TabControl.ItemTemplateProperty],
  374. }.RegisterInNameScope(scope),
  375. new ContentPresenter
  376. {
  377. Name = "PART_SelectedContentHost",
  378. [!ContentPresenter.ContentProperty] = parent[!TabControl.SelectedContentProperty],
  379. [!ContentPresenter.ContentTemplateProperty] = parent[!TabControl.SelectedContentTemplateProperty],
  380. }.RegisterInNameScope(scope)
  381. }
  382. });
  383. }
  384. private IControlTemplate TabItemTemplate()
  385. {
  386. return new FuncControlTemplate<TabItem>((parent, scope) =>
  387. new ContentPresenter
  388. {
  389. Name = "PART_ContentPresenter",
  390. [!ContentPresenter.ContentProperty] = parent[!TabItem.HeaderProperty],
  391. [!ContentPresenter.ContentTemplateProperty] = parent[!TabItem.HeaderTemplateProperty]
  392. }.RegisterInNameScope(scope));
  393. }
  394. private void Prepare(TabControl target)
  395. {
  396. ApplyTemplate(target);
  397. target.Measure(Size.Infinity);
  398. target.Arrange(new Rect(target.DesiredSize));
  399. }
  400. private void ApplyTemplate(TabControl target)
  401. {
  402. target.ApplyTemplate();
  403. target.Presenter.ApplyTemplate();
  404. foreach (var tabItem in target.GetLogicalChildren().OfType<TabItem>())
  405. {
  406. tabItem.Template = TabItemTemplate();
  407. tabItem.ApplyTemplate();
  408. ((ContentPresenter)tabItem.Presenter).UpdateChild();
  409. }
  410. target.ContentPart.ApplyTemplate();
  411. }
  412. private class Item
  413. {
  414. public Item(string value)
  415. {
  416. Value = value;
  417. }
  418. public string Value { get; }
  419. }
  420. }
  421. }