1
0

KeyboardNavigationTests_Tab.cs 44 KB

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