ClipboardTests.cs 8.1 KB

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