Explorar o código

IClipboard tests for NotSupportedException

Charlie Kindel %!s(int64=2) %!d(string=hai) anos
pai
achega
aca4ab3eae

+ 17 - 3
Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs

@@ -58,12 +58,12 @@ namespace Terminal.Gui {
 
 
 		static bool sync = false;
 		static bool sync = false;
 		static public bool usingFakeClipboard;
 		static public bool usingFakeClipboard;
-
-		public FakeDriver (bool useFakeClipboard = true)
+		
+		public FakeDriver (bool useFakeClipboard = true, bool fakeClipboardThrows = false)
 		{
 		{
 			usingFakeClipboard = useFakeClipboard;
 			usingFakeClipboard = useFakeClipboard;
 			if (usingFakeClipboard) {
 			if (usingFakeClipboard) {
-				clipboard = new FakeClipboard ();
+				clipboard = new FakeClipboard (fakeClipboardThrows);
 			} else {
 			} else {
 				if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
 				if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
 					clipboard = new WindowsClipboard ();
 					clipboard = new WindowsClipboard ();
@@ -651,16 +651,30 @@ namespace Terminal.Gui {
 
 
 		public class FakeClipboard : ClipboardBase {
 		public class FakeClipboard : ClipboardBase {
 			public override bool IsSupported => true;
 			public override bool IsSupported => true;
+			public Exception FakeException = null;
 
 
 			string contents = string.Empty;
 			string contents = string.Empty;
 
 
+			public FakeClipboard (bool fakeClipboardThrowsNotSupportedException = false)
+			{
+				if (fakeClipboardThrowsNotSupportedException) {
+					FakeException = new NotSupportedException ("Fake clipboard exception");
+				}
+			}
+
 			protected override string GetClipboardDataImpl ()
 			protected override string GetClipboardDataImpl ()
 			{
 			{
+				if (FakeException != null) {
+					throw FakeException;
+				}
 				return contents;
 				return contents;
 			}
 			}
 
 
 			protected override void SetClipboardDataImpl (string text)
 			protected override void SetClipboardDataImpl (string text)
 			{
 			{
+				if (FakeException != null) {
+					throw FakeException;
+				}
 				contents = text;
 				contents = text;
 			}
 			}
 		}
 		}

+ 1 - 3
Terminal.Gui/Core/Clipboard/Clipboard.cs

@@ -28,7 +28,7 @@ namespace Terminal.Gui {
 		static ustring contents;
 		static ustring contents;
 
 
 		/// <summary>
 		/// <summary>
-		/// Get or sets the operation system clipboard, otherwise the contents field.
+		/// Gets (copies from) or sets (pastes to) the contents of the OS clipboard.
 		/// </summary>
 		/// </summary>
 		public static ustring Contents {
 		public static ustring Contents {
 			get {
 			get {
@@ -65,8 +65,6 @@ namespace Terminal.Gui {
 		/// Returns true if the environmental dependencies are in place to interact with the OS clipboard.
 		/// Returns true if the environmental dependencies are in place to interact with the OS clipboard.
 		/// </summary>
 		/// </summary>
 		/// <remarks>
 		/// <remarks>
-		/// The first time IsSupported is called, it will spawn a process (e.g. "bash xclip" or "powershell.exe"
-		/// to determine if the clipboard is supported byt the OS platform. This is a one-time cost.
 		/// </remarks>
 		/// </remarks>
 		public static bool IsSupported { get => Application.Driver.Clipboard.IsSupported; }
 		public static bool IsSupported { get => Application.Driver.Clipboard.IsSupported; }
 
 

+ 6 - 5
Terminal.Gui/Core/Clipboard/ClipboardBase.cs

@@ -23,8 +23,8 @@ namespace Terminal.Gui {
 		{
 		{
 			try {
 			try {
 				return GetClipboardDataImpl ();
 				return GetClipboardDataImpl ();
-			} catch (Exception ex) {
-				throw new NotSupportedException ("Failed to read clipboard.", ex);
+			} catch (NotSupportedException ex) {
+				throw new NotSupportedException ("Failed to copy from the OS clipboard.", ex);
 			}
 			}
 		}
 		}
 
 
@@ -44,7 +44,7 @@ namespace Terminal.Gui {
 		{
 		{
 			try {
 			try {
 				SetClipboardDataImpl (text);
 				SetClipboardDataImpl (text);
-			} catch (Exception ex) {
+			} catch (NotSupportedException ex) {
 				throw new NotSupportedException ("Failed to paste to the OS clipboard.", ex);
 				throw new NotSupportedException ("Failed to paste to the OS clipboard.", ex);
 			}
 			}
 		}
 		}
@@ -75,7 +75,8 @@ namespace Terminal.Gui {
 					result = GetClipboardDataImpl ();
 					result = GetClipboardDataImpl ();
 				}
 				}
 				return true;
 				return true;
-			} catch (Exception) {
+			} catch (NotSupportedException ex) {
+				System.Diagnostics.Debug.WriteLine ($"TryGetClipboardData: {ex.Message}");
 				result = null;
 				result = null;
 				return false;
 				return false;
 			}
 			}
@@ -96,7 +97,7 @@ namespace Terminal.Gui {
 			try {
 			try {
 				SetClipboardDataImpl (text);
 				SetClipboardDataImpl (text);
 				return true;
 				return true;
-			} catch (Exception ex) {
+			} catch (NotSupportedException ex) {
 				System.Diagnostics.Debug.WriteLine ($"TrySetClipboardData: {ex.Message}");
 				System.Diagnostics.Debug.WriteLine ($"TrySetClipboardData: {ex.Message}");
 				return false;
 				return false;
 			}
 			}

+ 16 - 3
UnitTests/ClipboardTests.cs

@@ -12,7 +12,21 @@ namespace Terminal.Gui.ConsoleDrivers {
 		{
 		{
 			this.output = output;
 			this.output = output;
 		}
 		}
-		
+
+		[Fact, AutoInitShutdown (useFakeClipboard: true, fakeClipboardThrowsNotSupportedException: true)]
+		public void IClipboard_GetClipBoardData_Throws_NotSupportedException ()
+		{
+			IClipboard iclip = Application.Driver.Clipboard;
+			Assert.Throws<NotSupportedException> (() => iclip.GetClipboardData ());
+		}
+
+		[Fact, AutoInitShutdown (useFakeClipboard: true, fakeClipboardThrowsNotSupportedException: true)]
+		public void IClipboard_SetClipBoardData_Throws_NotSupportedException ()
+		{
+			IClipboard iclip = Application.Driver.Clipboard;
+			Assert.Throws<NotSupportedException> (() => iclip.SetClipboardData ("foo"));
+		}
+
 		[Fact, AutoInitShutdown (useFakeClipboard: false)]
 		[Fact, AutoInitShutdown (useFakeClipboard: false)]
 		public void Contents_Gets_Sets ()
 		public void Contents_Gets_Sets ()
 		{
 		{
@@ -87,7 +101,7 @@ namespace Terminal.Gui.ConsoleDrivers {
 				int exitCode = 0;
 				int exitCode = 0;
 				string result = "";
 				string result = "";
 				output.WriteLine ($"Setting OS clipboard to: {clipText}...");
 				output.WriteLine ($"Setting OS clipboard to: {clipText}...");
-				
+
 				if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
 				if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
 					(exitCode, result) = ClipboardProcessRunner.Process ("pwsh", $"-command \"Set-Clipboard -Value \\\"{clipText}\\\"\"");
 					(exitCode, result) = ClipboardProcessRunner.Process ("pwsh", $"-command \"Set-Clipboard -Value \\\"{clipText}\\\"\"");
 					output.WriteLine ($"  Windows: pwsh Set-Clipboard: exitCode = {exitCode}, result = {output}");
 					output.WriteLine ($"  Windows: pwsh Set-Clipboard: exitCode = {exitCode}, result = {output}");
@@ -142,7 +156,6 @@ namespace Terminal.Gui.ConsoleDrivers {
 			}
 			}
 		}
 		}
 
 
-
 		[Fact, AutoInitShutdown (useFakeClipboard: false)]
 		[Fact, AutoInitShutdown (useFakeClipboard: false)]
 		public void Contents_Sets_The_OS_Clipboard ()
 		public void Contents_Sets_The_OS_Clipboard ()
 		{
 		{

+ 1 - 1
UnitTests/DialogTests.cs

@@ -518,7 +518,7 @@ namespace Terminal.Gui.Views {
 		[AutoInitShutdown]
 		[AutoInitShutdown]
 		public void FileDialog_FileSystemWatcher ()
 		public void FileDialog_FileSystemWatcher ()
 		{
 		{
-			for (int i = 0; i < 256; i++) {
+			for (int i = 0; i < 8; i++) {
 				var fd = new FileDialog ();
 				var fd = new FileDialog ();
 				fd.Ready += () => Application.RequestStop ();
 				fd.Ready += () => Application.RequestStop ();
 				Application.Run (fd);
 				Application.Run (fd);

+ 4 - 2
UnitTests/TestHelpers.cs

@@ -22,11 +22,12 @@ using System.Diagnostics;
 [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
 [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
 public class AutoInitShutdownAttribute : Xunit.Sdk.BeforeAfterTestAttribute {
 public class AutoInitShutdownAttribute : Xunit.Sdk.BeforeAfterTestAttribute {
 
 
-	public AutoInitShutdownAttribute (bool autoInit = true, bool autoShutdown = true, bool useFakeClipboard = true)
+	public AutoInitShutdownAttribute (bool autoInit = true, bool autoShutdown = true, bool useFakeClipboard = true, bool fakeClipboardThrowsNotSupportedException = false)
 	{
 	{
 		this.AutoInit = autoInit;
 		this.AutoInit = autoInit;
 		this.AutoShutdown = autoShutdown;
 		this.AutoShutdown = autoShutdown;
 		this.UseFakeClipboard = useFakeClipboard;
 		this.UseFakeClipboard = useFakeClipboard;
+		this.FakeClipboardThrowsNotSupportedException = fakeClipboardThrowsNotSupportedException;
 	}
 	}
 
 
 	static bool _init = false;
 	static bool _init = false;
@@ -34,6 +35,7 @@ public class AutoInitShutdownAttribute : Xunit.Sdk.BeforeAfterTestAttribute {
 	public bool AutoInit { get; }
 	public bool AutoInit { get; }
 	public bool AutoShutdown { get; }
 	public bool AutoShutdown { get; }
 	public bool UseFakeClipboard { get; }
 	public bool UseFakeClipboard { get; }
+	public bool FakeClipboardThrowsNotSupportedException { get; }
 
 
 	public override void Before (MethodInfo methodUnderTest)
 	public override void Before (MethodInfo methodUnderTest)
 	{
 	{
@@ -41,7 +43,7 @@ public class AutoInitShutdownAttribute : Xunit.Sdk.BeforeAfterTestAttribute {
 			throw new InvalidOperationException ("After did not run when AutoShutdown was specified.");
 			throw new InvalidOperationException ("After did not run when AutoShutdown was specified.");
 		}
 		}
 		if (AutoInit) {
 		if (AutoInit) {
-			Application.Init (new FakeDriver (useFakeClipboard: UseFakeClipboard), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
+			Application.Init (new FakeDriver (useFakeClipboard: UseFakeClipboard, fakeClipboardThrows: FakeClipboardThrowsNotSupportedException), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
 			_init = true;
 			_init = true;
 		}
 		}
 	}
 	}