SetupFakeDriverAttribute.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System.Diagnostics;
  2. using System.Reflection;
  3. using Xunit.Sdk;
  4. namespace UnitTests;
  5. /// <summary>
  6. /// Enables test functions annotated with the [SetupFakeDriver] attribute to set Application.Driver to new
  7. /// FakeDriver(). The driver is set up with 25 rows and columns.
  8. /// </summary>
  9. /// <remarks>
  10. /// This attribute uses the built-in FakeDriver from Terminal.Gui.Drivers namespace.
  11. /// </remarks>
  12. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
  13. public class SetupFakeDriverAttribute : BeforeAfterTestAttribute
  14. {
  15. public override void After (MethodInfo methodUnderTest)
  16. {
  17. Debug.WriteLine ($"After: {methodUnderTest.Name}");
  18. // Turn off diagnostic flags in case some test left them on
  19. View.Diagnostics = ViewDiagnosticFlags.Off;
  20. Application.ResetState (true);
  21. Assert.Null (Application.Driver);
  22. Assert.Equal (new (0, 0, 2048, 2048), Application.Screen);
  23. base.After (methodUnderTest);
  24. }
  25. public override void Before (MethodInfo methodUnderTest)
  26. {
  27. Debug.WriteLine ($"Before: {methodUnderTest.Name}");
  28. Application.ResetState (true);
  29. Assert.Null (Application.Driver);
  30. // Use the built-in FakeDriver from the library
  31. var driver = new FakeDriver ();
  32. driver.Init ();
  33. Application.Driver = driver;
  34. driver.SetBufferSize (25, 25);
  35. Assert.Equal (new (0, 0, 25, 25), Application.Screen);
  36. // Ensures subscribing events, at least for the SizeChanged event
  37. Application.SubscribeDriverEvents ();
  38. base.Before (methodUnderTest);
  39. }
  40. }