Browse Source

Fixed TextFieldTests

Tig 1 year ago
parent
commit
c5d34b276c
1 changed files with 63 additions and 68 deletions
  1. 63 68
      UnitTests/Views/TextFieldTests.cs

+ 63 - 68
UnitTests/Views/TextFieldTests.cs

@@ -2,68 +2,62 @@
 using System.Globalization;
 using System.Reflection;
 using System.Text;
-using CsvHelper;
 using Xunit.Abstractions;
 
 namespace Terminal.Gui.ViewsTests;
 
-public class TextFieldTests
+public class TextFieldTests (ITestOutputHelper output)
 {
     private static TextField _textField;
-    private readonly ITestOutputHelper _output;
-    public TextFieldTests (ITestOutputHelper output) { _output = output; }
 
     [Fact]
-    [AutoInitShutdown]
+    [SetupFakeDriver]
     public void Accented_Letter_With_Three_Combining_Unicode_Chars ()
     {
         var tf = new TextField { Width = 3, Text = "ắ" };
-        Toplevel top = new ();
-        top.Add (tf);
-        Application.Begin (top);
+        tf.Draw ();
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
 ắ",
-                                                      _output
+                                                      output
                                                      );
 
         tf.Text = "\u1eaf";
-        Application.Refresh ();
+        tf.Draw ();
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
 ắ",
-                                                      _output
+                                                      output
                                                      );
 
         tf.Text = "\u0103\u0301";
-        Application.Refresh ();
+        tf.Draw ();
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
 ắ",
-                                                      _output
+                                                      output
                                                      );
 
         tf.Text = "\u0061\u0306\u0301";
-        Application.Refresh ();
+        tf.Draw ();
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
 ắ",
-                                                      _output
+                                                      output
                                                      );
     }
 
     [Fact]
-    [AutoInitShutdown]
+    [SetupFakeDriver]
     public void Adjust_First ()
     {
         var tf = new TextField { Width = Dim.Fill (), Text = "This is a test." };
-        var top = new Toplevel ();
-        top.Add (tf);
-        Application.Begin (top);
+        tf.SetRelativeLayout (new (20, 20));
+        tf.Draw ();
 
         Assert.Equal ("This is a test. ", GetContents ());
 
@@ -123,8 +117,8 @@ public class TextFieldTests
         Assert.False (fv.HasFocus);
 
         tf.NewMouseEvent (
-                       new MouseEvent { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked }
-                      );
+                          new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked }
+                         );
 
         Assert.Null (tf.SelectedText);
         Assert.False (tf.CanFocus);
@@ -137,8 +131,8 @@ public class TextFieldTests
         tf.CanFocus = true;
 
         tf.NewMouseEvent (
-                       new MouseEvent { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked }
-                      );
+                          new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked }
+                         );
 
         Assert.Equal ("some ", tf.SelectedText);
         Assert.True (tf.CanFocus);
@@ -149,14 +143,15 @@ public class TextFieldTests
         fv.CanFocus = false;
 
         tf.NewMouseEvent (
-                       new MouseEvent { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked }
-                      );
+                          new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked }
+                         );
 
         Assert.Equal ("some ", tf.SelectedText); // Setting CanFocus to false don't change the SelectedText
         Assert.False (tf.CanFocus);
         Assert.False (tf.HasFocus);
         Assert.False (fv.CanFocus);
         Assert.False (fv.HasFocus);
+        top.Dispose ();
     }
 
     [Theory]
@@ -173,7 +168,8 @@ public class TextFieldTests
         Assert.False (tf.HasFocus);
 
         tf.Draw ();
-        TestHelpers.AssertDriverContentsAre (expectedRender, _output);
+        TestHelpers.AssertDriverContentsAre (expectedRender, output);
+        Application.Top.Dispose ();
     }
 
     [Fact]
@@ -192,7 +188,9 @@ public class TextFieldTests
         Assert.False (tf.HasFocus);
 
         tf.Draw ();
-        TestHelpers.AssertDriverContentsAre ("Misérables", _output);
+        TestHelpers.AssertDriverContentsAre ("Misérables", output);
+        Application.Top.Dispose ();
+
     }
 
     [Theory]
@@ -204,7 +202,7 @@ public class TextFieldTests
         TextField tf = GetTextFieldsInView ();
 
         tf.Draw ();
-        TestHelpers.AssertDriverContentsAre ("", _output);
+        TestHelpers.AssertDriverContentsAre ("", output);
 
         tf.Caption = "Enter txt";
         Application.Driver.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
