PopverMenuTests.cs 18 KB

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