ApplicationPopoverTests.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. namespace Terminal.Gui.ApplicationTests;
  2. public class ApplicationPopoverTests
  3. {
  4. [Fact]
  5. public void Application_Init_Initializes_PopoverManager ()
  6. {
  7. // Arrange
  8. Assert.Null (Application.Popover);
  9. Application.Init (new FakeDriver ());
  10. // Act
  11. Assert.NotNull (Application.Popover);
  12. Application.ResetState (true);
  13. }
  14. [Fact]
  15. public void Application_Shutdown_Resets_PopoverManager ()
  16. {
  17. // Arrange
  18. Assert.Null (Application.Popover);
  19. Application.Init (new FakeDriver ());
  20. // Act
  21. Assert.NotNull (Application.Popover);
  22. Application.Shutdown ();
  23. // Test
  24. Assert.Null (Application.Popover);
  25. }
  26. [Fact]
  27. public void Application_End_Does_Not_Reset_PopoverManager ()
  28. {
  29. // Arrange
  30. Assert.Null (Application.Popover);
  31. Application.Init (new FakeDriver ());
  32. Assert.NotNull (Application.Popover);
  33. Application.Iteration += (s, a) => Application.RequestStop ();
  34. var top = new Toplevel ();
  35. RunState rs = Application.Begin (top);
  36. // Act
  37. Application.End (rs);
  38. // Test
  39. Assert.NotNull (Application.Popover);
  40. top.Dispose ();
  41. Application.Shutdown ();
  42. }
  43. [Fact]
  44. public void Application_End_Hides_Active ()
  45. {
  46. // Arrange
  47. Assert.Null (Application.Popover);
  48. Application.Init (new FakeDriver ());
  49. Application.Iteration += (s, a) => Application.RequestStop ();
  50. var top = new Toplevel ();
  51. RunState rs = Application.Begin (top);
  52. PopoverTestClass popover = new ();
  53. Application.Popover?.Show (popover);
  54. Assert.True (popover.Visible);
  55. // Act
  56. Application.End (rs);
  57. top.Dispose ();
  58. // Test
  59. Assert.False (popover.Visible);
  60. Assert.NotNull (Application.Popover);
  61. Application.Shutdown ();
  62. Assert.Equal (1, popover.DisposedCount);
  63. }
  64. [Fact]
  65. public void Application_Shutdown_Disposes_Registered_Popovers ()
  66. {
  67. // Arrange
  68. Assert.Null (Application.Popover);
  69. Application.Init (new FakeDriver ());
  70. PopoverTestClass popover = new ();
  71. // Act
  72. Application.Popover?.Register (popover);
  73. Application.Shutdown ();
  74. // Test
  75. Assert.Equal (1, popover.DisposedCount);
  76. }
  77. [Fact]
  78. public void Application_Shutdown_Does_Not_Dispose_DeRegistered_Popovers ()
  79. {
  80. // Arrange
  81. Assert.Null (Application.Popover);
  82. Application.Init (new FakeDriver ());
  83. PopoverTestClass popover = new ();
  84. Application.Popover?.Register (popover);
  85. // Act
  86. Application.Popover?.DeRegister (popover);
  87. Application.Shutdown ();
  88. // Test
  89. Assert.Equal (0, popover.DisposedCount);
  90. popover.Dispose ();
  91. Assert.Equal (1, popover.DisposedCount);
  92. }
  93. [Fact]
  94. public void Application_Shutdown_Does_Not_Dispose_ActiveNotRegistered_Popover ()
  95. {
  96. // Arrange
  97. Assert.Null (Application.Popover);
  98. Application.Init (new FakeDriver ());
  99. PopoverTestClass popover = new ();
  100. Application.Popover?.Show (popover);
  101. Application.Popover?.DeRegister (popover);
  102. // Act
  103. Application.Shutdown ();
  104. // Test
  105. Assert.Equal (0, popover.DisposedCount);
  106. popover.Dispose ();
  107. Assert.Equal (1, popover.DisposedCount);
  108. }
  109. public class PopoverTestClass : PopoverBaseImpl
  110. {
  111. public List<Key> HandledKeys { get; } = [];
  112. public int NewCommandInvokeCount { get; private set; }
  113. // NOTE: Hides the base DisposedCount property
  114. public new int DisposedCount { get; private set; }
  115. public PopoverTestClass ()
  116. {
  117. CanFocus = true;
  118. AddCommand (Command.New, NewCommandHandler);
  119. HotKeyBindings.Add (Key.N.WithCtrl, Command.New);
  120. return;
  121. bool? NewCommandHandler (ICommandContext ctx)
  122. {
  123. NewCommandInvokeCount++;
  124. return false;
  125. }
  126. }
  127. protected override bool OnKeyDown (Key key)
  128. {
  129. HandledKeys.Add (key);
  130. return false;
  131. }
  132. /// <inheritdoc/>
  133. protected override void Dispose (bool disposing)
  134. {
  135. base.Dispose (disposing);
  136. DisposedCount++;
  137. }
  138. }
  139. }