diff --git a/UnitTests/ScenarioTests.cs b/UnitTests/ScenarioTests.cs index 61fd9a3e8..10757b366 100644 --- a/UnitTests/ScenarioTests.cs +++ b/UnitTests/ScenarioTests.cs @@ -26,15 +26,15 @@ namespace Terminal.Gui { int CreateInput (string input) { // Put a control-q in at the end - Console.MockKeyPresses.Push (new ConsoleKeyInfo ('q', ConsoleKey.Q, shift: false, alt: false, control: true)); + FakeConsole.MockKeyPresses.Push (new ConsoleKeyInfo ('q', ConsoleKey.Q, shift: false, alt: false, control: true)); foreach (var c in input.Reverse ()) { if (char.IsLetter (c)) { - Console.MockKeyPresses.Push (new ConsoleKeyInfo (char.ToLower (c), (ConsoleKey)char.ToUpper (c), shift: char.IsUpper (c), alt: false, control: false)); + FakeConsole.MockKeyPresses.Push (new ConsoleKeyInfo (char.ToLower (c), (ConsoleKey)char.ToUpper (c), shift: char.IsUpper (c), alt: false, control: false)); } else { - Console.MockKeyPresses.Push (new ConsoleKeyInfo (c, (ConsoleKey)c, shift: false, alt: false, control: false)); + FakeConsole.MockKeyPresses.Push (new ConsoleKeyInfo (c, (ConsoleKey)c, shift: false, alt: false, control: false)); } } - return Console.MockKeyPresses.Count; + return FakeConsole.MockKeyPresses.Count; } /// @@ -48,59 +48,61 @@ namespace Terminal.Gui { List scenarioClasses = Scenario.GetDerivedClasses (); Assert.NotEmpty (scenarioClasses); - foreach (var scenarioClass in scenarioClasses) { + lock (FakeConsole.MockKeyPresses) { + foreach (var scenarioClass in scenarioClasses) { - // Setup some fake keypresses - // Passing empty string will cause just a ctrl-q to be fired - Console.MockKeyPresses.Clear (); - int stackSize = CreateInput (""); + // Setup some fake keypresses + // Passing empty string will cause just a ctrl-q to be fired + FakeConsole.MockKeyPresses.Clear (); + int stackSize = CreateInput (""); - Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true))); + Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true))); - int iterations = 0; - Application.Iteration = () => { - iterations++; - // Stop if we run out of control... - if (iterations > 10) { - Application.RequestStop (); + int iterations = 0; + Application.Iteration = () => { + iterations++; + // Stop if we run out of control... + if (iterations > 10) { + Application.RequestStop (); + } + }; + + int ms; + if (scenarioClass.Name == "CharacterMap") { + ms = 2000; + } else { + ms = 1000; } - }; + var abortCount = 0; + Func abortCallback = (MainLoop loop) => { + abortCount++; + Application.RequestStop (); + return false; + }; + var token = Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (ms), abortCallback); - int ms; - if (scenarioClass.Name == "CharacterMap") { - ms = 1500; - }else { - ms = 1000; + var scenario = (Scenario)Activator.CreateInstance (scenarioClass); + scenario.Init (Application.Top, Colors.Base); + scenario.Setup (); + // There is no need to call Application.Begin because Init already creates the Application.Top + // If Application.RunState is used then the Application.RunLoop must also be used instead Application.Run. + //var rs = Application.Begin (Application.Top); + scenario.Run (); + + //Application.End (rs); + + // Shutdown must be called to safely clean up Application if Init has been called + Application.Shutdown (); + + if (abortCount != 0) { + output.WriteLine ($"Scenario {scenarioClass} had abort count of {abortCount}"); + } + + Assert.Equal (0, abortCount); + // # of key up events should match # of iterations + Assert.Equal (1, iterations); + Assert.Equal (stackSize, iterations); } - var abortCount = 0; - Func abortCallback = (MainLoop loop) => { - abortCount++; - Application.RequestStop (); - return false; - }; - var token = Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (ms), abortCallback); - - var scenario = (Scenario)Activator.CreateInstance (scenarioClass); - scenario.Init (Application.Top, Colors.Base); - scenario.Setup (); - // There is no need to call Application.Begin because Init already creates the Application.Top - // If Application.RunState is used then the Application.RunLoop must also be used instead Application.Run. - //var rs = Application.Begin (Application.Top); - scenario.Run (); - - //Application.End (rs); - - // Shutdown must be called to safely clean up Application if Init has been called - Application.Shutdown (); - - if(abortCount != 0) { - output.WriteLine ($"Scenario {scenarioClass} had abort count of {abortCount}"); - } - - Assert.Equal (0, abortCount); - // # of key up events should match # of iterations - Assert.Equal (1, iterations); - Assert.Equal (stackSize, iterations); } #if DEBUG_IDISPOSABLE foreach (var inst in Responder.Instances) {