PopverMenuTests.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 (TestDrivers))]
  19. public void EnableForDesign_CreatesMenuItems (TestDriver d)
  20. {
  21. using GuiTestContext c = With.A<Window> (80, 25, d)
  22. .Then ((app) =>
  23. {
  24. PopoverMenu popoverMenu = new ();
  25. app.TopRunnable!.Add (popoverMenu);
  26. // Call EnableForDesign
  27. Toplevel top = app.TopRunnable;
  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. }
  39. private static readonly object o = new ();
  40. [Theory]
  41. [ClassData (typeof (TestDrivers))]
  42. public void Activate_Sets_Application_Navigation_Correctly (TestDriver d)
  43. {
  44. lock (o)
  45. {
  46. IApplication? app = null;
  47. using GuiTestContext c = With.A<Window> (50, 20, d)
  48. .Then ((a) =>
  49. {
  50. app = a;
  51. PopoverMenu popoverMenu = new ()
  52. {
  53. App = app
  54. };
  55. // Call EnableForDesign
  56. Toplevel top = app.TopRunnable!;
  57. popoverMenu.EnableForDesign (ref top);
  58. var view = new View
  59. {
  60. CanFocus = true,
  61. Height = Dim.Auto (),
  62. Width = Dim.Auto (),
  63. Id = "focusableView",
  64. Text = "View"
  65. };
  66. app.TopRunnable!.Add (view);
  67. // EnableForDesign sets to true; undo that
  68. popoverMenu.Visible = false;
  69. app?.Popover!.Register (popoverMenu);
  70. view.SetFocus ();
  71. })
  72. .AssertFalse (app?.Popover?.GetActivePopover () is PopoverMenu)
  73. .AssertIsNotType<MenuItem> (app?.Navigation!.GetFocused ())
  74. .ScreenShot ("PopoverMenu initial state", _out)
  75. .Then ((_) => app?.Popover!.Show (app?.Popover.Popovers.First ()))
  76. .ScreenShot ("After Show", _out)
  77. .AssertTrue (app?.Popover?.GetActivePopover () is PopoverMenu)
  78. .AssertEqual ("Cu_t", app?.Navigation!.GetFocused ()!.Title);
  79. }
  80. }
  81. [Theory]
  82. [ClassData (typeof (TestDrivers))]
  83. public void QuitKey_Hides (TestDriver d)
  84. {
  85. IApplication? app = null;
  86. using GuiTestContext c = With.A<Window> (50, 20, d)
  87. .Then ((a) =>
  88. {
  89. app = a;
  90. PopoverMenu popoverMenu = new ()
  91. {
  92. App = app
  93. };
  94. // Call EnableForDesign
  95. Toplevel top = app.TopRunnable!;
  96. bool result = popoverMenu.EnableForDesign (ref top);
  97. var view = new View
  98. {
  99. CanFocus = true,
  100. Height = Dim.Auto (),
  101. Width = Dim.Auto (),
  102. Id = "focusableView",
  103. Text = "View"
  104. };
  105. app.TopRunnable!.Add (view);
  106. // EnableForDesign sets to true; undo that
  107. popoverMenu.Visible = false;
  108. app?.Popover!.Register (popoverMenu);
  109. view.SetFocus ();
  110. })
  111. .ScreenShot ("PopoverMenu initial state", _out)
  112. .AssertFalse (app?.Popover?.GetActivePopover () is PopoverMenu)
  113. .Then ((_) => app?.Popover!.Show (app?.Popover.Popovers.First ()))
  114. .ScreenShot ("After Show", _out)
  115. .AssertTrue (app?.Popover?.GetActivePopover () is PopoverMenu)
  116. .EnqueueKeyEvent (Application.QuitKey)
  117. .ScreenShot ($"After {Application.QuitKey}", _out)
  118. .AssertFalse (app?.Popover!.Popovers.Cast<PopoverMenu> ().FirstOrDefault ()!.Visible)
  119. .AssertNull (app?.Popover!.GetActivePopover ())
  120. .AssertTrue (app?.TopRunnable!.Running);
  121. }
  122. [Theory]
  123. [ClassData (typeof (TestDrivers))]
  124. public void QuitKey_Restores_Focus_Correctly (TestDriver d)
  125. {
  126. IApplication? app = null;
  127. using GuiTestContext c = With.A<Window> (50, 20, d)
  128. .Then ((a) =>
  129. {
  130. app = a;
  131. PopoverMenu popoverMenu = new ()
  132. {
  133. App = app
  134. };
  135. // Call EnableForDesign
  136. Toplevel top = app.TopRunnable!;
  137. bool result = popoverMenu.EnableForDesign (ref top);
  138. var view = new View
  139. {
  140. CanFocus = true,
  141. Height = Dim.Auto (),
  142. Width = Dim.Auto (),
  143. Id = "focusableView",
  144. Text = "View"
  145. };
  146. app.TopRunnable!.Add (view);
  147. // EnableForDesign sets to true; undo that
  148. popoverMenu.Visible = false;
  149. app?.Popover!.Register (popoverMenu);
  150. view.SetFocus ();
  151. })
  152. .ScreenShot ("PopoverMenu initial state", _out)
  153. .AssertFalse (app?.Popover?.GetActivePopover () is PopoverMenu)
  154. .AssertIsNotType<MenuItem> (app?.Navigation!.GetFocused ())
  155. .Then ((_) => app?.Popover!.Show (app?.Popover.Popovers.First ()))
  156. .ScreenShot ("After Show", _out)
  157. .AssertTrue (app?.Popover?.GetActivePopover () is PopoverMenu)
  158. .AssertIsType<MenuItem> (app?.Navigation!.GetFocused ())
  159. .EnqueueKeyEvent (Application.QuitKey)
  160. .ScreenShot ($"After {Application.QuitKey}", _out)
  161. .AssertFalse (app?.Popover?.GetActivePopover () is PopoverMenu)
  162. .AssertIsNotType<MenuItem> (app?.Navigation!.GetFocused ());
  163. }
  164. [Theory]
  165. [ClassData (typeof (TestDrivers))]
  166. public void MenuBarItem_With_QuitKey_Open_QuitKey_Does_Not_Quit_App (TestDriver d)
  167. {
  168. IApplication? app = null;
  169. using GuiTestContext c = With.A<Window> (50, 20, d)
  170. .Then ((a) =>
  171. {
  172. app = a;
  173. PopoverMenu popoverMenu = new ()
  174. {
  175. App = app
  176. };
  177. // Call EnableForDesign
  178. Toplevel top = app.TopRunnable!;
  179. bool result = popoverMenu.EnableForDesign (ref top);
  180. var view = new View
  181. {
  182. CanFocus = true,
  183. Height = Dim.Auto (),
  184. Width = Dim.Auto (),
  185. Id = "focusableView",
  186. Text = "View"
  187. };
  188. app.TopRunnable!.Add (view);
  189. // EnableForDesign sets to true; undo that
  190. popoverMenu.Visible = false;
  191. app?.Popover!.Register (popoverMenu);
  192. view.SetFocus ();
  193. })
  194. .AssertIsNotType<MenuItem> (app?.Navigation!.GetFocused ())
  195. .ScreenShot ("PopoverMenu initial state", _out)
  196. .Then ((_) => app?.Popover!.Show (app?.Popover.Popovers.First ()))
  197. .ScreenShot ("PopoverMenu after Show", _out)
  198. .AssertEqual ("Cu_t", app?.Navigation!.GetFocused ()!.Title)
  199. .AssertTrue (app?.TopRunnable!.Running)
  200. .EnqueueKeyEvent (Application.QuitKey)
  201. .ScreenShot ($"After {Application.QuitKey}", _out)
  202. .AssertFalse (app?.Popover?.GetActivePopover () is PopoverMenu)
  203. .AssertTrue (app?.TopRunnable!.Running);
  204. }
  205. [Theory]
  206. [ClassData (typeof (TestDrivers))]
  207. public void Not_Active_DoesNotEat_Space (TestDriver d)
  208. {
  209. var spaceKeyDownCount = 0;
  210. var testView = new View
  211. {
  212. CanFocus = true,
  213. Id = "testView"
  214. };
  215. testView.KeyDown += (sender, key) =>
  216. {
  217. if (key == Key.Space)
  218. {
  219. spaceKeyDownCount++;
  220. }
  221. };
  222. IApplication? app = null;
  223. using GuiTestContext c = With.A<Window> (50, 20, d)
  224. .Then ((a) =>
  225. {
  226. app = a;
  227. PopoverMenu popoverMenu = new ()
  228. {
  229. App = app
  230. };
  231. Toplevel top = app.TopRunnable!;
  232. popoverMenu.EnableForDesign (ref top);
  233. app?.Popover!.Register (popoverMenu);
  234. })
  235. .Add (testView)
  236. .Focus (testView)
  237. .EnqueueKeyEvent (Key.Space)
  238. .AssertEqual (1, spaceKeyDownCount);
  239. }
  240. [Theory]
  241. [ClassData (typeof (TestDrivers))]
  242. public void Not_Active_DoesNotEat_Enter (TestDriver d)
  243. {
  244. var enterKeyDownCount = 0;
  245. var 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. IApplication? app = null;
  258. using GuiTestContext c = With.A<Window> (50, 20, d)
  259. .Then ((a) =>
  260. {
  261. app = a;
  262. PopoverMenu popoverMenu = new ()
  263. {
  264. App = app
  265. };
  266. Toplevel top = app.TopRunnable!;
  267. popoverMenu.EnableForDesign (ref top);
  268. app?.Popover!.Register (popoverMenu);
  269. })
  270. .Add (testView)
  271. .Focus (testView)
  272. .EnqueueKeyEvent (Key.Enter)
  273. .AssertEqual (1, enterKeyDownCount);
  274. }
  275. [Theory]
  276. [ClassData (typeof (TestDrivers))]
  277. public void Not_Active_DoesNotEat_QuitKey (TestDriver d)
  278. {
  279. var quitKeyDownCount = 0;
  280. var testView = new View
  281. {
  282. CanFocus = true,
  283. Id = "testView"
  284. };
  285. testView.KeyDown += (sender, key) =>
  286. {
  287. if (key == Application.QuitKey)
  288. {
  289. quitKeyDownCount++;
  290. }
  291. };
  292. IApplication? app = null;
  293. using GuiTestContext c = With.A<Window> (50, 20, d)
  294. .Then ((a) =>
  295. {
  296. app = a;
  297. PopoverMenu popoverMenu = new ()
  298. {
  299. App = app
  300. };
  301. Toplevel top = app.TopRunnable!;
  302. popoverMenu.EnableForDesign (ref top);
  303. app?.Popover!.Register (popoverMenu);
  304. })
  305. .Add (testView)
  306. .EnqueueKeyEvent (Application.QuitKey)
  307. .AssertEqual (1, quitKeyDownCount);
  308. }
  309. [Theory]
  310. [ClassData (typeof (TestDrivers))]
  311. public void ContextMenu_CrashesOnRight (TestDriver d)
  312. {
  313. var clicked = false;
  314. MenuItem [] menuItems = [new ("_New File", string.Empty, () => { clicked = true; })];
  315. IApplication? app = null;
  316. using GuiTestContext c = With.A<Window> (40, 10, d, _out)
  317. .Then ((a) => app = a)
  318. .WithContextMenu (new (menuItems) { App = app })
  319. .ScreenShot ("Before open menu", _out)
  320. // Click in main area inside border
  321. .RightClick (1, 1)
  322. .Then ((_) =>
  323. {
  324. // Test depends on menu having a border
  325. IPopover? popover = app?.Popover!.GetActivePopover ();
  326. Assert.NotNull (popover);
  327. var popoverMenu = popover as PopoverMenu;
  328. popoverMenu!.Root!.BorderStyle = LineStyle.Single;
  329. })
  330. .ScreenShot ("After open menu", _out)
  331. .LeftClick (2, 2)
  332. .AssertTrue(clicked);
  333. }
  334. [Theory]
  335. [ClassData (typeof (TestDrivers))]
  336. public void ContextMenu_OpenSubmenu (TestDriver d)
  337. {
  338. var clicked = false;
  339. MenuItem [] menuItems =
  340. [
  341. new ("One", "", null),
  342. new ("Two", "", null),
  343. new ("Three", "", null),
  344. new (
  345. "Four",
  346. "",
  347. new (
  348. [
  349. new ("SubMenu1", "", null),
  350. new ("SubMenu2", "", () => clicked = true),
  351. new ("SubMenu3", "", null),
  352. new ("SubMenu4", "", null),
  353. new ("SubMenu5", "", null),
  354. new ("SubMenu6", "", null),
  355. new ("SubMenu7", "", null)
  356. ])),
  357. new ("Five", "", null),
  358. new ("Six", "", null)
  359. ];
  360. IApplication? app = null;
  361. using GuiTestContext c = With.A<Window> (40, 10, d)
  362. .Then ((a) => app = a)
  363. .WithContextMenu (new (menuItems) { App = app })
  364. .ScreenShot ("Before open menu", _out)
  365. // Click in main area inside border
  366. .RightClick (1, 1)
  367. .ScreenShot ("After open menu", _out)
  368. .EnqueueKeyEvent (Key.CursorDown)
  369. .EnqueueKeyEvent (Key.CursorDown)
  370. .EnqueueKeyEvent (Key.CursorDown)
  371. .EnqueueKeyEvent (Key.CursorRight)
  372. .ScreenShot ("After open submenu", _out)
  373. .EnqueueKeyEvent (Key.CursorDown)
  374. .EnqueueKeyEvent (Key.Enter)
  375. .ScreenShot ("Menu should be closed after selecting", _out);
  376. Assert.True (clicked);
  377. }
  378. }