PopverMenuTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System.Reflection;
  2. using Terminal.Gui;
  3. using TerminalGuiFluentTesting;
  4. using Xunit.Abstractions;
  5. namespace IntegrationTests.FluentTests;
  6. /// <summary>
  7. /// Tests for the PopoverMenu class
  8. /// </summary>
  9. public class PopoverMenuTests (ITestOutputHelper outputHelper)
  10. {
  11. private readonly TextWriter _out = new TestOutputWriter (outputHelper);
  12. [Theory]
  13. [ClassData (typeof (V2TestDrivers))]
  14. public void EnableForDesign_CreatesMenuItems (V2TestDriver d)
  15. {
  16. using GuiTestContext c = With.A<Window> (80, 25, d)
  17. .Then (
  18. () =>
  19. {
  20. var popoverMenu = new PopoverMenu ();
  21. Application.Top!.Add (popoverMenu);
  22. // Call EnableForDesign
  23. Toplevel top = Application.Top;
  24. bool result = popoverMenu.EnableForDesign (ref top);
  25. // Should return true
  26. Assert.True (result);
  27. // Should have created menu items
  28. Assert.NotNull (popoverMenu.Root);
  29. Assert.Equal (7, popoverMenu.Root.SubViews.Count);
  30. // Should have Cut menu item
  31. View? cutMenuItem = popoverMenu.GetMenuItemsOfAllSubMenus ().FirstOrDefault (v => v?.Title == "Cu_t");
  32. Assert.NotNull (cutMenuItem);
  33. })
  34. .Stop ();
  35. }
  36. [Theory]
  37. [ClassData (typeof (V2TestDrivers))]
  38. public void Activate_Sets_Application_Navigation_Correctly (V2TestDriver d)
  39. {
  40. MenuBarv2? menuBar = null;
  41. using GuiTestContext c = With.A<Window> (50, 20, d)
  42. .Then (
  43. () =>
  44. {
  45. var popoverMenu = new PopoverMenu ();
  46. // Call EnableForDesign
  47. Toplevel top = Application.Top!;
  48. popoverMenu.EnableForDesign (ref top);
  49. View? view = new View ()
  50. {
  51. CanFocus = true,
  52. Height = Dim.Auto (),
  53. Width = Dim.Auto (),
  54. Id = "focusableView",
  55. Text = "View",
  56. };
  57. Application.Top!.Add (view);
  58. // EnableForDesign sets to true; undo that
  59. popoverMenu.Visible = false;
  60. Application.Popover!.Register (popoverMenu);
  61. view.SetFocus ();
  62. })
  63. .WaitIteration ()
  64. .Then (() => Assert.False (Application.Popover?.GetActivePopover () is PopoverMenu))
  65. .Then (() => Assert.IsNotType<MenuItemv2> (Application.Navigation!.GetFocused ()))
  66. .ScreenShot ("PopoverMenu initial state", _out)
  67. .Then (() => Application.Popover!.Show (Application.Popover.Popovers.First ()))
  68. .WaitIteration ()
  69. .ScreenShot ($"After Show", _out)
  70. .Then (() => Assert.True (Application.Popover?.GetActivePopover () is PopoverMenu))
  71. .Then (() => Assert.Equal ("Cu_t", Application.Navigation!.GetFocused ()!.Title))
  72. .WriteOutLogs (_out)
  73. .Stop ();
  74. }
  75. [Theory]
  76. [ClassData (typeof (V2TestDrivers))]
  77. public void QuitKey_Hides (V2TestDriver d)
  78. {
  79. MenuBarv2? menuBar = null;
  80. using GuiTestContext c = With.A<Window> (50, 20, d)
  81. .Then (
  82. () =>
  83. {
  84. var popoverMenu = new PopoverMenu ();
  85. // Call EnableForDesign
  86. Toplevel top = Application.Top!;
  87. bool result = popoverMenu.EnableForDesign (ref top);
  88. View? view = new View ()
  89. {
  90. CanFocus = true,
  91. Height = Dim.Auto (),
  92. Width = Dim.Auto (),
  93. Id = "focusableView",
  94. Text = "View",
  95. };
  96. Application.Top!.Add (view);
  97. // EnableForDesign sets to true; undo that
  98. popoverMenu.Visible = false;
  99. Application.Popover!.Register (popoverMenu);
  100. view.SetFocus ();
  101. })
  102. .WaitIteration ()
  103. .ScreenShot ("PopoverMenu initial state", _out)
  104. .Then (() => Assert.False (Application.Popover?.GetActivePopover () is PopoverMenu))
  105. .Then (() => Application.Popover!.Show (Application.Popover.Popovers.First ()))
  106. .WaitIteration ()
  107. .ScreenShot ($"After Show", _out)
  108. .Then (() => Assert.True (Application.Popover?.GetActivePopover () is PopoverMenu))
  109. .RaiseKeyDownEvent (Application.QuitKey)
  110. .Then (() => Application.LayoutAndDraw (true))
  111. .WaitIteration ()
  112. .WriteOutLogs (_out)
  113. .ScreenShot ($"After {Application.QuitKey}", _out)
  114. .Then (() => Assert.False (Application.Popover!.Popovers.Cast<PopoverMenu> ().FirstOrDefault()!.Visible))
  115. .Then (() => Assert.Null (Application.Popover!.GetActivePopover()))
  116. .Then (() => Assert.True (Application.Top!.Running))
  117. .WriteOutLogs (_out)
  118. .Stop ();
  119. }
  120. [Theory]
  121. [ClassData (typeof (V2TestDrivers))]
  122. public void QuitKey_Restores_Focus_Correctly (V2TestDriver d)
  123. {
  124. MenuBarv2? menuBar = null;
  125. using GuiTestContext c = With.A<Window> (50, 20, d)
  126. .Then (
  127. () =>
  128. {
  129. var popoverMenu = new PopoverMenu ();
  130. // Call EnableForDesign
  131. Toplevel top = Application.Top!;
  132. bool result = popoverMenu.EnableForDesign (ref top);
  133. View? view = new View ()
  134. {
  135. CanFocus = true,
  136. Height = Dim.Auto (),
  137. Width = Dim.Auto (),
  138. Id = "focusableView",
  139. Text = "View",
  140. };
  141. Application.Top!.Add (view);
  142. // EnableForDesign sets to true; undo that
  143. popoverMenu.Visible = false;
  144. Application.Popover!.Register (popoverMenu);
  145. view.SetFocus ();
  146. })
  147. .WaitIteration ()
  148. .ScreenShot ("PopoverMenu initial state", _out)
  149. .Then (() => Assert.False (Application.Popover?.GetActivePopover () is PopoverMenu))
  150. .Then (() => Assert.IsNotType<MenuItemv2>(Application.Navigation!.GetFocused()))
  151. .Then (() => Application.Popover!.Show (Application.Popover.Popovers.First ()))
  152. .WaitIteration ()
  153. .ScreenShot ($"After Show", _out)
  154. .Then (() => Assert.True (Application.Popover?.GetActivePopover () is PopoverMenu))
  155. .Then (() => Assert.IsType<MenuItemv2>(Application.Navigation!.GetFocused()))
  156. .RaiseKeyDownEvent (Application.QuitKey)
  157. .ScreenShot ($"After {Application.QuitKey}", _out)
  158. .Then (() => Assert.False (Application.Popover?.GetActivePopover () is PopoverMenu))
  159. .Then (() => Assert.IsNotType<MenuItemv2>(Application.Navigation!.GetFocused()))
  160. .WriteOutLogs (_out)
  161. .Stop ();
  162. }
  163. [Theory]
  164. [ClassData (typeof (V2TestDrivers))]
  165. public void MenuBarItem_With_QuitKey_Open_QuitKey_Does_Not_Quit_App (V2TestDriver d)
  166. {
  167. MenuBarv2? menuBar = null;
  168. using GuiTestContext c = With.A<Window> (50, 20, d)
  169. .Then (
  170. () =>
  171. {
  172. var popoverMenu = new PopoverMenu ();
  173. // Call EnableForDesign
  174. Toplevel top = Application.Top!;
  175. bool result = popoverMenu.EnableForDesign (ref top);
  176. View? view = new View ()
  177. {
  178. CanFocus = true,
  179. Height = Dim.Auto (),
  180. Width = Dim.Auto (),
  181. Id = "focusableView",
  182. Text = "View",
  183. };
  184. Application.Top!.Add (view);
  185. // EnableForDesign sets to true; undo that
  186. popoverMenu.Visible = false;
  187. Application.Popover!.Register (popoverMenu);
  188. view.SetFocus ();
  189. })
  190. .WaitIteration ()
  191. .Then (() => Assert.IsNotType<MenuItemv2>(Application.Navigation!.GetFocused()))
  192. .ScreenShot ("PopoverMenu initial state", _out)
  193. .Then (() => Application.Popover!.Show (Application.Popover.Popovers.First ()))
  194. .WaitIteration ()
  195. .ScreenShot ("PopoverMenu after Show", _out)
  196. .Then (() => Assert.Equal ("Cu_t", Application.Navigation!.GetFocused ()!.Title))
  197. .Then (() => Assert.True (Application.Top!.Running))
  198. .RaiseKeyDownEvent (Application.QuitKey)
  199. .Then (() => Application.LayoutAndDraw ())
  200. .ScreenShot ($"After {Application.QuitKey}", _out)
  201. .Then (() => Assert.False (Application.Popover?.GetActivePopover () is PopoverMenu))
  202. .Then (() => Assert.True (Application.Top!.Running))
  203. .WriteOutLogs (_out)
  204. .Stop ();
  205. }
  206. }