Files
Terminal.Gui/Tests/UnitTests/View/Navigation/NavigationTests.cs
Thomas Nind 51dda7e69f Fixes #3947 Adds Fake driver and fixes fluent tests (iteration-zero) (#4225)
* Consider width2 chars that are not IsBmp

* Apply same fix in WindowsDriver

* Explicitly use type of local variable

* Revert changes to WindowsDriver

* Assume we are running in a terminal that supports true color by default unless user explicitly forces 16

* Switch to SetAttribute and WriteConsole instead of WriteConsoleOutput for 16 color mode

* Fix some cursor issues (WIP)

* Remove concept of 'dirty rows' from v2 as its never actually used

* Remove damageRegion as it does nothing

* Make string builder to console writing simpler

* Radically simplify Write method

* Simplify conditional logic

* Simplify restoring cursor position

* Reference local variable for console buffer

* Reduce calls to ConsoleWrite by accumulating till attribute changes

* When resizing v2 16 color mode on windows, recreate the back buffer to match its size

* Fixes for VTS enabled

* Fix _lastSize never being assigned

* Fixes VTS for Force16Colors

* Fixes force16Colors in VTS

* Fixes escape sequences always echoing in non-VTS

* Force Force16Colors in non-VTS. It have a bug in adding a newline in the last line

* WIP Add base class for NetOutput

* Abstract away how we change attribute

* WIP - Make WindowsOutput use base class

* WIP working to fix set cursor position

* Remove commented out code

* Fixes legacy output mode

* Fixes size with no alt buffer supported on VTS and size restore after maximized.

* Fix set cursor which also fixes the broken surrogate pairs

* Add force parameter

* Fixes an issue that only happens with Windows Terminal when paste surrogate pairs by press Ctrl+V

* In Windows escape sequences must be sent during the lifetime of the console which is created in input handle

* Ensure flush the input buffer before reset the console

* Flush input buffer before reset console in v2win

* Fixes issue in v2net not being refreshing the menu bar at start

* Only force layout and draw on size changed.

* Fix v2net issue not draw first line by forcing set cursor position

* Set _lastCursorPosition nullable and remove bool force from set cursor position

* Remove force parameter

* Add v2 version of fake driver attribute

* Make direct replacement and wire up window resizing events

* Update casts to use V2 fake driver instead

* Adjust interfaces to expose less internals

* Fix not raising iteration event in v2

* WIP investigate what it takes to do resize and redraw using TextAlignment_Centered as example

* Sketch adding component factory

* Create relevant fake component factories

* Add window size monitor into factory

* Fake size monitor injecting

* Add helper for faking console resize in AutoInitShutdown tests

* Fix size setting in FakeDriverV2

* Switch to new method

* Fix IsLegacy becoming false when using blank constructor

* Fix for Ready not being raised when showing same top twice also fixes garbage collection issue if running millions of top levels

* Fix tests

* Remove auto init

* Restore conditional compilation stuff

* Restore 'if running unit tests' logic

* Check only for the output being specific classes for the suppression

* Fix ShadowView blowing up with index out of bounds error

* Fix resize in fluent tests

* Fix for people using Iteration call directly

* Fix more calls to iteration to use
        AutoInitShutdownAttribute.RunIteration ();

* Add comment

* Remove assumption that Run with prior view not disposed should throw

* Fix timings in Dialog_Opened_From_Another_Dialog

* Fix Zero_Buttons_Works

* Standardize and fix Button_IsDefault_True_Return_His_Index_On_Accepting

* Fix iteration counts on MessageBoxTests

* Fix WizartTests and DrawTests_Ruler

* Implement SendKeys into ConsoleDriverFacade

* Fix SendKeys in console driver facade such that FileDialogTests works
Fix when Clip is null in popover

* Add missing dispose call to test

* Fix support for Esc in facade SendKeys

* Fix AutocompleteTests

* Fix various tests

* Replace LayoutAndDraw with run iteration

* Fix draw issues

* fix draw order

* Fix run iteration calls

