ClipboardTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. namespace Terminal.Gui.ClipboardTests;
  2. #if RUN_CLIPBOARD_UNIT_TESTS
  3. public class ClipboardTests
  4. {
  5. readonly ITestOutputHelper output;
  6. public ClipboardTests (ITestOutputHelper output) { this.output = output; }
  7. [Fact, AutoInitShutdown (useFakeClipboard: true, fakeClipboardAlwaysThrowsNotSupportedException: true)]
  8. public void IClipboard_GetClipBoardData_Throws_NotSupportedException ()
  9. {
  10. var iclip = Application.Driver?.Clipboard;
  11. Assert.Throws<NotSupportedException> (() => iclip.GetClipboardData ());
  12. }
  13. [Fact, AutoInitShutdown (useFakeClipboard: true, fakeClipboardAlwaysThrowsNotSupportedException: true)]
  14. public void IClipboard_SetClipBoardData_Throws_NotSupportedException ()
  15. {
  16. var iclip = Application.Driver?.Clipboard;
  17. Assert.Throws<NotSupportedException> (() => iclip.SetClipboardData ("foo"));
  18. }
  19. [Fact, AutoInitShutdown (useFakeClipboard: true)]
  20. public void Contents_Fake_Gets_Sets ()
  21. {
  22. if (!Clipboard.IsSupported)
  23. {
  24. output.WriteLine ($"The Clipboard not supported on this platform.");
  25. return;
  26. }
  27. string clipText = "The Contents_Gets_Sets unit test pasted this to the OS clipboard.";
  28. Clipboard.Contents = clipText;
  29. Application.Iteration += (s, a) => Application.RequestStop ();
  30. Application.Run ();
  31. Assert.Equal (clipText, Clipboard.Contents);
  32. }
  33. [Fact, AutoInitShutdown (useFakeClipboard: false)]
  34. public void Contents_Gets_Sets ()
  35. {
  36. if (!Clipboard.IsSupported)
  37. {
  38. output.WriteLine ($"The Clipboard not supported on this platform.");
  39. return;
  40. }
  41. string clipText = "The Contents_Gets_Sets unit test pasted this to the OS clipboard.";
  42. Clipboard.Contents = clipText;
  43. Application.Iteration += (s, a) => Application.RequestStop ();
  44. Application.Run ();
  45. Assert.Equal (clipText, Clipboard.Contents);
  46. }
  47. [Fact, AutoInitShutdown (useFakeClipboard: false)]
  48. public void Contents_Gets_Sets_When_IsSupportedFalse ()
  49. {
  50. if (!Clipboard.IsSupported)
  51. {
  52. output.WriteLine ($"The Clipboard not supported on this platform.");
  53. return;
  54. }
  55. string clipText = "The Contents_Gets_Sets unit test pasted this to the OS clipboard.";
  56. Clipboard.Contents = clipText;
  57. Application.Iteration += (s, a) => Application.RequestStop ();
  58. Application.Run ();
  59. Assert.Equal (clipText, Clipboard.Contents);
  60. }
  61. [Fact, AutoInitShutdown (useFakeClipboard: true)]
  62. public void Contents_Fake_Gets_Sets_When_IsSupportedFalse ()
  63. {
  64. if (!Clipboard.IsSupported)
  65. {
  66. output.WriteLine ($"The Clipboard not supported on this platform.");
  67. return;
  68. }
  69. string clipText = "The Contents_Gets_Sets unit test pasted this to the OS clipboard.";
  70. Clipboard.Contents = clipText;
  71. Application.Iteration += (s, a) => Application.RequestStop ();
  72. Application.Run ();
  73. Assert.Equal (clipText, Clipboard.Contents);
  74. }
  75. [Fact, AutoInitShutdown (useFakeClipboard: false)]
  76. public void IsSupported_Get ()
  77. {
  78. if (Clipboard.IsSupported)
  79. {
  80. Assert.True (Clipboard.IsSupported);
  81. }
  82. else
  83. {
  84. Assert.False (Clipboard.IsSupported);
  85. }
  86. }
  87. [Fact, AutoInitShutdown (useFakeClipboard: false)]
  88. public void TryGetClipboardData_Gets_From_OS_Clipboard ()
  89. {
  90. string clipText = "The TryGetClipboardData_Gets_From_OS_Clipboard unit test pasted this to the OS clipboard.";
  91. Clipboard.Contents = clipText;
  92. Application.Iteration += (s, a) => Application.RequestStop ();
  93. Application.Run ();
  94. if (Clipboard.IsSupported)
  95. {
  96. Assert.True (Clipboard.TryGetClipboardData (out string result));
  97. Assert.Equal (clipText, result);
  98. }
  99. else
  100. {
  101. Assert.False (Clipboard.TryGetClipboardData (out string result));
  102. Assert.NotEqual (clipText, result);
  103. }
  104. }
  105. [Fact, AutoInitShutdown (useFakeClipboard: false)]
  106. public void TrySetClipboardData_Sets_The_OS_Clipboard ()
  107. {
  108. string clipText = "The TrySetClipboardData_Sets_The_OS_Clipboard unit test pasted this to the OS clipboard.";
  109. if (Clipboard.IsSupported)
  110. {
  111. Assert.True (Clipboard.TrySetClipboardData (clipText));
  112. }
  113. else
  114. {
  115. Assert.False (Clipboard.TrySetClipboardData (clipText));
  116. }
  117. Application.Iteration += (s, a) => Application.RequestStop ();
  118. Application.Run ();
  119. if (Clipboard.IsSupported)
  120. {
  121. Assert.Equal (clipText, Clipboard.Contents);
  122. }
  123. else
  124. {
  125. Assert.NotEqual (clipText, Clipboard.Contents);
  126. }
  127. }
  128. // Disabling this test for now because it is not reliable
  129. #if false
  130. [Fact, AutoInitShutdown (useFakeClipboard: false)]
  131. public void Contents_Copies_From_OS_Clipboard ()
  132. {
  133. if (!Clipboard.IsSupported) {
  134. output.WriteLine ($"The Clipboard not supported on this platform.");
  135. return;
  136. }
  137. var clipText = "The Contents_Copies_From_OS_Clipboard unit test pasted this to the OS clipboard.";
  138. var failed = false;
  139. var getClipText = "";
  140. Application.Iteration += (s, a) => {
  141. int exitCode = 0;
  142. string result = "";
  143. output.WriteLine ($"Pasting to OS clipboard: {clipText}...");
  144. if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
  145. (exitCode, result) =
  146. ClipboardProcessRunner.Process ("powershell.exe", $"-command \"Set-Clipboard -Value \\\"{clipText}\\\"\"");
  147. output.WriteLine ($" Windows: powershell.exe Set-Clipboard: exitCode = {exitCode}, result = {result}");
  148. getClipText = Clipboard.Contents;
  149. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
  150. (exitCode, result) = ClipboardProcessRunner.Process ("pbcopy", string.Empty, clipText);
  151. output.WriteLine ($" OSX: pbcopy: exitCode = {exitCode}, result = {result}");
  152. getClipText = Clipboard.Contents;
  153. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux)) {
  154. if (Is_WSL_Platform ()) {
  155. try {
  156. // This runs the WINDOWS version of powershell.exe via WSL.
  157. (exitCode, result) =
  158. ClipboardProcessRunner.Process ("powershell.exe", $"-noprofile -command \"Set-Clipboard -Value \\\"{clipText}\\\"\"");
  159. output.WriteLine ($" WSL: powershell.exe Set-Clipboard: exitCode = {exitCode}, result = {result}");
  160. } catch {
  161. failed = true;
  162. }
  163. if (!failed) {
  164. // If we set the OS clipboard via Powershell, then getting Contents should return the same text.
  165. getClipText = Clipboard.Contents;
  166. output.WriteLine ($" WSL: Clipboard.Contents: {getClipText}");
  167. }
  168. Application.RequestStop ();
  169. return;
  170. }
  171. if (failed = xclipExists () == false) {
  172. // if xclip doesn't exist then exit.
  173. output.WriteLine ($" WSL: no xclip found.");
  174. Application.RequestStop ();
  175. return;
  176. }
  177. // If we get here, powershell didn't work and xclip exists...
  178. (exitCode, result) =
  179. ClipboardProcessRunner.Process ("bash", $"-c \"xclip -sel clip -i\"", clipText);
  180. output.WriteLine ($" Linux: bash xclip -sel clip -i: exitCode = {exitCode}, result = {result}");
  181. if (!failed) {
  182. getClipText = Clipboard.Contents;
  183. output.WriteLine ($" Linux via xclip: Clipboard.Contents: {getClipText}");
  184. }
  185. }
  186. Application.RequestStop ();
  187. };
  188. Application.Run ();
  189. if (!failed) Assert.Equal (clipText, getClipText);
  190. }
  191. [Fact, AutoInitShutdown (useFakeClipboard: false)]
  192. public void Contents_Pastes_To_OS_Clipboard ()
  193. {
  194. if (!Clipboard.IsSupported) {
  195. output.WriteLine ($"The Clipboard not supported on this platform.");
  196. return;
  197. }
  198. var clipText = "The Contents_Pastes_To_OS_Clipboard unit test pasted this via Clipboard.Contents.";
  199. var clipReadText = "";
  200. var failed = false;
  201. Application.Iteration += (s, a) => {
  202. Clipboard.Contents = clipText;
  203. int exitCode = 0;
  204. output.WriteLine ($"Getting OS clipboard...");
  205. if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
  206. (exitCode, clipReadText) =
  207. ClipboardProcessRunner.Process ("powershell.exe", "-noprofile -command \"Get-Clipboard\"");
  208. output.WriteLine ($" Windows: powershell.exe Get-Clipboard: exitCode = {exitCode}, result = {clipReadText}");
  209. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
  210. (exitCode, clipReadText) = ClipboardProcessRunner.Process ("pbpaste", "");
  211. output.WriteLine ($" OSX: pbpaste: exitCode = {exitCode}, result = {clipReadText}");
  212. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux)) {
  213. if (Is_WSL_Platform ()) {
  214. (exitCode, clipReadText) =
  215. ClipboardProcessRunner.Process ("powershell.exe", "-noprofile -command \"Get-Clipboard\"");
  216. output.WriteLine ($" WSL: powershell.exe Get-Clipboard: exitCode = {exitCode}, result = {clipReadText}");
  217. if (exitCode == 0) {
  218. Application.RequestStop ();
  219. return;
  220. }
  221. failed = true;
  222. }
  223. if (failed = xclipExists () == false) {
  224. // xclip doesn't exist then exit.
  225. Application.RequestStop ();
  226. return;
  227. }
  228. (exitCode, clipReadText) = ClipboardProcessRunner.Process ("bash", $"-c \"xclip -sel clip -o\"");
  229. output.WriteLine ($" Linux: bash xclip -sel clip -o: exitCode = {exitCode}, result = {clipReadText}");
  230. Assert.Equal (0, exitCode);
  231. }
  232. Application.RequestStop ();
  233. };
  234. Application.Run ();
  235. if (!failed) Assert.Equal (clipText, clipReadText.TrimEnd ());
  236. }
  237. #endif
  238. bool Is_WSL_Platform ()
  239. {
  240. (int _, string result) = ClipboardProcessRunner.Process ("bash", $"-c \"uname -a\"");
  241. return result.Contains ("microsoft") && result.Contains ("WSL");
  242. }
  243. bool xclipExists ()
  244. {
  245. try
  246. {
  247. (int _, string result) = ClipboardProcessRunner.Process ("bash", $"-c \"which xclip\"");
  248. return result.TrimEnd () != "";
  249. }
  250. catch (Exception)
  251. {
  252. return false;
  253. }
  254. }
  255. }
  256. #endif