FakeComponentFactory.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #nullable enable
  2. using System.Collections.Concurrent;
  3. namespace Terminal.Gui.Drivers;
  4. /// <summary>
  5. /// <see cref="IComponentFactory{T}"/> implementation for fake/mock console I/O used in unit tests.
  6. /// This factory creates instances that simulate console behavior without requiring a real terminal.
  7. /// </summary>
  8. public class FakeComponentFactory : ComponentFactory<ConsoleKeyInfo>
  9. {
  10. private readonly ConcurrentQueue<ConsoleKeyInfo>? _predefinedInput;
  11. private readonly FakeConsoleOutput? _output;
  12. /// <summary>
  13. /// Creates a new FakeComponentFactory with optional predefined input and output capture.
  14. /// </summary>
  15. /// <param name="predefinedInput">Optional queue of predefined input events to simulate.</param>
  16. /// <param name="output">Optional fake output to capture what would be written to console.</param>
  17. public FakeComponentFactory (ConcurrentQueue<ConsoleKeyInfo>? predefinedInput = null, FakeConsoleOutput? output = null)
  18. {
  19. _predefinedInput = predefinedInput;
  20. _output = output;
  21. }
  22. /// <inheritdoc/>
  23. public override IConsoleInput<ConsoleKeyInfo> CreateInput ()
  24. {
  25. return new FakeConsoleInput (_predefinedInput);
  26. }
  27. /// <inheritdoc />
  28. public override IConsoleOutput CreateOutput ()
  29. {
  30. return _output ?? new FakeConsoleOutput ();
  31. }
  32. /// <inheritdoc />
  33. public override IInputProcessor CreateInputProcessor (ConcurrentQueue<ConsoleKeyInfo> inputBuffer)
  34. {
  35. return new NetInputProcessor (inputBuffer);
  36. }
  37. /// <inheritdoc />
  38. public override IConsoleSizeMonitor CreateConsoleSizeMonitor (IConsoleOutput consoleOutput, IOutputBuffer outputBuffer)
  39. {
  40. outputBuffer.SetSize(consoleOutput.GetSize().Width, consoleOutput.GetSize().Height);
  41. return new ConsoleSizeMonitor (consoleOutput, outputBuffer);
  42. }
  43. }