* Fix unit tests

* Fix SendKeys in facade.

* Manipulate upper and lower cases.

* Add IsValidInput method to the interface.

* Fix SendKeys scenario

* Fixes surrogate pairs in the label

* Make tests more sensible - they are testing draw functionality.  Callbacks do not need to happen in Iteration method

* Fix tests and harden cleanup in AutoInitShutdownAttribute v2 lifecycle dispose

* Delete extra create input call

* Fix mocks and order of exceptions thrown in Run when things are not initialized

* Revert use of `MapConsoleKeyInfoToKeyCode`

* Ignore casing as it is not what test is really about

* Clear application top and top levels before each auto init shutdown test

* Fix for unstable tests

* Restore actually working SendKeys code

* option to pass logger in fluent ctor

* restore ToArray

* Fix SendKeys method and add extension to unit test

* Leverage the EscSeqUtils.MapConsoleKeyInfo method to avoid duplicate code

* Remove unnecessary hack

* Using only KeyCode for rKeys

* Recover modifier keys in surrogate pairs

* Reformat

* Remove iteration limit for benchmarking in v2

* remove iteration delay to identify bugs

* Remove nudge to unique key and make Then run on UI thread

* fix fluid assertions

* Ensure UI operations all happen on UI thread

* Add explicit error for WaitIteration during an invoke

* Remove timeout added for debug

* Catch failing asserts better

* Fix screenshot

* Fix null ref

* Fix race condition in processing input

* Test fixing

* Standardize asserts

* Remove calls to layout and draw, remove pointless lock and enable reading Cancelled from Dialog even if it is disposed

* fix bad merge

* Make logs access threadsafe

* add extra wait to remove race between iteration end and assert

* Code cleanup

* Remove test for crash on access Cancelled after dispose as this is no longer a restriction

* Change resize console to run on UI thread - fixing race condition with redrawing

* Restore original frame rate after test

* Restore nudge to unique key

* Code Cleanup

* Fix for cascading failures when an assert fails in a specific test

* fix for bad merge

* Address PR feedback

* Move classes to seperate files and add xmldoc

* xml doc warnings

* More xml comments docs

* Fix spelling

---------

Co-authored-by: BDisp <bd.bdisp@gmail.com>
2025-09-10 10:01:57 -06:00

368 lines
9.9 KiB
C#

