CursorTests.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ApplicationTests;
  3. public class CursorTests
  4. {
  5. private readonly ITestOutputHelper _output;
  6. public CursorTests (ITestOutputHelper output)
  7. {
  8. _output = output;
  9. ConsoleDriver.RunningUnitTests = true;
  10. }
  11. private class TestView : View
  12. {
  13. public Point? TestLocation { get; set; }
  14. /// <inheritdoc />
  15. public override Point? PositionCursor ()
  16. {
  17. if (TestLocation.HasValue && HasFocus)
  18. {
  19. Driver.SetCursorVisibility (CursorVisibility.Default);
  20. }
  21. return TestLocation;
  22. }
  23. }
  24. [Fact]
  25. [SetupFakeDriver]
  26. public void PositionCursor_No_Focus_Returns_False ()
  27. {
  28. Assert.False (Application.PositionCursor (null));
  29. TestView view = new ()
  30. {
  31. CanFocus = false,
  32. Width = 1,
  33. Height = 1,
  34. };
  35. view.TestLocation = new Point (0, 0);
  36. Assert.False (Application.PositionCursor (view));
  37. }
  38. [Fact]
  39. [SetupFakeDriver]
  40. public void PositionCursor_No_Position_Returns_False ()
  41. {
  42. TestView view = new ()
  43. {
  44. CanFocus = false,
  45. Width = 1,
  46. Height = 1,
  47. };
  48. view.CanFocus = true;
  49. view.SetFocus();
  50. Assert.False (Application.PositionCursor (view));
  51. }
  52. [Fact]
  53. [SetupFakeDriver]
  54. public void PositionCursor_No_IntersectSuperView_Returns_False ()
  55. {
  56. View superView = new ()
  57. {
  58. Width = 1,
  59. Height = 1,
  60. };
  61. TestView view = new ()
  62. {
  63. CanFocus = false,
  64. X = 1,
  65. Y =1,
  66. Width = 1,
  67. Height = 1,
  68. };
  69. superView.Add (view);
  70. view.CanFocus = true;
  71. view.SetFocus ();
  72. view.TestLocation = new Point (0, 0);
  73. Assert.False (Application.PositionCursor (view));
  74. }
  75. [Fact]
  76. [SetupFakeDriver]
  77. public void PositionCursor_Position_OutSide_SuperView_Returns_False ()
  78. {
  79. View superView = new ()
  80. {
  81. Width = 1,
  82. Height = 1,
  83. };
  84. TestView view = new ()
  85. {
  86. CanFocus = false,
  87. X = 0,
  88. Y = 0,
  89. Width = 2,
  90. Height = 2,
  91. };
  92. superView.Add (view);
  93. view.CanFocus = true;
  94. view.SetFocus ();
  95. view.TestLocation = new Point (1, 1);
  96. Assert.False (Application.PositionCursor (view));
  97. }
  98. [Fact, Trait("BUGBUG", "Views without subviews don't support Focused or MostFocused")]
  99. [SetupFakeDriver]
  100. public void PositionCursor_Focused_With_Position_Returns_True ()
  101. {
  102. TestView view = new ()
  103. {
  104. CanFocus = false,
  105. Width = 1,
  106. Height = 1,
  107. };
  108. view.CanFocus = true;
  109. view.SetFocus ();
  110. view.TestLocation = new Point (0, 0);
  111. Assert.False (Application.PositionCursor (view)); // BUGBUG: This should be true
  112. }
  113. [Fact]
  114. [SetupFakeDriver]
  115. public void PositionCursor_Defaults_Invisible ()
  116. {
  117. View view = new ()
  118. {
  119. CanFocus = true,
  120. Width = 1,
  121. Height = 1,
  122. };
  123. view.SetFocus();
  124. Assert.True (view.HasFocus);
  125. Assert.False (Application.PositionCursor (view));
  126. Application.Driver.GetCursorVisibility (out CursorVisibility cursor);
  127. Assert.Equal (CursorVisibility.Invisible, cursor);
  128. }
  129. }