@@ -212,12 +210,13 @@ public class TextFieldTests
         // Caption should appear when not focused and no text
         Assert.False (tf.HasFocus);
         tf.Draw ();
-        TestHelpers.AssertDriverContentsAre ("Enter txt", _output);
+        TestHelpers.AssertDriverContentsAre ("Enter txt", output);
 
         // but disapear when text is added
         tf.Text = content;
         tf.Draw ();
-        TestHelpers.AssertDriverContentsAre (content, _output);
+        TestHelpers.AssertDriverContentsAre (content, output);
+        Application.Top.Dispose ();
     }
 
     [Fact]
@@ -227,19 +226,20 @@ public class TextFieldTests
         TextField tf = GetTextFieldsInView ();
 
         tf.Draw ();
-        TestHelpers.AssertDriverContentsAre ("", _output);
+        TestHelpers.AssertDriverContentsAre ("", output);
 
         // Caption has no effect when focused
         tf.Caption = "Enter txt";
         Assert.True (tf.HasFocus);
         tf.Draw ();
-        TestHelpers.AssertDriverContentsAre ("", _output);
+        TestHelpers.AssertDriverContentsAre ("", output);
 
         Application.Driver.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
 
         Assert.False (tf.HasFocus);
         tf.Draw ();
-        TestHelpers.AssertDriverContentsAre ("Enter txt", _output);
+        TestHelpers.AssertDriverContentsAre ("Enter txt", output);
+        Application.Top.Dispose ();
     }
 
     [Fact]
@@ -485,8 +485,8 @@ public class TextFieldTests
 
         Assert.True (
                      tf.NewMouseEvent (
-                                    new MouseEvent { Position = new (7, 1), Flags = MouseFlags.Button1DoubleClicked, View = tf }
-                                   )
+                                       new () { Position = new (7, 1), Flags = MouseFlags.Button1DoubleClicked, View = tf }
+                                      )
                     );
         Assert.Equal ("Misérables ", tf.SelectedText);
         Assert.Equal (11, tf.SelectedLength);
@@ -494,6 +494,7 @@ public class TextFieldTests
         Assert.Equal ("Les movie.", newText);
         Assert.Equal ("Les Misérables movie.", oldText);
         Assert.Equal ("Les movie.", tf.Text);
+        top.Dispose ();
     }
 
     [Fact]
@@ -747,7 +748,6 @@ public class TextFieldTests
         Assert.Equal ("", tf.Text);
     }
 
-
     [Fact]
     public void HotKey_Command_SetsFocus ()
     {
@@ -770,6 +770,7 @@ public class TextFieldTests
         Assert.False (accepted);
 
         return;
+
         void OnAccept (object sender, CancelEventArgs e) { accepted = true; }
     }
 
@@ -793,6 +794,7 @@ public class TextFieldTests
     {
         //var super = new View ();
         var view = new TextField ();
+
         //super.Add (view);
 
         //var superAcceptedInvoked = false;
@@ -904,13 +906,6 @@ public class TextFieldTests
 
         Assert.Equal (0, tf.ScrollOffset);
         Assert.Equal (16, tf.CursorPosition);
-
-        var top = new Toplevel ();
-        top.Add (tf);
-        Application.Begin (top);
-
-        Assert.Equal (0, tf.ScrollOffset);
-        Assert.Equal (16, tf.CursorPosition);
     }
 
     [Fact]
@@ -947,6 +942,7 @@ public class TextFieldTests
 
         //                                             TAB to jump between text fields.
         TestHelpers.AssertDriverAttributesAre ("1111000", Application.Driver, attributes);
+        top.Dispose ();
     }
 
     [Fact]
@@ -1044,7 +1040,7 @@ public class TextFieldTests
 
         // Get a fresh instance that represents a right click.
         // Should be ignored because of SuppressRightClick callback
-        mouseEvent = new MouseEvent { Flags = MouseFlags.Button3Clicked, View = tf };
+        mouseEvent = new () { Flags = MouseFlags.Button3Clicked, View = tf };
         Application.OnMouseEvent (mouseEvent);
         Assert.Equal (1, clickCounter);
 
@@ -1052,7 +1048,7 @@ public class TextFieldTests
 
         // Get a fresh instance that represents a right click.
         // Should no longer be ignored as the callback was removed
-        mouseEvent = new MouseEvent { Flags = MouseFlags.Button3Clicked, View = tf };
+        mouseEvent = new () { Flags = MouseFlags.Button3Clicked, View = tf };
 
         // In #3183 OnMouseClicked is no longer called before MouseEvent().
         // This call causes the context menu to pop, and MouseEvent() returns true.
