IClipboard tests for NotSupportedException

This commit is contained in:
Charlie Kindel
2022-11-08 20:44:04 -07:00
parent 66184a09c1
commit aca4ab3eae
6 changed files with 45 additions and 17 deletions

View File

@@ -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;
}
}

View File

@@ -28,7 +28,7 @@ namespace Terminal.Gui {
static ustring contents;
/// <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>
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.
/// </summary>
/// <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>
public static bool IsSupported { get => Application.Driver.Clipboard.IsSupported; }

View File

@@ -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;
}

View File

@@ -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<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)]
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 ()
{

View File

@@ -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);

View File

@@ -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;
}
}