1
0

KeyboardNavigationTests_Custom.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using Avalonia.Controls;
  2. using Avalonia.Input;
  3. using Xunit;
  4. namespace Avalonia.Base.UnitTests.Input
  5. {
  6. public class KeyboardNavigationTests_Custom
  7. {
  8. [Fact]
  9. public void Tab_Should_Custom_Navigate_Within_Children()
  10. {
  11. Button current;
  12. Button next;
  13. var target = new CustomNavigatingStackPanel
  14. {
  15. Children =
  16. {
  17. (current = new Button { Content = "Button 1" }),
  18. new Button { Content = "Button 2" },
  19. (next = new Button { Content = "Button 3" }),
  20. },
  21. NextControl = next,
  22. };
  23. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  24. Assert.Same(next, result);
  25. }
  26. [Fact]
  27. public void Right_Should_Custom_Navigate_Within_Children()
  28. {
  29. Button current;
  30. Button next;
  31. var target = new CustomNavigatingStackPanel
  32. {
  33. Children =
  34. {
  35. (current = new Button { Content = "Button 1" }),
  36. new Button { Content = "Button 2" },
  37. (next = new Button { Content = "Button 3" }),
  38. },
  39. NextControl = next,
  40. };
  41. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Right);
  42. Assert.Same(next, result);
  43. }
  44. [Fact]
  45. public void Tab_Should_Custom_Navigate_From_Outside()
  46. {
  47. Button current;
  48. Button next;
  49. var target = new CustomNavigatingStackPanel
  50. {
  51. Children =
  52. {
  53. new Button { Content = "Button 1" },
  54. new Button { Content = "Button 2" },
  55. (next = new Button { Content = "Button 3" }),
  56. },
  57. NextControl = next,
  58. };
  59. var root = new StackPanel
  60. {
  61. Children =
  62. {
  63. (current = new Button { Content = "Outside" }),
  64. target,
  65. }
  66. };
  67. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  68. Assert.Same(next, result);
  69. }
  70. [Fact]
  71. public void Tab_Should_Custom_Navigate_From_Outside_When_Wrapping()
  72. {
  73. Button current;
  74. Button next;
  75. var target = new CustomNavigatingStackPanel
  76. {
  77. Children =
  78. {
  79. new Button { Content = "Button 1" },
  80. new Button { Content = "Button 2" },
  81. (next = new Button { Content = "Button 3" }),
  82. },
  83. NextControl = next,
  84. };
  85. var root = new StackPanel
  86. {
  87. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  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. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  115. Children =
  116. {
  117. (current = new Button { Content = "Outside" }),
  118. target,
  119. }
  120. };
  121. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Previous);
  122. Assert.Same(next, result);
  123. }
  124. [Fact]
  125. public void ShiftTab_Should_Navigate_Outside_When_Null_Returned_As_Next()
  126. {
  127. Button current;
  128. Button next;
  129. var target = new CustomNavigatingStackPanel
  130. {
  131. Children =
  132. {
  133. new Button { Content = "Button 1" },
  134. (current = new Button { Content = "Button 2" }),
  135. new Button { Content = "Button 3" },
  136. },
  137. };
  138. var root = new StackPanel
  139. {
  140. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  141. Children =
  142. {
  143. target,
  144. (next = new Button { Content = "Outside" }),
  145. }
  146. };
  147. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Previous);
  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. [KeyboardNavigation.TabNavigationProperty] = KeyboardNavigationMode.Cycle,
  167. Children =
  168. {
  169. target,
  170. (next = new Button { Content = "Outside" }),
  171. }
  172. };
  173. var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next);
  174. Assert.Same(next, result);
  175. }
  176. private class CustomNavigatingStackPanel : StackPanel, ICustomKeyboardNavigation
  177. {
  178. public bool CustomNavigates { get; set; } = true;
  179. public IInputElement NextControl { get; set; }
  180. public (bool handled, IInputElement next) GetNext(IInputElement element, NavigationDirection direction)
  181. {
  182. return (CustomNavigates, NextControl);
  183. }
  184. }
  185. }
  186. }