ForceDriverTests.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using UICatalog;
  2. using Xunit.Abstractions;
  3. namespace IntegrationTests.UICatalog;
  4. /// <summary>
  5. /// Integration tests for ForceDriver persistence when opening scenarios in UICatalog.
  6. /// </summary>
  7. public class ForceDriverTests
  8. {
  9. private readonly ITestOutputHelper _output;
  10. public ForceDriverTests (ITestOutputHelper output)
  11. {
  12. _output = output;
  13. }
  14. /// <summary>
  15. /// Tests that ForceDriver persists when opening a scenario after Init/Shutdown cycles.
  16. /// This verifies the fix for issue #4391.
  17. /// </summary>
  18. [Fact]
  19. public void ForceDriver_Persists_Across_Init_Shutdown_Cycles ()
  20. {
  21. // Arrange
  22. const string expectedDriver = "fake";
  23. ConfigurationManager.Disable (true);
  24. Application.ResetState (true);
  25. // Set ForceDriver in RuntimeConfig (simulating what UICatalog does with --driver option)
  26. ConfigurationManager.RuntimeConfig = $$"""
  27. {
  28. "Application.ForceDriver": "{{expectedDriver}}"
  29. }
  30. """;
  31. // Enable ConfigurationManager with all locations (as UICatalog does)
  32. ConfigurationManager.Enable (ConfigLocations.All);
  33. var firstDriverName = string.Empty;
  34. var secondDriverName = string.Empty;
  35. try
  36. {
  37. // Act - Cycle 1: Init and check driver
  38. _output.WriteLine ("Cycle 1: First Init");
  39. Application.Init ();
  40. firstDriverName = Application.Driver?.GetName () ?? string.Empty;
  41. _output.WriteLine ($"Cycle 1 driver: {firstDriverName}");
  42. Application.Shutdown ();
  43. // Act - Cycle 2: Reload RuntimeConfig and Init again (simulating scenario opening)
  44. _output.WriteLine ("Cycle 2: Reload RuntimeConfig and Init again");
  45. // This simulates what the fix does before each scenario
  46. ConfigurationManager.Load (ConfigLocations.Runtime);
  47. ConfigurationManager.Apply ();
  48. // Scenario calls Application.Init() without parameters
  49. Application.Init ();
  50. secondDriverName = Application.Driver?.GetName () ?? string.Empty;
  51. _output.WriteLine ($"Cycle 2 driver: {secondDriverName}");
  52. Application.Shutdown ();
  53. // Assert
  54. Assert.Equal (expectedDriver, firstDriverName);
  55. Assert.Equal (expectedDriver, secondDriverName);
  56. _output.WriteLine ($"SUCCESS: Driver '{expectedDriver}' persisted across Init/Shutdown cycles");
  57. }
  58. finally
  59. {
  60. ConfigurationManager.Disable (true);
  61. Application.ResetState (true);
  62. }
  63. }
  64. /// <summary>
  65. /// Tests that ForceDriver is used when a scenario calls Application.Init() without parameters.
  66. /// This simulates the actual UICatalog scenario execution flow.
  67. /// </summary>
  68. [Fact]
  69. public void ForceDriver_Used_By_Scenario_Init ()
  70. {
  71. // Arrange
  72. const string expectedDriver = "fake";
  73. Scenario? scenario = null;
  74. ConfigurationManager.Disable (true);
  75. Application.ResetState (true);
  76. // Set ForceDriver in RuntimeConfig
  77. ConfigurationManager.RuntimeConfig = $$"""
  78. {
  79. "Application.ForceDriver": "{{expectedDriver}}"
  80. }
  81. """;
  82. // Enable ConfigurationManager
  83. ConfigurationManager.Enable (ConfigLocations.All);
  84. try
  85. {
  86. // Get the first available scenario
  87. var scenarios = Scenario.GetScenarios ();
  88. Assert.NotEmpty (scenarios);
  89. scenario = scenarios[0];
  90. var scenarioName = scenario.GetName ();
  91. _output.WriteLine ($"Testing with scenario: {scenarioName}");
  92. // Reload RuntimeConfig before scenario (as the fix does)
  93. ConfigurationManager.Load (ConfigLocations.Runtime);
  94. ConfigurationManager.Apply ();
  95. // Scenario calls Application.Init() - it should use ForceDriver
  96. Application.Init ();
  97. var driverName = Application.Driver?.GetName () ?? string.Empty;
  98. _output.WriteLine ($"Scenario driver: {driverName}");
  99. // Assert
  100. Assert.Equal (expectedDriver, driverName);
  101. _output.WriteLine ($"SUCCESS: Scenario uses ForceDriver '{expectedDriver}'");
  102. Application.Shutdown ();
  103. }
  104. finally
  105. {
  106. scenario?.Dispose ();
  107. ConfigurationManager.Disable (true);
  108. Application.ResetState (true);
  109. }
  110. }
  111. }