ClipboardTests.cs 8.5 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. public void Contents_Gets_Sets ()
  8. {
  9. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  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. Application.Shutdown ();
  16. }
  17. [Fact]
  18. public void IsSupported_Get ()
  19. {
  20. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  21. if (Clipboard.IsSupported) {
  22. Assert.True (Clipboard.IsSupported);
  23. } else {
  24. Assert.False (Clipboard.IsSupported);
  25. }
  26. Application.Shutdown ();
  27. }
  28. [Fact]
  29. public void TryGetClipboardData_Gets_From_OS_Clipboard ()
  30. {
  31. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  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. Application.Shutdown ();
  44. }
  45. [Fact]
  46. public void TrySetClipboardData_Sets_The_OS_Clipboard ()
  47. {
  48. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  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. Application.Shutdown ();
  63. }
  64. [Fact]
  65. public void Contents_Gets_From_OS_Clipboard ()
  66. {
  67. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  68. var clipText = "This is a clipboard unit test to get clipboard from OS.";
  69. var exit = false;
  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. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
  96. using (Process copy = new Process {
  97. StartInfo = new ProcessStartInfo {
  98. RedirectStandardInput = true,
  99. FileName = "pbcopy"
  100. }
  101. }) {
  102. copy.Start ();
  103. copy.StandardInput.Write (clipText);
  104. copy.StandardInput.Close ();
  105. copy.WaitForExit ();
  106. }
  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. Application.RequestStop ();
  138. return;
  139. }
  140. if (exit = xclipExists () == false) {
  141. // xclip doesn't exist then exit.
  142. Application.RequestStop ();
  143. return;
  144. }
  145. using (Process bash = new Process {
  146. StartInfo = new ProcessStartInfo {
  147. FileName = "bash",
  148. Arguments = $"-c \"xclip -sel clip -i\"",
  149. RedirectStandardInput = true,
  150. }
  151. }) {
  152. bash.Start ();
  153. bash.StandardInput.Write (clipText);
  154. bash.StandardInput.Close ();
  155. bash.WaitForExit ();
  156. }
  157. }
  158. Application.RequestStop ();
  159. };
  160. Application.Run ();
  161. if (!exit) {
  162. Assert.Equal (clipText, Clipboard.Contents);
  163. }
  164. Application.Shutdown ();
  165. }
  166. [Fact]
  167. public void Contents_Sets_The_OS_Clipboard ()
  168. {
  169. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  170. var clipText = "This is a clipboard unit test to set the OS clipboard.";
  171. var clipReadText = "";
  172. var exit = false;
  173. Application.Iteration += () => {
  174. Clipboard.Contents = clipText;
  175. if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
  176. using (Process pwsh = new Process {
  177. StartInfo = new ProcessStartInfo {
  178. RedirectStandardOutput = true,
  179. FileName = "powershell.exe",
  180. Arguments = "-noprofile -command \"Get-Clipboard\""
  181. }
  182. }) {
  183. pwsh.Start ();
  184. clipReadText = pwsh.StandardOutput.ReadToEnd ().TrimEnd ();
  185. pwsh.StandardOutput.Close ();
  186. pwsh.WaitForExit ();
  187. }
  188. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
  189. using (Process paste = new Process {
  190. StartInfo = new ProcessStartInfo {
  191. RedirectStandardOutput = true,
  192. FileName = "pbpaste"
  193. }
  194. }) {
  195. paste.Start ();
  196. clipReadText = paste.StandardOutput.ReadToEnd ();
  197. paste.StandardOutput.Close ();
  198. paste.WaitForExit ();
  199. }
  200. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux)) {
  201. if (Is_WSL_Platform ()) {
  202. try {
  203. using (Process bash = new Process {
  204. StartInfo = new ProcessStartInfo {
  205. RedirectStandardOutput = true,
  206. FileName = "powershell.exe",
  207. Arguments = "-noprofile -command \"Get-Clipboard\""
  208. }
  209. }) {
  210. bash.Start ();
  211. clipReadText = bash.StandardOutput.ReadToEnd ();
  212. bash.StandardOutput.Close ();
  213. bash.WaitForExit ();
  214. }
  215. } catch {
  216. exit = true;
  217. }
  218. Application.RequestStop ();
  219. }
  220. if (exit = xclipExists () == false) {
  221. // xclip doesn't exist then exit.
  222. Application.RequestStop ();
  223. }
  224. using (Process bash = new Process {
  225. StartInfo = new ProcessStartInfo {
  226. RedirectStandardOutput = true,
  227. FileName = "bash",
  228. Arguments = $"-c \"xclip -sel clip -o\""
  229. }
  230. }) {
  231. bash.Start ();
  232. clipReadText = bash.StandardOutput.ReadToEnd ();
  233. bash.StandardOutput.Close ();
  234. bash.WaitForExit ();
  235. }
  236. }
  237. Application.RequestStop ();
  238. };
  239. Application.Run ();
  240. if (!exit) {
  241. Assert.Equal (clipText, clipReadText);
  242. }
  243. Application.Shutdown ();
  244. }
  245. bool Is_WSL_Platform ()
  246. {
  247. using (Process bash = new Process {
  248. StartInfo = new ProcessStartInfo {
  249. FileName = "bash",
  250. Arguments = $"-c \"uname -a\"",
  251. RedirectStandardOutput = true,
  252. }
  253. }) {
  254. bash.Start ();
  255. var result = bash.StandardOutput.ReadToEnd ();
  256. var isWSL = false;
  257. if (result.Contains ("microsoft") && result.Contains ("WSL")) {
  258. isWSL = true;
  259. }
  260. bash.StandardOutput.Close ();
  261. bash.WaitForExit ();
  262. return isWSL;
  263. }
  264. }
  265. bool xclipExists ()
  266. {
  267. try {
  268. using (Process bash = new Process {
  269. StartInfo = new ProcessStartInfo {
  270. FileName = "bash",
  271. Arguments = $"-c \"which xclip\"",
  272. RedirectStandardOutput = true,
  273. }
  274. }) {
  275. bash.Start ();
  276. bool exist = bash.StandardOutput.ReadToEnd ().TrimEnd () != "";
  277. bash.StandardOutput.Close ();
  278. bash.WaitForExit ();
  279. if (exist) {
  280. return true;
  281. }
  282. }
  283. return false;
  284. } catch (System.Exception) {
  285. return false;
  286. }
  287. }
  288. }
  289. }