ClipboardTests.cs 9.3 KB

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