diff --git a/UnitTests/ViewTests.cs b/UnitTests/ViewTests.cs index 1e5f5eb70..9dd570375 100644 --- a/UnitTests/ViewTests.cs +++ b/UnitTests/ViewTests.cs @@ -899,5 +899,105 @@ namespace Terminal.Gui { Application.Run (); Application.Shutdown (); } + + [Fact] + public void Multi_Thread_Toplevels () + { + Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true))); + + var t = Application.Top; + var w = new Window (); + t.Add (w); + + int count = 0, count1 = 0, count2 = 0; + bool log = false, log1 = false, log2 = false; + bool fromTopStillKnowFirstIsRunning = false; + bool fromTopStillKnowSecondIsRunning = false; + bool fromFirstStillKnowSecondIsRunning = false; + + Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => { + count++; + if (count1 == 5) { + log1 = true; + } + if (count1 > 13 && count < 15) { + fromTopStillKnowFirstIsRunning = true; + } + if (count2 > 6 && count2 < 8) { + fromTopStillKnowSecondIsRunning = true; + } + if (count == 30) { + Assert.Equal (30, count); + Assert.Equal (20, count1); + Assert.Equal (10, count2); + + Assert.True (log); + Assert.True (log1); + Assert.True (log2); + + Assert.True (fromTopStillKnowFirstIsRunning); + Assert.True (fromTopStillKnowSecondIsRunning); + Assert.True (fromFirstStillKnowSecondIsRunning); + + Application.RequestStop (); + return false; + } + return true; + }); + + t.Ready = () => { + FirstDialogToplevel (); + }; + + void FirstDialogToplevel () + { + var od = new OpenDialog { + Ready = () => { + SecoundDialogToplevel (); + } + }; + + Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => { + count1++; + if (count2 == 5) { + log2 = true; + } + if (count2 > 3 && count2 < 5) { + fromFirstStillKnowSecondIsRunning = true; + } + if (count1 == 20) { + Assert.Equal (20, count1); + Application.RequestStop (); + return false; + } + return true; + }); + + Application.Run (od); + } + + void SecoundDialogToplevel () + { + var d = new Dialog (); + + Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => { + count2++; + if (count < 30) { + log = true; + } + if (count2 == 10) { + Assert.Equal (10, count2); + Application.RequestStop (); + return false; + } + return true; + }); + + Application.Run (d); + } + + Application.Run (); + Application.Shutdown (); + } } }