SetupFakeDriverAttribute.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System.Diagnostics;
  2. using System.Reflection;
  3. using TerminalGuiFluentTesting;
  4. using Xunit.Sdk;
  5. namespace UnitTests;
  6. /// <summary>
  7. /// Enables test functions annotated with the [SetupFakeDriver] attribute to set Application.Driver to new
  8. /// FakeDriver(). The driver is set up with 25 rows and columns.
  9. /// </summary>
  10. /// <remarks>
  11. /// On Before, sets Configuration.Locations to ConfigLocations.DefaultOnly.
  12. /// On After, sets Configuration.Locations to ConfigLocations.All.
  13. /// </remarks>
  14. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
  15. public class SetupFakeDriverAttribute : BeforeAfterTestAttribute
  16. {
  17. public override void After (MethodInfo methodUnderTest)
  18. {
  19. Debug.WriteLine ($"After: {methodUnderTest.Name}");
  20. // Turn off diagnostic flags in case some test left them on
  21. View.Diagnostics = ViewDiagnosticFlags.Off;
  22. Application.ResetState (true);
  23. Assert.Null (Application.Driver);
  24. Assert.Equal (new (0, 0, 2048, 2048), Application.Screen);
  25. base.After (methodUnderTest);
  26. }
  27. public override void Before (MethodInfo methodUnderTest)
  28. {
  29. Debug.WriteLine ($"Before: {methodUnderTest.Name}");
  30. Application.ResetState (true);
  31. Assert.Null (Application.Driver);
  32. var driver = new FakeDriver ();
  33. Application.Driver = driver;
  34. driver.SetScreenSize (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. }