ViewCommandTests.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. namespace UnitTests.ViewBaseTests;
  2. public class ViewCommandTests
  3. {
  4. // See https://github.com/gui-cs/Terminal.Gui/issues/3913
  5. [Fact]
  6. public void Button_IsDefault_Raises_Accepted_Correctly ()
  7. {
  8. var aAcceptedCount = 0;
  9. var aCancelAccepting = false;
  10. var bAcceptedCount = 0;
  11. var bCancelAccepting = false;
  12. var w = new Window
  13. {
  14. BorderStyle = LineStyle.None,
  15. Width = 10,
  16. Height = 10
  17. };
  18. var btnA = new Button
  19. {
  20. Width = 3,
  21. IsDefault = true
  22. };
  23. btnA.Accepting += (s, e) =>
  24. {
  25. aAcceptedCount++;
  26. e.Handled = aCancelAccepting;
  27. };
  28. var btnB = new Button
  29. {
  30. Width = 3,
  31. X = Pos.Right (btnA)
  32. };
  33. btnB.Accepting += (s, e) =>
  34. {
  35. bAcceptedCount++;
  36. e.Handled = bCancelAccepting;
  37. };
  38. w.Add (btnA, btnB);
  39. w.LayoutSubViews ();
  40. Application.Begin (w);
  41. Assert.Same (Application.TopRunnableView, w);
  42. // Click button 2
  43. Rectangle btn2Frame = btnB.FrameToScreen ();
  44. Application.RaiseMouseEvent (
  45. new()
  46. {
  47. ScreenPosition = btn2Frame.Location,
  48. Flags = MouseFlags.Button1Clicked
  49. });
  50. // Button A should have been accepted because B didn't cancel and A IsDefault
  51. Assert.Equal (1, aAcceptedCount);
  52. Assert.Equal (1, bAcceptedCount);
  53. bCancelAccepting = true;
  54. Application.RaiseMouseEvent (
  55. new()
  56. {
  57. ScreenPosition = btn2Frame.Location,
  58. Flags = MouseFlags.Button1Clicked
  59. });
  60. // Button A (IsDefault) should NOT have been accepted because B canceled
  61. Assert.Equal (1, aAcceptedCount);
  62. Assert.Equal (2, bAcceptedCount);
  63. Application.ResetState (true);
  64. }
  65. // See: https://github.com/gui-cs/Terminal.Gui/issues/3905
  66. [Fact]
  67. [SetupFakeApplication]
  68. public void Button_CanFocus_False_Raises_Accepted_Correctly ()
  69. {
  70. var wAcceptedCount = 0;
  71. var wCancelAccepting = false;
  72. var w = new Window
  73. {
  74. Title = "Window",
  75. BorderStyle = LineStyle.None,
  76. Width = 10,
  77. Height = 10
  78. };
  79. w.Accepting += (s, e) =>
  80. {
  81. wAcceptedCount++;
  82. e.Handled = wCancelAccepting;
  83. };
  84. var btnAcceptedCount = 0;
  85. var btnCancelAccepting = true;
  86. var btn = new Button
  87. {
  88. Title = "Button",
  89. Width = 3,
  90. IsDefault = true
  91. };
  92. btn.CanFocus = true;
  93. btn.Accepting += (s, e) =>
  94. {
  95. btnAcceptedCount++;
  96. e.Handled = btnCancelAccepting;
  97. };
  98. w.Add (btn);
  99. Application.Begin (w);
  100. Assert.Same (Application.TopRunnableView, w);
  101. w.LayoutSubViews ();
  102. // Click button just like a driver would
  103. Rectangle btnFrame = btn.FrameToScreen ();
  104. Application.RaiseMouseEvent (
  105. new()
  106. {
  107. ScreenPosition = btnFrame.Location,
  108. Flags = MouseFlags.Button1Pressed
  109. });
  110. Application.RaiseMouseEvent (
  111. new()
  112. {
  113. ScreenPosition = btnFrame.Location,
  114. Flags = MouseFlags.Button1Released
  115. });
  116. Application.RaiseMouseEvent (
  117. new()
  118. {
  119. ScreenPosition = btnFrame.Location,
  120. Flags = MouseFlags.Button1Clicked
  121. });
  122. Assert.Equal (1, btnAcceptedCount);
  123. Assert.Equal (0, wAcceptedCount);
  124. // The above grabbed the mouse. Need to ungrab.
  125. Application.Mouse.UngrabMouse ();
  126. w.Dispose ();
  127. Application.ResetState (true);
  128. }
  129. }