ContextMenuTests.cs 73 KB

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