ContextMenuTests.cs 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939
  1. using System.Globalization;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.ViewsTests;
  4. public class ContextMenuTests (ITestOutputHelper output)
  5. {
  6. [Fact]
  7. [AutoInitShutdown]
  8. public void ContextMenu_Constructors ()
  9. {
  10. var cm = new ContextMenu ();
  11. var top = new Toplevel ();
  12. Application.Begin (top);
  13. Assert.Equal (Point.Empty, cm.Position);
  14. Assert.Null (cm.MenuItems);
  15. Assert.Null (cm.Host);
  16. cm.Position = new Point (20, 10);
  17. var menuItems = new MenuBarItem (
  18. [
  19. new MenuItem ("First", "", null)
  20. ]
  21. );
  22. cm.Show (menuItems);
  23. Assert.Equal (new Point (20, 10), cm.Position);
  24. Assert.Single (cm.MenuItems!.Children);
  25. cm = new ContextMenu
  26. {
  27. Position = new Point (5, 10)
  28. };
  29. menuItems = new MenuBarItem (
  30. new [] { new MenuItem ("One", "", null), new MenuItem ("Two", "", null) }
  31. );
  32. cm.Show (menuItems);
  33. Assert.Equal (new Point (5, 10), cm.Position);
  34. Assert.Equal (2, cm.MenuItems!.Children.Length);
  35. Assert.Null (cm.Host);
  36. var view = new View { X = 5, Y = 10 };
  37. top.Add (view);
  38. cm = new ContextMenu
  39. {
  40. Host = view,
  41. Position = new Point (5, 10)
  42. };
  43. menuItems = new MenuBarItem (
  44. new [] { new MenuItem ("One", "", null), new MenuItem ("Two", "", null) }
  45. );
  46. cm.Show (menuItems);
  47. Assert.Equal (new Point (5, 10), cm.Position);
  48. Assert.Equal (2, cm.MenuItems.Children.Length);
  49. Assert.NotNull (cm.Host);
  50. top.Dispose ();
  51. }
  52. [Fact]
  53. [AutoInitShutdown]
  54. public void ContextMenu_Is_Closed_If_Another_MenuBar_Is_Open_Or_Vice_Versa ()
  55. {
  56. var cm = new ContextMenu
  57. {
  58. Position = new Point (10, 5)
  59. };
  60. var menuItems = new MenuBarItem (
  61. [
  62. new MenuItem ("One", "", null),
  63. new MenuItem ("Two", "", null)
  64. ]
  65. );
  66. var menu = new MenuBar
  67. {
  68. Menus =
  69. [
  70. new MenuBarItem ("File", "", null),
  71. new MenuBarItem ("Edit", "", null)
  72. ]
  73. };
  74. var top = new Toplevel ();
  75. top.Add (menu);
  76. Application.Begin (top);
  77. Assert.Null (Application.MouseGrabView);
  78. cm.Show (menuItems);
  79. Assert.True (ContextMenu.IsShow);
  80. Assert.Equal (cm.MenuBar, Application.MouseGrabView);
  81. Assert.False (menu.IsMenuOpen);
  82. Assert.True (menu.NewKeyDownEvent (menu.Key));
  83. Assert.False (ContextMenu.IsShow);
  84. Assert.Equal (menu, Application.MouseGrabView);
  85. Assert.True (menu.IsMenuOpen);
  86. cm.Show (menuItems);
  87. Assert.True (ContextMenu.IsShow);
  88. Assert.Equal (cm.MenuBar, Application.MouseGrabView);
  89. Assert.False (menu.IsMenuOpen);
  90. #if SUPPORT_ALT_TO_ACTIVATE_MENU
  91. Assert.True (Application.Top.ProcessKeyUp (new (Key.AltMask)));
  92. Assert.False (ContextMenu.IsShow);
  93. Assert.Equal (menu, Application.MouseGrabView);
  94. Assert.True (menu.IsMenuOpen);
  95. #endif
  96. cm.Show (menuItems);
  97. Assert.True (ContextMenu.IsShow);
  98. Assert.Equal (cm.MenuBar, Application.MouseGrabView);
  99. Assert.False (menu.IsMenuOpen);
  100. Assert.False (menu.NewMouseEvent (new MouseEvent { Position = new (1, 0), Flags = MouseFlags.ReportMousePosition, View = menu }));
  101. Assert.True (ContextMenu.IsShow);
  102. Assert.Equal (cm.MenuBar, Application.MouseGrabView);
  103. Assert.False (menu.IsMenuOpen);
  104. Assert.True (menu.NewMouseEvent (new MouseEvent { Position = new (1, 0), Flags = MouseFlags.Button1Clicked, View = menu }));
  105. Assert.False (ContextMenu.IsShow);
  106. Assert.Equal (menu, Application.MouseGrabView);
  107. Assert.True (menu.IsMenuOpen);
  108. top.Dispose ();
  109. }
  110. [Fact]
  111. [AutoInitShutdown]
  112. public void Draw_A_ContextMenu_Over_A_Borderless_Top ()
  113. {
  114. ((FakeDriver)Application.Driver!).SetBufferSize (20, 15);
  115. Assert.Equal (new Rectangle (0, 0, 20, 15), Application.Driver?.Clip);
  116. TestHelpers.AssertDriverContentsWithFrameAre ("", output);
  117. var top = new Toplevel { X = 2, Y = 2, Width = 15, Height = 4 };
  118. top.Add (new TextField { X = Pos.Center (), Width = 10, Text = "Test" });
  119. RunState rs = Application.Begin (top);
  120. Assert.Equal (new Rectangle (2, 2, 15, 4), top.Frame);
  121. Assert.Equal (top, Application.Top);
  122. TestHelpers.AssertDriverContentsWithFrameAre (
  123. @"
  124. Test",
  125. output
  126. );
  127. Application.RaiseMouseEvent (new MouseEvent { ScreenPosition = new (8, 2), Flags = MouseFlags.Button3Clicked });
  128. var firstIteration = false;
  129. Application.RunIteration (ref rs, ref firstIteration);
  130. TestHelpers.AssertDriverContentsWithFrameAre (
  131. @"
  132. Test
  133. ┌───────────────────
  134. │ Select All Ctrl+
  135. │ Delete All Ctrl+
  136. │ Copy Ctrl+
  137. │ Cut Ctrl+
  138. │ Paste Ctrl+
  139. │ Undo Ctrl+
  140. │ Redo Ctrl+
  141. └───────────────────",
  142. output
  143. );
  144. Application.End (rs);
  145. top.Dispose ();
  146. }
  147. [Fact]
  148. [AutoInitShutdown]
  149. public void Draw_A_ContextMenu_Over_A_Dialog ()
  150. {
  151. Toplevel top = new ();
  152. var win = new Window ();
  153. top.Add (win);
  154. RunState rsTop = Application.Begin (top);
  155. ((FakeDriver)Application.Driver!).SetBufferSize (20, 15);
  156. Assert.Equal (new Rectangle (0, 0, 20, 15), win.Frame);
  157. TestHelpers.AssertDriverContentsWithFrameAre (
  158. @"
  159. ┌──────────────────┐
  160. │ │
  161. │ │
  162. │ │
  163. │ │
  164. │ │
  165. │ │
  166. │ │
  167. │ │
  168. │ │
  169. │ │
  170. │ │
  171. │ │
  172. │ │
  173. └──────────────────┘",
  174. output
  175. );
  176. // Don't use Dialog here as it has more layout logic. Use Window instead.
  177. var dialog = new Window { X = 2, Y = 2, Width = 15, Height = 4 };
  178. dialog.Add (new TextField { X = Pos.Center (), Width = 10, Text = "Test" });
  179. RunState rsDialog = Application.Begin (dialog);
  180. Assert.Equal (new Rectangle (2, 2, 15, 4), dialog.Frame);
  181. TestHelpers.AssertDriverContentsWithFrameAre (
  182. @"
  183. ┌──────────────────┐
  184. │ │
  185. │ ┌─────────────┐ │
  186. │ │ Test │ │
  187. │ │ │ │
  188. │ └─────────────┘ │
  189. │ │
  190. │ │
  191. │ │
  192. │ │
  193. │ │
  194. │ │
  195. │ │
  196. │ │
  197. └──────────────────┘",
  198. output
  199. );
  200. Application.RaiseMouseEvent (new MouseEvent { ScreenPosition = new (9, 3), Flags = MouseFlags.Button3Clicked });
  201. var firstIteration = false;
  202. Application.RunIteration (ref rsDialog, ref firstIteration);
  203. TestHelpers.AssertDriverContentsWithFrameAre (
  204. @"
  205. ┌──────────────────┐
  206. │ │
  207. │ ┌─────────────┐ │
  208. │ │ Test │ │
  209. ┌───────────────────
  210. │ Select All Ctrl+
  211. │ Delete All Ctrl+
  212. │ Copy Ctrl+
  213. │ Cut Ctrl+
  214. │ Paste Ctrl+
  215. │ Undo Ctrl+
  216. │ Redo Ctrl+
  217. └───────────────────
  218. │ │
  219. └──────────────────┘",
  220. output
  221. );
  222. Application.End (rsDialog);
  223. Application.End (rsTop);
  224. top.Dispose ();
  225. }
  226. [Fact]
  227. [AutoInitShutdown]
  228. public void Draw_A_ContextMenu_Over_A_Top_Dialog ()
  229. {
  230. ((FakeDriver)Application.Driver!).SetBufferSize (20, 15);
  231. Assert.Equal (new Rectangle (0, 0, 20, 15), Application.Driver?.Clip);
  232. TestHelpers.AssertDriverContentsWithFrameAre ("", output);
  233. // Don't use Dialog here as it has more layout logic. Use Window instead.
  234. var dialog = new Window { X = 2, Y = 2, Width = 15, Height = 4 };
  235. dialog.Add (new TextField { X = Pos.Center (), Width = 10, Text = "Test" });
  236. RunState rs = Application.Begin (dialog);
  237. Assert.Equal (new Rectangle (2, 2, 15, 4), dialog.Frame);
  238. Assert.Equal (dialog, Application.Top);
  239. TestHelpers.AssertDriverContentsWithFrameAre (
  240. @"
  241. ┌─────────────┐
  242. │ Test │
  243. │ │
  244. └─────────────┘",
  245. output
  246. );
  247. Application.RaiseMouseEvent (new MouseEvent { ScreenPosition = new (9, 3), Flags = MouseFlags.Button3Clicked });
  248. var firstIteration = false;
  249. Application.RunIteration (ref rs, ref firstIteration);
  250. TestHelpers.AssertDriverContentsWithFrameAre (
  251. @"
  252. ┌─────────────┐
  253. │ Test │
  254. ┌───────────────────
  255. │ Select All Ctrl+
  256. │ Delete All Ctrl+
  257. │ Copy Ctrl+
  258. │ Cut Ctrl+
  259. │ Paste Ctrl+
  260. │ Undo Ctrl+
  261. │ Redo Ctrl+
  262. └───────────────────",
  263. output
  264. );
  265. Application.End (rs);
  266. dialog.Dispose ();
  267. }
  268. [Fact]
  269. [AutoInitShutdown]
  270. public void ForceMinimumPosToZero_True_False ()
  271. {
  272. var cm = new ContextMenu
  273. {
  274. Position = new Point (-1, -2)
  275. };
  276. var menuItems = new MenuBarItem (
  277. [
  278. new MenuItem ("One", "", null),
  279. new MenuItem ("Two", "", null)
  280. ]
  281. );
  282. Assert.Equal (new Point (-1, -2), cm.Position);
  283. Toplevel top = new ();
  284. Application.Begin (top);
  285. cm.Show (menuItems);
  286. Assert.Equal (new Point (-1, -2), cm.Position);
  287. Application.Refresh ();
  288. var expected = @"
  289. ┌──────┐
  290. │ One │
  291. │ Two │
  292. └──────┘
  293. ";
  294. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  295. Assert.Equal (new Rectangle (0, 1, 8, 4), pos);
  296. cm.ForceMinimumPosToZero = false;
  297. cm.Show (menuItems);
  298. Assert.Equal (new Point (-1, -2), cm.Position);
  299. Application.Refresh ();
  300. expected = @"
  301. One │
  302. Two │
  303. ──────┘
  304. ";
  305. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  306. Assert.Equal (new Rectangle (1, 0, 7, 3), pos);
  307. top.Dispose ();
  308. }
  309. [Fact]
  310. [AutoInitShutdown]
  311. public void Hide_Is_Invoke_At_Container_Closing ()
  312. {
  313. var cm = new ContextMenu
  314. {
  315. Position = new Point (80, 25)
  316. };
  317. var menuItems = new MenuBarItem (
  318. [
  319. new MenuItem ("One", "", null),
  320. new MenuItem ("Two", "", null)
  321. ]
  322. );
  323. Toplevel top = new ();
  324. Application.Begin (top);
  325. top.Running = true;
  326. Assert.False (ContextMenu.IsShow);
  327. cm.Show (menuItems);
  328. Assert.True (ContextMenu.IsShow);
  329. top.RequestStop ();
  330. Assert.False (ContextMenu.IsShow);
  331. top.Dispose ();
  332. }
  333. [Fact]
  334. [AutoInitShutdown]
  335. public void Key_Open_And_Close_The_ContextMenu ()
  336. {
  337. var tf = new TextField ();
  338. var top = new Toplevel ();
  339. top.Add (tf);
  340. Application.Begin (top);
  341. Assert.True (Application.RaiseKeyDownEvent (ContextMenu.DefaultKey));
  342. Assert.True (tf.ContextMenu.MenuBar!.IsMenuOpen);
  343. Assert.True (Application.RaiseKeyDownEvent (ContextMenu.DefaultKey));
  344. // The last context menu bar opened is always preserved
  345. Assert.NotNull (tf.ContextMenu.MenuBar);
  346. top.Dispose ();
  347. }
  348. [Fact]
  349. [AutoInitShutdown]
  350. public void KeyChanged_Event ()
  351. {
  352. var oldKey = Key.Empty;
  353. var cm = new ContextMenu ();
  354. cm.KeyChanged += (s, e) => oldKey = e.OldKey;
  355. cm.Key = Key.Space.WithCtrl;
  356. Assert.Equal (Key.Space.WithCtrl, cm.Key);
  357. Assert.Equal (ContextMenu.DefaultKey, oldKey);
  358. }
  359. [Fact]
  360. [AutoInitShutdown]
  361. public void MenuItens_Changing ()
  362. {
  363. var cm = new ContextMenu
  364. {
  365. Position = new Point (10, 5)
  366. };
  367. var menuItems = new MenuBarItem (
  368. [
  369. new MenuItem ("One", "", null),
  370. new MenuItem ("Two", "", null)
  371. ]
  372. );
  373. Toplevel top = new ();
  374. Application.Begin (top);
  375. cm.Show (menuItems);
  376. Application.Refresh ();
  377. var expected = @"
  378. ┌──────┐
  379. │ One │
  380. │ Two │
  381. └──────┘
  382. ";
  383. TestHelpers.AssertDriverContentsAre (expected, output);
  384. menuItems = new MenuBarItem (
  385. [
  386. new MenuItem ("First", "", null),
  387. new MenuItem ("Second", "", null),
  388. new MenuItem ("Third", "", null)
  389. ]
  390. );
  391. cm.Show (menuItems);
  392. Application.Refresh ();
  393. expected = @"
  394. ┌─────────┐
  395. │ First │
  396. │ Second │
  397. │ Third │
  398. └─────────┘
  399. ";
  400. TestHelpers.AssertDriverContentsAre (expected, output);
  401. top.Dispose ();
  402. }
  403. [Fact]
  404. [AutoInitShutdown]
  405. public void Menus_And_SubMenus_Always_Try_To_Be_On_Screen ()
  406. {
  407. var cm = new ContextMenu
  408. {
  409. Position = new Point (-1, -2)
  410. };
  411. var menuItems = new MenuBarItem (
  412. [
  413. new MenuItem ("One", "", null),
  414. new MenuItem ("Two", "", null),
  415. new MenuItem ("Three", "", null),
  416. new MenuBarItem (
  417. "Four",
  418. [
  419. new MenuItem ("SubMenu1", "", null),
  420. new MenuItem ("SubMenu2", "", null),
  421. new MenuItem ("SubMenu3", "", null),
  422. new MenuItem ("SubMenu4", "", null),
  423. new MenuItem ("SubMenu5", "", null),
  424. new MenuItem ("SubMenu6", "", null),
  425. new MenuItem ("SubMenu7", "", null)
  426. ]
  427. ),
  428. new MenuItem ("Five", "", null),
  429. new MenuItem ("Six", "", null)
  430. ]
  431. );
  432. Assert.Equal (new Point (-1, -2), cm.Position);
  433. Toplevel top = new ();
  434. Application.Begin (top);
  435. cm.Show (menuItems);
  436. Application.Refresh ();
  437. Assert.Equal (new Point (-1, -2), cm.Position);
  438. TestHelpers.AssertDriverContentsWithFrameAre (
  439. @"
  440. ┌────────┐
  441. │ One │
  442. │ Two │
  443. │ Three │
  444. │ Four ►│
  445. │ Five │
  446. │ Six │
  447. └────────┘
  448. ",
  449. output
  450. );
  451. Assert.True (
  452. top.Subviews [0]
  453. .NewMouseEvent (
  454. new MouseEvent { Position = new (0, 3), Flags = MouseFlags.ReportMousePosition, View = top.Subviews [0] }
  455. )
  456. );
  457. Application.Refresh ();
  458. Assert.Equal (new Point (-1, -2), cm.Position);
  459. TestHelpers.AssertDriverContentsWithFrameAre (
  460. @"
  461. ┌────────┐
  462. │ One │
  463. │ Two │
  464. │ Three │
  465. │ Four ►│┌───────────┐
  466. │ Five ││ SubMenu1 │
  467. │ Six ││ SubMenu2 │
  468. └────────┘│ SubMenu3 │
  469. │ SubMenu4 │
  470. │ SubMenu5 │
  471. │ SubMenu6 │
  472. │ SubMenu7 │
  473. └───────────┘
  474. ",
  475. output
  476. );
  477. ((FakeDriver)Application.Driver!).SetBufferSize (40, 20);
  478. cm.Position = new Point (41, -2);
  479. cm.Show (menuItems);
  480. Application.Refresh ();
  481. Assert.Equal (new Point (41, -2), cm.Position);
  482. TestHelpers.AssertDriverContentsWithFrameAre (
  483. @"
  484. ┌────────┐
  485. │ One │
  486. │ Two │
  487. │ Three │
  488. │ Four ►│
  489. │ Five │
  490. │ Six │
  491. └────────┘
  492. ",
  493. output
  494. );
  495. Assert.True (
  496. top.Subviews [0]
  497. .NewMouseEvent (
  498. new MouseEvent { Position = new (30, 3), Flags = MouseFlags.ReportMousePosition, View = top.Subviews [0] }
  499. )
  500. );
  501. Application.Refresh ();
  502. Assert.Equal (new Point (41, -2), cm.Position);
  503. TestHelpers.AssertDriverContentsWithFrameAre (
  504. @"
  505. ┌────────┐
  506. │ One │
  507. │ Two │
  508. │ Three │
  509. ┌───────────┐│ Four ►│
  510. │ SubMenu1 ││ Five │
  511. │ SubMenu2 ││ Six │
  512. │ SubMenu3 │└────────┘
  513. │ SubMenu4 │
  514. │ SubMenu5 │
  515. │ SubMenu6 │
  516. │ SubMenu7 │
  517. └───────────┘
  518. ",
  519. output
  520. );
  521. cm.Position = new Point (41, 9);
  522. cm.Show (menuItems);
  523. Application.Refresh ();
  524. Assert.Equal (new Point (41, 9), cm.Position);
  525. TestHelpers.AssertDriverContentsWithFrameAre (
  526. @"
  527. ┌────────┐
  528. │ One │
  529. │ Two │
  530. │ Three │
  531. │ Four ►│
  532. │ Five │
  533. │ Six │
  534. └────────┘
  535. ",
  536. output
  537. );
  538. Assert.True (
  539. top.Subviews [0]
  540. .NewMouseEvent (
  541. new MouseEvent { Position = new (30, 3), Flags = MouseFlags.ReportMousePosition, View = top.Subviews [0] }
  542. )
  543. );
  544. Application.Refresh ();
  545. Assert.Equal (new Point (41, 9), cm.Position);
  546. TestHelpers.AssertDriverContentsWithFrameAre (
  547. @"
  548. ┌────────┐
  549. ┌───────────┐│ One │
  550. │ SubMenu1 ││ Two │
  551. │ SubMenu2 ││ Three │
  552. │ SubMenu3 ││ Four ►│
  553. │ SubMenu4 ││ Five │
  554. │ SubMenu5 ││ Six │
  555. │ SubMenu6 │└────────┘
  556. │ SubMenu7 │
  557. └───────────┘
  558. ",
  559. output
  560. );
  561. cm.Position = new Point (41, 22);
  562. cm.Show (menuItems);
  563. Application.Refresh ();
  564. Assert.Equal (new Point (41, 22), cm.Position);
  565. TestHelpers.AssertDriverContentsWithFrameAre (
  566. @"
  567. ┌────────┐
  568. │ One │
  569. │ Two │
  570. │ Three │
  571. │ Four ►│
  572. │ Five │
  573. │ Six │
  574. └────────┘
  575. ",
  576. output
  577. );
  578. Assert.True (
  579. top.Subviews [0]
  580. .NewMouseEvent (
  581. new MouseEvent { Position = new (30, 3), Flags = MouseFlags.ReportMousePosition, View = top.Subviews [0] }
  582. )
  583. );
  584. Application.Refresh ();
  585. Assert.Equal (new Point (41, 22), cm.Position);
  586. TestHelpers.AssertDriverContentsWithFrameAre (
  587. @"
  588. ┌───────────┐
  589. │ SubMenu1 │┌────────┐
  590. │ SubMenu2 ││ One │
  591. │ SubMenu3 ││ Two │
  592. │ SubMenu4 ││ Three │
  593. │ SubMenu5 ││ Four ►│
  594. │ SubMenu6 ││ Five │
  595. │ SubMenu7 ││ Six │
  596. └───────────┘└────────┘
  597. ",
  598. output
  599. );
  600. ((FakeDriver)Application.Driver!).SetBufferSize (18, 8);
  601. cm.Position = new Point (19, 10);
  602. cm.Show (menuItems);
  603. Application.Refresh ();
  604. Assert.Equal (new Point (19, 10), cm.Position);
  605. TestHelpers.AssertDriverContentsWithFrameAre (
  606. @"
  607. ┌────────┐
  608. │ One │
  609. │ Two │
  610. │ Three │
  611. │ Four ►│
  612. │ Five │
  613. │ Six │
  614. └────────┘
  615. ",
  616. output
  617. );
  618. Assert.True (
  619. top.Subviews [0]
  620. .NewMouseEvent (
  621. new MouseEvent { Position = new (30, 3), Flags = MouseFlags.ReportMousePosition, View = top.Subviews [0] }
  622. )
  623. );
  624. Application.Refresh ();
  625. Assert.Equal (new Point (19, 10), cm.Position);
  626. TestHelpers.AssertDriverContentsWithFrameAre (
  627. @"
  628. ┌───────────┐────┐
  629. │ SubMenu1 │ │
  630. │ SubMenu2 │ │
  631. │ SubMenu3 │ee │
  632. │ SubMenu4 │r ►│
  633. │ SubMenu5 │e │
  634. │ SubMenu6 │ │
  635. │ SubMenu7 │────┘
  636. ",
  637. output
  638. );
  639. top.Dispose ();
  640. }
  641. [Fact]
  642. [AutoInitShutdown]
  643. public void MouseFlags_Changing ()
  644. {
  645. var lbl = new Label { Text = "Original" };
  646. var cm = new ContextMenu ();
  647. lbl.MouseClick += (s, e) =>
  648. {
  649. if (e.MouseEvent.Flags == cm.MouseFlags)
  650. {
  651. lbl.Text = "Replaced";
  652. e.Handled = true;
  653. }
  654. };
  655. Toplevel top = new ();
  656. top.Add (lbl);
  657. Application.Begin (top);
  658. Assert.True (lbl.NewMouseEvent (new MouseEvent { Flags = cm.MouseFlags }));
  659. Assert.Equal ("Replaced", lbl.Text);
  660. lbl.Text = "Original";
  661. cm.MouseFlags = MouseFlags.Button2Clicked;
  662. Assert.True (lbl.NewMouseEvent (new MouseEvent { Flags = cm.MouseFlags }));
  663. Assert.Equal ("Replaced", lbl.Text);
  664. top.Dispose ();
  665. }
  666. [Fact]
  667. public void MouseFlagsChanged_Event ()
  668. {
  669. var oldMouseFlags = new MouseFlags ();
  670. var cm = new ContextMenu ();
  671. cm.MouseFlagsChanged += (s, e) => oldMouseFlags = e.OldValue;
  672. cm.MouseFlags = MouseFlags.Button2Clicked;
  673. Assert.Equal (MouseFlags.Button2Clicked, cm.MouseFlags);
  674. Assert.Equal (MouseFlags.Button3Clicked, oldMouseFlags);
  675. }
  676. [Fact]
  677. [AutoInitShutdown]
  678. public void Position_Changing ()
  679. {
  680. var cm = new ContextMenu
  681. {
  682. Position = new Point (10, 5)
  683. };
  684. var menuItems = new MenuBarItem (
  685. [
  686. new MenuItem ("One", "", null),
  687. new MenuItem ("Two", "", null)
  688. ]
  689. );
  690. Toplevel top = new ();
  691. Application.Begin (top);
  692. cm.Show (menuItems);
  693. Application.Refresh ();
  694. var expected = @"
  695. ┌──────┐
  696. │ One │
  697. │ Two │
  698. └──────┘
  699. ";
  700. TestHelpers.AssertDriverContentsAre (expected, output);
  701. cm.Position = new Point (5, 10);
  702. cm.Show (menuItems);
  703. Application.Refresh ();
  704. expected = @"
  705. ┌──────┐
  706. │ One │
  707. │ Two │
  708. └──────┘
  709. ";
  710. TestHelpers.AssertDriverContentsAre (expected, output);
  711. top.Dispose ();
  712. }
  713. [Fact]
  714. [AutoInitShutdown]
  715. public void RequestStop_While_ContextMenu_Is_Open_Does_Not_Throws ()
  716. {
  717. ContextMenu cm = new ContextMenu
  718. {
  719. Position = new Point (10, 5)
  720. };
  721. var menuItems = new MenuBarItem (
  722. new MenuItem [] { new ("One", "", null), new ("Two", "", null) }
  723. );
  724. Toplevel top = new ();
  725. var isMenuAllClosed = false;
  726. MenuBarItem mi = null;
  727. int iterations = -1;
  728. Application.Iteration += (s, a) =>
  729. {
  730. iterations++;
  731. if (iterations == 0)
  732. {
  733. cm.Show (menuItems);
  734. Assert.True (ContextMenu.IsShow);
  735. mi = cm.MenuBar.Menus [0];
  736. mi.Action = () =>
  737. {
  738. Assert.True (ContextMenu.IsShow);
  739. var dialog1 = new Dialog () { Id = "dialog1" };
  740. Application.Run (dialog1);
  741. dialog1.Dispose ();
  742. Assert.False (ContextMenu.IsShow);
  743. Assert.True (isMenuAllClosed);
  744. };
  745. cm.MenuBar.MenuAllClosed += (_, _) => isMenuAllClosed = true;
  746. }
  747. else if (iterations == 1)
  748. {
  749. mi.Action ();
  750. }
  751. else if (iterations == 2)
  752. {
  753. Application.RequestStop ();
  754. }
  755. else if (iterations == 3)
  756. {
  757. isMenuAllClosed = false;
  758. cm.Show (menuItems);
  759. Assert.True (ContextMenu.IsShow);
  760. cm.MenuBar.MenuAllClosed += (_, _) => isMenuAllClosed = true;
  761. }
  762. else if (iterations == 4)
  763. {
  764. Exception exception = Record.Exception (() => Application.RequestStop ());
  765. Assert.Null (exception);
  766. }
  767. else
  768. {
  769. Application.RequestStop ();
  770. }
  771. };
  772. var isTopClosed = false;
  773. top.Closing += (_, _) =>
  774. {
  775. var dialog2 = new Dialog () { Id = "dialog2" };
  776. Application.Run (dialog2);
  777. dialog2.Dispose ();
  778. Assert.False (ContextMenu.IsShow);
  779. Assert.True (isMenuAllClosed);
  780. isTopClosed = true;
  781. };
  782. Application.Run (top);
  783. Assert.True (isTopClosed);
  784. Assert.False (ContextMenu.IsShow);
  785. Assert.True (isMenuAllClosed);
  786. top.Dispose ();
  787. }
  788. [Fact]
  789. [AutoInitShutdown]
  790. public void Show_Display_At_Zero_If_The_Toplevel_Height_Is_Less_Than_The_Menu_Height ()
  791. {
  792. ((FakeDriver)Application.Driver!).SetBufferSize (80, 3);
  793. var cm = new ContextMenu
  794. {
  795. Position = Point.Empty
  796. };
  797. var menuItems = new MenuBarItem (
  798. [
  799. new MenuItem ("One", "", null),
  800. new MenuItem ("Two", "", null)
  801. ]
  802. );
  803. Assert.Equal (Point.Empty, cm.Position);
  804. Toplevel top = new ();
  805. Application.Begin (top);
  806. cm.Show (menuItems);
  807. Assert.Equal (Point.Empty, cm.Position);
  808. Application.Refresh ();
  809. var expected = @"
  810. ┌──────┐
  811. │ One │
  812. │ Two │";
  813. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  814. Assert.Equal (new Rectangle (0, 0, 8, 3), pos);
  815. cm.Hide ();
  816. Assert.Equal (Point.Empty, cm.Position);
  817. top.Dispose ();
  818. }
  819. [Fact]
  820. [AutoInitShutdown]
  821. public void Show_Display_At_Zero_If_The_Toplevel_Width_Is_Less_Than_The_Menu_Width ()
  822. {
  823. ((FakeDriver)Application.Driver!).SetBufferSize (5, 25);
  824. var cm = new ContextMenu
  825. {
  826. Position = Point.Empty
  827. };
  828. var menuItems = new MenuBarItem (
  829. [
  830. new MenuItem ("One", "", null),
  831. new MenuItem ("Two", "", null)
  832. ]
  833. );
  834. Assert.Equal (Point.Empty, cm.Position);
  835. Toplevel top = new ();
  836. Application.Begin (top);
  837. cm.Show (menuItems);
  838. Assert.Equal (Point.Empty, cm.Position);
  839. Application.Refresh ();
  840. var expected = @"
  841. ┌────
  842. │ One
  843. │ Two
  844. └────";
  845. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  846. Assert.Equal (new Rectangle (0, 1, 5, 4), pos);
  847. cm.Hide ();
  848. Assert.Equal (Point.Empty, cm.Position);
  849. top.Dispose ();
  850. }
  851. [Fact]
  852. [AutoInitShutdown]
  853. public void Show_Display_Below_The_Bottom_Host_If_Has_Enough_Space ()
  854. {
  855. var view = new View
  856. {
  857. X = 10,
  858. Y = 5,
  859. Width = 10,
  860. Height = 1,
  861. Text = "View"
  862. };
  863. var cm = new ContextMenu
  864. {
  865. Host = view,
  866. Position = new Point (10, 5)
  867. };
  868. var menuItems = new MenuBarItem (
  869. [
  870. new MenuItem ("One", "", null),
  871. new MenuItem ("Two", "", null)
  872. ]
  873. );
  874. var top = new Toplevel ();
  875. top.Add (view);
  876. Application.Begin (top);
  877. Assert.Equal (new Point (10, 5), cm.Position);
  878. cm.Show (menuItems);
  879. top.Draw ();
  880. Assert.Equal (new Point (10, 5), cm.Position);
  881. var expected = @"
  882. View
  883. ┌──────┐
  884. │ One │
  885. │ Two │
  886. └──────┘
  887. ";
  888. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  889. Assert.Equal (new Rectangle (10, 5, 18, 5), pos);
  890. cm.Hide ();
  891. Assert.Equal (new Point (10, 5), cm.Position);
  892. cm.Host.X = 5;
  893. cm.Host.Y = 10;
  894. cm.Host.Height = 3;
  895. cm.Show (menuItems);
  896. Application.Top.Draw ();
  897. Assert.Equal (new Point (5, 12), cm.Position);
  898. expected = @"
  899. View
  900. ┌──────┐
  901. │ One │
  902. │ Two │
  903. └──────┘
  904. ";
  905. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  906. Assert.Equal (new Rectangle (5, 10, 13, 7), pos);
  907. cm.Hide ();
  908. Assert.Equal (new Point (5, 12), cm.Position);
  909. top.Dispose ();
  910. }
  911. [Fact]
  912. [AutoInitShutdown]
  913. public void Show_Ensures_Display_Inside_The_Container_But_Preserves_Position ()
  914. {
  915. var cm = new ContextMenu
  916. {
  917. Position = new Point (80, 25)
  918. };
  919. var menuItems = new MenuBarItem (
  920. [
  921. new MenuItem ("One", "", null),
  922. new MenuItem ("Two", "", null)
  923. ]
  924. );
  925. Assert.Equal (new Point (80, 25), cm.Position);
  926. Toplevel top = new ();
  927. Application.Begin (top);
  928. cm.Show (menuItems);
  929. Assert.Equal (new Point (80, 25), cm.Position);
  930. Application.Refresh ();
  931. var expected = @"
  932. ┌──────┐
  933. │ One │
  934. │ Two │
  935. └──────┘
  936. ";
  937. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  938. Assert.Equal (new Rectangle (72, 21, 80, 4), pos);
  939. cm.Hide ();
  940. Assert.Equal (new Point (80, 25), cm.Position);
  941. top.Dispose ();
  942. }
  943. [Fact]
  944. [AutoInitShutdown]
  945. public void Show_Ensures_Display_Inside_The_Container_Without_Overlap_The_Host ()
  946. {
  947. var view = new View
  948. {
  949. X = Pos.AnchorEnd (10),
  950. Y = Pos.AnchorEnd (1),
  951. Width = 10,
  952. Height = 1,
  953. Text = "View"
  954. };
  955. var cm = new ContextMenu
  956. {
  957. Host = view
  958. };
  959. var menuItems = new MenuBarItem (
  960. [
  961. new MenuItem ("One", "", null),
  962. new MenuItem ("Two", "", null)
  963. ]
  964. );
  965. var top = new Toplevel ();
  966. top.Add (view);
  967. Application.Begin (top);
  968. Assert.Equal (new Rectangle (70, 24, 10, 1), view.Frame);
  969. Assert.Equal (Point.Empty, cm.Position);
  970. cm.Show (menuItems);
  971. Assert.Equal (new Point (70, 24), cm.Position);
  972. top.Draw ();
  973. var expected = @"
  974. ┌──────┐
  975. │ One │
  976. │ Two │
  977. └──────┘
  978. View
  979. ";
  980. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  981. Assert.Equal (new Rectangle (70, 20, 78, 5), pos);
  982. cm.Hide ();
  983. Assert.Equal (new Point (70, 24), cm.Position);
  984. top.Dispose ();
  985. }
  986. [Fact]
  987. [AutoInitShutdown]
  988. public void Show_Hide_IsShow ()
  989. {
  990. ContextMenu cm = new ContextMenu
  991. {
  992. Position = new Point (10, 5)
  993. };
  994. var menuItems = new MenuBarItem (
  995. new MenuItem [] { new ("One", "", null), new ("Two", "", null) }
  996. );
  997. Toplevel top = new ();
  998. Application.Begin (top);
  999. cm.Show (menuItems);
  1000. Assert.True (ContextMenu.IsShow);
  1001. Application.Refresh ();
  1002. var expected = @"
  1003. ┌──────┐
  1004. │ One │
  1005. │ Two │
  1006. └──────┘
  1007. ";
  1008. TestHelpers.AssertDriverContentsAre (expected, output);
  1009. cm.Hide ();
  1010. Assert.False (ContextMenu.IsShow);
  1011. Application.Refresh ();
  1012. expected = "";
  1013. TestHelpers.AssertDriverContentsAre (expected, output);
  1014. top.Dispose ();
  1015. }
  1016. [Fact]
  1017. [AutoInitShutdown]
  1018. public void UseSubMenusSingleFrame_True_By_Mouse ()
  1019. {
  1020. var cm = new ContextMenu
  1021. {
  1022. Position = new Point (5, 10),
  1023. UseSubMenusSingleFrame = true
  1024. };
  1025. var menuItems = new MenuBarItem (
  1026. "Numbers",
  1027. [
  1028. new MenuItem ("One", "", null),
  1029. new MenuBarItem (
  1030. "Two",
  1031. [
  1032. new MenuItem (
  1033. "Sub-Menu 1",
  1034. "",
  1035. null
  1036. ),
  1037. new MenuItem ("Sub-Menu 2", "", null)
  1038. ]
  1039. ),
  1040. new MenuItem ("Three", "", null)
  1041. ]
  1042. );
  1043. Toplevel top = new ();
  1044. RunState rs = Application.Begin (top);
  1045. cm.Show (menuItems);
  1046. Assert.Equal (new Rectangle (5, 11, 10, 5), Application.Top!.Subviews [0].Frame);
  1047. Application.Refresh ();
  1048. TestHelpers.AssertDriverContentsWithFrameAre (
  1049. @"
  1050. ┌────────┐
  1051. │ One │
  1052. │ Two ►│
  1053. │ Three │
  1054. └────────┘",
  1055. output
  1056. );
  1057. // X=5 is the border and so need to use at least one more
  1058. Application.RaiseMouseEvent (new MouseEvent { ScreenPosition = new (6, 13), Flags = MouseFlags.Button1Clicked });
  1059. var firstIteration = false;
  1060. Application.RunIteration (ref rs, ref firstIteration);
  1061. Assert.Equal (new Rectangle (5, 11, 10, 5), Application.Top.Subviews [0].Frame);
  1062. Assert.Equal (new Rectangle (5, 11, 15, 6), Application.Top.Subviews [1].Frame);
  1063. TestHelpers.AssertDriverContentsWithFrameAre (
  1064. @"
  1065. ┌─────────────┐
  1066. │◄ Two │
  1067. ├─────────────┤
  1068. │ Sub-Menu 1 │
  1069. │ Sub-Menu 2 │
  1070. └─────────────┘",
  1071. output
  1072. );
  1073. Application.RaiseMouseEvent (new MouseEvent { ScreenPosition = new (6, 12), Flags = MouseFlags.Button1Clicked });
  1074. firstIteration = false;
  1075. Application.RunIteration (ref rs, ref firstIteration);
  1076. Assert.Equal (new Rectangle (5, 11, 10, 5), Application.Top.Subviews [0].Frame);
  1077. TestHelpers.AssertDriverContentsWithFrameAre (
  1078. @"
  1079. ┌────────┐
  1080. │ One │
  1081. │ Two ►│
  1082. │ Three │
  1083. └────────┘",
  1084. output
  1085. );
  1086. Application.End (rs);
  1087. top.Dispose ();
  1088. }
  1089. [Fact]
  1090. [AutoInitShutdown]
  1091. public void UseSubMenusSingleFrame_False_By_Mouse ()
  1092. {
  1093. var cm = new ContextMenu
  1094. {
  1095. Position = new Point (5, 10)
  1096. };
  1097. var menuItems = new MenuBarItem (
  1098. "Numbers",
  1099. [
  1100. new MenuItem ("One", "", null),
  1101. new MenuBarItem (
  1102. "Two",
  1103. [
  1104. new MenuItem (
  1105. "Two-Menu 1",
  1106. "",
  1107. null
  1108. ),
  1109. new MenuItem ("Two-Menu 2", "", null)
  1110. ]
  1111. ),
  1112. new MenuBarItem (
  1113. "Three",
  1114. [
  1115. new MenuItem (
  1116. "Three-Menu 1",
  1117. "",
  1118. null
  1119. ),
  1120. new MenuItem ("Three-Menu 2", "", null)
  1121. ]
  1122. )
  1123. ]
  1124. );
  1125. Toplevel top = new ();
  1126. RunState rs = Application.Begin (top);
  1127. cm.Show (menuItems);
  1128. Assert.Equal (new Rectangle (5, 11, 10, 5), Application.Top.Subviews [0].Frame);
  1129. Application.Refresh ();
  1130. TestHelpers.AssertDriverContentsWithFrameAre (
  1131. @"
  1132. ┌────────┐
  1133. │ One │
  1134. │ Two ►│
  1135. │ Three ►│
  1136. └────────┘",
  1137. output
  1138. );
  1139. Application.RaiseMouseEvent (new MouseEvent { ScreenPosition = new (6, 13), Flags = MouseFlags.ReportMousePosition });
  1140. var firstIteration = false;
  1141. Application.RunIteration (ref rs, ref firstIteration);
  1142. Assert.Equal (new Rectangle (5, 11, 10, 5), Application.Top.Subviews [0].Frame);
  1143. TestHelpers.AssertDriverContentsWithFrameAre (
  1144. @"
  1145. ┌────────┐
  1146. │ One │
  1147. │ Two ►│┌─────────────┐
  1148. │ Three ►││ Two-Menu 1 │
  1149. └────────┘│ Two-Menu 2 │
  1150. └─────────────┘",
  1151. output
  1152. );
  1153. Application.RaiseMouseEvent (new MouseEvent { ScreenPosition = new (6, 14), Flags = MouseFlags.ReportMousePosition });
  1154. firstIteration = false;
  1155. Application.RunIteration (ref rs, ref firstIteration);
  1156. Assert.Equal (new Rectangle (5, 11, 10, 5), Application.Top.Subviews [0].Frame);
  1157. TestHelpers.AssertDriverContentsWithFrameAre (
  1158. @"
  1159. ┌────────┐
  1160. │ One │
  1161. │ Two ►│
  1162. │ Three ►│┌───────────────┐
  1163. └────────┘│ Three-Menu 1 │
  1164. │ Three-Menu 2 │
  1165. └───────────────┘",
  1166. output
  1167. );
  1168. Application.RaiseMouseEvent (new MouseEvent { ScreenPosition = new (6, 13), Flags = MouseFlags.ReportMousePosition });
  1169. firstIteration = false;
  1170. Application.RunIteration (ref rs, ref firstIteration);
  1171. Assert.Equal (new Rectangle (5, 11, 10, 5), Application.Top.Subviews [0].Frame);
  1172. TestHelpers.AssertDriverContentsWithFrameAre (
  1173. @"
  1174. ┌────────┐
  1175. │ One │
  1176. │ Two ►│┌─────────────┐
  1177. │ Three ►││ Two-Menu 1 │
  1178. └────────┘│ Two-Menu 2 │
  1179. └─────────────┘",
  1180. output
  1181. );
  1182. Application.End (rs);
  1183. top.Dispose ();
  1184. }
  1185. [Fact]
  1186. [AutoInitShutdown]
  1187. public void Handling_TextField_With_Opened_ContextMenu_By_Mouse_HasFocus ()
  1188. {
  1189. var tf1 = new TextField { Width = 10, Text = "TextField 1" };
  1190. var tf2 = new TextField { Y = 2, Width = 10, Text = "TextField 2" };
  1191. var win = new Window ();
  1192. win.Add (tf1, tf2);
  1193. var rs = Application.Begin (win);
  1194. Assert.True (tf1.HasFocus);
  1195. Assert.False (tf2.HasFocus);
  1196. Assert.Equal (4, win.Subviews.Count); // TF & TV add autocomplete popup's to their superviews.
  1197. Assert.Empty (Application._cachedViewsUnderMouse);
  1198. // Right click on tf2 to open context menu
  1199. Application.RaiseMouseEvent (new () { ScreenPosition = new (1, 3), Flags = MouseFlags.Button3Clicked });
  1200. Assert.False (tf1.HasFocus);
  1201. Assert.False (tf2.HasFocus);
  1202. Assert.Equal (5, win.Subviews.Count);
  1203. Assert.True (tf2.ContextMenu.MenuBar.IsMenuOpen);
  1204. Assert.True (win.Focused is Menu);
  1205. Assert.True (Application.MouseGrabView is MenuBar);
  1206. Assert.Equal (tf2, Application._cachedViewsUnderMouse.LastOrDefault ());
  1207. // Click on tf1 to focus it, which cause context menu being closed
  1208. Application.RaiseMouseEvent (new () { ScreenPosition = new (1, 1), Flags = MouseFlags.Button1Clicked });
  1209. Assert.True (tf1.HasFocus);
  1210. Assert.False (tf2.HasFocus);
  1211. Assert.Equal (4, win.Subviews.Count);
  1212. // The last context menu bar opened is always preserved
  1213. Assert.NotNull (tf2.ContextMenu.MenuBar);
  1214. Assert.Equal (win.Focused, tf1);
  1215. Assert.Null (Application.MouseGrabView);
  1216. Assert.Equal (tf1, Application._cachedViewsUnderMouse.LastOrDefault ());
  1217. // Click on tf2 to focus it
  1218. Application.RaiseMouseEvent (new () { ScreenPosition = new (1, 3), Flags = MouseFlags.Button1Clicked });
  1219. Assert.False (tf1.HasFocus);
  1220. Assert.True (tf2.HasFocus);
  1221. Assert.Equal (4, win.Subviews.Count);
  1222. // The last context menu bar opened is always preserved
  1223. Assert.NotNull (tf2.ContextMenu.MenuBar);
  1224. Assert.Equal (win.Focused, tf2);
  1225. Assert.Null (Application.MouseGrabView);
  1226. Assert.Equal (tf2, Application._cachedViewsUnderMouse.LastOrDefault ());
  1227. Application.End (rs);
  1228. win.Dispose ();
  1229. }
  1230. [Fact]
  1231. [AutoInitShutdown]
  1232. public void Empty_Menus_Items_Children_Does_Not_Open_The_Menu ()
  1233. {
  1234. var cm = new ContextMenu ();
  1235. Assert.Null (cm.MenuItems);
  1236. var top = new Toplevel ();
  1237. Application.Begin (top);
  1238. cm.Show (cm.MenuItems);
  1239. Assert.Null (cm.MenuBar);
  1240. top.Dispose ();
  1241. }
  1242. [Fact]
  1243. [AutoInitShutdown]
  1244. public void KeyBindings_Removed_On_Close_ContextMenu ()
  1245. {
  1246. var newFile = false;
  1247. var renameFile = false;
  1248. var deleteFile = false;
  1249. var cm = new ContextMenu ();
  1250. var menuItems = new MenuBarItem (
  1251. [
  1252. new MenuItem ("New File", string.Empty, New, null, null, Key.N.WithCtrl),
  1253. new MenuItem ("Rename File", string.Empty, Rename, null, null, Key.R.WithCtrl),
  1254. new MenuItem ("Delete File", string.Empty, Delete, null, null, Key.D.WithCtrl)
  1255. ]
  1256. );
  1257. var top = new Toplevel ();
  1258. Application.Begin (top);
  1259. Assert.Null (cm.MenuBar);
  1260. Assert.False (Application.RaiseKeyDownEvent (Key.N.WithCtrl));
  1261. Assert.False (Application.RaiseKeyDownEvent (Key.R.WithCtrl));
  1262. Assert.False (Application.RaiseKeyDownEvent (Key.D.WithCtrl));
  1263. Assert.False (newFile);
  1264. Assert.False (renameFile);
  1265. Assert.False (deleteFile);
  1266. cm.Show (menuItems);
  1267. Assert.True (cm.MenuBar!.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
  1268. Assert.True (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithCtrl));
  1269. Assert.True (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.D.WithCtrl));
  1270. Assert.True (Application.RaiseKeyDownEvent (Key.N.WithCtrl));
  1271. Application.MainLoop!.RunIteration ();
  1272. Assert.True (newFile);
  1273. Assert.False (cm.MenuBar!.IsMenuOpen);
  1274. cm.Show (menuItems);
  1275. Assert.True (Application.RaiseKeyDownEvent (Key.R.WithCtrl));
  1276. Application.MainLoop!.RunIteration ();
  1277. Assert.True (renameFile);
  1278. Assert.False (cm.MenuBar.IsMenuOpen);
  1279. cm.Show (menuItems);
  1280. Assert.True (Application.RaiseKeyDownEvent (Key.D.WithCtrl));
  1281. Application.MainLoop!.RunIteration ();
  1282. Assert.True (deleteFile);
  1283. Assert.False (cm.MenuBar.IsMenuOpen);
  1284. Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
  1285. Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithCtrl));
  1286. Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.D.WithCtrl));
  1287. newFile = false;
  1288. renameFile = false;
  1289. deleteFile = false;
  1290. Assert.False (Application.RaiseKeyDownEvent (Key.N.WithCtrl));
  1291. Assert.False (Application.RaiseKeyDownEvent (Key.R.WithCtrl));
  1292. Assert.False (Application.RaiseKeyDownEvent (Key.D.WithCtrl));
  1293. Assert.False (newFile);
  1294. Assert.False (renameFile);
  1295. Assert.False (deleteFile);
  1296. top.Dispose ();
  1297. void New () { newFile = true; }
  1298. void Rename () { renameFile = true; }
  1299. void Delete () { deleteFile = true; }
  1300. }
  1301. [Fact]
  1302. [AutoInitShutdown]
  1303. public void KeyBindings_With_ContextMenu_And_MenuBar ()
  1304. {
  1305. var newFile = false;
  1306. var renameFile = false;
  1307. var menuBar = new MenuBar
  1308. {
  1309. Menus =
  1310. [
  1311. new (
  1312. "File",
  1313. new MenuItem []
  1314. {
  1315. new ("New", string.Empty, New, null, null, Key.N.WithCtrl)
  1316. })
  1317. ]
  1318. };
  1319. var cm = new ContextMenu ();
  1320. var menuItems = new MenuBarItem (
  1321. [
  1322. new ("Rename File", string.Empty, Rename, null, null, Key.R.WithCtrl),
  1323. ]
  1324. );
  1325. var top = new Toplevel ();
  1326. top.Add (menuBar);
  1327. Application.Begin (top);
  1328. Assert.True (menuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
  1329. Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithCtrl));
  1330. Assert.Null (cm.MenuBar);
  1331. Assert.True (Application.RaiseKeyDownEvent (Key.N.WithCtrl));
  1332. Assert.False (Application.RaiseKeyDownEvent (Key.R.WithCtrl));
  1333. Application.MainLoop!.RunIteration ();
  1334. Assert.True (newFile);
  1335. Assert.False (renameFile);
  1336. newFile = false;
  1337. cm.Show (menuItems);
  1338. Assert.True (menuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
  1339. Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithCtrl));
  1340. Assert.False (cm.MenuBar!.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
  1341. Assert.True (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithCtrl));
  1342. Assert.True (cm.MenuBar.IsMenuOpen);
  1343. Assert.True (Application.RaiseKeyDownEvent (Key.N.WithCtrl));
  1344. Application.MainLoop!.RunIteration ();
  1345. Assert.True (newFile);
  1346. Assert.False (cm.MenuBar!.IsMenuOpen);
  1347. cm.Show (menuItems);
  1348. Assert.True (Application.RaiseKeyDownEvent (Key.R.WithCtrl));
  1349. Application.MainLoop!.RunIteration ();
  1350. Assert.True (renameFile);
  1351. Assert.False (cm.MenuBar.IsMenuOpen);
  1352. Assert.True (menuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
  1353. Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithCtrl));
  1354. Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
  1355. Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithCtrl));
  1356. newFile = false;
  1357. renameFile = false;
  1358. Assert.True (Application.RaiseKeyDownEvent (Key.N.WithCtrl));
  1359. Assert.False (Application.RaiseKeyDownEvent (Key.R.WithCtrl));
  1360. Application.MainLoop!.RunIteration ();
  1361. Assert.True (newFile);
  1362. Assert.False (renameFile);
  1363. top.Dispose ();
  1364. void New () { newFile = true; }
  1365. void Rename () { renameFile = true; }
  1366. }
  1367. [Fact]
  1368. [AutoInitShutdown]
  1369. public void KeyBindings_With_Same_Shortcut_ContextMenu_And_MenuBar ()
  1370. {
  1371. var newMenuBar = false;
  1372. var newContextMenu = false;
  1373. var menuBar = new MenuBar
  1374. {
  1375. Menus =
  1376. [
  1377. new (
  1378. "File",
  1379. new MenuItem []
  1380. {
  1381. new ("New", string.Empty, NewMenuBar, null, null, Key.N.WithCtrl)
  1382. })
  1383. ]
  1384. };
  1385. var cm = new ContextMenu ();
  1386. var menuItems = new MenuBarItem (
  1387. [
  1388. new ("New File", string.Empty, NewContextMenu, null, null, Key.N.WithCtrl),
  1389. ]
  1390. );
  1391. var top = new Toplevel ();
  1392. top.Add (menuBar);
  1393. Application.Begin (top);
  1394. Assert.True (menuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
  1395. Assert.Null (cm.MenuBar);
  1396. Assert.True (Application.RaiseKeyDownEvent (Key.N.WithCtrl));
  1397. Application.MainLoop!.RunIteration ();
  1398. Assert.True (newMenuBar);
  1399. Assert.False (newContextMenu);
  1400. newMenuBar = false;
  1401. cm.Show (menuItems);
  1402. Assert.True (menuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
  1403. Assert.True (cm.MenuBar!.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
  1404. Assert.True (cm.MenuBar.IsMenuOpen);
  1405. Assert.True (Application.RaiseKeyDownEvent (Key.N.WithCtrl));
  1406. Application.MainLoop!.RunIteration ();
  1407. Assert.False (newMenuBar);
  1408. // The most focused shortcut is executed
  1409. Assert.True (newContextMenu);
  1410. Assert.False (cm.MenuBar!.IsMenuOpen);
  1411. Assert.True (menuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
  1412. Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
  1413. newMenuBar = false;
  1414. newContextMenu = false;
  1415. Assert.True (Application.RaiseKeyDownEvent (Key.N.WithCtrl));
  1416. Application.MainLoop!.RunIteration ();
  1417. Assert.True (newMenuBar);
  1418. Assert.False (newContextMenu);
  1419. top.Dispose ();
  1420. void NewMenuBar () { newMenuBar = true; }
  1421. void NewContextMenu () { newContextMenu = true; }
  1422. }
  1423. [Fact]
  1424. [AutoInitShutdown]
  1425. public void HotKeys_Removed_On_Close_ContextMenu ()
  1426. {
  1427. var newFile = false;
  1428. var renameFile = false;
  1429. var deleteFile = false;
  1430. var cm = new ContextMenu ();
  1431. var menuItems = new MenuBarItem (
  1432. [
  1433. new ("_New File", string.Empty, New, null, null),
  1434. new ("_Rename File", string.Empty, Rename, null, null),
  1435. new ("_Delete File", string.Empty, Delete, null, null)
  1436. ]
  1437. );
  1438. var top = new Toplevel ();
  1439. Application.Begin (top);
  1440. Assert.Null (cm.MenuBar);
  1441. Assert.False (Application.RaiseKeyDownEvent (Key.N.WithAlt));
  1442. Assert.False (Application.RaiseKeyDownEvent (Key.R.WithAlt));
  1443. Assert.False (Application.RaiseKeyDownEvent (Key.D.WithAlt));
  1444. Assert.False (newFile);
  1445. Assert.False (renameFile);
  1446. Assert.False (deleteFile);
  1447. cm.Show (menuItems);
  1448. Assert.True (cm.MenuBar!.IsMenuOpen);
  1449. Assert.False (cm.MenuBar!.KeyBindings.Bindings.ContainsKey (Key.N.WithAlt));
  1450. Assert.False (cm.MenuBar!.KeyBindings.Bindings.ContainsKey (Key.N.NoShift));
  1451. Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithAlt));
  1452. Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.NoShift));
  1453. Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.D.WithAlt));
  1454. Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.D.NoShift));
  1455. Assert.Single (Application.Top!.Subviews);
  1456. View [] menus = Application.Top!.Subviews.Where (v => v is Menu m && m.Host == cm.MenuBar).ToArray ();
  1457. Assert.True (menus [0].KeyBindings.Bindings.ContainsKey (Key.N.WithAlt));
  1458. Assert.True (menus [0].KeyBindings.Bindings.ContainsKey (Key.N.NoShift));
  1459. Assert.True (menus [0].KeyBindings.Bindings.ContainsKey (Key.R.WithAlt));
  1460. Assert.True (menus [0].KeyBindings.Bindings.ContainsKey (Key.R.NoShift));
  1461. Assert.True (menus [0].KeyBindings.Bindings.ContainsKey (Key.D.WithAlt));
  1462. Assert.True (menus [0].KeyBindings.Bindings.ContainsKey (Key.D.NoShift));
  1463. Assert.True (Application.RaiseKeyDownEvent (Key.N.WithAlt));
  1464. Assert.False (cm.MenuBar!.IsMenuOpen);
  1465. Application.MainLoop!.RunIteration ();
  1466. Assert.True (newFile);
  1467. cm.Show (menuItems);
  1468. Assert.True (Application.RaiseKeyDownEvent (Key.R.WithAlt));
  1469. Assert.False (cm.MenuBar.IsMenuOpen);
  1470. Application.MainLoop!.RunIteration ();
  1471. Assert.True (renameFile);
  1472. cm.Show (menuItems);
  1473. Assert.True (Application.RaiseKeyDownEvent (Key.D.WithAlt));
  1474. Assert.False (cm.MenuBar.IsMenuOpen);
  1475. Application.MainLoop!.RunIteration ();
  1476. Assert.True (deleteFile);
  1477. Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithAlt));
  1478. Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.N.NoShift));
  1479. Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithAlt));
  1480. Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.NoShift));
  1481. Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.D.WithAlt));
  1482. Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.D.NoShift));
  1483. newFile = false;
  1484. renameFile = false;
  1485. deleteFile = false;
  1486. Assert.False (Application.RaiseKeyDownEvent (Key.N.WithAlt));
  1487. Assert.False (Application.RaiseKeyDownEvent (Key.R.WithAlt));
  1488. Assert.False (Application.RaiseKeyDownEvent (Key.D.WithAlt));
  1489. Assert.False (newFile);
  1490. Assert.False (renameFile);
  1491. Assert.False (deleteFile);
  1492. top.Dispose ();
  1493. void New () { newFile = true; }
  1494. void Rename () { renameFile = true; }
  1495. void Delete () { deleteFile = true; }
  1496. }
  1497. [Fact]
  1498. [AutoInitShutdown]
  1499. public void HotKeys_With_ContextMenu_And_MenuBar ()
  1500. {
  1501. var newFile = false;
  1502. var renameFile = false;
  1503. var menuBar = new MenuBar
  1504. {
  1505. Menus =
  1506. [
  1507. new (
  1508. "_File",
  1509. new MenuItem []
  1510. {
  1511. new ("_New", string.Empty, New)
  1512. })
  1513. ]
  1514. };
  1515. var cm = new ContextMenu ();
  1516. var menuItems = new MenuBarItem (
  1517. [
  1518. new MenuBarItem (
  1519. "_Edit",
  1520. new MenuItem []
  1521. {
  1522. new ("_Rename File", string.Empty, Rename)
  1523. }
  1524. )
  1525. ]
  1526. );
  1527. var top = new Toplevel ();
  1528. top.Add (menuBar);
  1529. Application.Begin (top);
  1530. Assert.True (menuBar.KeyBindings.Bindings.ContainsKey (Key.F.WithAlt));
  1531. Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithAlt));
  1532. Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithAlt));
  1533. View [] menus = Application.Top!.Subviews.Where (v => v is Menu m && m.Host == menuBar).ToArray ();
  1534. Assert.Empty (menus);
  1535. Assert.Null (cm.MenuBar);
  1536. Assert.True (Application.RaiseKeyDownEvent (Key.F.WithAlt));
  1537. Assert.True (menuBar.IsMenuOpen);
  1538. Assert.Equal (2, Application.Top!.Subviews.Count);
  1539. menus = Application.Top!.Subviews.Where (v => v is Menu m && m.Host == menuBar).ToArray ();
  1540. Assert.True (menus [0].KeyBindings.Bindings.ContainsKey (Key.N.WithAlt));
  1541. Assert.True (Application.RaiseKeyDownEvent (Key.N.WithAlt));
  1542. Assert.False (menuBar.IsMenuOpen);
  1543. Assert.False (Application.RaiseKeyDownEvent (Key.R.WithAlt));
  1544. Application.MainLoop!.RunIteration ();
  1545. Assert.True (newFile);
  1546. Assert.False (renameFile);
  1547. newFile = false;
  1548. cm.Show (menuItems);
  1549. Assert.True (menuBar.KeyBindings.Bindings.ContainsKey (Key.F.WithAlt));
  1550. Assert.True (menuBar.KeyBindings.Bindings.ContainsKey (Key.F.NoShift));
  1551. Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithAlt));
  1552. Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.N.NoShift));
  1553. Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.E.WithAlt));
  1554. Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.E.NoShift));
  1555. Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithAlt));
  1556. Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.R.NoShift));
  1557. Assert.True (cm.MenuBar!.IsMenuOpen);
  1558. Assert.False (cm.MenuBar!.KeyBindings.Bindings.ContainsKey (Key.F.WithAlt));
  1559. Assert.False (cm.MenuBar!.KeyBindings.Bindings.ContainsKey (Key.F.NoShift));
  1560. Assert.False (cm.MenuBar!.KeyBindings.Bindings.ContainsKey (Key.N.WithAlt));
  1561. Assert.False (cm.MenuBar!.KeyBindings.Bindings.ContainsKey (Key.N.NoShift));
  1562. Assert.False (cm.MenuBar!.KeyBindings.Bindings.ContainsKey (Key.E.WithAlt));
  1563. Assert.False (cm.MenuBar!.KeyBindings.Bindings.ContainsKey (Key.E.NoShift));
  1564. Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithAlt));
  1565. Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.NoShift));
  1566. Assert.Equal (3, Application.Top!.Subviews.Count);
  1567. menus = Application.Top!.Subviews.Where (v => v is Menu m && m.Host == cm.MenuBar).ToArray ();
  1568. Assert.True (menus [0].KeyBindings.Bindings.ContainsKey (Key.E.WithAlt));
  1569. Assert.True (menus [0].KeyBindings.Bindings.ContainsKey (Key.E.NoShift));
  1570. Assert.True (menus [1].KeyBindings.Bindings.ContainsKey (Key.R.WithAlt));
  1571. Assert.True (menus [1].KeyBindings.Bindings.ContainsKey (Key.R.NoShift));
  1572. Assert.True (cm.MenuBar.IsMenuOpen);
  1573. Assert.True (Application.RaiseKeyDownEvent (Key.F.WithAlt));
  1574. Assert.False (cm.MenuBar.IsMenuOpen);
  1575. Assert.True (Application.RaiseKeyDownEvent (Key.N.WithAlt));
  1576. Application.MainLoop!.RunIteration ();
  1577. Assert.True (newFile);
  1578. cm.Show (menuItems);
  1579. Assert.True (cm.MenuBar.IsMenuOpen);
  1580. Assert.Equal (3, Application.Top!.Subviews.Count);
  1581. menus = Application.Top!.Subviews.Where (v => v is Menu m && m.Host == cm.MenuBar).ToArray ();
  1582. Assert.True (menus [0].KeyBindings.Bindings.ContainsKey (Key.E.WithAlt));
  1583. Assert.True (menus [0].KeyBindings.Bindings.ContainsKey (Key.E.NoShift));
  1584. Assert.False (menus [0].KeyBindings.Bindings.ContainsKey (Key.R.WithAlt));
  1585. Assert.False (menus [0].KeyBindings.Bindings.ContainsKey (Key.R.NoShift));
  1586. Assert.False (menus [1].KeyBindings.Bindings.ContainsKey (Key.E.WithAlt));
  1587. Assert.False (menus [1].KeyBindings.Bindings.ContainsKey (Key.E.NoShift));
  1588. Assert.True (menus [1].KeyBindings.Bindings.ContainsKey (Key.R.WithAlt));
  1589. Assert.True (menus [1].KeyBindings.Bindings.ContainsKey (Key.R.NoShift));
  1590. Assert.True (Application.RaiseKeyDownEvent (Key.E.NoShift));
  1591. Assert.True (Application.RaiseKeyDownEvent (Key.R.WithAlt));
  1592. Assert.False (cm.MenuBar.IsMenuOpen);
  1593. Application.MainLoop!.RunIteration ();
  1594. Assert.True (renameFile);
  1595. Assert.Single (Application.Top!.Subviews);
  1596. Assert.True (menuBar.KeyBindings.Bindings.ContainsKey (Key.F.WithAlt));
  1597. Assert.True (menuBar.KeyBindings.Bindings.ContainsKey (Key.F.NoShift));
  1598. Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithAlt));
  1599. Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.N.NoShift));
  1600. Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.E.WithAlt));
  1601. Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.E.NoShift));
  1602. Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithAlt));
  1603. Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.NoShift));
  1604. newFile = false;
  1605. renameFile = false;
  1606. Assert.True (Application.RaiseKeyDownEvent (Key.F.WithAlt));
  1607. Assert.True (Application.RaiseKeyDownEvent (Key.N.WithAlt));
  1608. Assert.False (Application.RaiseKeyDownEvent (Key.R.WithAlt));
  1609. Application.MainLoop!.RunIteration ();
  1610. Assert.True (newFile);
  1611. Assert.False (renameFile);
  1612. top.Dispose ();
  1613. void New () { newFile = true; }
  1614. void Rename () { renameFile = true; }
  1615. }
  1616. [Fact]
  1617. [AutoInitShutdown]
  1618. public void Opened_MenuBar_Is_Closed_When_Another_MenuBar_Is_Opening_Also_By_HotKey ()
  1619. {
  1620. var menuBar = new MenuBar
  1621. {
  1622. Menus =
  1623. [
  1624. new (
  1625. "_File",
  1626. new MenuItem []
  1627. {
  1628. new ("_New", string.Empty, null)
  1629. })
  1630. ]
  1631. };
  1632. var cm = new ContextMenu ();
  1633. var menuItems = new MenuBarItem (
  1634. [
  1635. new MenuBarItem (
  1636. "_Edit",
  1637. new MenuItem []
  1638. {
  1639. new ("_Rename File", string.Empty, null)
  1640. }
  1641. )
  1642. ]
  1643. );
  1644. var top = new Toplevel ();
  1645. top.Add (menuBar);
  1646. Application.Begin (top);
  1647. Assert.True (Application.RaiseKeyDownEvent (Key.F.WithAlt));
  1648. Assert.True (menuBar.IsMenuOpen);
  1649. cm.Show (menuItems);
  1650. Assert.False (menuBar.IsMenuOpen);
  1651. Assert.True (cm.MenuBar!.IsMenuOpen);
  1652. Assert.True (Application.RaiseKeyDownEvent (Key.F.WithAlt));
  1653. Assert.True (menuBar.IsMenuOpen);
  1654. Assert.False (cm.MenuBar!.IsMenuOpen);
  1655. top.Dispose ();
  1656. }
  1657. }