Bladeren bron

Unit tests

Tigger Kindel 2 jaren geleden
bovenliggende
commit
a23a33f20c

+ 64 - 44
Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs

@@ -3,6 +3,7 @@
 //
 using System;
 using System.Collections.Generic;
+using System.Diagnostics;
 using System.Runtime.InteropServices;
 using System.Text;
 using Unix.Terminal;
@@ -13,13 +14,16 @@ namespace Terminal.Gui;
 /// This is the Curses driver for the gui.cs/Terminal framework.
 /// </summary>
 internal class CursesDriver : ConsoleDriver {
+	
 	public override int Cols => Curses.Cols;
 	public override int Rows => Curses.Lines;
 
 	CursorVisibility? _initialCursorVisibility = null;
 	CursorVisibility? _currentCursorVisibility = null;
 
-	public override string GetVersionInfo () => $"{Curses.curses_version()}";
+	public override string GetVersionInfo () => $"{Curses.curses_version ()}";
+
+	public override bool SupportsTrueColor => false;
 
 	public override void Move (int col, int row)
 	{
@@ -65,6 +69,7 @@ internal class CursesDriver : ConsoleDriver {
 	static Attribute MakeColor (short foreground, short background)
 	{
 		var v = (short)((int)foreground | background << 4);
+
 		// TODO: for TrueColor - Use InitExtendedPair
 		Curses.InitColorPair (v, foreground, background);
 		return new Attribute (
@@ -81,7 +86,14 @@ internal class CursesDriver : ConsoleDriver {
 	/// </remarks>
 	public override Attribute MakeColor (Color fore, Color back)
 	{
-		return MakeColor (ColorToCursesColorNumber (fore), ColorToCursesColorNumber (back));
+		if (_window != null) {
+			return MakeColor (ColorToCursesColorNumber (fore), ColorToCursesColorNumber (back));
+		} else {
+			return new Attribute (
+				value: 0,
+				foreground: fore,
+				background: back);
+		}
 	}
 
 	static short ColorToCursesColorNumber (Color color)
@@ -191,6 +203,9 @@ internal class CursesDriver : ConsoleDriver {
 		StopReportingMouseMoves ();
 		SetCursorVisibility (CursorVisibility.Default);
 
+		if (_window == null) {
+			return;
+		}
 		// throws away any typeahead that has been typed by
 		// the user and has not yet been read by the program.
 		Curses.flushinp ();
@@ -222,7 +237,7 @@ internal class CursesDriver : ConsoleDriver {
 					}
 
 				} else {
-					Curses.mvaddwstr (row, col, rune.ToString());
+					Curses.mvaddwstr (row, col, rune.ToString ());
 					if (rune.GetColumns () > 1 && col + 1 < Cols) {
 						// TODO: This is a hack to deal with non-BMP and wide characters.
 						//col++;
@@ -599,35 +614,58 @@ internal class CursesDriver : ConsoleDriver {
 			_window = Curses.initscr ();
 			Curses.set_escdelay (10);
 		} catch (Exception e) {
-			throw new Exception ($"Curses failed to initialize, the exception is: {e.Message}");
+			_window = null;
+			Debug.WriteLine ($"Curses failed to initialize. Assuming Unit Tests. The exception is: {e.Message}");
 		}
 
-		// Ensures that all procedures are performed at some previous closing.
-		Curses.doupdate ();
+		if (_window != null) {
+			// Ensures that all procedures are performed at some previous closing.
+			Curses.doupdate ();
+
+			// 
+			// We are setting Invisible as default so we could ignore XTerm DECSUSR setting
+			//
+			switch (Curses.curs_set (0)) {
+			case 0:
+				_currentCursorVisibility = _initialCursorVisibility = CursorVisibility.Invisible;
+				break;
 
-		// 
-		// We are setting Invisible as default so we could ignore XTerm DECSUSR setting
-		//
-		switch (Curses.curs_set (0)) {
-		case 0:
-			_currentCursorVisibility = _initialCursorVisibility = CursorVisibility.Invisible;
-			break;
+			case 1:
+				_currentCursorVisibility = _initialCursorVisibility = CursorVisibility.Underline;
+				Curses.curs_set (1);
+				break;
 
-		case 1:
-			_currentCursorVisibility = _initialCursorVisibility = CursorVisibility.Underline;
-			Curses.curs_set (1);
-			break;
+			case 2:
+				_currentCursorVisibility = _initialCursorVisibility = CursorVisibility.Box;
+				Curses.curs_set (2);
+				break;
 
-		case 2:
-			_currentCursorVisibility = _initialCursorVisibility = CursorVisibility.Box;
-			Curses.curs_set (2);
-			break;
+			default:
+				_currentCursorVisibility = _initialCursorVisibility = null;
+				break;
+			}
+			if (!Curses.HasColors) {
+				throw new InvalidOperationException ("V2 - This should never happen. File an Issue if it does.");
+			}
 
-		default:
-			_currentCursorVisibility = _initialCursorVisibility = null;
-			break;
+			Curses.raw ();
+			Curses.noecho ();
+
+			Curses.Window.Standard.keypad (true);
+
+			Curses.StartColor ();
+			Curses.UseDefaultColors ();
+			Curses.CheckWinChange ();
 		}
 
+		CurrentAttribute = MakeColor (Color.White, Color.Black);
+		InitializeColorSchemes ();
+
+		ClearContents ();
+
+		TerminalResized = terminalResized;
+		StartReportingMouseMoves ();
+
 		if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
 			Clipboard = new MacOSXClipboard ();
 		} else {
@@ -638,27 +676,9 @@ internal class CursesDriver : ConsoleDriver {
 			}
 		}
 
-		if (!Curses.HasColors) {
-			throw new InvalidOperationException ("V2 - This should never happen. File an Issue if it does.");
+		if (_window != null) {
+			Curses.refresh ();
 		}
-
-		Curses.raw ();
-		Curses.noecho ();
-
-		Curses.Window.Standard.keypad (true);
-
-		TerminalResized = terminalResized;
-
-		Curses.StartColor ();
-		Curses.UseDefaultColors ();
-		CurrentAttribute = MakeColor (Color.White, Color.Black);
-		InitializeColorSchemes ();
-
-		Curses.CheckWinChange ();
-		ClearContents ();
-		Curses.refresh ();
-
-		StartReportingMouseMoves ();
 	}
 
 	public static bool Is_WSL_Platform ()

+ 11 - 5
Terminal.Gui/ConsoleDrivers/CursesDriver/UnixMainLoop.cs

@@ -86,6 +86,8 @@ namespace Terminal.Gui {
 		MainLoop mainLoop;
 		bool winChanged;
 
+		bool _runningUnitTests = false;
+		
 		public Action WinChanged;
 
 		void IMainLoopDriver.Wakeup ()
@@ -96,11 +98,15 @@ namespace Terminal.Gui {
 		void IMainLoopDriver.Setup (MainLoop mainLoop)
 		{
 			this.mainLoop = mainLoop;
-			pipe (wakeupPipes);
-			AddWatch (wakeupPipes [1], Condition.PollIn, ml => {
-				read (wakeupPipes [1], ignore, readHandle);
-				return true;
-			});
+			try {
+				pipe (wakeupPipes);
+				AddWatch (wakeupPipes [1], Condition.PollIn, ml => {
+					read (wakeupPipes [1], ignore, readHandle);
+					return true;
+				});
+			} catch (DllNotFoundException e) {
+				_runningUnitTests = true;
+			}
 		}
 
 		/// <summary>

+ 2 - 0
Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs

@@ -43,6 +43,8 @@ public class FakeDriver : ConsoleDriver {
 
 	public static FakeDriver.Behaviors FakeBehaviors = new Behaviors ();
 
+	public override bool SupportsTrueColor => false;
+
 	public FakeDriver ()
 	{
 		if (FakeBehaviors.UseFakeClipboard) {

+ 2 - 2
Terminal.Gui/ConsoleDrivers/NetDriver.cs

@@ -561,10 +561,10 @@ internal class NetDriver : ConsoleDriver {
 
 	public override void End ()
 	{
-		_mainLoop._netEvents.StopTasks ();
+		_mainLoop?._netEvents.StopTasks ();
 
 		if (IsWinPlatform) {
-			NetWinConsole.Cleanup ();
+			NetWinConsole?.Cleanup ();
 		}
 
 		StopReportingMouseMoves ();

+ 32 - 24
Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

@@ -784,7 +784,9 @@ internal class WindowsDriver : ConsoleDriver {
 		set {
 			base.Force16Colors = value;
 			// BUGBUG: This is a hack until we fully support VirtualTerminalSequences
-			WinConsole = new WindowsConsole ();
+			if (WinConsole != null) {
+				WinConsole = new WindowsConsole ();
+			}
 			Refresh ();
 		}
 	}
@@ -822,12 +824,19 @@ internal class WindowsDriver : ConsoleDriver {
 		if (w == Cols - 3 && e.Height < Rows) {
 			w += 3;
 		}
-		var newSize = WinConsole.SetConsoleWindow (
-		    (short)Math.Max (w, 16), (short)Math.Max (e.Height, 0));
 		Left = 0;
 		Top = 0;
-		Cols = newSize.Width;
-		Rows = newSize.Height;
+		Cols = e.Width;
+		Rows = e.Height;
+
+		if (WinConsole != null) {
+			var newSize = WinConsole.SetConsoleWindow (
+				(short)Math.Max (w, 16), (short)Math.Max (e.Height, 0));
+
+			Cols = newSize.Width;
+			Rows = newSize.Height;
+		}
+
 		ResizeScreen ();
 		ClearContents ();
 		TerminalResized.Invoke ();
@@ -1436,7 +1445,6 @@ internal class WindowsDriver : ConsoleDriver {
 	public override void Init (Action terminalResized)
 	{
 		TerminalResized = terminalResized;
-
 		
 		try {
 
@@ -1480,10 +1488,6 @@ internal class WindowsDriver : ConsoleDriver {
 
 	void ResizeScreen ()
 	{
-		if (WinConsole == null) {
-			return;
-		}
-
 		_outputBuffer = new WindowsConsole.ExtendedCharInfo [Rows * Cols];
 		Clip = new Rect (0, 0, Cols, Rows);
 		_damageRegion = new WindowsConsole.SmallRect () {
@@ -1494,13 +1498,13 @@ internal class WindowsDriver : ConsoleDriver {
 		};
 		_dirtyLines = new bool [Rows];
 
-		WinConsole.ForceRefreshCursorVisibility ();
+		WinConsole?.ForceRefreshCursorVisibility ();
 	}
-
-
+	
+	
 	public override void UpdateScreen ()
 	{
-		var windowSize = WinConsole.GetConsoleBufferWindow (out _);
+		var windowSize = WinConsole?.GetConsoleBufferWindow (out _) ?? new Size (Cols, Rows);
 		if (!windowSize.IsEmpty && (windowSize.Width != Cols || windowSize.Height != Rows)) {
 			return;
 		}
@@ -1548,7 +1552,7 @@ internal class WindowsDriver : ConsoleDriver {
 			Right = (short)Cols
 		};
 
-		if (!WinConsole.WriteToConsole (new Size (Cols, Rows), _outputBuffer, bufferCoords, _damageRegion, Force16Colors)) {
+		if (WinConsole != null && !WinConsole.WriteToConsole (new Size (Cols, Rows), _outputBuffer, bufferCoords, _damageRegion, Force16Colors)) {
 			var err = Marshal.GetLastWin32Error ();
 			if (err != 0) {
 				throw new System.ComponentModel.Win32Exception (err);
@@ -1560,7 +1564,7 @@ internal class WindowsDriver : ConsoleDriver {
 	public override void Refresh ()
 	{
 		UpdateScreen ();
-		WinConsole.SetInitialCursorVisibility ();
+		WinConsole?.SetInitialCursorVisibility ();
 		UpdateCursor ();
 	}
 
@@ -1594,42 +1598,46 @@ internal class WindowsDriver : ConsoleDriver {
 
 	#endregion
 
-	CursorVisibility savedCursorVisibility;
+	CursorVisibility _cachedCursorVisibility;
 
 	public override void UpdateCursor ()
 	{
 		if (Col < 0 || Row < 0 || Col > Cols || Row > Rows) {
 			GetCursorVisibility (out CursorVisibility cursorVisibility);
-			savedCursorVisibility = cursorVisibility;
+			_cachedCursorVisibility = cursorVisibility;
 			SetCursorVisibility (CursorVisibility.Invisible);
 			return;
 		}
 
-		SetCursorVisibility (savedCursorVisibility);
+		SetCursorVisibility (_cachedCursorVisibility);
 		var position = new WindowsConsole.Coord () {
 			X = (short)Col,
 			Y = (short)Row
 		};
-		WinConsole.SetCursorPosition (position);
+		WinConsole?.SetCursorPosition (position);
 	}
 
 	/// <inheritdoc/>
 	public override bool GetCursorVisibility (out CursorVisibility visibility)
 	{
-		return WinConsole.GetCursorVisibility (out visibility);
+		if (WinConsole != null) {
+			return WinConsole.GetCursorVisibility (out visibility);
+		}
+		visibility = _cachedCursorVisibility;
+		return true;
 	}
 
 	/// <inheritdoc/>
 	public override bool SetCursorVisibility (CursorVisibility visibility)
 	{
-		savedCursorVisibility = visibility;
-		return WinConsole.SetCursorVisibility (visibility);
+		_cachedCursorVisibility = visibility;
+		return WinConsole == null || WinConsole.SetCursorVisibility (visibility);
 	}
 
 	/// <inheritdoc/>
 	public override bool EnsureCursorVisibility ()
 	{
-		return WinConsole.EnsureCursorVisibility ();
+		return WinConsole == null || WinConsole.EnsureCursorVisibility ();
 	}
 
 	public override void SendKeys (char keyChar, ConsoleKey key, bool shift, bool alt, bool control)

+ 44 - 5
UnitTests/ConsoleDrivers/ColorTests.cs

@@ -9,14 +9,14 @@ namespace Terminal.Gui.DriverTests {
 
 		[Theory]
 		[InlineData (typeof (FakeDriver))]
-		//[InlineData (typeof (NetDriver))]
-		//[InlineData (typeof (CursesDriver))]
-		//[InlineData (typeof (WindowsDriver))]
+		[InlineData (typeof (NetDriver))]
+		[InlineData (typeof (CursesDriver))]
+		[InlineData (typeof (WindowsDriver))]
 		public void SetColors_Changes_Colors (Type driverType)
 		{
 			var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
 			Application.Init (driver);
-			driver.Init (() => { });
+//			driver.Init (() => { });
 			Assert.Equal (ConsoleColor.Gray, Console.ForegroundColor);
 			Assert.Equal (ConsoleColor.Black, Console.BackgroundColor);
 
@@ -29,7 +29,6 @@ namespace Terminal.Gui.DriverTests {
 			Console.ResetColor ();
 			Assert.Equal (ConsoleColor.Gray, Console.ForegroundColor);
 			Assert.Equal (ConsoleColor.Black, Console.BackgroundColor);
-			driver.End ();
 
 			// Shutdown must be called to safely clean up Application if Init has been called
 			Application.Shutdown ();
@@ -73,5 +72,45 @@ namespace Terminal.Gui.DriverTests {
 			Assert.Equal (new Attribute (Color.BrightYellow, Color.Blue), attrs [14]);
 			Assert.Equal (new Attribute (Color.White, Color.Black), attrs [^1]);
 		}
+
+		[Theory]
+		[InlineData (typeof (FakeDriver), false)]
+		[InlineData (typeof (NetDriver), true)]
+		[InlineData (typeof (CursesDriver), false)]
+		[InlineData (typeof (WindowsDriver), true)] // Because we're not Windows Terminal
+		public void SupportsTrueColor_Defaults (Type driverType, bool expectedSetting)
+		{
+			var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
+			driver.Init (() => { });
+
+			Assert.Equal (expectedSetting, driver.SupportsTrueColor);
+
+			driver.End ();
+
+			// Shutdown must be called to safely clean up Application if Init has been called
+			Application.Shutdown ();
+		}
+
+		[Theory]
+		[InlineData (typeof (FakeDriver), false)]
+		[InlineData (typeof (NetDriver), false)]
+		[InlineData (typeof (CursesDriver), false)]
+		[InlineData (typeof (WindowsDriver), false)] 
+		public void Force16Colors_Sets (Type driverType, bool expectedSetting)
+		{
+			var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
+			driver.Init (() => { });
+
+			Assert.Equal (expectedSetting, driver.Force16Colors);
+
+			driver.Force16Colors = true;
+
+			Assert.True (driver.Force16Colors);
+
+			driver.End ();
+
+			// Shutdown must be called to safely clean up Application if Init has been called
+			Application.Shutdown ();
+		}
 	}
 }