MenuBarv1Tests.cs 122 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412
  1. using UnitTests;
  2. using Xunit.Abstractions;
  3. namespace UnitTests.ViewsTests;
  4. #pragma warning disable CS0618 // Type or member is obsolete
  5. public class MenuBarv1Tests (ITestOutputHelper output)
  6. {
  7. [Fact]
  8. [AutoInitShutdown]
  9. public void AddMenuBarItem_RemoveMenuItem_Dynamically ()
  10. {
  11. var menuBar = new MenuBar ();
  12. var menuBarItem = new MenuBarItem { Title = "_New" };
  13. var action = "";
  14. var menuItem = new MenuItem { Title = "_Item", Action = () => action = "I", Parent = menuBarItem };
  15. Assert.Equal ("n", menuBarItem.HotKey);
  16. Assert.Equal ("i", menuItem.HotKey);
  17. Assert.Empty (menuBar.Menus);
  18. menuBarItem.AddMenuBarItem (menuBar, menuItem);
  19. menuBar.Menus = [menuBarItem];
  20. Assert.Single (menuBar.Menus);
  21. Assert.Single (menuBar.Menus [0].Children!);
  22. Assert.True (menuBar.HotKeyBindings.TryGet (Key.N.WithAlt, out _));
  23. Assert.False (menuBar.HotKeyBindings.TryGet (Key.I, out _));
  24. var top = new Toplevel ();
  25. top.Add (menuBar);
  26. Application.Begin (top);
  27. top.NewKeyDownEvent (Key.N.WithAlt);
  28. AutoInitShutdownAttribute.RunIteration ();
  29. Assert.True (menuBar.IsMenuOpen);
  30. Assert.Equal ("", action);
  31. top.NewKeyDownEvent (Key.I);
  32. AutoInitShutdownAttribute.RunIteration ();
  33. Assert.False (menuBar.IsMenuOpen);
  34. Assert.Equal ("I", action);
  35. menuItem.RemoveMenuItem ();
  36. Assert.Single (menuBar.Menus);
  37. Assert.Null (menuBar.Menus [0].Children);
  38. Assert.True (menuBar.HotKeyBindings.TryGet (Key.N.WithAlt, out _));
  39. Assert.False (menuBar.HotKeyBindings.TryGet (Key.I, out _));
  40. menuBarItem.RemoveMenuItem ();
  41. Assert.Empty (menuBar.Menus);
  42. Assert.False (menuBar.HotKeyBindings.TryGet (Key.N.WithAlt, out _));
  43. top.Dispose ();
  44. }
  45. [Fact]
  46. [AutoInitShutdown]
  47. public void AllowNullChecked_Get_Set ()
  48. {
  49. var mi = new MenuItem ("Check this out 你", "", null) { CheckType = MenuItemCheckStyle.Checked };
  50. mi.Action = mi.ToggleChecked;
  51. var menu = new MenuBar
  52. {
  53. Menus =
  54. [
  55. new ("Nullable Checked", new [] { mi })
  56. ]
  57. };
  58. //new CheckBox ();
  59. Toplevel top = new ();
  60. top.Add (menu);
  61. Application.Begin (top);
  62. Assert.False (mi.Checked);
  63. Assert.True (menu.NewKeyDownEvent (menu.Key));
  64. Assert.True (menu._openMenu.NewKeyDownEvent (Key.Enter));
  65. AutoInitShutdownAttribute.RunIteration ();
  66. Assert.True (mi.Checked);
  67. Assert.True (
  68. menu.NewMouseEvent (
  69. new () { Position = new (0, 0), Flags = MouseFlags.Button1Pressed, View = menu }
  70. )
  71. );
  72. Assert.True (
  73. menu._openMenu.NewMouseEvent (
  74. new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked, View = menu._openMenu }
  75. )
  76. );
  77. AutoInitShutdownAttribute.RunIteration ();
  78. Assert.False (mi.Checked);
  79. mi.AllowNullChecked = true;
  80. Assert.True (menu.NewKeyDownEvent (menu.Key));
  81. Assert.True (menu._openMenu.NewKeyDownEvent (Key.Enter));
  82. AutoInitShutdownAttribute.RunIteration ();
  83. Assert.Null (mi.Checked);
  84. Assert.True (
  85. menu.NewMouseEvent (
  86. new () { Position = new (0, 0), Flags = MouseFlags.Button1Pressed, View = menu }
  87. )
  88. );
  89. AutoInitShutdownAttribute.RunIteration ();
  90. DriverAssert.AssertDriverContentsWithFrameAre (
  91. @$"
  92. Nullable Checked
  93. ┌──────────────────────┐
  94. │ {Glyphs.CheckStateNone} Check this out 你 │
  95. └──────────────────────┘",
  96. output
  97. );
  98. Assert.True (
  99. menu._openMenu.NewMouseEvent (
  100. new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked, View = menu._openMenu }
  101. )
  102. );
  103. AutoInitShutdownAttribute.RunIteration ();
  104. Assert.True (mi.Checked);
  105. Assert.True (menu.NewKeyDownEvent (menu.Key));
  106. Assert.True (menu._openMenu.NewKeyDownEvent (Key.Enter));
  107. AutoInitShutdownAttribute.RunIteration ();
  108. Assert.False (mi.Checked);
  109. Assert.True (
  110. menu.NewMouseEvent (
  111. new () { Position = new (0, 0), Flags = MouseFlags.Button1Pressed, View = menu }
  112. )
  113. );
  114. Assert.True (
  115. menu._openMenu.NewMouseEvent (
  116. new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked, View = menu._openMenu }
  117. )
  118. );
  119. AutoInitShutdownAttribute.RunIteration ();
  120. Assert.Null (mi.Checked);
  121. mi.AllowNullChecked = false;
  122. Assert.False (mi.Checked);
  123. mi.CheckType = MenuItemCheckStyle.NoCheck;
  124. Assert.Throws<InvalidOperationException> (mi.ToggleChecked);
  125. mi.CheckType = MenuItemCheckStyle.Radio;
  126. Assert.Throws<InvalidOperationException> (mi.ToggleChecked);
  127. top.Dispose ();
  128. }
  129. [Fact]
  130. [AutoInitShutdown]
  131. public void CanExecute_False_Does_Not_Throws ()
  132. {
  133. var menu = new MenuBar
  134. {
  135. Menus =
  136. [
  137. new ("File", new MenuItem []
  138. {
  139. new ("New", "", null, () => false),
  140. null,
  141. new ("Quit", "", null)
  142. })
  143. ]
  144. };
  145. var top = new Toplevel ();
  146. top.Add (menu);
  147. Application.Begin (top);
  148. Assert.True (menu.NewKeyDownEvent (menu.Key));
  149. Assert.True (menu.IsMenuOpen);
  150. top.Dispose ();
  151. }
  152. [Fact]
  153. [AutoInitShutdown]
  154. public void CanExecute_HotKey ()
  155. {
  156. Window win = null;
  157. var menu = new MenuBar
  158. {
  159. Menus =
  160. [
  161. new (
  162. "_File",
  163. new MenuItem []
  164. {
  165. new ("_New", "", New, CanExecuteNew),
  166. new (
  167. "_Close",
  168. "",
  169. Close,
  170. CanExecuteClose
  171. )
  172. }
  173. )
  174. ]
  175. };
  176. Toplevel top = new ();
  177. top.Add (menu);
  178. bool CanExecuteNew () { return win == null; }
  179. void New () { win = new (); }
  180. bool CanExecuteClose () { return win != null; }
  181. void Close () { win = null; }
  182. Application.Begin (top);
  183. Assert.Null (win);
  184. Assert.True (CanExecuteNew ());
  185. Assert.False (CanExecuteClose ());
  186. Assert.True (top.NewKeyDownEvent (Key.F.WithAlt));
  187. Assert.True (top.NewKeyDownEvent (Key.N.WithAlt));
  188. AutoInitShutdownAttribute.RunIteration ();
  189. Assert.NotNull (win);
  190. Assert.False (CanExecuteNew ());
  191. Assert.True (CanExecuteClose ());
  192. top.Dispose ();
  193. }
  194. [Fact]
  195. [AutoInitShutdown]
  196. public void Click_Another_View_Close_An_Open_Menu ()
  197. {
  198. var menu = new MenuBar
  199. {
  200. Menus =
  201. [
  202. new ("File", new MenuItem [] { new ("New", "", null) })
  203. ]
  204. };
  205. var btnClicked = false;
  206. var btn = new Button { Y = 4, Text = "Test" };
  207. btn.Accepting += (s, e) => btnClicked = true;
  208. var top = new Toplevel ();
  209. top.Add (menu, btn);
  210. Application.Begin (top);
  211. Application.RaiseMouseEvent (new () { ScreenPosition = new (0, 4), Flags = MouseFlags.Button1Clicked });
  212. Assert.True (btnClicked);
  213. top.Dispose ();
  214. }
  215. // TODO: Lots of tests in here really test Menu and MenuItem - Move them to MenuTests.cs
  216. [Fact]
  217. public void Constructors_Defaults ()
  218. {
  219. var menuBar = new MenuBar ();
  220. Assert.Equal (KeyCode.F9, menuBar.Key);
  221. var menu = new Menu { Host = menuBar, X = 0, Y = 0, BarItems = new () };
  222. Assert.False (menu.HasScheme);
  223. Assert.False (menu.IsInitialized);
  224. menu.BeginInit ();
  225. menu.EndInit ();
  226. Assert.True (menu.CanFocus);
  227. Assert.False (menu.WantContinuousButtonPressed);
  228. Assert.Equal (LineStyle.Single, menuBar.MenusBorderStyle);
  229. menuBar = new ();
  230. Assert.Equal (0, menuBar.X);
  231. Assert.Equal (0, menuBar.Y);
  232. Assert.IsType<DimFill> (menuBar.Width);
  233. Assert.Equal (1, menuBar.Height);
  234. Assert.Empty (menuBar.Menus);
  235. Assert.True (menuBar.WantMousePositionReports);
  236. Assert.False (menuBar.IsMenuOpen);
  237. menuBar = new () { Menus = [] };
  238. Assert.Equal (0, menuBar.X);
  239. Assert.Equal (0, menuBar.Y);
  240. Assert.IsType<DimFill> (menuBar.Width);
  241. Assert.Equal (1, menuBar.Height);
  242. Assert.Empty (menuBar.Menus);
  243. Assert.True (menuBar.WantMousePositionReports);
  244. Assert.False (menuBar.IsMenuOpen);
  245. var menuBarItem = new MenuBarItem ();
  246. Assert.Equal ("", menuBarItem.Title);
  247. Assert.Null (menuBarItem.Parent);
  248. Assert.Empty (menuBarItem.Children);
  249. menuBarItem = new (new MenuBarItem [] { });
  250. Assert.Equal ("", menuBarItem.Title);
  251. Assert.Null (menuBarItem.Parent);
  252. Assert.Empty (menuBarItem.Children);
  253. menuBarItem = new ("Test", new MenuBarItem [] { });
  254. Assert.Equal ("Test", menuBarItem.Title);
  255. Assert.Null (menuBarItem.Parent);
  256. Assert.Empty (menuBarItem.Children);
  257. menuBarItem = new ("Test", new List<MenuItem []> ());
  258. Assert.Equal ("Test", menuBarItem.Title);
  259. Assert.Null (menuBarItem.Parent);
  260. Assert.Empty (menuBarItem.Children);
  261. menuBarItem = new ("Test", "Help", null);
  262. Assert.Equal ("Test", menuBarItem.Title);
  263. Assert.Equal ("Help", menuBarItem.Help);
  264. Assert.Null (menuBarItem.Action);
  265. Assert.Null (menuBarItem.CanExecute);
  266. Assert.Null (menuBarItem.Parent);
  267. Assert.Equal (Key.Empty, menuBarItem.ShortcutKey);
  268. }
  269. [Fact]
  270. [AutoInitShutdown]
  271. public void Disabled_MenuBar_Is_Never_Opened ()
  272. {
  273. Toplevel top = new ();
  274. var menu = new MenuBar
  275. {
  276. Menus =
  277. [
  278. new ("File", new MenuItem [] { new ("New", "", null) })
  279. ]
  280. };
  281. top.Add (menu);
  282. Application.Begin (top);
  283. Assert.True (menu.Enabled);
  284. menu.OpenMenu ();
  285. Assert.True (menu.IsMenuOpen);
  286. menu.Enabled = false;
  287. menu.CloseAllMenus ();
  288. menu.OpenMenu ();
  289. Assert.False (menu.IsMenuOpen);
  290. top.Dispose ();
  291. }
  292. [Fact (Skip = "#3798 Broke. Will fix in #2975")]
  293. [AutoInitShutdown]
  294. public void Disabled_MenuItem_Is_Never_Selected ()
  295. {
  296. var menu = new MenuBar
  297. {
  298. Menus =
  299. [
  300. new (
  301. "Menu",
  302. new MenuItem []
  303. {
  304. new ("Enabled 1", "", null),
  305. new ("Disabled", "", null, () => false),
  306. null,
  307. new ("Enabled 2", "", null)
  308. }
  309. )
  310. ]
  311. };
  312. Toplevel top = new ();
  313. top.Add (menu);
  314. Application.Begin (top);
  315. Attribute [] attributes =
  316. {
  317. // 0
  318. menu.GetAttributeForRole(VisualRole.Normal),
  319. // 1
  320. menu.GetAttributeForRole(VisualRole.Focus),
  321. // 2
  322. menu.GetAttributeForRole(VisualRole.Disabled)
  323. };
  324. DriverAssert.AssertDriverAttributesAre (
  325. @"
  326. 00000000000000",
  327. output,
  328. Application.Driver,
  329. attributes
  330. );
  331. Assert.True (
  332. menu.NewMouseEvent (
  333. new () { Position = new (0, 0), Flags = MouseFlags.Button1Pressed, View = menu }
  334. )
  335. );
  336. top.Draw ();
  337. DriverAssert.AssertDriverAttributesAre (
  338. @"
  339. 11111100000000
  340. 00000000000000
  341. 01111111111110
  342. 02222222222220
  343. 00000000000000
  344. 00000000000000
  345. 00000000000000",
  346. output,
  347. Application.Driver,
  348. attributes
  349. );
  350. Assert.True (
  351. top.SubViews.ElementAt (1)
  352. .NewMouseEvent (
  353. new () { Position = new (0, 2), Flags = MouseFlags.Button1Clicked, View = top.SubViews.ElementAt (1) }
  354. )
  355. );
  356. top.SubViews.ElementAt (1).Layout ();
  357. top.SubViews.ElementAt (1).Draw ();
  358. DriverAssert.AssertDriverAttributesAre (
  359. @"
  360. 11111100000000
  361. 00000000000000
  362. 01111111111110
  363. 02222222222220
  364. 00000000000000
  365. 00000000000000
  366. 00000000000000",
  367. output,
  368. Application.Driver,
  369. attributes
  370. );
  371. Assert.True (
  372. top.SubViews.ElementAt (1)
  373. .NewMouseEvent (
  374. new () { Position = new (0, 2), Flags = MouseFlags.ReportMousePosition, View = top.SubViews.ElementAt (1) }
  375. )
  376. );
  377. top.SubViews.ElementAt (1).Draw ();
  378. DriverAssert.AssertDriverAttributesAre (
  379. @"
  380. 11111100000000
  381. 00000000000000
  382. 01111111111110
  383. 02222222222220
  384. 00000000000000
  385. 00000000000000
  386. 00000000000000",
  387. output,
  388. Application.Driver,
  389. attributes
  390. );
  391. top.Dispose ();
  392. }
  393. [Fact (Skip = "#3798 Broke. Will fix in #2975")]
  394. [AutoInitShutdown]
  395. public void Draw_A_Menu_Over_A_Dialog ()
  396. {
  397. // Override CM
  398. Window.DefaultBorderStyle = LineStyle.Single;
  399. Dialog.DefaultButtonAlignment = Alignment.Center;
  400. Dialog.DefaultBorderStyle = LineStyle.Single;
  401. Dialog.DefaultShadow = ShadowStyle.None;
  402. Button.DefaultShadow = ShadowStyle.None;
  403. Toplevel top = new ();
  404. Window win = new ();
  405. top.Add (win);
  406. RunState rsTop = Application.Begin (top);
  407. AutoInitShutdownAttribute.FakeResize(new Size(40, 15)) ;
  408. Assert.Equal (new (0, 0, 40, 15), win.Frame);
  409. DriverAssert.AssertDriverContentsWithFrameAre (
  410. @"
  411. ┌──────────────────────────────────────┐
  412. │ │
  413. │ │
  414. │ │
  415. │ │
  416. │ │
  417. │ │
  418. │ │
  419. │ │
  420. │ │
  421. │ │
  422. │ │
  423. │ │
  424. │ │
  425. └──────────────────────────────────────┘",
  426. output
  427. );
  428. List<string> items = new ()
  429. {
  430. "New",
  431. "Open",
  432. "Close",
  433. "Save",
  434. "Save As",
  435. "Delete"
  436. };
  437. Dialog dialog = new () { X = 2, Y = 2, Width = 15, Height = 4 };
  438. MenuBar menu = new () { X = Pos.Center (), Width = 10 };
  439. menu.Menus = new MenuBarItem []
  440. {
  441. new (
  442. "File",
  443. new MenuItem []
  444. {
  445. new (
  446. items [0],
  447. "Create a new file",
  448. () => ChangeMenuTitle ("New"),
  449. null,
  450. null,
  451. KeyCode.CtrlMask | KeyCode.N
  452. ),
  453. new (
  454. items [1],
  455. "Open a file",
  456. () => ChangeMenuTitle ("Open"),
  457. null,
  458. null,
  459. KeyCode.CtrlMask | KeyCode.O
  460. ),
  461. new (
  462. items [2],
  463. "Close a file",
  464. () => ChangeMenuTitle ("Close"),
  465. null,
  466. null,
  467. KeyCode.CtrlMask | KeyCode.C
  468. ),
  469. new (
  470. items [3],
  471. "Save a file",
  472. () => ChangeMenuTitle ("Save"),
  473. null,
  474. null,
  475. KeyCode.CtrlMask | KeyCode.S
  476. ),
  477. new (
  478. items [4],
  479. "Save a file as",
  480. () => ChangeMenuTitle ("Save As"),
  481. null,
  482. null,
  483. KeyCode.CtrlMask | KeyCode.A
  484. ),
  485. new (
  486. items [5],
  487. "Delete a file",
  488. () => ChangeMenuTitle ("Delete"),
  489. null,
  490. null,
  491. KeyCode.CtrlMask | KeyCode.A
  492. )
  493. }
  494. )
  495. };
  496. dialog.Add (menu);
  497. void ChangeMenuTitle (string title)
  498. {
  499. menu.Menus [0].Title = title;
  500. menu.SetNeedsDraw ();
  501. }
  502. RunState rsDialog = Application.Begin (dialog);
  503. AutoInitShutdownAttribute.RunIteration ();
  504. Assert.Equal (new (2, 2, 15, 4), dialog.Frame);
  505. DriverAssert.AssertDriverContentsWithFrameAre (
  506. @"
  507. ┌──────────────────────────────────────┐
  508. │ │
  509. │ ┌─────────────┐ │
  510. │ │ File │ │
  511. │ │ │ │
  512. │ └─────────────┘ │
  513. │ │
  514. │ │
  515. │ │
  516. │ │
  517. │ │
  518. │ │
  519. │ │
  520. │ │
  521. └──────────────────────────────────────┘",
  522. output
  523. );
  524. Assert.Equal ("File", menu.Menus [0].Title);
  525. menu.OpenMenu ();
  526. AutoInitShutdownAttribute.RunIteration ();
  527. DriverAssert.AssertDriverContentsWithFrameAre (
  528. @"
  529. ┌──────────────────────────────────────┐
  530. │ │
  531. │ ┌─────────────┐ │
  532. │ │ File │ │
  533. │ │ ┌──────────────────────────────────┐
  534. │ └─│ New Create a new file Ctrl+N │
  535. │ │ Open Open a file Ctrl+O │
  536. │ │ Close Close a file Ctrl+C │
  537. │ │ Save Save a file Ctrl+S │
  538. │ │ Save As Save a file as Ctrl+A │
  539. │ │ Delete Delete a file Ctrl+A │
  540. │ └──────────────────────────────────┘
  541. │ │
  542. │ │
  543. └──────────────────────────────────────┘",
  544. output
  545. );
  546. Application.RaiseMouseEvent (new () { ScreenPosition = new (20, 5), Flags = MouseFlags.Button1Clicked });
  547. // Need to fool MainLoop into thinking it's running
  548. Application.MainLoop.Running = true;
  549. AutoInitShutdownAttribute.RunIteration ();
  550. Assert.Equal (items [0], menu.Menus [0].Title);
  551. DriverAssert.AssertDriverContentsWithFrameAre (
  552. @"
  553. ┌──────────────────────────────────────┐
  554. │ │
  555. │ ┌─────────────┐ │
  556. │ │ New │ │
  557. │ │ │ │
  558. │ └─────────────┘ │
  559. │ │
  560. │ │
  561. │ │
  562. │ │
  563. │ │
  564. │ │
  565. │ │
  566. │ │
  567. └──────────────────────────────────────┘",
  568. output
  569. );
  570. for (var i = 0; i < items.Count; i++)
  571. {
  572. menu.OpenMenu ();
  573. Application.RaiseMouseEvent (new () { ScreenPosition = new (20, 5 + i), Flags = MouseFlags.Button1Clicked });
  574. AutoInitShutdownAttribute.RunIteration ();
  575. Assert.Equal (items [i], menu.Menus [0].Title);
  576. }
  577. AutoInitShutdownAttribute.FakeResize(new Size(20, 15));
  578. menu.OpenMenu ();
  579. AutoInitShutdownAttribute.RunIteration ();
  580. DriverAssert.AssertDriverContentsWithFrameAre (
  581. @"
  582. ┌──────────────────┐
  583. │ │
  584. │ ┌─────────────┐ │
  585. │ │ Delete │ │
  586. │ │ ┌───────────────
  587. │ └─│ New Create
  588. │ │ Open O
  589. │ │ Close Cl
  590. │ │ Save S
  591. │ │ Save As Save
  592. │ │ Delete Del
  593. │ └───────────────
  594. │ │
  595. │ │
  596. └──────────────────┘",
  597. output
  598. );
  599. Application.End (rsDialog);
  600. Application.End (rsTop);
  601. top.Dispose ();
  602. }
  603. [Fact (Skip = "#3798 Broke. Will fix in #2975")]
  604. [AutoInitShutdown]
  605. public void Draw_A_Menu_Over_A_Top_Dialog ()
  606. {
  607. ((FakeDriver)Application.Driver).SetBufferSize (40, 15);
  608. // Override CM
  609. Window.DefaultBorderStyle = LineStyle.Single;
  610. Dialog.DefaultButtonAlignment = Alignment.Center;
  611. Dialog.DefaultBorderStyle = LineStyle.Single;
  612. Dialog.DefaultShadow = ShadowStyle.None;
  613. Button.DefaultShadow = ShadowStyle.None;
  614. Assert.Equal (new (0, 0, 40, 15), View.GetClip ()!.GetBounds ());
  615. DriverAssert.AssertDriverContentsWithFrameAre (@"", output);
  616. List<string> items = new ()
  617. {
  618. "New",
  619. "Open",
  620. "Close",
  621. "Save",
  622. "Save As",
  623. "Delete"
  624. };
  625. var dialog = new Dialog { X = 2, Y = 2, Width = 15, Height = 4 };
  626. var menu = new MenuBar { X = Pos.Center (), Width = 10 };
  627. menu.Menus = new MenuBarItem []
  628. {
  629. new (
  630. "File",
  631. new MenuItem []
  632. {
  633. new (
  634. items [0],
  635. "Create a new file",
  636. () => ChangeMenuTitle ("New"),
  637. null,
  638. null,
  639. KeyCode.CtrlMask | KeyCode.N
  640. ),
  641. new (
  642. items [1],
  643. "Open a file",
  644. () => ChangeMenuTitle ("Open"),
  645. null,
  646. null,
  647. KeyCode.CtrlMask | KeyCode.O
  648. ),
  649. new (
  650. items [2],
  651. "Close a file",
  652. () => ChangeMenuTitle ("Close"),
  653. null,
  654. null,
  655. KeyCode.CtrlMask | KeyCode.C
  656. ),
  657. new (
  658. items [3],
  659. "Save a file",
  660. () => ChangeMenuTitle ("Save"),
  661. null,
  662. null,
  663. KeyCode.CtrlMask | KeyCode.S
  664. ),
  665. new (
  666. items [4],
  667. "Save a file as",
  668. () => ChangeMenuTitle ("Save As"),
  669. null,
  670. null,
  671. KeyCode.CtrlMask | KeyCode.A
  672. ),
  673. new (
  674. items [5],
  675. "Delete a file",
  676. () => ChangeMenuTitle ("Delete"),
  677. null,
  678. null,
  679. KeyCode.CtrlMask | KeyCode.A
  680. )
  681. }
  682. )
  683. };
  684. dialog.Add (menu);
  685. void ChangeMenuTitle (string title)
  686. {
  687. menu.Menus [0].Title = title;
  688. menu.SetNeedsDraw ();
  689. }
  690. RunState rs = Application.Begin (dialog);
  691. AutoInitShutdownAttribute.RunIteration ();
  692. Assert.Equal (new (2, 2, 15, 4), dialog.Frame);
  693. DriverAssert.AssertDriverContentsWithFrameAre (
  694. @"
  695. ┌─────────────┐
  696. │ File │
  697. │ │
  698. └─────────────┘",
  699. output
  700. );
  701. Assert.Equal ("File", menu.Menus [0].Title);
  702. menu.OpenMenu ();
  703. AutoInitShutdownAttribute.RunIteration ();
  704. DriverAssert.AssertDriverContentsWithFrameAre (
  705. @"
  706. ┌─────────────┐
  707. │ File │
  708. │ ┌──────────────────────────────────┐
  709. └─│ New Create a new file Ctrl+N │
  710. │ Open Open a file Ctrl+O │
  711. │ Close Close a file Ctrl+C │
  712. │ Save Save a file Ctrl+S │
  713. │ Save As Save a file as Ctrl+A │
  714. │ Delete Delete a file Ctrl+A │
  715. └──────────────────────────────────┘",
  716. output
  717. );
  718. Application.RaiseMouseEvent (new () { ScreenPosition = new (20, 5), Flags = MouseFlags.Button1Clicked });
  719. // Need to fool MainLoop into thinking it's running
  720. Application.MainLoop.Running = true;
  721. AutoInitShutdownAttribute.RunIteration ();
  722. Assert.Equal (items [0], menu.Menus [0].Title);
  723. DriverAssert.AssertDriverContentsWithFrameAre (
  724. @"
  725. ┌─────────────┐
  726. │ New │
  727. │ │
  728. └─────────────┘",
  729. output
  730. );
  731. for (var i = 1; i < items.Count; i++)
  732. {
  733. menu.OpenMenu ();
  734. Application.RaiseMouseEvent (new () { ScreenPosition = new (20, 5 + i), Flags = MouseFlags.Button1Clicked });
  735. AutoInitShutdownAttribute.RunIteration ();
  736. Assert.Equal (items [i], menu.Menus [0].Title);
  737. }
  738. AutoInitShutdownAttribute.FakeResize(new Size(20, 15));
  739. menu.OpenMenu ();
  740. AutoInitShutdownAttribute.RunIteration ();
  741. DriverAssert.AssertDriverContentsWithFrameAre (
  742. @"
  743. ┌─────────────┐
  744. │ Delete │
  745. │ ┌───────────────
  746. └─│ New Create
  747. │ Open O
  748. │ Close Cl
  749. │ Save S
  750. │ Save As Save
  751. │ Delete Del
  752. └───────────────",
  753. output
  754. );
  755. Application.End (rs);
  756. dialog.Dispose ();
  757. }
  758. [Fact]
  759. [AutoInitShutdown]
  760. public void DrawFrame_With_Negative_Positions ()
  761. {
  762. var menu = new MenuBar
  763. {
  764. X = -1,
  765. Y = -1,
  766. Menus =
  767. [
  768. new (new MenuItem [] { new ("One", "", null), new ("Two", "", null) })
  769. ]
  770. };
  771. menu.Layout ();
  772. Assert.Equal (new (-1, -1), new Point (menu.Frame.X, menu.Frame.Y));
  773. Toplevel top = new ();
  774. Application.Begin (top);
  775. menu.OpenMenu ();
  776. AutoInitShutdownAttribute.RunIteration ();
  777. var expected = @"
  778. ──────┐
  779. One │
  780. Two │
  781. ──────┘
  782. ";
  783. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  784. Assert.Equal (new (0, 0, 7, 4), pos);
  785. menu.CloseAllMenus ();
  786. menu.Frame = new (-1, -2, menu.Frame.Width, menu.Frame.Height);
  787. menu.OpenMenu ();
  788. AutoInitShutdownAttribute.RunIteration ();
  789. expected = @"
  790. One │
  791. Two │
  792. ──────┘
  793. ";
  794. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  795. Assert.Equal (new (1, 0, 7, 3), pos);
  796. menu.CloseAllMenus ();
  797. menu.Frame = new (0, 0, menu.Frame.Width, menu.Frame.Height);
  798. AutoInitShutdownAttribute.FakeResize(new Size(7, 5));
  799. menu.OpenMenu ();
  800. AutoInitShutdownAttribute.RunIteration ();
  801. expected = @"
  802. ┌──────
  803. │ One
  804. │ Two
  805. └──────
  806. ";
  807. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  808. Assert.Equal (new (0, 1, 7, 4), pos);
  809. menu.CloseAllMenus ();
  810. menu.Frame = new (0, 0, menu.Frame.Width, menu.Frame.Height);
  811. AutoInitShutdownAttribute.FakeResize(new Size(7, 3));
  812. menu.OpenMenu ();
  813. AutoInitShutdownAttribute.RunIteration ();
  814. expected = @"
  815. ┌──────
  816. │ One
  817. │ Two
  818. ";
  819. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  820. Assert.Equal (new (0, 0, 7, 3), pos);
  821. top.Dispose ();
  822. }
  823. [Fact]
  824. [AutoInitShutdown]
  825. public void DrawFrame_With_Negative_Positions_Disabled_Border ()
  826. {
  827. var menu = new MenuBar
  828. {
  829. X = -2,
  830. Y = -1,
  831. MenusBorderStyle = LineStyle.None,
  832. Menus =
  833. [
  834. new (new MenuItem [] { new ("One", "", null), new ("Two", "", null) })
  835. ]
  836. };
  837. menu.Layout ();
  838. Assert.Equal (new (-2, -1), new Point (menu.Frame.X, menu.Frame.Y));
  839. Toplevel top = new ();
  840. Application.Begin (top);
  841. menu.OpenMenu ();
  842. AutoInitShutdownAttribute.RunIteration ();
  843. var expected = @"
  844. ne
  845. wo
  846. ";
  847. _ = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  848. menu.CloseAllMenus ();
  849. menu.Frame = new (-2, -2, menu.Frame.Width, menu.Frame.Height);
  850. menu.OpenMenu ();
  851. AutoInitShutdownAttribute.RunIteration ();
  852. expected = @"
  853. wo
  854. ";
  855. _ = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  856. menu.CloseAllMenus ();
  857. menu.Frame = new (0, 0, menu.Frame.Width, menu.Frame.Height);
  858. AutoInitShutdownAttribute.FakeResize(new Size(3, 2));
  859. menu.OpenMenu ();
  860. AutoInitShutdownAttribute.RunIteration ();
  861. expected = @"
  862. On
  863. Tw
  864. ";
  865. _ = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  866. menu.CloseAllMenus ();
  867. menu.Frame = new (0, 0, menu.Frame.Width, menu.Frame.Height);
  868. AutoInitShutdownAttribute.FakeResize(new Size(3, 1));
  869. menu.OpenMenu ();
  870. AutoInitShutdownAttribute.RunIteration ();
  871. expected = @"
  872. On
  873. ";
  874. _ = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  875. top.Dispose ();
  876. }
  877. [Fact]
  878. [AutoInitShutdown]
  879. public void DrawFrame_With_Positive_Positions ()
  880. {
  881. var menu = new MenuBar
  882. {
  883. Menus =
  884. [
  885. new (new MenuItem [] { new ("One", "", null), new ("Two", "", null) })
  886. ]
  887. };
  888. Assert.Equal (Point.Empty, new (menu.Frame.X, menu.Frame.Y));
  889. Toplevel top = new ();
  890. Application.Begin (top);
  891. menu.OpenMenu ();
  892. AutoInitShutdownAttribute.RunIteration ();
  893. var expected = @"
  894. ┌──────┐
  895. │ One │
  896. │ Two │
  897. └──────┘
  898. ";
  899. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  900. Assert.Equal (new (0, 1, 8, 4), pos);
  901. top.Dispose ();
  902. }
  903. [Fact]
  904. [AutoInitShutdown]
  905. public void DrawFrame_With_Positive_Positions_Disabled_Border ()
  906. {
  907. var menu = new MenuBar
  908. {
  909. MenusBorderStyle = LineStyle.None,
  910. Menus =
  911. [
  912. new (new MenuItem [] { new ("One", "", null), new ("Two", "", null) })
  913. ]
  914. };
  915. Assert.Equal (Point.Empty, new (menu.Frame.X, menu.Frame.Y));
  916. Toplevel top = new ();
  917. Application.Begin (top);
  918. menu.OpenMenu ();
  919. AutoInitShutdownAttribute.RunIteration ();
  920. var expected = @"
  921. One
  922. Two
  923. ";
  924. _ = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  925. top.Dispose ();
  926. }
  927. [Fact]
  928. public void Exceptions ()
  929. {
  930. Assert.Throws<ArgumentNullException> (() => new MenuBarItem ("Test", (MenuItem [])null));
  931. Assert.Throws<ArgumentNullException> (() => new MenuBarItem ("Test", (List<MenuItem []>)null));
  932. }
  933. [Fact]
  934. [AutoInitShutdown]
  935. public void HotKey_MenuBar_OnKeyDown_OnKeyUp_ProcessKeyPressed ()
  936. {
  937. var newAction = false;
  938. var copyAction = false;
  939. var menu = new MenuBar
  940. {
  941. Menus =
  942. [
  943. new ("_File", new MenuItem [] { new ("_New", "", () => newAction = true) }),
  944. new (
  945. "_Edit",
  946. new MenuItem [] { new ("_Copy", "", () => copyAction = true) }
  947. )
  948. ]
  949. };
  950. var top = new Toplevel ();
  951. top.Add (menu);
  952. Application.Begin (top);
  953. Assert.False (newAction);
  954. Assert.False (copyAction);
  955. #if SUPPORT_ALT_TO_ACTIVATE_MENU
  956. Assert.False (Application.Top.ProcessKeyDown (new KeyEventArgs (Key.AltMask)));
  957. Assert.False (Application.Top.ProcessKeyDown (new KeyEventArgs (Key.AltMask)));
  958. Assert.True (Application.Top.ProcessKeyUp (new KeyEventArgs (Key.AltMask)));
  959. Assert.True (menu.IsMenuOpen);
  960. Application.Top.Draw ();
  961. string expected = @"
  962. File Edit
  963. ";
  964. var pos = DriverAsserts.AssertDriverContentsWithFrameAre (expected, output);
  965. Assert.Equal (new (1, 0, 11, 1), pos);
  966. Assert.True (Application.Top.ProcessKeyDown (new KeyEventArgs (Key.N)));
  967. AutoInitShutdownAttribute.RunIteration ();
  968. Assert.False (newAction); // not yet, hot keys don't work if the item is not visible
  969. Assert.True (Application.Top.ProcessKeyDown (new KeyEventArgs (Key.F)));
  970. AutoInitShutdownAttribute.RunIteration ();
  971. Assert.True (Application.Top.ProcessKeyDown (new KeyEventArgs (Key.N)));
  972. AutoInitShutdownAttribute.RunIteration ();
  973. Assert.True (newAction);
  974. Application.Top.Draw ();
  975. expected = @"
  976. File Edit
  977. ";
  978. Assert.False (Application.Top.ProcessKeyDown (new KeyEventArgs (Key.AltMask)));
  979. Assert.True (Application.Top.ProcessKeyUp (new KeyEventArgs (Key.AltMask)));
  980. Assert.True (Application.Top.ProcessKeyUp (new KeyEventArgs (Key.AltMask)));
  981. Assert.True (menu.IsMenuOpen);
  982. Application.Top.Draw ();
  983. expected = @"
  984. File Edit
  985. ";
  986. pos = DriverAsserts.AssertDriverContentsWithFrameAre (expected, output);
  987. Assert.Equal (new (1, 0, 11, 1), pos);
  988. Assert.True (Application.Top.ProcessKeyDown (new KeyEventArgs (Key.CursorRight)));
  989. Assert.True (Application.Top.ProcessKeyDown (new KeyEventArgs (Key.C)));
  990. AutoInitShutdownAttribute.RunIteration ();
  991. Assert.True (copyAction);
  992. #endif
  993. top.Dispose ();
  994. }
  995. [Fact]
  996. [AutoInitShutdown]
  997. public void HotKey_MenuBar_ProcessKeyPressed_Menu_ProcessKey ()
  998. {
  999. var newAction = false;
  1000. var copyAction = false;
  1001. // Define the expected menu
  1002. var expectedMenu = new ExpectedMenuBar
  1003. {
  1004. Menus =
  1005. [
  1006. new ("File", new MenuItem [] { new ("New", "", null) }),
  1007. new (
  1008. "Edit",
  1009. new MenuItem [] { new ("Copy", "", null) }
  1010. )
  1011. ]
  1012. };
  1013. // The real menu
  1014. var menu = new MenuBar
  1015. {
  1016. Menus =
  1017. [
  1018. new (
  1019. "_" + expectedMenu.Menus [0].Title,
  1020. new MenuItem []
  1021. {
  1022. new (
  1023. "_" + expectedMenu.Menus [0].Children [0].Title,
  1024. "",
  1025. () => newAction = true
  1026. )
  1027. }
  1028. ),
  1029. new (
  1030. "_" + expectedMenu.Menus [1].Title,
  1031. new MenuItem []
  1032. {
  1033. new (
  1034. "_"
  1035. + expectedMenu.Menus [1]
  1036. .Children [0]
  1037. .Title,
  1038. "",
  1039. () => copyAction = true
  1040. )
  1041. }
  1042. )
  1043. ]
  1044. };
  1045. var top = new Toplevel ();
  1046. top.Add (menu);
  1047. Application.Begin (top);
  1048. Assert.False (newAction);
  1049. Assert.False (copyAction);
  1050. Assert.True (menu.NewKeyDownEvent (Key.F.WithAlt));
  1051. Assert.True (menu.IsMenuOpen);
  1052. Application.Top.Draw ();
  1053. DriverAssert.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (0), output);
  1054. Assert.True (Application.Top.SubViews.ElementAt (1).NewKeyDownEvent (Key.N));
  1055. AutoInitShutdownAttribute.RunIteration ();
  1056. Assert.True (newAction);
  1057. Assert.True (menu.NewKeyDownEvent (Key.E.WithAlt));
  1058. Assert.True (menu.IsMenuOpen);
  1059. Application.Top.Draw ();
  1060. DriverAssert.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (1), output);
  1061. Assert.True (Application.Top.SubViews.ElementAt (1).NewKeyDownEvent (Key.C));
  1062. AutoInitShutdownAttribute.RunIteration ();
  1063. Assert.True (copyAction);
  1064. top.Dispose ();
  1065. }
  1066. [Fact]
  1067. [AutoInitShutdown]
  1068. public void Key_Open_And_Close_The_MenuBar ()
  1069. {
  1070. var menu = new MenuBar
  1071. {
  1072. Menus =
  1073. [
  1074. new ("File", new MenuItem [] { new ("New", "", null) })
  1075. ]
  1076. };
  1077. var top = new Toplevel ();
  1078. top.Add (menu);
  1079. Application.Begin (top);
  1080. Assert.True (top.NewKeyDownEvent (menu.Key));
  1081. Assert.True (menu.IsMenuOpen);
  1082. Assert.True (top.NewKeyDownEvent (menu.Key));
  1083. Assert.False (menu.IsMenuOpen);
  1084. menu.Key = Key.F10.WithShift;
  1085. Assert.False (top.NewKeyDownEvent (Key.F9));
  1086. Assert.False (menu.IsMenuOpen);
  1087. Assert.True (top.NewKeyDownEvent (Key.F10.WithShift));
  1088. Assert.True (menu.IsMenuOpen);
  1089. Assert.True (top.NewKeyDownEvent (Key.F10.WithShift));
  1090. Assert.False (menu.IsMenuOpen);
  1091. top.Dispose ();
  1092. }
  1093. [Theory]
  1094. [AutoInitShutdown]
  1095. [InlineData ("_File", "_New", "", KeyCode.Space | KeyCode.CtrlMask)]
  1096. [InlineData ("Closed", "None", "", KeyCode.Space | KeyCode.CtrlMask, KeyCode.Space | KeyCode.CtrlMask)]
  1097. [InlineData ("_File", "_New", "", KeyCode.F9)]
  1098. [InlineData ("Closed", "None", "", KeyCode.F9, KeyCode.F9)]
  1099. [InlineData ("_File", "_Open", "", KeyCode.F9, KeyCode.CursorDown)]
  1100. [InlineData ("_File", "_Save", "", KeyCode.F9, KeyCode.CursorDown, KeyCode.CursorDown)]
  1101. [InlineData ("_File", "_Quit", "", KeyCode.F9, KeyCode.CursorDown, KeyCode.CursorDown, KeyCode.CursorDown)]
  1102. [InlineData (
  1103. "_File",
  1104. "_New",
  1105. "",
  1106. KeyCode.F9,
  1107. KeyCode.CursorDown,
  1108. KeyCode.CursorDown,
  1109. KeyCode.CursorDown,
  1110. KeyCode.CursorDown
  1111. )]
  1112. [InlineData ("_File", "_New", "", KeyCode.F9, KeyCode.CursorDown, KeyCode.CursorUp)]
  1113. [InlineData ("_File", "_Quit", "", KeyCode.F9, KeyCode.CursorUp)]
  1114. [InlineData ("_File", "_New", "", KeyCode.F9, KeyCode.CursorUp, KeyCode.CursorDown)]
  1115. [InlineData ("Closed", "None", "Open", KeyCode.F9, KeyCode.CursorDown, KeyCode.Enter)]
  1116. [InlineData ("_Edit", "_Copy", "", KeyCode.F9, KeyCode.CursorRight)]
  1117. [InlineData ("_About", "_About", "", KeyCode.F9, KeyCode.CursorLeft)]
  1118. [InlineData ("_Edit", "_Copy", "", KeyCode.F9, KeyCode.CursorLeft, KeyCode.CursorLeft)]
  1119. [InlineData ("_Edit", "_Select All", "", KeyCode.F9, KeyCode.CursorRight, KeyCode.CursorUp)]
  1120. [InlineData ("_File", "_New", "", KeyCode.F9, KeyCode.CursorRight, KeyCode.CursorDown, KeyCode.CursorLeft)]
  1121. [InlineData ("_About", "_About", "", KeyCode.F9, KeyCode.CursorRight, KeyCode.CursorRight)]
  1122. [InlineData ("Closed", "None", "New", KeyCode.F9, KeyCode.Enter)]
  1123. [InlineData ("Closed", "None", "Quit", KeyCode.F9, KeyCode.CursorUp, KeyCode.Enter)]
  1124. [InlineData ("Closed", "None", "Copy", KeyCode.F9, KeyCode.CursorRight, KeyCode.Enter)]
  1125. [InlineData (
  1126. "Closed",
  1127. "None",
  1128. "Find",
  1129. KeyCode.F9,
  1130. KeyCode.CursorRight,
  1131. KeyCode.CursorUp,
  1132. KeyCode.CursorUp,
  1133. KeyCode.Enter
  1134. )]
  1135. [InlineData (
  1136. "Closed",
  1137. "None",
  1138. "Replace",
  1139. KeyCode.F9,
  1140. KeyCode.CursorRight,
  1141. KeyCode.CursorUp,
  1142. KeyCode.CursorUp,
  1143. KeyCode.CursorDown,
  1144. KeyCode.Enter
  1145. )]
  1146. [InlineData (
  1147. "_Edit",
  1148. "F_ind",
  1149. "",
  1150. KeyCode.F9,
  1151. KeyCode.CursorRight,
  1152. KeyCode.CursorUp,
  1153. KeyCode.CursorUp,
  1154. KeyCode.CursorLeft,
  1155. KeyCode.Enter
  1156. )]
  1157. [InlineData ("Closed", "None", "About", KeyCode.F9, KeyCode.CursorRight, KeyCode.CursorRight, KeyCode.Enter)]
  1158. //// Hotkeys
  1159. [InlineData ("_File", "_New", "", KeyCode.AltMask | KeyCode.F)]
  1160. [InlineData ("Closed", "None", "", KeyCode.AltMask | KeyCode.ShiftMask | KeyCode.F)]
  1161. [InlineData ("Closed", "None", "", KeyCode.AltMask | KeyCode.F, KeyCode.Esc)]
  1162. [InlineData ("Closed", "None", "", KeyCode.AltMask | KeyCode.F, KeyCode.AltMask | KeyCode.F)]
  1163. [InlineData ("Closed", "None", "Open", KeyCode.AltMask | KeyCode.F, KeyCode.O)]
  1164. [InlineData ("_File", "_New", "", KeyCode.AltMask | KeyCode.F, KeyCode.ShiftMask | KeyCode.O)]
  1165. [InlineData ("Closed", "None", "Open", KeyCode.AltMask | KeyCode.F, KeyCode.AltMask | KeyCode.O)]
  1166. [InlineData ("_Edit", "_Copy", "", KeyCode.AltMask | KeyCode.E)]
  1167. [InlineData ("_Edit", "F_ind", "", KeyCode.AltMask | KeyCode.E, KeyCode.F)]
  1168. [InlineData ("_Edit", "F_ind", "", KeyCode.AltMask | KeyCode.E, KeyCode.AltMask | KeyCode.F)]
  1169. [InlineData ("Closed", "None", "Replace", KeyCode.AltMask | KeyCode.E, KeyCode.F, KeyCode.R)]
  1170. [InlineData ("Closed", "None", "Copy", KeyCode.AltMask | KeyCode.E, KeyCode.F, KeyCode.C)]
  1171. [InlineData ("_Edit", "_1st", "", KeyCode.AltMask | KeyCode.E, KeyCode.F, KeyCode.D3)]
  1172. [InlineData ("Closed", "None", "1", KeyCode.AltMask | KeyCode.E, KeyCode.F, KeyCode.D3, KeyCode.D1)]
  1173. [InlineData ("Closed", "None", "1", KeyCode.AltMask | KeyCode.E, KeyCode.F, KeyCode.D3, KeyCode.Enter)]
  1174. [InlineData ("Closed", "None", "2", KeyCode.AltMask | KeyCode.E, KeyCode.F, KeyCode.D3, KeyCode.D2)]
  1175. [InlineData ("_Edit", "_5th", "", KeyCode.AltMask | KeyCode.E, KeyCode.F, KeyCode.D3, KeyCode.D4)]
  1176. [InlineData ("Closed", "None", "5", KeyCode.AltMask | KeyCode.E, KeyCode.F, KeyCode.D4, KeyCode.D5)]
  1177. [InlineData ("Closed", "None", "About", KeyCode.AltMask | KeyCode.A)]
  1178. public void KeyBindings_Navigation_Commands (
  1179. string expectedBarTitle,
  1180. string expectedItemTitle,
  1181. string expectedAction,
  1182. params KeyCode [] keys
  1183. )
  1184. {
  1185. var miAction = "";
  1186. MenuItem mbiCurrent = null;
  1187. MenuItem miCurrent = null;
  1188. var menu = new MenuBar ();
  1189. Func<object, bool> fn = s =>
  1190. {
  1191. miAction = s as string;
  1192. return true;
  1193. };
  1194. menu.EnableForDesign (ref fn);
  1195. menu.Key = KeyCode.F9;
  1196. menu.MenuOpening += (s, e) => mbiCurrent = e.CurrentMenu;
  1197. menu.MenuOpened += (s, e) => { miCurrent = e.MenuItem; };
  1198. menu.MenuClosing += (s, e) =>
  1199. {
  1200. mbiCurrent = null;
  1201. miCurrent = null;
  1202. };
  1203. menu.UseKeysUpDownAsKeysLeftRight = true;
  1204. var top = new Toplevel ();
  1205. top.Add (menu);
  1206. Application.Begin (top);
  1207. foreach (Key key in keys)
  1208. {
  1209. top.NewKeyDownEvent (key);
  1210. AutoInitShutdownAttribute.RunIteration ();
  1211. }
  1212. Assert.Equal (expectedBarTitle, mbiCurrent != null ? mbiCurrent.Title : "Closed");
  1213. Assert.Equal (expectedItemTitle, miCurrent != null ? miCurrent.Title : "None");
  1214. Assert.Equal (expectedAction, miAction);
  1215. top.Dispose ();
  1216. }
  1217. [Theory]
  1218. [AutoInitShutdown]
  1219. [InlineData ("New", KeyCode.CtrlMask | KeyCode.N)]
  1220. [InlineData ("Quit", KeyCode.CtrlMask | KeyCode.Q)]
  1221. [InlineData ("Copy", KeyCode.CtrlMask | KeyCode.C)]
  1222. [InlineData ("Replace", KeyCode.CtrlMask | KeyCode.H)]
  1223. [InlineData ("1", KeyCode.F1)]
  1224. [InlineData ("5", KeyCode.CtrlMask | KeyCode.D5)]
  1225. public void KeyBindings_Shortcut_Commands (string expectedAction, params KeyCode [] keys)
  1226. {
  1227. var miAction = "";
  1228. MenuItem mbiCurrent = null;
  1229. MenuItem miCurrent = null;
  1230. var menu = new MenuBar ();
  1231. bool FnAction (string s)
  1232. {
  1233. miAction = s;
  1234. return true;
  1235. }
  1236. // Declare a variable for the function
  1237. Func<string, bool> fnActionVariable = FnAction;
  1238. menu.EnableForDesign (ref fnActionVariable);
  1239. menu.Key = KeyCode.F9;
  1240. menu.MenuOpening += (s, e) => mbiCurrent = e.CurrentMenu;
  1241. menu.MenuOpened += (s, e) => { miCurrent = e.MenuItem; };
  1242. menu.MenuClosing += (s, e) =>
  1243. {
  1244. mbiCurrent = null;
  1245. miCurrent = null;
  1246. };
  1247. menu.UseKeysUpDownAsKeysLeftRight = true;
  1248. var top = new Toplevel ();
  1249. top.Add (menu);
  1250. Application.Begin (top);
  1251. foreach (KeyCode key in keys)
  1252. {
  1253. Assert.True (top.NewKeyDownEvent (new (key)));
  1254. AutoInitShutdownAttribute.RunIteration ();
  1255. }
  1256. Assert.Equal (expectedAction, miAction);
  1257. top.Dispose ();
  1258. }
  1259. [Fact]
  1260. [AutoInitShutdown]
  1261. public void Menu_With_Separator ()
  1262. {
  1263. var menu = new MenuBar
  1264. {
  1265. Menus =
  1266. [
  1267. new (
  1268. "File",
  1269. new MenuItem []
  1270. {
  1271. new (
  1272. "_Open",
  1273. "Open a file",
  1274. () => { },
  1275. null,
  1276. null,
  1277. KeyCode.CtrlMask | KeyCode.O
  1278. ),
  1279. null,
  1280. new ("_Quit", "", null)
  1281. }
  1282. )
  1283. ]
  1284. };
  1285. var top = new Toplevel ();
  1286. top.Add (menu);
  1287. Application.Begin (top);
  1288. menu.OpenMenu ();
  1289. AutoInitShutdownAttribute.RunIteration ();
  1290. DriverAssert.AssertDriverContentsWithFrameAre (
  1291. @"
  1292. File
  1293. ┌────────────────────────────┐
  1294. │ Open Open a file Ctrl+O │
  1295. ├────────────────────────────┤
  1296. │ Quit │
  1297. └────────────────────────────┘",
  1298. output
  1299. );
  1300. top.Dispose ();
  1301. }
  1302. [Fact]
  1303. [AutoInitShutdown]
  1304. public void Menu_With_Separator_Disabled_Border ()
  1305. {
  1306. var menu = new MenuBar
  1307. {
  1308. MenusBorderStyle = LineStyle.None,
  1309. Menus =
  1310. [
  1311. new (
  1312. "File",
  1313. new MenuItem []
  1314. {
  1315. new (
  1316. "_Open",
  1317. "Open a file",
  1318. () => { },
  1319. null,
  1320. null,
  1321. KeyCode.CtrlMask | KeyCode.O
  1322. ),
  1323. null,
  1324. new ("_Quit", "", null)
  1325. }
  1326. )
  1327. ]
  1328. };
  1329. var top = new Toplevel ();
  1330. top.Add (menu);
  1331. Application.Begin (top);
  1332. menu.OpenMenu ();
  1333. AutoInitShutdownAttribute.RunIteration ();
  1334. DriverAssert.AssertDriverContentsWithFrameAre (
  1335. @"
  1336. File
  1337. Open Open a file Ctrl+O
  1338. ────────────────────────────
  1339. Quit ",
  1340. output
  1341. );
  1342. top.Dispose ();
  1343. }
  1344. [Fact]
  1345. [AutoInitShutdown]
  1346. public void MenuBar_ButtonPressed_Open_The_Menu_ButtonPressed_Again_Close_The_Menu ()
  1347. {
  1348. // Define the expected menu
  1349. var expectedMenu = new ExpectedMenuBar
  1350. {
  1351. Menus =
  1352. [
  1353. new ("File", new MenuItem [] { new ("Open", "", null) }),
  1354. new (
  1355. "Edit",
  1356. new MenuItem [] { new ("Copy", "", null) }
  1357. )
  1358. ]
  1359. };
  1360. // Test without HotKeys first
  1361. var menu = new MenuBar
  1362. {
  1363. Menus =
  1364. [
  1365. new (
  1366. "_" + expectedMenu.Menus [0].Title,
  1367. new MenuItem [] { new ("_" + expectedMenu.Menus [0].Children [0].Title, "", null) }
  1368. ),
  1369. new (
  1370. "_" + expectedMenu.Menus [1].Title,
  1371. new MenuItem []
  1372. {
  1373. new (
  1374. "_"
  1375. + expectedMenu.Menus [1]
  1376. .Children [0]
  1377. .Title,
  1378. "",
  1379. null
  1380. )
  1381. }
  1382. )
  1383. ]
  1384. };
  1385. var top = new Toplevel ();
  1386. top.Add (menu);
  1387. Application.Begin (top);
  1388. Assert.True (menu.NewMouseEvent (new () { Position = new (1, 0), Flags = MouseFlags.Button1Pressed, View = menu }));
  1389. Assert.True (menu.IsMenuOpen);
  1390. top.Draw ();
  1391. DriverAssert.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (0), output);
  1392. Assert.True (menu.NewMouseEvent (new () { Position = new (1, 0), Flags = MouseFlags.Button1Pressed, View = menu }));
  1393. Assert.False (menu.IsMenuOpen);
  1394. View.SetClipToScreen ();
  1395. top.Draw ();
  1396. DriverAssert.AssertDriverContentsAre (expectedMenu.ClosedMenuText, output);
  1397. top.Dispose ();
  1398. }
  1399. [Fact]
  1400. [AutoInitShutdown]
  1401. public void MenuBar_In_Window_Without_Other_Views_With_Top_Init ()
  1402. {
  1403. var win = new Window ();
  1404. var menu = new MenuBar
  1405. {
  1406. Menus =
  1407. [
  1408. new ("File", new MenuItem [] { new ("New", "", null) }),
  1409. new (
  1410. "Edit",
  1411. new MenuItem []
  1412. {
  1413. new MenuBarItem (
  1414. "Delete",
  1415. new MenuItem []
  1416. { new ("All", "", null), new ("Selected", "", null) }
  1417. )
  1418. }
  1419. )
  1420. ]
  1421. };
  1422. win.Add (menu);
  1423. Toplevel top = new ();
  1424. top.Add (win);
  1425. Application.Begin (top);
  1426. AutoInitShutdownAttribute.FakeResize(new Size(40, 8));
  1427. DriverAssert.AssertDriverContentsWithFrameAre (
  1428. @"
  1429. ┌──────────────────────────────────────┐
  1430. │ File Edit │
  1431. │ │
  1432. │ │
  1433. │ │
  1434. │ │
  1435. │ │
  1436. └──────────────────────────────────────┘",
  1437. output
  1438. );
  1439. Assert.True (win.NewKeyDownEvent (menu.Key));
  1440. top.Draw ();
  1441. DriverAssert.AssertDriverContentsWithFrameAre (
  1442. @"
  1443. ┌──────────────────────────────────────┐
  1444. │ File Edit │
  1445. │┌──────┐ │
  1446. ││ New │ │
  1447. │└──────┘ │
  1448. │ │
  1449. │ │
  1450. └──────────────────────────────────────┘",
  1451. output
  1452. );
  1453. Assert.True (menu.NewKeyDownEvent (Key.CursorRight));
  1454. AutoInitShutdownAttribute.RunIteration ();
  1455. DriverAssert.AssertDriverContentsWithFrameAre (
  1456. @"
  1457. ┌──────────────────────────────────────┐
  1458. │ File Edit │
  1459. │ ┌─────────┐ │
  1460. │ │ Delete ►│ │
  1461. │ └─────────┘ │
  1462. │ │
  1463. │ │
  1464. └──────────────────────────────────────┘",
  1465. output
  1466. );
  1467. Assert.True (menu._openMenu.NewKeyDownEvent (Key.CursorRight));
  1468. top.Draw ();
  1469. DriverAssert.AssertDriverContentsWithFrameAre (
  1470. @"
  1471. ┌──────────────────────────────────────┐
  1472. │ File Edit │
  1473. │ ┌─────────┐ │
  1474. │ │ Delete ►│┌───────────┐ │
  1475. │ └─────────┘│ All │ │
  1476. │ │ Selected │ │
  1477. │ └───────────┘ │
  1478. └──────────────────────────────────────┘",
  1479. output
  1480. );
  1481. Assert.True (menu._openMenu.NewKeyDownEvent (Key.CursorRight));
  1482. View.SetClipToScreen ();
  1483. top.Draw ();
  1484. DriverAssert.AssertDriverContentsWithFrameAre (
  1485. @"
  1486. ┌──────────────────────────────────────┐
  1487. │ File Edit │
  1488. │┌──────┐ │
  1489. ││ New │ │
  1490. │└──────┘ │
  1491. │ │
  1492. │ │
  1493. └──────────────────────────────────────┘",
  1494. output
  1495. );
  1496. top.Dispose ();
  1497. }
  1498. [Fact]
  1499. [AutoInitShutdown]
  1500. public void MenuBar_In_Window_Without_Other_Views_With_Top_Init_With_Parameterless_Run ()
  1501. {
  1502. var win = new Window ();
  1503. var menu = new MenuBar
  1504. {
  1505. Menus =
  1506. [
  1507. new ("File", new MenuItem [] { new ("New", "", null) }),
  1508. new (
  1509. "Edit",
  1510. new MenuItem []
  1511. {
  1512. new MenuBarItem (
  1513. "Delete",
  1514. new MenuItem []
  1515. { new ("All", "", null), new ("Selected", "", null) }
  1516. )
  1517. }
  1518. )
  1519. ]
  1520. };
  1521. win.Add (menu);
  1522. Toplevel top = new ();
  1523. top.Add (win);
  1524. Application.AddTimeout (TimeSpan.Zero, () =>
  1525. {
  1526. AutoInitShutdownAttribute.FakeResize(new Size (40, 8));
  1527. DriverAssert.AssertDriverContentsWithFrameAre (
  1528. @"
  1529. ┌──────────────────────────────────────┐
  1530. │ File Edit │
  1531. │ │
  1532. │ │
  1533. │ │
  1534. │ │
  1535. │ │
  1536. └──────────────────────────────────────┘",
  1537. output
  1538. );
  1539. Assert.True (win.NewKeyDownEvent (menu.Key));
  1540. top.Draw ();
  1541. DriverAssert.AssertDriverContentsWithFrameAre (
  1542. @"
  1543. ┌──────────────────────────────────────┐
  1544. │ File Edit │
  1545. │┌──────┐ │
  1546. ││ New │ │
  1547. │└──────┘ │
  1548. │ │
  1549. │ │
  1550. └──────────────────────────────────────┘",
  1551. output
  1552. );
  1553. Assert.True (menu.NewKeyDownEvent (Key.CursorRight));
  1554. AutoInitShutdownAttribute.RunIteration ();
  1555. DriverAssert.AssertDriverContentsWithFrameAre (
  1556. @"
  1557. ┌──────────────────────────────────────┐
  1558. │ File Edit │
  1559. │ ┌─────────┐ │
  1560. │ │ Delete ►│ │
  1561. │ └─────────┘ │
  1562. │ │
  1563. │ │
  1564. └──────────────────────────────────────┘",
  1565. output
  1566. );
  1567. Assert.True (menu._openMenu.NewKeyDownEvent (Key.CursorRight));
  1568. top.Draw ();
  1569. DriverAssert.AssertDriverContentsWithFrameAre (
  1570. @"
  1571. ┌──────────────────────────────────────┐
  1572. │ File Edit │
  1573. │ ┌─────────┐ │
  1574. │ │ Delete ►│┌───────────┐ │
  1575. │ └─────────┘│ All │ │
  1576. │ │ Selected │ │
  1577. │ └───────────┘ │
  1578. └──────────────────────────────────────┘",
  1579. output
  1580. );
  1581. Assert.True (menu._openMenu.NewKeyDownEvent (Key.CursorRight));
  1582. View.SetClipToScreen ();
  1583. top.Draw ();
  1584. DriverAssert.AssertDriverContentsWithFrameAre (
  1585. @"
  1586. ┌──────────────────────────────────────┐
  1587. │ File Edit │
  1588. │┌──────┐ │
  1589. ││ New │ │
  1590. │└──────┘ │
  1591. │ │
  1592. │ │
  1593. └──────────────────────────────────────┘",
  1594. output
  1595. );
  1596. Application.RequestStop ();
  1597. return false;
  1598. });
  1599. Application.Run (top);
  1600. top.Dispose ();
  1601. }
  1602. [Fact]
  1603. [AutoInitShutdown]
  1604. public void MenuBar_In_Window_Without_Other_Views_Without_Top_Init ()
  1605. {
  1606. var win = new Window ();
  1607. var menu = new MenuBar
  1608. {
  1609. Menus =
  1610. [
  1611. new ("File", new MenuItem [] { new ("New", "", null) }),
  1612. new (
  1613. "Edit",
  1614. new MenuItem []
  1615. {
  1616. new MenuBarItem (
  1617. "Delete",
  1618. new MenuItem []
  1619. { new ("All", "", null), new ("Selected", "", null) }
  1620. )
  1621. }
  1622. )
  1623. ]
  1624. };
  1625. win.Add (menu);
  1626. AutoInitShutdownAttribute.FakeResize(new Size(40, 8));
  1627. RunState rs = Application.Begin (win);
  1628. AutoInitShutdownAttribute.RunIteration ();
  1629. DriverAssert.AssertDriverContentsWithFrameAre (
  1630. @"
  1631. ┌──────────────────────────────────────┐
  1632. │ File Edit │
  1633. │ │
  1634. │ │
  1635. │ │
  1636. │ │
  1637. │ │
  1638. └──────────────────────────────────────┘",
  1639. output
  1640. );
  1641. Assert.True (win.NewKeyDownEvent (menu.Key));
  1642. AutoInitShutdownAttribute.RunIteration ();
  1643. DriverAssert.AssertDriverContentsWithFrameAre (
  1644. @"
  1645. ┌──────────────────────────────────────┐
  1646. │ File Edit │
  1647. │┌──────┐ │
  1648. ││ New │ │
  1649. │└──────┘ │
  1650. │ │
  1651. │ │
  1652. └──────────────────────────────────────┘",
  1653. output
  1654. );
  1655. Assert.True (menu.NewKeyDownEvent (Key.CursorRight));
  1656. AutoInitShutdownAttribute.RunIteration ();
  1657. DriverAssert.AssertDriverContentsWithFrameAre (
  1658. @"
  1659. ┌──────────────────────────────────────┐
  1660. │ File Edit │
  1661. │ ┌─────────┐ │
  1662. │ │ Delete ►│ │
  1663. │ └─────────┘ │
  1664. │ │
  1665. │ │
  1666. └──────────────────────────────────────┘",
  1667. output
  1668. );
  1669. Assert.True (menu._openMenu.NewKeyDownEvent (Key.CursorRight));
  1670. AutoInitShutdownAttribute.RunIteration ();
  1671. DriverAssert.AssertDriverContentsWithFrameAre (
  1672. @"
  1673. ┌──────────────────────────────────────┐
  1674. │ File Edit │
  1675. │ ┌─────────┐ │
  1676. │ │ Delete ►│┌───────────┐ │
  1677. │ └─────────┘│ All │ │
  1678. │ │ Selected │ │
  1679. │ └───────────┘ │
  1680. └──────────────────────────────────────┘",
  1681. output
  1682. );
  1683. Assert.True (menu._openMenu.NewKeyDownEvent (Key.CursorRight));
  1684. AutoInitShutdownAttribute.RunIteration ();
  1685. DriverAssert.AssertDriverContentsWithFrameAre (
  1686. @"
  1687. ┌──────────────────────────────────────┐
  1688. │ File Edit │
  1689. │┌──────┐ │
  1690. ││ New │ │
  1691. │└──────┘ │
  1692. │ │
  1693. │ │
  1694. └──────────────────────────────────────┘",
  1695. output
  1696. );
  1697. win.Dispose ();
  1698. }
  1699. [Fact]
  1700. [AutoInitShutdown]
  1701. public void MenuBar_In_Window_Without_Other_Views_Without_Top_Init_With_Run_T ()
  1702. {
  1703. AutoInitShutdownAttribute.FakeResize(new Size(40, 8));
  1704. Application.AddTimeout (TimeSpan.Zero, () =>
  1705. {
  1706. Toplevel top = Application.Top;
  1707. AutoInitShutdownAttribute.RunIteration ();
  1708. DriverAssert.AssertDriverContentsWithFrameAre (
  1709. @"
  1710. ┌──────────────────────────────────────┐
  1711. │ File Edit │
  1712. │ │
  1713. │ │
  1714. │ │
  1715. │ │
  1716. │ │
  1717. └──────────────────────────────────────┘",
  1718. output
  1719. );
  1720. Assert.True (top.NewKeyDownEvent (Key.F9));
  1721. top.Draw ();
  1722. DriverAssert.AssertDriverContentsWithFrameAre (
  1723. @"
  1724. ┌──────────────────────────────────────┐
  1725. │ File Edit │
  1726. │┌──────┐ │
  1727. ││ New │ │
  1728. │└──────┘ │
  1729. │ │
  1730. │ │
  1731. └──────────────────────────────────────┘",
  1732. output
  1733. );
  1734. Assert.True (top.SubViews.ElementAt (0).NewKeyDownEvent (Key.CursorRight));
  1735. AutoInitShutdownAttribute.RunIteration ();
  1736. DriverAssert.AssertDriverContentsWithFrameAre (
  1737. @"
  1738. ┌──────────────────────────────────────┐
  1739. │ File Edit │
  1740. │ ┌─────────┐ │
  1741. │ │ Delete ►│ │
  1742. │ └─────────┘ │
  1743. │ │
  1744. │ │
  1745. └──────────────────────────────────────┘",
  1746. output
  1747. );
  1748. Assert.True (
  1749. ((MenuBar)top.SubViews.ElementAt (0))._openMenu.NewKeyDownEvent (Key.CursorRight)
  1750. );
  1751. top.Draw ();
  1752. DriverAssert.AssertDriverContentsWithFrameAre (
  1753. @"
  1754. ┌──────────────────────────────────────┐
  1755. │ File Edit │
  1756. │ ┌─────────┐ │
  1757. │ │ Delete ►│┌───────────┐ │
  1758. │ └─────────┘│ All │ │
  1759. │ │ Selected │ │
  1760. │ └───────────┘ │
  1761. └──────────────────────────────────────┘",
  1762. output
  1763. );
  1764. Assert.True (
  1765. ((MenuBar)top.SubViews.ElementAt (0))._openMenu.NewKeyDownEvent (Key.CursorRight)
  1766. );
  1767. View.SetClipToScreen ();
  1768. top.Draw ();
  1769. DriverAssert.AssertDriverContentsWithFrameAre (
  1770. @"
  1771. ┌──────────────────────────────────────┐
  1772. │ File Edit │
  1773. │┌──────┐ │
  1774. ││ New │ │
  1775. │└──────┘ │
  1776. │ │
  1777. │ │
  1778. └──────────────────────────────────────┘",
  1779. output
  1780. );
  1781. Application.RequestStop ();
  1782. return false;
  1783. });
  1784. Application.Run<CustomWindow> ().Dispose ();
  1785. }
  1786. [Fact]
  1787. [AutoInitShutdown]
  1788. public void MenuBar_Position_And_Size_With_HotKeys_Is_The_Same_As_Without_HotKeys ()
  1789. {
  1790. // Define the expected menu
  1791. var expectedMenu = new ExpectedMenuBar
  1792. {
  1793. Menus =
  1794. [
  1795. new ("File", new MenuItem [] { new ("12", "", null) }),
  1796. new (
  1797. "Edit",
  1798. new MenuItem [] { new ("Copy", "", null) }
  1799. )
  1800. ]
  1801. };
  1802. // Test without HotKeys first
  1803. var menu = new MenuBar
  1804. {
  1805. Menus =
  1806. [
  1807. new (
  1808. expectedMenu.Menus [0].Title,
  1809. new MenuItem [] { new (expectedMenu.Menus [0].Children [0].Title, "", null) }
  1810. ),
  1811. new (
  1812. expectedMenu.Menus [1].Title,
  1813. new MenuItem []
  1814. {
  1815. new (
  1816. expectedMenu.Menus [1].Children [0].Title,
  1817. "",
  1818. null
  1819. )
  1820. }
  1821. )
  1822. ]
  1823. };
  1824. var top = new Toplevel ();
  1825. top.Add (menu);
  1826. Application.Begin (top);
  1827. // Open first
  1828. Assert.True (menu.NewKeyDownEvent (menu.Key));
  1829. Assert.True (menu.IsMenuOpen);
  1830. top.Draw ();
  1831. DriverAssert.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (0), output);
  1832. // Open second
  1833. Assert.True (Application.Top.SubViews.ElementAt (1).NewKeyDownEvent (Key.CursorRight));
  1834. Assert.True (menu.IsMenuOpen);
  1835. View.SetClipToScreen ();
  1836. top.Draw ();
  1837. DriverAssert.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (1), output);
  1838. // Close menu
  1839. Assert.True (menu.NewKeyDownEvent (menu.Key));
  1840. Assert.False (menu.IsMenuOpen);
  1841. View.SetClipToScreen ();
  1842. top.Draw ();
  1843. DriverAssert.AssertDriverContentsAre (expectedMenu.ClosedMenuText, output);
  1844. top.Remove (menu);
  1845. // Now test WITH HotKeys
  1846. menu = new ()
  1847. {
  1848. Menus =
  1849. [
  1850. new (
  1851. "_" + expectedMenu.Menus [0].Title,
  1852. new MenuItem [] { new ("_" + expectedMenu.Menus [0].Children [0].Title, "", null) }
  1853. ),
  1854. new (
  1855. "_" + expectedMenu.Menus [1].Title,
  1856. new MenuItem []
  1857. {
  1858. new (
  1859. "_" + expectedMenu.Menus [1].Children [0].Title,
  1860. "",
  1861. null
  1862. )
  1863. }
  1864. )
  1865. ]
  1866. };
  1867. top.Add (menu);
  1868. // Open first
  1869. Assert.True (menu.NewKeyDownEvent (menu.Key));
  1870. Assert.True (menu.IsMenuOpen);
  1871. View.SetClipToScreen ();
  1872. top.Draw ();
  1873. DriverAssert.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (0), output);
  1874. // Open second
  1875. Assert.True (top.SubViews.ElementAt (1).NewKeyDownEvent (Key.CursorRight));
  1876. Assert.True (menu.IsMenuOpen);
  1877. View.SetClipToScreen ();
  1878. Application.Top.Draw ();
  1879. DriverAssert.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (1), output);
  1880. // Close menu
  1881. Assert.True (menu.NewKeyDownEvent (menu.Key));
  1882. Assert.False (menu.IsMenuOpen);
  1883. View.SetClipToScreen ();
  1884. top.Draw ();
  1885. DriverAssert.AssertDriverContentsAre (expectedMenu.ClosedMenuText, output);
  1886. top.Dispose ();
  1887. }
  1888. [Fact]
  1889. [AutoInitShutdown]
  1890. public void MenuBar_Submenus_Alignment_Correct ()
  1891. {
  1892. // Define the expected menu
  1893. var expectedMenu = new ExpectedMenuBar
  1894. {
  1895. Menus =
  1896. [
  1897. new (
  1898. "File",
  1899. new MenuItem []
  1900. {
  1901. new (
  1902. "Really Long Sub Menu",
  1903. "",
  1904. null
  1905. )
  1906. }
  1907. ),
  1908. new (
  1909. "123",
  1910. new MenuItem [] { new ("Copy", "", null) }
  1911. ),
  1912. new (
  1913. "Format",
  1914. new MenuItem [] { new ("Word Wrap", "", null) }
  1915. ),
  1916. new (
  1917. "Help",
  1918. new MenuItem [] { new ("About", "", null) }
  1919. ),
  1920. new (
  1921. "1",
  1922. new MenuItem [] { new ("2", "", null) }
  1923. ),
  1924. new (
  1925. "3",
  1926. new MenuItem [] { new ("2", "", null) }
  1927. ),
  1928. new (
  1929. "Last one",
  1930. new MenuItem [] { new ("Test", "", null) }
  1931. )
  1932. ]
  1933. };
  1934. MenuBarItem [] items = new MenuBarItem [expectedMenu.Menus.Length];
  1935. for (var i = 0; i < expectedMenu.Menus.Length; i++)
  1936. {
  1937. items [i] = new (
  1938. expectedMenu.Menus [i].Title,
  1939. new MenuItem [] { new (expectedMenu.Menus [i].Children [0].Title, "", null) }
  1940. );
  1941. }
  1942. var menu = new MenuBar { Menus = items };
  1943. var top = new Toplevel ();
  1944. top.Add (menu);
  1945. Application.Begin (top);
  1946. View.SetClipToScreen ();
  1947. top.Draw ();
  1948. DriverAssert.AssertDriverContentsAre (expectedMenu.ClosedMenuText, output);
  1949. for (var i = 0; i < expectedMenu.Menus.Length; i++)
  1950. {
  1951. menu.OpenMenu (i);
  1952. Assert.True (menu.IsMenuOpen);
  1953. View.SetClipToScreen ();
  1954. top.Draw ();
  1955. DriverAssert.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (i), output);
  1956. }
  1957. top.Dispose ();
  1958. }
  1959. [Fact]
  1960. [AutoInitShutdown]
  1961. public void MenuBar_With_Action_But_Without_MenuItems_Not_Throw ()
  1962. {
  1963. var menu = new MenuBar
  1964. {
  1965. Menus =
  1966. [
  1967. new () { Title = "Test 1", Action = () => { } },
  1968. new () { Title = "Test 2", Action = () => { } }
  1969. ]
  1970. };
  1971. var top = new Toplevel ();
  1972. top.Add (menu);
  1973. Application.Begin (top);
  1974. #if SUPPORT_ALT_TO_ACTIVATE_MENU
  1975. Assert.True (
  1976. Application.OnKeyUp (
  1977. new KeyEventArgs (
  1978. Key.AltMask
  1979. )
  1980. )
  1981. ); // changed to true because Alt activates menu bar
  1982. #endif
  1983. Assert.True (menu.NewKeyDownEvent (Key.CursorRight));
  1984. Assert.True (menu.NewKeyDownEvent (Key.CursorRight));
  1985. top.Dispose ();
  1986. }
  1987. [Fact]
  1988. [AutoInitShutdown]
  1989. public void MenuBarItem_Children_Null_Does_Not_Throw ()
  1990. {
  1991. var menu = new MenuBar
  1992. {
  1993. Menus =
  1994. [
  1995. new ("Test", "", null)
  1996. ]
  1997. };
  1998. var top = new Toplevel ();
  1999. top.Add (menu);
  2000. Exception exception = Record.Exception (() => menu.NewKeyDownEvent (Key.Space));
  2001. Assert.Null (exception);
  2002. top.Dispose ();
  2003. }
  2004. [Fact]
  2005. [AutoInitShutdown]
  2006. public void MenuOpened_On_Disabled_MenuItem ()
  2007. {
  2008. MenuItem parent = null;
  2009. MenuItem miCurrent = null;
  2010. Menu mCurrent = null;
  2011. var menu = new MenuBar
  2012. {
  2013. Menus =
  2014. [
  2015. new (
  2016. "_File",
  2017. new MenuItem []
  2018. {
  2019. new MenuBarItem (
  2020. "_New",
  2021. new MenuItem []
  2022. {
  2023. new (
  2024. "_New doc",
  2025. "Creates new doc.",
  2026. null,
  2027. () => false
  2028. )
  2029. }
  2030. ),
  2031. null,
  2032. new ("_Save", "Saves the file.", null)
  2033. }
  2034. )
  2035. ]
  2036. };
  2037. menu.MenuOpened += (s, e) =>
  2038. {
  2039. parent = e.Parent;
  2040. miCurrent = e.MenuItem;
  2041. mCurrent = menu._openMenu;
  2042. };
  2043. menu.UseKeysUpDownAsKeysLeftRight = true;
  2044. var top = new Toplevel ();
  2045. top.Add (menu);
  2046. Application.Begin (top);
  2047. // open the menu
  2048. Assert.True (
  2049. menu.NewMouseEvent (
  2050. new () { Position = new (1, 0), Flags = MouseFlags.Button1Pressed, View = menu }
  2051. )
  2052. );
  2053. Assert.True (menu.IsMenuOpen);
  2054. Assert.Equal ("_File", parent.Title);
  2055. Assert.Equal ("_File", miCurrent.Parent.Title);
  2056. Assert.Equal ("_New", miCurrent.Title);
  2057. Assert.True (
  2058. mCurrent.NewMouseEvent (
  2059. new () { Position = new (1, 1), Flags = MouseFlags.ReportMousePosition, View = mCurrent }
  2060. )
  2061. );
  2062. Assert.True (menu.IsMenuOpen);
  2063. Assert.Equal ("_File", parent.Title);
  2064. Assert.Equal ("_File", miCurrent.Parent.Title);
  2065. Assert.Equal ("_New", miCurrent.Title);
  2066. Assert.True (
  2067. mCurrent.NewMouseEvent (
  2068. new () { Position = new (1, 1), Flags = MouseFlags.ReportMousePosition, View = mCurrent }
  2069. )
  2070. );
  2071. Assert.True (menu.IsMenuOpen);
  2072. Assert.Equal ("_File", parent.Title);
  2073. Assert.Equal ("_File", miCurrent.Parent.Title);
  2074. Assert.Equal ("_New", miCurrent.Title);
  2075. Assert.True (
  2076. mCurrent.NewMouseEvent (
  2077. new () { Position = new (1, 2), Flags = MouseFlags.ReportMousePosition, View = mCurrent }
  2078. )
  2079. );
  2080. Assert.True (menu.IsMenuOpen);
  2081. Assert.Equal ("_File", parent.Title);
  2082. Assert.Equal ("_File", miCurrent.Parent.Title);
  2083. Assert.Equal ("_Save", miCurrent.Title);
  2084. // close the menu
  2085. Assert.True (
  2086. menu.NewMouseEvent (
  2087. new () { Position = new (1, 0), Flags = MouseFlags.Button1Pressed, View = menu }
  2088. )
  2089. );
  2090. Assert.False (menu.IsMenuOpen);
  2091. // open the menu
  2092. Assert.True (menu.NewKeyDownEvent (menu.Key));
  2093. Assert.True (menu.IsMenuOpen);
  2094. // The _New doc is enabled but the sub-menu isn't enabled. Is show but can't be selected and executed
  2095. Assert.Equal ("_New", parent.Title);
  2096. Assert.Equal ("_New", miCurrent.Parent.Title);
  2097. Assert.Equal ("_New doc", miCurrent.Title);
  2098. Assert.True (mCurrent.NewKeyDownEvent (Key.CursorDown));
  2099. Assert.True (menu.IsMenuOpen);
  2100. Assert.Equal ("_File", parent.Title);
  2101. Assert.Equal ("_File", miCurrent.Parent.Title);
  2102. Assert.Equal ("_Save", miCurrent.Title);
  2103. Assert.True (mCurrent.NewKeyDownEvent (Key.CursorUp));
  2104. Assert.True (menu.IsMenuOpen);
  2105. Assert.Equal ("_File", parent.Title);
  2106. Assert.Null (miCurrent);
  2107. // close the menu
  2108. Assert.True (menu.NewKeyDownEvent (menu.Key));
  2109. Assert.False (menu.IsMenuOpen);
  2110. top.Dispose ();
  2111. }
  2112. [Fact]
  2113. [AutoInitShutdown]
  2114. public void MenuOpening_MenuOpened_MenuClosing_Events ()
  2115. {
  2116. var miAction = "";
  2117. var isMenuClosed = true;
  2118. var cancelClosing = false;
  2119. var menu = new MenuBar
  2120. {
  2121. Menus =
  2122. [
  2123. new ("_File", new MenuItem [] { new ("_New", "Creates new file.", New) })
  2124. ]
  2125. };
  2126. menu.MenuOpening += (s, e) =>
  2127. {
  2128. Assert.Equal ("_File", e.CurrentMenu.Title);
  2129. Assert.Equal ("_New", e.CurrentMenu.Children [0].Title);
  2130. Assert.Equal ("Creates new file.", e.CurrentMenu.Children [0].Help);
  2131. Assert.Equal (New, e.CurrentMenu.Children [0].Action);
  2132. e.CurrentMenu.Children [0].Action ();
  2133. Assert.Equal ("New", miAction);
  2134. e.NewMenuBarItem = new (
  2135. "_Edit",
  2136. new MenuItem [] { new ("_Copy", "Copies the selection.", Copy) }
  2137. );
  2138. };
  2139. menu.MenuOpened += (s, e) =>
  2140. {
  2141. MenuItem mi = e.MenuItem;
  2142. Assert.Equal ("_Edit", mi.Parent.Title);
  2143. Assert.Equal ("_Copy", mi.Title);
  2144. Assert.Equal ("Copies the selection.", mi.Help);
  2145. Assert.Equal (Copy, mi.Action);
  2146. mi.Action ();
  2147. Assert.Equal ("Copy", miAction);
  2148. };
  2149. menu.MenuClosing += (s, e) =>
  2150. {
  2151. Assert.False (isMenuClosed);
  2152. if (cancelClosing)
  2153. {
  2154. e.Cancel = true;
  2155. isMenuClosed = false;
  2156. }
  2157. else
  2158. {
  2159. isMenuClosed = true;
  2160. }
  2161. };
  2162. var top = new Toplevel ();
  2163. top.Add (menu);
  2164. Application.Begin (top);
  2165. Assert.True (menu.NewKeyDownEvent (menu.Key));
  2166. Assert.True (menu.IsMenuOpen);
  2167. isMenuClosed = !menu.IsMenuOpen;
  2168. Assert.False (isMenuClosed);
  2169. top.Draw ();
  2170. var expected = @"
  2171. Edit
  2172. ┌──────────────────────────────┐
  2173. │ Copy Copies the selection. │
  2174. └──────────────────────────────┘
  2175. ";
  2176. DriverAssert.AssertDriverContentsAre (expected, output);
  2177. cancelClosing = true;
  2178. Assert.True (menu.NewKeyDownEvent (menu.Key));
  2179. Assert.True (menu.IsMenuOpen);
  2180. Assert.False (isMenuClosed);
  2181. View.SetClipToScreen ();
  2182. top.Draw ();
  2183. expected = @"
  2184. Edit
  2185. ┌──────────────────────────────┐
  2186. │ Copy Copies the selection. │
  2187. └──────────────────────────────┘
  2188. ";
  2189. DriverAssert.AssertDriverContentsAre (expected, output);
  2190. cancelClosing = false;
  2191. Assert.True (menu.NewKeyDownEvent (menu.Key));
  2192. Assert.False (menu.IsMenuOpen);
  2193. Assert.True (isMenuClosed);
  2194. View.SetClipToScreen ();
  2195. top.Draw ();
  2196. expected = @"
  2197. Edit
  2198. ";
  2199. DriverAssert.AssertDriverContentsAre (expected, output);
  2200. void New () { miAction = "New"; }
  2201. void Copy () { miAction = "Copy"; }
  2202. top.Dispose ();
  2203. }
  2204. [Fact]
  2205. [AutoInitShutdown]
  2206. public void MouseEvent_Test ()
  2207. {
  2208. MenuItem miCurrent = null;
  2209. Menu mCurrent = null;
  2210. var menuBar = new MenuBar
  2211. {
  2212. Menus =
  2213. [
  2214. new (
  2215. "_File",
  2216. new MenuItem [] { new ("_New", "", null), new ("_Open", "", null), new ("_Save", "", null) }
  2217. ),
  2218. new (
  2219. "_Edit",
  2220. new MenuItem [] { new ("_Copy", "", null), new ("C_ut", "", null), new ("_Paste", "", null) }
  2221. )
  2222. ]
  2223. };
  2224. menuBar.MenuOpened += (s, e) =>
  2225. {
  2226. miCurrent = e.MenuItem;
  2227. mCurrent = menuBar.OpenCurrentMenu;
  2228. };
  2229. var top = new Toplevel ();
  2230. top.Add (menuBar);
  2231. Application.Begin (top);
  2232. // Click on Edit
  2233. Assert.True (
  2234. menuBar.NewMouseEvent (
  2235. new () { Position = new (10, 0), Flags = MouseFlags.Button1Pressed, View = menuBar }
  2236. )
  2237. );
  2238. Assert.True (menuBar.IsMenuOpen);
  2239. Assert.Equal ("_Edit", miCurrent.Parent.Title);
  2240. Assert.Equal ("_Copy", miCurrent.Title);
  2241. // Click on Paste
  2242. Assert.True (
  2243. mCurrent.NewMouseEvent (
  2244. new () { Position = new (10, 2), Flags = MouseFlags.ReportMousePosition, View = mCurrent }
  2245. )
  2246. );
  2247. Assert.True (menuBar.IsMenuOpen);
  2248. Assert.Equal ("_Edit", miCurrent.Parent.Title);
  2249. Assert.Equal ("_Paste", miCurrent.Title);
  2250. for (var i = 4; i >= -1; i--)
  2251. {
  2252. Application.RaiseMouseEvent (
  2253. new () { ScreenPosition = new (10, i), Flags = MouseFlags.ReportMousePosition }
  2254. );
  2255. Assert.True (menuBar.IsMenuOpen);
  2256. Menu menu = (Menu)top.SubViews.First (v => v is Menu);
  2257. if (i is < 0 or > 0)
  2258. {
  2259. Assert.Equal (menu, Application.Mouse.MouseGrabView);
  2260. }
  2261. else
  2262. {
  2263. Assert.Equal (menuBar, Application.Mouse.MouseGrabView);
  2264. }
  2265. Assert.Equal ("_Edit", miCurrent.Parent.Title);
  2266. if (i == 4)
  2267. {
  2268. Assert.Equal ("_Paste", miCurrent.Title);
  2269. }
  2270. else if (i == 3)
  2271. {
  2272. Assert.Equal ("C_ut", miCurrent.Title);
  2273. }
  2274. else if (i == 2)
  2275. {
  2276. Assert.Equal ("_Copy", miCurrent.Title);
  2277. }
  2278. else
  2279. {
  2280. Assert.Equal ("_Copy", miCurrent.Title);
  2281. }
  2282. }
  2283. top.Dispose ();
  2284. }
  2285. [Fact]
  2286. [AutoInitShutdown]
  2287. public void Parent_MenuItem_Stay_Focused_If_Child_MenuItem_Is_Empty_By_Keyboard ()
  2288. {
  2289. var expectedMenu = new ExpectedMenuBar
  2290. {
  2291. Menus =
  2292. [
  2293. new ("File", new MenuItem [] { new ("New", "", null) }),
  2294. new ("Edit", Array.Empty<MenuItem> ()),
  2295. new (
  2296. "Format",
  2297. new MenuItem [] { new ("Wrap", "", null) }
  2298. )
  2299. ]
  2300. };
  2301. MenuBarItem [] items = new MenuBarItem [expectedMenu.Menus.Length];
  2302. for (var i = 0; i < expectedMenu.Menus.Length; i++)
  2303. {
  2304. items [i] = new (
  2305. expectedMenu.Menus [i].Title,
  2306. expectedMenu.Menus [i].Children.Length > 0
  2307. ? new MenuItem [] { new (expectedMenu.Menus [i].Children [0].Title, "", null) }
  2308. : Array.Empty<MenuItem> ()
  2309. );
  2310. }
  2311. var menu = new MenuBar { Menus = items };
  2312. var tf = new TextField { Y = 2, Width = 10 };
  2313. var top = new Toplevel ();
  2314. top.Add (menu, tf);
  2315. Application.Begin (top);
  2316. Assert.True (tf.HasFocus);
  2317. Assert.True (menu.NewKeyDownEvent (menu.Key));
  2318. Assert.True (menu.IsMenuOpen);
  2319. Assert.False (tf.HasFocus);
  2320. top.Draw ();
  2321. DriverAssert.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (0), output);
  2322. // Right - Edit has no sub menu; this tests that no sub menu shows
  2323. Assert.True (menu._openMenu.NewKeyDownEvent (Key.CursorRight));
  2324. Assert.True (menu.IsMenuOpen);
  2325. Assert.False (tf.HasFocus);
  2326. Assert.Equal (1, menu._selected);
  2327. Assert.Equal (-1, menu._selectedSub);
  2328. Assert.Null (menu._openSubMenu);
  2329. View.SetClipToScreen ();
  2330. top.Draw ();
  2331. DriverAssert.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (1), output);
  2332. // Right - Format
  2333. Assert.True (menu._openMenu.NewKeyDownEvent (Key.CursorRight));
  2334. Assert.True (menu.IsMenuOpen);
  2335. Assert.False (tf.HasFocus);
  2336. View.SetClipToScreen ();
  2337. top.Draw ();
  2338. DriverAssert.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (2), output);
  2339. // Left - Edit
  2340. Assert.True (menu._openMenu.NewKeyDownEvent (Key.CursorLeft));
  2341. Assert.True (menu.IsMenuOpen);
  2342. Assert.False (tf.HasFocus);
  2343. View.SetClipToScreen ();
  2344. top.Draw ();
  2345. DriverAssert.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (1), output);
  2346. Assert.True (menu._openMenu.NewKeyDownEvent (Key.CursorLeft));
  2347. Assert.True (menu.IsMenuOpen);
  2348. Assert.False (tf.HasFocus);
  2349. View.SetClipToScreen ();
  2350. top.Draw ();
  2351. DriverAssert.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (0), output);
  2352. Assert.True (Application.RaiseKeyDownEvent (menu.Key));
  2353. Assert.False (menu.IsMenuOpen);
  2354. Assert.True (tf.HasFocus);
  2355. View.SetClipToScreen ();
  2356. top.Draw ();
  2357. DriverAssert.AssertDriverContentsAre (expectedMenu.ClosedMenuText, output);
  2358. top.Dispose ();
  2359. }
  2360. [Fact]
  2361. [AutoInitShutdown]
  2362. public void Parent_MenuItem_Stay_Focused_If_Child_MenuItem_Is_Empty_By_Mouse ()
  2363. {
  2364. // File Edit Format
  2365. //┌──────┐ ┌───────┐
  2366. //│ New │ │ Wrap │
  2367. //└──────┘ └───────┘
  2368. // Define the expected menu
  2369. var expectedMenu = new ExpectedMenuBar
  2370. {
  2371. Menus =
  2372. [
  2373. new ("File", new MenuItem [] { new ("New", "", null) }),
  2374. new ("Edit", new MenuItem [] { }),
  2375. new (
  2376. "Format",
  2377. new MenuItem [] { new ("Wrap", "", null) }
  2378. )
  2379. ]
  2380. };
  2381. var menu = new MenuBar
  2382. {
  2383. Menus =
  2384. [
  2385. new (
  2386. expectedMenu.Menus [0].Title,
  2387. new MenuItem [] { new (expectedMenu.Menus [0].Children [0].Title, "", null) }
  2388. ),
  2389. new (expectedMenu.Menus [1].Title, new MenuItem [] { }),
  2390. new (
  2391. expectedMenu.Menus [2].Title,
  2392. new MenuItem []
  2393. {
  2394. new (
  2395. expectedMenu.Menus [2].Children [0].Title,
  2396. "",
  2397. null
  2398. )
  2399. }
  2400. )
  2401. ]
  2402. };
  2403. var tf = new TextField { Y = 2, Width = 10 };
  2404. var top = new Toplevel ();
  2405. top.Add (menu, tf);
  2406. Application.Begin (top);
  2407. Assert.True (tf.HasFocus);
  2408. Assert.True (menu.NewMouseEvent (new () { Position = new (1, 0), Flags = MouseFlags.Button1Pressed, View = menu }));
  2409. Assert.True (menu.IsMenuOpen);
  2410. Assert.False (tf.HasFocus);
  2411. top.Draw ();
  2412. DriverAssert.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (0), output);
  2413. Assert.True (
  2414. menu.NewMouseEvent (
  2415. new () { Position = new (8, 0), Flags = MouseFlags.ReportMousePosition, View = menu }
  2416. )
  2417. );
  2418. Assert.True (menu.IsMenuOpen);
  2419. Assert.False (tf.HasFocus);
  2420. View.SetClipToScreen ();
  2421. top.Draw ();
  2422. DriverAssert.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (1), output);
  2423. Assert.True (
  2424. menu.NewMouseEvent (
  2425. new () { Position = new (15, 0), Flags = MouseFlags.ReportMousePosition, View = menu }
  2426. )
  2427. );
  2428. Assert.True (menu.IsMenuOpen);
  2429. Assert.False (tf.HasFocus);
  2430. View.SetClipToScreen ();
  2431. top.Draw ();
  2432. DriverAssert.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (2), output);
  2433. Assert.True (
  2434. menu.NewMouseEvent (
  2435. new () { Position = new (8, 0), Flags = MouseFlags.ReportMousePosition, View = menu }
  2436. )
  2437. );
  2438. Assert.True (menu.IsMenuOpen);
  2439. Assert.False (tf.HasFocus);
  2440. View.SetClipToScreen ();
  2441. top.Draw ();
  2442. DriverAssert.AssertDriverContentsAre (expectedMenu.ClosedMenuText, output);
  2443. Assert.True (
  2444. menu.NewMouseEvent (
  2445. new () { Position = new (1, 0), Flags = MouseFlags.ReportMousePosition, View = menu }
  2446. )
  2447. );
  2448. Assert.True (menu.IsMenuOpen);
  2449. Assert.False (tf.HasFocus);
  2450. View.SetClipToScreen ();
  2451. top.Draw ();
  2452. DriverAssert.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (0), output);
  2453. Assert.True (menu.NewMouseEvent (new () { Position = new (8, 0), Flags = MouseFlags.Button1Pressed, View = menu }));
  2454. Assert.False (menu.IsMenuOpen);
  2455. Assert.True (tf.HasFocus);
  2456. View.SetClipToScreen ();
  2457. top.Draw ();
  2458. DriverAssert.AssertDriverContentsAre (expectedMenu.ClosedMenuText, output);
  2459. top.Dispose ();
  2460. }
  2461. [Fact]
  2462. public void RemoveAndThenAddMenuBar_ShouldNotChangeWidth ()
  2463. {
  2464. MenuBar menuBar;
  2465. MenuBar menuBar2;
  2466. // TODO: When https: //github.com/gui-cs/Terminal.Gui/issues/3136 is fixed,
  2467. // TODO: Change this to Window
  2468. var w = new View ();
  2469. menuBar2 = new ();
  2470. menuBar = new ();
  2471. w.Width = Dim.Fill ();
  2472. w.Height = Dim.Fill ();
  2473. w.X = 0;
  2474. w.Y = 0;
  2475. w.Visible = true;
  2476. // TODO: When https: //github.com/gui-cs/Terminal.Gui/issues/3136 is fixed,
  2477. // TODO: uncomment this.
  2478. //w.Modal = false;
  2479. w.Title = "";
  2480. menuBar.Width = Dim.Fill ();
  2481. menuBar.Height = 1;
  2482. menuBar.X = 0;
  2483. menuBar.Y = 0;
  2484. menuBar.Visible = true;
  2485. w.Add (menuBar);
  2486. menuBar2.Width = Dim.Fill ();
  2487. menuBar2.Height = 1;
  2488. menuBar2.X = 0;
  2489. menuBar2.Y = 4;
  2490. menuBar2.Visible = true;
  2491. w.Add (menuBar2);
  2492. MenuBar [] menuBars = w.SubViews.OfType<MenuBar> ().ToArray ();
  2493. Assert.Equal (2, menuBars.Length);
  2494. Assert.Equal (Dim.Fill (), menuBars [0].Width);
  2495. Assert.Equal (Dim.Fill (), menuBars [1].Width);
  2496. // Goes wrong here
  2497. w.Remove (menuBar);
  2498. w.Remove (menuBar2);
  2499. w.Add (menuBar);
  2500. w.Add (menuBar2);
  2501. // These assertions fail
  2502. Assert.Equal (Dim.Fill (), menuBars [0].Width);
  2503. Assert.Equal (Dim.Fill (), menuBars [1].Width);
  2504. }
  2505. [Fact]
  2506. [AutoInitShutdown]
  2507. public void Resizing_Close_Menus ()
  2508. {
  2509. var menu = new MenuBar
  2510. {
  2511. Menus =
  2512. [
  2513. new (
  2514. "File",
  2515. new MenuItem []
  2516. {
  2517. new (
  2518. "Open",
  2519. "Open a file",
  2520. () => { },
  2521. null,
  2522. null,
  2523. KeyCode.CtrlMask | KeyCode.O
  2524. )
  2525. }
  2526. )
  2527. ]
  2528. };
  2529. var top = new Toplevel ();
  2530. top.Add (menu);
  2531. RunState rs = Application.Begin (top);
  2532. menu.OpenMenu ();
  2533. AutoInitShutdownAttribute.RunIteration ();
  2534. DriverAssert.AssertDriverContentsWithFrameAre (
  2535. @"
  2536. File
  2537. ┌────────────────────────────┐
  2538. │ Open Open a file Ctrl+O │
  2539. └────────────────────────────┘",
  2540. output
  2541. );
  2542. AutoInitShutdownAttribute.FakeResize(new Size(20, 15));
  2543. AutoInitShutdownAttribute.RunIteration ();
  2544. DriverAssert.AssertDriverContentsWithFrameAre (
  2545. @"
  2546. File",
  2547. output
  2548. );
  2549. Application.End (rs);
  2550. top.Dispose ();
  2551. }
  2552. [Fact]
  2553. public void Separator_Does_Not_Throws_Pressing_Menu_Hotkey ()
  2554. {
  2555. var menu = new MenuBar
  2556. {
  2557. Menus =
  2558. [
  2559. new (
  2560. "File",
  2561. new MenuItem [] { new ("_New", "", null), null, new ("_Quit", "", null) }
  2562. )
  2563. ]
  2564. };
  2565. Assert.False (menu.NewKeyDownEvent (Key.Q.WithAlt));
  2566. }
  2567. [Fact]
  2568. public void SetMenus_With_Same_HotKey_Does_Not_Throws ()
  2569. {
  2570. var mb = new MenuBar ();
  2571. var i1 = new MenuBarItem ("_heey", "fff", () => { }, () => true);
  2572. mb.Menus = new [] { i1 };
  2573. mb.Menus = new [] { i1 };
  2574. Assert.Equal (Key.H, mb.Menus [0].HotKey);
  2575. }
  2576. [Fact]
  2577. [AutoInitShutdown]
  2578. public void ShortCut_Activates ()
  2579. {
  2580. var saveAction = false;
  2581. var menu = new MenuBar
  2582. {
  2583. Menus =
  2584. [
  2585. new (
  2586. "_File",
  2587. new MenuItem []
  2588. {
  2589. new (
  2590. "_Save",
  2591. "Saves the file.",
  2592. () => { saveAction = true; },
  2593. null,
  2594. null,
  2595. (KeyCode)Key.S.WithCtrl
  2596. )
  2597. }
  2598. )
  2599. ]
  2600. };
  2601. var top = new Toplevel ();
  2602. top.Add (menu);
  2603. Application.Begin (top);
  2604. Application.RaiseKeyDownEvent (Key.S.WithCtrl);
  2605. AutoInitShutdownAttribute.RunIteration ();
  2606. Assert.True (saveAction);
  2607. top.Dispose ();
  2608. }
  2609. [Fact]
  2610. public void Update_ShortcutKey_KeyBindings_Old_ShortcutKey_Is_Removed ()
  2611. {
  2612. var menuBar = new MenuBar
  2613. {
  2614. Menus =
  2615. [
  2616. new (
  2617. "_File",
  2618. new MenuItem []
  2619. {
  2620. new ("New", "Create New", null, null, null, Key.A.WithCtrl)
  2621. }
  2622. )
  2623. ]
  2624. };
  2625. Assert.True (menuBar.HotKeyBindings.TryGet (Key.A.WithCtrl, out _));
  2626. menuBar.Menus [0].Children! [0].ShortcutKey = Key.B.WithCtrl;
  2627. Assert.False (menuBar.HotKeyBindings.TryGet (Key.A.WithCtrl, out _));
  2628. Assert.True (menuBar.HotKeyBindings.TryGet (Key.B.WithCtrl, out _));
  2629. }
  2630. [Fact]
  2631. public void UseKeysUpDownAsKeysLeftRight_And_UseSubMenusSingleFrame_Cannot_Be_Both_True ()
  2632. {
  2633. var menu = new MenuBar ();
  2634. Assert.False (menu.UseKeysUpDownAsKeysLeftRight);
  2635. Assert.False (menu.UseSubMenusSingleFrame);
  2636. menu.UseKeysUpDownAsKeysLeftRight = true;
  2637. Assert.True (menu.UseKeysUpDownAsKeysLeftRight);
  2638. Assert.False (menu.UseSubMenusSingleFrame);
  2639. menu.UseSubMenusSingleFrame = true;
  2640. Assert.False (menu.UseKeysUpDownAsKeysLeftRight);
  2641. Assert.True (menu.UseSubMenusSingleFrame);
  2642. }
  2643. [Fact (Skip = "#3798 Broke. Will fix in #2975")]
  2644. [AutoInitShutdown]
  2645. public void UseSubMenusSingleFrame_False_By_Keyboard ()
  2646. {
  2647. var menu = new MenuBar
  2648. {
  2649. Menus = new MenuBarItem []
  2650. {
  2651. new (
  2652. "Numbers",
  2653. new MenuItem []
  2654. {
  2655. new ("One", "", null),
  2656. new MenuBarItem (
  2657. "Two",
  2658. new MenuItem []
  2659. {
  2660. new ("Sub-Menu 1", "", null),
  2661. new ("Sub-Menu 2", "", null)
  2662. }
  2663. ),
  2664. new ("Three", "", null)
  2665. }
  2666. )
  2667. }
  2668. };
  2669. menu.UseKeysUpDownAsKeysLeftRight = true;
  2670. var top = new Toplevel ();
  2671. top.Add (menu);
  2672. Application.Begin (top);
  2673. Assert.Equal (Point.Empty, new (menu.Frame.X, menu.Frame.Y));
  2674. Assert.False (menu.UseSubMenusSingleFrame);
  2675. top.Draw ();
  2676. var expected = @"
  2677. Numbers
  2678. ";
  2679. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  2680. Assert.True (menu.NewKeyDownEvent (menu.Key));
  2681. top.Draw ();
  2682. expected = @"
  2683. Numbers
  2684. ┌────────┐
  2685. │ One │
  2686. │ Two ►│
  2687. │ Three │
  2688. └────────┘
  2689. ";
  2690. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  2691. Assert.True (Application.Top.SubViews.ElementAt (1).NewKeyDownEvent (Key.CursorDown));
  2692. top.Draw ();
  2693. expected = @"
  2694. Numbers
  2695. ┌────────┐
  2696. │ One │
  2697. │ Two ►│┌─────────────┐
  2698. │ Three ││ Sub-Menu 1 │
  2699. └────────┘│ Sub-Menu 2 │
  2700. └─────────────┘
  2701. ";
  2702. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  2703. Assert.True (Application.Top.SubViews.ElementAt (2).NewKeyDownEvent (Key.CursorLeft));
  2704. top.Draw ();
  2705. expected = @"
  2706. Numbers
  2707. ┌────────┐
  2708. │ One │
  2709. │ Two ►│
  2710. │ Three │
  2711. └────────┘
  2712. ";
  2713. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  2714. Assert.True (Application.Top.SubViews.ElementAt (1).NewKeyDownEvent (Key.Esc));
  2715. top.Draw ();
  2716. expected = @"
  2717. Numbers
  2718. ";
  2719. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  2720. top.Dispose ();
  2721. }
  2722. [Fact (Skip = "#3798 Broke. Will fix in #2975")]
  2723. [AutoInitShutdown]
  2724. public void UseSubMenusSingleFrame_False_By_Mouse ()
  2725. {
  2726. var menu = new MenuBar
  2727. {
  2728. Menus =
  2729. [
  2730. new (
  2731. "Numbers",
  2732. new MenuItem []
  2733. {
  2734. new ("One", "", null),
  2735. new MenuBarItem (
  2736. "Two",
  2737. new MenuItem []
  2738. {
  2739. new (
  2740. "Sub-Menu 1",
  2741. "",
  2742. null
  2743. ),
  2744. new (
  2745. "Sub-Menu 2",
  2746. "",
  2747. null
  2748. )
  2749. }
  2750. ),
  2751. new ("Three", "", null)
  2752. }
  2753. )
  2754. ]
  2755. };
  2756. var top = new Toplevel ();
  2757. top.Add (menu);
  2758. Application.Begin (top);
  2759. Assert.Equal (Point.Empty, new (menu.Frame.X, menu.Frame.Y));
  2760. Assert.False (menu.UseSubMenusSingleFrame);
  2761. top.Draw ();
  2762. var expected = @"
  2763. Numbers
  2764. ";
  2765. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  2766. Assert.Equal (new (1, 0, 8, 1), pos);
  2767. menu.NewMouseEvent (
  2768. new () { Position = new (1, 0), Flags = MouseFlags.Button1Pressed, View = menu }
  2769. );
  2770. top.Draw ();
  2771. expected = @"
  2772. Numbers
  2773. ┌────────┐
  2774. │ One │
  2775. │ Two ►│
  2776. │ Three │
  2777. └────────┘
  2778. ";
  2779. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  2780. Assert.Equal (new (1, 0, 10, 6), pos);
  2781. menu.NewMouseEvent (
  2782. new ()
  2783. {
  2784. Position = new (1, 2), Flags = MouseFlags.ReportMousePosition, View = Application.Top.SubViews.ElementAt (1)
  2785. }
  2786. );
  2787. top.Draw ();
  2788. expected = @"
  2789. Numbers
  2790. ┌────────┐
  2791. │ One │
  2792. │ Two ►│┌─────────────┐
  2793. │ Three ││ Sub-Menu 1 │
  2794. └────────┘│ Sub-Menu 2 │
  2795. └─────────────┘
  2796. ";
  2797. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  2798. Assert.Equal (new (1, 0, 25, 7), pos);
  2799. Assert.False (
  2800. menu.NewMouseEvent (
  2801. new ()
  2802. {
  2803. Position = new (1, 1), Flags = MouseFlags.ReportMousePosition, View = Application.Top.SubViews.ElementAt (1)
  2804. }
  2805. )
  2806. );
  2807. top.Draw ();
  2808. expected = @"
  2809. Numbers
  2810. ┌────────┐
  2811. │ One │
  2812. │ Two ►│
  2813. │ Three │
  2814. └────────┘
  2815. ";
  2816. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  2817. Assert.Equal (new (1, 0, 10, 6), pos);
  2818. menu.NewMouseEvent (
  2819. new () { Position = new (70, 2), Flags = MouseFlags.Button1Clicked, View = Application.Top }
  2820. );
  2821. top.Draw ();
  2822. expected = @"
  2823. Numbers
  2824. ";
  2825. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  2826. Assert.Equal (new (1, 0, 8, 1), pos);
  2827. top.Dispose ();
  2828. }
  2829. [Fact]
  2830. [AutoInitShutdown]
  2831. public void Visible_False_Key_Does_Not_Open_And_Close_All_Opened_Menus ()
  2832. {
  2833. var menu = new MenuBar
  2834. {
  2835. Menus =
  2836. [
  2837. new ("File", new MenuItem [] { new ("New", "", null) })
  2838. ]
  2839. };
  2840. var top = new Toplevel ();
  2841. top.Add (menu);
  2842. Application.Begin (top);
  2843. Assert.True (menu.Visible);
  2844. Assert.True (menu.NewKeyDownEvent (menu.Key));
  2845. Assert.True (menu.IsMenuOpen);
  2846. menu.Visible = false;
  2847. Assert.False (menu.IsMenuOpen);
  2848. Assert.True (menu.NewKeyDownEvent (menu.Key));
  2849. Assert.False (menu.IsMenuOpen);
  2850. top.Dispose ();
  2851. }
  2852. [Fact]
  2853. [AutoInitShutdown]
  2854. public void CanFocus_True_Key_Esc_Exit_Toplevel_If_IsMenuOpen_False ()
  2855. {
  2856. var menu = new MenuBar
  2857. {
  2858. Menus =
  2859. [
  2860. new ("File", new MenuItem [] { new ("New", "", null) })
  2861. ],
  2862. CanFocus = true
  2863. };
  2864. var top = new Toplevel ();
  2865. top.Add (menu);
  2866. Application.Begin (top);
  2867. Assert.True (menu.CanFocus);
  2868. Assert.True (menu.NewKeyDownEvent (menu.Key));
  2869. Assert.True (menu.IsMenuOpen);
  2870. Assert.True (menu.NewKeyDownEvent (Key.Esc));
  2871. Assert.False (menu.IsMenuOpen);
  2872. Assert.False (menu.NewKeyDownEvent (Key.Esc));
  2873. Assert.False (menu.IsMenuOpen);
  2874. top.Dispose ();
  2875. }
  2876. // Defines the expected strings for a Menu. Currently supports
  2877. // - MenuBar with any number of MenuItems
  2878. // - Each top-level MenuItem can have a SINGLE sub-menu
  2879. //
  2880. // TODO: Enable multiple sub-menus
  2881. // TODO: Enable checked sub-menus
  2882. // TODO: Enable sub-menus with sub-menus (perhaps better to put this in a separate class with focused unit tests?)
  2883. //
  2884. // E.g:
  2885. //
  2886. // File Edit
  2887. // New Copy
  2888. public class ExpectedMenuBar : MenuBar
  2889. {
  2890. // The expected strings when the menu is closed
  2891. public string ClosedMenuText => MenuBarText + "\n";
  2892. public string ExpectedBottomRow (int i)
  2893. {
  2894. return $"{Glyphs.LLCorner}{new (Glyphs.HLine.ToString () [0], Menus [i].Children [0].TitleLength + 3)}{Glyphs.LRCorner} \n";
  2895. }
  2896. // The 3 spaces at end are a result of Menu.cs line 1062 where `pos` is calculated (` + spacesAfterTitle`)
  2897. public string ExpectedMenuItemRow (int i) { return $"{Glyphs.VLine} {Menus [i].Children [0].Title} {Glyphs.VLine} \n"; }
  2898. // The full expected string for an open sub menu
  2899. public string ExpectedSubMenuOpen (int i)
  2900. {
  2901. return ClosedMenuText
  2902. + (Menus [i].Children.Length > 0
  2903. ? ExpectedPadding (i)
  2904. + ExpectedTopRow (i)
  2905. + ExpectedPadding (i)
  2906. + ExpectedMenuItemRow (i)
  2907. + ExpectedPadding (i)
  2908. + ExpectedBottomRow (i)
  2909. : "");
  2910. }
  2911. // Define expected menu frame
  2912. // "┌──────┐"
  2913. // "│ New │"
  2914. // "└──────┘"
  2915. //
  2916. // The width of the Frame is determined in Menu.cs line 144, where `Width` is calculated
  2917. // 1 space before the Title and 2 spaces after the Title/Check/Help
  2918. public string ExpectedTopRow (int i)
  2919. {
  2920. return $"{Glyphs.ULCorner}{new (Glyphs.HLine.ToString () [0], Menus [i].Children [0].TitleLength + 3)}{Glyphs.URCorner} \n";
  2921. }
  2922. // Each MenuBar title has a 1 space pad on each side
  2923. // See `static int leftPadding` and `static int rightPadding` on line 1037 of Menu.cs
  2924. public string MenuBarText
  2925. {
  2926. get
  2927. {
  2928. var txt = string.Empty;
  2929. foreach (MenuBarItem m in Menus)
  2930. {
  2931. txt += " " + m.Title + " ";
  2932. }
  2933. return txt;
  2934. }
  2935. }
  2936. // Padding for the X of the sub menu Frame
  2937. // Menu.cs - Line 1239 in `internal void OpenMenu` is where the Menu is created
  2938. private string ExpectedPadding (int i)
  2939. {
  2940. var n = 0;
  2941. while (i > 0)
  2942. {
  2943. n += Menus [i - 1].TitleLength + 2;
  2944. i--;
  2945. }
  2946. return new (' ', n);
  2947. }
  2948. }
  2949. private class CustomWindow : Window
  2950. {
  2951. public CustomWindow ()
  2952. {
  2953. var menu = new MenuBar
  2954. {
  2955. Menus =
  2956. [
  2957. new ("File", new MenuItem [] { new ("New", "", null) }),
  2958. new (
  2959. "Edit",
  2960. new MenuItem []
  2961. {
  2962. new MenuBarItem (
  2963. "Delete",
  2964. new MenuItem []
  2965. { new ("All", "", null), new ("Selected", "", null) }
  2966. )
  2967. }
  2968. )
  2969. ]
  2970. };
  2971. Add (menu);
  2972. }
  2973. }
  2974. }
  2975. #pragma warning restore CS0618 // Type or member is obsolete