KeyboardNavigationTests_Tab.cs 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Controls;
  4. using Avalonia.Input;
  5. using Avalonia.UnitTests;
  6. using Xunit;
  7. namespace Avalonia.Base.UnitTests.Input
  8. {
  9. public class KeyboardNavigationTests_Tab
  10. {
  11. [Fact]
  12. public void Next_Continue_Returns_Next_Control_In_Container()
  13. {
  14. Button current;
  15. Button next;
  16. var top = new StackPanel
  17. {
  18. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  19. Children =
  20. {
  21. new StackPanel
  22. {
  23. Children =
  24. {
  25. new Button { Name = "Button1" },
  26. (current = new Button { Name = "Button2" }),
  27. (next = new Button { Name = "Button3" }),
  28. }
  29. },
  30. new StackPanel
  31. {
  32. Children =
  33. {
  34. new Button { Name = "Button4" },
  35. new Button { Name = "Button5" },
  36. new Button { Name = "Button6" },
  37. }
  38. },
  39. }
  40. };
  41. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  42. Assert.Equal(next, result);
  43. }
  44. [Fact]
  45. public void Next_Continue_Returns_First_Control_In_Next_Sibling_Container()
  46. {
  47. Button current;
  48. Button next;
  49. var top = new StackPanel
  50. {
  51. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  52. Children =
  53. {
  54. new StackPanel
  55. {
  56. Children =
  57. {
  58. new Button { Name = "Button1" },
  59. new Button { Name = "Button2" },
  60. (current = new Button { Name = "Button3" }),
  61. }
  62. },
  63. new StackPanel
  64. {
  65. Children =
  66. {
  67. (next = new Button { Name = "Button4" }),
  68. new Button { Name = "Button5" },
  69. new Button { Name = "Button6" },
  70. }
  71. },
  72. }
  73. };
  74. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  75. Assert.Equal(next, result);
  76. }
  77. [Fact]
  78. public void Next_Skips_Unfocusable_Siblings()
  79. {
  80. Button current;
  81. Button next;
  82. var top = new StackPanel
  83. {
  84. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  85. Children =
  86. {
  87. new StackPanel
  88. {
  89. Children =
  90. {
  91. new Button { Name = "Button1" },
  92. new Button { Name = "Button2" },
  93. new StackPanel
  94. {
  95. Children =
  96. {
  97. (current = new Button { Name = "Button3" }),
  98. }
  99. },
  100. new TextBlock { Name = "TextBlock" },
  101. (next = new Button { Name = "Button4" }),
  102. }
  103. },
  104. new Button { Name = "Button5" },
  105. new Button { Name = "Button6" },
  106. }
  107. };
  108. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  109. Assert.Equal(next, result);
  110. }
  111. [Fact]
  112. public void Next_Continue_Doesnt_Enter_Panel_With_TabNavigation_None()
  113. {
  114. Button current;
  115. Button next;
  116. var top = new StackPanel
  117. {
  118. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  119. Children =
  120. {
  121. new StackPanel
  122. {
  123. Children =
  124. {
  125. (next = new Button { Name = "Button1" }),
  126. new Button { Name = "Button2" },
  127. (current = new Button { Name = "Button3" }),
  128. }
  129. },
  130. new StackPanel
  131. {
  132. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.None,
  133. Children =
  134. {
  135. new StackPanel
  136. {
  137. Children =
  138. {
  139. new Button { Name = "Button4" },
  140. new Button { Name = "Button5" },
  141. new Button { Name = "Button6" },
  142. }
  143. },
  144. }
  145. }
  146. }
  147. };
  148. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  149. Assert.Equal(next, result);
  150. }
  151. [Fact]
  152. public void Next_Continue_Returns_Next_Sibling()
  153. {
  154. Button current;
  155. Button next;
  156. var top = new StackPanel
  157. {
  158. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  159. Children =
  160. {
  161. new StackPanel
  162. {
  163. Children =
  164. {
  165. new Button { Name = "Button1" },
  166. new Button { Name = "Button2" },
  167. (current = new Button { Name = "Button3" }),
  168. }
  169. },
  170. (next = new Button { Name = "Button4" }),
  171. }
  172. };
  173. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  174. Assert.Equal(next, result);
  175. }
  176. [Fact]
  177. public void Next_Skips_Non_TabStop_Siblings()
  178. {
  179. Button current;
  180. Button next;
  181. var top = new StackPanel
  182. {
  183. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  184. Children =
  185. {
  186. new StackPanel
  187. {
  188. Children =
  189. {
  190. new Button { Name = "Button1" },
  191. new Button { Name = "Button2" },
  192. (current = new Button { Name = "Button3" }),
  193. new Button { Name="Button4", [KeyboardNavigation.IsTabStopProperty] = false }
  194. }
  195. },
  196. (next = new Button { Name = "Button5" }),
  197. }
  198. };
  199. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  200. Assert.Equal(next, result);
  201. }
  202. [Fact]
  203. public void Next_Continue_Returns_First_Control_In_Next_Uncle_Container()
  204. {
  205. Button current;
  206. Button next;
  207. var top = new StackPanel
  208. {
  209. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  210. Children =
  211. {
  212. new StackPanel
  213. {
  214. Children =
  215. {
  216. new StackPanel
  217. {
  218. Children =
  219. {
  220. new Button { Name = "Button1" },
  221. new Button { Name = "Button2" },
  222. (current = new Button { Name = "Button3" }),
  223. }
  224. },
  225. },
  226. },
  227. new StackPanel
  228. {
  229. Children =
  230. {
  231. (next = new Button { Name = "Button4" }),
  232. new Button { Name = "Button5" },
  233. new Button { Name = "Button6" },
  234. }
  235. },
  236. }
  237. };
  238. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  239. Assert.Equal(next, result);
  240. }
  241. [Fact]
  242. public void Next_Continue_Returns_Child_Of_Top_Level()
  243. {
  244. Button next;
  245. var top = new StackPanel
  246. {
  247. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  248. Children =
  249. {
  250. (next = new Button { Name = "Button1" }),
  251. }
  252. };
  253. var result = KeyboardNavigationHandler.GetNext(top, NavigationDirection.Next);
  254. Assert.Equal(next, result);
  255. }
  256. [Fact]
  257. public void Next_Continue_Wraps()
  258. {
  259. Button current;
  260. Button next;
  261. var top = new StackPanel
  262. {
  263. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  264. Children =
  265. {
  266. new StackPanel
  267. {
  268. Children =
  269. {
  270. new StackPanel
  271. {
  272. Children =
  273. {
  274. (next = new Button { Name = "Button1" }),
  275. new Button { Name = "Button2" },
  276. new Button { Name = "Button3" },
  277. }
  278. },
  279. },
  280. },
  281. new StackPanel
  282. {
  283. Children =
  284. {
  285. new Button { Name = "Button4" },
  286. new Button { Name = "Button5" },
  287. (current = new Button { Name = "Button6" }),
  288. }
  289. },
  290. }
  291. };
  292. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  293. Assert.Equal(next, result);
  294. }
  295. [Fact]
  296. public void Next_Cycle_Returns_Next_Control_In_Container()
  297. {
  298. Button current;
  299. Button next;
  300. var top = new StackPanel
  301. {
  302. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  303. Children =
  304. {
  305. new StackPanel
  306. {
  307. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  308. Children =
  309. {
  310. new Button { Name = "Button1" },
  311. (current = new Button { Name = "Button2" }),
  312. (next = new Button { Name = "Button3" }),
  313. }
  314. },
  315. new StackPanel
  316. {
  317. Children =
  318. {
  319. new Button { Name = "Button4" },
  320. new Button { Name = "Button5" },
  321. new Button { Name = "Button6" },
  322. }
  323. },
  324. }
  325. };
  326. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  327. Assert.Equal(next, result);
  328. }
  329. [Fact]
  330. public void Next_Cycle_Wraps_To_First()
  331. {
  332. Button current;
  333. Button next;
  334. var top = new StackPanel
  335. {
  336. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  337. Children =
  338. {
  339. new StackPanel
  340. {
  341. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  342. Children =
  343. {
  344. (next = new Button { Name = "Button1" }),
  345. new Button { Name = "Button2" },
  346. (current = new Button { Name = "Button3" }),
  347. }
  348. },
  349. new StackPanel
  350. {
  351. Children =
  352. {
  353. new Button { Name = "Button4" },
  354. new Button { Name = "Button5" },
  355. new Button { Name = "Button6" },
  356. }
  357. },
  358. }
  359. };
  360. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  361. Assert.Equal(next, result);
  362. }
  363. [Fact]
  364. public void Next_Contained_Returns_Next_Control_In_Container()
  365. {
  366. Button current;
  367. Button next;
  368. var top = new StackPanel
  369. {
  370. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  371. Children =
  372. {
  373. new StackPanel
  374. {
  375. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Contained,
  376. Children =
  377. {
  378. new Button { Name = "Button1" },
  379. (current = new Button { Name = "Button2" }),
  380. (next = new Button { Name = "Button3" }),
  381. }
  382. },
  383. new StackPanel
  384. {
  385. Children =
  386. {
  387. new Button { Name = "Button4" },
  388. new Button { Name = "Button5" },
  389. new Button { Name = "Button6" },
  390. }
  391. },
  392. }
  393. };
  394. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  395. Assert.Equal(next, result);
  396. }
  397. [Fact]
  398. public void Next_Contained_Stops_At_End()
  399. {
  400. Button current;
  401. var top = new StackPanel
  402. {
  403. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  404. Children =
  405. {
  406. new StackPanel
  407. {
  408. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Contained,
  409. Children =
  410. {
  411. new Button { Name = "Button1" },
  412. new Button { Name = "Button2" },
  413. (current = new Button { Name = "Button3" }),
  414. }
  415. },
  416. new StackPanel
  417. {
  418. Children =
  419. {
  420. new Button { Name = "Button4" },
  421. new Button { Name = "Button5" },
  422. new Button { Name = "Button6" },
  423. }
  424. },
  425. }
  426. };
  427. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  428. Assert.Null(result);
  429. }
  430. [Fact]
  431. public void Next_Once_Moves_To_Next_Container()
  432. {
  433. Button current;
  434. Button next;
  435. var top = new StackPanel
  436. {
  437. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  438. Children =
  439. {
  440. new StackPanel
  441. {
  442. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Once,
  443. Children =
  444. {
  445. new Button { Name = "Button1" },
  446. (current = new Button { Name = "Button2" }),
  447. new Button { Name = "Button3" },
  448. }
  449. },
  450. new StackPanel
  451. {
  452. Children =
  453. {
  454. (next = new Button { Name = "Button4" }),
  455. new Button { Name = "Button5" },
  456. new Button { Name = "Button6" },
  457. }
  458. },
  459. }
  460. };
  461. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  462. Assert.Equal(next, result);
  463. }
  464. [Fact]
  465. public void Next_Once_Moves_To_Active_Element()
  466. {
  467. StackPanel container;
  468. Button current;
  469. Button next;
  470. var top = new StackPanel
  471. {
  472. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  473. Children =
  474. {
  475. (container = new StackPanel
  476. {
  477. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Once,
  478. Children =
  479. {
  480. new Button { Name = "Button1" },
  481. (next = new Button { Name = "Button2" }),
  482. new Button { Name = "Button3" },
  483. }
  484. }),
  485. new StackPanel
  486. {
  487. Children =
  488. {
  489. new Button { Name = "Button4" },
  490. new Button { Name = "Button5" },
  491. (current = new Button { Name = "Button6" }),
  492. }
  493. },
  494. }
  495. };
  496. KeyboardNavigation.SetTabOnceActiveElement(container, next);
  497. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  498. Assert.Equal(next, result);
  499. }
  500. [Fact]
  501. public void Next_None_Moves_To_Next_Container()
  502. {
  503. Button current;
  504. Button next;
  505. var top = new StackPanel
  506. {
  507. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  508. Children =
  509. {
  510. new StackPanel
  511. {
  512. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.None,
  513. Children =
  514. {
  515. new Button { Name = "Button1" },
  516. (current = new Button { Name = "Button2" }),
  517. new Button { Name = "Button3" },
  518. }
  519. },
  520. new StackPanel
  521. {
  522. Children =
  523. {
  524. (next = new Button { Name = "Button4" }),
  525. new Button { Name = "Button5" },
  526. new Button { Name = "Button6" },
  527. }
  528. },
  529. }
  530. };
  531. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  532. Assert.Equal(next, result);
  533. }
  534. [Fact]
  535. public void Next_None_Skips_Container()
  536. {
  537. StackPanel container;
  538. Button current;
  539. Button next;
  540. var top = new StackPanel
  541. {
  542. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  543. Children =
  544. {
  545. (container = new StackPanel
  546. {
  547. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.None,
  548. Children =
  549. {
  550. new Button { Name = "Button1" },
  551. new Button { Name = "Button2" },
  552. new Button { Name = "Button3" },
  553. }
  554. }),
  555. new StackPanel
  556. {
  557. Children =
  558. {
  559. (next = new Button { Name = "Button4" }),
  560. new Button { Name = "Button5" },
  561. (current = new Button { Name = "Button6" }),
  562. }
  563. },
  564. }
  565. };
  566. KeyboardNavigation.SetTabOnceActiveElement(container, next);
  567. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  568. Assert.Equal(next, result);
  569. }
  570. [Fact]
  571. public void Previous_Continue_Returns_Previous_Control_In_Container()
  572. {
  573. Button current;
  574. Button next;
  575. var top = new StackPanel
  576. {
  577. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  578. Children =
  579. {
  580. new StackPanel
  581. {
  582. Children =
  583. {
  584. new Button { Name = "Button1" },
  585. (next = new Button { Name = "Button2" }),
  586. (current = new Button { Name = "Button3" }),
  587. }
  588. },
  589. new StackPanel
  590. {
  591. Children =
  592. {
  593. new Button { Name = "Button4" },
  594. new Button { Name = "Button5" },
  595. new Button { Name = "Button6" },
  596. }
  597. },
  598. }
  599. };
  600. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Previous);
  601. Assert.Equal(next, result);
  602. }
  603. [Fact]
  604. public void Previous_Continue_Returns_Last_Control_In_Previous_Sibling_Container()
  605. {
  606. Button current;
  607. Button next;
  608. var top = new StackPanel
  609. {
  610. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  611. Children =
  612. {
  613. new StackPanel
  614. {
  615. Children =
  616. {
  617. new Button { Name = "Button1" },
  618. new Button { Name = "Button2" },
  619. (next = new Button { Name = "Button3" }),
  620. }
  621. },
  622. new StackPanel
  623. {
  624. Children =
  625. {
  626. (current = new Button { Name = "Button4" }),
  627. new Button { Name = "Button5" },
  628. new Button { Name = "Button6" },
  629. }
  630. },
  631. }
  632. };
  633. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Previous);
  634. Assert.Equal(next, result);
  635. }
  636. [Fact]
  637. public void Previous_Continue_Returns_Last_Child_Of_Sibling()
  638. {
  639. Button current;
  640. Button next;
  641. var top = new StackPanel
  642. {
  643. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  644. Children =
  645. {
  646. new StackPanel
  647. {
  648. Children =
  649. {
  650. new Button { Name = "Button1" },
  651. new Button { Name = "Button2" },
  652. (next = new Button { Name = "Button3" }),
  653. }
  654. },
  655. (current = new Button { Name = "Button4" }),
  656. }
  657. };
  658. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Previous);
  659. Assert.Equal(next, result);
  660. }
  661. [Fact]
  662. public void Previous_Continue_Returns_Last_Control_In_Previous_Nephew_Container()
  663. {
  664. Button current;
  665. Button next;
  666. var top = new StackPanel
  667. {
  668. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  669. Children =
  670. {
  671. new StackPanel
  672. {
  673. Children =
  674. {
  675. new StackPanel
  676. {
  677. Children =
  678. {
  679. new Button { Name = "Button1" },
  680. new Button { Name = "Button2" },
  681. (next = new Button { Name = "Button3" }),
  682. }
  683. },
  684. },
  685. },
  686. new StackPanel
  687. {
  688. Children =
  689. {
  690. (current = new Button { Name = "Button4" }),
  691. new Button { Name = "Button5" },
  692. new Button { Name = "Button6" },
  693. }
  694. },
  695. }
  696. };
  697. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Previous);
  698. Assert.Equal(next, result);
  699. }
  700. [Fact]
  701. public void Previous_Continue_Wraps()
  702. {
  703. Button current;
  704. Button next;
  705. var top = new StackPanel
  706. {
  707. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  708. Children =
  709. {
  710. new StackPanel
  711. {
  712. Children =
  713. {
  714. new StackPanel
  715. {
  716. Children =
  717. {
  718. (current = new Button { Name = "Button1" }),
  719. new Button { Name = "Button2" },
  720. new Button { Name = "Button3" },
  721. }
  722. },
  723. },
  724. },
  725. new StackPanel
  726. {
  727. Children =
  728. {
  729. new Button { Name = "Button4" },
  730. new Button { Name = "Button5" },
  731. (next = new Button { Name = "Button6" }),
  732. }
  733. },
  734. }
  735. };
  736. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Previous);
  737. Assert.Equal(next, result);
  738. }
  739. [Fact]
  740. public void Previous_Continue_Returns_Parent()
  741. {
  742. Button current;
  743. var top = new Decorator
  744. {
  745. Focusable = true,
  746. Child = current = new Button
  747. {
  748. Name = "Button",
  749. }
  750. };
  751. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Previous);
  752. Assert.Equal(top, result);
  753. }
  754. [Fact]
  755. public void Previous_Cycle_Returns_Previous_Control_In_Container()
  756. {
  757. Button current;
  758. Button next;
  759. var top = new StackPanel
  760. {
  761. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  762. Children =
  763. {
  764. new StackPanel
  765. {
  766. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  767. Children =
  768. {
  769. (next = new Button { Name = "Button1" }),
  770. (current = new Button { Name = "Button2" }),
  771. new Button { Name = "Button3" },
  772. }
  773. },
  774. new StackPanel
  775. {
  776. Children =
  777. {
  778. new Button { Name = "Button4" },
  779. new Button { Name = "Button5" },
  780. new Button { Name = "Button6" },
  781. }
  782. },
  783. }
  784. };
  785. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Previous);
  786. Assert.Equal(next, result);
  787. }
  788. [Fact]
  789. public void Previous_Cycle_Wraps_To_Last()
  790. {
  791. Button current;
  792. Button next;
  793. var top = new StackPanel
  794. {
  795. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  796. Children =
  797. {
  798. new StackPanel
  799. {
  800. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  801. Children =
  802. {
  803. (current = new Button { Name = "Button1" }),
  804. new Button { Name = "Button2" },
  805. (next = new Button { Name = "Button3" }),
  806. }
  807. },
  808. new StackPanel
  809. {
  810. Children =
  811. {
  812. new Button { Name = "Button4" },
  813. new Button { Name = "Button5" },
  814. new Button { Name = "Button6" },
  815. }
  816. },
  817. }
  818. };
  819. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Previous);
  820. Assert.Equal(next, result);
  821. }
  822. [Fact]
  823. public void Previous_Contained_Returns_Previous_Control_In_Container()
  824. {
  825. Button current;
  826. Button next;
  827. var top = new StackPanel
  828. {
  829. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  830. Children =
  831. {
  832. new StackPanel
  833. {
  834. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Contained,
  835. Children =
  836. {
  837. (next = new Button { Name = "Button1" }),
  838. (current = new Button { Name = "Button2" }),
  839. new Button { Name = "Button3" },
  840. }
  841. },
  842. new StackPanel
  843. {
  844. Children =
  845. {
  846. new Button { Name = "Button4" },
  847. new Button { Name = "Button5" },
  848. new Button { Name = "Button6" },
  849. }
  850. },
  851. }
  852. };
  853. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Previous);
  854. Assert.Equal(next, result);
  855. }
  856. [Fact]
  857. public void Previous_Contained_Stops_At_Beginning()
  858. {
  859. Button current;
  860. var top = new StackPanel
  861. {
  862. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  863. Children =
  864. {
  865. new StackPanel
  866. {
  867. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Contained,
  868. Children =
  869. {
  870. (current = new Button { Name = "Button1" }),
  871. new Button { Name = "Button2" },
  872. new Button { Name = "Button3" },
  873. }
  874. },
  875. new StackPanel
  876. {
  877. Children =
  878. {
  879. new Button { Name = "Button4" },
  880. new Button { Name = "Button5" },
  881. new Button { Name = "Button6" },
  882. }
  883. },
  884. }
  885. };
  886. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Previous);
  887. Assert.Null(result);
  888. }
  889. [Fact]
  890. public void Previous_Once_Moves_To_Previous_Container()
  891. {
  892. Button current;
  893. Button next;
  894. var top = new StackPanel
  895. {
  896. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  897. Children =
  898. {
  899. new StackPanel
  900. {
  901. Children =
  902. {
  903. new Button { Name = "Button1" },
  904. new Button { Name = "Button2" },
  905. (next = new Button { Name = "Button3" }),
  906. }
  907. },
  908. new StackPanel
  909. {
  910. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Once,
  911. Children =
  912. {
  913. new Button { Name = "Button4" },
  914. (current = new Button { Name = "Button5" }),
  915. new Button { Name = "Button6" },
  916. }
  917. },
  918. }
  919. };
  920. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Previous);
  921. Assert.Equal(next, result);
  922. }
  923. [Fact]
  924. public void Previous_Once_Moves_To_Active_Element()
  925. {
  926. StackPanel container;
  927. Button current;
  928. Button next;
  929. var top = new StackPanel
  930. {
  931. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  932. Children =
  933. {
  934. (container = new StackPanel
  935. {
  936. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Once,
  937. Children =
  938. {
  939. new Button { Name = "Button1" },
  940. (next = new Button { Name = "Button2" }),
  941. new Button { Name = "Button3" },
  942. }
  943. }),
  944. new StackPanel
  945. {
  946. Children =
  947. {
  948. (current = new Button { Name = "Button4" }),
  949. new Button { Name = "Button5" },
  950. new Button { Name = "Button6" },
  951. }
  952. },
  953. }
  954. };
  955. KeyboardNavigation.SetTabOnceActiveElement(container, next);
  956. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Previous);
  957. Assert.Equal(next, result);
  958. }
  959. [Fact]
  960. public void Previous_Once_Moves_To_First_Element()
  961. {
  962. Button current;
  963. Button next;
  964. var top = new StackPanel
  965. {
  966. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  967. Children =
  968. {
  969. new StackPanel
  970. {
  971. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Once,
  972. Children =
  973. {
  974. (next = new Button { Name = "Button1" }),
  975. new Button { Name = "Button2" },
  976. new Button { Name = "Button3" },
  977. }
  978. },
  979. new StackPanel
  980. {
  981. Children =
  982. {
  983. (current = new Button { Name = "Button4" }),
  984. new Button { Name = "Button5" },
  985. new Button { Name = "Button6" },
  986. }
  987. },
  988. }
  989. };
  990. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Previous);
  991. Assert.Equal(next, result);
  992. }
  993. [Fact]
  994. public void Previous_Contained_Doesnt_Select_Child_Control()
  995. {
  996. Decorator current;
  997. var top = new StackPanel
  998. {
  999. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Contained,
  1000. Children =
  1001. {
  1002. (current = new Decorator
  1003. {
  1004. Focusable = true,
  1005. Child = new Button(),
  1006. })
  1007. }
  1008. };
  1009. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Previous);
  1010. Assert.Null(result);
  1011. }
  1012. [Fact]
  1013. public void Respects_TabIndex_Moving_Forwards()
  1014. {
  1015. Button start;
  1016. var top = new StackPanel
  1017. {
  1018. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  1019. Children =
  1020. {
  1021. new StackPanel
  1022. {
  1023. Children =
  1024. {
  1025. new Button { Name = "Button1", TabIndex = 5 },
  1026. (start = new Button { Name = "Button2", TabIndex = 2 }),
  1027. new Button { Name = "Button3", TabIndex = 1 },
  1028. }
  1029. },
  1030. new StackPanel
  1031. {
  1032. Children =
  1033. {
  1034. new Button { Name = "Button4", TabIndex = 3 },
  1035. new Button { Name = "Button5", TabIndex = 6 },
  1036. new Button { Name = "Button6", TabIndex = 4 },
  1037. }
  1038. },
  1039. }
  1040. };
  1041. var result = new List<string>();
  1042. var current = (IInputElement)start;
  1043. do
  1044. {
  1045. result.Add(((Control)current).Name);
  1046. current = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  1047. } while (current is object && current != start);
  1048. Assert.Equal(new[]
  1049. {
  1050. "Button2", "Button4", "Button6", "Button1", "Button5", "Button3"
  1051. }, result);
  1052. }
  1053. [Fact]
  1054. public void Respects_TabIndex_Moving_Backwards()
  1055. {
  1056. Button start;
  1057. var top = new StackPanel
  1058. {
  1059. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  1060. Children =
  1061. {
  1062. new StackPanel
  1063. {
  1064. Children =
  1065. {
  1066. new Button { Name = "Button1", TabIndex = 5 },
  1067. (start = new Button { Name = "Button2", TabIndex = 2 }),
  1068. new Button { Name = "Button3", TabIndex = 1 },
  1069. }
  1070. },
  1071. new StackPanel
  1072. {
  1073. Children =
  1074. {
  1075. new Button { Name = "Button4", TabIndex = 3 },
  1076. new Button { Name = "Button5", TabIndex = 6 },
  1077. new Button { Name = "Button6", TabIndex = 4 },
  1078. }
  1079. },
  1080. }
  1081. };
  1082. var result = new List<string>();
  1083. var current = (IInputElement)start;
  1084. do
  1085. {
  1086. result.Add(((Control)current).Name);
  1087. current = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Previous);
  1088. } while (current is object && current != start);
  1089. Assert.Equal(new[]
  1090. {
  1091. "Button2", "Button3", "Button5", "Button1", "Button6", "Button4"
  1092. }, result);
  1093. }
  1094. [Fact]
  1095. public void Cannot_Focus_Child_Of_Disabled_Control()
  1096. {
  1097. Button start;
  1098. Button expected;
  1099. var top = new StackPanel
  1100. {
  1101. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  1102. Children =
  1103. {
  1104. (start = new Button { Name = "Button1" }),
  1105. new Border
  1106. {
  1107. IsEnabled = false,
  1108. Child = new Button { Name = "Button2" },
  1109. },
  1110. (expected = new Button { Name = "Button3" }),
  1111. }
  1112. };
  1113. var current = (IInputElement)start;
  1114. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  1115. Assert.Same(expected, result);
  1116. }
  1117. [Fact]
  1118. public void Focuses_First_Child_From_No_Focus()
  1119. {
  1120. using var app = UnitTestApplication.Start(TestServices.RealFocus);
  1121. var button = new Button();
  1122. var root = new TestRoot(button);
  1123. var target = new KeyboardNavigationHandler();
  1124. target.SetOwner(root);
  1125. root.RaiseEvent(new KeyEventArgs
  1126. {
  1127. RoutedEvent = InputElement.KeyDownEvent,
  1128. Key = Key.Tab,
  1129. });
  1130. Assert.True(button.IsFocused);
  1131. }
  1132. [Fact]
  1133. public void Next_Skip_Button_When_Command_CanExecute_Is_False()
  1134. {
  1135. Button current;
  1136. Button expected;
  1137. bool executed = false;
  1138. var top = new StackPanel
  1139. {
  1140. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  1141. Children =
  1142. {
  1143. new StackPanel
  1144. {
  1145. Children =
  1146. {
  1147. (current = new Button { Name = "Button1" }),
  1148. new Button
  1149. {
  1150. Name = "Button2",
  1151. Command = new Utilities.DelegateCommand(()=>executed = true,
  1152. _ => false),
  1153. },
  1154. (expected = new Button { Name = "Button3" }),
  1155. }
  1156. }
  1157. }
  1158. };
  1159. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next) as Button;
  1160. Assert.Equal(expected.Name, result?.Name);
  1161. Assert.False(executed);
  1162. }
  1163. }
  1164. }