ClipboardTests.cs 9.3 KB

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