Files
Terminal.Gui/Tests/UnitTests/View/Navigation/CanFocusTests.cs
Tig 713efcc2f4 WIP: Fixed Parallel tests; non-Parallel still broken
Refactor application model usage tracking

Refactored `ApplicationModelUsage` into a public enum in the new `Terminal.Gui.App` namespace, making it accessible across the codebase. Replaced the private `_modelUsage` field in `ApplicationImpl` with a public static `ModelUsage` property to improve clarity and accessibility.

Renamed error message constants for consistency and updated methods like `SetInstance` and `MarkInstanceBasedModelUsed` to use the new `ModelUsage` property. Removed the private `ApplicationModelUsage` enum from `ApplicationImpl`.

Updated test cases to use `ApplicationImpl.Instance` instead of `Application.Create` to enforce the legacy static model. Skipped obsolete tests in `ApplicationForceDriverTests` and added null checks in `DriverAssert` and `SelectorBase` to handle edge cases.

Commented out an unused line in `WindowsOutput` and made general improvements to code readability, maintainability, and consistency.
2025-11-22 21:29:44 -07:00

132 lines
4.1 KiB
C#

using UnitTests;
namespace UnitTests.ViewTests;
public class CanFocusTests
{
// TODO: Figure out what this test is supposed to be testing
[Fact]
[AutoInitShutdown]
public void CanFocus_Faced_With_Container_Before_Run ()
{
using Toplevel t = new ();
var w = new Window ();
var f = new FrameView ();
var v = new View { CanFocus = true };
f.Add (v);
w.Add (f);
t.Add (w);
Assert.True (t.CanFocus);
Assert.True (w.CanFocus);
Assert.True (f.CanFocus);
Assert.True (v.CanFocus);
f.CanFocus = false;
Assert.False (f.CanFocus);
Assert.True (v.CanFocus);
v.CanFocus = false;
Assert.False (f.CanFocus);
Assert.False (v.CanFocus);
v.CanFocus = true;
Assert.False (f.CanFocus);
Assert.True (v.CanFocus);
Application.StopAfterFirstIteration = true;
Application.Run (t);
t.Dispose ();
Application.Shutdown ();
}
//[Fact]
//public void CanFocus_Set_Changes_TabIndex_And_TabStop ()
//{
// var r = new View ();
// var v1 = new View { Text = "1" };
// var v2 = new View { Text = "2" };
// var v3 = new View { Text = "3" };
// r.Add (v1, v2, v3);
// v2.CanFocus = true;
// Assert.Equal (r.TabIndexes.IndexOf (v2), v2.TabIndex);
// Assert.Equal (0, v2.TabIndex);
// Assert.Equal (TabBehavior.TabStop, v2.TabStop);
// v1.CanFocus = true;
// Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
// Assert.Equal (1, v1.TabIndex);
// Assert.Equal (TabBehavior.TabStop, v1.TabStop);
// v1.TabIndex = 2;
// Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
// Assert.Equal (1, v1.TabIndex);
// v3.CanFocus = true;
// Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
// Assert.Equal (1, v1.TabIndex);
// Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
// Assert.Equal (2, v3.TabIndex);
// Assert.Equal (TabBehavior.TabStop, v3.TabStop);
// v2.CanFocus = false;
// Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
// Assert.Equal (1, v1.TabIndex);
// Assert.Equal (TabBehavior.TabStop, v1.TabStop);
// Assert.Equal (r.TabIndexes.IndexOf (v2), v2.TabIndex); // TabIndex is not changed
// Assert.NotEqual (-1, v2.TabIndex);
// Assert.Equal (TabBehavior.TabStop, v2.TabStop); // TabStop is not changed
// Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
// Assert.Equal (2, v3.TabIndex);
// Assert.Equal (TabBehavior.TabStop, v3.TabStop);
// r.Dispose ();
//}
[Fact]
public void CanFocus_Set_True_Get_AdvanceFocus_Works ()
{
IApplication app = ApplicationImpl.Instance; // Force legacy
app.TopRunnable = new () { App = app };
Label label = new () { Text = "label" };
View view = new () { Text = "view", CanFocus = true };
app.TopRunnable.Add (label, view);
app.TopRunnable.SetFocus ();
Assert.Equal (view, app.Navigation!.GetFocused ());
Assert.False (label.CanFocus);
Assert.False (label.HasFocus);
Assert.True (view.CanFocus);
Assert.True (view.HasFocus);
Assert.False (app.Navigation.AdvanceFocus (NavigationDirection.Forward, null));
Assert.False (label.HasFocus);
Assert.True (view.HasFocus);
// Set label CanFocus to true
label.CanFocus = true;
Assert.False (label.HasFocus);
Assert.True (view.HasFocus);
// label can now be focused, so AdvanceFocus should move to it.
Assert.True (app.Navigation.AdvanceFocus (NavigationDirection.Forward, null));
Assert.True (label.HasFocus);
Assert.False (view.HasFocus);
// Move back to view
view.SetFocus ();
Assert.False (label.HasFocus);
Assert.True (view.HasFocus);
Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.Tab));
Assert.True (label.HasFocus);
Assert.False (view.HasFocus);
app.TopRunnable.Dispose ();
app.ResetState ();
}
}