using System.Collections.Concurrent;
namespace Terminal.Gui.Drivers;
///
/// implementation that uses a fake input source for testing.
/// The and methods are executed
/// on the input thread created by .
///
public class FakeInput : InputImpl, ITestableInput
{
// Queue for storing injected input that will be returned by Peek/Read
private readonly ConcurrentQueue _testInput = new ();
private int _peekCallCount;
///
/// Gets the number of times has been called.
/// This is useful for verifying that the input loop throttling is working correctly.
///
internal int PeekCallCount => _peekCallCount;
///
/// Creates a new FakeInput.
///
public FakeInput () { }
///
public override bool Peek ()
{
// Will be called on the input thread.
Interlocked.Increment (ref _peekCallCount);
return !_testInput.IsEmpty;
}
///
public override IEnumerable Read ()
{
// Will be called on the input thread.
while (_testInput.TryDequeue (out ConsoleKeyInfo input))
{
yield return input;
}
}
///
public void AddInput (ConsoleKeyInfo input)
{
//Logging.Trace ($"Enqueuing input: {input.Key}");
// Will be called on the main loop thread.
_testInput.Enqueue (input);
}
}