ViewCommandTests.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. namespace UnitTests.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. 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.Top = w;
  41. Application.TopLevels.Push (w);
  42. Assert.Same (Application.Top, w);
  43. // Click button 2
  44. Rectangle btn2Frame = btnB.FrameToScreen ();
  45. Application.RaiseMouseEvent (
  46. new()
  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()
  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 (Skip = "Failing as part of ##4270. Disabling temporarily.")]
  68. public void Button_CanFocus_False_Raises_Accepted_Correctly ()
  69. {
  70. Application.Init (new FakeDriver ());
  71. var wAcceptedCount = 0;
  72. var wCancelAccepting = false;
  73. var w = new Window
  74. {
  75. Title = "Window",
  76. BorderStyle = LineStyle.None,
  77. Width = 10,
  78. Height = 10
  79. };
  80. w.Accepting += (s, e) =>
  81. {
  82. wAcceptedCount++;
  83. e.Handled = wCancelAccepting;
  84. };
  85. var btnAcceptedCount = 0;
  86. var btnCancelAccepting = true;
  87. var btn = new Button
  88. {
  89. Title = "Button",
  90. Width = 3,
  91. IsDefault = true
  92. };
  93. btn.CanFocus = true;
  94. btn.Accepting += (s, e) =>
  95. {
  96. btnAcceptedCount++;
  97. e.Handled = btnCancelAccepting;
  98. };
  99. w.Add (btn);
  100. Application.Top = w;
  101. Application.TopLevels.Push (w);
  102. Assert.Same (Application.Top, w);
  103. w.LayoutSubViews ();
  104. // Click button just like a driver would
  105. Rectangle btnFrame = btn.FrameToScreen ();
  106. Application.RaiseMouseEvent (
  107. new()
  108. {
  109. ScreenPosition = btnFrame.Location,
  110. Flags = MouseFlags.Button1Pressed
  111. });
  112. Application.RaiseMouseEvent (
  113. new()
  114. {
  115. ScreenPosition = btnFrame.Location,
  116. Flags = MouseFlags.Button1Released
  117. });
  118. Application.RaiseMouseEvent (
  119. new()
  120. {
  121. ScreenPosition = btnFrame.Location,
  122. Flags = MouseFlags.Button1Clicked
  123. });
  124. Assert.Equal (1, btnAcceptedCount);
  125. Assert.Equal (0, wAcceptedCount);
  126. Application.ResetState (true);
  127. }
  128. }