ClipboardTests.cs 8.3 KB

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