InputElement_Focus.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. using System;
  2. using Avalonia.Controls;
  3. using Avalonia.Input;
  4. using Avalonia.Interactivity;
  5. using Avalonia.UnitTests;
  6. using Xunit;
  7. namespace Avalonia.Base.UnitTests.Input
  8. {
  9. public class InputElement_Focus
  10. {
  11. [Fact]
  12. public void Focus_Should_Set_FocusManager_Current()
  13. {
  14. Button target;
  15. using (UnitTestApplication.Start(TestServices.RealFocus))
  16. {
  17. var root = new TestRoot
  18. {
  19. Child = target = new Button()
  20. };
  21. target.Focus();
  22. Assert.Same(target, FocusManager.Instance.Current);
  23. }
  24. }
  25. [Fact]
  26. public void Focus_Should_Not_Get_Restored_To_Enabled_Control()
  27. {
  28. using (UnitTestApplication.Start(TestServices.RealFocus))
  29. {
  30. var sp = new StackPanel();
  31. Button target = new Button();
  32. Button target1 = new Button();
  33. target.Click += (s, e) => target.IsEnabled = false;
  34. target1.Click += (s, e) => target.IsEnabled = true;
  35. sp.Children.Add(target);
  36. sp.Children.Add(target1);
  37. var root = new TestRoot
  38. {
  39. Child = sp
  40. };
  41. target.Focus();
  42. target.RaiseEvent(new RoutedEventArgs(AccessKeyHandler.AccessKeyPressedEvent));
  43. Assert.False(target.IsEnabled);
  44. Assert.False(target.IsFocused);
  45. target1.RaiseEvent(new RoutedEventArgs(AccessKeyHandler.AccessKeyPressedEvent));
  46. Assert.True(target.IsEnabled);
  47. Assert.False(target.IsFocused);
  48. }
  49. }
  50. [Fact]
  51. public void Focus_Should_Be_Cleared_When_Control_Is_Removed_From_VisualTree()
  52. {
  53. Button target;
  54. using (UnitTestApplication.Start(TestServices.RealFocus))
  55. {
  56. var root = new TestRoot
  57. {
  58. Child = target = new Button()
  59. };
  60. target.Focus();
  61. root.Child = null;
  62. Assert.Null(FocusManager.Instance.Current);
  63. }
  64. }
  65. [Fact]
  66. public void Focus_Pseudoclass_Should_Be_Applied_On_Focus()
  67. {
  68. using (UnitTestApplication.Start(TestServices.RealFocus))
  69. {
  70. var target1 = new Decorator();
  71. var target2 = new Decorator();
  72. var root = new TestRoot
  73. {
  74. Child = new StackPanel
  75. {
  76. Children =
  77. {
  78. target1,
  79. target2
  80. }
  81. }
  82. };
  83. target1.ApplyTemplate();
  84. target2.ApplyTemplate();
  85. FocusManager.Instance?.Focus(target1);
  86. Assert.True(target1.IsFocused);
  87. Assert.True(target1.Classes.Contains(":focus"));
  88. Assert.False(target2.IsFocused);
  89. Assert.False(target2.Classes.Contains(":focus"));
  90. FocusManager.Instance?.Focus(target2, NavigationMethod.Tab);
  91. Assert.False(target1.IsFocused);
  92. Assert.False(target1.Classes.Contains(":focus"));
  93. Assert.True(target2.IsFocused);
  94. Assert.True(target2.Classes.Contains(":focus"));
  95. }
  96. }
  97. [Fact]
  98. public void Control_FocusVsisible_Pseudoclass_Should_Be_Applied_On_Tab_And_DirectionalFocus()
  99. {
  100. using (UnitTestApplication.Start(TestServices.RealFocus))
  101. {
  102. var target1 = new Decorator();
  103. var target2 = new Decorator();
  104. var root = new TestRoot
  105. {
  106. Child = new StackPanel
  107. {
  108. Children =
  109. {
  110. target1,
  111. target2
  112. }
  113. }
  114. };
  115. target1.ApplyTemplate();
  116. target2.ApplyTemplate();
  117. FocusManager.Instance?.Focus(target1);
  118. Assert.True(target1.IsFocused);
  119. Assert.False(target1.Classes.Contains(":focus-visible"));
  120. Assert.False(target2.IsFocused);
  121. Assert.False(target2.Classes.Contains(":focus-visible"));
  122. FocusManager.Instance?.Focus(target2, NavigationMethod.Tab);
  123. Assert.False(target1.IsFocused);
  124. Assert.False(target1.Classes.Contains(":focus-visible"));
  125. Assert.True(target2.IsFocused);
  126. Assert.True(target2.Classes.Contains(":focus-visible"));
  127. FocusManager.Instance?.Focus(target1, NavigationMethod.Directional);
  128. Assert.True(target1.IsFocused);
  129. Assert.True(target1.Classes.Contains(":focus-visible"));
  130. Assert.False(target2.IsFocused);
  131. Assert.False(target2.Classes.Contains(":focus-visible"));
  132. }
  133. }
  134. [Fact]
  135. public void Control_FocusWithin_PseudoClass_Should_Be_Applied()
  136. {
  137. using (UnitTestApplication.Start(TestServices.RealFocus))
  138. {
  139. var target1 = new Decorator();
  140. var target2 = new Decorator();
  141. var root = new TestRoot
  142. {
  143. Child = new StackPanel
  144. {
  145. Children =
  146. {
  147. target1,
  148. target2
  149. }
  150. }
  151. };
  152. target1.ApplyTemplate();
  153. target2.ApplyTemplate();
  154. FocusManager.Instance?.Focus(target1);
  155. Assert.True(target1.IsFocused);
  156. Assert.True(target1.Classes.Contains(":focus-within"));
  157. Assert.True(target1.IsKeyboardFocusWithin);
  158. Assert.True(root.Child.Classes.Contains(":focus-within"));
  159. Assert.True(root.Child.IsKeyboardFocusWithin);
  160. Assert.True(root.Classes.Contains(":focus-within"));
  161. Assert.True(root.IsKeyboardFocusWithin);
  162. }
  163. }
  164. [Fact]
  165. public void Control_FocusWithin_PseudoClass_Should_Be_Applied_and_Removed()
  166. {
  167. using (UnitTestApplication.Start(TestServices.RealFocus))
  168. {
  169. var target1 = new Decorator();
  170. var target2 = new Decorator();
  171. var panel1 = new Panel { Children = { target1 } };
  172. var panel2 = new Panel { Children = { target2 } };
  173. var root = new TestRoot
  174. {
  175. Child = new StackPanel
  176. {
  177. Children =
  178. {
  179. panel1,
  180. panel2
  181. }
  182. }
  183. };
  184. target1.ApplyTemplate();
  185. target2.ApplyTemplate();
  186. FocusManager.Instance?.Focus(target1);
  187. Assert.True(target1.IsFocused);
  188. Assert.True(target1.Classes.Contains(":focus-within"));
  189. Assert.True(target1.IsKeyboardFocusWithin);
  190. Assert.True(panel1.Classes.Contains(":focus-within"));
  191. Assert.True(panel1.IsKeyboardFocusWithin);
  192. Assert.True(root.Child.Classes.Contains(":focus-within"));
  193. Assert.True(root.Child.IsKeyboardFocusWithin);
  194. Assert.True(root.Classes.Contains(":focus-within"));
  195. Assert.True(root.IsKeyboardFocusWithin);
  196. FocusManager.Instance?.Focus(target2);
  197. Assert.False(target1.IsFocused);
  198. Assert.False(target1.Classes.Contains(":focus-within"));
  199. Assert.False(target1.IsKeyboardFocusWithin);
  200. Assert.False(panel1.Classes.Contains(":focus-within"));
  201. Assert.False(panel1.IsKeyboardFocusWithin);
  202. Assert.True(root.Child.Classes.Contains(":focus-within"));
  203. Assert.True(root.Child.IsKeyboardFocusWithin);
  204. Assert.True(root.Classes.Contains(":focus-within"));
  205. Assert.True(root.IsKeyboardFocusWithin);
  206. Assert.True(target2.IsFocused);
  207. Assert.True(target2.Classes.Contains(":focus-within"));
  208. Assert.True(target2.IsKeyboardFocusWithin);
  209. Assert.True(panel2.Classes.Contains(":focus-within"));
  210. Assert.True(panel2.IsKeyboardFocusWithin);
  211. }
  212. }
  213. [Fact]
  214. public void Control_FocusWithin_Pseudoclass_Should_Be_Removed_When_Removed_From_Tree()
  215. {
  216. using (UnitTestApplication.Start(TestServices.RealFocus))
  217. {
  218. var target1 = new Decorator();
  219. var target2 = new Decorator();
  220. var root = new TestRoot
  221. {
  222. Child = new StackPanel
  223. {
  224. Children =
  225. {
  226. target1,
  227. target2
  228. }
  229. }
  230. };
  231. target1.ApplyTemplate();
  232. target2.ApplyTemplate();
  233. FocusManager.Instance?.Focus(target1);
  234. Assert.True(target1.IsFocused);
  235. Assert.True(target1.Classes.Contains(":focus-within"));
  236. Assert.True(target1.IsKeyboardFocusWithin);
  237. Assert.True(root.Child.Classes.Contains(":focus-within"));
  238. Assert.True(root.Child.IsKeyboardFocusWithin);
  239. Assert.True(root.Classes.Contains(":focus-within"));
  240. Assert.True(root.IsKeyboardFocusWithin);
  241. Assert.Equal(KeyboardDevice.Instance.FocusedElement, target1);
  242. root.Child = null;
  243. Assert.Null(KeyboardDevice.Instance.FocusedElement);
  244. Assert.False(target1.IsFocused);
  245. Assert.False(target1.Classes.Contains(":focus-within"));
  246. Assert.False(target1.IsKeyboardFocusWithin);
  247. Assert.False(root.Classes.Contains(":focus-within"));
  248. Assert.False(root.IsKeyboardFocusWithin);
  249. }
  250. }
  251. [Fact]
  252. public void Control_FocusWithin_Pseudoclass_Should_Be_Removed_Focus_Moves_To_Different_Root()
  253. {
  254. using (UnitTestApplication.Start(TestServices.RealFocus))
  255. {
  256. var target1 = new Decorator();
  257. var target2 = new Decorator();
  258. var root1 = new TestRoot
  259. {
  260. Child = new StackPanel
  261. {
  262. Children =
  263. {
  264. target1,
  265. }
  266. }
  267. };
  268. var root2 = new TestRoot
  269. {
  270. Child = new StackPanel
  271. {
  272. Children =
  273. {
  274. target2,
  275. }
  276. }
  277. };
  278. target1.ApplyTemplate();
  279. target2.ApplyTemplate();
  280. FocusManager.Instance?.Focus(target1);
  281. Assert.True(target1.IsFocused);
  282. Assert.True(target1.Classes.Contains(":focus-within"));
  283. Assert.True(target1.IsKeyboardFocusWithin);
  284. Assert.True(root1.Child.Classes.Contains(":focus-within"));
  285. Assert.True(root1.Child.IsKeyboardFocusWithin);
  286. Assert.True(root1.Classes.Contains(":focus-within"));
  287. Assert.True(root1.IsKeyboardFocusWithin);
  288. Assert.Equal(KeyboardDevice.Instance.FocusedElement, target1);
  289. FocusManager.Instance?.Focus(target2);
  290. Assert.False(target1.IsFocused);
  291. Assert.False(target1.Classes.Contains(":focus-within"));
  292. Assert.False(target1.IsKeyboardFocusWithin);
  293. Assert.False(root1.Child.Classes.Contains(":focus-within"));
  294. Assert.False(root1.Child.IsKeyboardFocusWithin);
  295. Assert.False(root1.Classes.Contains(":focus-within"));
  296. Assert.False(root1.IsKeyboardFocusWithin);
  297. Assert.True(target2.IsFocused);
  298. Assert.True(target2.Classes.Contains(":focus-within"));
  299. Assert.True(target2.IsKeyboardFocusWithin);
  300. Assert.True(root2.Child.Classes.Contains(":focus-within"));
  301. Assert.True(root2.Child.IsKeyboardFocusWithin);
  302. Assert.True(root2.Classes.Contains(":focus-within"));
  303. Assert.True(root2.IsKeyboardFocusWithin);
  304. }
  305. }
  306. [Fact]
  307. public void Can_Clear_Focus()
  308. {
  309. Button target;
  310. using (UnitTestApplication.Start(TestServices.RealFocus))
  311. {
  312. var root = new TestRoot
  313. {
  314. Child = target = new Button()
  315. };
  316. target.Focus();
  317. FocusManager.Instance.Focus(null);
  318. Assert.Null(FocusManager.Instance.Current);
  319. }
  320. }
  321. }
  322. }