TabControlTests.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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 : ScopedTestBase
  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. keyboardDevice: () => new KeyboardDevice());
  440. using var app = UnitTestApplication.Start(services);
  441. var target = new TabControl
  442. {
  443. Template = TabControlTemplate(),
  444. Items =
  445. {
  446. new TabItem { Header = "foo" },
  447. new TabItem { Header = "bar" },
  448. new TabItem { Header = "baz" },
  449. }
  450. };
  451. var button = new Button
  452. {
  453. Content = "Button",
  454. [DockPanel.DockProperty] = Dock.Top,
  455. };
  456. var root = new TestRoot
  457. {
  458. Child = new DockPanel
  459. {
  460. Children =
  461. {
  462. button,
  463. target,
  464. }
  465. }
  466. };
  467. var navigation = new KeyboardNavigationHandler();
  468. navigation.SetOwner(root);
  469. root.LayoutManager.ExecuteInitialLayoutPass();
  470. button.Focus();
  471. RaiseKeyEvent(button, Key.Tab);
  472. var item = target.ContainerFromIndex(0);
  473. Assert.Same(item, root.FocusManager.GetFocusedElement());
  474. }
  475. [Fact]
  476. public void Tab_Navigation_Should_Move_To_Anchor_TabItem()
  477. {
  478. var services = TestServices.StyledWindow.With(
  479. keyboardDevice: () => new KeyboardDevice());
  480. using var app = UnitTestApplication.Start(services);
  481. var target = new TestTabControl
  482. {
  483. Template = TabControlTemplate(),
  484. Items =
  485. {
  486. new TabItem { Header = "foo" },
  487. new TabItem { Header = "bar" },
  488. new TabItem { Header = "baz" },
  489. }
  490. };
  491. var button = new Button
  492. {
  493. Content = "Button",
  494. [DockPanel.DockProperty] = Dock.Top,
  495. };
  496. var root = new TestRoot
  497. {
  498. Width = 1000,
  499. Height = 1000,
  500. Child = new DockPanel
  501. {
  502. Children =
  503. {
  504. button,
  505. target,
  506. }
  507. }
  508. };
  509. var navigation = new KeyboardNavigationHandler();
  510. navigation.SetOwner(root);
  511. root.LayoutManager.ExecuteInitialLayoutPass();
  512. button.Focus();
  513. target.Selection.AnchorIndex = 1;
  514. RaiseKeyEvent(button, Key.Tab);
  515. var item = target.ContainerFromIndex(1);
  516. Assert.Same(item, root.FocusManager.GetFocusedElement());
  517. RaiseKeyEvent(item, Key.Tab);
  518. Assert.Same(button, root.FocusManager.GetFocusedElement());
  519. target.Selection.AnchorIndex = 2;
  520. RaiseKeyEvent(button, Key.Tab);
  521. item = target.ContainerFromIndex(2);
  522. Assert.Same(item, root.FocusManager.GetFocusedElement());
  523. }
  524. [Fact]
  525. public void TabItem_Header_Should_Be_Settable_By_Style_When_DataContext_Is_Set()
  526. {
  527. var tabItem = new TabItem
  528. {
  529. DataContext = "Some DataContext"
  530. };
  531. _ = new TestRoot
  532. {
  533. Styles =
  534. {
  535. new Style(x => x.OfType<TabItem>())
  536. {
  537. Setters =
  538. {
  539. new Setter(HeaderedContentControl.HeaderProperty, "Header from style")
  540. }
  541. }
  542. },
  543. Child = tabItem
  544. };
  545. Assert.Equal("Header from style", tabItem.Header);
  546. }
  547. [Fact]
  548. public void TabItem_TabStripPlacement_Should_Be_Correctly_Set()
  549. {
  550. var items = new object[]
  551. {
  552. "Foo",
  553. new TabItem { Content = new TextBlock { Text = "Baz" } }
  554. };
  555. var target = new TabControl
  556. {
  557. Template = TabControlTemplate(),
  558. DataContext = "Base",
  559. ItemsSource = items
  560. };
  561. ApplyTemplate(target);
  562. var result = target.GetLogicalChildren()
  563. .OfType<TabItem>()
  564. .ToList();
  565. Assert.Collection(
  566. result,
  567. x => Assert.Equal(Dock.Top, x.TabStripPlacement),
  568. x => Assert.Equal(Dock.Top, x.TabStripPlacement)
  569. );
  570. target.TabStripPlacement = Dock.Right;
  571. result = target.GetLogicalChildren()
  572. .OfType<TabItem>()
  573. .ToList();
  574. Assert.Collection(
  575. result,
  576. x => Assert.Equal(Dock.Right, x.TabStripPlacement),
  577. x => Assert.Equal(Dock.Right, x.TabStripPlacement)
  578. );
  579. }
  580. [Fact]
  581. public void TabItem_TabStripPlacement_Should_Be_Correctly_Set_For_New_Items()
  582. {
  583. var items = new object[]
  584. {
  585. "Foo",
  586. new TabItem { Content = new TextBlock { Text = "Baz" } }
  587. };
  588. var target = new TabControl
  589. {
  590. Template = TabControlTemplate(),
  591. DataContext = "Base"
  592. };
  593. ApplyTemplate(target);
  594. target.ItemsSource = items;
  595. var result = target.GetLogicalChildren()
  596. .OfType<TabItem>()
  597. .ToList();
  598. Assert.Collection(
  599. result,
  600. x => Assert.Equal(Dock.Top, x.TabStripPlacement),
  601. x => Assert.Equal(Dock.Top, x.TabStripPlacement)
  602. );
  603. target.TabStripPlacement = Dock.Right;
  604. result = target.GetLogicalChildren()
  605. .OfType<TabItem>()
  606. .ToList();
  607. Assert.Collection(
  608. result,
  609. x => Assert.Equal(Dock.Right, x.TabStripPlacement),
  610. x => Assert.Equal(Dock.Right, x.TabStripPlacement)
  611. );
  612. }
  613. [Theory]
  614. [InlineData(Key.A, 1)]
  615. [InlineData(Key.L, 2)]
  616. [InlineData(Key.D, 0)]
  617. public void Should_TabControl_Recognizes_AccessKey(Key accessKey, int selectedTabIndex)
  618. {
  619. var ah = new AccessKeyHandler();
  620. var kd = new KeyboardDevice();
  621. using (UnitTestApplication.Start(TestServices.StyledWindow
  622. .With(
  623. accessKeyHandler: ah,
  624. keyboardDevice: () => kd)
  625. ))
  626. {
  627. var impl = CreateMockTopLevelImpl();
  628. var tabControl = new TabControl()
  629. {
  630. Template = TabControlTemplate(),
  631. Items =
  632. {
  633. new TabItem
  634. {
  635. Header = "General",
  636. },
  637. new TabItem { Header = "_Arch" },
  638. new TabItem { Header = "_Leaf"},
  639. new TabItem { Header = "_Disabled", IsEnabled = false },
  640. }
  641. };
  642. kd.SetFocusedElement((TabItem)tabControl.Items[selectedTabIndex], NavigationMethod.Unspecified, KeyModifiers.None);
  643. var root = new TestTopLevel(impl.Object)
  644. {
  645. Template = CreateTemplate(),
  646. Content = tabControl,
  647. };
  648. root.ApplyTemplate();
  649. root.Presenter.UpdateChild();
  650. ApplyTemplate(tabControl);
  651. KeyDown(root, Key.LeftAlt);
  652. KeyDown(root, accessKey, KeyModifiers.Alt);
  653. KeyUp(root, accessKey, KeyModifiers.Alt);
  654. KeyUp(root, Key.LeftAlt);
  655. Assert.Equal(selectedTabIndex, tabControl.SelectedIndex);
  656. }
  657. static FuncControlTemplate<TestTopLevel> CreateTemplate()
  658. {
  659. return new FuncControlTemplate<TestTopLevel>((x, scope) =>
  660. new ContentPresenter
  661. {
  662. Name = "PART_ContentPresenter",
  663. [~ContentPresenter.ContentProperty] = new TemplateBinding(ContentControl.ContentProperty),
  664. [~ContentPresenter.ContentTemplateProperty] = new TemplateBinding(ContentControl.ContentTemplateProperty)
  665. }.RegisterInNameScope(scope));
  666. }
  667. static Mock<ITopLevelImpl> CreateMockTopLevelImpl(bool setupProperties = false)
  668. {
  669. var topLevel = new Mock<ITopLevelImpl>();
  670. if (setupProperties)
  671. topLevel.SetupAllProperties();
  672. topLevel.Setup(x => x.RenderScaling).Returns(1);
  673. topLevel.Setup(x => x.Compositor).Returns(RendererMocks.CreateDummyCompositor());
  674. return topLevel;
  675. }
  676. static void KeyDown(IInputElement target, Key key, KeyModifiers modifiers = KeyModifiers.None)
  677. {
  678. target.RaiseEvent(new KeyEventArgs
  679. {
  680. RoutedEvent = InputElement.KeyDownEvent,
  681. Key = key,
  682. KeyModifiers = modifiers,
  683. });
  684. }
  685. static void KeyUp(IInputElement target, Key key, KeyModifiers modifiers = KeyModifiers.None)
  686. {
  687. target.RaiseEvent(new KeyEventArgs
  688. {
  689. RoutedEvent = InputElement.KeyUpEvent,
  690. Key = key,
  691. KeyModifiers = modifiers,
  692. });
  693. }
  694. }
  695. private static IControlTemplate TabControlTemplate()
  696. {
  697. return new FuncControlTemplate<TabControl>((parent, scope) =>
  698. new StackPanel
  699. {
  700. Children =
  701. {
  702. new ItemsPresenter
  703. {
  704. Name = "PART_ItemsPresenter",
  705. }.RegisterInNameScope(scope),
  706. new ContentPresenter
  707. {
  708. Name = "PART_SelectedContentHost",
  709. [~ContentPresenter.ContentProperty] = new TemplateBinding(TabControl.SelectedContentProperty),
  710. [~ContentPresenter.ContentTemplateProperty] = new TemplateBinding(TabControl.SelectedContentTemplateProperty),
  711. }.RegisterInNameScope(scope)
  712. }
  713. });
  714. }
  715. private static IControlTemplate TabItemTemplate()
  716. {
  717. return new FuncControlTemplate<TabItem>((parent, scope) =>
  718. new ContentPresenter
  719. {
  720. Name = "PART_ContentPresenter",
  721. [~ContentPresenter.ContentProperty] = new TemplateBinding(TabItem.HeaderProperty),
  722. [~ContentPresenter.ContentTemplateProperty] = new TemplateBinding(TabItem.HeaderTemplateProperty),
  723. RecognizesAccessKey = true,
  724. }.RegisterInNameScope(scope));
  725. }
  726. private class TestTopLevel : TopLevel
  727. {
  728. private readonly ILayoutManager _layoutManager;
  729. public bool IsClosed { get; private set; }
  730. public TestTopLevel(ITopLevelImpl impl, ILayoutManager layoutManager = null)
  731. : base(impl)
  732. {
  733. _layoutManager = layoutManager ?? new LayoutManager(this);
  734. }
  735. private protected override ILayoutManager CreateLayoutManager() => _layoutManager;
  736. }
  737. private static void Prepare(TabControl target)
  738. {
  739. ApplyTemplate(target);
  740. target.Measure(Size.Infinity);
  741. target.Arrange(new Rect(target.DesiredSize));
  742. }
  743. private static void RaiseKeyEvent(Control target, Key key, KeyModifiers inputModifiers = 0)
  744. {
  745. target.RaiseEvent(new KeyEventArgs
  746. {
  747. RoutedEvent = InputElement.KeyDownEvent,
  748. KeyModifiers = inputModifiers,
  749. Key = key
  750. });
  751. }
  752. private static void ApplyTemplate(TabControl target)
  753. {
  754. target.ApplyTemplate();
  755. target.Presenter.ApplyTemplate();
  756. foreach (var tabItem in target.GetLogicalChildren().OfType<TabItem>())
  757. {
  758. tabItem.Template = TabItemTemplate();
  759. tabItem.ApplyTemplate();
  760. tabItem.Presenter.UpdateChild();
  761. }
  762. target.ContentPart.ApplyTemplate();
  763. }
  764. private class Item
  765. {
  766. public Item(string value)
  767. {
  768. Value = value;
  769. }
  770. public string Value { get; }
  771. }
  772. private class TestTabControl : TabControl
  773. {
  774. protected override Type StyleKeyOverride => typeof(TabControl);
  775. public new ISelectionModel Selection => base.Selection;
  776. }
  777. }
  778. }