ClipboardTests.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System.Diagnostics;
  2. using System.Runtime.InteropServices;
  3. using Xunit;
  4. namespace Terminal.Gui.Core {
  5. public class ClipboardTests {
  6. [Fact]
  7. public void Contents_Gets_Sets ()
  8. {
  9. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  10. var clipText = "This is a clipboard unit test.";
  11. Clipboard.Contents = clipText;
  12. Assert.Equal (clipText, Clipboard.Contents);
  13. Application.Shutdown ();
  14. }
  15. [Fact]
  16. public void Contents_Gets_From_OS_Clipboard ()
  17. {
  18. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  19. var clipText = "This is a clipboard unit test to get clipboard from OS.";
  20. var exit = false;
  21. Application.Iteration += () => {
  22. if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
  23. // using (Process clipExe = new Process {
  24. // StartInfo = new ProcessStartInfo {
  25. // RedirectStandardInput = true,
  26. // FileName = "clip"
  27. // }
  28. // }) {
  29. // clipExe.Start ();
  30. // clipExe.StandardInput.Write (clipText);
  31. // clipExe.StandardInput.Close ();
  32. // var result = clipExe.WaitForExit (500);
  33. // if (result) {
  34. // clipExe.WaitForExit ();
  35. // }
  36. // }
  37. using (Process pwsh = new Process {
  38. StartInfo = new ProcessStartInfo {
  39. FileName = "powershell",
  40. Arguments = $"-command \"Set-Clipboard -Value \\\"{clipText}\\\"\""
  41. }
  42. }) {
  43. pwsh.Start ();
  44. pwsh.WaitForExit ();
  45. }
  46. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
  47. using (Process copy = new Process {
  48. StartInfo = new ProcessStartInfo {
  49. RedirectStandardInput = true,
  50. FileName = "pbcopy"
  51. }
  52. }) {
  53. copy.Start ();
  54. copy.StandardInput.Write (clipText);
  55. copy.StandardInput.Close ();
  56. copy.WaitForExit ();
  57. }
  58. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux)) {
  59. if (exit = xclipExists () == false) {
  60. // xclip doesn't exist then exit.
  61. Application.RequestStop ();
  62. }
  63. using (Process bash = new Process {
  64. StartInfo = new ProcessStartInfo {
  65. FileName = "bash",
  66. Arguments = $"-c \"xclip -sel clip -i\"",
  67. RedirectStandardInput = true,
  68. }
  69. }) {
  70. bash.Start ();
  71. bash.StandardInput.Write (clipText);
  72. bash.StandardInput.Close ();
  73. bash.WaitForExit ();
  74. }
  75. }
  76. Application.RequestStop ();
  77. };
  78. Application.Run ();
  79. if (!exit) {
  80. Assert.Equal (clipText, Clipboard.Contents);
  81. }
  82. Application.Shutdown ();
  83. }
  84. [Fact]
  85. public void Contents_Sets_The_OS_Clipboard ()
  86. {
  87. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  88. var clipText = "This is a clipboard unit test to set the OS clipboard.";
  89. var clipReadText = "";
  90. var exit = false;
  91. Application.Iteration += () => {
  92. Clipboard.Contents = clipText;
  93. if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
  94. using (Process pwsh = new Process {
  95. StartInfo = new ProcessStartInfo {
  96. RedirectStandardOutput = true,
  97. FileName = "powershell.exe",
  98. Arguments = "-command \"Get-Clipboard\""
  99. }
  100. }) {
  101. pwsh.Start ();
  102. clipReadText = pwsh.StandardOutput.ReadToEnd ().TrimEnd ();
  103. pwsh.StandardOutput.Close ();
  104. pwsh.WaitForExit ();
  105. }
  106. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
  107. using (Process paste = new Process {
  108. StartInfo = new ProcessStartInfo {
  109. RedirectStandardOutput = true,
  110. FileName = "pbpaste"
  111. }
  112. }) {
  113. paste.Start ();
  114. clipReadText = paste.StandardOutput.ReadToEnd ();
  115. paste.StandardOutput.Close ();
  116. paste.WaitForExit ();
  117. }
  118. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux)) {
  119. if (exit = xclipExists () == false) {
  120. // xclip doesn't exist then exit.
  121. Application.RequestStop ();
  122. }
  123. using (Process bash = new Process {
  124. StartInfo = new ProcessStartInfo {
  125. RedirectStandardOutput = true,
  126. FileName = "bash",
  127. Arguments = $"-c \"xclip -sel clip -o\""
  128. }
  129. }) {
  130. bash.Start ();
  131. clipReadText = bash.StandardOutput.ReadToEnd ();
  132. bash.StandardOutput.Close ();
  133. bash.WaitForExit ();
  134. }
  135. }
  136. Application.RequestStop ();
  137. };
  138. Application.Run ();
  139. if (!exit) {
  140. Assert.Equal (clipText, clipReadText);
  141. }
  142. Application.Shutdown ();
  143. }
  144. bool xclipExists ()
  145. {
  146. using (Process bash = new Process {
  147. StartInfo = new ProcessStartInfo {
  148. FileName = "bash",
  149. Arguments = $"-c \"which xclip\"",
  150. RedirectStandardOutput = true,
  151. }
  152. }) {
  153. bash.Start ();
  154. bool exist = bash.StandardOutput.ReadToEnd ().TrimEnd () != "";
  155. bash.StandardOutput.Close ();
  156. bash.WaitForExit ();
  157. if (exist) {
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. }
  164. }