From a22a59ebbd85492d6aa44f6403e0508e6c2ebe3d Mon Sep 17 00:00:00 2001 From: Tig Date: Fri, 23 Aug 2024 14:57:19 -0600 Subject: [PATCH] Added more nav tests --- Terminal.Gui/View/View.Hierarchy.cs | 1 + UnitTests/View/Navigation/AddRemoveTests.cs | 44 ++++++++++-- .../View/Navigation/AdvanceFocusTests.cs | 69 +++++++++++++++++++ UnitTests/Views/ColorPickerTests.cs | 2 +- 4 files changed, 109 insertions(+), 7 deletions(-) diff --git a/Terminal.Gui/View/View.Hierarchy.cs b/Terminal.Gui/View/View.Hierarchy.cs index 70778cb92..e43ef7a9c 100644 --- a/Terminal.Gui/View/View.Hierarchy.cs +++ b/Terminal.Gui/View/View.Hierarchy.cs @@ -69,6 +69,7 @@ public partial class View // SuperView/SubView hierarchy management (SuperView, if (view.Enabled && view.Visible && view.CanFocus) { + // Add will cause the newly added subview to gain focus if it's focusable if (HasFocus) { view.SetFocus (); diff --git a/UnitTests/View/Navigation/AddRemoveTests.cs b/UnitTests/View/Navigation/AddRemoveTests.cs index d52222e3d..e2182179f 100644 --- a/UnitTests/View/Navigation/AddRemoveTests.cs +++ b/UnitTests/View/Navigation/AddRemoveTests.cs @@ -4,8 +4,8 @@ namespace Terminal.Gui.ViewTests; public class AddRemoveNavigationTests (ITestOutputHelper _output) : TestsAllViews { - [Fact] - public void Add_Subview_Gets_Focus () + [Fact] + public void Add_First_Subview_Gets_Focus () { View top = new View () { @@ -16,24 +16,56 @@ public class AddRemoveNavigationTests (ITestOutputHelper _output) : TestsAllView top.SetFocus (); Assert.True (top.HasFocus); - int nEnter = 0; View subView = new View () { Id = "subView", CanFocus = true }; - subView.HasFocusChanging += (s, e) => nEnter++; top.Add (subView); Assert.True (top.HasFocus); Assert.Equal (subView, top.Focused); Assert.True (subView.HasFocus); - Assert.Equal (1, nEnter); } [Fact] - public void Add_Subview_Deepest_Gets_Focus () + public void Add_Subsequent_Subview_Gets_Focus () + { + View top = new View () + { + Id = "top", + CanFocus = true + }; + + top.SetFocus (); + Assert.True (top.HasFocus); + + View subView = new View () + { + Id = "subView", + CanFocus = true + }; + + top.Add (subView); + + Assert.True (subView.HasFocus); + + View subView2 = new View () + { + Id = "subView2", + CanFocus = true + }; + + top.Add (subView2); + + Assert.True (subView2.HasFocus); + + + } + + [Fact] + public void Add_Nested_Subviews_Deepest_Gets_Focus () { View top = new View () { diff --git a/UnitTests/View/Navigation/AdvanceFocusTests.cs b/UnitTests/View/Navigation/AdvanceFocusTests.cs index 4596a1fcf..ffdea4c10 100644 --- a/UnitTests/View/Navigation/AdvanceFocusTests.cs +++ b/UnitTests/View/Navigation/AdvanceFocusTests.cs @@ -200,6 +200,75 @@ public class AdvanceFocusTests (ITestOutputHelper _output) Assert.Equal (tabStop, view.TabStop); } + + [Fact] + public void AdvanceFocus_Compound_Subview () + { + var top = new View () { Id = "top", CanFocus = true }; + + var compoundSubview = new View () + { + CanFocus = true, + Id = "compoundSubview", + }; + var v1 = new View { Id = "v1", CanFocus = true }; + var v2 = new View { Id = "v2", CanFocus = true }; + var v3 = new View { Id = "v3", CanFocus = false }; + + compoundSubview.Add (v1, v2, v3); + + top.Add (compoundSubview); + + // Cycle through v1 & v2 + top.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop); + Assert.True (v1.HasFocus); + Assert.False (v2.HasFocus); + Assert.False (v3.HasFocus); + top.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop); + Assert.False (v1.HasFocus); + Assert.True (v2.HasFocus); + Assert.False (v3.HasFocus); + top.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop); + Assert.True (v1.HasFocus); + Assert.False (v2.HasFocus); + Assert.False (v3.HasFocus); + + // Add another subview + View otherSubview = new () + { + CanFocus = true, + Id = "otherSubview", + }; + + top.Add (otherSubview); + // Adding a focusable subview causes advancefocus + Assert.True (otherSubview.HasFocus); + Assert.False (v1.HasFocus); + + // Cycle through v1 & v2 + top.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop); + Assert.True (v1.HasFocus); + Assert.False (v2.HasFocus); + Assert.False (v3.HasFocus); + top.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop); + Assert.False (v1.HasFocus); + Assert.True (v2.HasFocus); + Assert.False (v3.HasFocus); + top.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop); + Assert.False (v1.HasFocus); + Assert.False (v2.HasFocus); + Assert.False (v3.HasFocus); + + Assert.True (otherSubview.HasFocus); + // v2 was previously focused down the compoundSubView focus chain + top.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop); + Assert.False (v1.HasFocus); + Assert.True (v2.HasFocus); + Assert.False (v3.HasFocus); + + top.Dispose (); + } + [Fact] public void AdvanceFocus_With_CanFocus_Are_All_True () { diff --git a/UnitTests/Views/ColorPickerTests.cs b/UnitTests/Views/ColorPickerTests.cs index d575d7d98..03b21b7b4 100644 --- a/UnitTests/Views/ColorPickerTests.cs +++ b/UnitTests/Views/ColorPickerTests.cs @@ -733,7 +733,7 @@ public class ColorPickerTests Assert.True (hex.HasFocus); // Tab out of the hex field - should wrap to first focusable subview - Application.OnKeyDown (Key.Tab); + Application.OnKeyDown (Key.Tab); Assert.False (hex.HasFocus); Assert.NotSame (hex, cp.Focused);