KeyboardNavigationTests_Arrows.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using Avalonia.Controls;
  4. using Xunit;
  5. namespace Avalonia.Input.UnitTests
  6. {
  7. public class KeyboardNavigationTests_Arrows
  8. {
  9. [Fact]
  10. public void Down_Continue_Returns_Down_Control_In_Container()
  11. {
  12. Button current;
  13. Button next;
  14. var top = new StackPanel
  15. {
  16. Children =
  17. {
  18. new StackPanel
  19. {
  20. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  21. Children =
  22. {
  23. new Button { Name = "Button1" },
  24. (current = new Button { Name = "Button2" }),
  25. (next = new Button { Name = "Button3" }),
  26. }
  27. },
  28. new StackPanel
  29. {
  30. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  31. Children =
  32. {
  33. new Button { Name = "Button4" },
  34. new Button { Name = "Button5" },
  35. new Button { Name = "Button6" },
  36. }
  37. },
  38. }
  39. };
  40. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Down);
  41. Assert.Equal(next, result);
  42. }
  43. [Fact]
  44. public void Down_Continue_Returns_First_Control_In_Down_Sibling_Container()
  45. {
  46. Button current;
  47. Button next;
  48. var top = new StackPanel
  49. {
  50. Children =
  51. {
  52. new StackPanel
  53. {
  54. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  55. Children =
  56. {
  57. new Button { Name = "Button1" },
  58. new Button { Name = "Button2" },
  59. (current = new Button { Name = "Button3" }),
  60. }
  61. },
  62. new StackPanel
  63. {
  64. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  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.Down);
  75. Assert.Equal(next, result);
  76. }
  77. [Fact]
  78. public void Down_Continue_Returns_Down_Sibling()
  79. {
  80. Button current;
  81. Button next;
  82. var top = new StackPanel
  83. {
  84. Children =
  85. {
  86. new StackPanel
  87. {
  88. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  89. Children =
  90. {
  91. new Button { Name = "Button1" },
  92. new Button { Name = "Button2" },
  93. (current = new Button { Name = "Button3" }),
  94. }
  95. },
  96. (next = new Button { Name = "Button4" }),
  97. }
  98. };
  99. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Down);
  100. Assert.Equal(next, result);
  101. }
  102. [Fact]
  103. public void Down_Continue_Returns_First_Control_In_Down_Uncle_Container()
  104. {
  105. Button current;
  106. Button next;
  107. var top = new StackPanel
  108. {
  109. Children =
  110. {
  111. new StackPanel
  112. {
  113. Children =
  114. {
  115. new StackPanel
  116. {
  117. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  118. Children =
  119. {
  120. new Button { Name = "Button1" },
  121. new Button { Name = "Button2" },
  122. (current = new Button { Name = "Button3" }),
  123. }
  124. },
  125. },
  126. },
  127. new StackPanel
  128. {
  129. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  130. Children =
  131. {
  132. (next = new Button { Name = "Button4" }),
  133. new Button { Name = "Button5" },
  134. new Button { Name = "Button6" },
  135. }
  136. },
  137. }
  138. };
  139. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Down);
  140. Assert.Equal(next, result);
  141. }
  142. [Fact]
  143. public void Down_Continue_Returns_Child_Of_Top_Level()
  144. {
  145. Button next;
  146. var top = new StackPanel
  147. {
  148. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  149. Children =
  150. {
  151. (next = new Button { Name = "Button1" }),
  152. }
  153. };
  154. var result = KeyboardNavigationHandler.GetNext(top, NavigationDirection.Down);
  155. Assert.Equal(next, result);
  156. }
  157. [Fact]
  158. public void Down_Continue_Wraps()
  159. {
  160. Button current;
  161. Button next;
  162. var top = new StackPanel
  163. {
  164. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  165. Children =
  166. {
  167. new StackPanel
  168. {
  169. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  170. Children =
  171. {
  172. new StackPanel
  173. {
  174. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  175. Children =
  176. {
  177. (next = new Button { Name = "Button1" }),
  178. new Button { Name = "Button2" },
  179. new Button { Name = "Button3" },
  180. }
  181. },
  182. },
  183. },
  184. new StackPanel
  185. {
  186. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  187. Children =
  188. {
  189. new Button { Name = "Button4" },
  190. new Button { Name = "Button5" },
  191. (current = new Button { Name = "Button6" }),
  192. }
  193. },
  194. }
  195. };
  196. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Down);
  197. Assert.Equal(next, result);
  198. }
  199. [Fact]
  200. public void Down_Cycle_Returns_Down_Control_In_Container()
  201. {
  202. Button current;
  203. Button next;
  204. var top = new StackPanel
  205. {
  206. Children =
  207. {
  208. new StackPanel
  209. {
  210. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Cycle,
  211. Children =
  212. {
  213. new Button { Name = "Button1" },
  214. (current = new Button { Name = "Button2" }),
  215. (next = new Button { Name = "Button3" }),
  216. }
  217. },
  218. new StackPanel
  219. {
  220. Children =
  221. {
  222. new Button { Name = "Button4" },
  223. new Button { Name = "Button5" },
  224. new Button { Name = "Button6" },
  225. }
  226. },
  227. }
  228. };
  229. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Down);
  230. Assert.Equal(next, result);
  231. }
  232. [Fact]
  233. public void Down_Cycle_Wraps_To_First()
  234. {
  235. Button current;
  236. Button next;
  237. var top = new StackPanel
  238. {
  239. Children =
  240. {
  241. new StackPanel
  242. {
  243. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Cycle,
  244. Children =
  245. {
  246. (next = new Button { Name = "Button1" }),
  247. new Button { Name = "Button2" },
  248. (current = new Button { Name = "Button3" }),
  249. }
  250. },
  251. new StackPanel
  252. {
  253. Children =
  254. {
  255. new Button { Name = "Button4" },
  256. new Button { Name = "Button5" },
  257. new Button { Name = "Button6" },
  258. }
  259. },
  260. }
  261. };
  262. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Down);
  263. Assert.Equal(next, result);
  264. }
  265. [Fact]
  266. public void Down_Contained_Returns_Down_Control_In_Container()
  267. {
  268. Button current;
  269. Button next;
  270. var top = new StackPanel
  271. {
  272. Children =
  273. {
  274. new StackPanel
  275. {
  276. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Contained,
  277. Children =
  278. {
  279. new Button { Name = "Button1" },
  280. (current = new Button { Name = "Button2" }),
  281. (next = new Button { Name = "Button3" }),
  282. }
  283. },
  284. new StackPanel
  285. {
  286. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Contained,
  287. Children =
  288. {
  289. new Button { Name = "Button4" },
  290. new Button { Name = "Button5" },
  291. new Button { Name = "Button6" },
  292. }
  293. },
  294. }
  295. };
  296. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Down);
  297. Assert.Equal(next, result);
  298. }
  299. [Fact]
  300. public void Down_Contained_Stops_At_End()
  301. {
  302. Button current;
  303. var top = new StackPanel
  304. {
  305. Children =
  306. {
  307. new StackPanel
  308. {
  309. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Contained,
  310. Children =
  311. {
  312. new Button { Name = "Button1" },
  313. new Button { Name = "Button2" },
  314. (current = new Button { Name = "Button3" }),
  315. }
  316. },
  317. new StackPanel
  318. {
  319. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Contained,
  320. Children =
  321. {
  322. new Button { Name = "Button4" },
  323. new Button { Name = "Button5" },
  324. new Button { Name = "Button6" },
  325. }
  326. },
  327. }
  328. };
  329. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Down);
  330. Assert.Null(result);
  331. }
  332. [Fact]
  333. public void Down_None_Does_Nothing()
  334. {
  335. Button current;
  336. var top = new StackPanel
  337. {
  338. Children =
  339. {
  340. new StackPanel
  341. {
  342. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.None,
  343. Children =
  344. {
  345. new Button { Name = "Button1" },
  346. (current = new Button { Name = "Button2" }),
  347. new Button { Name = "Button3" },
  348. }
  349. },
  350. new StackPanel
  351. {
  352. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Contained,
  353. Children =
  354. {
  355. new Button { Name = "Button4" },
  356. new Button { Name = "Button5" },
  357. new Button { Name = "Button6" },
  358. }
  359. },
  360. }
  361. };
  362. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Down);
  363. Assert.Null(result);
  364. }
  365. [Fact]
  366. public void Up_Continue_Returns_Up_Control_In_Container()
  367. {
  368. Button current;
  369. Button next;
  370. var top = new StackPanel
  371. {
  372. Children =
  373. {
  374. new StackPanel
  375. {
  376. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  377. Children =
  378. {
  379. new Button { Name = "Button1" },
  380. (next = new Button { Name = "Button2" }),
  381. (current = new Button { Name = "Button3" }),
  382. }
  383. },
  384. new StackPanel
  385. {
  386. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Contained,
  387. Children =
  388. {
  389. new Button { Name = "Button4" },
  390. new Button { Name = "Button5" },
  391. new Button { Name = "Button6" },
  392. }
  393. },
  394. }
  395. };
  396. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Up);
  397. Assert.Equal(next, result);
  398. }
  399. [Fact]
  400. public void Up_Continue_Returns_Last_Control_In_Up_Sibling_Container()
  401. {
  402. Button current;
  403. Button next;
  404. var top = new StackPanel
  405. {
  406. Children =
  407. {
  408. new StackPanel
  409. {
  410. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  411. Children =
  412. {
  413. new Button { Name = "Button1" },
  414. new Button { Name = "Button2" },
  415. (next = new Button { Name = "Button3" }),
  416. }
  417. },
  418. new StackPanel
  419. {
  420. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  421. Children =
  422. {
  423. (current = new Button { Name = "Button4" }),
  424. new Button { Name = "Button5" },
  425. new Button { Name = "Button6" },
  426. }
  427. },
  428. }
  429. };
  430. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Up);
  431. Assert.Equal(next, result);
  432. }
  433. [Fact]
  434. public void Up_Continue_Returns_Last_Child_Of_Sibling()
  435. {
  436. Button current;
  437. Button next;
  438. var top = new StackPanel
  439. {
  440. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  441. Children =
  442. {
  443. new StackPanel
  444. {
  445. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  446. Children =
  447. {
  448. new Button { Name = "Button1" },
  449. new Button { Name = "Button2" },
  450. (next = new Button { Name = "Button3" }),
  451. }
  452. },
  453. (current = new Button { Name = "Button4" }),
  454. }
  455. };
  456. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Up);
  457. Assert.Equal(next, result);
  458. }
  459. [Fact]
  460. public void Up_Continue_Returns_Last_Control_In_Up_Nephew_Container()
  461. {
  462. Button current;
  463. Button next;
  464. var top = new StackPanel
  465. {
  466. Children =
  467. {
  468. new StackPanel
  469. {
  470. Children =
  471. {
  472. new StackPanel
  473. {
  474. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  475. Children =
  476. {
  477. new Button { Name = "Button1" },
  478. new Button { Name = "Button2" },
  479. (next = new Button { Name = "Button3" }),
  480. }
  481. },
  482. },
  483. },
  484. new StackPanel
  485. {
  486. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  487. Children =
  488. {
  489. (current = new Button { Name = "Button4" }),
  490. new Button { Name = "Button5" },
  491. new Button { Name = "Button6" },
  492. }
  493. },
  494. }
  495. };
  496. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Up);
  497. Assert.Equal(next, result);
  498. }
  499. [Fact]
  500. public void Up_Continue_Wraps()
  501. {
  502. Button current;
  503. Button next;
  504. var top = new StackPanel
  505. {
  506. Children =
  507. {
  508. new StackPanel
  509. {
  510. Children =
  511. {
  512. new StackPanel
  513. {
  514. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  515. Children =
  516. {
  517. (current = new Button { Name = "Button1" }),
  518. new Button { Name = "Button2" },
  519. new Button { Name = "Button3" },
  520. }
  521. },
  522. },
  523. },
  524. new StackPanel
  525. {
  526. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  527. Children =
  528. {
  529. new Button { Name = "Button4" },
  530. new Button { Name = "Button5" },
  531. (next = new Button { Name = "Button6" }),
  532. }
  533. },
  534. }
  535. };
  536. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Up);
  537. Assert.Equal(next, result);
  538. }
  539. [Fact]
  540. public void Up_Continue_Returns_Parent()
  541. {
  542. Button current;
  543. var top = new Decorator
  544. {
  545. Focusable = true,
  546. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  547. Child = current = new Button
  548. {
  549. Name = "Button",
  550. }
  551. };
  552. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Up);
  553. Assert.Equal(top, result);
  554. }
  555. [Fact]
  556. public void Up_Cycle_Returns_Up_Control_In_Container()
  557. {
  558. Button current;
  559. Button next;
  560. var top = new StackPanel
  561. {
  562. Children =
  563. {
  564. new StackPanel
  565. {
  566. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Cycle,
  567. Children =
  568. {
  569. (next = new Button { Name = "Button1" }),
  570. (current = new Button { Name = "Button2" }),
  571. new Button { Name = "Button3" },
  572. }
  573. },
  574. new StackPanel
  575. {
  576. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Cycle,
  577. Children =
  578. {
  579. new Button { Name = "Button4" },
  580. new Button { Name = "Button5" },
  581. new Button { Name = "Button6" },
  582. }
  583. },
  584. }
  585. };
  586. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Up);
  587. Assert.Equal(next, result);
  588. }
  589. [Fact]
  590. public void Up_Cycle_Wraps_To_Last()
  591. {
  592. Button current;
  593. Button next;
  594. var top = new StackPanel
  595. {
  596. Children =
  597. {
  598. new StackPanel
  599. {
  600. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Cycle,
  601. Children =
  602. {
  603. (current = new Button { Name = "Button1" }),
  604. new Button { Name = "Button2" },
  605. (next = new Button { Name = "Button3" }),
  606. }
  607. },
  608. new StackPanel
  609. {
  610. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Cycle,
  611. Children =
  612. {
  613. new Button { Name = "Button4" },
  614. new Button { Name = "Button5" },
  615. new Button { Name = "Button6" },
  616. }
  617. },
  618. }
  619. };
  620. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Up);
  621. Assert.Equal(next, result);
  622. }
  623. [Fact]
  624. public void Up_Contained_Returns_Up_Control_In_Container()
  625. {
  626. Button current;
  627. Button next;
  628. var top = new StackPanel
  629. {
  630. Children =
  631. {
  632. new StackPanel
  633. {
  634. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Contained,
  635. Children =
  636. {
  637. (next = new Button { Name = "Button1" }),
  638. (current = new Button { Name = "Button2" }),
  639. new Button { Name = "Button3" },
  640. }
  641. },
  642. new StackPanel
  643. {
  644. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Contained,
  645. Children =
  646. {
  647. new Button { Name = "Button4" },
  648. new Button { Name = "Button5" },
  649. new Button { Name = "Button6" },
  650. }
  651. },
  652. }
  653. };
  654. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Up);
  655. Assert.Equal(next, result);
  656. }
  657. [Fact]
  658. public void Up_Contained_Stops_At_Beginning()
  659. {
  660. Button current;
  661. var top = new StackPanel
  662. {
  663. Children =
  664. {
  665. new StackPanel
  666. {
  667. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Contained,
  668. Children =
  669. {
  670. (current = new Button { Name = "Button1" }),
  671. new Button { Name = "Button2" },
  672. new Button { Name = "Button3" },
  673. }
  674. },
  675. new StackPanel
  676. {
  677. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Contained,
  678. Children =
  679. {
  680. new Button { Name = "Button4" },
  681. new Button { Name = "Button5" },
  682. new Button { Name = "Button6" },
  683. }
  684. },
  685. }
  686. };
  687. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Up);
  688. Assert.Null(result);
  689. }
  690. [Fact]
  691. public void Up_Contained_Doesnt_Return_Child_Control()
  692. {
  693. Decorator current;
  694. var top = new StackPanel
  695. {
  696. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Contained,
  697. Children =
  698. {
  699. (current = new Decorator
  700. {
  701. Focusable = true,
  702. Child = new Button(),
  703. })
  704. }
  705. };
  706. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Up);
  707. Assert.Null(result);
  708. }
  709. }
  710. }