Browse Source

Adds CanFocus/SetFocus test

Tig 1 year ago
parent
commit
7d6d5280f1
2 changed files with 37 additions and 1 deletions
  1. 36 0
      UnitTests/View/MouseTests.cs
  2. 1 1
      UnitTests/Views/LabelTests.cs

+ 36 - 0
UnitTests/View/MouseTests.cs

@@ -0,0 +1,36 @@
+using Xunit.Abstractions;
+
+namespace Terminal.Gui.ViewTests;
+
+public class MouseTests (ITestOutputHelper output)
+{
+
+    [Theory]
+    [InlineData(false, false, false)]
+    [InlineData (true, false, true)]
+    [InlineData (true, true, true)]
+    public void MouseClick_SetsFocus_If_CanFocus (bool canFocus, bool setFocus, bool expectedHasFocus)
+    {
+        var superView = new View () { CanFocus = true, Height = 1, Width = 15 };
+        var focusedView = new View () { CanFocus = true, Width = 1, Height = 1 };
+        var testView = new View () { CanFocus = canFocus, X = 4, Width = 4, Height = 1 };
+        superView.Add (focusedView, testView);
+        superView.BeginInit ();
+        superView.EndInit (); 
+        
+        focusedView.SetFocus();
+
+        Assert.True (superView.HasFocus);
+        Assert.True (focusedView.HasFocus);
+        Assert.False (testView.HasFocus);
+
+        if (setFocus)
+        {
+            testView.SetFocus ();
+        }
+
+        testView.OnMouseEvent (new MouseEvent () { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked });
+        Assert.True (superView.HasFocus);
+        Assert.Equal(expectedHasFocus, testView.HasFocus);
+    }
+}

+ 1 - 1
UnitTests/Views/LabelTests.cs

@@ -59,7 +59,7 @@ public class LabelTests
         var focusedView = new View () { CanFocus = true, Width = 1, Height = 1 };
         var focusedView = new View () { CanFocus = true, Width = 1, Height = 1 };
         var label = new Label () { X = 2, Title = "_x" };
         var label = new Label () { X = 2, Title = "_x" };
         var nextSubview = new View () { CanFocus = true, X = 4, Width = 4, Height = 1 };
         var nextSubview = new View () { CanFocus = true, X = 4, Width = 4, Height = 1 };
-        superView.Add (label, nextSubview);
+        superView.Add (focusedView, label, nextSubview);
         superView.BeginInit ();
         superView.BeginInit ();
         superView.EndInit ();
         superView.EndInit ();