FakeApplicationFactory.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #nullable enable
  2. using System.Drawing;
  3. using TerminalGuiFluentTesting;
  4. namespace Terminal.Gui.Drivers;
  5. #pragma warning disable CS1591
  6. public class FakeApplicationFactory
  7. {
  8. /// <summary>
  9. /// Creates an initialized fake application which will be cleaned up when result object
  10. /// is disposed.
  11. /// </summary>
  12. /// <returns></returns>
  13. public IDisposable SetupFakeApplication ()
  14. {
  15. var cts = new CancellationTokenSource ();
  16. var fakeInput = new FakeNetInput (cts.Token);
  17. FakeOutput output = new ();
  18. output.Size = new (25, 25);
  19. IApplication origApp = ApplicationImpl.Instance;
  20. var sizeMonitor = new FakeSizeMonitor ();
  21. var v2 = new ApplicationV2 (new FakeNetComponentFactory (fakeInput, output, sizeMonitor));
  22. ApplicationImpl.ChangeInstance (v2);
  23. v2.Init (null, "v2net");
  24. ConsoleDriverFacade<ConsoleKeyInfo> d = (ConsoleDriverFacade<ConsoleKeyInfo>)Application.Driver!;
  25. sizeMonitor.SizeChanging += (_, e) =>
  26. {
  27. if (e.Size != null)
  28. {
  29. Size s = e.Size.Value;
  30. output.Size = s;
  31. d.OutputBuffer.SetWindowSize (s.Width, s.Height);
  32. }
  33. };
  34. return new FakeApplicationLifecycle (origApp, cts);
  35. }
  36. }