PopverMenuTests.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. using System.Globalization;
  2. using TerminalGuiFluentTesting;
  3. using Xunit.Abstractions;
  4. namespace IntegrationTests.FluentTests;
  5. /// <summary>
  6. /// Tests for the PopoverMenu class
  7. /// </summary>
  8. public class PopoverMenuTests
  9. {
  10. private readonly TextWriter _out;
  11. public PopoverMenuTests (ITestOutputHelper outputHelper)
  12. {
  13. CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
  14. _out = new TestOutputWriter (outputHelper);
  15. }
  16. [Theory]
  17. [ClassData (typeof (V2TestDrivers))]
  18. public void EnableForDesign_CreatesMenuItems (V2TestDriver d)
  19. {
  20. using GuiTestContext c = With.A<Window> (80, 25, d)
  21. .Then (
  22. () =>
  23. {
  24. var popoverMenu = new PopoverMenu ();
  25. Application.Top!.Add (popoverMenu);
  26. // Call EnableForDesign
  27. Toplevel top = Application.Top;
  28. bool result = popoverMenu.EnableForDesign (ref top);
  29. // Should return true
  30. Assert.True (result);
  31. // Should have created menu items
  32. Assert.NotNull (popoverMenu.Root);
  33. Assert.Equal (7, popoverMenu.Root.SubViews.Count);
  34. // Should have Cut menu item
  35. View? cutMenuItem = popoverMenu.GetMenuItemsOfAllSubMenus ().FirstOrDefault (v => v?.Title == "Cu_t");
  36. Assert.NotNull (cutMenuItem);
  37. })
  38. .Stop ();
  39. }
  40. [Theory]
  41. [ClassData (typeof (V2TestDrivers))]
  42. public void Activate_Sets_Application_Navigation_Correctly (V2TestDriver d)
  43. {
  44. using GuiTestContext c = With.A<Window> (50, 20, d)
  45. .Then (
  46. () =>
  47. {
  48. var popoverMenu = new PopoverMenu ();
  49. // Call EnableForDesign
  50. Toplevel top = Application.Top!;
  51. popoverMenu.EnableForDesign (ref top);
  52. View? view = new View ()
  53. {
  54. CanFocus = true,
  55. Height = Dim.Auto (),
  56. Width = Dim.Auto (),
  57. Id = "focusableView",
  58. Text = "View",
  59. };
  60. Application.Top!.Add (view);
  61. // EnableForDesign sets to true; undo that
  62. popoverMenu.Visible = false;
  63. Application.Popover!.Register (popoverMenu);
  64. view.SetFocus ();
  65. })
  66. .WaitIteration ()
  67. .Then (() => Assert.False (Application.Popover?.GetActivePopover () is PopoverMenu))
  68. .Then (() => Assert.IsNotType<MenuItemv2> (Application.Navigation!.GetFocused ()))
  69. .ScreenShot ("PopoverMenu initial state", _out)
  70. .Then (() => Application.Popover!.Show (Application.Popover.Popovers.First ()))
  71. .WaitIteration ()
  72. .ScreenShot ($"After Show", _out)
  73. .Then (() => Assert.True (Application.Popover?.GetActivePopover () is PopoverMenu))
  74. .Then (() => Assert.Equal ("Cu_t", Application.Navigation!.GetFocused ()!.Title))
  75. .WriteOutLogs (_out)
  76. .Stop ();
  77. }
  78. [Theory]
  79. [ClassData (typeof (V2TestDrivers))]
  80. public void QuitKey_Hides (V2TestDriver d)
  81. {
  82. using GuiTestContext c = With.A<Window> (50, 20, d)
  83. .Then (
  84. () =>
  85. {
  86. var popoverMenu = new PopoverMenu ();
  87. // Call EnableForDesign
  88. Toplevel top = Application.Top!;
  89. bool result = popoverMenu.EnableForDesign (ref top);
  90. View? view = new View ()
  91. {
  92. CanFocus = true,
  93. Height = Dim.Auto (),
  94. Width = Dim.Auto (),
  95. Id = "focusableView",
  96. Text = "View",
  97. };
  98. Application.Top!.Add (view);
  99. // EnableForDesign sets to true; undo that
  100. popoverMenu.Visible = false;
  101. Application.Popover!.Register (popoverMenu);
  102. view.SetFocus ();
  103. })
  104. .WaitIteration ()
  105. .ScreenShot ("PopoverMenu initial state", _out)
  106. .Then (() => Assert.False (Application.Popover?.GetActivePopover () is PopoverMenu))
  107. .Then (() => Application.Popover!.Show (Application.Popover.Popovers.First ()))
  108. .WaitIteration ()
  109. .ScreenShot ($"After Show", _out)
  110. .Then (() => Assert.True (Application.Popover?.GetActivePopover () is PopoverMenu))
  111. .RaiseKeyDownEvent (Application.QuitKey)
  112. .Then (() => Application.LayoutAndDraw (true))
  113. .WaitIteration ()
  114. .WriteOutLogs (_out)
  115. .ScreenShot ($"After {Application.QuitKey}", _out)
  116. .Then (() => Assert.False (Application.Popover!.Popovers.Cast<PopoverMenu> ().FirstOrDefault()!.Visible))
  117. .Then (() => Assert.Null (Application.Popover!.GetActivePopover()))
  118. .Then (() => Assert.True (Application.Top!.Running))
  119. .WriteOutLogs (_out)
  120. .Stop ();
  121. }
  122. [Theory]
  123. [ClassData (typeof (V2TestDrivers))]
  124. public void QuitKey_Restores_Focus_Correctly (V2TestDriver d)
  125. {
  126. using GuiTestContext c = With.A<Window> (50, 20, d)
  127. .Then (
  128. () =>
  129. {
  130. var popoverMenu = new PopoverMenu ();
  131. // Call EnableForDesign
  132. Toplevel top = Application.Top!;
  133. bool result = popoverMenu.EnableForDesign (ref top);
  134. View? view = new View ()
  135. {
  136. CanFocus = true,
  137. Height = Dim.Auto (),
  138. Width = Dim.Auto (),
  139. Id = "focusableView",
  140. Text = "View",
  141. };
  142. Application.Top!.Add (view);
  143. // EnableForDesign sets to true; undo that
  144. popoverMenu.Visible = false;
  145. Application.Popover!.Register (popoverMenu);
  146. view.SetFocus ();
  147. })
  148. .WaitIteration ()
  149. .ScreenShot ("PopoverMenu initial state", _out)
  150. .Then (() => Assert.False (Application.Popover?.GetActivePopover () is PopoverMenu))
  151. .Then (() => Assert.IsNotType<MenuItemv2>(Application.Navigation!.GetFocused()))
  152. .Then (() => Application.Popover!.Show (Application.Popover.Popovers.First ()))
  153. .WaitIteration ()
  154. .ScreenShot ($"After Show", _out)
  155. .Then (() => Assert.True (Application.Popover?.GetActivePopover () is PopoverMenu))
  156. .Then (() => Assert.IsType<MenuItemv2>(Application.Navigation!.GetFocused()))
  157. .RaiseKeyDownEvent (Application.QuitKey)
  158. .ScreenShot ($"After {Application.QuitKey}", _out)
  159. .Then (() => Assert.False (Application.Popover?.GetActivePopover () is PopoverMenu))
  160. .Then (() => Assert.IsNotType<MenuItemv2>(Application.Navigation!.GetFocused()))
  161. .WriteOutLogs (_out)
  162. .Stop ();
  163. }
  164. [Theory]
  165. [ClassData (typeof (V2TestDrivers))]
  166. public void MenuBarItem_With_QuitKey_Open_QuitKey_Does_Not_Quit_App (V2TestDriver d)
  167. {
  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. [Theory]
  207. [ClassData (typeof (V2TestDrivers))]
  208. public void Not_Active_DoesNotEat_Space (V2TestDriver d)
  209. {
  210. int spaceKeyDownCount = 0;
  211. View testView = new View ()
  212. {
  213. CanFocus = true,
  214. Id = "testView",
  215. };
  216. testView.KeyDown += (sender, key) =>
  217. {
  218. if (key == Key.Space)
  219. {
  220. spaceKeyDownCount++;
  221. }
  222. };
  223. using GuiTestContext c = With.A<Window> (50, 20, d)
  224. .Then (
  225. () =>
  226. {
  227. var popoverMenu = new PopoverMenu();
  228. Toplevel top = Application.Top!;
  229. popoverMenu.EnableForDesign (ref top);
  230. Application.Popover!.Register (popoverMenu);
  231. })
  232. .Add (testView)
  233. .WaitIteration ()
  234. .Focus (testView)
  235. .RaiseKeyDownEvent (Key.Space)
  236. .Then (() => Assert.Equal (1, spaceKeyDownCount))
  237. .WriteOutLogs (_out)
  238. .Stop ();
  239. }
  240. [Theory]
  241. [ClassData (typeof (V2TestDrivers))]
  242. public void Not_Active_DoesNotEat_Enter (V2TestDriver d)
  243. {
  244. int enterKeyDownCount = 0;
  245. View testView = new View ()
  246. {
  247. CanFocus = true,
  248. Id = "testView",
  249. };
  250. testView.KeyDown += (sender, key) =>
  251. {
  252. if (key == Key.Enter)
  253. {
  254. enterKeyDownCount++;
  255. }
  256. };
  257. using GuiTestContext c = With.A<Window> (50, 20, d)
  258. .Then (
  259. () =>
  260. {
  261. var popoverMenu = new PopoverMenu ();
  262. Toplevel top = Application.Top!;
  263. popoverMenu.EnableForDesign (ref top);
  264. Application.Popover!.Register (popoverMenu);
  265. })
  266. .Add (testView)
  267. .WaitIteration ()
  268. .Focus (testView)
  269. .RaiseKeyDownEvent (Key.Enter)
  270. .Then (() => Assert.Equal (1, enterKeyDownCount))
  271. .WriteOutLogs (_out)
  272. .Stop ();
  273. }
  274. [Theory]
  275. [ClassData (typeof (V2TestDrivers))]
  276. public void Not_Active_DoesNotEat_QuitKey (V2TestDriver d)
  277. {
  278. int quitKeyDownCount = 0;
  279. View testView = new View ()
  280. {
  281. CanFocus = true,
  282. Id = "testView",
  283. };
  284. testView.KeyDown += (sender, key) =>
  285. {
  286. if (key == Application.QuitKey)
  287. {
  288. quitKeyDownCount++;
  289. }
  290. };
  291. using GuiTestContext c = With.A<Window> (50, 20, d)
  292. .Then (
  293. () =>
  294. {
  295. var popoverMenu = new PopoverMenu ();
  296. Toplevel top = Application.Top!;
  297. popoverMenu.EnableForDesign (ref top);
  298. Application.Popover!.Register (popoverMenu);
  299. })
  300. .Add (testView)
  301. .WaitIteration ()
  302. .Focus (testView)
  303. .RaiseKeyDownEvent (Application.QuitKey)
  304. .Then (() => Assert.Equal (1, quitKeyDownCount))
  305. .WriteOutLogs (_out)
  306. .Stop ();
  307. }
  308. }