TabControlTests.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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.Selection;
  9. using Avalonia.Controls.Templates;
  10. using Avalonia.Controls.Utils;
  11. using Avalonia.Input;
  12. using Avalonia.LogicalTree;
  13. using Avalonia.Markup.Xaml;
  14. using Avalonia.Styling;
  15. using Avalonia.UnitTests;
  16. using Xunit;
  17. namespace Avalonia.Controls.UnitTests
  18. {
  19. public class TabControlTests
  20. {
  21. [Fact]
  22. public void First_Tab_Should_Be_Selected_By_Default()
  23. {
  24. TabItem selected;
  25. var target = new TabControl
  26. {
  27. Template = TabControlTemplate(),
  28. Items =
  29. {
  30. (selected = new TabItem
  31. {
  32. Name = "first",
  33. Content = "foo",
  34. }),
  35. new TabItem
  36. {
  37. Name = "second",
  38. Content = "bar",
  39. },
  40. }
  41. };
  42. target.ApplyTemplate();
  43. Assert.Equal(0, target.SelectedIndex);
  44. Assert.Equal(selected, target.SelectedItem);
  45. }
  46. [Fact]
  47. public void Pre_Selecting_TabItem_Should_Set_SelectedContent_After_It_Was_Added()
  48. {
  49. const string secondContent = "Second";
  50. var target = new TabControl
  51. {
  52. Template = TabControlTemplate(),
  53. Items =
  54. {
  55. new TabItem { Header = "First"},
  56. new TabItem { Header = "Second", Content = secondContent, IsSelected = true }
  57. },
  58. };
  59. ApplyTemplate(target);
  60. Assert.Equal(secondContent, target.SelectedContent);
  61. }
  62. [Fact]
  63. public void Logical_Children_Should_Be_TabItems()
  64. {
  65. var target = new TabControl
  66. {
  67. Template = TabControlTemplate(),
  68. Items =
  69. {
  70. new TabItem
  71. {
  72. Content = "foo"
  73. },
  74. new TabItem
  75. {
  76. Content = "bar"
  77. },
  78. }
  79. };
  80. Assert.Equal(target.Items, target.GetLogicalChildren().ToList());
  81. target.ApplyTemplate();
  82. Assert.Equal(target.Items, target.GetLogicalChildren().ToList());
  83. }
  84. [Fact]
  85. public void Removal_Should_Set_First_Tab()
  86. {
  87. var target = new TabControl
  88. {
  89. Template = TabControlTemplate(),
  90. Items =
  91. {
  92. new TabItem
  93. {
  94. Name = "first",
  95. Content = "foo",
  96. },
  97. new TabItem
  98. {
  99. Name = "second",
  100. Content = "bar",
  101. },
  102. new TabItem
  103. {
  104. Name = "3rd",
  105. Content = "barf",
  106. },
  107. }
  108. };
  109. Prepare(target);
  110. target.SelectedItem = target.Items[1];
  111. var item = Assert.IsType<TabItem>(target.Items[1]);
  112. Assert.Same(item, target.SelectedItem);
  113. Assert.Equal(item.Content, target.SelectedContent);
  114. target.Items.RemoveAt(1);
  115. item = Assert.IsType<TabItem>(target.Items[0]);
  116. Assert.Same(item, target.SelectedItem);
  117. Assert.Equal(item.Content, target.SelectedContent);
  118. }
  119. [Fact]
  120. public void Removal_Should_Set_New_Item0_When_Item0_Selected()
  121. {
  122. var target = new TabControl
  123. {
  124. Template = TabControlTemplate(),
  125. Items =
  126. {
  127. new TabItem
  128. {
  129. Name = "first",
  130. Content = "foo",
  131. },
  132. new TabItem
  133. {
  134. Name = "second",
  135. Content = "bar",
  136. },
  137. new TabItem
  138. {
  139. Name = "3rd",
  140. Content = "barf",
  141. },
  142. }
  143. };
  144. Prepare(target);
  145. target.SelectedItem = target.Items[0];
  146. var item = Assert.IsType<TabItem>(target.Items[0]);
  147. Assert.Same(item, target.SelectedItem);
  148. Assert.Equal(item.Content, target.SelectedContent);
  149. target.Items.RemoveAt(0);
  150. item = Assert.IsType<TabItem>(target.Items[0]);
  151. Assert.Same(item, target.SelectedItem);
  152. Assert.Equal(item.Content, target.SelectedContent);
  153. }
  154. [Fact]
  155. public void Removal_Should_Set_New_Item0_When_Item0_Selected_With_DataTemplate()
  156. {
  157. using var app = UnitTestApplication.Start(TestServices.StyledWindow);
  158. var collection = new ObservableCollection<Item>()
  159. {
  160. new Item("first"),
  161. new Item("second"),
  162. new Item("3rd"),
  163. };
  164. var target = new TabControl
  165. {
  166. Template = TabControlTemplate(),
  167. ItemsSource = collection,
  168. };
  169. Prepare(target);
  170. target.SelectedItem = collection[0];
  171. Assert.Same(collection[0], target.SelectedItem);
  172. Assert.Equal(collection[0], target.SelectedContent);
  173. collection.RemoveAt(0);
  174. Assert.Same(collection[0], target.SelectedItem);
  175. Assert.Equal(collection[0], target.SelectedContent);
  176. }
  177. [Fact]
  178. public void TabItem_Templates_Should_Be_Set_Before_TabItem_ApplyTemplate()
  179. {
  180. var template = new FuncControlTemplate<TabItem>((x, __) => new Decorator());
  181. TabControl target;
  182. var root = new TestRoot
  183. {
  184. Styles =
  185. {
  186. new Style(x => x.OfType<TabItem>())
  187. {
  188. Setters =
  189. {
  190. new Setter(TemplatedControl.TemplateProperty, template)
  191. }
  192. }
  193. },
  194. Child = (target = new TabControl
  195. {
  196. Template = TabControlTemplate(),
  197. Items =
  198. {
  199. new TabItem
  200. {
  201. Name = "first",
  202. Content = "foo",
  203. },
  204. new TabItem
  205. {
  206. Name = "second",
  207. Content = "bar",
  208. },
  209. new TabItem
  210. {
  211. Name = "3rd",
  212. Content = "barf",
  213. },
  214. },
  215. })
  216. };
  217. var collection = target.Items.Cast<TabItem>().ToList();
  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. ItemsSource = items,
  242. };
  243. ApplyTemplate(target);
  244. target.ContentPart.UpdateChild();
  245. var dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  246. Assert.Equal(items[0], dataContext);
  247. target.SelectedIndex = 1;
  248. target.ContentPart.UpdateChild();
  249. dataContext = ((Button)target.ContentPart.Child).DataContext;
  250. Assert.Equal(items[1], dataContext);
  251. target.SelectedIndex = 2;
  252. target.ContentPart.UpdateChild();
  253. dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  254. Assert.Equal("Base", dataContext);
  255. target.SelectedIndex = 3;
  256. target.ContentPart.UpdateChild();
  257. dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  258. Assert.Equal("Qux", dataContext);
  259. target.SelectedIndex = 4;
  260. 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 target = new TabControl
  277. {
  278. Template = TabControlTemplate(),
  279. Items =
  280. {
  281. new TextBlock { Text = "foo" },
  282. new TextBlock { Text = "bar" },
  283. },
  284. };
  285. ApplyTemplate(target);
  286. var logicalChildren = target.GetLogicalChildren();
  287. var result = logicalChildren
  288. .OfType<TabItem>()
  289. .Select(x => x.Header)
  290. .ToList();
  291. Assert.Equal(new object[] { null, null }, result);
  292. }
  293. [Fact]
  294. public void Should_Handle_Changing_To_TabItem_With_Null_Content()
  295. {
  296. TabControl target = new TabControl
  297. {
  298. Template = TabControlTemplate(),
  299. Items =
  300. {
  301. new TabItem { Header = "Foo" },
  302. new TabItem { Header = "Foo", Content = new Decorator() },
  303. new TabItem { Header = "Baz" },
  304. },
  305. };
  306. ApplyTemplate(target);
  307. target.SelectedIndex = 2;
  308. var page = (TabItem)target.SelectedItem;
  309. Assert.Null(page.Content);
  310. }
  311. [Fact]
  312. public void DataTemplate_Created_Content_Should_Be_Logical_Child_After_ApplyTemplate()
  313. {
  314. TabControl target = new TabControl
  315. {
  316. Template = TabControlTemplate(),
  317. ContentTemplate = new FuncDataTemplate<string>((x, _) =>
  318. new TextBlock { Tag = "bar", Text = x }),
  319. ItemsSource = new[] { "Foo" },
  320. };
  321. var root = new TestRoot(target);
  322. ApplyTemplate(target);
  323. target.ContentPart.UpdateChild();
  324. var content = Assert.IsType<TextBlock>(target.ContentPart.Child);
  325. Assert.Equal("bar", content.Tag);
  326. Assert.Same(target, content.GetLogicalParent());
  327. Assert.Single(target.GetLogicalChildren(), content);
  328. }
  329. [Fact]
  330. public void SelectedContentTemplate_Updates_After_New_ContentTemplate()
  331. {
  332. TabControl target = new TabControl
  333. {
  334. Template = TabControlTemplate(),
  335. ItemsSource = new[] { "Foo" },
  336. };
  337. var root = new TestRoot(target);
  338. ApplyTemplate(target);
  339. ((ContentPresenter)target.ContentPart).UpdateChild();
  340. Assert.Equal(null, Assert.IsType<TextBlock>(target.ContentPart.Child).Tag);
  341. target.ContentTemplate = new FuncDataTemplate<string>((x, _) =>
  342. new TextBlock { Tag = "bar", Text = x });
  343. Assert.Equal("bar", Assert.IsType<TextBlock>(target.ContentPart.Child).Tag);
  344. }
  345. [Fact]
  346. public void Should_Not_Propagate_DataContext_To_TabItem_Content()
  347. {
  348. var dataContext = "DataContext";
  349. var tabItem = new TabItem();
  350. var target = new TabControl
  351. {
  352. Template = TabControlTemplate(),
  353. DataContext = dataContext,
  354. Items = { tabItem }
  355. };
  356. ApplyTemplate(target);
  357. Assert.NotEqual(dataContext, tabItem.Content);
  358. }
  359. [Fact]
  360. public void Can_Have_Empty_Tab_Control()
  361. {
  362. using (UnitTestApplication.Start(TestServices.StyledWindow))
  363. {
  364. var xaml = @"
  365. <Window xmlns='https://github.com/avaloniaui'
  366. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  367. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
  368. <TabControl Name='tabs' ItemsSource='{Binding Tabs}'/>
  369. </Window>";
  370. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  371. var tabControl = window.FindControl<TabControl>("tabs");
  372. tabControl.DataContext = new { Tabs = new List<string>() };
  373. window.ApplyTemplate();
  374. Assert.Equal(0, tabControl.ItemsSource.Count());
  375. }
  376. }
  377. [Fact]
  378. public void Tab_Navigation_Should_Move_To_First_TabItem_When_No_Anchor_Element_Selected()
  379. {
  380. var services = TestServices.StyledWindow.With(
  381. focusManager: new FocusManager(),
  382. keyboardDevice: () => new KeyboardDevice());
  383. using var app = UnitTestApplication.Start(services);
  384. var target = new TabControl
  385. {
  386. Template = TabControlTemplate(),
  387. Items =
  388. {
  389. new TabItem { Header = "foo" },
  390. new TabItem { Header = "bar" },
  391. new TabItem { Header = "baz" },
  392. }
  393. };
  394. var button = new Button
  395. {
  396. Content = "Button",
  397. [DockPanel.DockProperty] = Dock.Top,
  398. };
  399. var root = new TestRoot
  400. {
  401. Child = new DockPanel
  402. {
  403. Children =
  404. {
  405. button,
  406. target,
  407. }
  408. }
  409. };
  410. var navigation = new KeyboardNavigationHandler();
  411. navigation.SetOwner(root);
  412. root.LayoutManager.ExecuteInitialLayoutPass();
  413. button.Focus();
  414. RaiseKeyEvent(button, Key.Tab);
  415. var item = target.ContainerFromIndex(0);
  416. Assert.Same(item, root.FocusManager.GetFocusedElement());
  417. }
  418. [Fact]
  419. public void Tab_Navigation_Should_Move_To_Anchor_TabItem()
  420. {
  421. var services = TestServices.StyledWindow.With(
  422. focusManager: new FocusManager(),
  423. keyboardDevice: () => new KeyboardDevice());
  424. using var app = UnitTestApplication.Start(services);
  425. var target = new TestTabControl
  426. {
  427. Template = TabControlTemplate(),
  428. Items =
  429. {
  430. new TabItem { Header = "foo" },
  431. new TabItem { Header = "bar" },
  432. new TabItem { Header = "baz" },
  433. }
  434. };
  435. var button = new Button
  436. {
  437. Content = "Button",
  438. [DockPanel.DockProperty] = Dock.Top,
  439. };
  440. var root = new TestRoot
  441. {
  442. Width = 1000,
  443. Height = 1000,
  444. Child = new DockPanel
  445. {
  446. Children =
  447. {
  448. button,
  449. target,
  450. }
  451. }
  452. };
  453. var navigation = new KeyboardNavigationHandler();
  454. navigation.SetOwner(root);
  455. root.LayoutManager.ExecuteInitialLayoutPass();
  456. button.Focus();
  457. target.Selection.AnchorIndex = 1;
  458. RaiseKeyEvent(button, Key.Tab);
  459. var item = target.ContainerFromIndex(1);
  460. Assert.Same(item, root.FocusManager.GetFocusedElement());
  461. RaiseKeyEvent(item, Key.Tab);
  462. Assert.Same(button, root.FocusManager.GetFocusedElement());
  463. target.Selection.AnchorIndex = 2;
  464. RaiseKeyEvent(button, Key.Tab);
  465. item = target.ContainerFromIndex(2);
  466. Assert.Same(item, root.FocusManager.GetFocusedElement());
  467. }
  468. private static IControlTemplate TabControlTemplate()
  469. {
  470. return new FuncControlTemplate<TabControl>((parent, scope) =>
  471. new StackPanel
  472. {
  473. Children =
  474. {
  475. new ItemsPresenter
  476. {
  477. Name = "PART_ItemsPresenter",
  478. }.RegisterInNameScope(scope),
  479. new ContentPresenter
  480. {
  481. Name = "PART_SelectedContentHost",
  482. [!ContentPresenter.ContentProperty] = parent[!TabControl.SelectedContentProperty],
  483. [!ContentPresenter.ContentTemplateProperty] = parent[!TabControl.SelectedContentTemplateProperty],
  484. }.RegisterInNameScope(scope)
  485. }
  486. });
  487. }
  488. private static IControlTemplate TabItemTemplate()
  489. {
  490. return new FuncControlTemplate<TabItem>((parent, scope) =>
  491. new ContentPresenter
  492. {
  493. Name = "PART_ContentPresenter",
  494. [!ContentPresenter.ContentProperty] = parent[!TabItem.HeaderProperty],
  495. [!ContentPresenter.ContentTemplateProperty] = parent[!TabItem.HeaderTemplateProperty]
  496. }.RegisterInNameScope(scope));
  497. }
  498. private static void Prepare(TabControl target)
  499. {
  500. ApplyTemplate(target);
  501. target.Measure(Size.Infinity);
  502. target.Arrange(new Rect(target.DesiredSize));
  503. }
  504. private static void RaiseKeyEvent(Control target, Key key, KeyModifiers inputModifiers = 0)
  505. {
  506. target.RaiseEvent(new KeyEventArgs
  507. {
  508. RoutedEvent = InputElement.KeyDownEvent,
  509. KeyModifiers = inputModifiers,
  510. Key = key
  511. });
  512. }
  513. private static void ApplyTemplate(TabControl target)
  514. {
  515. target.ApplyTemplate();
  516. target.Presenter.ApplyTemplate();
  517. foreach (var tabItem in target.GetLogicalChildren().OfType<TabItem>())
  518. {
  519. tabItem.Template = TabItemTemplate();
  520. tabItem.ApplyTemplate();
  521. tabItem.Presenter.UpdateChild();
  522. }
  523. target.ContentPart.ApplyTemplate();
  524. }
  525. private class Item
  526. {
  527. public Item(string value)
  528. {
  529. Value = value;
  530. }
  531. public string Value { get; }
  532. }
  533. private class TestTabControl : TabControl
  534. {
  535. protected override Type StyleKeyOverride => typeof(TabControl);
  536. public new ISelectionModel Selection => base.Selection;
  537. }
  538. }
  539. }