ViewCommandTests.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. namespace Terminal.Gui.ViewTests;
  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. int aAcceptedCount = 0;
  9. bool aCancelAccepting = false;
  10. int bAcceptedCount = 0;
  11. bool 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.Cancel = 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.Cancel = bCancelAccepting;
  37. };
  38. w.Add (btnA, btnB);
  39. w.LayoutSubViews ();
  40. Application.Top = w;
  41. Application.TopLevels.Push(w);
  42. Assert.Same (Application.Top, w);
  43. // Click button 2
  44. var btn2Frame = btnB.FrameToScreen ();
  45. Application.RaiseMouseEvent (
  46. new MouseEventArgs ()
  47. {
  48. ScreenPosition = btn2Frame.Location,
  49. Flags = MouseFlags.Button1Clicked
  50. });
  51. // Button A should have been accepted because B didn't cancel and A IsDefault
  52. Assert.Equal (1, aAcceptedCount);
  53. Assert.Equal (1, bAcceptedCount);
  54. bCancelAccepting = true;
  55. Application.RaiseMouseEvent (
  56. new MouseEventArgs ()
  57. {
  58. ScreenPosition = btn2Frame.Location,
  59. Flags = MouseFlags.Button1Clicked
  60. });
  61. // Button A (IsDefault) should NOT have been accepted because B canceled
  62. Assert.Equal (1, aAcceptedCount);
  63. Assert.Equal (2, bAcceptedCount);
  64. Application.ResetState (true);
  65. }
  66. // See: https://github.com/gui-cs/Terminal.Gui/issues/3905
  67. [Fact]
  68. public void Button_CanFocus_False_Raises_Accepted_Correctly ()
  69. {
  70. int wAcceptedCount = 0;
  71. bool 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.Cancel = wCancelAccepting;
  83. };
  84. int btnAcceptedCount = 0;
  85. bool btnCancelAccepting = false;
  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.Cancel = btnCancelAccepting;
  97. };
  98. w.Add (btn);
  99. Application.Top = w;
  100. Application.TopLevels.Push (w);
  101. Assert.Same (Application.Top, w);
  102. w.LayoutSubViews ();
  103. // Click button just like a driver would
  104. var btnFrame = btn.FrameToScreen ();
  105. Application.RaiseMouseEvent (
  106. new MouseEventArgs ()
  107. {
  108. ScreenPosition = btnFrame.Location,
  109. Flags = MouseFlags.Button1Pressed
  110. });
  111. Application.RaiseMouseEvent (
  112. new MouseEventArgs ()
  113. {
  114. ScreenPosition = btnFrame.Location,
  115. Flags = MouseFlags.Button1Released
  116. });
  117. Application.RaiseMouseEvent (
  118. new MouseEventArgs ()
  119. {
  120. ScreenPosition = btnFrame.Location,
  121. Flags = MouseFlags.Button1Clicked
  122. });
  123. Assert.Equal (1, btnAcceptedCount);
  124. Assert.Equal (2, wAcceptedCount);
  125. Application.ResetState (true);
  126. }
  127. }