123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733 |
- using Xunit.Abstractions;
- using static System.Net.Mime.MediaTypeNames;
- namespace Terminal.Gui.ViewTests;
- public class NavigationTests (ITestOutputHelper _output) : TestsAllViews
- {
- [Theory]
- [MemberData (nameof (AllViewTypes))]
- public void AllViews_AtLeastOneNavKey_Leaves (Type viewType)
- {
- View view = CreateInstanceIfNotGeneric (viewType);
- if (view == null)
- {
- _output.WriteLine ($"Ignoring {viewType} - It's a Generic");
- return;
- }
- if (!view.CanFocus)
- {
- _output.WriteLine ($"Ignoring {viewType} - It can't focus.");
- return;
- }
- Application.Init (new FakeDriver ());
- Toplevel top = new ();
- View otherView = new ()
- {
- Id = "otherView",
- CanFocus = true,
- TabStop = view.TabStop == TabBehavior.NoStop ? TabBehavior.TabStop : view.TabStop
- };
- top.Add (view, otherView);
- Application.Begin (top);
- // Start with the focus on our test view
- view.SetFocus ();
- Key [] navKeys = { Key.Tab, Key.Tab.WithShift, Key.CursorUp, Key.CursorDown, Key.CursorLeft, Key.CursorRight };
- if (view.TabStop == TabBehavior.TabGroup)
- {
- navKeys = new [] { Key.F6, Key.F6.WithShift };
- }
- var left = false;
- foreach (Key key in navKeys)
- {
- switch (view.TabStop)
- {
- case TabBehavior.TabStop:
- case TabBehavior.NoStop:
- case TabBehavior.TabGroup:
- Application.OnKeyDown (key);
- break;
- default:
- Application.OnKeyDown (Key.Tab);
- break;
- }
- if (!view.HasFocus)
- {
- left = true;
- _output.WriteLine ($"{view.GetType ().Name} - {key} Left.");
- view.SetFocus ();
- }
- else
- {
- _output.WriteLine ($"{view.GetType ().Name} - {key} did not Leave.");
- }
- }
- top.Dispose ();
- Application.Shutdown ();
- Assert.True (left);
- }
- [Theory]
- [MemberData (nameof (AllViewTypes))]
- public void AllViews_Enter_Leave_Events (Type viewType)
- {
- View view = CreateInstanceIfNotGeneric (viewType);
- if (view == null)
- {
- _output.WriteLine ($"Ignoring {viewType} - It's a Generic");
- return;
- }
- if (!view.CanFocus)
- {
- _output.WriteLine ($"Ignoring {viewType} - It can't focus.");
- return;
- }
- if (view is Toplevel && ((Toplevel)view).Modal)
- {
- _output.WriteLine ($"Ignoring {viewType} - It's a Modal Toplevel");
- return;
- }
- Application.Init (new FakeDriver ());
- Toplevel top = new ()
- {
- Height = 10,
- Width = 10
- };
- View otherView = new ()
- {
- Id = "otherView",
- X = 0, Y = 0,
- Height = 1,
- Width = 1,
- CanFocus = true,
- TabStop = view.TabStop == TabBehavior.NoStop ? TabBehavior.TabStop : view.TabStop
- };
- view.X = Pos.Right (otherView);
- view.Y = 0;
- view.Width = 10;
- view.Height = 1;
- var nEnter = 0;
- var nLeave = 0;
- view.HasFocusChanging += (s, e) => nEnter++;
- view.HasFocusChanged += (s, e) => nLeave++;
- top.Add (view, otherView);
- Assert.False (view.HasFocus);
- Assert.False (otherView.HasFocus);
- Application.Begin (top);
- Assert.True (Application.Current!.HasFocus);
- Assert.True (top.HasFocus);
- // Start with the focus on our test view
- Assert.True (view.HasFocus);
- Assert.Equal (1, nEnter);
- Assert.Equal (0, nLeave);
- // Use keyboard to navigate to next view (otherView).
- var tries = 0;
- while (view.HasFocus)
- {
- if (++tries > 10)
- {
- Assert.Fail ($"{view} is not leaving.");
- }
- switch (view.TabStop)
- {
- case null:
- case TabBehavior.NoStop:
- case TabBehavior.TabStop:
- if (Application.OnKeyDown (Key.Tab))
- {
- if (view.HasFocus)
- {
- // Try another nav key (e.g. for TextView that eats Tab)
- Application.OnKeyDown (Key.CursorDown);
- }
- };
- break;
- case TabBehavior.TabGroup:
- Application.OnKeyDown (Key.F6);
- break;
- default:
- throw new ArgumentOutOfRangeException ();
- }
- }
- Assert.Equal (1, nEnter);
- Assert.Equal (1, nLeave);
- Assert.False (view.HasFocus);
- Assert.True (otherView.HasFocus);
- // Now navigate back to our test view
- switch (view.TabStop)
- {
- case TabBehavior.NoStop:
- view.SetFocus ();
- break;
- case TabBehavior.TabStop:
- Application.OnKeyDown (Key.Tab);
- break;
- case TabBehavior.TabGroup:
- Application.OnKeyDown (Key.F6);
- break;
- case null:
- Application.OnKeyDown (Key.Tab);
- break;
- default:
- throw new ArgumentOutOfRangeException ();
- }
- Assert.Equal (2, nEnter);
- Assert.Equal (1, nLeave);
- Assert.True (view.HasFocus);
- Assert.False (otherView.HasFocus);
- // Cache state because Shutdown has side effects.
- // Also ensures other tests can continue running if there's a fail
- bool otherViewHasFocus = otherView.HasFocus;
- bool viewHasFocus = view.HasFocus;
- int enterCount = nEnter;
- int leaveCount = nLeave;
- top.Dispose ();
- Application.Shutdown ();
- Assert.False (otherViewHasFocus);
- Assert.True (viewHasFocus);
- Assert.Equal (2, enterCount);
- Assert.Equal (1, leaveCount);
- }
- [Theory]
- [MemberData (nameof (AllViewTypes))]
- public void AllViews_Enter_Leave_Events_Visible_False (Type viewType)
- {
- View view = CreateInstanceIfNotGeneric (viewType);
- if (view == null)
- {
- _output.WriteLine ($"Ignoring {viewType} - It's a Generic");
- return;
- }
- if (!view.CanFocus)
- {
- _output.WriteLine ($"Ignoring {viewType} - It can't focus.");
- return;
- }
- if (view is Toplevel && ((Toplevel)view).Modal)
- {
- _output.WriteLine ($"Ignoring {viewType} - It's a Modal Toplevel");
- return;
- }
- Application.Init (new FakeDriver ());
- Toplevel top = new ()
- {
- Height = 10,
- Width = 10
- };
- View otherView = new ()
- {
- X = 0, Y = 0,
- Height = 1,
- Width = 1,
- CanFocus = true
- };
- view.Visible = false;
- view.X = Pos.Right (otherView);
- view.Y = 0;
- view.Width = 10;
- view.Height = 1;
- var nEnter = 0;
- var nLeave = 0;
- view.HasFocusChanging += (s, e) => nEnter++;
- view.HasFocusChanged += (s, e) => nLeave++;
- top.Add (view, otherView);
- Application.Begin (top);
- // Start with the focus on our test view
- view.SetFocus ();
- Assert.Equal (0, nEnter);
- Assert.Equal (0, nLeave);
- // Use keyboard to navigate to next view (otherView).
- if (view is TextView)
- {
- Application.OnKeyDown (Key.F6);
- }
- else if (view is DatePicker)
- {
- for (var i = 0; i < 4; i++)
- {
- Application.OnKeyDown (Key.F6);
- }
- }
- else
- {
- Application.OnKeyDown (Key.Tab);
- }
- Assert.Equal (0, nEnter);
- Assert.Equal (0, nLeave);
- top.NewKeyDownEvent (Key.Tab);
- Assert.Equal (0, nEnter);
- Assert.Equal (0, nLeave);
- top.Dispose ();
- Application.Shutdown ();
- }
- [Fact]
- public void BringSubviewForward_Subviews_vs_TabIndexes ()
- {
- var r = new View ();
- var v1 = new View { CanFocus = true };
- var v2 = new View { CanFocus = true };
- var v3 = new View { CanFocus = true };
- r.Add (v1, v2, v3);
- r.BringSubviewForward (v1);
- Assert.True (r.Subviews.IndexOf (v1) == 1);
- Assert.True (r.Subviews.IndexOf (v2) == 0);
- Assert.True (r.Subviews.IndexOf (v3) == 2);
- Assert.True (r.TabIndexes.IndexOf (v1) == 0);
- Assert.True (r.TabIndexes.IndexOf (v2) == 1);
- Assert.True (r.TabIndexes.IndexOf (v3) == 2);
- r.Dispose ();
- }
- [Fact]
- public void BringSubviewToFront_Subviews_vs_TabIndexes ()
- {
- var r = new View ();
- var v1 = new View { CanFocus = true };
- var v2 = new View { CanFocus = true };
- var v3 = new View { CanFocus = true };
- r.Add (v1, v2, v3);
- r.BringSubviewToFront (v1);
- Assert.True (r.Subviews.IndexOf (v1) == 2);
- Assert.True (r.Subviews.IndexOf (v2) == 0);
- Assert.True (r.Subviews.IndexOf (v3) == 1);
- Assert.True (r.TabIndexes.IndexOf (v1) == 0);
- Assert.True (r.TabIndexes.IndexOf (v2) == 1);
- Assert.True (r.TabIndexes.IndexOf (v3) == 2);
- r.Dispose ();
- }
- // View.Focused & View.MostFocused tests
- // View.Focused - No subviews
- [Fact]
- [Trait ("BUGBUG", "Fix in Issue #3444")]
- public void Focused_NoSubviews ()
- {
- var view = new View ();
- Assert.Null (view.Focused);
- view.CanFocus = true;
- view.SetFocus ();
- Assert.True (view.HasFocus);
- Assert.Null (view.Focused); // BUGBUG: Should be view
- }
- [Fact]
- public void FocusNearestView_Ensure_Focus_Ordered ()
- {
- Application.Top = Application.Current = new Toplevel ();
- var win = new Window ();
- var winSubview = new View { CanFocus = true, Text = "WindowSubview" };
- win.Add (winSubview);
- Application.Current.Add (win);
- var frm = new FrameView ();
- var frmSubview = new View { CanFocus = true, Text = "FrameSubview" };
- frm.Add (frmSubview);
- Application.Current.Add (frm);
- Application.Current.SetFocus ();
- Assert.Equal (winSubview, Application.Current.MostFocused);
- Application.OnKeyDown (Key.Tab); // Move to the next TabStop. There is none. So we should stay.
- Assert.Equal (winSubview, Application.Current.MostFocused);
- Application.OnKeyDown (Key.F6);
- Assert.Equal (frmSubview, Application.Current.MostFocused);
- Application.OnKeyDown (Key.Tab);
- Assert.Equal (frmSubview, Application.Current.MostFocused);
- Application.OnKeyDown (Key.F6);
- Assert.Equal (winSubview, Application.Current.MostFocused);
- Application.OnKeyDown (Key.F6.WithShift);
- Assert.Equal (frmSubview, Application.Current.MostFocused);
- Application.OnKeyDown (Key.F6.WithShift);
- Assert.Equal (winSubview, Application.Current.MostFocused);
- Application.Current.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void FocusNext_Does_Not_Throws_If_A_View_Was_Removed_From_The_Collection ()
- {
- Toplevel top1 = new ();
- var view1 = new View { Id = "view1", Width = 10, Height = 5, CanFocus = true };
- var top2 = new Toplevel { Id = "top2", Y = 1, Width = 10, Height = 5 };
- var view2 = new View
- {
- Id = "view2",
- Y = 1,
- Width = 10,
- Height = 5,
- CanFocus = true
- };
- View view3 = null;
- var removed = false;
- view2.HasFocusChanging += (s, e) =>
- {
- if (!removed)
- {
- removed = true;
- view3 = new () { Id = "view3", Y = 1, Width = 10, Height = 5 };
- Application.Current.Add (view3);
- Application.Current.BringSubviewToFront (view3);
- Assert.False (view3.HasFocus);
- }
- };
- view2.HasFocusChanged += (s, e) =>
- {
- Application.Current.Remove (view3);
- view3.Dispose ();
- view3 = null;
- };
- top2.Add (view2);
- top1.Add (view1, top2);
- Application.Begin (top1);
- Assert.True (top1.HasFocus);
- Assert.True (view1.HasFocus);
- Assert.False (view2.HasFocus);
- Assert.False (removed);
- Assert.Null (view3);
- Assert.True (Application.OnKeyDown (Key.F6));
- Assert.True (top1.HasFocus);
- Assert.False (view1.HasFocus);
- Assert.True (view2.HasFocus);
- Assert.True (removed);
- Assert.NotNull (view3);
- Exception exception = Record.Exception (() => Application.OnKeyDown (Key.F6));
- Assert.Null (exception);
- Assert.True (removed);
- //Assert.Null (view3);
- top1.Dispose ();
- }
- [Fact]
- public void GetMostFocused_NoSubviews_Returns_Null ()
- {
- var view = new View ();
- Assert.Null (view.Focused);
- view.CanFocus = true;
- Assert.False (view.HasFocus);
- view.SetFocus ();
- Assert.True (view.HasFocus);
- Assert.Null (view.MostFocused);
- }
- [Fact]
- public void GetMostFocused_Returns_Most ()
- {
- var view = new View ()
- {
- Id = "view",
- CanFocus = true
- };
- var subview = new View ()
- {
- Id = "subview",
- CanFocus = true
- };
- view.Add (subview);
- view.SetFocus ();
- Assert.True (view.HasFocus);
- Assert.True (subview.HasFocus);
- Assert.Equal (subview, view.MostFocused);
- var subview2 = new View ()
- {
- Id = "subview2",
- CanFocus = true
- };
- view.Add (subview2);
- Assert.Equal (subview2, view.MostFocused);
- }
- // [Fact]
- // [AutoInitShutdown]
- // public void HotKey_Will_Invoke_KeyPressed_Only_For_The_MostFocused_With_Top_KeyPress_Event ()
- // {
- // var sbQuiting = false;
- // var tfQuiting = false;
- // var topQuiting = false;
- // var sb = new StatusBar (
- // new Shortcut []
- // {
- // new (
- // KeyCode.CtrlMask | KeyCode.Q,
- // "Quit",
- // () => sbQuiting = true
- // )
- // }
- // );
- // var tf = new TextField ();
- // tf.KeyDown += Tf_KeyPressed;
- // void Tf_KeyPressed (object sender, Key obj)
- // {
- // if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
- // {
- // obj.Handled = tfQuiting = true;
- // }
- // }
- // var win = new Window ();
- // win.Add (sb, tf);
- // Toplevel top = new ();
- // top.KeyDown += Top_KeyPress;
- // void Top_KeyPress (object sender, Key obj)
- // {
- // if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
- // {
- // obj.Handled = topQuiting = true;
- // }
- // }
- // top.Add (win);
- // Application.Begin (top);
- // Assert.False (sbQuiting);
- // Assert.False (tfQuiting);
- // Assert.False (topQuiting);
- // Application.Driver?.SendKeys ('Q', ConsoleKey.Q, false, false, true);
- // Assert.False (sbQuiting);
- // Assert.True (tfQuiting);
- // Assert.False (topQuiting);
- //#if BROKE_WITH_2927
- // tf.KeyPressed -= Tf_KeyPress;
- // tfQuiting = false;
- // Application.Driver?.SendKeys ('q', ConsoleKey.Q, false, false, true);
- // Application.MainLoop.RunIteration ();
- // Assert.True (sbQuiting);
- // Assert.False (tfQuiting);
- // Assert.False (topQuiting);
- // sb.RemoveItem (0);
- // sbQuiting = false;
- // Application.Driver?.SendKeys ('q', ConsoleKey.Q, false, false, true);
- // Application.MainLoop.RunIteration ();
- // Assert.False (sbQuiting);
- // Assert.False (tfQuiting);
- //// This test is now invalid because `win` is focused, so it will receive the keypress
- // Assert.True (topQuiting);
- //#endif
- // top.Dispose ();
- // }
- // [Fact]
- // [AutoInitShutdown]
- // public void HotKey_Will_Invoke_KeyPressed_Only_For_The_MostFocused_Without_Top_KeyPress_Event ()
- // {
- // var sbQuiting = false;
- // var tfQuiting = false;
- // var sb = new StatusBar (
- // new Shortcut []
- // {
- // new (
- // KeyCode.CtrlMask | KeyCode.Q,
- // "~^Q~ Quit",
- // () => sbQuiting = true
- // )
- // }
- // );
- // var tf = new TextField ();
- // tf.KeyDown += Tf_KeyPressed;
- // void Tf_KeyPressed (object sender, Key obj)
- // {
- // if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
- // {
- // obj.Handled = tfQuiting = true;
- // }
- // }
- // var win = new Window ();
- // win.Add (sb, tf);
- // Toplevel top = new ();
- // top.Add (win);
- // Application.Begin (top);
- // Assert.False (sbQuiting);
- // Assert.False (tfQuiting);
- // Application.Driver?.SendKeys ('Q', ConsoleKey.Q, false, false, true);
- // Assert.False (sbQuiting);
- // Assert.True (tfQuiting);
- // tf.KeyDown -= Tf_KeyPressed;
- // tfQuiting = false;
- // Application.Driver?.SendKeys ('Q', ConsoleKey.Q, false, false, true);
- // Application.MainLoop.RunIteration ();
- //#if BROKE_WITH_2927
- // Assert.True (sbQuiting);
- // Assert.False (tfQuiting);
- //#endif
- // top.Dispose ();
- // }
- [Fact]
- [SetupFakeDriver]
- public void Navigation_With_Null_Focused_View ()
- {
- // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
- Application.Init (new FakeDriver ());
- var top = new Toplevel ();
- top.Ready += (s, e) => { Assert.Null (top.Focused); };
- // Keyboard navigation with tab
- FakeConsole.MockKeyPresses.Push (new ('\t', ConsoleKey.Tab, false, false, false));
- Application.Iteration += (s, a) => Application.RequestStop ();
- Application.Run (top);
- top.Dispose ();
- Application.Shutdown ();
- }
- #if V2_NEW_FOCUS_IMPL // bogus test - Depends on auto setting of CanFocus
- [Fact]
- [AutoInitShutdown]
- public void Remove_Does_Not_Change_Focus ()
- {
- var top = new Toplevel ();
- Assert.True (top.CanFocus);
- Assert.False (top.HasFocus);
- var container = new View { Width = 10, Height = 10 };
- var leave = false;
- container.Leave += (s, e) => leave = true;
- Assert.False (container.CanFocus);
- var child = new View { Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
- container.Add (child);
- Assert.True (container.CanFocus);
- Assert.False (container.HasFocus);
- Assert.True (child.CanFocus);
- Assert.False (child.HasFocus);
- top.Add (container);
- Application.Begin (top);
- Assert.True (top.CanFocus);
- Assert.True (top.HasFocus);
- Assert.True (container.CanFocus);
- Assert.True (container.HasFocus);
- Assert.True (child.CanFocus);
- Assert.True (child.HasFocus);
- container.Remove (child);
- child.Dispose ();
- child = null;
- Assert.True (top.HasFocus);
- Assert.True (container.CanFocus);
- Assert.True (container.HasFocus);
- Assert.Null (child);
- Assert.False (leave);
- top.Dispose ();
- }
- #endif
- }
|