123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- using System.Diagnostics;
- using System.Runtime.InteropServices;
- using Xunit;
- namespace Terminal.Gui.Core {
- public class ClipboardTests {
- [Fact]
- public void Contents_Gets_Sets ()
- {
- Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
- var clipText = "This is a clipboard unit test.";
- Clipboard.Contents = clipText;
- Application.Iteration += () => Application.RequestStop ();
- Application.Run ();
- Assert.Equal (clipText, Clipboard.Contents);
- Application.Shutdown ();
- }
- [Fact]
- public void IsSupported_Get ()
- {
- Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
- if (Clipboard.IsSupported) {
- Assert.True (Clipboard.IsSupported);
- } else {
- Assert.False (Clipboard.IsSupported);
- }
- Application.Shutdown ();
- }
- [Fact]
- public void TryGetClipboardData_Gets_From_OS_Clipboard ()
- {
- Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
- 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);
- }
- Application.Shutdown ();
- }
- [Fact]
- public void TrySetClipboardData_Sets_The_OS_Clipboard ()
- {
- Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
- 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);
- }
- Application.Shutdown ();
- }
- [Fact]
- public void Contents_Gets_From_OS_Clipboard ()
- {
- Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
- var clipText = "This is a clipboard unit test to get clipboard from OS.";
- var exit = false;
- Application.Iteration += () => {
- if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
- // using (Process clipExe = new Process {
- // StartInfo = new ProcessStartInfo {
- // RedirectStandardInput = true,
- // FileName = "clip"
- // }
- // }) {
- // clipExe.Start ();
- // clipExe.StandardInput.Write (clipText);
- // clipExe.StandardInput.Close ();
- // var result = clipExe.WaitForExit (500);
- // if (result) {
- // clipExe.WaitForExit ();
- // }
- // }
- using (Process pwsh = new Process {
- StartInfo = new ProcessStartInfo {
- FileName = "powershell",
- Arguments = $"-command \"Set-Clipboard -Value \\\"{clipText}\\\"\""
- }
- }) {
- pwsh.Start ();
- pwsh.WaitForExit ();
- }
- } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
- using (Process copy = new Process {
- StartInfo = new ProcessStartInfo {
- RedirectStandardInput = true,
- FileName = "pbcopy"
- }
- }) {
- copy.Start ();
- copy.StandardInput.Write (clipText);
- copy.StandardInput.Close ();
- copy.WaitForExit ();
- }
- } else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux)) {
- if (Is_WSL_Platform ()) {
- try {
- using (Process bash = new Process {
- StartInfo = new ProcessStartInfo {
- FileName = "powershell.exe",
- Arguments = $"-noprofile -command \"Set-Clipboard -Value \\\"{clipText}\\\"\""
- }
- }) {
- bash.Start ();
- bash.WaitForExit ();
- }
- //using (Process clipExe = new Process {
- // StartInfo = new ProcessStartInfo {
- // RedirectStandardInput = true,
- // FileName = "clip.exe"
- // }
- //}) {
- // clipExe.Start ();
- // clipExe.StandardInput.Write (clipText);
- // clipExe.StandardInput.Close ();
- // clipExe.WaitForExit ();
- // //var result = clipExe.WaitForExit (500);
- // //if (result) {
- // // clipExe.WaitForExit ();
- // //}
- //}
- } catch {
- exit = true;
- }
- Application.RequestStop ();
- return;
- }
- if (exit = xclipExists () == false) {
- // xclip doesn't exist then exit.
- Application.RequestStop ();
- return;
- }
- using (Process bash = new Process {
- StartInfo = new ProcessStartInfo {
- FileName = "bash",
- Arguments = $"-c \"xclip -sel clip -i\"",
- RedirectStandardInput = true,
- }
- }) {
- bash.Start ();
- bash.StandardInput.Write (clipText);
- bash.StandardInput.Close ();
- bash.WaitForExit ();
- }
- }
- Application.RequestStop ();
- };
- Application.Run ();
- if (!exit) {
- Assert.Equal (clipText, Clipboard.Contents);
- }
- Application.Shutdown ();
- }
- [Fact]
- public void Contents_Sets_The_OS_Clipboard ()
- {
- Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
- var clipText = "This is a clipboard unit test to set the OS clipboard.";
- var clipReadText = "";
- var exit = false;
- Application.Iteration += () => {
- Clipboard.Contents = clipText;
- if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
- using (Process pwsh = new Process {
- StartInfo = new ProcessStartInfo {
- RedirectStandardOutput = true,
- FileName = "powershell.exe",
- Arguments = "-noprofile -command \"Get-Clipboard\""
- }
- }) {
- pwsh.Start ();
- clipReadText = pwsh.StandardOutput.ReadToEnd ().TrimEnd ();
- pwsh.StandardOutput.Close ();
- pwsh.WaitForExit ();
- }
- } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
- using (Process paste = new Process {
- StartInfo = new ProcessStartInfo {
- RedirectStandardOutput = true,
- FileName = "pbpaste"
- }
- }) {
- paste.Start ();
- clipReadText = paste.StandardOutput.ReadToEnd ();
- paste.StandardOutput.Close ();
- paste.WaitForExit ();
- }
- } else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux)) {
- if (Is_WSL_Platform ()) {
- try {
- using (Process bash = new Process {
- StartInfo = new ProcessStartInfo {
- RedirectStandardOutput = true,
- FileName = "powershell.exe",
- Arguments = "-noprofile -command \"Get-Clipboard\""
- }
- }) {
- bash.Start ();
- clipReadText = bash.StandardOutput.ReadToEnd ();
- bash.StandardOutput.Close ();
- bash.WaitForExit ();
- }
- } catch {
- exit = true;
- }
- Application.RequestStop ();
- }
- if (exit = xclipExists () == false) {
- // xclip doesn't exist then exit.
- Application.RequestStop ();
- }
- using (Process bash = new Process {
- StartInfo = new ProcessStartInfo {
- RedirectStandardOutput = true,
- FileName = "bash",
- Arguments = $"-c \"xclip -sel clip -o\""
- }
- }) {
- bash.Start ();
- clipReadText = bash.StandardOutput.ReadToEnd ();
- bash.StandardOutput.Close ();
- bash.WaitForExit ();
- }
- }
- Application.RequestStop ();
- };
- Application.Run ();
- if (!exit) {
- Assert.Equal (clipText, clipReadText);
- }
- Application.Shutdown ();
- }
- bool Is_WSL_Platform ()
- {
- using (Process bash = new Process {
- StartInfo = new ProcessStartInfo {
- FileName = "bash",
- Arguments = $"-c \"uname -a\"",
- RedirectStandardOutput = true,
- }
- }) {
- bash.Start ();
- var result = bash.StandardOutput.ReadToEnd ();
- var isWSL = false;
- if (result.Contains ("microsoft") && result.Contains ("WSL")) {
- isWSL = true;
- }
- bash.StandardOutput.Close ();
- bash.WaitForExit ();
- return isWSL;
- }
- }
- bool xclipExists ()
- {
- try {
- using (Process bash = new Process {
- StartInfo = new ProcessStartInfo {
- FileName = "bash",
- Arguments = $"-c \"which xclip\"",
- RedirectStandardOutput = true,
- }
- }) {
- bash.Start ();
- bool exist = bash.StandardOutput.ReadToEnd ().TrimEnd () != "";
- bash.StandardOutput.Close ();
- bash.WaitForExit ();
- if (exist) {
- return true;
- }
- }
- return false;
- } catch (System.Exception) {
- return false;
- }
- }
- }
- }
|