PopverMenuTests.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. using GuiTestContext c = With.A<Window> (50, 20, d)
  41. .Then (
  42. () =>
  43. {
  44. var popoverMenu = new PopoverMenu ();
  45. // Call EnableForDesign
  46. Toplevel top = Application.Top!;
  47. popoverMenu.EnableForDesign (ref top);
  48. View? view = new View ()
  49. {
  50. CanFocus = true,
  51. Height = Dim.Auto (),
  52. Width = Dim.Auto (),
  53. Id = "focusableView",
  54. Text = "View",
  55. };
  56. Application.Top!.Add (view);
  57. // EnableForDesign sets to true; undo that
  58. popoverMenu.Visible = false;
  59. Application.Popover!.Register (popoverMenu);
  60. view.SetFocus ();
  61. })
  62. .WaitIteration ()
  63. .Then (() => Assert.False (Application.Popover?.GetActivePopover () is PopoverMenu))
  64. .Then (() => Assert.IsNotType<MenuItemv2> (Application.Navigation!.GetFocused ()))
  65. .ScreenShot ("PopoverMenu initial state", _out)
  66. .Then (() => Application.Popover!.Show (Application.Popover.Popovers.First ()))
  67. .WaitIteration ()
  68. .ScreenShot ($"After Show", _out)
  69. .Then (() => Assert.True (Application.Popover?.GetActivePopover () is PopoverMenu))
  70. .Then (() => Assert.Equal ("Cu_t", Application.Navigation!.GetFocused ()!.Title))
  71. .WriteOutLogs (_out)
  72. .Stop ();
  73. }
  74. [Theory]
  75. [ClassData (typeof (V2TestDrivers))]
  76. public void QuitKey_Hides (V2TestDriver d)
  77. {
  78. using GuiTestContext c = With.A<Window> (50, 20, d)
  79. .Then (
  80. () =>
  81. {
  82. var popoverMenu = new PopoverMenu ();
  83. // Call EnableForDesign
  84. Toplevel top = Application.Top!;
  85. bool result = popoverMenu.EnableForDesign (ref top);
  86. View? view = new View ()
  87. {
  88. CanFocus = true,
  89. Height = Dim.Auto (),
  90. Width = Dim.Auto (),
  91. Id = "focusableView",
  92. Text = "View",
  93. };
  94. Application.Top!.Add (view);
  95. // EnableForDesign sets to true; undo that
  96. popoverMenu.Visible = false;
  97. Application.Popover!.Register (popoverMenu);
  98. view.SetFocus ();
  99. })
  100. .WaitIteration ()
  101. .ScreenShot ("PopoverMenu initial state", _out)
  102. .Then (() => Assert.False (Application.Popover?.GetActivePopover () is PopoverMenu))
  103. .Then (() => Application.Popover!.Show (Application.Popover.Popovers.First ()))
  104. .WaitIteration ()
  105. .ScreenShot ($"After Show", _out)
  106. .Then (() => Assert.True (Application.Popover?.GetActivePopover () is PopoverMenu))
  107. .RaiseKeyDownEvent (Application.QuitKey)
  108. .Then (() => Application.LayoutAndDraw (true))
  109. .WaitIteration ()
  110. .WriteOutLogs (_out)
  111. .ScreenShot ($"After {Application.QuitKey}", _out)
  112. .Then (() => Assert.False (Application.Popover!.Popovers.Cast<PopoverMenu> ().FirstOrDefault()!.Visible))
  113. .Then (() => Assert.Null (Application.Popover!.GetActivePopover()))
  114. .Then (() => Assert.True (Application.Top!.Running))
  115. .WriteOutLogs (_out)
  116. .Stop ();
  117. }
  118. [Theory]
  119. [ClassData (typeof (V2TestDrivers))]
  120. public void QuitKey_Restores_Focus_Correctly (V2TestDriver d)
  121. {
  122. using GuiTestContext c = With.A<Window> (50, 20, d)
  123. .Then (
  124. () =>
  125. {
  126. var popoverMenu = new PopoverMenu ();
  127. // Call EnableForDesign
  128. Toplevel top = Application.Top!;
  129. bool result = popoverMenu.EnableForDesign (ref top);
  130. View? view = new View ()
  131. {
  132. CanFocus = true,
  133. Height = Dim.Auto (),
  134. Width = Dim.Auto (),
  135. Id = "focusableView",
  136. Text = "View",
  137. };
  138. Application.Top!.Add (view);
  139. // EnableForDesign sets to true; undo that
  140. popoverMenu.Visible = false;
  141. Application.Popover!.Register (popoverMenu);
  142. view.SetFocus ();
  143. })
  144. .WaitIteration ()
  145. .ScreenShot ("PopoverMenu initial state", _out)
  146. .Then (() => Assert.False (Application.Popover?.GetActivePopover () is PopoverMenu))
  147. .Then (() => Assert.IsNotType<MenuItemv2>(Application.Navigation!.GetFocused()))
  148. .Then (() => Application.Popover!.Show (Application.Popover.Popovers.First ()))
  149. .WaitIteration ()
  150. .ScreenShot ($"After Show", _out)
  151. .Then (() => Assert.True (Application.Popover?.GetActivePopover () is PopoverMenu))
  152. .Then (() => Assert.IsType<MenuItemv2>(Application.Navigation!.GetFocused()))
  153. .RaiseKeyDownEvent (Application.QuitKey)
  154. .ScreenShot ($"After {Application.QuitKey}", _out)
  155. .Then (() => Assert.False (Application.Popover?.GetActivePopover () is PopoverMenu))
  156. .Then (() => Assert.IsNotType<MenuItemv2>(Application.Navigation!.GetFocused()))
  157. .WriteOutLogs (_out)
  158. .Stop ();
  159. }
  160. [Theory]
  161. [ClassData (typeof (V2TestDrivers))]
  162. public void MenuBarItem_With_QuitKey_Open_QuitKey_Does_Not_Quit_App (V2TestDriver d)
  163. {
  164. using GuiTestContext c = With.A<Window> (50, 20, d)
  165. .Then (
  166. () =>
  167. {
  168. var popoverMenu = new PopoverMenu ();
  169. // Call EnableForDesign
  170. Toplevel top = Application.Top!;
  171. bool result = popoverMenu.EnableForDesign (ref top);
  172. View? view = new View ()
  173. {
  174. CanFocus = true,
  175. Height = Dim.Auto (),
  176. Width = Dim.Auto (),
  177. Id = "focusableView",
  178. Text = "View",
  179. };
  180. Application.Top!.Add (view);
  181. // EnableForDesign sets to true; undo that
  182. popoverMenu.Visible = false;
  183. Application.Popover!.Register (popoverMenu);
  184. view.SetFocus ();
  185. })
  186. .WaitIteration ()
  187. .Then (() => Assert.IsNotType<MenuItemv2>(Application.Navigation!.GetFocused()))
  188. .ScreenShot ("PopoverMenu initial state", _out)
  189. .Then (() => Application.Popover!.Show (Application.Popover.Popovers.First ()))
  190. .WaitIteration ()
  191. .ScreenShot ("PopoverMenu after Show", _out)
  192. .Then (() => Assert.Equal ("Cu_t", Application.Navigation!.GetFocused ()!.Title))
  193. .Then (() => Assert.True (Application.Top!.Running))
  194. .RaiseKeyDownEvent (Application.QuitKey)
  195. .Then (() => Application.LayoutAndDraw ())
  196. .ScreenShot ($"After {Application.QuitKey}", _out)
  197. .Then (() => Assert.False (Application.Popover?.GetActivePopover () is PopoverMenu))
  198. .Then (() => Assert.True (Application.Top!.Running))
  199. .WriteOutLogs (_out)
  200. .Stop ();
  201. }
  202. [Theory]
  203. [ClassData (typeof (V2TestDrivers))]
  204. public void Not_Active_DoesNotEat_Space (V2TestDriver d)
  205. {
  206. int spaceKeyDownCount = 0;
  207. View testView = new View ()
  208. {
  209. CanFocus = true,
  210. Id = "testView",
  211. };
  212. testView.KeyDown += (sender, key) =>
  213. {
  214. if (key == Key.Space)
  215. {
  216. spaceKeyDownCount++;
  217. }
  218. };
  219. using GuiTestContext c = With.A<Window> (50, 20, d)
  220. .Then (
  221. () =>
  222. {
  223. var popoverMenu = new PopoverMenu();
  224. Toplevel top = Application.Top!;
  225. popoverMenu.EnableForDesign (ref top);
  226. Application.Popover!.Register (popoverMenu);
  227. })
  228. .Add (testView)
  229. .WaitIteration ()
  230. .Focus (testView)
  231. .RaiseKeyDownEvent (Key.Space)
  232. .Then (() => Assert.Equal (1, spaceKeyDownCount))
  233. .WriteOutLogs (_out)
  234. .Stop ();
  235. }
  236. [Theory]
  237. [ClassData (typeof (V2TestDrivers))]
  238. public void Not_Active_DoesNotEat_Enter (V2TestDriver d)
  239. {
  240. int enterKeyDownCount = 0;
  241. View testView = new View ()
  242. {
  243. CanFocus = true,
  244. Id = "testView",
  245. };
  246. testView.KeyDown += (sender, key) =>
  247. {
  248. if (key == Key.Enter)
  249. {
  250. enterKeyDownCount++;
  251. }
  252. };
  253. using GuiTestContext c = With.A<Window> (50, 20, d)
  254. .Then (
  255. () =>
  256. {
  257. var popoverMenu = new PopoverMenu ();
  258. Toplevel top = Application.Top!;
  259. popoverMenu.EnableForDesign (ref top);
  260. Application.Popover!.Register (popoverMenu);
  261. })
  262. .Add (testView)
  263. .WaitIteration ()
  264. .Focus (testView)
  265. .RaiseKeyDownEvent (Key.Enter)
  266. .Then (() => Assert.Equal (1, enterKeyDownCount))
  267. .WriteOutLogs (_out)
  268. .Stop ();
  269. }
  270. [Theory]
  271. [ClassData (typeof (V2TestDrivers))]
  272. public void Not_Active_DoesNotEat_QuitKey (V2TestDriver d)
  273. {
  274. int quitKeyDownCount = 0;
  275. View testView = new View ()
  276. {
  277. CanFocus = true,
  278. Id = "testView",
  279. };
  280. testView.KeyDown += (sender, key) =>
  281. {
  282. if (key == Application.QuitKey)
  283. {
  284. quitKeyDownCount++;
  285. }
  286. };
  287. using GuiTestContext c = With.A<Window> (50, 20, d)
  288. .Then (
  289. () =>
  290. {
  291. var popoverMenu = new PopoverMenu ();
  292. Toplevel top = Application.Top!;
  293. popoverMenu.EnableForDesign (ref top);
  294. Application.Popover!.Register (popoverMenu);
  295. })
  296. .Add (testView)
  297. .WaitIteration ()
  298. .Focus (testView)
  299. .RaiseKeyDownEvent (Application.QuitKey)
  300. .Then (() => Assert.Equal (1, quitKeyDownCount))
  301. .WriteOutLogs (_out)
  302. .Stop ();
  303. }
  304. }