ClipboardTests.cs 7.9 KB

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