ClipboardTests.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. Application.Iteration += () => Application.RequestStop ();
  13. Application.Run ();
  14. Assert.Equal (clipText, Clipboard.Contents);
  15. Application.Shutdown ();
  16. }
  17. [Fact]
  18. public void IsSupported_Get ()
  19. {
  20. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  21. Assert.True (Clipboard.IsSupported);
  22. Application.Shutdown ();
  23. }
  24. [Fact]
  25. public void TryGetClipboardData_Gets_From_OS_Clipboard ()
  26. {
  27. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  28. var clipText = "Trying to get from the OS clipboard.";
  29. Clipboard.Contents = clipText;
  30. Application.Iteration += () => Application.RequestStop ();
  31. Application.Run ();
  32. if (Clipboard.IsSupported) {
  33. Assert.True (Clipboard.TryGetClipboardData (out string result));
  34. Assert.Equal (clipText, result);
  35. } else {
  36. Assert.False (Clipboard.TryGetClipboardData (out string result));
  37. Assert.NotEqual (clipText, result);
  38. }
  39. Application.Shutdown ();
  40. }
  41. [Fact]
  42. public void TrySetClipboardData_Sets_The_OS_Clipboard ()
  43. {
  44. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  45. var clipText = "Trying to set the OS clipboard.";
  46. Assert.True (Clipboard.TrySetClipboardData (clipText));
  47. Application.Iteration += () => Application.RequestStop ();
  48. Application.Run ();
  49. Assert.Equal (clipText, Clipboard.Contents);
  50. Application.Shutdown ();
  51. }
  52. [Fact]
  53. public void Contents_Gets_From_OS_Clipboard ()
  54. {
  55. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  56. var clipText = "This is a clipboard unit test to get clipboard from OS.";
  57. var exit = false;
  58. Application.Iteration += () => {
  59. if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
  60. // using (Process clipExe = new Process {
  61. // StartInfo = new ProcessStartInfo {
  62. // RedirectStandardInput = true,
  63. // FileName = "clip"
  64. // }
  65. // }) {
  66. // clipExe.Start ();
  67. // clipExe.StandardInput.Write (clipText);
  68. // clipExe.StandardInput.Close ();
  69. // var result = clipExe.WaitForExit (500);
  70. // if (result) {
  71. // clipExe.WaitForExit ();
  72. // }
  73. // }
  74. using (Process pwsh = new Process {
  75. StartInfo = new ProcessStartInfo {
  76. FileName = "powershell",
  77. Arguments = $"-command \"Set-Clipboard -Value \\\"{clipText}\\\"\""
  78. }
  79. }) {
  80. pwsh.Start ();
  81. pwsh.WaitForExit ();
  82. }
  83. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
  84. using (Process copy = new Process {
  85. StartInfo = new ProcessStartInfo {
  86. RedirectStandardInput = true,
  87. FileName = "pbcopy"
  88. }
  89. }) {
  90. copy.Start ();
  91. copy.StandardInput.Write (clipText);
  92. copy.StandardInput.Close ();
  93. copy.WaitForExit ();
  94. }
  95. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux)) {
  96. if (Is_WSL_Platform ()) {
  97. try {
  98. using (Process bash = new Process {
  99. StartInfo = new ProcessStartInfo {
  100. FileName = "powershell.exe",
  101. Arguments = $"-command \"Set-Clipboard -Value \\\"{clipText}\\\"\""
  102. }
  103. }) {
  104. bash.Start ();
  105. bash.WaitForExit ();
  106. }
  107. } catch {
  108. exit = true;
  109. }
  110. Application.RequestStop ();
  111. return;
  112. }
  113. if (exit = xclipExists () == false) {
  114. // xclip doesn't exist then exit.
  115. Application.RequestStop ();
  116. return;
  117. }
  118. using (Process bash = new Process {
  119. StartInfo = new ProcessStartInfo {
  120. FileName = "bash",
  121. Arguments = $"-c \"xclip -sel clip -i\"",
  122. RedirectStandardInput = true,
  123. }
  124. }) {
  125. bash.Start ();
  126. bash.StandardInput.Write (clipText);
  127. bash.StandardInput.Close ();
  128. bash.WaitForExit ();
  129. }
  130. }
  131. Application.RequestStop ();
  132. };
  133. Application.Run ();
  134. if (!exit) {
  135. Assert.Equal (clipText, Clipboard.Contents);
  136. }
  137. Application.Shutdown ();
  138. }
  139. [Fact]
  140. public void Contents_Sets_The_OS_Clipboard ()
  141. {
  142. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  143. var clipText = "This is a clipboard unit test to set the OS clipboard.";
  144. var clipReadText = "";
  145. var exit = false;
  146. Application.Iteration += () => {
  147. Clipboard.Contents = clipText;
  148. if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
  149. using (Process pwsh = new Process {
  150. StartInfo = new ProcessStartInfo {
  151. RedirectStandardOutput = true,
  152. FileName = "powershell.exe",
  153. Arguments = "-command \"Get-Clipboard\""
  154. }
  155. }) {
  156. pwsh.Start ();
  157. clipReadText = pwsh.StandardOutput.ReadToEnd ().TrimEnd ();
  158. pwsh.StandardOutput.Close ();
  159. pwsh.WaitForExit ();
  160. }
  161. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
  162. using (Process paste = new Process {
  163. StartInfo = new ProcessStartInfo {
  164. RedirectStandardOutput = true,
  165. FileName = "pbpaste"
  166. }
  167. }) {
  168. paste.Start ();
  169. clipReadText = paste.StandardOutput.ReadToEnd ();
  170. paste.StandardOutput.Close ();
  171. paste.WaitForExit ();
  172. }
  173. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux)) {
  174. if (Is_WSL_Platform ()) {
  175. try {
  176. using (Process bash = new Process {
  177. StartInfo = new ProcessStartInfo {
  178. RedirectStandardOutput = true,
  179. FileName = "powershell.exe",
  180. Arguments = "-command \"Get-Clipboard\""
  181. }
  182. }) {
  183. bash.Start ();
  184. clipReadText = bash.StandardOutput.ReadToEnd ();
  185. bash.StandardOutput.Close ();
  186. bash.WaitForExit ();
  187. }
  188. } catch {
  189. exit = true;
  190. }
  191. Application.RequestStop ();
  192. }
  193. if (exit = xclipExists () == false) {
  194. // xclip doesn't exist then exit.
  195. Application.RequestStop ();
  196. }
  197. using (Process bash = new Process {
  198. StartInfo = new ProcessStartInfo {
  199. RedirectStandardOutput = true,
  200. FileName = "bash",
  201. Arguments = $"-c \"xclip -sel clip -o\""
  202. }
  203. }) {
  204. bash.Start ();
  205. clipReadText = bash.StandardOutput.ReadToEnd ();
  206. bash.StandardOutput.Close ();
  207. bash.WaitForExit ();
  208. }
  209. }
  210. Application.RequestStop ();
  211. };
  212. Application.Run ();
  213. if (!exit) {
  214. Assert.Equal (clipText, clipReadText);
  215. }
  216. Application.Shutdown ();
  217. }
  218. bool Is_WSL_Platform ()
  219. {
  220. using (Process bash = new Process {
  221. StartInfo = new ProcessStartInfo {
  222. FileName = "bash",
  223. Arguments = $"-c \"uname -a\"",
  224. RedirectStandardOutput = true,
  225. }
  226. }) {
  227. bash.Start ();
  228. var result = bash.StandardOutput.ReadToEnd ();
  229. var isWSL = false;
  230. if (result.Contains ("microsoft") && result.Contains ("WSL")) {
  231. isWSL = true;
  232. }
  233. bash.StandardOutput.Close ();
  234. bash.WaitForExit ();
  235. return isWSL;
  236. }
  237. }
  238. bool xclipExists ()
  239. {
  240. using (Process bash = new Process {
  241. StartInfo = new ProcessStartInfo {
  242. FileName = "bash",
  243. Arguments = $"-c \"which xclip\"",
  244. RedirectStandardOutput = true,
  245. }
  246. }) {
  247. bash.Start ();
  248. bool exist = bash.StandardOutput.ReadToEnd ().TrimEnd () != "";
  249. bash.StandardOutput.Close ();
  250. bash.WaitForExit ();
  251. if (exist) {
  252. return true;
  253. }
  254. }
  255. return false;
  256. }
  257. }
  258. }