FakeApplicationFactory.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System.Drawing;
  2. using TerminalGuiFluentTesting;
  3. namespace Terminal.Gui.Drivers;
  4. #pragma warning disable CS1591
  5. public class FakeApplicationFactory
  6. {
  7. /// <summary>
  8. /// Creates an initialized fake application which will be cleaned up when result object
  9. /// is disposed.
  10. /// </summary>
  11. /// <returns></returns>
  12. public IDisposable SetupFakeApplication ()
  13. {
  14. var cts = new CancellationTokenSource ();
  15. var fakeInput = new FakeNetInput (cts.Token);
  16. FakeOutput output = new ();
  17. output.Size = new (80, 25);
  18. IApplication origApp = ApplicationImpl.Instance;
  19. var sizeMonitor = new FakeSizeMonitor (output, output.LastBuffer!);
  20. var impl = new ApplicationImpl (new FakeNetComponentFactory (fakeInput, output, sizeMonitor));
  21. ApplicationImpl.ChangeInstance (impl);
  22. // Initialize with a fake driver
  23. impl.Init (null, "fake");
  24. // Handle different facade types - cast to common interface instead
  25. var d = (IConsoleDriverFacade)Application.Driver!;
  26. sizeMonitor.SizeChanged += (_, e) =>
  27. {
  28. if (e.Size != null)
  29. {
  30. Size s = e.Size.Value;
  31. output.Size = s;
  32. d.OutputBuffer.SetSize (s.Width, s.Height);
  33. }
  34. };
  35. return new FakeApplicationLifecycle (origApp, cts);
  36. }
  37. }