|
| 1 | +using System.Collections.Concurrent; |
| 2 | +using System.Drawing; |
| 3 | +using Terminal.Gui; |
| 4 | + |
| 5 | +namespace TerminalGuiFluentAssertions; |
| 6 | + |
| 7 | +class FakeInput<T> : IConsoleInput<T> |
| 8 | +{ |
| 9 | + /// <inheritdoc /> |
| 10 | + public void Dispose () { } |
| 11 | + |
| 12 | + /// <inheritdoc /> |
| 13 | + public void Initialize (ConcurrentQueue<T> inputBuffer) { } |
| 14 | + |
| 15 | + /// <inheritdoc /> |
| 16 | + public void Run (CancellationToken token) |
| 17 | + { |
| 18 | + // Simulate an infinite loop that checks for cancellation |
| 19 | + token.WaitHandle.WaitOne (); // Blocks until the token is cancelled |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +class FakeNetInput : FakeInput<ConsoleKeyInfo>, INetInput |
| 24 | +{ |
| 25 | + |
| 26 | +} |
| 27 | + |
| 28 | +class FakeWindowsInput : FakeInput<WindowsConsole.InputRecord>, IWindowsInput |
| 29 | +{ |
| 30 | + |
| 31 | +} |
| 32 | + |
| 33 | +class FakeOutput : IConsoleOutput |
| 34 | +{ |
| 35 | + public Size Size { get; set; } |
| 36 | + |
| 37 | + /// <inheritdoc /> |
| 38 | + public void Dispose () |
| 39 | + { |
| 40 | + |
| 41 | + } |
| 42 | + |
| 43 | + /// <inheritdoc /> |
| 44 | + public void Write (ReadOnlySpan<char> text) |
| 45 | + { |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + /// <inheritdoc /> |
| 50 | + public void Write (IOutputBuffer buffer) |
| 51 | + { |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + /// <inheritdoc /> |
| 56 | + public Size GetWindowSize () |
| 57 | + { |
| 58 | + return Size; |
| 59 | + } |
| 60 | + |
| 61 | + /// <inheritdoc /> |
| 62 | + public void SetCursorVisibility (CursorVisibility visibility) |
| 63 | + { |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + /// <inheritdoc /> |
| 68 | + public void SetCursorPosition (int col, int row) |
| 69 | + { |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | +} |
| 74 | +/// <summary> |
| 75 | +/// Entry point to fluent assertions. |
| 76 | +/// </summary> |
| 77 | +public static class With |
| 78 | +{ |
| 79 | + /// <summary> |
| 80 | + /// Entrypoint to fluent assertions |
| 81 | + /// </summary> |
| 82 | + /// <param name="width"></param> |
| 83 | + /// <param name="height"></param> |
| 84 | + /// <returns></returns> |
| 85 | + public static GuiTestContext<T> A<T> (int width, int height) where T : Toplevel, new () |
| 86 | + { |
| 87 | + return new GuiTestContext<T> (width,height); |
| 88 | + } |
| 89 | +} |
| 90 | +public class GuiTestContext<T> where T : Toplevel, new() |
| 91 | +{ |
| 92 | + private readonly CancellationTokenSource _cts; |
| 93 | + private readonly Task _runTask; |
| 94 | + |
| 95 | + internal GuiTestContext (int width, int height) |
| 96 | + { |
| 97 | + IApplication origApp = ApplicationImpl.Instance; |
| 98 | + |
| 99 | + var netInput = new FakeNetInput (); |
| 100 | + var winInput = new FakeWindowsInput (); |
| 101 | + var output = new FakeOutput (); |
| 102 | + |
| 103 | + output.Size = new (width, height); |
| 104 | + |
| 105 | + var v2 = new ApplicationV2( |
| 106 | + () => netInput, |
| 107 | + ()=>output, |
| 108 | + () => winInput, |
| 109 | + () => output); |
| 110 | + |
| 111 | + // Create a cancellation token |
| 112 | + _cts = new (); |
| 113 | + |
| 114 | + // Start the application in a background thread |
| 115 | + _runTask = Task.Run (() => |
| 116 | + { |
| 117 | + try |
| 118 | + { |
| 119 | + ApplicationImpl.ChangeInstance (v2); |
| 120 | + |
| 121 | + v2.Init (); |
| 122 | + |
| 123 | + Application.Run<T> (); // This will block, but it's on a background thread now |
| 124 | + |
| 125 | + Application.Shutdown (); |
| 126 | + } |
| 127 | + catch (OperationCanceledException) |
| 128 | + { |
| 129 | + |
| 130 | + } |
| 131 | + finally |
| 132 | + { |
| 133 | + ApplicationImpl.ChangeInstance (origApp); |
| 134 | + } |
| 135 | + }, _cts.Token); |
| 136 | + |
| 137 | + Application.Shutdown (); |
| 138 | + } |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// Stops the application and waits for the background thread to exit. |
| 142 | + /// </summary> |
| 143 | + public void Stop () |
| 144 | + { |
| 145 | + _cts.Cancel (); |
| 146 | + Application.Invoke (()=>Application.RequestStop()); |
| 147 | + _runTask.Wait (); // Ensure the background thread exits |
| 148 | + } |
| 149 | + |
| 150 | + // Cleanup to avoid state bleed between tests |
| 151 | + public void Dispose () |
| 152 | + { |
| 153 | + } |
| 154 | +} |
| 155 | + |
0 commit comments