MouseTests.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using Terminal.Gui.ViewsTests;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.ViewTests;
  4. public class MouseTests (ITestOutputHelper output)
  5. {
  6. [Theory]
  7. [InlineData (false, false, false)]
  8. [InlineData (true, false, true)]
  9. [InlineData (true, true, true)]
  10. public void MouseClick_SetsFocus_If_CanFocus (bool canFocus, bool setFocus, bool expectedHasFocus)
  11. {
  12. var superView = new View { CanFocus = true, Height = 1, Width = 15 };
  13. var focusedView = new View { CanFocus = true, Width = 1, Height = 1 };
  14. var testView = new View { CanFocus = canFocus, X = 4, Width = 4, Height = 1 };
  15. superView.Add (focusedView, testView);
  16. focusedView.SetFocus ();
  17. Assert.True (superView.HasFocus);
  18. Assert.True (focusedView.HasFocus);
  19. Assert.False (testView.HasFocus);
  20. if (setFocus)
  21. {
  22. testView.SetFocus ();
  23. }
  24. testView.OnMouseEvent (new () { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked });
  25. Assert.True (superView.HasFocus);
  26. Assert.Equal (expectedHasFocus, testView.HasFocus);
  27. }
  28. // TODO: Add more tests that ensure the above test works with positive adornments
  29. // Test drag to move
  30. [Theory]
  31. [InlineData (0, 0, 0, 0, false)]
  32. [InlineData (0, 0, 0, 4, false)]
  33. [InlineData (1, 0, 0, 4, true)]
  34. [InlineData (0, 1, 0, 4, true)]
  35. [InlineData (0, 0, 1, 4, false)]
  36. [InlineData (1, 1, 0, 3, false)]
  37. [InlineData (1, 1, 0, 4, true)]
  38. [InlineData (1, 1, 0, 5, true)]
  39. [InlineData (1, 1, 0, 6, false)]
  40. [InlineData (1, 1, 0, 11, false)]
  41. [InlineData (1, 1, 0, 12, true)]
  42. [InlineData (1, 1, 0, 13, true)]
  43. [InlineData (1, 1, 0, 14, false)]
  44. [AutoInitShutdown]
  45. public void ButtonPressed_In_Margin_Or_Border_Starts_Drag (int marginThickness, int borderThickness, int paddingThickness, int xy, bool expectedMoved)
  46. {
  47. var testView = new View
  48. {
  49. CanFocus = true,
  50. X = 4,
  51. Y = 4,
  52. Width = 10,
  53. Height = 10,
  54. Arrangement = ViewArrangement.Movable
  55. };
  56. testView.Margin.Thickness = new (marginThickness);
  57. testView.Border.Thickness = new (borderThickness);
  58. testView.Padding.Thickness = new (paddingThickness);
  59. var top = new Toplevel ();
  60. top.Add (testView);
  61. Application.Begin (top);
  62. Assert.Equal (new Point (4, 4), testView.Frame.Location);
  63. Application.OnMouseEvent (new (new () { X = xy, Y = xy, Flags = MouseFlags.Button1Pressed }));
  64. Assert.False (Application.MouseGrabView is { } && (Application.MouseGrabView != testView.Margin && Application.MouseGrabView != testView.Border));
  65. Application.OnMouseEvent (new (new () { X = xy + 1, Y = xy + 1, Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition }));
  66. Assert.Equal (expectedMoved, new Point (5, 5) == testView.Frame.Location);
  67. }
  68. [Theory]
  69. [InlineData (MouseFlags.WheeledUp | MouseFlags.ButtonCtrl, MouseFlags.WheeledLeft)]
  70. [InlineData (MouseFlags.WheeledDown | MouseFlags.ButtonCtrl, MouseFlags.WheeledRight)]
  71. public void WheeledLeft_WheeledRight (MouseFlags mouseFlags, MouseFlags expectedMouseFlagsFromEvent)
  72. {
  73. MouseFlags mouseFlagsFromEvent = MouseFlags.None;
  74. var view = new View ();
  75. view.MouseEvent += (s, e) => mouseFlagsFromEvent = e.MouseEvent.Flags;
  76. view.OnMouseEvent (new MouseEvent () { Flags = mouseFlags });
  77. Assert.Equal (mouseFlagsFromEvent, expectedMouseFlagsFromEvent);
  78. }
  79. public static TheoryData<View, string> AllViews => TestHelpers.GetAllViewsTheoryData ();
  80. [Theory]
  81. [MemberData (nameof (AllViews))]
  82. public void AllViews_Enter_Leave_Events (View view, string viewName)
  83. {
  84. if (view == null)
  85. {
  86. output.WriteLine ($"Ignoring {viewName} - It's a Generic");
  87. return;
  88. }
  89. if (!view.CanFocus)
  90. {
  91. output.WriteLine ($"Ignoring {viewName} - It can't focus.");
  92. return;
  93. }
  94. if (view is Toplevel && ((Toplevel)view).Modal)
  95. {
  96. output.WriteLine ($"Ignoring {viewName} - It's a Modal Toplevel");
  97. return;
  98. }
  99. Application.Init (new FakeDriver ());
  100. Toplevel top = new ()
  101. {
  102. Height = 10,
  103. Width = 10
  104. };
  105. View otherView = new ()
  106. {
  107. X = 0, Y = 0,
  108. Height = 1,
  109. Width = 1,
  110. CanFocus = true,
  111. };
  112. view.AutoSize = false;
  113. view.X = Pos.Right(otherView);
  114. view.Y = 0;
  115. view.Width = 10;
  116. view.Height = 1;
  117. var nEnter = 0;
  118. var nLeave = 0;
  119. view.Enter += (s, e) => nEnter++;
  120. view.Leave += (s, e) => nLeave++;
  121. top.Add (view, otherView);
  122. Application.Begin (top);
  123. // Start with the focus on our test view
  124. view.SetFocus ();
  125. Assert.Equal (1, nEnter);
  126. Assert.Equal (0, nLeave);
  127. // Use keyboard to navigate to next view (otherView).
  128. if (view is TextView)
  129. {
  130. top.NewKeyDownEvent (Key.Tab.WithCtrl);
  131. }
  132. else if (view is DatePicker)
  133. {
  134. for (var i = 0; i < 4; i++)
  135. {
  136. top.NewKeyDownEvent (Key.Tab.WithCtrl);
  137. }
  138. }
  139. else
  140. {
  141. top.NewKeyDownEvent (Key.Tab);
  142. }
  143. Assert.Equal (1, nEnter);
  144. Assert.Equal (1, nLeave);
  145. top.NewKeyDownEvent (Key.Tab);
  146. Assert.Equal (2, nEnter);
  147. Assert.Equal (1, nLeave);
  148. top.Dispose ();
  149. Application.Shutdown ();
  150. }
  151. [Theory]
  152. [MemberData (nameof (AllViews))]
  153. public void AllViews_Enter_Leave_Events_Visible_False (View view, string viewName)
  154. {
  155. if (view == null)
  156. {
  157. output.WriteLine ($"Ignoring {viewName} - It's a Generic");
  158. return;
  159. }
  160. if (!view.CanFocus)
  161. {
  162. output.WriteLine ($"Ignoring {viewName} - It can't focus.");
  163. return;
  164. }
  165. if (view is Toplevel && ((Toplevel)view).Modal)
  166. {
  167. output.WriteLine ($"Ignoring {viewName} - It's a Modal Toplevel");
  168. return;
  169. }
  170. Application.Init (new FakeDriver ());
  171. Toplevel top = new ()
  172. {
  173. Height = 10,
  174. Width = 10
  175. };
  176. View otherView = new ()
  177. {
  178. X = 0, Y = 0,
  179. Height = 1,
  180. Width = 1,
  181. CanFocus = true,
  182. };
  183. view.Visible = false;
  184. view.AutoSize = false;
  185. view.X = Pos.Right (otherView);
  186. view.Y = 0;
  187. view.Width = 10;
  188. view.Height = 1;
  189. var nEnter = 0;
  190. var nLeave = 0;
  191. view.Enter += (s, e) => nEnter++;
  192. view.Leave += (s, e) => nLeave++;
  193. top.Add (view, otherView);
  194. Application.Begin (top);
  195. // Start with the focus on our test view
  196. view.SetFocus ();
  197. Assert.Equal (0, nEnter);
  198. Assert.Equal (0, nLeave);
  199. // Use keyboard to navigate to next view (otherView).
  200. if (view is TextView)
  201. {
  202. top.NewKeyDownEvent (Key.Tab.WithCtrl);
  203. }
  204. else if (view is DatePicker)
  205. {
  206. for (var i = 0; i < 4; i++)
  207. {
  208. top.NewKeyDownEvent (Key.Tab.WithCtrl);
  209. }
  210. }
  211. else
  212. {
  213. top.NewKeyDownEvent (Key.Tab);
  214. }
  215. Assert.Equal (0, nEnter);
  216. Assert.Equal (0, nLeave);
  217. top.NewKeyDownEvent (Key.Tab);
  218. Assert.Equal (0, nEnter);
  219. Assert.Equal (0, nLeave);
  220. top.Dispose ();
  221. Application.Shutdown ();
  222. }
  223. [Theory]
  224. [MemberData (nameof (AllViews))]
  225. public void AllViews_OnMouseEvent_Enabled_False_Does_Not_Set_Handled (View view, string viewName)
  226. {
  227. if (view == null)
  228. {
  229. output.WriteLine ($"Ignoring {viewName} - It's a Generic");
  230. return;
  231. }
  232. view.Enabled = false;
  233. var me = new MouseEvent ();
  234. view.OnMouseEvent (me);
  235. Assert.False (me.Handled);
  236. view.Dispose ();
  237. }
  238. [Theory]
  239. [MemberData (nameof (AllViews))]
  240. public void AllViews_OnMouseClick_Enabled_False_Does_Not_Set_Handled (View view, string viewName)
  241. {
  242. if (view == null)
  243. {
  244. output.WriteLine ($"Ignoring {viewName} - It's a Generic");
  245. return;
  246. }
  247. view.Enabled = false;
  248. var me = new MouseEvent ()
  249. {
  250. Flags = MouseFlags.Button1Clicked
  251. };
  252. view.OnMouseEvent (me);
  253. Assert.False (me.Handled);
  254. view.Dispose ();
  255. }
  256. }