MenuBarv1Tests.cs 122 KB

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