|
| 1 | +namespace UnitTestsParallelizable.Drivers; |
| 2 | + |
| 3 | +public class DriverImplClipboardTests |
| 4 | +{ |
| 5 | + [Fact] |
| 6 | + public void UnixClipboard_Set_Then_Get_RoundTrips_Through_Xclip_Runner () |
| 7 | + { |
| 8 | + UnixClipboardProcessRunner processRunner = new (); |
| 9 | + UnixClipboard clipboard = new (processRunner); |
| 10 | + |
| 11 | + Assert.True (clipboard.IsSupported); |
| 12 | + |
| 13 | + clipboard.SetClipboardData ("hello"); |
| 14 | + |
| 15 | + Assert.Equal ("hello", clipboard.GetClipboardData ()); |
| 16 | + Assert.Contains ("-selection clipboard -i", processRunner.LastSetCommandLine); |
| 17 | + Assert.Contains ("-selection clipboard -o", processRunner.LastGetCommandLine); |
| 18 | + } |
| 19 | + |
| 20 | + [Fact] |
| 21 | + public void CreateClipboard_Linux_AttachedToTerminal_And_UnixSupported_Uses_UnixClipboard () |
| 22 | + { |
| 23 | + FakeClipboard fallbackClipboard = new (); |
| 24 | + FakeClipboard unixClipboard = new (); |
| 25 | + |
| 26 | + IClipboard clipboard = DriverImpl.CreateClipboard (isWindows: () => false, |
| 27 | + isMac: () => false, |
| 28 | + isWsl: () => false, |
| 29 | + isLinux: () => true, |
| 30 | + createWindowsClipboard: () => throw new InvalidOperationException (), |
| 31 | + createMacClipboard: () => throw new InvalidOperationException (), |
| 32 | + createWslClipboard: () => throw new InvalidOperationException (), |
| 33 | + createUnixClipboard: () => unixClipboard, |
| 34 | + fallbackClipboard: fallbackClipboard); |
| 35 | + |
| 36 | + Assert.Same (unixClipboard, clipboard); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void CreateClipboard_NotLinux_Uses_FallbackClipboard () |
| 41 | + { |
| 42 | + FakeClipboard fallbackClipboard = new (); |
| 43 | + |
| 44 | + IClipboard clipboard = DriverImpl.CreateClipboard (isWindows: () => false, |
| 45 | + isMac: () => false, |
| 46 | + isWsl: () => false, |
| 47 | + isLinux: () => false, |
| 48 | + createWindowsClipboard: () => throw new InvalidOperationException (), |
| 49 | + createMacClipboard: () => throw new InvalidOperationException (), |
| 50 | + createWslClipboard: () => throw new InvalidOperationException (), |
| 51 | + createUnixClipboard: () => throw new InvalidOperationException (), |
| 52 | + fallbackClipboard: fallbackClipboard); |
| 53 | + |
| 54 | + Assert.Same (fallbackClipboard, clipboard); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void CreateClipboard_Linux_AttachedToTerminal_But_UnixUnsupported_Uses_FallbackClipboard () |
| 59 | + { |
| 60 | + FakeClipboard fallbackClipboard = new (); |
| 61 | + FakeClipboard unsupportedUnixClipboard = new (isSupportedAlwaysFalse: true); |
| 62 | + |
| 63 | + IClipboard clipboard = DriverImpl.CreateClipboard (isWindows: () => false, |
| 64 | + isMac: () => false, |
| 65 | + isWsl: () => false, |
| 66 | + isLinux: () => true, |
| 67 | + createWindowsClipboard: () => throw new InvalidOperationException (), |
| 68 | + createMacClipboard: () => throw new InvalidOperationException (), |
| 69 | + createWslClipboard: () => throw new InvalidOperationException (), |
| 70 | + createUnixClipboard: () => unsupportedUnixClipboard, |
| 71 | + fallbackClipboard: fallbackClipboard); |
| 72 | + |
| 73 | + Assert.Same (fallbackClipboard, clipboard); |
| 74 | + } |
| 75 | + |
| 76 | + private sealed class UnixClipboardProcessRunner : IClipboardProcessRunner |
| 77 | + { |
| 78 | + private string _clipboardText = string.Empty; |
| 79 | + |
| 80 | + public string LastGetCommandLine { get; private set; } = string.Empty; |
| 81 | + public string LastSetCommandLine { get; private set; } = string.Empty; |
| 82 | + |
| 83 | + public (int exitCode, string result) Bash (string commandLine, string inputText = "", bool waitForOutput = false) |
| 84 | + { |
| 85 | + if (commandLine == "which xclip") |
| 86 | + { |
| 87 | + return (0, "/usr/bin/xclip"); |
| 88 | + } |
| 89 | + |
| 90 | + if (commandLine.Contains ("-selection clipboard -i", StringComparison.Ordinal)) |
| 91 | + { |
| 92 | + LastSetCommandLine = commandLine; |
| 93 | + _clipboardText = inputText; |
| 94 | + |
| 95 | + return (0, string.Empty); |
| 96 | + } |
| 97 | + |
| 98 | + if (!commandLine.Contains ("-selection clipboard -o", StringComparison.Ordinal)) |
| 99 | + { |
| 100 | + return (1, string.Empty); |
| 101 | + } |
| 102 | + LastGetCommandLine = commandLine; |
| 103 | + int redirectIndex = commandLine.IndexOf (" > ", StringComparison.Ordinal); |
| 104 | + string tempFileName = commandLine [(redirectIndex + 3)..]; |
| 105 | + |
| 106 | + File.WriteAllText (tempFileName, _clipboardText); |
| 107 | + |
| 108 | + return (0, string.Empty); |
| 109 | + } |
| 110 | + |
| 111 | + public (int exitCode, string result) Process (string cmd, string arguments, string? input = null, bool waitForOutput = true) |
| 112 | + { |
| 113 | + throw new NotSupportedException (); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments