ClipboardTests.cs 9.4 KB

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