ClipboardTests.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. var clipText = "This is a clipboard unit test.";
  29. Clipboard.Contents = clipText;
  30. Application.Iteration += () => Application.RequestStop ();
  31. Application.Run ();
  32. Assert.Equal (clipText, Clipboard.Contents);
  33. }
  34. [Fact, AutoInitShutdown (useFakeClipboard: false)]
  35. public void IsSupported_Get ()
  36. {
  37. if (Clipboard.IsSupported) {
  38. Assert.True (Clipboard.IsSupported);
  39. } else {
  40. Assert.False (Clipboard.IsSupported);
  41. }
  42. }
  43. [Fact, AutoInitShutdown (useFakeClipboard: false)]
  44. public void TryGetClipboardData_Gets_From_OS_Clipboard ()
  45. {
  46. var clipText = "Trying to get from the OS clipboard.";
  47. Clipboard.Contents = clipText;
  48. Application.Iteration += () => Application.RequestStop ();
  49. Application.Run ();
  50. if (Clipboard.IsSupported) {
  51. Assert.True (Clipboard.TryGetClipboardData (out string result));
  52. Assert.Equal (clipText, result);
  53. } else {
  54. Assert.False (Clipboard.TryGetClipboardData (out string result));
  55. Assert.NotEqual (clipText, result);
  56. }
  57. }
  58. [Fact, AutoInitShutdown (useFakeClipboard: false)]
  59. public void TrySetClipboardData_Sets_The_OS_Clipboard ()
  60. {
  61. var clipText = "Trying to set the OS clipboard.";
  62. if (Clipboard.IsSupported) {
  63. Assert.True (Clipboard.TrySetClipboardData (clipText));
  64. } else {
  65. Assert.False (Clipboard.TrySetClipboardData (clipText));
  66. }
  67. Application.Iteration += () => Application.RequestStop ();
  68. Application.Run ();
  69. if (Clipboard.IsSupported) {
  70. Assert.Equal (clipText, Clipboard.Contents);
  71. } else {
  72. Assert.NotEqual (clipText, Clipboard.Contents);
  73. }
  74. }
  75. [Fact, AutoInitShutdown (useFakeClipboard: false)]
  76. public void Contents_Gets_From_OS_Clipboard ()
  77. {
  78. var clipText = "This is a clipboard unit test to get clipboard from OS.";
  79. var failed = false;
  80. var getClipText = "";
  81. Application.Iteration += () => {
  82. int exitCode = 0;
  83. string result = "";
  84. output.WriteLine ($"Setting OS clipboard to: {clipText}...");
  85. if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
  86. (exitCode, result) = ClipboardProcessRunner.Process ("pwsh", $"-command \"Set-Clipboard -Value \\\"{clipText}\\\"\"");
  87. output.WriteLine ($" Windows: pwsh Set-Clipboard: exitCode = {exitCode}, result = {output}");
  88. getClipText = Clipboard.Contents.ToString ();
  89. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
  90. (exitCode, result) = ClipboardProcessRunner.Process ("pbcopy", string.Empty, clipText);
  91. output.WriteLine ($" OSX: pbcopy: exitCode = {exitCode}, result = {result}");
  92. getClipText = Clipboard.Contents.ToString ();
  93. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux)) {
  94. if (Is_WSL_Platform ()) {
  95. try {
  96. // This runs the WINDOWS version of powershell.exe via WSL.
  97. (exitCode, result) = ClipboardProcessRunner.Process ("powershell.exe", $"-noprofile -command \"Set-Clipboard -Value \\\"{clipText}\\\"\"");
  98. output.WriteLine ($" WSL: powershell.exe Set-Clipboard: exitCode = {exitCode}, result = {result}");
  99. } catch {
  100. failed = true;
  101. }
  102. if (!failed) {
  103. getClipText = Clipboard.Contents.ToString ();
  104. output.WriteLine ($" WSL: Clipboard.Contents: {getClipText}");
  105. }
  106. Application.RequestStop ();
  107. return;
  108. }
  109. if (failed = xclipExists () == false) {
  110. // if xclip doesn't exist then exit.
  111. output.WriteLine ($" WSL: no xclip found.");
  112. Application.RequestStop ();
  113. return;
  114. }
  115. // If we get here, powershell didn't work and xclip exists...
  116. (exitCode, result) = ClipboardProcessRunner.Process ("bash", $"-c \"xclip -sel clip -i\"", clipText);
  117. output.WriteLine ($" Linux: bash xclip -sel clip -i: exitCode = {exitCode}, result = {result}");
  118. if (!failed) {
  119. getClipText = Clipboard.Contents.ToString ();
  120. output.WriteLine ($" Linux via xclip: Clipboard.Contents: {getClipText}");
  121. }
  122. }
  123. Application.RequestStop ();
  124. };
  125. Application.Run ();
  126. if (!failed) {
  127. Assert.Equal (clipText, getClipText);
  128. }
  129. }
  130. [Fact, AutoInitShutdown (useFakeClipboard: false)]
  131. public void Contents_Sets_The_OS_Clipboard ()
  132. {
  133. var clipText = "This is a clipboard unit test to set the OS clipboard.";
  134. var clipReadText = "";
  135. var failed = false;
  136. Application.Iteration += () => {
  137. Clipboard.Contents = clipText;
  138. int exitCode = 0;
  139. output.WriteLine ($"Getting OS clipboard...");
  140. if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
  141. (exitCode, clipReadText) = ClipboardProcessRunner.Process ("pwsh", "-noprofile -command \"Get-Clipboard\"");
  142. output.WriteLine ($" Windows: pwsh Get-Clipboard: exitCode = {exitCode}, result = {clipReadText}");
  143. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
  144. (exitCode, clipReadText) = ClipboardProcessRunner.Process ("pbpaste", "");
  145. output.WriteLine ($" OSX: pbpaste: exitCode = {exitCode}, result = {clipReadText}");
  146. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux)) {
  147. if (Is_WSL_Platform ()) {
  148. (exitCode, clipReadText) = ClipboardProcessRunner.Process ("powershell.exe", "-noprofile -command \"Get-Clipboard\"");
  149. output.WriteLine ($" WSL: powershell.exe Get-Clipboard: exitCode = {exitCode}, result = {clipReadText}");
  150. if (exitCode == 0) {
  151. Application.RequestStop ();
  152. return;
  153. }
  154. failed = true;
  155. }
  156. if (failed = xclipExists () == false) {
  157. // xclip doesn't exist then exit.
  158. Application.RequestStop ();
  159. return;
  160. }
  161. (exitCode, clipReadText) = ClipboardProcessRunner.Process ("bash", $"-c \"xclip -sel clip -o\"");
  162. output.WriteLine ($" Linux: bash xclip -sel clip -o: exitCode = {exitCode}, result = {clipReadText}");
  163. Assert.Equal (0, exitCode);
  164. }
  165. Application.RequestStop ();
  166. };
  167. Application.Run ();
  168. if (!failed) {
  169. Assert.Equal (clipText, clipReadText);
  170. }
  171. }
  172. bool Is_WSL_Platform ()
  173. {
  174. var (_, result) = ClipboardProcessRunner.Process ("bash", $"-c \"uname -a\"");
  175. return result.Contains ("microsoft") && result.Contains ("WSL");
  176. }
  177. bool xclipExists ()
  178. {
  179. try {
  180. var (_, result) = ClipboardProcessRunner.Process ("bash", $"-c \"which xclip\"");
  181. return result.TrimEnd () != "";
  182. } catch (System.Exception) {
  183. return false;
  184. }
  185. }
  186. }
  187. }