SetupFakeDriverAttribute.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. /// On Before, sets Configuration.Locations to ConfigLocations.DefaultOnly.
  11. /// On After, sets Configuration.Locations to ConfigLocations.All.
  12. /// </remarks>
  13. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
  14. public class SetupFakeDriverAttribute : BeforeAfterTestAttribute
  15. {
  16. public override void After (MethodInfo methodUnderTest)
  17. {
  18. Debug.WriteLine ($"After: {methodUnderTest.Name}");
  19. // Turn off diagnostic flags in case some test left them on
  20. View.Diagnostics = ViewDiagnosticFlags.Off;
  21. Application.ResetState (true);
  22. Assert.Null (Application.Driver);
  23. Assert.Equal (new (0, 0, 2048, 2048), Application.Screen);
  24. base.After (methodUnderTest);
  25. }
  26. public override void Before (MethodInfo methodUnderTest)
  27. {
  28. Debug.WriteLine ($"Before: {methodUnderTest.Name}");
  29. Application.ResetState (true);
  30. Assert.Null (Application.Driver);
  31. Application.Driver = new FakeDriver { Rows = 25, Cols = 25 };
  32. Assert.Equal (new (0, 0, 25, 25), Application.Screen);
  33. // Ensures subscribing events, at least for the SizeChanged event
  34. Application.SubscribeDriverEvents ();
  35. base.Before (methodUnderTest);
  36. }
  37. }