using UnitTests;
using Xunit.Abstractions;
namespace Terminal.Gui.ViewTests;
public class NavigationTests (ITestOutputHelper output) : TestsAllViews
{
[Theory]
[MemberData (nameof (AllViewTypes))]
[SetupFakeDriver] // SetupFakeDriver resets app state; helps to avoid test pollution
public void AllViews_AtLeastOneNavKey_Advances (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;
}
Toplevel top = new ();
Application.Top = top;
Application.Navigation = new ();
View otherView = new ()
{
Id = "otherView",
CanFocus = true,
TabStop = view.TabStop == TabBehavior.NoStop ? TabBehavior.TabStop : view.TabStop
};
top.Add (view, otherView);
// 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.RaiseKeyDownEvent (key);
if (view.HasFocus)
{
// Try once more (HexView)
Application.RaiseKeyDownEvent (key);
}
break;
default:
Application.RaiseKeyDownEvent (Key.Tab);
break;
}
if (!view.HasFocus)
{
left = true;
output.WriteLine ($"{view.GetType ().Name} - {key} Left.");
break;
}
output.WriteLine ($"{view.GetType ().Name} - {key} did not Leave.");
}
top.Dispose ();
Application.ResetState ();
Assert.True (left);
}
[Theory]
[MemberData (nameof (AllViewTypes))]
[SetupFakeDriver] // SetupFakeDriver resets app state; helps to avoid test pollution
public void AllViews_HasFocus_Changed_Event (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;
}
Toplevel top = new ();
Application.Top = top;
Application.Navigation = new ();
View otherView = new ()
{
Id = "otherView",
CanFocus = true,
TabStop = view.TabStop == TabBehavior.NoStop ? TabBehavior.TabStop : view.TabStop
};
var hasFocusTrue = 0;
var hasFocusFalse = 0;
view.HasFocusChanged += (s, e) =>
{
if (e.NewValue)
{
hasFocusTrue++;
}
else
{
hasFocusFalse++;
}
};
top.Add (view, otherView);
Assert.False (view.HasFocus);
Assert.False (otherView.HasFocus);
// Ensure the view is Visible
view.Visible = true;
Application.Top.SetFocus ();
Assert.True (Application.Top!.HasFocus);
Assert.True (top.HasFocus);
// Start with the focus on our test view
Assert.True (view.HasFocus);
Assert.Equal (1, hasFocusTrue);
Assert.Equal (0, hasFocusFalse);
// 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.RaiseKeyDownEvent (Key.Tab))
{
if (view.HasFocus)
{
// Try another nav key (e.g. for TextView that eats Tab)
Application.RaiseKeyDownEvent (Key.CursorDown);
}
}
;
break;
case TabBehavior.TabGroup:
Application.RaiseKeyDownEvent (Key.F6);
break;
default:
throw new ArgumentOutOfRangeException ();
}
}
Assert.Equal (1, hasFocusTrue);
Assert.Equal (1, hasFocusFalse);
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.RaiseKeyDownEvent (Key.Tab);
break;
case TabBehavior.TabGroup:
if (!Application.RaiseKeyDownEvent (Key.F6))
{
view.SetFocus ();
}
break;
case null:
Application.RaiseKeyDownEvent (Key.Tab);
break;
default:
throw new ArgumentOutOfRangeException ();
}
Assert.Equal (2, hasFocusTrue);
Assert.Equal (1, hasFocusFalse);
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 = hasFocusTrue;
int leaveCount = hasFocusFalse;
top.Dispose ();
Assert.False (otherViewHasFocus);
Assert.True (viewHasFocus);
Assert.Equal (2, enterCount);
Assert.Equal (1, leaveCount);
Application.ResetState ();
}
[Theory]
[MemberData (nameof (AllViewTypes))]
[SetupFakeDriver] // SetupFakeDriver resets app state; helps to avoid test pollution
public void AllViews_Visible_False_No_HasFocus_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;
}
Toplevel top = new ();
Application.Top = top;
Application.Navigation = new ();
View otherView = new ()
{
CanFocus = true
};
view.Visible = false;
var hasFocusChangingCount = 0;
var hasFocusChangedCount = 0;
view.HasFocusChanging += (s, e) => hasFocusChangingCount++;
view.HasFocusChanged += (s, e) => hasFocusChangedCount++;
top.Add (view, otherView);
// Start with the focus on our test view
view.SetFocus ();
Assert.Equal (0, hasFocusChangingCount);
Assert.Equal (0, hasFocusChangedCount);
Application.RaiseKeyDownEvent (Key.Tab);
Assert.Equal (0, hasFocusChangingCount);
Assert.Equal (0, hasFocusChangedCount);
Application.RaiseKeyDownEvent (Key.F6);
Assert.Equal (0, hasFocusChangingCount);
Assert.Equal (0, hasFocusChangedCount);
top.Dispose ();
Application.ResetState ();
}
[Fact]
[AutoInitShutdown]
public void Navigation_With_Null_Focused_View ()
{
// Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
using 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 ();
}
[Fact]
[AutoInitShutdown]
public void Application_Begin_FocusesDeepest ()
{
var win1 = new Window { Id = "win1", Width = 10, Height = 1 };
var view1 = new View { Id = "view1", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
var win2 = new Window { Id = "win2", Y = 6, Width = 10, Height = 1 };
var view2 = new View { Id = "view2", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
win2.Add (view2);
win1.Add (view1, win2);
Application.Begin (win1);
Assert.True (win1.HasFocus);
Assert.True (view1.HasFocus);
Assert.False (win2.HasFocus);
Assert.False (view2.HasFocus);
win1.Dispose ();
}
}