MenuBarv1Tests.cs 122 KB

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