2
0

ClipboardTests.cs 11 KB

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