Browse Source

Fixed bug; added unit tests

Tig 1 year ago
parent
commit
b7f3a85203

+ 1 - 1
Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs

@@ -833,7 +833,7 @@ internal class CursesDriver : ConsoleDriver
     /// <returns></returns>
     private static Attribute MakeColor (short foreground, short background)
     {
-        var v = (short)(foreground | (background << 4));
+        var v = (short)((ushort)foreground | (background << 4));
 
         // TODO: for TrueColor - Use InitExtendedPair
         Curses.InitColorPair (v, foreground, background);

+ 5 - 4
Terminal.Gui/View/Layout/ViewLayout.cs

@@ -670,9 +670,10 @@ public partial class View
 
         CheckDimAuto ();
 
-        LayoutAdornments ();
+        var contentSize = ContentSize.GetValueOrDefault ();
+        OnLayoutStarted (new (contentSize));
 
-        OnLayoutStarted (new (ContentSize.GetValueOrDefault ()));
+        LayoutAdornments ();
 
         SetTextFormatterSize ();
 
@@ -684,7 +685,7 @@ public partial class View
 
         foreach (View v in ordered)
         {
-            LayoutSubview (v, Viewport.Size);
+            LayoutSubview (v, contentSize);
         }
 
         // If the 'to' is rooted to 'from' it's a special-case.
@@ -699,7 +700,7 @@ public partial class View
 
         LayoutNeeded = false;
 
-        OnLayoutComplete (new (ContentSize.GetValueOrDefault ()));
+        OnLayoutComplete (new (contentSize));
     }
 
     private void LayoutSubview (View v, Size contentSize)

+ 61 - 62
UnitTests/View/Layout/LayoutTests.cs

@@ -115,66 +115,65 @@ public class LayoutTests (ITestOutputHelper output)
         sub2.Dispose ();
     }
 
-    //[Fact]
-    //[AutoInitShutdown]
-    //public void TrySetHeight_ForceValidatePosDim ()
-    //{
-    //    var top = new View { X = 0, Y = 0, Height = 20 };
-
-    //    var v = new View { Height = Dim.Fill (), ValidatePosDim = true };
-    //    top.Add (v);
-
-    //    Assert.False (v.TrySetHeight (10, out int rHeight));
-    //    Assert.Equal (10, rHeight);
-
-    //    v.Height = Dim.Fill (1);
-    //    Assert.False (v.TrySetHeight (10, out rHeight));
-    //    Assert.Equal (9, rHeight);
-
-    //    v.Height = 0;
-    //    Assert.True (v.TrySetHeight (10, out rHeight));
-    //    Assert.Equal (10, rHeight);
-    //    Assert.False (v.IsInitialized);
-
-    //    var toplevel = new Toplevel ();
-    //    toplevel.Add (top);
-    //    Application.Begin (toplevel);
-
-    //    Assert.True (v.IsInitialized);
-
-    //    v.Height = 15;
-    //    Assert.True (v.TrySetHeight (5, out rHeight));
-    //    Assert.Equal (5, rHeight);
-    //}
-
-    //[Fact]
-    //[AutoInitShutdown]
-    //public void TrySetWidth_ForceValidatePosDim ()
-    //{
-    //    var top = new View { X = 0, Y = 0, Width = 80 };
-
-    //    var v = new View { Width = Dim.Fill (), ValidatePosDim = true };
-    //    top.Add (v);
-
-    //    Assert.False (v.TrySetWidth (70, out int rWidth));
-    //    Assert.Equal (70, rWidth);
-
-    //    v.Width = Dim.Fill (1);
-    //    Assert.False (v.TrySetWidth (70, out rWidth));
-    //    Assert.Equal (69, rWidth);
-
-    //    v.Width = 0;
-    //    Assert.True (v.TrySetWidth (70, out rWidth));
-    //    Assert.Equal (70, rWidth);
-    //    Assert.False (v.IsInitialized);
-
-    //    var toplevel = new Toplevel ();
-    //    toplevel.Add (top);
-    //    Application.Begin (toplevel);
-
-    //    Assert.True (v.IsInitialized);
-    //    v.Width = 75;
-    //    Assert.True (v.TrySetWidth (60, out rWidth));
-    //    Assert.Equal (60, rWidth);
-    //}
+    [Fact]
+    public void LayoutSubviews_Uses_ContentSize ()
+    {
+        var superView = new View ()
+        {
+            Width = 5,
+            Height = 5,
+            ContentSize = new (10, 10)
+        };
+        var view = new View ()
+        {
+            X = Pos.Center ()
+        };
+        superView.Add (view);
+
+        superView.LayoutSubviews ();
+
+        Assert.Equal (5, view.Frame.X);
+        superView.Dispose ();
+    }
+
+    // Test OnLayoutStarted/OnLayoutComplete - ensure that they are called at right times
+    [Fact]
+    public void LayoutSubviews_LayoutStarted_Complete ()
+    {
+        var superView = new View ();
+        var view = new View ();
+        superView.Add (view);
+        superView.BeginInit ();
+        superView.EndInit ();
+
+        var layoutStarted = false;
+        var layoutComplete = false;
+
+        var borderLayoutStarted = false;
+        var borderLayoutComplete = false;
+
+        view.LayoutStarted += (sender, e) => layoutStarted = true;
+        view.LayoutComplete += (sender, e) => layoutComplete = true;
+
+        view.Border.LayoutStarted += (sender, e) =>
+                                     {
+                                         Assert.True (layoutStarted);
+                                         borderLayoutStarted = true;
+                                     };
+        view.Border.LayoutComplete += (sender, e) =>
+                                      {
+                                          Assert.True (layoutStarted);
+                                          Assert.False (layoutComplete);
+                                          borderLayoutComplete = true;
+                                      };
+
+        superView.LayoutSubviews ();
+
+        Assert.True (borderLayoutStarted);
+        Assert.True (borderLayoutComplete);
+
+        Assert.True (layoutStarted);
+        Assert.True (layoutComplete);
+        superView.Dispose ();
+    }
 }