KeyboardNavigationTests_Custom.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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_Custom
  8. {
  9. [Fact]
  10. public void Tab_Should_Custom_Navigate_Within_Children()
  11. {
  12. Button current;
  13. Button next;
  14. var target = new CustomNavigatingStackPanel
  15. {
  16. Children =
  17. {
  18. (current = new Button { Content = "Button 1" }),
  19. new Button { Content = "Button 2" },
  20. (next = new Button { Content = "Button 3" }),
  21. },
  22. NextControl = next,
  23. };
  24. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  25. Assert.Same(next, result);
  26. }
  27. [Fact]
  28. public void Right_Should_Custom_Navigate_Within_Children()
  29. {
  30. Button current;
  31. Button next;
  32. var target = new CustomNavigatingStackPanel
  33. {
  34. Children =
  35. {
  36. (current = new Button { Content = "Button 1" }),
  37. new Button { Content = "Button 2" },
  38. (next = new Button { Content = "Button 3" }),
  39. },
  40. NextControl = next,
  41. };
  42. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Right);
  43. Assert.Same(next, result);
  44. }
  45. [Fact]
  46. public void Tab_Should_Custom_Navigate_From_Outside()
  47. {
  48. Button current;
  49. Button next;
  50. var target = new CustomNavigatingStackPanel
  51. {
  52. Children =
  53. {
  54. new Button { Content = "Button 1" },
  55. new Button { Content = "Button 2" },
  56. (next = new Button { Content = "Button 3" }),
  57. },
  58. NextControl = next,
  59. };
  60. var root = new StackPanel
  61. {
  62. Children =
  63. {
  64. (current = new Button { Content = "Outside" }),
  65. target,
  66. }
  67. };
  68. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  69. Assert.Same(next, result);
  70. }
  71. [Fact]
  72. public void Tab_Should_Custom_Navigate_From_Outside_When_Wrapping()
  73. {
  74. Button current;
  75. Button next;
  76. var target = new CustomNavigatingStackPanel
  77. {
  78. Children =
  79. {
  80. new Button { Content = "Button 1" },
  81. new Button { Content = "Button 2" },
  82. (next = new Button { Content = "Button 3" }),
  83. },
  84. NextControl = next,
  85. };
  86. var root = new StackPanel
  87. {
  88. Children =
  89. {
  90. target,
  91. (current = new Button { Content = "Outside" }),
  92. }
  93. };
  94. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  95. Assert.Same(next, result);
  96. }
  97. [Fact]
  98. public void ShiftTab_Should_Custom_Navigate_From_Outside()
  99. {
  100. Button current;
  101. Button next;
  102. var target = new CustomNavigatingStackPanel
  103. {
  104. Children =
  105. {
  106. new Button { Content = "Button 1" },
  107. new Button { Content = "Button 2" },
  108. (next = new Button { Content = "Button 3" }),
  109. },
  110. NextControl = next,
  111. };
  112. var root = new StackPanel
  113. {
  114. Children =
  115. {
  116. (current = new Button { Content = "Outside" }),
  117. target,
  118. }
  119. };
  120. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Previous);
  121. Assert.Same(next, result);
  122. }
  123. [Fact]
  124. public void Right_Should_Custom_Navigate_From_Outside()
  125. {
  126. Button current;
  127. Button next;
  128. var target = new CustomNavigatingStackPanel
  129. {
  130. Children =
  131. {
  132. new Button { Content = "Button 1" },
  133. new Button { Content = "Button 2" },
  134. (next = new Button { Content = "Button 3" }),
  135. },
  136. NextControl = next,
  137. };
  138. var root = new StackPanel
  139. {
  140. Children =
  141. {
  142. (current = new Button { Content = "Outside" }),
  143. target,
  144. },
  145. [KeyboardNavigation.DirectionalNavigationProperty] = KeyboardNavigationMode.Continue,
  146. };
  147. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Right);
  148. Assert.Same(next, result);
  149. }
  150. [Fact]
  151. public void Tab_Should_Navigate_Outside_When_Null_Returned_As_Next()
  152. {
  153. Button current;
  154. Button next;
  155. var target = new CustomNavigatingStackPanel
  156. {
  157. Children =
  158. {
  159. new Button { Content = "Button 1" },
  160. (current = new Button { Content = "Button 2" }),
  161. new Button { Content = "Button 3" },
  162. },
  163. };
  164. var root = new StackPanel
  165. {
  166. Children =
  167. {
  168. target,
  169. (next = new Button { Content = "Outside" }),
  170. }
  171. };
  172. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  173. Assert.Same(next, result);
  174. }
  175. private class CustomNavigatingStackPanel : StackPanel, ICustomKeyboardNavigation
  176. {
  177. public bool CustomNavigates { get; set; } = true;
  178. public IInputElement NextControl { get; set; }
  179. public (bool handled, IInputElement next) GetNext(IInputElement element, NavigationDirection direction)
  180. {
  181. return (CustomNavigates, NextControl);
  182. }
  183. }
  184. }
  185. }