ClipboardTests.cs 8.4 KB

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