123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- using System;
- using System.Diagnostics;
- using System.Runtime.InteropServices;
- using Xunit;
- using Xunit.Abstractions;
- namespace Terminal.Gui.ConsoleDrivers {
- public class ClipboardTests {
- [Fact, AutoInitShutdown (useFakeClipboard: false)]
- public void Contents_Gets_Sets ()
- {
- var clipText = "This is a clipboard unit test.";
- Clipboard.Contents = clipText;
- Application.Iteration += () => Application.RequestStop ();
- Application.Run ();
- Assert.Equal (clipText, Clipboard.Contents);
- }
- [Fact, AutoInitShutdown (useFakeClipboard: false)]
- public void IsSupported_Get ()
- {
- if (Clipboard.IsSupported) {
- Assert.True (Clipboard.IsSupported);
- } else {
- Assert.False (Clipboard.IsSupported);
- }
- }
- [Fact, AutoInitShutdown (useFakeClipboard: false)]
- public void TryGetClipboardData_Gets_From_OS_Clipboard ()
- {
- var clipText = "Trying to get from the OS clipboard.";
- Clipboard.Contents = clipText;
- Application.Iteration += () => Application.RequestStop ();
- Application.Run ();
- if (Clipboard.IsSupported) {
- Assert.True (Clipboard.TryGetClipboardData (out string result));
- Assert.Equal (clipText, result);
- } else {
- Assert.False (Clipboard.TryGetClipboardData (out string result));
- Assert.NotEqual (clipText, result);
- }
- }
- [Fact, AutoInitShutdown (useFakeClipboard: false)]
- public void TrySetClipboardData_Sets_The_OS_Clipboard ()
- {
- var clipText = "Trying to set the OS clipboard.";
- if (Clipboard.IsSupported) {
- Assert.True (Clipboard.TrySetClipboardData (clipText));
- } else {
- Assert.False (Clipboard.TrySetClipboardData (clipText));
- }
- Application.Iteration += () => Application.RequestStop ();
- Application.Run ();
- if (Clipboard.IsSupported) {
- Assert.Equal (clipText, Clipboard.Contents);
- } else {
- Assert.NotEqual (clipText, Clipboard.Contents);
- }
- }
- private static string RunClipboardProcess (string cmd, string args, string writeText = null)
- {
- string output = string.Empty;
- using (Process process = new Process {
- StartInfo = new ProcessStartInfo {
- FileName = cmd,
- Arguments = args,
- RedirectStandardOutput = true,
- RedirectStandardError = true,
- RedirectStandardInput = true
- }
- }) {
- process.Start ();
- if (string.IsNullOrEmpty (writeText)) {
- process.StandardInput.Write (writeText);
- process.StandardInput.Close ();
- }
- process.WaitForExit ();
- if (process.ExitCode > 0) {
- var error = $@"RunClipboardProcess failed. Command line: {cmd} {args}.
- Output: {process.StandardOutput.ReadToEnd ()}
- Error: {process.StandardError.ReadToEnd ()}";
- throw new InvalidOperationException (error);
- }
- output = process.StandardOutput.ReadToEnd ().TrimEnd ();
- process.StandardOutput.Close ();
- }
- return output;
- }
- [Fact, AutoInitShutdown (useFakeClipboard: false)]
- public void Contents_Gets_From_OS_Clipboard ()
- {
- var clipText = "This is a clipboard unit test to get clipboard from OS.";
- var failed = false;
- var getClipText = "";
- Application.Iteration += () => {
- if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
- RunClipboardProcess ("pwsh", $"-command \"Set-Clipboard -Value \\\"{clipText}\\\"\"");
- getClipText = Clipboard.Contents.ToString ();
- } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
- RunClipboardProcess ("pbcopy", string.Empty, clipText);
- getClipText = Clipboard.Contents.ToString ();
- } else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux)) {
- if (Is_WSL_Platform ()) {
- try {
- // This runs the WINDOWS version of powershell.exe via WSL.
- RunClipboardProcess ("powershell.exe", $"-noprofile -command \"Set-Clipboard -Value \\\"{clipText}\\\"\"");
- } catch {
- failed = true;
- }
- if (!failed) {
- getClipText = Clipboard.Contents.ToString ();
- }
- Application.RequestStop ();
- return;
- }
- if (failed = xclipExists () == false) {
- // xclip doesn't exist then exit.
- Application.RequestStop ();
- return;
- }
- // If we get here, powershell didn't work and xclip exists...
- RunClipboardProcess ("bash", $"-c \"xclip -sel clip -i\"", clipText);
- if (!failed) {
- getClipText = Clipboard.Contents.ToString ();
- }
- }
- Application.RequestStop ();
- };
- Application.Run ();
- if (!failed) {
- Assert.Equal (clipText, getClipText);
- }
- }
- [Fact, AutoInitShutdown (useFakeClipboard: false)]
- public void Contents_Sets_The_OS_Clipboard ()
- {
- var clipText = "This is a clipboard unit test to set the OS clipboard.";
- var clipReadText = "";
- var failed = false;
- Application.Iteration += () => {
- Clipboard.Contents = clipText;
- if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
- clipReadText = RunClipboardProcess ("pwsh", "-noprofile -command \"Get-Clipboard\"");
- } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
- clipReadText = RunClipboardProcess ("pbpaste", "");
- } else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux)) {
- if (Is_WSL_Platform ()) {
- try {
- clipReadText = RunClipboardProcess ("/opt/microsoft/powershell/7/pwsh", "-noprofile -command \"Get-Clipboard\"");
- } catch {
- failed = true;
- }
- Application.RequestStop ();
- }
- if (failed = xclipExists () == false) {
- // xclip doesn't exist then exit.
- Application.RequestStop ();
- return;
- }
- clipReadText = RunClipboardProcess ("bash", $"-c \"xclip -sel clip -o\"");
- }
- Application.RequestStop ();
- };
- Application.Run ();
- if (!failed) {
- Assert.Equal (clipText, clipReadText);
- }
- }
- bool Is_WSL_Platform ()
- {
- var result = RunClipboardProcess ("bash", $"-c \"uname -a\"");
- return result.Contains ("microsoft") && result.Contains ("WSL");
- }
- bool xclipExists ()
- {
- try {
- var result = RunClipboardProcess ("bash", $"-c \"which xclip\"");
- return result.TrimEnd () != "";
- } catch (System.Exception) {
- return false;
- }
- }
- }
- }
|