InputElement_Focus.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. using Avalonia.Controls;
  2. using Avalonia.Input;
  3. using Avalonia.UnitTests;
  4. using Xunit;
  5. namespace Avalonia.Base.UnitTests.Input
  6. {
  7. public class InputElement_Focus
  8. {
  9. [Fact]
  10. public void Focus_Should_Set_FocusManager_Current()
  11. {
  12. Button target;
  13. using (UnitTestApplication.Start(TestServices.RealFocus))
  14. {
  15. var root = new TestRoot
  16. {
  17. Child = target = new Button()
  18. };
  19. target.Focus();
  20. Assert.Same(target, root.FocusManager.GetFocusedElement());
  21. }
  22. }
  23. [Fact]
  24. public void Invisible_Controls_Should_Not_Receive_Focus()
  25. {
  26. Button target;
  27. using (UnitTestApplication.Start(TestServices.RealFocus))
  28. {
  29. var root = new TestRoot
  30. {
  31. Child = target = new Button() { IsVisible = false}
  32. };
  33. Assert.Null(root.FocusManager.GetFocusedElement());
  34. target.Focus();
  35. Assert.False(target.IsFocused);
  36. Assert.False(target.IsKeyboardFocusWithin);
  37. Assert.Null(root.FocusManager.GetFocusedElement());
  38. }
  39. }
  40. [Fact]
  41. public void Effectively_Invisible_Controls_Should_Not_Receive_Focus()
  42. {
  43. var target = new Button();
  44. Panel container;
  45. using (UnitTestApplication.Start(TestServices.RealFocus))
  46. {
  47. var root = new TestRoot
  48. {
  49. Child = container = new Panel
  50. {
  51. IsVisible = false,
  52. Children = { target }
  53. }
  54. };
  55. Assert.Null(root.FocusManager.GetFocusedElement());
  56. target.Focus();
  57. Assert.False(target.IsFocused);
  58. Assert.False(target.IsKeyboardFocusWithin);
  59. Assert.Null(root.FocusManager.GetFocusedElement());
  60. }
  61. }
  62. [Fact]
  63. public void Trying_To_Focus_Invisible_Control_Should_Not_Change_Focus()
  64. {
  65. Button first;
  66. Button second;
  67. using (UnitTestApplication.Start(TestServices.RealFocus))
  68. {
  69. var root = new TestRoot
  70. {
  71. Child = new StackPanel
  72. {
  73. Children =
  74. {
  75. (first = new Button()),
  76. (second = new Button() { IsVisible = false}),
  77. }
  78. }
  79. };
  80. first.Focus();
  81. Assert.Same(first, root.FocusManager.GetFocusedElement());
  82. second.Focus();
  83. Assert.Same(first, root.FocusManager.GetFocusedElement());
  84. }
  85. }
  86. [Fact]
  87. public void Disabled_Controls_Should_Not_Receive_Focus()
  88. {
  89. Button target;
  90. using (UnitTestApplication.Start(TestServices.RealFocus))
  91. {
  92. var root = new TestRoot
  93. {
  94. Child = target = new Button() { IsEnabled = false }
  95. };
  96. Assert.Null(root.FocusManager.GetFocusedElement());
  97. target.Focus();
  98. Assert.False(target.IsFocused);
  99. Assert.False(target.IsKeyboardFocusWithin);
  100. Assert.Null(root.FocusManager.GetFocusedElement());
  101. }
  102. }
  103. [Fact]
  104. public void Effectively_Disabled_Controls_Should_Not_Receive_Focus()
  105. {
  106. var target = new Button();
  107. Panel container;
  108. using (UnitTestApplication.Start(TestServices.RealFocus))
  109. {
  110. var root = new TestRoot
  111. {
  112. Child = container = new Panel
  113. {
  114. IsEnabled = false,
  115. Children = { target }
  116. }
  117. };
  118. Assert.Null(root.FocusManager.GetFocusedElement());
  119. target.Focus();
  120. Assert.False(target.IsFocused);
  121. Assert.False(target.IsKeyboardFocusWithin);
  122. Assert.Null(root.FocusManager.GetFocusedElement());
  123. }
  124. }
  125. [Fact]
  126. public void Focus_Should_Not_Get_Restored_To_Enabled_Control()
  127. {
  128. using (UnitTestApplication.Start(TestServices.RealFocus))
  129. {
  130. var sp = new StackPanel();
  131. Button target = new Button();
  132. Button target1 = new Button();
  133. target.Click += (s, e) => target.IsEnabled = false;
  134. target1.Click += (s, e) => target.IsEnabled = true;
  135. sp.Children.Add(target);
  136. sp.Children.Add(target1);
  137. var root = new TestRoot
  138. {
  139. Child = sp
  140. };
  141. target.Focus();
  142. target.RaiseEvent(new AccessKeyEventArgs("b1", false));
  143. Assert.False(target.IsEnabled);
  144. Assert.False(target.IsFocused);
  145. target1.RaiseEvent(new AccessKeyEventArgs("b2", false));
  146. Assert.True(target.IsEnabled);
  147. Assert.False(target.IsFocused);
  148. }
  149. }
  150. [Fact]
  151. public void Focus_Should_Be_Cleared_When_Control_Is_Hidden()
  152. {
  153. Button target;
  154. using (UnitTestApplication.Start(TestServices.RealFocus))
  155. {
  156. var root = new TestRoot
  157. {
  158. Child = target = new Button()
  159. };
  160. target.Focus();
  161. target.IsVisible = false;
  162. Assert.Null(root.FocusManager.GetFocusedElement());
  163. }
  164. }
  165. [Fact]
  166. public void Focus_Should_Be_Cleared_When_Control_Is_Effectively_Hidden()
  167. {
  168. Border container;
  169. Button target;
  170. using (UnitTestApplication.Start(TestServices.RealFocus))
  171. {
  172. var root = new TestRoot
  173. {
  174. Child = container = new Border
  175. {
  176. Child = target = new Button(),
  177. }
  178. };
  179. target.Focus();
  180. container.IsVisible = false;
  181. Assert.Null(root.FocusManager.GetFocusedElement());
  182. }
  183. }
  184. [Fact]
  185. public void Focus_Should_Be_Cleared_When_Control_Is_Disabled()
  186. {
  187. Button target;
  188. using (UnitTestApplication.Start(TestServices.RealFocus))
  189. {
  190. var root = new TestRoot
  191. {
  192. Child = target = new Button()
  193. };
  194. target.Focus();
  195. target.IsEnabled = false;
  196. Assert.Null(root.FocusManager.GetFocusedElement());
  197. }
  198. }
  199. [Fact]
  200. public void Focus_Should_Be_Cleared_When_Control_Is_Effectively_Disabled()
  201. {
  202. Border container;
  203. Button target;
  204. using (UnitTestApplication.Start(TestServices.RealFocus))
  205. {
  206. var root = new TestRoot
  207. {
  208. Child = container = new Border
  209. {
  210. Child = target = new Button(),
  211. }
  212. };
  213. target.Focus();
  214. container.IsEnabled = false;
  215. Assert.Null(root.FocusManager.GetFocusedElement());
  216. }
  217. }
  218. [Fact]
  219. public void Focus_Should_Be_Cleared_When_Control_Is_Removed_From_VisualTree()
  220. {
  221. Button target;
  222. using (UnitTestApplication.Start(TestServices.RealFocus))
  223. {
  224. var root = new TestRoot
  225. {
  226. Child = target = new Button()
  227. };
  228. target.Focus();
  229. root.Child = null;
  230. Assert.Null(root.FocusManager.GetFocusedElement());
  231. }
  232. }
  233. [Fact]
  234. public void Focus_Pseudoclass_Should_Be_Applied_On_Focus()
  235. {
  236. using (UnitTestApplication.Start(TestServices.RealFocus))
  237. {
  238. var target1 = new Decorator { Focusable = true };
  239. var target2 = new Decorator { Focusable = true };
  240. var root = new TestRoot
  241. {
  242. Child = new StackPanel
  243. {
  244. Children =
  245. {
  246. target1,
  247. target2
  248. }
  249. }
  250. };
  251. target1.ApplyTemplate();
  252. target2.ApplyTemplate();
  253. target1.Focus();
  254. Assert.True(target1.IsFocused);
  255. Assert.True(target1.Classes.Contains(":focus"));
  256. Assert.False(target2.IsFocused);
  257. Assert.False(target2.Classes.Contains(":focus"));
  258. target2.Focus(NavigationMethod.Tab);
  259. Assert.False(target1.IsFocused);
  260. Assert.False(target1.Classes.Contains(":focus"));
  261. Assert.True(target2.IsFocused);
  262. Assert.True(target2.Classes.Contains(":focus"));
  263. }
  264. }
  265. [Fact]
  266. public void Control_FocusVsisible_Pseudoclass_Should_Be_Applied_On_Tab_And_DirectionalFocus()
  267. {
  268. using (UnitTestApplication.Start(TestServices.RealFocus))
  269. {
  270. var target1 = new Decorator { Focusable = true };
  271. var target2 = new Decorator { Focusable = true };
  272. var root = new TestRoot
  273. {
  274. Child = new StackPanel
  275. {
  276. Children =
  277. {
  278. target1,
  279. target2
  280. }
  281. }
  282. };
  283. target1.ApplyTemplate();
  284. target2.ApplyTemplate();
  285. target1.Focus();
  286. Assert.True(target1.IsFocused);
  287. Assert.False(target1.Classes.Contains(":focus-visible"));
  288. Assert.False(target2.IsFocused);
  289. Assert.False(target2.Classes.Contains(":focus-visible"));
  290. target2.Focus(NavigationMethod.Tab);
  291. Assert.False(target1.IsFocused);
  292. Assert.False(target1.Classes.Contains(":focus-visible"));
  293. Assert.True(target2.IsFocused);
  294. Assert.True(target2.Classes.Contains(":focus-visible"));
  295. target1.Focus(NavigationMethod.Directional);
  296. Assert.True(target1.IsFocused);
  297. Assert.True(target1.Classes.Contains(":focus-visible"));
  298. Assert.False(target2.IsFocused);
  299. Assert.False(target2.Classes.Contains(":focus-visible"));
  300. }
  301. }
  302. [Fact]
  303. public void Control_FocusWithin_PseudoClass_Should_Be_Applied()
  304. {
  305. using (UnitTestApplication.Start(TestServices.RealFocus))
  306. {
  307. var target1 = new Decorator { Focusable = true };
  308. var target2 = new Decorator { Focusable = true };
  309. var root = new TestRoot
  310. {
  311. Child = new StackPanel
  312. {
  313. Children =
  314. {
  315. target1,
  316. target2
  317. }
  318. }
  319. };
  320. target1.ApplyTemplate();
  321. target2.ApplyTemplate();
  322. target1.Focus();
  323. Assert.True(target1.IsFocused);
  324. Assert.True(target1.Classes.Contains(":focus-within"));
  325. Assert.True(target1.IsKeyboardFocusWithin);
  326. Assert.True(root.Child.Classes.Contains(":focus-within"));
  327. Assert.True(root.Child.IsKeyboardFocusWithin);
  328. Assert.True(root.Classes.Contains(":focus-within"));
  329. Assert.True(root.IsKeyboardFocusWithin);
  330. }
  331. }
  332. [Fact]
  333. public void Control_FocusWithin_PseudoClass_Should_Be_Applied_and_Removed()
  334. {
  335. using (UnitTestApplication.Start(TestServices.RealFocus))
  336. {
  337. var target1 = new Decorator { Focusable = true };
  338. var target2 = new Decorator { Focusable = true };
  339. var panel1 = new Panel { Children = { target1 } };
  340. var panel2 = new Panel { Children = { target2 } };
  341. var root = new TestRoot
  342. {
  343. Child = new StackPanel
  344. {
  345. Children =
  346. {
  347. panel1,
  348. panel2
  349. }
  350. }
  351. };
  352. target1.ApplyTemplate();
  353. target2.ApplyTemplate();
  354. target1.Focus();
  355. Assert.True(target1.IsFocused);
  356. Assert.True(target1.Classes.Contains(":focus-within"));
  357. Assert.True(target1.IsKeyboardFocusWithin);
  358. Assert.True(panel1.Classes.Contains(":focus-within"));
  359. Assert.True(panel1.IsKeyboardFocusWithin);
  360. Assert.True(root.Child.Classes.Contains(":focus-within"));
  361. Assert.True(root.Child.IsKeyboardFocusWithin);
  362. Assert.True(root.Classes.Contains(":focus-within"));
  363. Assert.True(root.IsKeyboardFocusWithin);
  364. target2.Focus();
  365. Assert.False(target1.IsFocused);
  366. Assert.False(target1.Classes.Contains(":focus-within"));
  367. Assert.False(target1.IsKeyboardFocusWithin);
  368. Assert.False(panel1.Classes.Contains(":focus-within"));
  369. Assert.False(panel1.IsKeyboardFocusWithin);
  370. Assert.True(root.Child.Classes.Contains(":focus-within"));
  371. Assert.True(root.Child.IsKeyboardFocusWithin);
  372. Assert.True(root.Classes.Contains(":focus-within"));
  373. Assert.True(root.IsKeyboardFocusWithin);
  374. Assert.True(target2.IsFocused);
  375. Assert.True(target2.Classes.Contains(":focus-within"));
  376. Assert.True(target2.IsKeyboardFocusWithin);
  377. Assert.True(panel2.Classes.Contains(":focus-within"));
  378. Assert.True(panel2.IsKeyboardFocusWithin);
  379. }
  380. }
  381. [Fact]
  382. public void Control_FocusWithin_Pseudoclass_Should_Be_Removed_When_Removed_From_Tree()
  383. {
  384. using (UnitTestApplication.Start(TestServices.RealFocus))
  385. {
  386. var target1 = new Decorator { Focusable = true };
  387. var target2 = new Decorator { Focusable = true };
  388. var root = new TestRoot
  389. {
  390. Child = new StackPanel
  391. {
  392. Children =
  393. {
  394. target1,
  395. target2
  396. }
  397. }
  398. };
  399. target1.ApplyTemplate();
  400. target2.ApplyTemplate();
  401. target1.Focus();
  402. Assert.True(target1.IsFocused);
  403. Assert.True(target1.Classes.Contains(":focus-within"));
  404. Assert.True(target1.IsKeyboardFocusWithin);
  405. Assert.True(root.Child.Classes.Contains(":focus-within"));
  406. Assert.True(root.Child.IsKeyboardFocusWithin);
  407. Assert.True(root.Classes.Contains(":focus-within"));
  408. Assert.True(root.IsKeyboardFocusWithin);
  409. Assert.Equal(KeyboardDevice.Instance.FocusedElement, target1);
  410. root.Child = null;
  411. Assert.Null(KeyboardDevice.Instance.FocusedElement);
  412. Assert.False(target1.IsFocused);
  413. Assert.False(target1.Classes.Contains(":focus-within"));
  414. Assert.False(target1.IsKeyboardFocusWithin);
  415. Assert.False(root.Classes.Contains(":focus-within"));
  416. Assert.False(root.IsKeyboardFocusWithin);
  417. }
  418. }
  419. [Fact]
  420. public void Control_FocusWithin_Pseudoclass_Should_Be_Removed_Focus_Moves_To_Different_Root()
  421. {
  422. using (UnitTestApplication.Start(TestServices.RealFocus))
  423. {
  424. var target1 = new Decorator { Focusable = true };
  425. var target2 = new Decorator { Focusable = true };
  426. var root1 = new TestRoot
  427. {
  428. Child = new StackPanel
  429. {
  430. Children =
  431. {
  432. target1,
  433. }
  434. }
  435. };
  436. var root2 = new TestRoot
  437. {
  438. Child = new StackPanel
  439. {
  440. Children =
  441. {
  442. target2,
  443. }
  444. }
  445. };
  446. target1.ApplyTemplate();
  447. target2.ApplyTemplate();
  448. target1.Focus();
  449. Assert.True(target1.IsFocused);
  450. Assert.True(target1.Classes.Contains(":focus-within"));
  451. Assert.True(target1.IsKeyboardFocusWithin);
  452. Assert.True(root1.Child.Classes.Contains(":focus-within"));
  453. Assert.True(root1.Child.IsKeyboardFocusWithin);
  454. Assert.True(root1.Classes.Contains(":focus-within"));
  455. Assert.True(root1.IsKeyboardFocusWithin);
  456. Assert.Equal(KeyboardDevice.Instance.FocusedElement, target1);
  457. target2.Focus();
  458. Assert.False(target1.IsFocused);
  459. Assert.False(target1.Classes.Contains(":focus-within"));
  460. Assert.False(target1.IsKeyboardFocusWithin);
  461. Assert.False(root1.Child.Classes.Contains(":focus-within"));
  462. Assert.False(root1.Child.IsKeyboardFocusWithin);
  463. Assert.False(root1.Classes.Contains(":focus-within"));
  464. Assert.False(root1.IsKeyboardFocusWithin);
  465. Assert.True(target2.IsFocused);
  466. Assert.True(target2.Classes.Contains(":focus-within"));
  467. Assert.True(target2.IsKeyboardFocusWithin);
  468. Assert.True(root2.Child.Classes.Contains(":focus-within"));
  469. Assert.True(root2.Child.IsKeyboardFocusWithin);
  470. Assert.True(root2.Classes.Contains(":focus-within"));
  471. Assert.True(root2.IsKeyboardFocusWithin);
  472. }
  473. }
  474. [Fact]
  475. public void Can_Clear_Focus()
  476. {
  477. Button target;
  478. using (UnitTestApplication.Start(TestServices.RealFocus))
  479. {
  480. var root = new TestRoot
  481. {
  482. Child = target = new Button()
  483. };
  484. target.Focus();
  485. root.FocusManager.ClearFocus();
  486. Assert.Null(root.FocusManager.GetFocusedElement());
  487. }
  488. }
  489. [Fact]
  490. public void Removing_Focused_Element_Inside_Focus_Scope_Activates_Root_Focus_Scope()
  491. {
  492. // Issue #13325
  493. using var app = UnitTestApplication.Start(TestServices.RealFocus);
  494. Button innerButton, intermediateButton, outerButton;
  495. TestFocusScope innerScope;
  496. var root = new TestRoot
  497. {
  498. Child = new StackPanel
  499. {
  500. Children =
  501. {
  502. // Intermediate focus scope to make sure that the root focus scope gets
  503. // activated, not this one.
  504. new TestFocusScope
  505. {
  506. Children =
  507. {
  508. (innerScope = new TestFocusScope
  509. {
  510. Children =
  511. {
  512. (innerButton = new Button()),
  513. }
  514. }),
  515. (intermediateButton = new Button()),
  516. }
  517. },
  518. (outerButton = new Button()),
  519. }
  520. }
  521. };
  522. // Focus a control in each scope, ending with the innermost one.
  523. outerButton.Focus();
  524. intermediateButton.Focus();
  525. innerButton.Focus();
  526. // Remove the focused control from the tree.
  527. ((Panel)innerButton.Parent).Children.Remove(innerButton);
  528. var focusManager = Assert.IsType<FocusManager>(root.FocusManager);
  529. Assert.Same(outerButton, focusManager.GetFocusedElement());
  530. Assert.Null(focusManager.GetFocusedElement(innerScope));
  531. }
  532. [Fact]
  533. public void Removing_Focus_Scope_Activates_Root_Focus_Scope()
  534. {
  535. using var app = UnitTestApplication.Start(TestServices.RealFocus);
  536. Button innerButton, outerButton;
  537. TestFocusScope innerScope;
  538. var root = new TestRoot
  539. {
  540. Child = new StackPanel
  541. {
  542. Children =
  543. {
  544. (innerScope = new TestFocusScope
  545. {
  546. Children =
  547. {
  548. (innerButton = new Button()),
  549. }
  550. }),
  551. (outerButton = new Button()),
  552. }
  553. }
  554. };
  555. // Focus a control in the top-level and inner focus scopes.
  556. outerButton.Focus();
  557. innerButton.Focus();
  558. // Remove the inner focus scope.
  559. ((Panel)innerScope.Parent).Children.Remove(innerScope);
  560. var focusManager = Assert.IsType<FocusManager>(root.FocusManager);
  561. Assert.Same(outerButton, focusManager.GetFocusedElement());
  562. }
  563. [Fact]
  564. public void Switching_Focus_Scope_Changes_Focus()
  565. {
  566. using var app = UnitTestApplication.Start(TestServices.RealFocus);
  567. Button innerButton, outerButton;
  568. TestFocusScope innerScope;
  569. var root = new TestRoot
  570. {
  571. Child = new StackPanel
  572. {
  573. Children =
  574. {
  575. (innerScope = new TestFocusScope
  576. {
  577. Children =
  578. {
  579. (innerButton = new Button()),
  580. }
  581. }),
  582. (outerButton = new Button()),
  583. }
  584. }
  585. };
  586. // Focus a control in the top-level and inner focus scopes.
  587. outerButton.Focus();
  588. innerButton.Focus();
  589. var focusManager = Assert.IsType<FocusManager>(root.FocusManager);
  590. Assert.Same(innerButton, focusManager.GetFocusedElement());
  591. focusManager.SetFocusScope(root);
  592. Assert.Same(outerButton, focusManager.GetFocusedElement());
  593. focusManager.SetFocusScope(innerScope);
  594. Assert.Same(innerButton, focusManager.GetFocusedElement());
  595. }
  596. [Fact]
  597. public void Can_Get_First_Focusable_Element()
  598. {
  599. using (UnitTestApplication.Start(TestServices.RealFocus))
  600. {
  601. var target1 = new Button { Focusable = true, Content = "1" };
  602. var target2 = new Button { Focusable = true, Content = "2" };
  603. var target3 = new Button { Focusable = true, Content = "3" };
  604. var target4 = new Button { Focusable = true, Content = "4" };
  605. var container = new StackPanel
  606. {
  607. Children =
  608. {
  609. target1,
  610. target2,
  611. target3,
  612. target4
  613. }
  614. };
  615. var root = new TestRoot
  616. {
  617. Child = container
  618. };
  619. var firstFocusable = FocusManager.FindFirstFocusableElement(container);
  620. Assert.Equal(target1, firstFocusable);
  621. firstFocusable = (root.FocusManager as FocusManager)?.FindFirstFocusableElement();
  622. Assert.Equal(target1, firstFocusable);
  623. }
  624. }
  625. [Fact]
  626. public void Can_Get_Last_Focusable_Element()
  627. {
  628. using (UnitTestApplication.Start(TestServices.RealFocus))
  629. {
  630. var target1 = new Button { Focusable = true, Content = "1" };
  631. var target2 = new Button { Focusable = true, Content = "2" };
  632. var target3 = new Button { Focusable = true, Content = "3" };
  633. var target4 = new Button { Focusable = true, Content = "4" };
  634. var container = new StackPanel
  635. {
  636. Children =
  637. {
  638. target1,
  639. target2,
  640. target3,
  641. target4
  642. }
  643. };
  644. var root = new TestRoot
  645. {
  646. Child = container
  647. };
  648. var lastFocusable = FocusManager.FindLastFocusableElement(container);
  649. Assert.Equal(target4, lastFocusable);
  650. lastFocusable = (root.FocusManager as FocusManager)?.FindLastFocusableElement();
  651. Assert.Equal(target4, lastFocusable);
  652. }
  653. }
  654. [Fact]
  655. public void Can_Get_Next_Element()
  656. {
  657. using (UnitTestApplication.Start(TestServices.RealFocus))
  658. {
  659. var target1 = new Button { Focusable = true, Content = "1" };
  660. var target2 = new Button { Focusable = true, Content = "2" };
  661. var target3 = new Button { Focusable = true, Content = "3" };
  662. var target4 = new Button { Focusable = true, Content = "4" };
  663. var container = new StackPanel
  664. {
  665. Children =
  666. {
  667. target1,
  668. target2,
  669. target3,
  670. target4
  671. }
  672. };
  673. var root = new TestRoot
  674. {
  675. Child = container
  676. };
  677. var focusManager = FocusManager.GetFocusManager(container);
  678. target1.Focus();
  679. var next = focusManager.FindNextElement(NavigationDirection.Next);
  680. Assert.Equal(next, target2);
  681. }
  682. }
  683. [Fact]
  684. public void Can_Get_Next_Element_With_Options()
  685. {
  686. using (UnitTestApplication.Start(TestServices.RealFocus))
  687. {
  688. var target1 = new Button { Focusable = true, Content = "1" };
  689. var target2 = new Button { Focusable = true, Content = "2" };
  690. var target3 = new Button { Focusable = true, Content = "3" };
  691. var target4 = new Button { Focusable = true, Content = "4" };
  692. var target5 = new Button { Focusable = true, Content = "5" };
  693. var seachStack = new StackPanel()
  694. {
  695. Children =
  696. {
  697. target3,
  698. target4
  699. }
  700. };
  701. var container = new StackPanel
  702. {
  703. Orientation = Avalonia.Layout.Orientation.Horizontal,
  704. Children =
  705. {
  706. target1,
  707. target2,
  708. seachStack,
  709. target5
  710. }
  711. };
  712. var root = new TestRoot
  713. {
  714. Child = container
  715. };
  716. root.InvalidateMeasure();
  717. root.ExecuteInitialLayoutPass();
  718. var focusManager = FocusManager.GetFocusManager(container);
  719. target1.Focus();
  720. var options = new FindNextElementOptions()
  721. {
  722. SearchRoot = seachStack
  723. };
  724. // Search root is right of the current focus, should return the first focusable element in the search root
  725. var next = focusManager.FindNextElement(NavigationDirection.Right, options);
  726. Assert.Equal(next, target3);
  727. target5.Focus();
  728. // Search root is right of the current focus, should return the first focusable element in the search root
  729. next = focusManager.FindNextElement(NavigationDirection.Left, options);
  730. Assert.Equal(next, target3);
  731. // Search root isn't to the right of the current focus, should return null
  732. next = focusManager.FindNextElement(NavigationDirection.Right, options);
  733. Assert.Null(next);
  734. }
  735. }
  736. [Fact]
  737. public void Focus_Should_Move_According_To_Direction()
  738. {
  739. using (UnitTestApplication.Start(TestServices.RealFocus))
  740. {
  741. var target1 = new Button { Focusable = true, Content = "1" };
  742. var target2 = new Button { Focusable = true, Content = "2" };
  743. var target3 = new Button { Focusable = true, Content = "3" };
  744. var target4 = new Button { Focusable = true, Content = "4" };
  745. var container = new StackPanel
  746. {
  747. Children =
  748. {
  749. target1,
  750. target2,
  751. target3,
  752. target4
  753. }
  754. };
  755. var root = new TestRoot
  756. {
  757. Child = container
  758. };
  759. var focusManager = FocusManager.GetFocusManager(container);
  760. var hasMoved = focusManager.TryMoveFocus(NavigationDirection.Next);
  761. Assert.True(target1.IsFocused);
  762. Assert.True(hasMoved);
  763. hasMoved = focusManager.TryMoveFocus(NavigationDirection.Previous);
  764. Assert.True(target4.IsFocused);
  765. Assert.True(hasMoved);
  766. }
  767. }
  768. [Fact]
  769. public void Focus_Should_Move_According_To_XY_Direction()
  770. {
  771. using (UnitTestApplication.Start(TestServices.RealFocus))
  772. {
  773. var target1 = new Button { Focusable = true, Content = "1" };
  774. var target2 = new Button { Focusable = true, Content = "2" };
  775. var target3 = new Button { Focusable = true, Content = "3" };
  776. var target4 = new Button { Focusable = true, Content = "4" };
  777. var center = new Button
  778. {
  779. [XYFocus.LeftProperty] = target1,
  780. [XYFocus.RightProperty] = target2,
  781. [XYFocus.UpProperty] = target3,
  782. [XYFocus.DownProperty] = target4,
  783. };
  784. var container = new Canvas
  785. {
  786. Children =
  787. {
  788. target1,
  789. target2,
  790. target3,
  791. target4,
  792. center
  793. }
  794. };
  795. var root = new TestRoot
  796. {
  797. Child = container
  798. };
  799. var focusManager = FocusManager.GetFocusManager(container);
  800. center.Focus();
  801. var options = new FindNextElementOptions()
  802. {
  803. SearchRoot = container
  804. };
  805. var hasMoved = focusManager.TryMoveFocus(NavigationDirection.Up, options);
  806. Assert.True(target3.IsFocused);
  807. Assert.True(hasMoved);
  808. }
  809. }
  810. private class TestFocusScope : Panel, IFocusScope
  811. {
  812. }
  813. }
  814. }