ClipboardTests.cs 8.3 KB

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