123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938 |
- using System.Globalization;
- using Xunit.Abstractions;
- namespace Terminal.Gui.ViewsTests;
- public class ContextMenuTests (ITestOutputHelper output)
- {
- [Fact]
- [AutoInitShutdown]
- public void ContextMenu_Constructors ()
- {
- var cm = new ContextMenu ();
- var top = new Toplevel ();
- Application.Begin (top);
- Assert.Equal (Point.Empty, cm.Position);
- Assert.Null (cm.MenuItems);
- Assert.Null (cm.Host);
- cm.Position = new Point (20, 10);
- var menuItems = new MenuBarItem (
- [
- new MenuItem ("First", "", null)
- ]
- );
- cm.Show (menuItems);
- Assert.Equal (new Point (20, 10), cm.Position);
- Assert.Single (cm.MenuItems!.Children);
- cm = new ContextMenu
- {
- Position = new Point (5, 10)
- };
- menuItems = new MenuBarItem (
- new [] { new MenuItem ("One", "", null), new MenuItem ("Two", "", null) }
- );
- cm.Show (menuItems);
- Assert.Equal (new Point (5, 10), cm.Position);
- Assert.Equal (2, cm.MenuItems!.Children.Length);
- Assert.Null (cm.Host);
- var view = new View { X = 5, Y = 10 };
- top.Add (view);
- cm = new ContextMenu
- {
- Host = view,
- Position = new Point (5, 10)
- };
- menuItems = new MenuBarItem (
- new [] { new MenuItem ("One", "", null), new MenuItem ("Two", "", null) }
- );
- cm.Show (menuItems);
- Assert.Equal (new Point (5, 10), cm.Position);
- Assert.Equal (2, cm.MenuItems.Children.Length);
- Assert.NotNull (cm.Host);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void ContextMenu_Is_Closed_If_Another_MenuBar_Is_Open_Or_Vice_Versa ()
- {
- var cm = new ContextMenu
- {
- Position = new Point (10, 5)
- };
- var menuItems = new MenuBarItem (
- [
- new MenuItem ("One", "", null),
- new MenuItem ("Two", "", null)
- ]
- );
- var menu = new MenuBar
- {
- Menus =
- [
- new MenuBarItem ("File", "", null),
- new MenuBarItem ("Edit", "", null)
- ]
- };
- var top = new Toplevel ();
- top.Add (menu);
- Application.Begin (top);
- Assert.Null (Application.MouseGrabView);
- cm.Show (menuItems);
- Assert.True (ContextMenu.IsShow);
- Assert.Equal (cm.MenuBar, Application.MouseGrabView);
- Assert.False (menu.IsMenuOpen);
- Assert.True (menu.NewKeyDownEvent (menu.Key));
- Assert.False (ContextMenu.IsShow);
- Assert.Equal (menu, Application.MouseGrabView);
- Assert.True (menu.IsMenuOpen);
- cm.Show (menuItems);
- Assert.True (ContextMenu.IsShow);
- Assert.Equal (cm.MenuBar, Application.MouseGrabView);
- Assert.False (menu.IsMenuOpen);
- #if SUPPORT_ALT_TO_ACTIVATE_MENU
- Assert.True (Application.Top.ProcessKeyUp (new (Key.AltMask)));
- Assert.False (ContextMenu.IsShow);
- Assert.Equal (menu, Application.MouseGrabView);
- Assert.True (menu.IsMenuOpen);
- #endif
- cm.Show (menuItems);
- Assert.True (ContextMenu.IsShow);
- Assert.Equal (cm.MenuBar, Application.MouseGrabView);
- Assert.False (menu.IsMenuOpen);
- Assert.False (menu.NewMouseEvent (new MouseEventArgs { Position = new (1, 0), Flags = MouseFlags.ReportMousePosition, View = menu }));
- Assert.True (ContextMenu.IsShow);
- Assert.Equal (cm.MenuBar, Application.MouseGrabView);
- Assert.False (menu.IsMenuOpen);
- Assert.True (menu.NewMouseEvent (new MouseEventArgs { Position = new (1, 0), Flags = MouseFlags.Button1Clicked, View = menu }));
- Assert.False (ContextMenu.IsShow);
- Assert.Equal (menu, Application.MouseGrabView);
- Assert.True (menu.IsMenuOpen);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Draw_A_ContextMenu_Over_A_Borderless_Top ()
- {
- ((FakeDriver)Application.Driver!).SetBufferSize (20, 15);
- Assert.Equal (new Rectangle (0, 0, 20, 15), Application.Driver?.Clip);
- TestHelpers.AssertDriverContentsWithFrameAre ("", output);
- var top = new Toplevel { X = 2, Y = 2, Width = 15, Height = 4 };
- top.Add (new TextField { X = Pos.Center (), Width = 10, Text = "Test" });
- RunState rs = Application.Begin (top);
- Assert.Equal (new Rectangle (2, 2, 15, 4), top.Frame);
- Assert.Equal (top, Application.Top);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- Test",
- output
- );
- Application.RaiseMouseEvent (new MouseEventArgs { ScreenPosition = new (8, 2), Flags = MouseFlags.Button3Clicked });
- var firstIteration = false;
- Application.RunIteration (ref rs, firstIteration);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- Test
- ┌───────────────────
- │ Select All Ctrl+
- │ Delete All Ctrl+
- │ Copy Ctrl+
- │ Cut Ctrl+
- │ Paste Ctrl+
- │ Undo Ctrl+
- │ Redo Ctrl+
- └───────────────────",
- output
- );
- Application.End (rs);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Draw_A_ContextMenu_Over_A_Dialog ()
- {
- Toplevel top = new ();
- var win = new Window ();
- top.Add (win);
- RunState rsTop = Application.Begin (top);
- ((FakeDriver)Application.Driver!).SetBufferSize (20, 15);
- Assert.Equal (new Rectangle (0, 0, 20, 15), win.Frame);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌──────────────────┐
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- └──────────────────┘",
- output
- );
- // Don't use Dialog here as it has more layout logic. Use Window instead.
- var testWindow = new Window { X = 2, Y = 2, Width = 15, Height = 4 };
- testWindow.Add (new TextField { X = Pos.Center (), Width = 10, Text = "Test" });
- RunState rsDialog = Application.Begin (testWindow);
- Assert.Equal (new Rectangle (2, 2, 15, 4), testWindow.Frame);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌──────────────────┐
- │ │
- │ ┌─────────────┐ │
- │ │ Test │ │
- │ │ │ │
- │ └─────────────┘ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- └──────────────────┘",
- output
- );
- Application.RaiseMouseEvent (new MouseEventArgs { ScreenPosition = new (9, 3), Flags = MouseFlags.Button3Clicked });
- Application.RunIteration (ref rsDialog);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌──────────────────┐
- │ │
- │ ┌─────────────┐ │
- │ │ Test │ │
- ┌───────────────────
- │ Select All Ctrl+
- │ Delete All Ctrl+
- │ Copy Ctrl+
- │ Cut Ctrl+
- │ Paste Ctrl+
- │ Undo Ctrl+
- │ Redo Ctrl+
- └───────────────────
- │ │
- └──────────────────┘",
- output
- );
- Application.End (rsDialog);
- Application.End (rsTop);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Draw_A_ContextMenu_Over_A_Top_Dialog ()
- {
- ((FakeDriver)Application.Driver!).SetBufferSize (20, 15);
- Assert.Equal (new Rectangle (0, 0, 20, 15), Application.Driver?.Clip);
- TestHelpers.AssertDriverContentsWithFrameAre ("", output);
- // Don't use Dialog here as it has more layout logic. Use Window instead.
- var dialog = new Window { X = 2, Y = 2, Width = 15, Height = 4 };
- dialog.Add (new TextField { X = Pos.Center (), Width = 10, Text = "Test" });
- RunState rs = Application.Begin (dialog);
- Assert.Equal (new Rectangle (2, 2, 15, 4), dialog.Frame);
- Assert.Equal (dialog, Application.Top);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌─────────────┐
- │ Test │
- │ │
- └─────────────┘",
- output
- );
- Application.RaiseMouseEvent (new MouseEventArgs { ScreenPosition = new (9, 3), Flags = MouseFlags.Button3Clicked });
- var firstIteration = false;
- Application.RunIteration (ref rs, firstIteration);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌─────────────┐
- │ Test │
- ┌───────────────────
- │ Select All Ctrl+
- │ Delete All Ctrl+
- │ Copy Ctrl+
- │ Cut Ctrl+
- │ Paste Ctrl+
- │ Undo Ctrl+
- │ Redo Ctrl+
- └───────────────────",
- output
- );
- Application.End (rs);
- dialog.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void ForceMinimumPosToZero_True_False ()
- {
- var cm = new ContextMenu
- {
- Position = new Point (-1, -2)
- };
- var menuItems = new MenuBarItem (
- [
- new MenuItem ("One", "", null),
- new MenuItem ("Two", "", null)
- ]
- );
- Assert.Equal (new Point (-1, -2), cm.Position);
- Toplevel top = new ();
- Application.Begin (top);
- cm.Show (menuItems);
- Assert.Equal (new Point (-1, -2), cm.Position);
- Application.Refresh ();
- var expected = @"
- ┌──────┐
- │ One │
- │ Two │
- └──────┘
- ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rectangle (0, 1, 8, 4), pos);
- cm.ForceMinimumPosToZero = false;
- cm.Show (menuItems);
- Assert.Equal (new Point (-1, -2), cm.Position);
- Application.Refresh ();
- expected = @"
- One │
- Two │
- ──────┘
- ";
- pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rectangle (1, 0, 7, 3), pos);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Hide_Is_Invoke_At_Container_Closing ()
- {
- var cm = new ContextMenu
- {
- Position = new Point (80, 25)
- };
- var menuItems = new MenuBarItem (
- [
- new MenuItem ("One", "", null),
- new MenuItem ("Two", "", null)
- ]
- );
- Toplevel top = new ();
- Application.Begin (top);
- top.Running = true;
- Assert.False (ContextMenu.IsShow);
- cm.Show (menuItems);
- Assert.True (ContextMenu.IsShow);
- top.RequestStop ();
- Assert.False (ContextMenu.IsShow);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Key_Open_And_Close_The_ContextMenu ()
- {
- var tf = new TextField ();
- var top = new Toplevel ();
- top.Add (tf);
- Application.Begin (top);
- Assert.True (Application.RaiseKeyDownEvent (ContextMenu.DefaultKey));
- Assert.True (tf.ContextMenu.MenuBar!.IsMenuOpen);
- Assert.True (Application.RaiseKeyDownEvent (ContextMenu.DefaultKey));
- // The last context menu bar opened is always preserved
- Assert.NotNull (tf.ContextMenu.MenuBar);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void KeyChanged_Event ()
- {
- var oldKey = Key.Empty;
- var cm = new ContextMenu ();
- cm.KeyChanged += (s, e) => oldKey = e.OldKey;
- cm.Key = Key.Space.WithCtrl;
- Assert.Equal (Key.Space.WithCtrl, cm.Key);
- Assert.Equal (ContextMenu.DefaultKey, oldKey);
- }
- [Fact]
- [AutoInitShutdown]
- public void MenuItens_Changing ()
- {
- var cm = new ContextMenu
- {
- Position = new Point (10, 5)
- };
- var menuItems = new MenuBarItem (
- [
- new MenuItem ("One", "", null),
- new MenuItem ("Two", "", null)
- ]
- );
- Toplevel top = new ();
- Application.Begin (top);
- cm.Show (menuItems);
- Application.Refresh ();
- var expected = @"
- ┌──────┐
- │ One │
- │ Two │
- └──────┘
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- menuItems = new MenuBarItem (
- [
- new MenuItem ("First", "", null),
- new MenuItem ("Second", "", null),
- new MenuItem ("Third", "", null)
- ]
- );
- cm.Show (menuItems);
- Application.Refresh ();
- expected = @"
- ┌─────────┐
- │ First │
- │ Second │
- │ Third │
- └─────────┘
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Menus_And_SubMenus_Always_Try_To_Be_On_Screen ()
- {
- var cm = new ContextMenu
- {
- Position = new Point (-1, -2)
- };
- var menuItems = new MenuBarItem (
- [
- new MenuItem ("One", "", null),
- new MenuItem ("Two", "", null),
- new MenuItem ("Three", "", null),
- new MenuBarItem (
- "Four",
- [
- new MenuItem ("SubMenu1", "", null),
- new MenuItem ("SubMenu2", "", null),
- new MenuItem ("SubMenu3", "", null),
- new MenuItem ("SubMenu4", "", null),
- new MenuItem ("SubMenu5", "", null),
- new MenuItem ("SubMenu6", "", null),
- new MenuItem ("SubMenu7", "", null)
- ]
- ),
- new MenuItem ("Five", "", null),
- new MenuItem ("Six", "", null)
- ]
- );
- Assert.Equal (new Point (-1, -2), cm.Position);
- Toplevel top = new ();
- RunState rs = Application.Begin (top);
- cm.Show (menuItems);
- Application.RunIteration (ref rs);
- Assert.Equal (new Point (-1, -2), cm.Position);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- │ One │
- │ Two │
- │ Three │
- │ Four ►│
- │ Five │
- │ Six │
- └────────┘
- ",
- output
- );
- Assert.True (
- top.Subviews [0]
- .NewMouseEvent (
- new MouseEventArgs { Position = new (0, 3), Flags = MouseFlags.ReportMousePosition, View = top.Subviews [0] }
- )
- );
- Application.RunIteration (ref rs);
- Assert.Equal (new Point (-1, -2), cm.Position);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- │ One │
- │ Two │
- │ Three │
- │ Four ►│┌───────────┐
- │ Five ││ SubMenu1 │
- │ Six ││ SubMenu2 │
- └────────┘│ SubMenu3 │
- │ SubMenu4 │
- │ SubMenu5 │
- │ SubMenu6 │
- │ SubMenu7 │
- └───────────┘
- ",
- output
- );
- ((FakeDriver)Application.Driver!).SetBufferSize (40, 20);
- cm.Position = new Point (41, -2);
- cm.Show (menuItems);
- Application.RunIteration (ref rs);
- Assert.Equal (new Point (41, -2), cm.Position);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- │ One │
- │ Two │
- │ Three │
- │ Four ►│
- │ Five │
- │ Six │
- └────────┘
- ",
- output
- );
- Assert.True (
- top.Subviews [0]
- .NewMouseEvent (
- new MouseEventArgs { Position = new (30, 3), Flags = MouseFlags.ReportMousePosition, View = top.Subviews [0] }
- )
- );
- Application.RunIteration (ref rs);
- Assert.Equal (new Point (41, -2), cm.Position);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- │ One │
- │ Two │
- │ Three │
- ┌───────────┐│ Four ►│
- │ SubMenu1 ││ Five │
- │ SubMenu2 ││ Six │
- │ SubMenu3 │└────────┘
- │ SubMenu4 │
- │ SubMenu5 │
- │ SubMenu6 │
- │ SubMenu7 │
- └───────────┘
- ",
- output
- );
- cm.Position = new Point (41, 9);
- cm.Show (menuItems);
- Application.RunIteration (ref rs);
- Assert.Equal (new Point (41, 9), cm.Position);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- │ One │
- │ Two │
- │ Three │
- │ Four ►│
- │ Five │
- │ Six │
- └────────┘
- ",
- output
- );
- Assert.True (
- top.Subviews [0]
- .NewMouseEvent (
- new MouseEventArgs { Position = new (30, 3), Flags = MouseFlags.ReportMousePosition, View = top.Subviews [0] }
- )
- );
- Application.RunIteration (ref rs);
- Assert.Equal (new Point (41, 9), cm.Position);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- ┌───────────┐│ One │
- │ SubMenu1 ││ Two │
- │ SubMenu2 ││ Three │
- │ SubMenu3 ││ Four ►│
- │ SubMenu4 ││ Five │
- │ SubMenu5 ││ Six │
- │ SubMenu6 │└────────┘
- │ SubMenu7 │
- └───────────┘
- ",
- output
- );
- cm.Position = new Point (41, 22);
- cm.Show (menuItems);
- Application.RunIteration (ref rs);
- Assert.Equal (new Point (41, 22), cm.Position);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- │ One │
- │ Two │
- │ Three │
- │ Four ►│
- │ Five │
- │ Six │
- └────────┘
- ",
- output
- );
- Assert.True (
- top.Subviews [0]
- .NewMouseEvent (
- new MouseEventArgs { Position = new (30, 3), Flags = MouseFlags.ReportMousePosition, View = top.Subviews [0] }
- )
- );
- Application.RunIteration (ref rs);
- Assert.Equal (new Point (41, 22), cm.Position);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌───────────┐
- │ SubMenu1 │┌────────┐
- │ SubMenu2 ││ One │
- │ SubMenu3 ││ Two │
- │ SubMenu4 ││ Three │
- │ SubMenu5 ││ Four ►│
- │ SubMenu6 ││ Five │
- │ SubMenu7 ││ Six │
- └───────────┘└────────┘
- ",
- output
- );
- ((FakeDriver)Application.Driver!).SetBufferSize (18, 8);
- cm.Position = new Point (19, 10);
- cm.Show (menuItems);
- Application.RunIteration (ref rs);
- Assert.Equal (new Point (19, 10), cm.Position);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- │ One │
- │ Two │
- │ Three │
- │ Four ►│
- │ Five │
- │ Six │
- └────────┘
- ",
- output
- );
- Assert.True (
- top.Subviews [0]
- .NewMouseEvent (
- new MouseEventArgs { Position = new (30, 3), Flags = MouseFlags.ReportMousePosition, View = top.Subviews [0] }
- )
- );
- Application.RunIteration (ref rs);
- Assert.Equal (new Point (19, 10), cm.Position);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌───────────┐────┐
- │ SubMenu1 │ │
- │ SubMenu2 │ │
- │ SubMenu3 │ee │
- │ SubMenu4 │r ►│
- │ SubMenu5 │e │
- │ SubMenu6 │ │
- │ SubMenu7 │────┘
- ",
- output
- );
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void MouseFlags_Changing ()
- {
- var lbl = new Label { Text = "Original" };
- var cm = new ContextMenu ();
- lbl.MouseClick += (s, e) =>
- {
- if (e.Flags == cm.MouseFlags)
- {
- lbl.Text = "Replaced";
- e.Handled = true;
- }
- };
- Toplevel top = new ();
- top.Add (lbl);
- Application.Begin (top);
- Assert.True (lbl.NewMouseEvent (new MouseEventArgs { Flags = cm.MouseFlags }));
- Assert.Equal ("Replaced", lbl.Text);
- lbl.Text = "Original";
- cm.MouseFlags = MouseFlags.Button2Clicked;
- Assert.True (lbl.NewMouseEvent (new MouseEventArgs { Flags = cm.MouseFlags }));
- Assert.Equal ("Replaced", lbl.Text);
- top.Dispose ();
- }
- [Fact]
- public void MouseFlagsChanged_Event ()
- {
- var oldMouseFlags = new MouseFlags ();
- var cm = new ContextMenu ();
- cm.MouseFlagsChanged += (s, e) => oldMouseFlags = e.OldValue;
- cm.MouseFlags = MouseFlags.Button2Clicked;
- Assert.Equal (MouseFlags.Button2Clicked, cm.MouseFlags);
- Assert.Equal (MouseFlags.Button3Clicked, oldMouseFlags);
- }
- [Fact]
- [AutoInitShutdown]
- public void Position_Changing ()
- {
- var cm = new ContextMenu
- {
- Position = new Point (10, 5)
- };
- var menuItems = new MenuBarItem (
- [
- new MenuItem ("One", "", null),
- new MenuItem ("Two", "", null)
- ]
- );
- Toplevel top = new ();
- Application.Begin (top);
- cm.Show (menuItems);
- Application.Refresh ();
- var expected = @"
- ┌──────┐
- │ One │
- │ Two │
- └──────┘
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- cm.Position = new Point (5, 10);
- cm.Show (menuItems);
- Application.Refresh ();
- expected = @"
- ┌──────┐
- │ One │
- │ Two │
- └──────┘
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void RequestStop_While_ContextMenu_Is_Open_Does_Not_Throws ()
- {
- ContextMenu cm = new ContextMenu
- {
- Position = new Point (10, 5)
- };
- var menuItems = new MenuBarItem (
- new MenuItem [] { new ("One", "", null), new ("Two", "", null) }
- );
- Toplevel top = new ();
- var isMenuAllClosed = false;
- MenuBarItem mi = null;
- int iterations = -1;
- Application.Iteration += (s, a) =>
- {
- iterations++;
- if (iterations == 0)
- {
- cm.Show (menuItems);
- Assert.True (ContextMenu.IsShow);
- mi = cm.MenuBar.Menus [0];
- mi.Action = () =>
- {
- Assert.True (ContextMenu.IsShow);
- var dialog1 = new Dialog () { Id = "dialog1" };
- Application.Run (dialog1);
- dialog1.Dispose ();
- Assert.False (ContextMenu.IsShow);
- Assert.True (isMenuAllClosed);
- };
- cm.MenuBar.MenuAllClosed += (_, _) => isMenuAllClosed = true;
- }
- else if (iterations == 1)
- {
- mi.Action ();
- }
- else if (iterations == 2)
- {
- Application.RequestStop ();
- }
- else if (iterations == 3)
- {
- isMenuAllClosed = false;
- cm.Show (menuItems);
- Assert.True (ContextMenu.IsShow);
- cm.MenuBar.MenuAllClosed += (_, _) => isMenuAllClosed = true;
- }
- else if (iterations == 4)
- {
- Exception exception = Record.Exception (() => Application.RequestStop ());
- Assert.Null (exception);
- }
- else
- {
- Application.RequestStop ();
- }
- };
- var isTopClosed = false;
- top.Closing += (_, _) =>
- {
- var dialog2 = new Dialog () { Id = "dialog2" };
- Application.Run (dialog2);
- dialog2.Dispose ();
- Assert.False (ContextMenu.IsShow);
- Assert.True (isMenuAllClosed);
- isTopClosed = true;
- };
- Application.Run (top);
- Assert.True (isTopClosed);
- Assert.False (ContextMenu.IsShow);
- Assert.True (isMenuAllClosed);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Show_Display_At_Zero_If_The_Toplevel_Height_Is_Less_Than_The_Menu_Height ()
- {
- ((FakeDriver)Application.Driver!).SetBufferSize (80, 3);
- var cm = new ContextMenu
- {
- Position = Point.Empty
- };
- var menuItems = new MenuBarItem (
- [
- new MenuItem ("One", "", null),
- new MenuItem ("Two", "", null)
- ]
- );
- Assert.Equal (Point.Empty, cm.Position);
- Toplevel top = new ();
- Application.Begin (top);
- cm.Show (menuItems);
- Assert.Equal (Point.Empty, cm.Position);
- Application.Refresh ();
- var expected = @"
- ┌──────┐
- │ One │
- │ Two │";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rectangle (0, 0, 8, 3), pos);
- cm.Hide ();
- Assert.Equal (Point.Empty, cm.Position);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Show_Display_At_Zero_If_The_Toplevel_Width_Is_Less_Than_The_Menu_Width ()
- {
- ((FakeDriver)Application.Driver!).SetBufferSize (5, 25);
- var cm = new ContextMenu
- {
- Position = Point.Empty
- };
- var menuItems = new MenuBarItem (
- [
- new MenuItem ("One", "", null),
- new MenuItem ("Two", "", null)
- ]
- );
- Assert.Equal (Point.Empty, cm.Position);
- Toplevel top = new ();
- Application.Begin (top);
- cm.Show (menuItems);
- Assert.Equal (Point.Empty, cm.Position);
- Application.Refresh ();
- var expected = @"
- ┌────
- │ One
- │ Two
- └────";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rectangle (0, 1, 5, 4), pos);
- cm.Hide ();
- Assert.Equal (Point.Empty, cm.Position);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Show_Display_Below_The_Bottom_Host_If_Has_Enough_Space ()
- {
- var view = new View
- {
- X = 10,
- Y = 5,
- Width = 10,
- Height = 1,
- Text = "View"
- };
- var cm = new ContextMenu
- {
- Host = view,
- Position = new Point (10, 5)
- };
- var menuItems = new MenuBarItem (
- [
- new MenuItem ("One", "", null),
- new MenuItem ("Two", "", null)
- ]
- );
- var top = new Toplevel ();
- top.Add (view);
- Application.Begin (top);
- Assert.Equal (new Point (10, 5), cm.Position);
- cm.Show (menuItems);
- top.Draw ();
- Assert.Equal (new Point (10, 5), cm.Position);
- var expected = @"
- View
- ┌──────┐
- │ One │
- │ Two │
- └──────┘
- ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rectangle (10, 5, 18, 5), pos);
- cm.Hide ();
- Assert.Equal (new Point (10, 5), cm.Position);
- cm.Host.X = 5;
- cm.Host.Y = 10;
- cm.Host.Height = 3;
- cm.Show (menuItems);
- Application.Top.Draw ();
- Assert.Equal (new Point (5, 12), cm.Position);
- expected = @"
- View
-
-
- ┌──────┐
- │ One │
- │ Two │
- └──────┘
- ";
- pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rectangle (5, 10, 13, 7), pos);
- cm.Hide ();
- Assert.Equal (new Point (5, 12), cm.Position);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Show_Ensures_Display_Inside_The_Container_But_Preserves_Position ()
- {
- var cm = new ContextMenu
- {
- Position = new Point (80, 25)
- };
- var menuItems = new MenuBarItem (
- [
- new MenuItem ("One", "", null),
- new MenuItem ("Two", "", null)
- ]
- );
- Assert.Equal (new Point (80, 25), cm.Position);
- Toplevel top = new ();
- Application.Begin (top);
- cm.Show (menuItems);
- Assert.Equal (new Point (80, 25), cm.Position);
- Application.Refresh ();
- var expected = @"
- ┌──────┐
- │ One │
- │ Two │
- └──────┘
- ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rectangle (72, 21, 80, 4), pos);
- cm.Hide ();
- Assert.Equal (new Point (80, 25), cm.Position);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Show_Ensures_Display_Inside_The_Container_Without_Overlap_The_Host ()
- {
- var view = new View
- {
- X = Pos.AnchorEnd (10),
- Y = Pos.AnchorEnd (1),
- Width = 10,
- Height = 1,
- Text = "View"
- };
- var cm = new ContextMenu
- {
- Host = view
- };
- var menuItems = new MenuBarItem (
- [
- new MenuItem ("One", "", null),
- new MenuItem ("Two", "", null)
- ]
- );
- var top = new Toplevel ();
- top.Add (view);
- Application.Begin (top);
- Assert.Equal (new Rectangle (70, 24, 10, 1), view.Frame);
- Assert.Equal (Point.Empty, cm.Position);
- cm.Show (menuItems);
- Assert.Equal (new Point (70, 24), cm.Position);
- top.Draw ();
- var expected = @"
- ┌──────┐
- │ One │
- │ Two │
- └──────┘
- View
- ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rectangle (70, 20, 78, 5), pos);
- cm.Hide ();
- Assert.Equal (new Point (70, 24), cm.Position);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Show_Hide_IsShow ()
- {
- ContextMenu cm = new ContextMenu
- {
- Position = new Point (10, 5)
- };
- var menuItems = new MenuBarItem (
- new MenuItem [] { new ("One", "", null), new ("Two", "", null) }
- );
- Toplevel top = new ();
- Application.Begin (top);
- cm.Show (menuItems);
- Assert.True (ContextMenu.IsShow);
- Application.Refresh ();
- var expected = @"
- ┌──────┐
- │ One │
- │ Two │
- └──────┘
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- cm.Hide ();
- Assert.False (ContextMenu.IsShow);
- Application.Refresh ();
- expected = "";
- TestHelpers.AssertDriverContentsAre (expected, output);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void UseSubMenusSingleFrame_True_By_Mouse ()
- {
- var cm = new ContextMenu
- {
- Position = new Point (5, 10),
- UseSubMenusSingleFrame = true
- };
- var menuItems = new MenuBarItem (
- "Numbers",
- [
- new MenuItem ("One", "", null),
- new MenuBarItem (
- "Two",
- [
- new MenuItem (
- "Sub-Menu 1",
- "",
- null
- ),
- new MenuItem ("Sub-Menu 2", "", null)
- ]
- ),
- new MenuItem ("Three", "", null)
- ]
- );
- Toplevel top = new ();
- RunState rs = Application.Begin (top);
- cm.Show (menuItems);
- Assert.Equal (new Rectangle (5, 11, 10, 5), Application.Top!.Subviews [0].Frame);
- Application.Refresh ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- │ One │
- │ Two ►│
- │ Three │
- └────────┘",
- output
- );
- // X=5 is the border and so need to use at least one more
- Application.RaiseMouseEvent (new MouseEventArgs { ScreenPosition = new (6, 13), Flags = MouseFlags.Button1Clicked });
- var firstIteration = false;
- Application.RunIteration (ref rs, firstIteration);
- Assert.Equal (new Rectangle (5, 11, 10, 5), Application.Top.Subviews [0].Frame);
- Assert.Equal (new Rectangle (5, 11, 15, 6), Application.Top.Subviews [1].Frame);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌─────────────┐
- │◄ Two │
- ├─────────────┤
- │ Sub-Menu 1 │
- │ Sub-Menu 2 │
- └─────────────┘",
- output
- );
- Application.RaiseMouseEvent (new MouseEventArgs { ScreenPosition = new (6, 12), Flags = MouseFlags.Button1Clicked });
- firstIteration = false;
- Application.RunIteration (ref rs, firstIteration);
- Assert.Equal (new Rectangle (5, 11, 10, 5), Application.Top.Subviews [0].Frame);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- │ One │
- │ Two ►│
- │ Three │
- └────────┘",
- output
- );
- Application.End (rs);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void UseSubMenusSingleFrame_False_By_Mouse ()
- {
- var cm = new ContextMenu
- {
- Position = new Point (5, 10)
- };
- var menuItems = new MenuBarItem (
- "Numbers",
- [
- new MenuItem ("One", "", null),
- new MenuBarItem (
- "Two",
- [
- new MenuItem (
- "Two-Menu 1",
- "",
- null
- ),
- new MenuItem ("Two-Menu 2", "", null)
- ]
- ),
- new MenuBarItem (
- "Three",
- [
- new MenuItem (
- "Three-Menu 1",
- "",
- null
- ),
- new MenuItem ("Three-Menu 2", "", null)
- ]
- )
- ]
- );
- Toplevel top = new ();
- RunState rs = Application.Begin (top);
- cm.Show (menuItems);
- Assert.Equal (new Rectangle (5, 11, 10, 5), Application.Top.Subviews [0].Frame);
- Application.Refresh ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- │ One │
- │ Two ►│
- │ Three ►│
- └────────┘",
- output
- );
- Application.RaiseMouseEvent (new MouseEventArgs { ScreenPosition = new (6, 13), Flags = MouseFlags.ReportMousePosition });
- var firstIteration = false;
- Application.RunIteration (ref rs, firstIteration);
- Assert.Equal (new Rectangle (5, 11, 10, 5), Application.Top.Subviews [0].Frame);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- │ One │
- │ Two ►│┌─────────────┐
- │ Three ►││ Two-Menu 1 │
- └────────┘│ Two-Menu 2 │
- └─────────────┘",
- output
- );
- Application.RaiseMouseEvent (new MouseEventArgs { ScreenPosition = new (6, 14), Flags = MouseFlags.ReportMousePosition });
- firstIteration = false;
- Application.RunIteration (ref rs, firstIteration);
- Assert.Equal (new Rectangle (5, 11, 10, 5), Application.Top.Subviews [0].Frame);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- │ One │
- │ Two ►│
- │ Three ►│┌───────────────┐
- └────────┘│ Three-Menu 1 │
- │ Three-Menu 2 │
- └───────────────┘",
- output
- );
- Application.RaiseMouseEvent (new MouseEventArgs { ScreenPosition = new (6, 13), Flags = MouseFlags.ReportMousePosition });
- firstIteration = false;
- Application.RunIteration (ref rs, firstIteration);
- Assert.Equal (new Rectangle (5, 11, 10, 5), Application.Top.Subviews [0].Frame);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- │ One │
- │ Two ►│┌─────────────┐
- │ Three ►││ Two-Menu 1 │
- └────────┘│ Two-Menu 2 │
- └─────────────┘",
- output
- );
- Application.End (rs);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Handling_TextField_With_Opened_ContextMenu_By_Mouse_HasFocus ()
- {
- var tf1 = new TextField { Width = 10, Text = "TextField 1" };
- var tf2 = new TextField { Y = 2, Width = 10, Text = "TextField 2" };
- var win = new Window ();
- win.Add (tf1, tf2);
- var rs = Application.Begin (win);
- Assert.True (tf1.HasFocus);
- Assert.False (tf2.HasFocus);
- Assert.Equal (4, win.Subviews.Count); // TF & TV add autocomplete popup's to their superviews.
- Assert.Empty (Application._cachedViewsUnderMouse);
- // Right click on tf2 to open context menu
- Application.RaiseMouseEvent (new () { ScreenPosition = new (1, 3), Flags = MouseFlags.Button3Clicked });
- Assert.False (tf1.HasFocus);
- Assert.False (tf2.HasFocus);
- Assert.Equal (5, win.Subviews.Count);
- Assert.True (tf2.ContextMenu.MenuBar.IsMenuOpen);
- Assert.True (win.Focused is Menu);
- Assert.True (Application.MouseGrabView is MenuBar);
- Assert.Equal (tf2, Application._cachedViewsUnderMouse.LastOrDefault ());
- // Click on tf1 to focus it, which cause context menu being closed
- Application.RaiseMouseEvent (new () { ScreenPosition = new (1, 1), Flags = MouseFlags.Button1Clicked });
- Assert.True (tf1.HasFocus);
- Assert.False (tf2.HasFocus);
- Assert.Equal (4, win.Subviews.Count);
- // The last context menu bar opened is always preserved
- Assert.NotNull (tf2.ContextMenu.MenuBar);
- Assert.Equal (win.Focused, tf1);
- Assert.Null (Application.MouseGrabView);
- Assert.Equal (tf1, Application._cachedViewsUnderMouse.LastOrDefault ());
- // Click on tf2 to focus it
- Application.RaiseMouseEvent (new () { ScreenPosition = new (1, 3), Flags = MouseFlags.Button1Clicked });
- Assert.False (tf1.HasFocus);
- Assert.True (tf2.HasFocus);
- Assert.Equal (4, win.Subviews.Count);
- // The last context menu bar opened is always preserved
- Assert.NotNull (tf2.ContextMenu.MenuBar);
- Assert.Equal (win.Focused, tf2);
- Assert.Null (Application.MouseGrabView);
- Assert.Equal (tf2, Application._cachedViewsUnderMouse.LastOrDefault ());
- Application.End (rs);
- win.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Empty_Menus_Items_Children_Does_Not_Open_The_Menu ()
- {
- var cm = new ContextMenu ();
- Assert.Null (cm.MenuItems);
- var top = new Toplevel ();
- Application.Begin (top);
- cm.Show (cm.MenuItems);
- Assert.Null (cm.MenuBar);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void KeyBindings_Removed_On_Close_ContextMenu ()
- {
- var newFile = false;
- var renameFile = false;
- var deleteFile = false;
- var cm = new ContextMenu ();
- var menuItems = new MenuBarItem (
- [
- new MenuItem ("New File", string.Empty, New, null, null, Key.N.WithCtrl),
- new MenuItem ("Rename File", string.Empty, Rename, null, null, Key.R.WithCtrl),
- new MenuItem ("Delete File", string.Empty, Delete, null, null, Key.D.WithCtrl)
- ]
- );
- var top = new Toplevel ();
- Application.Begin (top);
- Assert.Null (cm.MenuBar);
- Assert.False (Application.RaiseKeyDownEvent (Key.N.WithCtrl));
- Assert.False (Application.RaiseKeyDownEvent (Key.R.WithCtrl));
- Assert.False (Application.RaiseKeyDownEvent (Key.D.WithCtrl));
- Assert.False (newFile);
- Assert.False (renameFile);
- Assert.False (deleteFile);
- cm.Show (menuItems);
- Assert.True (cm.MenuBar!.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
- Assert.True (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithCtrl));
- Assert.True (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.D.WithCtrl));
- Assert.True (Application.RaiseKeyDownEvent (Key.N.WithCtrl));
- Application.MainLoop!.RunIteration ();
- Assert.True (newFile);
- Assert.False (cm.MenuBar!.IsMenuOpen);
- cm.Show (menuItems);
- Assert.True (Application.RaiseKeyDownEvent (Key.R.WithCtrl));
- Application.MainLoop!.RunIteration ();
- Assert.True (renameFile);
- Assert.False (cm.MenuBar.IsMenuOpen);
- cm.Show (menuItems);
- Assert.True (Application.RaiseKeyDownEvent (Key.D.WithCtrl));
- Application.MainLoop!.RunIteration ();
- Assert.True (deleteFile);
- Assert.False (cm.MenuBar.IsMenuOpen);
- Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
- Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithCtrl));
- Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.D.WithCtrl));
- newFile = false;
- renameFile = false;
- deleteFile = false;
- Assert.False (Application.RaiseKeyDownEvent (Key.N.WithCtrl));
- Assert.False (Application.RaiseKeyDownEvent (Key.R.WithCtrl));
- Assert.False (Application.RaiseKeyDownEvent (Key.D.WithCtrl));
- Assert.False (newFile);
- Assert.False (renameFile);
- Assert.False (deleteFile);
- top.Dispose ();
- void New () { newFile = true; }
- void Rename () { renameFile = true; }
- void Delete () { deleteFile = true; }
- }
- [Fact]
- [AutoInitShutdown]
- public void KeyBindings_With_ContextMenu_And_MenuBar ()
- {
- var newFile = false;
- var renameFile = false;
- var menuBar = new MenuBar
- {
- Menus =
- [
- new (
- "File",
- new MenuItem []
- {
- new ("New", string.Empty, New, null, null, Key.N.WithCtrl)
- })
- ]
- };
- var cm = new ContextMenu ();
- var menuItems = new MenuBarItem (
- [
- new ("Rename File", string.Empty, Rename, null, null, Key.R.WithCtrl),
- ]
- );
- var top = new Toplevel ();
- top.Add (menuBar);
- Application.Begin (top);
- Assert.True (menuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
- Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithCtrl));
- Assert.Null (cm.MenuBar);
- Assert.True (Application.RaiseKeyDownEvent (Key.N.WithCtrl));
- Assert.False (Application.RaiseKeyDownEvent (Key.R.WithCtrl));
- Application.MainLoop!.RunIteration ();
- Assert.True (newFile);
- Assert.False (renameFile);
- newFile = false;
- cm.Show (menuItems);
- Assert.True (menuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
- Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithCtrl));
- Assert.False (cm.MenuBar!.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
- Assert.True (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithCtrl));
- Assert.True (cm.MenuBar.IsMenuOpen);
- Assert.True (Application.RaiseKeyDownEvent (Key.N.WithCtrl));
- Application.MainLoop!.RunIteration ();
- Assert.True (newFile);
- Assert.False (cm.MenuBar!.IsMenuOpen);
- cm.Show (menuItems);
- Assert.True (Application.RaiseKeyDownEvent (Key.R.WithCtrl));
- Application.MainLoop!.RunIteration ();
- Assert.True (renameFile);
- Assert.False (cm.MenuBar.IsMenuOpen);
- Assert.True (menuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
- Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithCtrl));
- Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
- Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithCtrl));
- newFile = false;
- renameFile = false;
- Assert.True (Application.RaiseKeyDownEvent (Key.N.WithCtrl));
- Assert.False (Application.RaiseKeyDownEvent (Key.R.WithCtrl));
- Application.MainLoop!.RunIteration ();
- Assert.True (newFile);
- Assert.False (renameFile);
- top.Dispose ();
- void New () { newFile = true; }
- void Rename () { renameFile = true; }
- }
- [Fact]
- [AutoInitShutdown]
- public void KeyBindings_With_Same_Shortcut_ContextMenu_And_MenuBar ()
- {
- var newMenuBar = false;
- var newContextMenu = false;
- var menuBar = new MenuBar
- {
- Menus =
- [
- new (
- "File",
- new MenuItem []
- {
- new ("New", string.Empty, NewMenuBar, null, null, Key.N.WithCtrl)
- })
- ]
- };
- var cm = new ContextMenu ();
- var menuItems = new MenuBarItem (
- [
- new ("New File", string.Empty, NewContextMenu, null, null, Key.N.WithCtrl),
- ]
- );
- var top = new Toplevel ();
- top.Add (menuBar);
- Application.Begin (top);
- Assert.True (menuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
- Assert.Null (cm.MenuBar);
- Assert.True (Application.RaiseKeyDownEvent (Key.N.WithCtrl));
- Application.MainLoop!.RunIteration ();
- Assert.True (newMenuBar);
- Assert.False (newContextMenu);
- newMenuBar = false;
- cm.Show (menuItems);
- Assert.True (menuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
- Assert.True (cm.MenuBar!.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
- Assert.True (cm.MenuBar.IsMenuOpen);
- Assert.True (Application.RaiseKeyDownEvent (Key.N.WithCtrl));
- Application.MainLoop!.RunIteration ();
- Assert.False (newMenuBar);
- // The most focused shortcut is executed
- Assert.True (newContextMenu);
- Assert.False (cm.MenuBar!.IsMenuOpen);
- Assert.True (menuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
- Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithCtrl));
- newMenuBar = false;
- newContextMenu = false;
- Assert.True (Application.RaiseKeyDownEvent (Key.N.WithCtrl));
- Application.MainLoop!.RunIteration ();
- Assert.True (newMenuBar);
- Assert.False (newContextMenu);
- top.Dispose ();
- void NewMenuBar () { newMenuBar = true; }
- void NewContextMenu () { newContextMenu = true; }
- }
- [Fact]
- [AutoInitShutdown]
- public void HotKeys_Removed_On_Close_ContextMenu ()
- {
- var newFile = false;
- var renameFile = false;
- var deleteFile = false;
- var cm = new ContextMenu ();
- var menuItems = new MenuBarItem (
- [
- new ("_New File", string.Empty, New, null, null),
- new ("_Rename File", string.Empty, Rename, null, null),
- new ("_Delete File", string.Empty, Delete, null, null)
- ]
- );
- var top = new Toplevel ();
- Application.Begin (top);
- Assert.Null (cm.MenuBar);
- Assert.False (Application.RaiseKeyDownEvent (Key.N.WithAlt));
- Assert.False (Application.RaiseKeyDownEvent (Key.R.WithAlt));
- Assert.False (Application.RaiseKeyDownEvent (Key.D.WithAlt));
- Assert.False (newFile);
- Assert.False (renameFile);
- Assert.False (deleteFile);
- cm.Show (menuItems);
- Assert.True (cm.MenuBar!.IsMenuOpen);
- Assert.False (cm.MenuBar!.KeyBindings.Bindings.ContainsKey (Key.N.WithAlt));
- Assert.False (cm.MenuBar!.KeyBindings.Bindings.ContainsKey (Key.N.NoShift));
- Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithAlt));
- Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.NoShift));
- Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.D.WithAlt));
- Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.D.NoShift));
- Assert.Single (Application.Top!.Subviews);
- View [] menus = Application.Top!.Subviews.Where (v => v is Menu m && m.Host == cm.MenuBar).ToArray ();
- Assert.True (menus [0].KeyBindings.Bindings.ContainsKey (Key.N.WithAlt));
- Assert.True (menus [0].KeyBindings.Bindings.ContainsKey (Key.N.NoShift));
- Assert.True (menus [0].KeyBindings.Bindings.ContainsKey (Key.R.WithAlt));
- Assert.True (menus [0].KeyBindings.Bindings.ContainsKey (Key.R.NoShift));
- Assert.True (menus [0].KeyBindings.Bindings.ContainsKey (Key.D.WithAlt));
- Assert.True (menus [0].KeyBindings.Bindings.ContainsKey (Key.D.NoShift));
- Assert.True (Application.RaiseKeyDownEvent (Key.N.WithAlt));
- Assert.False (cm.MenuBar!.IsMenuOpen);
- Application.MainLoop!.RunIteration ();
- Assert.True (newFile);
- cm.Show (menuItems);
- Assert.True (Application.RaiseKeyDownEvent (Key.R.WithAlt));
- Assert.False (cm.MenuBar.IsMenuOpen);
- Application.MainLoop!.RunIteration ();
- Assert.True (renameFile);
- cm.Show (menuItems);
- Assert.True (Application.RaiseKeyDownEvent (Key.D.WithAlt));
- Assert.False (cm.MenuBar.IsMenuOpen);
- Application.MainLoop!.RunIteration ();
- Assert.True (deleteFile);
- Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithAlt));
- Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.N.NoShift));
- Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithAlt));
- Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.NoShift));
- Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.D.WithAlt));
- Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.D.NoShift));
- newFile = false;
- renameFile = false;
- deleteFile = false;
- Assert.False (Application.RaiseKeyDownEvent (Key.N.WithAlt));
- Assert.False (Application.RaiseKeyDownEvent (Key.R.WithAlt));
- Assert.False (Application.RaiseKeyDownEvent (Key.D.WithAlt));
- Assert.False (newFile);
- Assert.False (renameFile);
- Assert.False (deleteFile);
- top.Dispose ();
- void New () { newFile = true; }
- void Rename () { renameFile = true; }
- void Delete () { deleteFile = true; }
- }
- [Fact]
- [AutoInitShutdown]
- public void HotKeys_With_ContextMenu_And_MenuBar ()
- {
- var newFile = false;
- var renameFile = false;
- var menuBar = new MenuBar
- {
- Menus =
- [
- new (
- "_File",
- new MenuItem []
- {
- new ("_New", string.Empty, New)
- })
- ]
- };
- var cm = new ContextMenu ();
- var menuItems = new MenuBarItem (
- [
- new MenuBarItem (
- "_Edit",
- new MenuItem []
- {
- new ("_Rename File", string.Empty, Rename)
- }
- )
- ]
- );
- var top = new Toplevel ();
- top.Add (menuBar);
- Application.Begin (top);
- Assert.True (menuBar.KeyBindings.Bindings.ContainsKey (Key.F.WithAlt));
- Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithAlt));
- Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithAlt));
- View [] menus = Application.Top!.Subviews.Where (v => v is Menu m && m.Host == menuBar).ToArray ();
- Assert.Empty (menus);
- Assert.Null (cm.MenuBar);
- Assert.True (Application.RaiseKeyDownEvent (Key.F.WithAlt));
- Assert.True (menuBar.IsMenuOpen);
- Assert.Equal (2, Application.Top!.Subviews.Count);
- menus = Application.Top!.Subviews.Where (v => v is Menu m && m.Host == menuBar).ToArray ();
- Assert.True (menus [0].KeyBindings.Bindings.ContainsKey (Key.N.WithAlt));
- Assert.True (Application.RaiseKeyDownEvent (Key.N.WithAlt));
- Assert.False (menuBar.IsMenuOpen);
- Assert.False (Application.RaiseKeyDownEvent (Key.R.WithAlt));
- Application.MainLoop!.RunIteration ();
- Assert.True (newFile);
- Assert.False (renameFile);
- newFile = false;
- cm.Show (menuItems);
- Assert.True (menuBar.KeyBindings.Bindings.ContainsKey (Key.F.WithAlt));
- Assert.True (menuBar.KeyBindings.Bindings.ContainsKey (Key.F.NoShift));
- Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithAlt));
- Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.N.NoShift));
- Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.E.WithAlt));
- Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.E.NoShift));
- Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithAlt));
- Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.R.NoShift));
- Assert.True (cm.MenuBar!.IsMenuOpen);
- Assert.False (cm.MenuBar!.KeyBindings.Bindings.ContainsKey (Key.F.WithAlt));
- Assert.False (cm.MenuBar!.KeyBindings.Bindings.ContainsKey (Key.F.NoShift));
- Assert.False (cm.MenuBar!.KeyBindings.Bindings.ContainsKey (Key.N.WithAlt));
- Assert.False (cm.MenuBar!.KeyBindings.Bindings.ContainsKey (Key.N.NoShift));
- Assert.False (cm.MenuBar!.KeyBindings.Bindings.ContainsKey (Key.E.WithAlt));
- Assert.False (cm.MenuBar!.KeyBindings.Bindings.ContainsKey (Key.E.NoShift));
- Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithAlt));
- Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.NoShift));
- Assert.Equal (3, Application.Top!.Subviews.Count);
- menus = Application.Top!.Subviews.Where (v => v is Menu m && m.Host == cm.MenuBar).ToArray ();
- Assert.True (menus [0].KeyBindings.Bindings.ContainsKey (Key.E.WithAlt));
- Assert.True (menus [0].KeyBindings.Bindings.ContainsKey (Key.E.NoShift));
- Assert.True (menus [1].KeyBindings.Bindings.ContainsKey (Key.R.WithAlt));
- Assert.True (menus [1].KeyBindings.Bindings.ContainsKey (Key.R.NoShift));
- Assert.True (cm.MenuBar.IsMenuOpen);
- Assert.True (Application.RaiseKeyDownEvent (Key.F.WithAlt));
- Assert.False (cm.MenuBar.IsMenuOpen);
- Assert.True (Application.RaiseKeyDownEvent (Key.N.WithAlt));
- Application.MainLoop!.RunIteration ();
- Assert.True (newFile);
- cm.Show (menuItems);
- Assert.True (cm.MenuBar.IsMenuOpen);
- Assert.Equal (3, Application.Top!.Subviews.Count);
- menus = Application.Top!.Subviews.Where (v => v is Menu m && m.Host == cm.MenuBar).ToArray ();
- Assert.True (menus [0].KeyBindings.Bindings.ContainsKey (Key.E.WithAlt));
- Assert.True (menus [0].KeyBindings.Bindings.ContainsKey (Key.E.NoShift));
- Assert.False (menus [0].KeyBindings.Bindings.ContainsKey (Key.R.WithAlt));
- Assert.False (menus [0].KeyBindings.Bindings.ContainsKey (Key.R.NoShift));
- Assert.False (menus [1].KeyBindings.Bindings.ContainsKey (Key.E.WithAlt));
- Assert.False (menus [1].KeyBindings.Bindings.ContainsKey (Key.E.NoShift));
- Assert.True (menus [1].KeyBindings.Bindings.ContainsKey (Key.R.WithAlt));
- Assert.True (menus [1].KeyBindings.Bindings.ContainsKey (Key.R.NoShift));
- Assert.True (Application.RaiseKeyDownEvent (Key.E.NoShift));
- Assert.True (Application.RaiseKeyDownEvent (Key.R.WithAlt));
- Assert.False (cm.MenuBar.IsMenuOpen);
- Application.MainLoop!.RunIteration ();
- Assert.True (renameFile);
- Assert.Single (Application.Top!.Subviews);
- Assert.True (menuBar.KeyBindings.Bindings.ContainsKey (Key.F.WithAlt));
- Assert.True (menuBar.KeyBindings.Bindings.ContainsKey (Key.F.NoShift));
- Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.N.WithAlt));
- Assert.False (menuBar.KeyBindings.Bindings.ContainsKey (Key.N.NoShift));
- Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.E.WithAlt));
- Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.E.NoShift));
- Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.WithAlt));
- Assert.False (cm.MenuBar.KeyBindings.Bindings.ContainsKey (Key.R.NoShift));
- newFile = false;
- renameFile = false;
- Assert.True (Application.RaiseKeyDownEvent (Key.F.WithAlt));
- Assert.True (Application.RaiseKeyDownEvent (Key.N.WithAlt));
- Assert.False (Application.RaiseKeyDownEvent (Key.R.WithAlt));
- Application.MainLoop!.RunIteration ();
- Assert.True (newFile);
- Assert.False (renameFile);
- top.Dispose ();
- void New () { newFile = true; }
- void Rename () { renameFile = true; }
- }
- [Fact]
- [AutoInitShutdown]
- public void Opened_MenuBar_Is_Closed_When_Another_MenuBar_Is_Opening_Also_By_HotKey ()
- {
- var menuBar = new MenuBar
- {
- Menus =
- [
- new (
- "_File",
- new MenuItem []
- {
- new ("_New", string.Empty, null)
- })
- ]
- };
- var cm = new ContextMenu ();
- var menuItems = new MenuBarItem (
- [
- new MenuBarItem (
- "_Edit",
- new MenuItem []
- {
- new ("_Rename File", string.Empty, null)
- }
- )
- ]
- );
- var top = new Toplevel ();
- top.Add (menuBar);
- Application.Begin (top);
- Assert.True (Application.RaiseKeyDownEvent (Key.F.WithAlt));
- Assert.True (menuBar.IsMenuOpen);
- cm.Show (menuItems);
- Assert.False (menuBar.IsMenuOpen);
- Assert.True (cm.MenuBar!.IsMenuOpen);
- Assert.True (Application.RaiseKeyDownEvent (Key.F.WithAlt));
- Assert.True (menuBar.IsMenuOpen);
- Assert.False (cm.MenuBar!.IsMenuOpen);
- top.Dispose ();
- }
- }
|