KeyboardNavigationTests_XY.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. using System;
  2. using Avalonia.Controls;
  3. using Avalonia.Input;
  4. using Avalonia.Layout;
  5. using Avalonia.UnitTests;
  6. using Xunit;
  7. namespace Avalonia.Base.UnitTests.Input;
  8. public class KeyboardNavigationTests_XY : ScopedTestBase
  9. {
  10. private static (Canvas canvas, Button[] buttons) CreateXYTestLayout()
  11. {
  12. // 111
  13. // 111
  14. // 111
  15. // 2
  16. // 3
  17. //
  18. // 4
  19. Button x1, x2, x3, x4;
  20. var canvas = new Canvas
  21. {
  22. Width = 500,
  23. Children =
  24. {
  25. (x1 = new Button
  26. {
  27. Content = "A",
  28. [Canvas.LeftProperty] = 50, [Canvas.TopProperty] = 0, Width = 150, Height = 150,
  29. }),
  30. (x2 = new Button
  31. {
  32. Content = "B",
  33. [Canvas.LeftProperty] = 400, [Canvas.TopProperty] = 150, Width = 50, Height = 50,
  34. }),
  35. (x3 = new Button
  36. {
  37. Content = "C",
  38. [Canvas.LeftProperty] = 0, [Canvas.TopProperty] = 200, Width = 50, Height = 50,
  39. }),
  40. (x4 = new Button
  41. {
  42. Content = "D",
  43. [Canvas.LeftProperty] = 100, [Canvas.TopProperty] = 300, Width = 50, Height = 50,
  44. })
  45. }
  46. };
  47. return (canvas, new[] { x1, x2, x3, x4 });
  48. }
  49. [Theory]
  50. [InlineData(1, NavigationDirection.Down, 4)]
  51. [InlineData(1, NavigationDirection.Up, -1)]
  52. [InlineData(1, NavigationDirection.Left, -1)]
  53. [InlineData(1, NavigationDirection.Right, 2)]
  54. // TODO: [InlineData(2, NavigationDirection.Down, 4)] Actual: 3
  55. // TODO: [InlineData(2, NavigationDirection.Up, -1)] Actual 1
  56. [InlineData(2, NavigationDirection.Left, 1)]
  57. [InlineData(2, NavigationDirection.Right, -1)]
  58. [InlineData(3, NavigationDirection.Down, 4)]
  59. // TODO: [InlineData(3, NavigationDirection.Up, 1)] Actual: 2
  60. [InlineData(3, NavigationDirection.Left, -1)]
  61. // TODO: [InlineData(3, NavigationDirection.Right, 4)] Actual: 1
  62. [InlineData(4, NavigationDirection.Down, -1)]
  63. [InlineData(4, NavigationDirection.Up, 1)]
  64. [InlineData(4, NavigationDirection.Left, 3)]
  65. [InlineData(4, NavigationDirection.Right, 2)]
  66. public void Projection_Focus_Depending_On_Direction(int from, NavigationDirection direction, int to)
  67. {
  68. using var _ = UnitTestApplication.Start(TestServices.FocusableWindow);
  69. var (canvas, buttons) = CreateXYTestLayout();
  70. var window = new Window
  71. {
  72. [XYFocus.NavigationModesProperty] = XYFocusNavigationModes.Enabled,
  73. Content = canvas
  74. };
  75. window.Show();
  76. var fromButton = buttons[from - 1];
  77. fromButton.SetValue(XYFocus.UpNavigationStrategyProperty, XYFocusNavigationStrategy.Projection);
  78. fromButton.SetValue(XYFocus.LeftNavigationStrategyProperty, XYFocusNavigationStrategy.Projection);
  79. fromButton.SetValue(XYFocus.RightNavigationStrategyProperty, XYFocusNavigationStrategy.Projection);
  80. fromButton.SetValue(XYFocus.DownNavigationStrategyProperty, XYFocusNavigationStrategy.Projection);
  81. var result = KeyboardNavigationHandler.GetNext(fromButton, direction) as Button;
  82. Assert.Equal(to, result == null ? -1 : Array.IndexOf(buttons, result) + 1);
  83. }
  84. [Theory]
  85. [InlineData(1, NavigationDirection.Down, 3)]
  86. [InlineData(1, NavigationDirection.Up, -1)]
  87. [InlineData(1, NavigationDirection.Left, 3)]
  88. [InlineData(1, NavigationDirection.Right, 2)]
  89. [InlineData(2, NavigationDirection.Down, 3)]
  90. [InlineData(2, NavigationDirection.Up, 1)]
  91. [InlineData(2, NavigationDirection.Left, 1)]
  92. [InlineData(2, NavigationDirection.Right, -1)]
  93. [InlineData(3, NavigationDirection.Down, 4)]
  94. [InlineData(3, NavigationDirection.Up, 1)]
  95. [InlineData(3, NavigationDirection.Left, -1)]
  96. [InlineData(3, NavigationDirection.Right, 1)]
  97. [InlineData(4, NavigationDirection.Down, -1)]
  98. [InlineData(4, NavigationDirection.Up, 3)]
  99. [InlineData(4, NavigationDirection.Left, 3)]
  100. [InlineData(4, NavigationDirection.Right, 2)]
  101. public void RectilinearDistance_Focus_Depending_On_Direction(int from, NavigationDirection direction, int to)
  102. {
  103. using var _ = UnitTestApplication.Start(TestServices.FocusableWindow);
  104. var (canvas, buttons) = CreateXYTestLayout();
  105. var window = new Window
  106. {
  107. [XYFocus.NavigationModesProperty] = XYFocusNavigationModes.Enabled,
  108. Content = canvas
  109. };
  110. window.Show();
  111. var fromButton = buttons[from - 1];
  112. fromButton.SetValue(XYFocus.UpNavigationStrategyProperty, XYFocusNavigationStrategy.RectilinearDistance);
  113. fromButton.SetValue(XYFocus.LeftNavigationStrategyProperty, XYFocusNavigationStrategy.RectilinearDistance);
  114. fromButton.SetValue(XYFocus.RightNavigationStrategyProperty, XYFocusNavigationStrategy.RectilinearDistance);
  115. fromButton.SetValue(XYFocus.DownNavigationStrategyProperty, XYFocusNavigationStrategy.RectilinearDistance);
  116. var result = KeyboardNavigationHandler.GetNext(fromButton, direction) as Button;
  117. Assert.Equal(to, result == null ? -1 : Array.IndexOf(buttons, result) + 1);
  118. }
  119. [Theory]
  120. [InlineData(1, NavigationDirection.Down, 2)]
  121. [InlineData(1, NavigationDirection.Up, -1)]
  122. [InlineData(1, NavigationDirection.Left, 3)]
  123. [InlineData(1, NavigationDirection.Right, 2)]
  124. [InlineData(2, NavigationDirection.Down, 3)]
  125. [InlineData(2, NavigationDirection.Up, 1)]
  126. [InlineData(2, NavigationDirection.Left, 1)]
  127. [InlineData(2, NavigationDirection.Right, -1)]
  128. [InlineData(3, NavigationDirection.Down, 4)]
  129. [InlineData(3, NavigationDirection.Up, 2)]
  130. [InlineData(3, NavigationDirection.Left, -1)]
  131. [InlineData(3, NavigationDirection.Right, 1)]
  132. [InlineData(4, NavigationDirection.Down, -1)]
  133. [InlineData(4, NavigationDirection.Up, 3)]
  134. [InlineData(4, NavigationDirection.Left, 3)]
  135. [InlineData(4, NavigationDirection.Right, 2)]
  136. public void NavigationDirectionDistance_Focus_Depending_On_Direction(int from, NavigationDirection direction, int to)
  137. {
  138. using var _ = UnitTestApplication.Start(TestServices.FocusableWindow);
  139. var (canvas, buttons) = CreateXYTestLayout();
  140. var window = new Window
  141. {
  142. [XYFocus.NavigationModesProperty] = XYFocusNavigationModes.Enabled,
  143. Content = canvas
  144. };
  145. window.Show();
  146. var fromButton = buttons[from - 1];
  147. fromButton.SetValue(XYFocus.UpNavigationStrategyProperty, XYFocusNavigationStrategy.NavigationDirectionDistance);
  148. fromButton.SetValue(XYFocus.LeftNavigationStrategyProperty, XYFocusNavigationStrategy.NavigationDirectionDistance);
  149. fromButton.SetValue(XYFocus.RightNavigationStrategyProperty, XYFocusNavigationStrategy.NavigationDirectionDistance);
  150. fromButton.SetValue(XYFocus.DownNavigationStrategyProperty, XYFocusNavigationStrategy.NavigationDirectionDistance);
  151. var result = KeyboardNavigationHandler.GetNext(fromButton, direction) as Button;
  152. Assert.Equal(to, result == null ? -1 : Array.IndexOf(buttons, result) + 1);
  153. }
  154. [Fact]
  155. public void Uses_XY_Directional_Overrides()
  156. {
  157. using var _ = UnitTestApplication.Start(TestServices.FocusableWindow);
  158. var left = new Button();
  159. var right = new Button();
  160. var up = new Button();
  161. var down = new Button();
  162. var center = new Button
  163. {
  164. [XYFocus.LeftProperty] = left,
  165. [XYFocus.RightProperty] = right,
  166. [XYFocus.UpProperty] = up,
  167. [XYFocus.DownProperty] = down,
  168. };
  169. var window = new Window
  170. {
  171. [XYFocus.NavigationModesProperty] = XYFocusNavigationModes.Enabled,
  172. Content = new Canvas
  173. {
  174. Children =
  175. {
  176. left, right, up, down, center
  177. }
  178. }
  179. };
  180. window.Show();
  181. Assert.Equal(left, KeyboardNavigationHandler.GetNext(center, NavigationDirection.Left));
  182. Assert.Equal(right, KeyboardNavigationHandler.GetNext(center, NavigationDirection.Right));
  183. Assert.Equal(up, KeyboardNavigationHandler.GetNext(center, NavigationDirection.Up));
  184. Assert.Equal(down, KeyboardNavigationHandler.GetNext(center, NavigationDirection.Down));
  185. }
  186. [Fact]
  187. public void XY_Directional_Override_Discarded_If_Not_Part_Of_The_Same_Root()
  188. {
  189. using var _ = UnitTestApplication.Start(TestServices.FocusableWindow);
  190. var left = new Button();
  191. var center = new Button
  192. {
  193. [XYFocus.LeftProperty] = left
  194. };
  195. var window = new Window
  196. {
  197. [XYFocus.NavigationModesProperty] = XYFocusNavigationModes.Enabled,
  198. Content = center
  199. };
  200. window.Show();
  201. Assert.Null(KeyboardNavigationHandler.GetNext(center, NavigationDirection.Left));
  202. }
  203. [Fact]
  204. public void Parent_Can_Override_Navigation_When_Directional_Is_Set()
  205. {
  206. using var _ = UnitTestApplication.Start(TestServices.FocusableWindow);
  207. // With double stack panel layout we have something like this:
  208. // [ [ EXPECTED, CURRENT ] CANDIDATE ]
  209. // Where normally from Current focus would go to the Candidate.
  210. // But since we set `XYFocus.Right` on nested StackPanel, it should be used instead.
  211. // But ONLY if Candidate isn't part of that nested StackPanel (it isn't).
  212. var current = new Button();
  213. var candidate = new Button();
  214. var expectedOverride = new Button();
  215. var parent = new StackPanel
  216. {
  217. Orientation = Orientation.Horizontal,
  218. Children = { expectedOverride, current },
  219. [XYFocus.RightProperty] = expectedOverride,
  220. // Property value to simplify test.
  221. [XYFocus.RightNavigationStrategyProperty] = XYFocusNavigationStrategy.RectilinearDistance
  222. };
  223. var window = new Window
  224. {
  225. [XYFocus.NavigationModesProperty] = XYFocusNavigationModes.Enabled,
  226. Content = new StackPanel
  227. {
  228. Orientation = Orientation.Horizontal,
  229. Children = { parent, candidate }
  230. }
  231. };
  232. window.Show();
  233. Assert.Equal(expectedOverride, KeyboardNavigationHandler.GetNext(current, NavigationDirection.Right));
  234. }
  235. [Fact]
  236. public void Clipped_Element_Should_Not_Be_Focused()
  237. {
  238. using var _ = UnitTestApplication.Start(TestServices.FocusableWindow);
  239. var current = new Button() { Height = 20 };
  240. var candidate = new Button() { Height = 20 };
  241. var parent = new StackPanel
  242. {
  243. Orientation = Orientation.Vertical,
  244. Spacing = 20,
  245. Children = { current, candidate }
  246. };
  247. var window = new Window
  248. {
  249. [XYFocus.NavigationModesProperty] = XYFocusNavigationModes.Enabled,
  250. Content = parent,
  251. Height = 30
  252. };
  253. window.Show();
  254. Assert.Null(KeyboardNavigationHandler.GetNext(current, NavigationDirection.Down));
  255. }
  256. [Fact]
  257. public void Clipped_Element_Should_Not_Focused_If_Inside_Of_ScrollViewer()
  258. {
  259. using var _ = UnitTestApplication.Start(TestServices.FocusableWindow);
  260. var current = new Button() { Height = 20 };
  261. var candidate = new Button() { Height = 20 };
  262. var parent = new StackPanel
  263. {
  264. Orientation = Orientation.Vertical,
  265. Spacing = 20,
  266. Children = { current, candidate }
  267. };
  268. var window = new Window
  269. {
  270. [XYFocus.NavigationModesProperty] = XYFocusNavigationModes.Enabled,
  271. Content = new ScrollViewer
  272. {
  273. Content = parent
  274. },
  275. Height = 30
  276. };
  277. window.Show();
  278. Assert.Equal(candidate, KeyboardNavigationHandler.GetNext(current, NavigationDirection.Down));
  279. }
  280. [Theory]
  281. [InlineData(Key.Left, NavigationDirection.Left)]
  282. [InlineData(Key.Right, NavigationDirection.Right)]
  283. [InlineData(Key.Up, NavigationDirection.Up)]
  284. [InlineData(Key.Down, NavigationDirection.Down)]
  285. public void Arrow_Key_Should_Focus_Element(Key key, NavigationDirection direction)
  286. {
  287. using var _ = UnitTestApplication.Start(TestServices.FocusableWindow);
  288. var candidate = new Button();
  289. var current = new Button();
  290. current[direction switch
  291. {
  292. NavigationDirection.Left => XYFocus.LeftProperty,
  293. NavigationDirection.Right => XYFocus.RightProperty,
  294. NavigationDirection.Up => XYFocus.UpProperty,
  295. NavigationDirection.Down => XYFocus.DownProperty,
  296. _ => throw new ArgumentOutOfRangeException(nameof(direction), direction, null)
  297. }] = candidate;
  298. var window = new Window
  299. {
  300. [XYFocus.NavigationModesProperty] = XYFocusNavigationModes.Enabled,
  301. Content = new Canvas
  302. {
  303. Children = { current, candidate }
  304. }
  305. };
  306. window.Show();
  307. Assert.True(current.Focus());
  308. var args = new KeyEventArgs { RoutedEvent = InputElement.KeyDownEvent, Key = key, Source = current };
  309. window.RaiseEvent(args);
  310. Assert.Equal(candidate, FocusManager.GetFocusManager(current)!.GetFocusedElement());
  311. Assert.True(args.Handled);
  312. }
  313. [Theory]
  314. [InlineData(Key.Left)]
  315. [InlineData(Key.Right)]
  316. [InlineData(Key.Up)]
  317. [InlineData(Key.Down)]
  318. public void Arrow_Key_Should_Not_Be_Handled_If_No_Focus(Key key)
  319. {
  320. using var _ = UnitTestApplication.Start(TestServices.FocusableWindow);
  321. var current = new Button();
  322. var window = new Window
  323. {
  324. [XYFocus.NavigationModesProperty] = XYFocusNavigationModes.Enabled,
  325. Content = new Canvas
  326. {
  327. Children = { current }
  328. }
  329. };
  330. window.Show();
  331. Assert.True(current.Focus());
  332. var args = new KeyEventArgs { RoutedEvent = InputElement.KeyDownEvent, Key = key, Source = current };
  333. window.RaiseEvent(args);
  334. Assert.Equal(current, FocusManager.GetFocusManager(current)!.GetFocusedElement());
  335. Assert.False(args.Handled);
  336. }
  337. [Fact]
  338. public void Can_Focus_Child_Of_Current_Focused()
  339. {
  340. using var _ = UnitTestApplication.Start(TestServices.FocusableWindow);
  341. var candidate = new Button() { Height = 20, Width = 20 };
  342. var window = new Window
  343. {
  344. [XYFocus.NavigationModesProperty] = XYFocusNavigationModes.Enabled,
  345. Content = candidate,
  346. Height = 30
  347. };
  348. window.Show();
  349. Assert.Null(KeyboardNavigationHandler.GetNext(window, NavigationDirection.Down));
  350. }
  351. [Fact]
  352. public void Can_Focus_Any_Element_If_Nothing_Was_Focused()
  353. {
  354. // In the future we might auto-focus any element, but for now XY algorithm should be aware of Avalonia specifics.
  355. using var _ = UnitTestApplication.Start(TestServices.FocusableWindow);
  356. var candidate = new Button();
  357. var window = new Window
  358. {
  359. [XYFocus.NavigationModesProperty] = XYFocusNavigationModes.Enabled,
  360. Content = new Canvas
  361. {
  362. Children = { candidate }
  363. }
  364. };
  365. window.Show();
  366. Assert.Null(FocusManager.GetFocusManager(window)!.GetFocusedElement());
  367. var args = new KeyEventArgs { RoutedEvent = InputElement.KeyDownEvent, Key = Key.Down, Source = window };
  368. window.RaiseEvent(args);
  369. Assert.Equal(candidate, FocusManager.GetFocusManager(window)!.GetFocusedElement());
  370. }
  371. [Fact]
  372. public void Cannot_Focus_Across_XYFocus_Boundaries()
  373. {
  374. using var _ = UnitTestApplication.Start(TestServices.FocusableWindow);
  375. var current = new Button() { Height = 20 };
  376. var candidate = new Button() { Height = 20 };
  377. var currentParent = new StackPanel
  378. {
  379. [XYFocus.NavigationModesProperty] = XYFocusNavigationModes.Enabled,
  380. Orientation = Orientation.Vertical,
  381. Spacing = 20,
  382. Children = { current }
  383. };
  384. var candidateParent = new StackPanel
  385. {
  386. [XYFocus.NavigationModesProperty] = XYFocusNavigationModes.Enabled,
  387. Orientation = Orientation.Vertical,
  388. Spacing = 20,
  389. Children = { candidate }
  390. };
  391. var grandparent = new StackPanel
  392. {
  393. [XYFocus.NavigationModesProperty] = XYFocusNavigationModes.Disabled,
  394. Orientation = Orientation.Vertical,
  395. Spacing = 20,
  396. Children = { currentParent, candidateParent }
  397. };
  398. var window = new Window
  399. {
  400. [XYFocus.NavigationModesProperty] = XYFocusNavigationModes.Enabled,
  401. Content = grandparent,
  402. Height = 300
  403. };
  404. window.Show();
  405. Assert.Null(KeyboardNavigationHandler.GetNext(current, NavigationDirection.Down));
  406. }
  407. }