TabControlTests.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  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.Data;
  12. using Avalonia.Input;
  13. using Avalonia.Layout;
  14. using Avalonia.LogicalTree;
  15. using Avalonia.Markup.Xaml;
  16. using Avalonia.Platform;
  17. using Avalonia.Styling;
  18. using Avalonia.UnitTests;
  19. using Moq;
  20. using Xunit;
  21. namespace Avalonia.Controls.UnitTests
  22. {
  23. public class TabControlTests
  24. {
  25. static TabControlTests()
  26. {
  27. RuntimeHelpers.RunClassConstructor(typeof(RelativeSource).TypeHandle);
  28. }
  29. [Fact]
  30. public void First_Tab_Should_Be_Selected_By_Default()
  31. {
  32. TabItem selected;
  33. var target = new TabControl
  34. {
  35. Template = TabControlTemplate(),
  36. Items =
  37. {
  38. (selected = new TabItem
  39. {
  40. Name = "first",
  41. Content = "foo",
  42. }),
  43. new TabItem
  44. {
  45. Name = "second",
  46. Content = "bar",
  47. },
  48. }
  49. };
  50. target.ApplyTemplate();
  51. Assert.Equal(0, target.SelectedIndex);
  52. Assert.Equal(selected, target.SelectedItem);
  53. }
  54. [Fact]
  55. public void Pre_Selecting_TabItem_Should_Set_SelectedContent_After_It_Was_Added()
  56. {
  57. const string secondContent = "Second";
  58. var target = new TabControl
  59. {
  60. Template = TabControlTemplate(),
  61. Items =
  62. {
  63. new TabItem { Header = "First"},
  64. new TabItem { Header = "Second", Content = secondContent, IsSelected = true }
  65. },
  66. };
  67. ApplyTemplate(target);
  68. Assert.Equal(secondContent, target.SelectedContent);
  69. }
  70. [Fact]
  71. public void Logical_Children_Should_Be_TabItems()
  72. {
  73. var target = new TabControl
  74. {
  75. Template = TabControlTemplate(),
  76. Items =
  77. {
  78. new TabItem
  79. {
  80. Content = "foo"
  81. },
  82. new TabItem
  83. {
  84. Content = "bar"
  85. },
  86. }
  87. };
  88. Assert.Equal(target.Items, target.GetLogicalChildren().ToList());
  89. target.ApplyTemplate();
  90. Assert.Equal(target.Items, target.GetLogicalChildren().ToList());
  91. }
  92. [Fact]
  93. public void Removal_Should_Set_First_Tab()
  94. {
  95. var target = new TabControl
  96. {
  97. Template = TabControlTemplate(),
  98. Items =
  99. {
  100. new TabItem
  101. {
  102. Name = "first",
  103. Content = "foo",
  104. },
  105. new TabItem
  106. {
  107. Name = "second",
  108. Content = "bar",
  109. },
  110. new TabItem
  111. {
  112. Name = "3rd",
  113. Content = "barf",
  114. },
  115. }
  116. };
  117. Prepare(target);
  118. target.SelectedItem = target.Items[1];
  119. var item = Assert.IsType<TabItem>(target.Items[1]);
  120. Assert.Same(item, target.SelectedItem);
  121. Assert.Equal(item.Content, target.SelectedContent);
  122. target.Items.RemoveAt(1);
  123. item = Assert.IsType<TabItem>(target.Items[0]);
  124. Assert.Same(item, target.SelectedItem);
  125. Assert.Equal(item.Content, target.SelectedContent);
  126. }
  127. [Fact]
  128. public void Removal_Should_Set_New_Item0_When_Item0_Selected()
  129. {
  130. var target = new TabControl
  131. {
  132. Template = TabControlTemplate(),
  133. Items =
  134. {
  135. new TabItem
  136. {
  137. Name = "first",
  138. Content = "foo",
  139. },
  140. new TabItem
  141. {
  142. Name = "second",
  143. Content = "bar",
  144. },
  145. new TabItem
  146. {
  147. Name = "3rd",
  148. Content = "barf",
  149. },
  150. }
  151. };
  152. Prepare(target);
  153. target.SelectedItem = target.Items[0];
  154. var item = Assert.IsType<TabItem>(target.Items[0]);
  155. Assert.Same(item, target.SelectedItem);
  156. Assert.Equal(item.Content, target.SelectedContent);
  157. target.Items.RemoveAt(0);
  158. item = Assert.IsType<TabItem>(target.Items[0]);
  159. Assert.Same(item, target.SelectedItem);
  160. Assert.Equal(item.Content, target.SelectedContent);
  161. }
  162. [Fact]
  163. public void Removal_Should_Set_New_Item0_When_Item0_Selected_With_DataTemplate()
  164. {
  165. using var app = UnitTestApplication.Start(TestServices.StyledWindow);
  166. var collection = new ObservableCollection<Item>()
  167. {
  168. new Item("first"),
  169. new Item("second"),
  170. new Item("3rd"),
  171. };
  172. var target = new TabControl
  173. {
  174. Template = TabControlTemplate(),
  175. ItemsSource = collection,
  176. };
  177. Prepare(target);
  178. target.SelectedItem = collection[0];
  179. Assert.Same(collection[0], target.SelectedItem);
  180. Assert.Equal(collection[0], target.SelectedContent);
  181. collection.RemoveAt(0);
  182. Assert.Same(collection[0], target.SelectedItem);
  183. Assert.Equal(collection[0], target.SelectedContent);
  184. }
  185. [Fact]
  186. public void TabItem_Templates_Should_Be_Set_Before_TabItem_ApplyTemplate()
  187. {
  188. var template = new FuncControlTemplate<TabItem>((x, __) => new Decorator());
  189. TabControl target;
  190. var root = new TestRoot
  191. {
  192. Styles =
  193. {
  194. new Style(x => x.OfType<TabItem>())
  195. {
  196. Setters =
  197. {
  198. new Setter(TemplatedControl.TemplateProperty, template)
  199. }
  200. }
  201. },
  202. Child = (target = new TabControl
  203. {
  204. Template = TabControlTemplate(),
  205. Items =
  206. {
  207. new TabItem
  208. {
  209. Name = "first",
  210. Content = "foo",
  211. },
  212. new TabItem
  213. {
  214. Name = "second",
  215. Content = "bar",
  216. },
  217. new TabItem
  218. {
  219. Name = "3rd",
  220. Content = "barf",
  221. },
  222. },
  223. })
  224. };
  225. var collection = target.Items.Cast<TabItem>().ToList();
  226. Assert.Same(collection[0].Template, template);
  227. Assert.Same(collection[1].Template, template);
  228. Assert.Same(collection[2].Template, template);
  229. }
  230. [Fact]
  231. public void DataContexts_Should_Be_Correctly_Set()
  232. {
  233. var items = new object[]
  234. {
  235. "Foo",
  236. new Item("Bar"),
  237. new TextBlock { Text = "Baz" },
  238. new TabItem { Content = "Qux" },
  239. new TabItem { Content = new TextBlock { Text = "Bob" } }
  240. };
  241. var target = new TabControl
  242. {
  243. Template = TabControlTemplate(),
  244. DataContext = "Base",
  245. DataTemplates =
  246. {
  247. new FuncDataTemplate<Item>((x, __) => new Button { Content = x })
  248. },
  249. ItemsSource = items,
  250. };
  251. ApplyTemplate(target);
  252. target.ContentPart.UpdateChild();
  253. var dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  254. Assert.Equal(items[0], dataContext);
  255. target.SelectedIndex = 1;
  256. target.ContentPart.UpdateChild();
  257. dataContext = ((Button)target.ContentPart.Child).DataContext;
  258. Assert.Equal(items[1], dataContext);
  259. target.SelectedIndex = 2;
  260. target.ContentPart.UpdateChild();
  261. dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  262. Assert.Equal("Base", dataContext);
  263. target.SelectedIndex = 3;
  264. target.ContentPart.UpdateChild();
  265. dataContext = ((TextBlock)target.ContentPart.Child).DataContext;
  266. Assert.Equal("Qux", dataContext);
  267. target.SelectedIndex = 4;
  268. target.ContentPart.UpdateChild();
  269. dataContext = target.ContentPart.DataContext;
  270. Assert.Equal("Base", dataContext);
  271. }
  272. /// <summary>
  273. /// Non-headered control items should result in TabItems with empty header.
  274. /// </summary>
  275. /// <remarks>
  276. /// If a TabControl is created with non IHeadered controls as its items, don't try to
  277. /// display the control in the header: if the control is part of the header then
  278. /// *that* control would also end up in the content region, resulting in dual-parentage
  279. /// breakage.
  280. /// </remarks>
  281. [Fact]
  282. public void Non_IHeadered_Control_Items_Should_Be_Ignored()
  283. {
  284. var target = new TabControl
  285. {
  286. Template = TabControlTemplate(),
  287. Items =
  288. {
  289. new TextBlock { Text = "foo" },
  290. new TextBlock { Text = "bar" },
  291. },
  292. };
  293. ApplyTemplate(target);
  294. var logicalChildren = target.GetLogicalChildren();
  295. var result = logicalChildren
  296. .OfType<TabItem>()
  297. .Select(x => x.Header)
  298. .ToList();
  299. Assert.Equal(new object[] { null, null }, result);
  300. }
  301. [Fact]
  302. public void Should_Handle_Changing_To_TabItem_With_Null_Content()
  303. {
  304. TabControl target = new TabControl
  305. {
  306. Template = TabControlTemplate(),
  307. Items =
  308. {
  309. new TabItem { Header = "Foo" },
  310. new TabItem { Header = "Foo", Content = new Decorator() },
  311. new TabItem { Header = "Baz" },
  312. },
  313. };
  314. ApplyTemplate(target);
  315. target.SelectedIndex = 2;
  316. var page = (TabItem)target.SelectedItem;
  317. Assert.Null(page.Content);
  318. }
  319. [Fact]
  320. public void DataTemplate_Created_Content_Should_Be_Logical_Child_After_ApplyTemplate()
  321. {
  322. TabControl target = new TabControl
  323. {
  324. Template = TabControlTemplate(),
  325. ContentTemplate = new FuncDataTemplate<string>((x, _) =>
  326. new TextBlock { Tag = "bar", Text = x }),
  327. ItemsSource = new[] { "Foo" },
  328. };
  329. var root = new TestRoot(target);
  330. ApplyTemplate(target);
  331. target.ContentPart.UpdateChild();
  332. var content = Assert.IsType<TextBlock>(target.ContentPart.Child);
  333. Assert.Equal("bar", content.Tag);
  334. Assert.Same(target, content.GetLogicalParent());
  335. Assert.Single(target.GetLogicalChildren(), content);
  336. }
  337. [Fact]
  338. public void SelectedContentTemplate_Updates_After_New_ContentTemplate()
  339. {
  340. TabControl target = new TabControl
  341. {
  342. Template = TabControlTemplate(),
  343. ItemsSource = new[] { "Foo" },
  344. };
  345. var root = new TestRoot(target);
  346. ApplyTemplate(target);
  347. ((ContentPresenter)target.ContentPart).UpdateChild();
  348. Assert.Equal(null, Assert.IsType<TextBlock>(target.ContentPart.Child).Tag);
  349. target.ContentTemplate = new FuncDataTemplate<string>((x, _) =>
  350. new TextBlock { Tag = "bar", Text = x });
  351. Assert.Equal("bar", Assert.IsType<TextBlock>(target.ContentPart.Child).Tag);
  352. }
  353. [Fact]
  354. public void Previous_ContentTemplate_Is_Not_Reused_When_TabItem_Changes()
  355. {
  356. using var app = UnitTestApplication.Start(TestServices.StyledWindow);
  357. int templatesBuilt = 0;
  358. var target = new TabControl
  359. {
  360. Template = TabControlTemplate(),
  361. Items =
  362. {
  363. TabItemFactory("First tab content"),
  364. TabItemFactory("Second tab content"),
  365. },
  366. };
  367. var root = new TestRoot(target);
  368. ApplyTemplate(target);
  369. target.SelectedIndex = 0;
  370. target.SelectedIndex = 1;
  371. Assert.Equal(2, templatesBuilt);
  372. TabItem TabItemFactory(object content) => new()
  373. {
  374. Content = content,
  375. ContentTemplate = new FuncDataTemplate<object>((actual, ns) =>
  376. {
  377. Assert.Equal(content, actual);
  378. templatesBuilt++;
  379. return new Border();
  380. })
  381. };
  382. }
  383. [Fact]
  384. public void Should_Not_Propagate_DataContext_To_TabItem_Content()
  385. {
  386. var dataContext = "DataContext";
  387. var tabItem = new TabItem();
  388. var target = new TabControl
  389. {
  390. Template = TabControlTemplate(),
  391. DataContext = dataContext,
  392. Items = { tabItem }
  393. };
  394. ApplyTemplate(target);
  395. Assert.NotEqual(dataContext, tabItem.Content);
  396. }
  397. [Fact]
  398. public void Can_Have_Empty_Tab_Control()
  399. {
  400. using (UnitTestApplication.Start(TestServices.StyledWindow))
  401. {
  402. var xaml = @"
  403. <Window xmlns='https://github.com/avaloniaui'
  404. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  405. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
  406. <TabControl Name='tabs' ItemsSource='{Binding Tabs}'/>
  407. </Window>";
  408. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  409. var tabControl = window.FindControl<TabControl>("tabs");
  410. tabControl.DataContext = new { Tabs = new List<string>() };
  411. window.ApplyTemplate();
  412. Assert.Equal(0, tabControl.ItemsSource.Count());
  413. }
  414. }
  415. [Fact]
  416. public void Should_Have_Initial_SelectedValue()
  417. {
  418. var xaml = @"
  419. <TabControl
  420. xmlns='https://github.com/avaloniaui'
  421. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  422. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'
  423. x:DataType='TabItem'
  424. x:Name='tabs'
  425. Tag='World'
  426. SelectedValue='{Binding $self.Tag}'
  427. SelectedValueBinding='{Binding Header}'>
  428. <TabItem Header='Hello'/>
  429. <TabItem Header='World'/>
  430. </TabControl>";
  431. var tabControl = (TabControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  432. Assert.Equal("World", tabControl.SelectedValue);
  433. Assert.Equal(1, tabControl.SelectedIndex);
  434. }
  435. [Fact]
  436. public void Tab_Navigation_Should_Move_To_First_TabItem_When_No_Anchor_Element_Selected()
  437. {
  438. var services = TestServices.StyledWindow.With(
  439. focusManager: new FocusManager(),
  440. keyboardDevice: () => new KeyboardDevice());
  441. using var app = UnitTestApplication.Start(services);
  442. var target = new TabControl
  443. {
  444. Template = TabControlTemplate(),
  445. Items =
  446. {
  447. new TabItem { Header = "foo" },
  448. new TabItem { Header = "bar" },
  449. new TabItem { Header = "baz" },
  450. }
  451. };
  452. var button = new Button
  453. {
  454. Content = "Button",
  455. [DockPanel.DockProperty] = Dock.Top,
  456. };
  457. var root = new TestRoot
  458. {
  459. Child = new DockPanel
  460. {
  461. Children =
  462. {
  463. button,
  464. target,
  465. }
  466. }
  467. };
  468. var navigation = new KeyboardNavigationHandler();
  469. navigation.SetOwner(root);
  470. root.LayoutManager.ExecuteInitialLayoutPass();
  471. button.Focus();
  472. RaiseKeyEvent(button, Key.Tab);
  473. var item = target.ContainerFromIndex(0);
  474. Assert.Same(item, root.FocusManager.GetFocusedElement());
  475. }
  476. [Fact]
  477. public void Tab_Navigation_Should_Move_To_Anchor_TabItem()
  478. {
  479. var services = TestServices.StyledWindow.With(
  480. focusManager: new FocusManager(),
  481. keyboardDevice: () => new KeyboardDevice());
  482. using var app = UnitTestApplication.Start(services);
  483. var target = new TestTabControl
  484. {
  485. Template = TabControlTemplate(),
  486. Items =
  487. {
  488. new TabItem { Header = "foo" },
  489. new TabItem { Header = "bar" },
  490. new TabItem { Header = "baz" },
  491. }
  492. };
  493. var button = new Button
  494. {
  495. Content = "Button",
  496. [DockPanel.DockProperty] = Dock.Top,
  497. };
  498. var root = new TestRoot
  499. {
  500. Width = 1000,
  501. Height = 1000,
  502. Child = new DockPanel
  503. {
  504. Children =
  505. {
  506. button,
  507. target,
  508. }
  509. }
  510. };
  511. var navigation = new KeyboardNavigationHandler();
  512. navigation.SetOwner(root);
  513. root.LayoutManager.ExecuteInitialLayoutPass();
  514. button.Focus();
  515. target.Selection.AnchorIndex = 1;
  516. RaiseKeyEvent(button, Key.Tab);
  517. var item = target.ContainerFromIndex(1);
  518. Assert.Same(item, root.FocusManager.GetFocusedElement());
  519. RaiseKeyEvent(item, Key.Tab);
  520. Assert.Same(button, root.FocusManager.GetFocusedElement());
  521. target.Selection.AnchorIndex = 2;
  522. RaiseKeyEvent(button, Key.Tab);
  523. item = target.ContainerFromIndex(2);
  524. Assert.Same(item, root.FocusManager.GetFocusedElement());
  525. }
  526. [Fact]
  527. public void TabItem_Header_Should_Be_Settable_By_Style_When_DataContext_Is_Set()
  528. {
  529. var tabItem = new TabItem
  530. {
  531. DataContext = "Some DataContext"
  532. };
  533. _ = new TestRoot
  534. {
  535. Styles =
  536. {
  537. new Style(x => x.OfType<TabItem>())
  538. {
  539. Setters =
  540. {
  541. new Setter(HeaderedContentControl.HeaderProperty, "Header from style")
  542. }
  543. }
  544. },
  545. Child = tabItem
  546. };
  547. Assert.Equal("Header from style", tabItem.Header);
  548. }
  549. [Fact]
  550. public void TabItem_TabStripPlacement_Should_Be_Correctly_Set()
  551. {
  552. var items = new object[]
  553. {
  554. "Foo",
  555. new TabItem { Content = new TextBlock { Text = "Baz" } }
  556. };
  557. var target = new TabControl
  558. {
  559. Template = TabControlTemplate(),
  560. DataContext = "Base",
  561. ItemsSource = items
  562. };
  563. ApplyTemplate(target);
  564. var result = target.GetLogicalChildren()
  565. .OfType<TabItem>()
  566. .ToList();
  567. Assert.Collection(
  568. result,
  569. x => Assert.Equal(Dock.Top, x.TabStripPlacement),
  570. x => Assert.Equal(Dock.Top, x.TabStripPlacement)
  571. );
  572. target.TabStripPlacement = Dock.Right;
  573. result = target.GetLogicalChildren()
  574. .OfType<TabItem>()
  575. .ToList();
  576. Assert.Collection(
  577. result,
  578. x => Assert.Equal(Dock.Right, x.TabStripPlacement),
  579. x => Assert.Equal(Dock.Right, x.TabStripPlacement)
  580. );
  581. }
  582. [Fact]
  583. public void TabItem_TabStripPlacement_Should_Be_Correctly_Set_For_New_Items()
  584. {
  585. var items = new object[]
  586. {
  587. "Foo",
  588. new TabItem { Content = new TextBlock { Text = "Baz" } }
  589. };
  590. var target = new TabControl
  591. {
  592. Template = TabControlTemplate(),
  593. DataContext = "Base"
  594. };
  595. ApplyTemplate(target);
  596. target.ItemsSource = items;
  597. var result = target.GetLogicalChildren()
  598. .OfType<TabItem>()
  599. .ToList();
  600. Assert.Collection(
  601. result,
  602. x => Assert.Equal(Dock.Top, x.TabStripPlacement),
  603. x => Assert.Equal(Dock.Top, x.TabStripPlacement)
  604. );
  605. target.TabStripPlacement = Dock.Right;
  606. result = target.GetLogicalChildren()
  607. .OfType<TabItem>()
  608. .ToList();
  609. Assert.Collection(
  610. result,
  611. x => Assert.Equal(Dock.Right, x.TabStripPlacement),
  612. x => Assert.Equal(Dock.Right, x.TabStripPlacement)
  613. );
  614. }
  615. [Theory]
  616. [InlineData(Key.A, 1)]
  617. [InlineData(Key.L, 2)]
  618. [InlineData(Key.D, 0)]
  619. public void Should_TabControl_Recognizes_AccessKey(Key accessKey, int selectedTabIndex)
  620. {
  621. var ah = new AccessKeyHandler();
  622. var kd = new KeyboardDevice();
  623. using (UnitTestApplication.Start(TestServices.StyledWindow
  624. .With(
  625. accessKeyHandler: ah,
  626. keyboardDevice: () => kd)
  627. ))
  628. {
  629. var impl = CreateMockTopLevelImpl();
  630. var tabControl = new TabControl()
  631. {
  632. Template = TabControlTemplate(),
  633. Items =
  634. {
  635. new TabItem
  636. {
  637. Header = "General",
  638. },
  639. new TabItem { Header = "_Arch" },
  640. new TabItem { Header = "_Leaf"},
  641. new TabItem { Header = "_Disabled", IsEnabled = false },
  642. }
  643. };
  644. kd.SetFocusedElement((TabItem)tabControl.Items[selectedTabIndex], NavigationMethod.Unspecified, KeyModifiers.None);
  645. var root = new TestTopLevel(impl.Object)
  646. {
  647. Template = CreateTemplate(),
  648. Content = tabControl,
  649. };
  650. root.ApplyTemplate();
  651. root.Presenter.UpdateChild();
  652. ApplyTemplate(tabControl);
  653. KeyDown(root, Key.LeftAlt);
  654. KeyDown(root, accessKey, KeyModifiers.Alt);
  655. KeyUp(root, accessKey, KeyModifiers.Alt);
  656. KeyUp(root, Key.LeftAlt);
  657. Assert.Equal(selectedTabIndex, tabControl.SelectedIndex);
  658. }
  659. static FuncControlTemplate<TestTopLevel> CreateTemplate()
  660. {
  661. return new FuncControlTemplate<TestTopLevel>((x, scope) =>
  662. new ContentPresenter
  663. {
  664. Name = "PART_ContentPresenter",
  665. [~ContentPresenter.ContentProperty] = new TemplateBinding(ContentControl.ContentProperty),
  666. [~ContentPresenter.ContentTemplateProperty] = new TemplateBinding(ContentControl.ContentTemplateProperty)
  667. }.RegisterInNameScope(scope));
  668. }
  669. static Mock<ITopLevelImpl> CreateMockTopLevelImpl(bool setupProperties = false)
  670. {
  671. var topLevel = new Mock<ITopLevelImpl>();
  672. if (setupProperties)
  673. topLevel.SetupAllProperties();
  674. topLevel.Setup(x => x.RenderScaling).Returns(1);
  675. topLevel.Setup(x => x.Compositor).Returns(RendererMocks.CreateDummyCompositor());
  676. return topLevel;
  677. }
  678. static void KeyDown(IInputElement target, Key key, KeyModifiers modifiers = KeyModifiers.None)
  679. {
  680. target.RaiseEvent(new KeyEventArgs
  681. {
  682. RoutedEvent = InputElement.KeyDownEvent,
  683. Key = key,
  684. KeyModifiers = modifiers,
  685. });
  686. }
  687. static void KeyUp(IInputElement target, Key key, KeyModifiers modifiers = KeyModifiers.None)
  688. {
  689. target.RaiseEvent(new KeyEventArgs
  690. {
  691. RoutedEvent = InputElement.KeyUpEvent,
  692. Key = key,
  693. KeyModifiers = modifiers,
  694. });
  695. }
  696. }
  697. private static IControlTemplate TabControlTemplate()
  698. {
  699. return new FuncControlTemplate<TabControl>((parent, scope) =>
  700. new StackPanel
  701. {
  702. Children =
  703. {
  704. new ItemsPresenter
  705. {
  706. Name = "PART_ItemsPresenter",
  707. }.RegisterInNameScope(scope),
  708. new ContentPresenter
  709. {
  710. Name = "PART_SelectedContentHost",
  711. [~ContentPresenter.ContentProperty] = new TemplateBinding(TabControl.SelectedContentProperty),
  712. [~ContentPresenter.ContentTemplateProperty] = new TemplateBinding(TabControl.SelectedContentTemplateProperty),
  713. }.RegisterInNameScope(scope)
  714. }
  715. });
  716. }
  717. private static IControlTemplate TabItemTemplate()
  718. {
  719. return new FuncControlTemplate<TabItem>((parent, scope) =>
  720. new ContentPresenter
  721. {
  722. Name = "PART_ContentPresenter",
  723. [~ContentPresenter.ContentProperty] = new TemplateBinding(TabItem.HeaderProperty),
  724. [~ContentPresenter.ContentTemplateProperty] = new TemplateBinding(TabItem.HeaderTemplateProperty),
  725. RecognizesAccessKey = true,
  726. }.RegisterInNameScope(scope));
  727. }
  728. private class TestTopLevel : TopLevel
  729. {
  730. private readonly ILayoutManager _layoutManager;
  731. public bool IsClosed { get; private set; }
  732. public TestTopLevel(ITopLevelImpl impl, ILayoutManager layoutManager = null)
  733. : base(impl)
  734. {
  735. _layoutManager = layoutManager ?? new LayoutManager(this);
  736. }
  737. private protected override ILayoutManager CreateLayoutManager() => _layoutManager;
  738. }
  739. private static void Prepare(TabControl target)
  740. {
  741. ApplyTemplate(target);
  742. target.Measure(Size.Infinity);
  743. target.Arrange(new Rect(target.DesiredSize));
  744. }
  745. private static void RaiseKeyEvent(Control target, Key key, KeyModifiers inputModifiers = 0)
  746. {
  747. target.RaiseEvent(new KeyEventArgs
  748. {
  749. RoutedEvent = InputElement.KeyDownEvent,
  750. KeyModifiers = inputModifiers,
  751. Key = key
  752. });
  753. }
  754. private static void ApplyTemplate(TabControl target)
  755. {
  756. target.ApplyTemplate();
  757. target.Presenter.ApplyTemplate();
  758. foreach (var tabItem in target.GetLogicalChildren().OfType<TabItem>())
  759. {
  760. tabItem.Template = TabItemTemplate();
  761. tabItem.ApplyTemplate();
  762. tabItem.Presenter.UpdateChild();
  763. }
  764. target.ContentPart.ApplyTemplate();
  765. }
  766. private class Item
  767. {
  768. public Item(string value)
  769. {
  770. Value = value;
  771. }
  772. public string Value { get; }
  773. }
  774. private class TestTabControl : TabControl
  775. {
  776. protected override Type StyleKeyOverride => typeof(TabControl);
  777. public new ISelectionModel Selection => base.Selection;
  778. }
  779. }
  780. }