Browse Source

Added more nav tests

Tig 11 months ago
parent
commit
a22a59ebbd

+ 1 - 0
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)
         if (view.Enabled && view.Visible && view.CanFocus)
         {
         {
+            // Add will cause the newly added subview to gain focus if it's focusable
             if (HasFocus)
             if (HasFocus)
             {
             {
                 view.SetFocus ();
                 view.SetFocus ();

+ 38 - 6
UnitTests/View/Navigation/AddRemoveTests.cs

@@ -4,8 +4,8 @@ namespace Terminal.Gui.ViewTests;
 
 
 public class AddRemoveNavigationTests (ITestOutputHelper _output) : TestsAllViews
 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 ()
         View top = new View ()
         {
         {
@@ -16,24 +16,56 @@ public class AddRemoveNavigationTests (ITestOutputHelper _output) : TestsAllView
         top.SetFocus ();
         top.SetFocus ();
         Assert.True (top.HasFocus);
         Assert.True (top.HasFocus);
 
 
-        int nEnter = 0;
         View subView = new View ()
         View subView = new View ()
         {
         {
             Id = "subView",
             Id = "subView",
             CanFocus = true
             CanFocus = true
         };
         };
-        subView.HasFocusChanging += (s, e) => nEnter++;
 
 
         top.Add (subView);
         top.Add (subView);
 
 
         Assert.True (top.HasFocus);
         Assert.True (top.HasFocus);
         Assert.Equal (subView, top.Focused);
         Assert.Equal (subView, top.Focused);
         Assert.True (subView.HasFocus);
         Assert.True (subView.HasFocus);
-        Assert.Equal (1, nEnter);
     }
     }
 
 
     [Fact]
     [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 ()
         View top = new View ()
         {
         {

+ 69 - 0
UnitTests/View/Navigation/AdvanceFocusTests.cs

@@ -200,6 +200,75 @@ public class AdvanceFocusTests (ITestOutputHelper _output)
         Assert.Equal (tabStop, view.TabStop);
         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]
     [Fact]
     public void AdvanceFocus_With_CanFocus_Are_All_True ()
     public void AdvanceFocus_With_CanFocus_Are_All_True ()
     {
     {

+ 1 - 1
UnitTests/Views/ColorPickerTests.cs

@@ -733,7 +733,7 @@ public class ColorPickerTests
         Assert.True (hex.HasFocus);
         Assert.True (hex.HasFocus);
 
 
         // Tab out of the hex field - should wrap to first focusable subview 
         // 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.False (hex.HasFocus);
         Assert.NotSame (hex, cp.Focused);
         Assert.NotSame (hex, cp.Focused);