@@ -1060,6 +1056,7 @@ public class TextFieldTests
         // Which is correct, because the user did NOT click with the left mouse button.
         Application.OnMouseEvent (mouseEvent);
         Assert.Equal (1, clickCounter);
+        top.Dispose ();
 
         return;
 
@@ -1145,7 +1142,6 @@ public class TextFieldTests
     }
 
     [Fact]
-    [TextFieldTestsAutoInitShutdown]
     public void SpaceHandling ()
     {
         var tf = new TextField { Width = 10, Text = " " };
@@ -1155,7 +1151,7 @@ public class TextFieldTests
         tf.NewMouseEvent (ev);
         Assert.Equal (1, tf.SelectedLength);
 
-        ev = new MouseEvent { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked };
+        ev = new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked };
 
         tf.NewMouseEvent (ev);
         Assert.Equal (1, tf.SelectedLength);
@@ -1372,9 +1368,9 @@ public class TextFieldTests
         while (_textField.CursorPosition > 0)
         {
             _textField.NewKeyDownEvent (
-                                        new Key (
-                                                 KeyCode.CursorLeft | KeyCode.CtrlMask | KeyCode.ShiftMask
-                                                )
+                                        new (
+                                             KeyCode.CursorLeft | KeyCode.CtrlMask | KeyCode.ShiftMask
+                                            )
                                        );
 
             switch (iteration)
@@ -1446,9 +1442,9 @@ public class TextFieldTests
         while (_textField.CursorPosition > 0)
         {
             _textField.NewKeyDownEvent (
-                                        new Key (
-                                                 KeyCode.CursorLeft | KeyCode.CtrlMask | KeyCode.ShiftMask
-                                                )
+                                        new (
+                                             KeyCode.CursorLeft | KeyCode.CtrlMask | KeyCode.ShiftMask
+                                            )
                                        );
 
             switch (iteration)
@@ -1538,15 +1534,15 @@ public class TextFieldTests
 
         Assert.True (
                      tf.NewMouseEvent (
-                                    new MouseEvent { Position = new (idx, 1), Flags = MouseFlags.Button1DoubleClicked, View = tf }
-                                   )
+                                       new () { Position = new (idx, 1), Flags = MouseFlags.Button1DoubleClicked, View = tf }
+                                      )
                     );
         Assert.Equal ("movie.", tf.SelectedText);
 
         Assert.True (
                      tf.NewMouseEvent (
-                                    new MouseEvent { Position = new (idx + 1, 1), Flags = MouseFlags.Button1DoubleClicked, View = tf }
-                                   )
+                                       new () { Position = new (idx + 1, 1), Flags = MouseFlags.Button1DoubleClicked, View = tf }
+                                      )
                     );
         Assert.Equal ("movie.", tf.SelectedText);
     }
@@ -1823,37 +1819,36 @@ public class TextFieldTests
     }
 
     [Fact]
-    [AutoInitShutdown]
+    [SetupFakeDriver]
     public void Words_With_Accents_Incorrect_Order_Will_Result_With_Wrong_Accent_Place ()
     {
         var tf = new TextField { Width = 30, Text = "Les Misérables" };
-        Toplevel top = new ();
-        top.Add (tf);
-        Application.Begin (top);
+        tf.SetRelativeLayout(new Size(100,100));
+        tf.Draw();
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
 Les Misérables",
-                                                      _output
+                                                      output
                                                      );
 
         tf.Text = "Les Mise" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "rables";
-        Application.Refresh ();
+        tf.Draw ();
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
 Les Misérables",
-                                                      _output
+                                                      output
                                                      );
 
         // incorrect order will result with a wrong accent place
         tf.Text = "Les Mis" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "erables";
-        Application.Refresh ();
+        tf.Draw ();
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
 Les Miśerables",
-                                                      _output
+                                                      output
                                                      );
     }
 
@@ -1893,9 +1888,9 @@ Les Miśerables",
             base.Before (methodUnderTest);
 
             //Application.Top.ColorScheme = Colors.ColorSchemes ["Base"];
-            _textField = new TextField
+            _textField = new ()
             {
-                ColorScheme = new ColorScheme (Colors.ColorSchemes ["Base"]),
+                ColorScheme = new (Colors.ColorSchemes ["Base"]),
 
                 //                1         2         3 
                 //      01234567890123456789012345678901=32 (Length)