ClipboardTests.cs 8.3 KB

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