KeyboardNavigationTests_Arrows.cs 28 KB

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