Files
Terminal.Gui/Tests/UnitTests/View/Navigation/CanFocusTests.cs
Tig acb5979e6c Cleans up/Refactors View.Subviews (#3962)
* Subview clean up

* New Add/Remove event pattern

* Using Logging

* cleanup

* Subview -> SubView

* Test code cleanup. Killed many warnings.

* Fix tznind feedback

* Refactored AllViewTest helpers

* Moved keyboard tests to parallel

* Moved mouse tests to parallel

* Moved view tests to parallel

* Test code cleanup. Killed many warnings.

* dupe test

* Some mouse tests can't run in parallel because MouseGrabView

* Made SpinnerView more testable

* Moved more tests

* SubViews to IReadOnlyCollection<View>

* SubViews to IReadOnlyCollection<View> 2

* scrollbar tests

* shortcut tests

* Use InternalSubViews vs. _subviews

* Nuked View.IsAdded.
Added View.SuperViewChanged.

* API doc updats

* Unit Test tweak

* Unit Test tweak
2025-03-08 15:42:17 -07:00

132 lines
4.2 KiB
C#

using UnitTests;
namespace Terminal.Gui.ViewTests;
public class CanFocusTests
{
// TODO: Figure out what this test is supposed to be testing
[Fact]
public void CanFocus_Faced_With_Container_Before_Run ()
{
Application.Init (new FakeDriver ());
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.Iteration += (s, a) => Application.RequestStop ();
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 ()
{
Label label = new () { Text = "label" };
View view = new () { Text = "view", CanFocus = true };
Application.Navigation = new ();
Application.Top = new ();
Application.Top.Add (label, view);
Application.Top.SetFocus ();
Assert.Equal (view, Application.Navigation.GetFocused ());
Assert.False (label.CanFocus);
Assert.False (label.HasFocus);
Assert.True (view.CanFocus);
Assert.True (view.HasFocus);
Assert.False (Application.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 (Application.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 (Application.RaiseKeyDownEvent (Key.Tab));
Assert.True (label.HasFocus);
Assert.False (view.HasFocus);
Application.Top.Dispose ();
Application.ResetState ();
}
}