From aca4ab3eaedcd8e6e44e9a4746f79c271f826502 Mon Sep 17 00:00:00 2001 From: Charlie Kindel Date: Tue, 8 Nov 2022 20:44:04 -0700 Subject: [PATCH] IClipboard tests for NotSupportedException --- .../ConsoleDrivers/FakeDriver/FakeDriver.cs | 20 ++++++++++++++++--- Terminal.Gui/Core/Clipboard/Clipboard.cs | 4 +--- Terminal.Gui/Core/Clipboard/ClipboardBase.cs | 11 +++++----- UnitTests/ClipboardTests.cs | 19 +++++++++++++++--- UnitTests/DialogTests.cs | 2 +- UnitTests/TestHelpers.cs | 6 ++++-- 6 files changed, 45 insertions(+), 17 deletions(-) diff --git a/Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs b/Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs index 95e04af4a..56cb24ced 100644 --- a/Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs @@ -58,12 +58,12 @@ namespace Terminal.Gui { static bool sync = false; static public bool usingFakeClipboard; - - public FakeDriver (bool useFakeClipboard = true) + + public FakeDriver (bool useFakeClipboard = true, bool fakeClipboardThrows = false) { usingFakeClipboard = useFakeClipboard; if (usingFakeClipboard) { - clipboard = new FakeClipboard (); + clipboard = new FakeClipboard (fakeClipboardThrows); } else { if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) { clipboard = new WindowsClipboard (); @@ -651,16 +651,30 @@ namespace Terminal.Gui { public class FakeClipboard : ClipboardBase { public override bool IsSupported => true; + public Exception FakeException = null; string contents = string.Empty; + public FakeClipboard (bool fakeClipboardThrowsNotSupportedException = false) + { + if (fakeClipboardThrowsNotSupportedException) { + FakeException = new NotSupportedException ("Fake clipboard exception"); + } + } + protected override string GetClipboardDataImpl () { + if (FakeException != null) { + throw FakeException; + } return contents; } protected override void SetClipboardDataImpl (string text) { + if (FakeException != null) { + throw FakeException; + } contents = text; } } diff --git a/Terminal.Gui/Core/Clipboard/Clipboard.cs b/Terminal.Gui/Core/Clipboard/Clipboard.cs index f8631c167..0c6b6b6ff 100644 --- a/Terminal.Gui/Core/Clipboard/Clipboard.cs +++ b/Terminal.Gui/Core/Clipboard/Clipboard.cs @@ -28,7 +28,7 @@ namespace Terminal.Gui { static ustring contents; /// - /// Get or sets the operation system clipboard, otherwise the contents field. + /// Gets (copies from) or sets (pastes to) the contents of the OS clipboard. /// public static ustring Contents { get { @@ -65,8 +65,6 @@ namespace Terminal.Gui { /// Returns true if the environmental dependencies are in place to interact with the OS clipboard. /// /// - /// 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. /// public static bool IsSupported { get => Application.Driver.Clipboard.IsSupported; } diff --git a/Terminal.Gui/Core/Clipboard/ClipboardBase.cs b/Terminal.Gui/Core/Clipboard/ClipboardBase.cs index fccbe1519..ee2e437d4 100644 --- a/Terminal.Gui/Core/Clipboard/ClipboardBase.cs +++ b/Terminal.Gui/Core/Clipboard/ClipboardBase.cs @@ -23,8 +23,8 @@ namespace Terminal.Gui { { try { 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 { SetClipboardDataImpl (text); - } catch (Exception ex) { + } catch (NotSupportedException ex) { throw new NotSupportedException ("Failed to paste to the OS clipboard.", ex); } } @@ -75,7 +75,8 @@ namespace Terminal.Gui { result = GetClipboardDataImpl (); } return true; - } catch (Exception) { + } catch (NotSupportedException ex) { + System.Diagnostics.Debug.WriteLine ($"TryGetClipboardData: {ex.Message}"); result = null; return false; } @@ -96,7 +97,7 @@ namespace Terminal.Gui { try { SetClipboardDataImpl (text); return true; - } catch (Exception ex) { + } catch (NotSupportedException ex) { System.Diagnostics.Debug.WriteLine ($"TrySetClipboardData: {ex.Message}"); return false; } diff --git a/UnitTests/ClipboardTests.cs b/UnitTests/ClipboardTests.cs index e7f84b60f..0b8ab2aa4 100644 --- a/UnitTests/ClipboardTests.cs +++ b/UnitTests/ClipboardTests.cs @@ -12,7 +12,21 @@ namespace Terminal.Gui.ConsoleDrivers { { this.output = output; } - + + [Fact, AutoInitShutdown (useFakeClipboard: true, fakeClipboardThrowsNotSupportedException: true)] + public void IClipboard_GetClipBoardData_Throws_NotSupportedException () + { + IClipboard iclip = Application.Driver.Clipboard; + Assert.Throws (() => iclip.GetClipboardData ()); + } + + [Fact, AutoInitShutdown (useFakeClipboard: true, fakeClipboardThrowsNotSupportedException: true)] + public void IClipboard_SetClipBoardData_Throws_NotSupportedException () + { + IClipboard iclip = Application.Driver.Clipboard; + Assert.Throws (() => iclip.SetClipboardData ("foo")); + } + [Fact, AutoInitShutdown (useFakeClipboard: false)] public void Contents_Gets_Sets () { @@ -87,7 +101,7 @@ namespace Terminal.Gui.ConsoleDrivers { int exitCode = 0; string result = ""; output.WriteLine ($"Setting OS clipboard to: {clipText}..."); - + if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) { (exitCode, result) = ClipboardProcessRunner.Process ("pwsh", $"-command \"Set-Clipboard -Value \\\"{clipText}\\\"\""); output.WriteLine ($" Windows: pwsh Set-Clipboard: exitCode = {exitCode}, result = {output}"); @@ -142,7 +156,6 @@ namespace Terminal.Gui.ConsoleDrivers { } } - [Fact, AutoInitShutdown (useFakeClipboard: false)] public void Contents_Sets_The_OS_Clipboard () { diff --git a/UnitTests/DialogTests.cs b/UnitTests/DialogTests.cs index 929b4ddf6..9449ce3ea 100644 --- a/UnitTests/DialogTests.cs +++ b/UnitTests/DialogTests.cs @@ -518,7 +518,7 @@ namespace Terminal.Gui.Views { [AutoInitShutdown] public void FileDialog_FileSystemWatcher () { - for (int i = 0; i < 256; i++) { + for (int i = 0; i < 8; i++) { var fd = new FileDialog (); fd.Ready += () => Application.RequestStop (); Application.Run (fd); diff --git a/UnitTests/TestHelpers.cs b/UnitTests/TestHelpers.cs index cc9f663c8..5bf54a923 100644 --- a/UnitTests/TestHelpers.cs +++ b/UnitTests/TestHelpers.cs @@ -22,11 +22,12 @@ using System.Diagnostics; [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] 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.AutoShutdown = autoShutdown; this.UseFakeClipboard = useFakeClipboard; + this.FakeClipboardThrowsNotSupportedException = fakeClipboardThrowsNotSupportedException; } static bool _init = false; @@ -34,6 +35,7 @@ public class AutoInitShutdownAttribute : Xunit.Sdk.BeforeAfterTestAttribute { public bool AutoInit { get; } public bool AutoShutdown { get; } public bool UseFakeClipboard { get; } + public bool FakeClipboardThrowsNotSupportedException { get; } 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."); } 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; } }