浏览代码

Fix all tests.

BDisp 1 年之前
父节点
当前提交
41971a6e5a
共有 40 个文件被更改,包括 1556 次插入1193 次删除
  1. 4 2
      UnitTests/Dialogs/DialogTests.cs
  2. 0 1
      UnitTests/Dialogs/MessageBoxTests.cs
  3. 1 1
      UnitTests/Input/EscSeqUtilsTests.cs
  4. 8 6
      UnitTests/View/Adornment/BorderTests.cs
  5. 13 9
      UnitTests/View/DrawTests.cs
  6. 9 8
      UnitTests/View/Layout/DimTests.cs
  7. 24 15
      UnitTests/View/Layout/LayoutTests.cs
  8. 27 20
      UnitTests/View/Layout/PosTests.cs
  9. 3 1
      UnitTests/View/MouseTests.cs
  10. 58 45
      UnitTests/View/NavigationTests.cs
  11. 2 0
      UnitTests/View/SubviewTests.cs
  12. 410 398
      UnitTests/View/Text/AutoSizeTrueTests.cs
  13. 12 8
      UnitTests/View/ViewKeyBindingTests.cs
  14. 24 20
      UnitTests/View/ViewTests.cs
  15. 43 30
      UnitTests/Views/ComboBoxTests.cs
  16. 52 34
      UnitTests/Views/ContextMenuTests.cs
  17. 6 4
      UnitTests/Views/DatePickerTests.cs
  18. 3 2
      UnitTests/Views/FrameViewTests.cs
  19. 4 2
      UnitTests/Views/GraphViewTests.cs
  20. 16 11
      UnitTests/Views/HexViewTests.cs
  21. 38 26
      UnitTests/Views/LabelTests.cs
  22. 84 73
      UnitTests/Views/ListViewTests.cs
  23. 171 138
      UnitTests/Views/MenuBarTests.cs
  24. 127 25
      UnitTests/Views/OverlappedTests.cs
  25. 3 2
      UnitTests/Views/RadioGroupTests.cs
  26. 8 5
      UnitTests/Views/RuneCellTests.cs
  27. 115 99
      UnitTests/Views/ScrollBarViewTests.cs
  28. 36 28
      UnitTests/Views/ScrollViewTests.cs
  29. 3 2
      UnitTests/Views/SpinnerViewTests.cs
  30. 9 7
      UnitTests/Views/StatusBarTests.cs
  31. 10 7
      UnitTests/Views/TabViewTests.cs
  32. 18 12
      UnitTests/Views/TableViewTests.cs
  33. 19 14
      UnitTests/Views/TextFieldTests.cs
  34. 88 66
      UnitTests/Views/TextViewTests.cs
  35. 8 4
      UnitTests/Views/TileViewTests.cs
  36. 84 58
      UnitTests/Views/ToplevelTests.cs
  37. 8 4
      UnitTests/Views/TreeTableSourceTests.cs
  38. 3 2
      UnitTests/Views/TreeViewTests.cs
  39. 1 1
      UnitTests/Views/ViewDisposalTest.cs
  40. 4 3
      UnitTests/Views/WindowTests.cs

+ 4 - 2
UnitTests/Dialogs/DialogTests.cs

@@ -1211,7 +1211,7 @@ public class DialogTests
                          if (iterations == 0)
                          {
                              var d = new Dialog { X = 5, Y = 5, Height = 3, Width = 5 };
-                             Begin (d);
+                             var rs = Begin (d);
 
                              Assert.Equal (new Point (5, 5), (Point)d.Frame.Location);
 
@@ -1229,10 +1229,11 @@ public class DialogTests
 ╚══════════════════╝",
                                                                            _output
                                                                           );
+                             End (rs);
                              d.Dispose ();
 
                              d = new Dialog { X = 5, Y = 5 };
-                             Begin (d);
+                             rs = Begin (d);
 
                              // This is because of PostionTopLevels and EnsureVisibleBounds
                              Assert.Equal (new (3, 2), d.Frame.Location);
@@ -1268,6 +1269,7 @@ public class DialogTests
 ╚══════════════════╝",
                                                                            _output
                                                                           );
+                             End (rs);
                              d.Dispose ();
                          }
                          else if (iterations > 0)

+ 0 - 1
UnitTests/Dialogs/MessageBoxTests.cs

@@ -125,7 +125,6 @@ public class MessageBoxTests
     public void Location_Default ()
     {
         int iterations = -1;
-        Application.Begin (new ());
         ((FakeDriver)Application.Driver).SetBufferSize (100, 100);
 
         Application.Iteration += (s, a) =>

+ 1 - 1
UnitTests/Input/EscSeqUtilsTests.cs

@@ -770,7 +770,7 @@ public class EscSeqUtilsTests
                                      }
                                  };
 
-        Application.Run ();
+        Application.Run (top);
 
         Assert.Null (Application.WantContinuousButtonPressedView);
 

+ 8 - 6
UnitTests/View/Adornment/BorderTests.cs

@@ -711,12 +711,13 @@ public class BorderTests
     [AutoInitShutdown]
     public void HasSuperView ()
     {
-        Application.Top.BorderStyle = LineStyle.Double;
+        var top = new Toplevel ();
+        top.BorderStyle = LineStyle.Double;
 
         var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
 
-        Application.Top.Add (frame);
-        RunState rs = Application.Begin (Application.Top);
+        top.Add (frame);
+        RunState rs = Application.Begin (top);
         var firstIteration = false;
 
         ((FakeDriver)Application.Driver).SetBufferSize (5, 5);
@@ -737,12 +738,13 @@ public class BorderTests
     [AutoInitShutdown]
     public void HasSuperView_Title ()
     {
-        Application.Top.BorderStyle = LineStyle.Double;
+        var top = new Toplevel ();
+        top.BorderStyle = LineStyle.Double;
 
         var frame = new FrameView { Title = "1234", Width = Dim.Fill (), Height = Dim.Fill () };
 
-        Application.Top.Add (frame);
-        RunState rs = Application.Begin (Application.Top);
+        top.Add (frame);
+        RunState rs = Application.Begin (top);
         var firstIteration = false;
 
         ((FakeDriver)Application.Driver).SetBufferSize (10, 4);

+ 13 - 9
UnitTests/View/DrawTests.cs

@@ -70,7 +70,7 @@ public class DrawTests
         var view = new View { Text = r.ToString (), Height = Dim.Fill (), Width = Dim.Fill () };
         var tf = new TextField { Text = us, Y = 1, Width = 3 };
         win.Add (view, tf);
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (win);
 
         Application.Begin (top);
@@ -135,7 +135,8 @@ public class DrawTests
         };
         var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
         win.Add (tv);
-        Application.Top.Add (win);
+        var top = new Toplevel ();
+        top.Add (win);
 
         // Don't use Label. It sets AutoSize = true which is not what we're testing here.
         var view = new View { Text = "ワイドルーン。", Height = Dim.Fill (), Width = Dim.Fill () };
@@ -143,8 +144,8 @@ public class DrawTests
         // Don't have unit tests use things that aren't absolutely critical for the test, like Dialog
         var dg = new Window { X = 2, Y = 2, Width = 14, Height = 3 };
         dg.Add (view);
-        Application.Begin (Application.Top);
-        Application.Begin (dg);
+        RunState rsTop = Application.Begin (top);
+        RunState rsDiag = Application.Begin (dg);
         ((FakeDriver)Application.Driver).SetBufferSize (30, 10);
 
         const string expectedOutput = """
@@ -163,6 +164,9 @@ public class DrawTests
 
         Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expectedOutput, _output);
         Assert.Equal (new Rectangle (0, 0, 30, 10), pos);
+
+        Application.End (rsDiag);
+        Application.End (rsTop);
     }
 
     [Fact]
@@ -189,7 +193,7 @@ public class DrawTests
             VerticalTextAlignment = VerticalTextAlignment.Bottom,
             ColorScheme = Colors.ColorSchemes ["Base"]
         };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (viewRight, viewBottom);
 
         Application.Begin (top);
@@ -402,7 +406,7 @@ public class DrawTests
             Height = 5
         };
         container.Add (content);
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (container);
         Application.Driver.Clip = container.Frame;
         Application.Begin (top);
@@ -517,7 +521,7 @@ public class DrawTests
             Height = 5
         };
         container.Add (content);
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (container);
 
         // BUGBUG: v2 - it's bogus to reference .Frame before BeginInit. And why is the clip being set anyway???
@@ -604,7 +608,7 @@ public class DrawTests
             Height = 5
         };
         container.Add (content);
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (container);
         Application.Driver.Clip = container.Frame;
         Application.Begin (top);
@@ -723,7 +727,7 @@ public class DrawTests
         var view = new Label { Text = r.ToString () };
         var tf = new TextField { Text = us, Y = 1, Width = 3 };
         win.Add (view, tf);
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (win);
 
         Application.Begin (top);

+ 9 - 8
UnitTests/View/Layout/DimTests.cs

@@ -28,7 +28,7 @@ public class DimTests
     [AutoInitShutdown]
     public void Dim_Add_Operator ()
     {
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
 
         var view = new View { X = 0, Y = 0, Width = 20, Height = 0 };
         var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
@@ -102,7 +102,7 @@ public class DimTests
     [AutoInitShutdown]
     public void Dim_Subtract_Operator ()
     {
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
 
         var view = new View { X = 0, Y = 0, Width = 20, Height = 0 };
         var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
@@ -386,10 +386,11 @@ public class DimTests
         };
 
         container.Add (label);
-        Application.Top.Add (container);
-        Application.Top.BeginInit ();
-        Application.Top.EndInit ();
-        Application.Top.LayoutSubviews ();
+        var top = new Toplevel ();
+        top.Add (container);
+        top.BeginInit ();
+        top.EndInit ();
+        top.LayoutSubviews ();
 
         Assert.Equal (100, container.Frame.Width);
         Assert.Equal (100, container.Frame.Height);
@@ -528,7 +529,7 @@ public class DimTests
     public void Only_DimAbsolute_And_DimFactor_As_A_Different_Procedure_For_Assigning_Value_To_Width_Or_Height ()
     {
         // Testing with the Button because it properly handles the Dim class.
-        Toplevel t = Application.Top;
+        Toplevel t = new ();
 
         var w = new Window { Width = 100, Height = 100 };
 
@@ -765,7 +766,7 @@ public class DimTests
 
         Application.Iteration += (s, a) => Application.RequestStop ();
 
-        Application.Run ();
+        Application.Run (t);
     }
 
     [Fact]

+ 24 - 15
UnitTests/View/Layout/LayoutTests.cs

@@ -331,12 +331,13 @@ public class LayoutTests
     public void DimFill_SizedCorrectly ()
     {
         var view = new View { Width = Dim.Fill (), Height = Dim.Fill (), BorderStyle = LineStyle.Single };
-        Application.Top.Add (view);
-        RunState rs = Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (view);
+        RunState rs = Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (32, 5);
 
         //view.SetNeedsLayout ();
-        Application.Top.LayoutSubviews ();
+        top.LayoutSubviews ();
 
         //view.SetRelativeLayout (new (0, 0, 32, 5));
         Assert.Equal (32, view.Frame.Width);
@@ -349,7 +350,7 @@ public class LayoutTests
     {
         Application.Init (new FakeDriver ());
 
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
 
         var view = new View { X = -2, Text = "view" };
         top.Add (view);
@@ -363,7 +364,7 @@ public class LayoutTests
 
         try
         {
-            Application.Run ();
+            Application.Run (top);
         }
         catch (IndexOutOfRangeException ex)
         {
@@ -371,6 +372,7 @@ public class LayoutTests
             Assert.IsType<IndexOutOfRangeException> (ex);
         }
 
+        top.Dispose ();
         // Shutdown must be called to safely clean up Application if Init has been called
         Application.Shutdown ();
     }
@@ -381,7 +383,7 @@ public class LayoutTests
     {
         Application.Init (new FakeDriver ());
 
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
 
         var view = new View { Y = -2, Height = 10, TextDirection = TextDirection.TopBottom_LeftRight, Text = "view" };
         top.Add (view);
@@ -395,7 +397,7 @@ public class LayoutTests
 
         try
         {
-            Application.Run ();
+            Application.Run (top);
         }
         catch (IndexOutOfRangeException ex)
         {
@@ -403,6 +405,7 @@ public class LayoutTests
             Assert.IsType<IndexOutOfRangeException> (ex);
         }
 
+        top.Dispose ();
         // Shutdown must be called to safely clean up Application if Init has been called
         Application.Shutdown ();
     }
@@ -488,7 +491,7 @@ public class LayoutTests
     {
         Application.Init (new FakeDriver ());
 
-        Toplevel t = Application.Top;
+        Toplevel t = new ();
 
         var w = new Window { X = Pos.Left (t) + 2, Y = Pos.At (2) };
 
@@ -506,7 +509,8 @@ public class LayoutTests
 
         Application.Iteration += (s, a) => Application.RequestStop ();
 
-        Application.Run ();
+        Application.Run (t);
+        t.Dispose ();
         Application.Shutdown ();
     }
 
@@ -545,14 +549,16 @@ public class LayoutTests
     {
         Application.Init (new FakeDriver ());
 
-        var w = new Window { X = Pos.Left (Application.Top) + 2, Y = Pos.Top (Application.Top) + 2 };
+        var top = new Toplevel ();
+        var w = new Window { X = Pos.Left (top) + 2, Y = Pos.Top (top) + 2 };
         var f = new FrameView ();
         var v1 = new View { X = Pos.Left (w) + 2, Y = Pos.Top (w) + 2 };
         var v2 = new View { X = Pos.Left (v1) + 2, Y = Pos.Top (v1) + 2 };
 
         f.Add (v1, v2);
         w.Add (f);
-        Application.Top.Add (w);
+        top.Add (w);
+        Application.Begin (top);
 
         f.X = Pos.X (Application.Top) + Pos.X (v2) - Pos.X (v1);
         f.Y = Pos.Y (Application.Top) + Pos.Y (v2) - Pos.Y (v1);
@@ -574,6 +580,7 @@ public class LayoutTests
         Application.Iteration += (s, a) => Application.RequestStop ();
 
         Assert.Throws<InvalidOperationException> (() => Application.Run ());
+        top.Dispose ();
         Application.Shutdown ();
     }
 
@@ -634,8 +641,9 @@ public class LayoutTests
         Assert.Equal (10, rHeight);
         Assert.False (v.IsInitialized);
 
-        Application.Top.Add (top);
-        Application.Begin (Application.Top);
+        var toplevel = new Toplevel ();
+        toplevel.Add (top);
+        Application.Begin (toplevel);
 
         Assert.True (v.IsInitialized);
 
@@ -665,8 +673,9 @@ public class LayoutTests
         Assert.Equal (70, rWidth);
         Assert.False (v.IsInitialized);
 
-        Application.Top.Add (top);
-        Application.Begin (Application.Top);
+        var toplevel = new Toplevel ();
+        toplevel.Add (top);
+        Application.Begin (toplevel);
 
         Assert.True (v.IsInitialized);
         v.Width = 75;

+ 27 - 20
UnitTests/View/Layout/PosTests.cs

@@ -43,7 +43,7 @@ public class PosTests
 
         win.Add (tv);
 
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (win);
         RunState rs = Application.Begin (top);
 
@@ -73,7 +73,7 @@ public class PosTests
 
         var menu = new MenuBar ();
         var status = new StatusBar ();
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (win, menu, status);
         RunState rs = Application.Begin (top);
 
@@ -317,17 +317,18 @@ public class PosTests
     public void LeftTopBottomRight_Win_ShouldNotThrow ()
     {
         // Setup Fake driver
-        (Window win, Button button) setup ()
+        (Toplevel top, Window win, Button button) setup ()
         {
             Application.Init (new FakeDriver ());
             Application.Iteration += (s, a) => { Application.RequestStop (); };
             var win = new Window { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
-            Application.Top.Add (win);
+            var top = new Toplevel ();
+            top.Add (win);
 
             var button = new Button { X = Pos.Center (), Text = "button" };
             win.Add (button);
 
-            return (win, button);
+            return (top, win, button);
         }
 
         RunState rs;
@@ -337,14 +338,15 @@ public class PosTests
             // Cleanup
             Application.End (rs);
 
+            Application.Top.Dispose ();
             // Shutdown must be called to safely clean up Application if Init has been called
             Application.Shutdown ();
         }
 
         // Test cases:
-        (Window win, Button button) app = setup ();
+        (Toplevel top, Window win, Button button) app = setup ();
         app.button.Y = Pos.Left (app.win);
-        rs = Application.Begin (Application.Top);
+        rs = Application.Begin (app.top);
 
         // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
         Application.RunLoop (rs);
@@ -352,7 +354,7 @@ public class PosTests
 
         app = setup ();
         app.button.Y = Pos.X (app.win);
-        rs = Application.Begin (Application.Top);
+        rs = Application.Begin (app.top);
 
         // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
         Application.RunLoop (rs);
@@ -360,7 +362,7 @@ public class PosTests
 
         app = setup ();
         app.button.Y = Pos.Top (app.win);
-        rs = Application.Begin (Application.Top);
+        rs = Application.Begin (app.top);
 
         // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
         Application.RunLoop (rs);
@@ -368,7 +370,7 @@ public class PosTests
 
         app = setup ();
         app.button.Y = Pos.Y (app.win);
-        rs = Application.Begin (Application.Top);
+        rs = Application.Begin (app.top);
 
         // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
         Application.RunLoop (rs);
@@ -376,7 +378,7 @@ public class PosTests
 
         app = setup ();
         app.button.Y = Pos.Bottom (app.win);
-        rs = Application.Begin (Application.Top);
+        rs = Application.Begin (app.top);
 
         // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
         Application.RunLoop (rs);
@@ -384,7 +386,7 @@ public class PosTests
 
         app = setup ();
         app.button.Y = Pos.Right (app.win);
-        rs = Application.Begin (Application.Top);
+        rs = Application.Begin (app.top);
 
         // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
         Application.RunLoop (rs);
@@ -467,7 +469,7 @@ public class PosTests
     {
         Application.Init (new FakeDriver ());
 
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
 
         var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
         var field = new TextField { X = 0, Y = 0, Width = 20 };
@@ -510,6 +512,7 @@ public class PosTests
 
         Assert.Equal (20, count);
 
+        top.Dispose ();
         // Shutdown must be called to safely clean up Application if Init has been called
         Application.Shutdown ();
     }
@@ -522,7 +525,7 @@ public class PosTests
     {
         Application.Init (new FakeDriver ());
 
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
 
         var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
         var field = new TextField { X = 0, Y = 0, Width = 20 };
@@ -578,6 +581,7 @@ public class PosTests
 
         Assert.Equal (0, count);
 
+        top.Dispose ();
         // Shutdown must be called to safely clean up Application if Init has been called
         Application.Shutdown ();
     }
@@ -589,7 +593,7 @@ public class PosTests
     {
         Application.Init (new FakeDriver ());
 
-        Toplevel t = Application.Top;
+        Toplevel t = new ();
 
         var w = new Window { X = 1, Y = 2, Width = 3, Height = 5 };
         t.Add (w);
@@ -602,7 +606,8 @@ public class PosTests
 
         Application.Iteration += (s, a) => Application.RequestStop ();
 
-        Application.Run ();
+        Application.Run (t);
+        t.Dispose ();
         Application.Shutdown ();
     }
 
@@ -637,7 +642,7 @@ public class PosTests
     {
         Application.Init (new FakeDriver ());
 
-        Toplevel t = Application.Top;
+        Toplevel t = new ();
 
         var w = new Window { X = Pos.Left (t) + 2, Y = Pos.Top (t) + 2 };
         var f = new FrameView ();
@@ -651,7 +656,8 @@ public class PosTests
         f.X = Pos.X (v2) - Pos.X (v1);
         f.Y = Pos.Y (v2) - Pos.Y (v1);
 
-        Assert.Throws<InvalidOperationException> (() => Application.Run ());
+        Assert.Throws<InvalidOperationException> (() => Application.Run (t));
+        t.Dispose ();
         Application.Shutdown ();
 
         v2.Dispose ();
@@ -676,8 +682,9 @@ public class PosTests
         };
 
         container.Add (view);
-        Application.Top.Add (container);
-        Application.Top.LayoutSubviews ();
+        var top = new Toplevel ();
+        top.Add (container);
+        top.LayoutSubviews ();
 
         Assert.Equal (100, container.Frame.Width);
         Assert.Equal (100, container.Frame.Height);

+ 3 - 1
UnitTests/View/MouseTests.cs

@@ -68,7 +68,9 @@ public class MouseTests (ITestOutputHelper output)
         testView.Border.Thickness = new (borderThickness);
         testView.Padding.Thickness = new (paddingThickness);
 
-        Application.Top.Add (testView);
+        var top = new Toplevel ();
+        top.Add (testView);
+        Application.Begin (top);
 
         Assert.Equal (new Point (4, 4), testView.Frame.Location);
         Application.OnMouseEvent (new (new () { X = xy, Y = xy, Flags = MouseFlags.Button1Pressed }));

+ 58 - 45
UnitTests/View/NavigationTests.cs

@@ -57,7 +57,7 @@ public class NavigationTests
     {
         Application.Init (new FakeDriver ());
 
-        Toplevel t = Application.Top;
+        Toplevel t = new ();
 
         var w = new Window ();
         var f = new FrameView ();
@@ -84,7 +84,8 @@ public class NavigationTests
 
         Application.Iteration += (s, a) => Application.RequestStop ();
 
-        Application.Run ();
+        Application.Run (t);
+        t.Dispose ();
         Application.Shutdown ();
     }
 
@@ -93,7 +94,7 @@ public class NavigationTests
     {
         Application.Init (new FakeDriver ());
 
-        Toplevel t = Application.Top;
+        Toplevel t = new ();
 
         var w = new Window ();
         var f = new FrameView ();
@@ -126,7 +127,8 @@ public class NavigationTests
 
         Application.Iteration += (s, a) => Application.RequestStop ();
 
-        Application.Run ();
+        Application.Run (t);
+        t.Dispose ();
         Application.Shutdown ();
     }
 
@@ -166,7 +168,7 @@ public class NavigationTests
     {
         Application.Init (new FakeDriver ());
 
-        Toplevel t = Application.Top;
+        Toplevel t = new ();
 
         var w = new Window ();
         var f = new FrameView ();
@@ -201,7 +203,8 @@ public class NavigationTests
 
         Application.Iteration += (s, a) => Application.RequestStop ();
 
-        Application.Run ();
+        Application.Run (t);
+        t.Dispose ();
         Application.Shutdown ();
     }
 
@@ -210,7 +213,7 @@ public class NavigationTests
     {
         Application.Init (new FakeDriver ());
 
-        Toplevel t = Application.Top;
+        Toplevel t = new ();
 
         var w = new Window ();
         var f = new FrameView ();
@@ -238,7 +241,8 @@ public class NavigationTests
 
         Application.Iteration += (s, a) => Application.RequestStop ();
 
-        Application.Run ();
+        Application.Run (t);
+        t.Dispose ();
         Application.Shutdown ();
     }
 
@@ -292,8 +296,9 @@ public class NavigationTests
         var view = new View { CanFocus = true };
         var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
         win.Add (view);
-        Application.Top.Add (win);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (win);
+        Application.Begin (top);
 
         Assert.True (view.CanFocus);
         Assert.True (view.HasFocus);
@@ -315,21 +320,22 @@ public class NavigationTests
         var view2 = new View { Id = "view2", Width = 20, Height = 2, CanFocus = true };
         var win2 = new Window { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
         win2.Add (view2);
-        Application.Top.Add (win1, win2);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (win1, win2);
+        Application.Begin (top);
 
         Assert.True (view1.CanFocus);
         Assert.True (view1.HasFocus);
         Assert.True (view2.CanFocus);
         Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
 
-        Assert.True (Application.Top.NewKeyDownEvent (Key.Tab));
+        Assert.True (top.NewKeyDownEvent (Key.Tab));
         Assert.True (view1.CanFocus);
         Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
         Assert.True (view2.CanFocus);
         Assert.True (view2.HasFocus);
 
-        Assert.True (Application.Top.NewKeyDownEvent (Key.Tab));
+        Assert.True (top.NewKeyDownEvent (Key.Tab));
         Assert.True (view1.CanFocus);
         Assert.True (view1.HasFocus);
         Assert.True (view2.CanFocus);
@@ -354,21 +360,22 @@ public class NavigationTests
         var view2 = new View { Id = "view2", Width = 20, Height = 2, CanFocus = true };
         var win2 = new Window { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
         win2.Add (view2);
-        Application.Top.Add (win1, win2);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (win1, win2);
+        Application.Begin (top);
 
         Assert.True (view1.CanFocus);
         Assert.True (view1.HasFocus);
         Assert.True (view2.CanFocus);
         Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
 
-        Assert.True (Application.Top.NewKeyDownEvent (Key.Tab.WithCtrl));
+        Assert.True (top.NewKeyDownEvent (Key.Tab.WithCtrl));
         Assert.True (view1.CanFocus);
         Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
         Assert.True (view2.CanFocus);
         Assert.True (view2.HasFocus);
 
-        Assert.True (Application.Top.NewKeyDownEvent (Key.Tab.WithCtrl));
+        Assert.True (top.NewKeyDownEvent (Key.Tab.WithCtrl));
         Assert.True (view1.CanFocus);
         Assert.True (view1.HasFocus);
         Assert.True (view2.CanFocus);
@@ -404,22 +411,23 @@ public class NavigationTests
         var view2 = new View { Id = "view2", Width = 20, Height = 2, CanFocus = true };
         var win2 = new Window { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
         win2.Add (view2);
-        Application.Top.Add (win1, win2);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (win1, win2);
+        Application.Begin (top);
 
         Assert.True (view1.CanFocus);
         Assert.True (view1.HasFocus);
         Assert.True (view2.CanFocus);
         Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
 
-        Assert.True (Application.Top.NewKeyDownEvent (Key.Tab.WithCtrl));
-        Assert.True (Application.Top.NewKeyDownEvent (Key.Tab.WithCtrl));
+        Assert.True (top.NewKeyDownEvent (Key.Tab.WithCtrl));
+        Assert.True (top.NewKeyDownEvent (Key.Tab.WithCtrl));
         Assert.True (view1.CanFocus);
         Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
         Assert.True (view2.CanFocus);
         Assert.True (view2.HasFocus);
 
-        Assert.True (Application.Top.NewKeyDownEvent (Key.Tab.WithCtrl));
+        Assert.True (top.NewKeyDownEvent (Key.Tab.WithCtrl));
         Assert.True (view1.CanFocus);
         Assert.True (view1.HasFocus);
         Assert.True (view2.CanFocus);
@@ -441,7 +449,6 @@ public class NavigationTests
         var wasClicked = false;
         var view = new Button { Text = "Click Me" };
         view.Accept += (s, e) => wasClicked = !wasClicked;
-        Application.Top.Add (view);
 
         view.NewKeyDownEvent (Key.Space);
         Assert.True (wasClicked);
@@ -473,7 +480,8 @@ public class NavigationTests
         button.Accept += (s, e) => wasClicked = !wasClicked;
         var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
         win.Add (button);
-        Application.Top.Add (win);
+        var top = new Toplevel ();
+        top.Add (win);
 
         var iterations = 0;
 
@@ -518,7 +526,7 @@ public class NavigationTests
                                      Application.RequestStop ();
                                  };
 
-        Application.Run ();
+        Application.Run (top);
 
         Assert.Equal (1, iterations);
     }
@@ -556,7 +564,7 @@ public class NavigationTests
     [AutoInitShutdown]
     public void FocusNext_Does_Not_Throws_If_A_View_Was_Removed_From_The_Collection ()
     {
-        Toplevel top1 = Application.Top;
+        Toplevel top1 = new ();
         var view1 = new View { Id = "view1", Width = 10, Height = 5, CanFocus = true };
         var top2 = new Toplevel { Id = "top2", Y = 1, Width = 10, Height = 5 };
 
@@ -644,7 +652,7 @@ public class NavigationTests
 
         var win = new Window ();
         win.Add (sb, tf);
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.KeyDown += Top_KeyPress;
 
         void Top_KeyPress (object sender, Key obj)
@@ -718,7 +726,7 @@ public class NavigationTests
 
         var win = new Window ();
         win.Add (sb, tf);
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (win);
         Application.Begin (top);
 
@@ -746,14 +754,16 @@ public class NavigationTests
 
         Application.Init (new FakeDriver ());
 
-        Application.Top.Ready += (s, e) => { Assert.Null (Application.Top.Focused); };
+        var top = new Toplevel ();
+        top.Ready += (s, e) => { Assert.Null (top.Focused); };
 
         // Keyboard navigation with tab
         Console.MockKeyPresses.Push (new ConsoleKeyInfo ('\t', ConsoleKey.Tab, false, false, false));
 
         Application.Iteration += (s, a) => Application.RequestStop ();
 
-        Application.Run ();
+        Application.Run (top);
+        top.Dispose ();
         Application.Shutdown ();
     }
 
@@ -761,8 +771,9 @@ public class NavigationTests
     [AutoInitShutdown]
     public void Remove_Does_Not_Change_Focus ()
     {
-        Assert.True (Application.Top.CanFocus);
-        Assert.False (Application.Top.HasFocus);
+        var top = new Toplevel ();
+        Assert.True (top.CanFocus);
+        Assert.False (top.HasFocus);
 
         var container = new View { Width = 10, Height = 10 };
         var leave = false;
@@ -776,11 +787,11 @@ public class NavigationTests
         Assert.True (child.CanFocus);
         Assert.False (child.HasFocus);
 
-        Application.Top.Add (container);
-        Application.Begin (Application.Top);
+        top.Add (container);
+        Application.Begin (top);
 
-        Assert.True (Application.Top.CanFocus);
-        Assert.True (Application.Top.HasFocus);
+        Assert.True (top.CanFocus);
+        Assert.True (top.HasFocus);
         Assert.True (container.CanFocus);
         Assert.True (container.HasFocus);
         Assert.True (child.CanFocus);
@@ -789,7 +800,7 @@ public class NavigationTests
         container.Remove (child);
         child.Dispose ();
         child = null;
-        Assert.True (Application.Top.HasFocus);
+        Assert.True (top.HasFocus);
         Assert.True (container.CanFocus);
         Assert.True (container.HasFocus);
         Assert.Null (child);
@@ -800,7 +811,7 @@ public class NavigationTests
     [AutoInitShutdown]
     public void ScreenToView_ViewToScreen_FindDeepestView_Full_Top ()
     {
-        Toplevel top = Application.Current;
+        Toplevel top = new ();
         top.BorderStyle = LineStyle.Single;
 
         var view = new View
@@ -1120,13 +1131,14 @@ public class NavigationTests
     [AutoInitShutdown]
     public void SetFocus_View_With_Null_Superview_Does_Not_Throw_Exception ()
     {
-        Assert.True (Application.Top.CanFocus);
-        Assert.False (Application.Top.HasFocus);
+        var top = new Toplevel ();
+        Assert.True (top.CanFocus);
+        Assert.False (top.HasFocus);
 
-        Exception exception = Record.Exception (Application.Top.SetFocus);
+        Exception exception = Record.Exception (top.SetFocus);
         Assert.Null (exception);
-        Assert.True (Application.Top.CanFocus);
-        Assert.True (Application.Top.HasFocus);
+        Assert.True (top.CanFocus);
+        Assert.True (top.HasFocus);
     }
 
     [Fact]
@@ -1136,7 +1148,7 @@ public class NavigationTests
         var view1Leave = false;
         var subView1Leave = false;
         var subView1subView1Leave = false;
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         var view1 = new View { CanFocus = true };
         var subView1 = new View { CanFocus = true };
         var subView1subView1 = new View { CanFocus = true };
@@ -1459,6 +1471,7 @@ public class NavigationTests
         // Act
         RunState rs = Application.Begin (top);
         Application.End (rs);
+        top.Dispose ();
         Application.Shutdown ();
 
         // Assert does Not throw NullReferenceException

+ 2 - 0
UnitTests/View/SubviewTests.cs

@@ -206,6 +206,7 @@ public class SubviewTests
                                  };
 
         Application.Run (top);
+        top.Dispose ();
         Application.Shutdown ();
 
         Assert.Equal (1, tc);
@@ -301,6 +302,7 @@ public class SubviewTests
                                  };
 
         Application.Run (t);
+        t.Dispose ();
         Application.Shutdown ();
 
         Assert.Equal (1, tc);

文件差异内容过多而无法显示
+ 410 - 398
UnitTests/View/Text/AutoSizeTrueTests.cs


+ 12 - 8
UnitTests/View/ViewKeyBindingTests.cs

@@ -15,8 +15,9 @@ public class ViewKeyBindingTests
         var invoked = false;
         view.InvokingKeyBindings += (s, e) => invoked = true;
 
-        Application.Top.Add (view);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (view);
+        Application.Begin (top);
 
         Application.OnKeyDown (Key.A);
         Assert.True (invoked);
@@ -51,8 +52,9 @@ public class ViewKeyBindingTests
         var invoked = false;
         view.InvokingKeyBindings += (s, e) => invoked = true;
 
-        Application.Top.Add (view);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (view);
+        Application.Begin (top);
 
         Application.OnKeyDown (Key.Z);
         Assert.False (invoked);
@@ -77,8 +79,9 @@ public class ViewKeyBindingTests
         var invoked = false;
         view.InvokingKeyBindings += (s, e) => invoked = true;
 
-        Application.Top.Add (view);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (view);
+        Application.Begin (top);
 
         invoked = false;
         Application.OnKeyDown (Key.H);
@@ -105,8 +108,9 @@ public class ViewKeyBindingTests
         var invoked = false;
         view.InvokingKeyBindings += (s, e) => invoked = true;
 
-        Application.Top.Add (view);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (view);
+        Application.Begin (top);
 
         Application.OnKeyDown (Key.Z);
         Assert.False (invoked);

+ 24 - 20
UnitTests/View/ViewTests.cs

@@ -33,8 +33,9 @@ public class ViewTests
                                 Application.Driver.Clip = savedClip;
                                 e.Cancel = true;
                             };
-        Application.Top.Add (view);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (view);
+        Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
 
         var expected = @"
@@ -95,8 +96,9 @@ public class ViewTests
                                 Application.Driver.Clip = savedClip;
                                 e.Cancel = true;
                             };
-        Application.Top.Add (view);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (view);
+        Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
 
         var expected = @"
@@ -147,8 +149,9 @@ public class ViewTests
 
         root.Add (v);
 
-        Application.Top.Add (root);
-        RunState runState = Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (root);
+        RunState runState = Application.Begin (top);
 
         if (label)
         {
@@ -232,7 +235,7 @@ cccccccccccccccccccc",
             Height = 2,
             Text = "A text with some long width\n and also with two lines."
         };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (label, view);
         RunState runState = Application.Begin (top);
 
@@ -277,7 +280,7 @@ At 0,0
             Height = 2,
             Text = "A text with some long width\n and also with two lines."
         };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (label, view);
         RunState runState = Application.Begin (top);
 
@@ -326,7 +329,7 @@ At 0,0
             Height = 2,
             Text = "A text with some long width\n and also with two lines."
         };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (label, view);
         RunState runState = Application.Begin (top);
 
@@ -371,7 +374,7 @@ At 0,0
             Height = 2,
             Text = "A text with some long width\n and also with two lines."
         };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (label, view);
         RunState runState = Application.Begin (top);
 
@@ -435,8 +438,9 @@ At 0,0
         var tv = new TextView { Y = 11, Width = 10, Height = 10 };
         tv.DrawContentComplete += (s, e) => tvCalled = true;
 
-        Application.Top.Add (view, tv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+       top.Add (view, tv);
+        Application.Begin (top);
 
         Assert.True (viewCalled);
         Assert.True (tvCalled);
@@ -467,7 +471,7 @@ At 0,0
         frame.Width = 40;
         frame.Height = 8;
 
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
 
         top.Add (frame);
 
@@ -539,7 +543,7 @@ At 0,0
             Height = 2,
             Text = "A text with some long width\n and also with two lines."
         };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (label, view);
         RunState runState = Application.Begin (top);
 
@@ -584,7 +588,7 @@ At 0,0
             Height = 2,
             Text = "A text with some long width\n and also with two lines."
         };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (label, view);
         RunState runState = Application.Begin (top);
 
@@ -633,7 +637,7 @@ At 0,0
             Height = 2,
             Text = "A text with some long width\n and also with two lines."
         };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (label, view);
         RunState runState = Application.Begin (top);
 
@@ -680,7 +684,7 @@ At 0,0
             Height = 2,
             Text = "A text with some long width\n and also with two lines."
         };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (label, view);
         RunState runState = Application.Begin (top);
 
@@ -999,7 +1003,7 @@ At 0,0
         Assert.Equal (0, view.Height);
         var win = new Window ();
         win.Add (view);
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (win);
         RunState rs = Application.Begin (top);
 
@@ -1044,7 +1048,7 @@ At 0,0
         var button = new Button { Text = "Click Me" };
         var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
         win.Add (button);
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (win);
 
         var iterations = 0;
@@ -1087,7 +1091,7 @@ At 0,0
                                      Application.RequestStop ();
                                  };
 
-        Application.Run ();
+        Application.Run (top);
 
         Assert.Equal (1, iterations);
 

+ 43 - 30
UnitTests/Views/ComboBoxTests.cs

@@ -86,7 +86,8 @@ public class ComboBoxTests
         string [] source = Enumerable.Range (0, 15).Select (x => x.ToString ()).ToArray ();
         comboBox.SetSource (source);
 
-        Application.Top.Add (comboBox);
+        var top = new Toplevel ();
+        top.Add (comboBox);
 
         foreach (KeyCode key in (KeyCode [])Enum.GetValues (typeof (KeyCode)))
         {
@@ -104,8 +105,9 @@ public class ComboBoxTests
         cb.Expanded += (s, e) => cb.SetSource (list);
         cb.Collapsed += (s, e) => cb.Source = null;
 
-        Application.Top.Add (cb);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (cb);
+        Application.Begin (top);
 
         Assert.Null (cb.Source);
         Assert.False (cb.IsShow);
@@ -133,8 +135,9 @@ public class ComboBoxTests
         var cb = new ComboBox { Height = 4, Width = 5, HideDropdownListOnClick = false };
         cb.SetSource (new List<string> { "One", "Two", "Three" });
         cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
-        Application.Top.Add (cb);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (cb);
+        Application.Begin (top);
 
         Assert.False (cb.HideDropdownListOnClick);
         Assert.False (cb.ReadOnly);
@@ -190,8 +193,9 @@ public class ComboBoxTests
         var cb = new ComboBox { Height = 4, Width = 5, HideDropdownListOnClick = false };
         cb.SetSource (new List<string> { "One", "Two", "Three" });
         cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
-        Application.Top.Add (cb);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (cb);
+        Application.Begin (top);
 
         Assert.False (cb.HideDropdownListOnClick);
         Assert.False (cb.IsShow);
@@ -225,8 +229,9 @@ public class ComboBoxTests
         var cb = new ComboBox { Height = 4, Width = 5, HideDropdownListOnClick = false, ReadOnly = true };
         cb.SetSource (new List<string> { "One", "Two", "Three" });
         cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
-        Application.Top.Add (cb);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (cb);
+        Application.Begin (top);
 
         Assert.False (cb.HideDropdownListOnClick);
         Assert.True (cb.ReadOnly);
@@ -282,8 +287,9 @@ public class ComboBoxTests
         var cb = new ComboBox { Height = 4, Width = 5 };
         cb.SetSource (new List<string> { "One", "Two", "Three" });
         cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
-        Application.Top.Add (cb);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (cb);
+        Application.Begin (top);
 
         Assert.False (cb.HideDropdownListOnClick);
         Assert.False (cb.IsShow);
@@ -382,8 +388,9 @@ public class ComboBoxTests
         var cb = new ComboBox { Height = 4, Width = 5, HideDropdownListOnClick = true };
         cb.SetSource (new List<string> { "One", "Two", "Three" });
         cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
-        Application.Top.Add (cb);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (cb);
+        Application.Begin (top);
 
         Assert.True (cb.HideDropdownListOnClick);
         Assert.False (cb.IsShow);
@@ -499,8 +506,9 @@ public class ComboBoxTests
         var cb = new ComboBox { Width = 6, Height = 4, HideDropdownListOnClick = true };
         cb.SetSource (new List<string> { "One", "Two", "Three" });
         cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
-        Application.Top.Add (cb);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (cb);
+        Application.Begin (top);
 
         Assert.True (cb.HideDropdownListOnClick);
         Assert.False (cb.IsShow);
@@ -655,8 +663,9 @@ Three ",
         var cb = new ComboBox { Height = 4, Width = 5, HideDropdownListOnClick = true };
         cb.SetSource (new List<string> { "One", "Two", "Three" });
         cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
-        Application.Top.Add (cb);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (cb);
+        Application.Begin (top);
 
         Assert.True (cb.HideDropdownListOnClick);
         Assert.False (cb.IsShow);
@@ -716,8 +725,9 @@ Three ",
         var cb = new ComboBox { Height = 4, Width = 5, HideDropdownListOnClick = true };
         cb.SetSource (new List<string> { "One", "Two", "Three" });
         cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
-        Application.Top.Add (cb);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (cb);
+        Application.Begin (top);
 
         Assert.True (cb.HideDropdownListOnClick);
         Assert.False (cb.IsShow);
@@ -772,8 +782,9 @@ Three ",
         var cb = new ComboBox { Height = 4, Width = 5, HideDropdownListOnClick = true };
         cb.SetSource (new List<string> { "One", "Two", "Three" });
         cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
-        Application.Top.Add (cb);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (cb);
+        Application.Begin (top);
 
         Assert.True (cb.HideDropdownListOnClick);
         Assert.False (cb.IsShow);
@@ -805,8 +816,9 @@ Three ",
         List<string> source = new () { "One", "Two", "Three" };
         var cb = new ComboBox { Width = 10 };
         cb.SetSource (source);
-        Application.Top.Add (cb);
-        Application.Top.FocusFirst ();
+        var top = new Toplevel ();
+        top.Add (cb);
+        top.FocusFirst ();
         Assert.Equal (-1, cb.SelectedItem);
         Assert.Equal (string.Empty, cb.Text);
         var opened = false;
@@ -835,7 +847,7 @@ Three ",
         Assert.True (cb.NewKeyDownEvent (Key.CursorDown)); // losing focus
         Assert.False (cb.IsShow);
         Assert.False (cb.HasFocus);
-        Application.Top.FocusFirst (); // Gets focus again
+        top.FocusFirst (); // Gets focus again
         Assert.False (cb.IsShow);
         Assert.True (cb.HasFocus);
         cb.Expand ();
@@ -866,7 +878,7 @@ Three ",
         Assert.True (cb.IsShow);
         Assert.Equal (0, cb.SelectedItem);
         Assert.Equal ("One", cb.Text);
-        Application.Begin (Application.Top);
+        Application.Begin (top);
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
@@ -880,7 +892,7 @@ One
         Assert.True (cb.IsShow);
         Assert.Equal (1, cb.SelectedItem);
         Assert.Equal ("Two", cb.Text);
-        Application.Begin (Application.Top);
+        Application.Begin (top);
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
@@ -894,7 +906,7 @@ Two
         Assert.True (cb.IsShow);
         Assert.Equal (2, cb.SelectedItem);
         Assert.Equal ("Three", cb.Text);
-        Application.Begin (Application.Top);
+        Application.Begin (top);
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
@@ -944,7 +956,7 @@ Three
         Assert.False (cb.IsShow);
         Assert.Equal (-1, cb.SelectedItem);
         Assert.Equal ("One", cb.Text);
-        Application.Top.FocusFirst (); // Gets focus again
+        top.FocusFirst (); // Gets focus again
         Assert.True (cb.HasFocus);
         Assert.False (cb.IsShow);
         Assert.Equal (-1, cb.SelectedItem);
@@ -962,8 +974,9 @@ Three
     public void Source_Equal_Null_Or_Count_Equal_Zero_Sets_SelectedItem_Equal_To_Minus_One ()
     {
         var cb = new ComboBox ();
-        Application.Top.Add (cb);
-        Application.Top.FocusFirst ();
+        var top = new Toplevel ();
+        top.Add (cb);
+        top.FocusFirst ();
         Assert.Null (cb.Source);
         Assert.Equal (-1, cb.SelectedItem);
         List<string> source = new ();

+ 52 - 34
UnitTests/Views/ContextMenuTests.cs

@@ -77,8 +77,9 @@ public class ContextMenuTests
             ]
         };
 
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
         Assert.Null (Application.MouseGrabView);
 
@@ -142,9 +143,10 @@ public class ContextMenuTests
                                        ]
                                       );
 
-        Application.Top.Add (menu, label, tf, statusBar);
+        var top = new Toplevel ();
+        top.Add (menu, label, tf, statusBar);
         ((FakeDriver)Application.Driver).SetBufferSize (45, 17);
-        Application.Begin (Application.Top);
+        Application.Begin (top);
 
         Assert.Equal (new Rectangle (9, 3, 20, 1), tf.Frame);
         Assert.True (tf.HasFocus);
@@ -208,8 +210,9 @@ public class ContextMenuTests
                                        }
                                       );
 
-        Application.Top.Add (menu, win, statusBar);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu, win, statusBar);
+        Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (44, 17);
 
         Assert.Equal (new Rectangle (9, 3, 20, 1), tf.Frame);
@@ -297,10 +300,10 @@ public class ContextMenuTests
     [AutoInitShutdown]
     public void Draw_A_ContextMenu_Over_A_Dialog ()
     {
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         var win = new Window ();
         top.Add (win);
-        Application.Begin (top);
+        RunState rsTop = Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (20, 15);
 
         Assert.Equal (new Rectangle (0, 0, 20, 15), win.Frame);
@@ -328,7 +331,7 @@ public class ContextMenuTests
         // Don't use Dialog here as it has more layout logic. Use Window instead.
         var dialog = new Window { X = 2, Y = 2, Width = 15, Height = 4 };
         dialog.Add (new TextField { X = Pos.Center (), Width = 10, Text = "Test" });
-        RunState rs = Application.Begin (dialog);
+        RunState rsDialog = Application.Begin (dialog);
 
         Assert.Equal (new Rectangle (2, 2, 15, 4), dialog.Frame);
 
@@ -359,7 +362,7 @@ public class ContextMenuTests
                                  );
 
         var firstIteration = false;
-        Application.RunIteration (ref rs, ref firstIteration);
+        Application.RunIteration (ref rsDialog, ref firstIteration);
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
@@ -381,7 +384,8 @@ public class ContextMenuTests
                                                       _output
                                                      );
 
-        Application.End (rs);
+        Application.End (rsDialog);
+        Application.End (rsTop);
     }
 
     [Fact]
@@ -455,9 +459,11 @@ public class ContextMenuTests
 
         Assert.Equal (new Point (-1, -2), cm.Position);
 
+        Application.Begin (new ());
+
         cm.Show ();
         Assert.Equal (new Point (-1, -2), cm.Position);
-        Application.Begin (Application.Top);
+        Application.Refresh ();
 
         var expected = @"
 ┌──────┐
@@ -499,7 +505,7 @@ public class ContextMenuTests
                                         )
         };
 
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         Application.Begin (top);
         top.Running = true;
 
@@ -517,8 +523,9 @@ public class ContextMenuTests
     public void Key_Open_And_Close_The_ContextMenu ()
     {
         var tf = new TextField ();
-        Application.Top.Add (tf);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tf);
+        Application.Begin (top);
 
         Assert.True (Application.OnKeyDown (ContextMenu.DefaultKey));
         Assert.True (tf.ContextMenu.MenuBar.IsMenuOpen);
@@ -555,8 +562,9 @@ public class ContextMenuTests
                                         )
         };
 
+        Application.Begin (new ());
         cm.Show ();
-        Application.Begin (Application.Top);
+        Application.Refresh ();
 
         var expected = @"
           ┌──────┐
@@ -621,10 +629,13 @@ public class ContextMenuTests
 
         Assert.Equal (new Point (-1, -2), cm.Position);
 
+        Toplevel top = new ();
+        Application.Begin (top);
+
         cm.Show ();
+        Application.Refresh ();
+
         Assert.Equal (new Point (-1, -2), cm.Position);
-        Toplevel top = Application.Top;
-        Application.Begin (top);
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
@@ -864,7 +875,7 @@ public class ContextMenuTests
                               }
                           };
 
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (lbl);
         Application.Begin (top);
 
@@ -906,8 +917,9 @@ public class ContextMenuTests
                                         )
         };
 
+        Application.Begin (new ());
         cm.Show ();
-        Application.Begin (Application.Top);
+        Application.Refresh ();
 
         var expected = @"
           ┌──────┐
@@ -1027,9 +1039,10 @@ public class ContextMenuTests
 
         Assert.Equal (Point.Empty, cm.Position);
 
+        Application.Begin (new ());
         cm.Show ();
         Assert.Equal (Point.Empty, cm.Position);
-        Application.Begin (Application.Top);
+        Application.Refresh ();
 
         var expected = @"
 ┌──────┐
@@ -1062,9 +1075,10 @@ public class ContextMenuTests
 
         Assert.Equal (Point.Empty, cm.Position);
 
+        Application.Begin (new ());
         cm.Show ();
         Assert.Equal (Point.Empty, cm.Position);
-        Application.Begin (Application.Top);
+        Application.Refresh ();
 
         var expected = @"
 ┌────
@@ -1104,13 +1118,14 @@ public class ContextMenuTests
                                         )
         };
 
-        Application.Top.Add (view);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (view);
+        Application.Begin (top);
 
         Assert.Equal (new Point (10, 5), cm.Position);
 
         cm.Show ();
-        Application.Top.Draw ();
+        top.Draw ();
         Assert.Equal (new Point (10, 5), cm.Position);
 
         var expected = @"
@@ -1168,9 +1183,10 @@ public class ContextMenuTests
 
         Assert.Equal (new Point (80, 25), cm.Position);
 
+        Application.Begin (new ());
         cm.Show ();
         Assert.Equal (new Point (80, 25), cm.Position);
-        Application.Begin (Application.Top);
+        Application.Refresh ();
 
         var expected = @"
                                                                         ┌──────┐
@@ -1210,15 +1226,16 @@ public class ContextMenuTests
                                         )
         };
 
-        Application.Top.Add (view);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (view);
+        Application.Begin (top);
 
         Assert.Equal (new Rectangle (70, 24, 10, 1), view.Frame);
         Assert.Equal (Point.Empty, cm.Position);
 
         cm.Show ();
         Assert.Equal (new Point (70, 24), cm.Position);
-        Application.Top.Draw ();
+        top.Draw ();
 
         var expected = @"
                                                                       ┌──────┐
@@ -1241,10 +1258,10 @@ public class ContextMenuTests
     {
         ContextMenu cm = Create_ContextMenu_With_Two_MenuItem (10, 5);
 
+        Application.Begin (new ());
         cm.Show ();
         Assert.True (ContextMenu.IsShow);
-
-        Application.Begin (Application.Top);
+        Application.Refresh ();
 
         var expected = @"
           ┌──────┐
@@ -1293,10 +1310,10 @@ public class ContextMenuTests
             UseSubMenusSingleFrame = true
         };
 
+        RunState rs = Application.Begin (new ());
         cm.Show ();
-        RunState rs = Application.Begin (Application.Top);
-
         Assert.Equal (new Rectangle (5, 11, 10, 5), Application.Top.Subviews [0].Frame);
+        Application.Refresh ();
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
@@ -1390,10 +1407,11 @@ public class ContextMenuTests
                                         )
         };
 
+        RunState rs = Application.Begin (new ());
         cm.Show ();
-        RunState rs = Application.Begin (Application.Top);
 
         Assert.Equal (new Rectangle (5, 11, 10, 5), Application.Top.Subviews [0].Frame);
+        Application.Refresh ();
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"

+ 6 - 4
UnitTests/Views/DatePickerTests.cs

@@ -49,8 +49,9 @@ public class DatePickerTests
         var date = new DateTime (9999, 11, 15);
         var datePicker = new DatePicker (date);
 
-        Application.Top.Add (datePicker);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+       top.Add (datePicker);
+        Application.Begin (top);
 
         // Set focus to next month button
         datePicker.FocusNext ();
@@ -72,10 +73,11 @@ public class DatePickerTests
     {
         var date = new DateTime (1, 2, 15);
         var datePicker = new DatePicker (date);
+        var top = new Toplevel ();
 
         // Move focus to previous month button
-        Application.Top.Add (datePicker);
-        Application.Begin (Application.Top);
+        top.Add (datePicker);
+        Application.Begin (top);
 
         // set focus to the previous month button
         datePicker.FocusNext ();

+ 3 - 2
UnitTests/Views/FrameViewTests.cs

@@ -44,8 +44,9 @@ public class FrameViewTests
         var fv = new FrameView ();
         Assert.Equal (string.Empty, fv.Title);
         Assert.Equal (string.Empty, fv.Text);
-        Application.Top.Add (fv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (fv);
+        Application.Begin (top);
         Assert.Equal (new Rectangle (0, 0, 0, 0), fv.Frame);
         TestHelpers.AssertDriverContentsWithFrameAre (@"", _output);
 

+ 4 - 2
UnitTests/Views/GraphViewTests.cs

@@ -1500,6 +1500,7 @@ public class PathAnnotationTests
 
         // create a wide window
         var mount = new View { Width = 100, Height = 100 };
+        var top = new Toplevel ();
 
         try
         {
@@ -1521,8 +1522,8 @@ public class PathAnnotationTests
 
             //putting mount into Toplevel since changing size
             //also change AutoSize to false
-            Application.Top.Add (mount);
-            Application.Begin (Application.Top);
+            top.Add (mount);
+            Application.Begin (top);
 
             // render view
             view.ColorScheme = new ColorScheme ();
@@ -1541,6 +1542,7 @@ public class PathAnnotationTests
         }
         finally
         {
+            top.Dispose ();
             Application.Shutdown ();
         }
     }

+ 16 - 11
UnitTests/Views/HexViewTests.cs

@@ -98,8 +98,9 @@ public class HexViewTests
     public void CursorPosition_Encoding_Default ()
     {
         var hv = new HexView (LoadStream ()) { Width = Dim.Fill (), Height = Dim.Fill () };
-        Application.Top.Add (hv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (hv);
+        Application.Begin (top);
 
         Assert.Equal (new Point (1, 1), hv.CursorPosition);
 
@@ -126,8 +127,9 @@ public class HexViewTests
     public void CursorPosition_Encoding_Unicode ()
     {
         var hv = new HexView (LoadStream (true)) { Width = Dim.Fill (), Height = Dim.Fill () };
-        Application.Top.Add (hv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (hv);
+        Application.Begin (top);
 
         Assert.Equal (new Point (1, 1), hv.CursorPosition);
 
@@ -220,8 +222,9 @@ public class HexViewTests
     public void KeyBindings_Command ()
     {
         var hv = new HexView (LoadStream ()) { Width = 20, Height = 10 };
-        Application.Top.Add (hv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (hv);
+        Application.Begin (top);
 
         Assert.Equal (63, hv.Source.Length);
         Assert.Equal (1, hv.Position);
@@ -348,8 +351,9 @@ public class HexViewTests
         var hv = new HexView (LoadStream ()) { Width = Dim.Fill (), Height = Dim.Fill () };
         HexViewEventArgs hexViewEventArgs = null;
         hv.PositionChanged += (s, e) => hexViewEventArgs = e;
-        Application.Top.Add (hv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (hv);
+        Application.Begin (top);
 
         Assert.True (hv.NewKeyDownEvent (Key.CursorRight)); // left side must press twice
         Assert.True (hv.NewKeyDownEvent (Key.CursorRight));
@@ -365,8 +369,9 @@ public class HexViewTests
     public void Source_Sets_DisplayStart_And_Position_To_Zero_If_Greater_Than_Source_Length ()
     {
         var hv = new HexView (LoadStream ()) { Width = 10, Height = 5 };
-        Application.Top.Add (hv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (hv);
+        Application.Begin (top);
 
         Assert.True (hv.NewKeyDownEvent (Key.End));
         Assert.Equal (62, hv.DisplayStart);
@@ -379,7 +384,7 @@ public class HexViewTests
         hv.Source = LoadStream ();
         hv.Width = Dim.Fill ();
         hv.Height = Dim.Fill ();
-        Application.Top.LayoutSubviews ();
+        top.LayoutSubviews ();
         Assert.Equal (0, hv.DisplayStart);
         Assert.Equal (0, hv.Position - 1);
 

+ 38 - 26
UnitTests/Views/LabelTests.cs

@@ -96,11 +96,12 @@ public class LabelTests
 
         var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
         win.Add (label);
-        Application.Top.Add (win);
+        var top = new Toplevel ();
+        top.Add (win);
 
         Assert.True (label.AutoSize);
 
-        Application.Begin (Application.Top);
+        Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
 
         var expected = @"
@@ -137,11 +138,12 @@ public class LabelTests
 
         var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
         win.Add (label);
-        Application.Top.Add (win);
+        var top = new Toplevel ();
+        top.Add (win);
 
         Assert.True (label.AutoSize);
 
-        Application.Begin (Application.Top);
+        Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
 
         var expected = @"
@@ -178,7 +180,8 @@ public class LabelTests
 
         var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
         win.Add (label);
-        Application.Top.Add (win);
+        var top = new Toplevel ();
+        top.Add (win);
 
         Assert.True (label.AutoSize);
 
@@ -186,7 +189,7 @@ public class LabelTests
 
         Assert.True (label.AutoSize);
 
-        Application.Begin (Application.Top);
+        Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
 
         var expected = @"
@@ -226,8 +229,9 @@ public class LabelTests
         tf2.Text = "This TextFormatter (tf2) with fill will be cleared on rewritten.";
         Size tf2Size = tf2.Size;
 
-        Application.Top.Add (label);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (label);
+        Application.Begin (top);
 
         Assert.True (label.AutoSize);
 
@@ -277,8 +281,9 @@ This TextFormatter (tf2) is rewritten.
     public void Label_Draw_Horizontal_Simple_Runes ()
     {
         var label = new Label { Text = "Demo Simple Rune" };
-        Application.Top.Add (label);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (label);
+        Application.Begin (top);
 
         Assert.True (label.AutoSize);
         Assert.Equal (new Rectangle (0, 0, 16, 1), label.Frame);
@@ -296,8 +301,9 @@ Demo Simple Rune
     public void Label_Draw_Vertical_Simple_Runes ()
     {
         var label = new Label { TextDirection = TextDirection.TopBottom_LeftRight, Text = "Demo Simple Rune" };
-        Application.Top.Add (label);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (label);
+        Application.Begin (top);
 
         Assert.NotNull (label.Width);
         Assert.NotNull (label.Height);
@@ -330,8 +336,9 @@ e
     public void Label_Draw_Vertical_Wide_Runes ()
     {
         var label = new Label { TextDirection = TextDirection.TopBottom_LeftRight, Text = "デモエムポンズ" };
-        Application.Top.Add (label);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (label);
+        Application.Begin (top);
 
         var expected = @"
@@ -411,11 +418,12 @@ e
         var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
         var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
         win.Add (label);
-        Application.Top.Add (win);
+        var top = new Toplevel ();
+        top.Add (win);
 
         Assert.False (label.IsInitialized);
 
-        Application.Begin (Application.Top);
+        Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
 
         Assert.True (label.IsInitialized);
@@ -441,11 +449,12 @@ e
         var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你", AutoSize = true };
         var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
         win.Add (label);
-        Application.Top.Add (win);
+        var top = new Toplevel ();
+        top.Add (win);
 
         Assert.False (label.IsInitialized);
 
-        Application.Begin (Application.Top);
+        Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
 
         Assert.True (label.IsInitialized);
@@ -471,8 +480,9 @@ e
     public void Full_Border ()
     {
         var label = new Label { Text = "Test", /*Width = 6, Height = 3, */BorderStyle = LineStyle.Single };
-        Application.Top.Add (label);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (label);
+        Application.Begin (top);
 
         Assert.Equal (new (0, 0, 6, 3), label.Frame);
         Assert.Equal (new (0, 0, 4, 1), label.Bounds);
@@ -493,12 +503,13 @@ e
         var label = new Label { Text = "Test", /*Width = 6, Height = 3,*/ BorderStyle = LineStyle.Single };
         label.Margin.Thickness = new Thickness (0, 1, 0, 0);
         label.Border.Thickness = new Thickness (1, 0, 1, 1);
-        Application.Top.Add (label);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (label);
+        Application.Begin (top);
 
         Assert.Equal (new (0, 0, 6, 3), label.Frame);
         Assert.Equal (new (0, 0, 4, 1), label.Bounds);
-        Application.Begin (Application.Top);
+        Application.Begin (top);
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
@@ -514,12 +525,13 @@ e
     {
         var label = new Label { Text = "Test", /* Width = 6, Height = 3, */BorderStyle = LineStyle.Single };
         label.Border.Thickness = new Thickness (1, 0, 1, 1);
-        Application.Top.Add (label);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (label);
+        Application.Begin (top);
 
         Assert.Equal (new (0, 0, 6, 2), label.Frame);
         Assert.Equal (new (0, 0, 4, 1), label.Bounds);
-        Application.Begin (Application.Top);
+        Application.Begin (top);
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"

+ 84 - 73
UnitTests/Views/ListViewTests.cs

@@ -53,8 +53,9 @@ public class ListViewTests
         var lv = new ListView { Width = Dim.Fill (), Height = Dim.Fill (), Source = new ListWrapper (source) };
         var win = new Window ();
         win.Add (lv);
-        Application.Top.Add (win);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (win);
+        Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (12, 12);
         Application.Refresh ();
 
@@ -300,8 +301,9 @@ public class ListViewTests
         }
 
         var lv = new ListView { Width = 10, Height = 5, Source = new ListWrapper (source) };
-        Application.Top.Add (lv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (lv);
+        Application.Begin (top);
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
@@ -335,8 +337,9 @@ Item 6",
         List<string> source = new () { "First", "Second" };
         var lv = new ListView { Width = Dim.Fill (), Height = 1, Source = new ListWrapper (source) };
         lv.SelectedItem = 1;
-        Application.Top.Add (lv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (lv);
+        Application.Begin (top);
 
         Assert.Equal ("Second ", GetContents (0));
         Assert.Equal (new string (' ', 7), GetContents (1));
@@ -618,8 +621,9 @@ Item 6",
         List<string> source = new () { "one", "two", "three" };
         var lv = new ListView { Width = Dim.Fill (), Height = Dim.Fill () };
         lv.RowRender += (s, _) => rendered = true;
-        Application.Top.Add (lv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (lv);
+        Application.Begin (top);
         Assert.False (rendered);
 
         lv.SetSource (source);
@@ -688,69 +692,76 @@ Item 6",
         public IList ToList () { return new List<string> { "One", "Two", "Three" }; }
     }
 
-    // No longer needed given PR #2920
-    //		[Fact, AutoInitShutdown]
-    //		public void Clicking_On_Border_Is_Ignored ()
-    //		{
-    //			var selected = "";
-    //			var lv = new ListView {
-    //				Height = 5,
-    //				Width = 7,
-    //				BorderStyle = LineStyle.Single
-    //			};
-    //			lv.SetSource (new List<string> { "One", "Two", "Three", "Four" });
-    //			lv.SelectedItemChanged += (s, e) => selected = e.Value.ToString ();
-    //			Application.Top.Add (lv);
-    //			Application.Begin (Application.Top);
-
-    //			Assert.Equal (new Thickness (1), lv.Border.Thickness);
-    //			Assert.Equal (-1, lv.SelectedItem);
-    //			Assert.Equal ("", lv.Text);
-    //			TestHelpers.AssertDriverContentsWithFrameAre (@"
-    //┌─────┐
-    //│One  │
-    //│Two  │
-    //│Three│
-    //└─────┘", output);
-
-    //			Assert.True (lv.OnMouseEvent (new MouseEvent {
-    //				X = 0,
-    //				Y = 0,
-    //				Flags = MouseFlags.Button1Clicked
-    //			}));
-    //			Assert.Equal ("", selected);
-    //			Assert.Equal (-1, lv.SelectedItem);
-
-    //			Assert.True (lv.OnMouseEvent (new MouseEvent {
-    //				X = 0,
-    //				Y = 1,
-    //				Flags = MouseFlags.Button1Clicked
-    //			}));
-    //			Assert.Equal ("One", selected);
-    //			Assert.Equal (0, lv.SelectedItem);
-
-    //			Assert.True (lv.OnMouseEvent (new MouseEvent {
-    //				X = 0,
-    //				Y = 2,
-    //				Flags = MouseFlags.Button1Clicked
-    //			}));
-    //			Assert.Equal ("Two", selected);
-    //			Assert.Equal (1, lv.SelectedItem);
-
-    //			Assert.True (lv.OnMouseEvent (new MouseEvent {
-    //				X = 0,
-    //				Y = 3,
-    //				Flags = MouseFlags.Button1Clicked
-    //			}));
-    //			Assert.Equal ("Three", selected);
-    //			Assert.Equal (2, lv.SelectedItem);
-
-    //			Assert.True (lv.OnMouseEvent (new MouseEvent {
-    //				X = 0,
-    //				Y = 4,
-    //				Flags = MouseFlags.Button1Clicked
-    //			}));
-    //			Assert.Equal ("Three", selected);
-    //			Assert.Equal (2, lv.SelectedItem);
-    //		}
+    [Fact]
+    [AutoInitShutdown]
+    public void Clicking_On_Border_Is_Ignored ()
+    {
+        var selected = "";
+        var lv = new ListView
+        {
+            Height = 5,
+            Width = 7,
+            BorderStyle = LineStyle.Single
+        };
+        lv.SetSource (new List<string> { "One", "Two", "Three", "Four" });
+        lv.SelectedItemChanged += (s, e) => selected = e.Value.ToString ();
+        var top = new Toplevel ();
+        top.Add (lv);
+        Application.Begin (top);
+
+        Assert.Equal (new Thickness (1), lv.Border.Thickness);
+        Assert.Equal (-1, lv.SelectedItem);
+        Assert.Equal ("", lv.Text);
+        TestHelpers.AssertDriverContentsWithFrameAre (@"
+┌─────┐
+│One  │
+│Two  │
+│Three│
+└─────┘", _output);
+
+        Application.OnMouseEvent (new (new ()
+        {
+            X = 0,
+            Y = 0,
+            Flags = MouseFlags.Button1Clicked
+        }));
+        Assert.Equal ("", selected);
+        Assert.Equal (-1, lv.SelectedItem);
+
+        Application.OnMouseEvent (new (new ()
+        {
+            X = 1,
+            Y = 1,
+            Flags = MouseFlags.Button1Clicked
+        }));
+        Assert.Equal ("One", selected);
+        Assert.Equal (0, lv.SelectedItem);
+
+        Application.OnMouseEvent (new (new ()
+        {
+            X = 1,
+            Y = 2,
+            Flags = MouseFlags.Button1Clicked
+        }));
+        Assert.Equal ("Two", selected);
+        Assert.Equal (1, lv.SelectedItem);
+
+        Application.OnMouseEvent (new (new ()
+        {
+            X = 1,
+            Y = 3,
+            Flags = MouseFlags.Button1Clicked
+        }));
+        Assert.Equal ("Three", selected);
+        Assert.Equal (2, lv.SelectedItem);
+
+        Application.OnMouseEvent (new (new ()
+        {
+            X = 1,
+            Y = 4,
+            Flags = MouseFlags.Button1Clicked
+        }));
+        Assert.Equal ("Three", selected);
+        Assert.Equal (2, lv.SelectedItem);
+    }
 }

+ 171 - 138
UnitTests/Views/MenuBarTests.cs

@@ -26,7 +26,7 @@ public class MenuBarTests
         };
 
         //new CheckBox ();
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (menu);
         Application.Begin (top);
 
@@ -135,7 +135,7 @@ public class MenuBarTests
                                 )
             ]
         };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (menu);
 
         bool CanExecuteNew () { return win == null; }
@@ -230,7 +230,7 @@ public class MenuBarTests
     [AutoInitShutdown]
     public void Disabled_MenuBar_Is_Never_Opened ()
     {
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
 
         var menu = new MenuBar
         {
@@ -272,7 +272,7 @@ public class MenuBarTests
             ]
         };
 
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (menu);
         Application.Begin (top);
 
@@ -362,10 +362,10 @@ public class MenuBarTests
     [AutoInitShutdown]
     public void Draw_A_Menu_Over_A_Dialog ()
     {
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         var win = new Window ();
         top.Add (win);
-        Application.Begin (top);
+        RunState rsTop = Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (40, 15);
 
         Assert.Equal (new Rectangle (0, 0, 40, 15), win.Frame);
@@ -467,7 +467,7 @@ public class MenuBarTests
             menu.SetNeedsDisplay ();
         }
 
-        RunState rs = Application.Begin (dialog);
+        RunState rsDialog = Application.Begin (dialog);
 
         Assert.Equal (new Rectangle (2, 2, 15, 4), dialog.Frame);
 
@@ -494,7 +494,7 @@ public class MenuBarTests
         Assert.Equal ("File", menu.Menus [0].Title);
         menu.OpenMenu ();
         var firstIteration = false;
-        Application.RunIteration (ref rs, ref firstIteration);
+        Application.RunIteration (ref rsDialog, ref firstIteration);
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
@@ -526,7 +526,7 @@ public class MenuBarTests
 
         // Need to fool MainLoop into thinking it's running
         Application.MainLoop.Running = true;
-        Application.RunIteration (ref rs, ref firstIteration);
+        Application.RunIteration (ref rsDialog, ref firstIteration);
         Assert.Equal (items [0], menu.Menus [0].Title);
 
         TestHelpers.AssertDriverContentsWithFrameAre (
@@ -560,14 +560,14 @@ public class MenuBarTests
                                      );
 
             firstIteration = false;
-            Application.RunIteration (ref rs, ref firstIteration);
+            Application.RunIteration (ref rsDialog, ref firstIteration);
             Assert.Equal (items [i], menu.Menus [0].Title);
         }
 
         ((FakeDriver)Application.Driver).SetBufferSize (20, 15);
         menu.OpenMenu ();
         firstIteration = false;
-        Application.RunIteration (ref rs, ref firstIteration);
+        Application.RunIteration (ref rsDialog, ref firstIteration);
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
@@ -589,7 +589,8 @@ public class MenuBarTests
                                                       _output
                                                      );
 
-        Application.End (rs);
+        Application.End (rsDialog);
+        Application.End (rsTop);
     }
 
     [Fact]
@@ -787,8 +788,9 @@ public class MenuBarTests
 
         Assert.Equal (new Point (-1, -1), new Point (menu.Frame.X, menu.Frame.Y));
 
+        Application.Begin (new ());
         menu.OpenMenu ();
-        Application.Begin (Application.Top);
+        Application.Refresh ();
 
         var expected = @"
 ──────┐
@@ -863,8 +865,9 @@ public class MenuBarTests
 
         Assert.Equal (new Point (-2, -1), new Point (menu.Frame.X, menu.Frame.Y));
 
+        Application.Begin (new ());
         menu.OpenMenu ();
-        Application.Begin (Application.Top);
+        Application.Refresh ();
 
         var expected = @"
 ne
@@ -924,8 +927,9 @@ wo
 
         Assert.Equal (Point.Empty, new Point (menu.Frame.X, menu.Frame.Y));
 
+        Application.Begin (new ());
         menu.OpenMenu ();
-        Application.Begin (Application.Top);
+        Application.Refresh ();
 
         var expected = @"
 ┌──────┐
@@ -953,8 +957,9 @@ wo
 
         Assert.Equal (Point.Empty, new Point (menu.Frame.X, menu.Frame.Y));
 
+        Application.Begin (new ());
         menu.OpenMenu ();
-        Application.Begin (Application.Top);
+        Application.Refresh ();
 
         var expected = @"
  One
@@ -990,8 +995,9 @@ wo
             ]
         };
 
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
         Assert.False (newAction);
         Assert.False (copyAction);
@@ -1098,8 +1104,9 @@ wo
             ]
         };
 
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
         Assert.False (newAction);
         Assert.False (copyAction);
@@ -1134,21 +1141,22 @@ wo
                 new MenuBarItem ("File", new MenuItem [] { new ("New", "", null) })
             ]
         };
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
-        Assert.True (Application.Top.NewKeyDownEvent (menu.Key));
+        Assert.True (top.NewKeyDownEvent (menu.Key));
         Assert.True (menu.IsMenuOpen);
-        Assert.True (Application.Top.NewKeyDownEvent (menu.Key));
+        Assert.True (top.NewKeyDownEvent (menu.Key));
         Assert.False (menu.IsMenuOpen);
 
         menu.Key = Key.F10.WithShift;
-        Assert.False (Application.Top.NewKeyDownEvent (Key.F9));
+        Assert.False (top.NewKeyDownEvent (Key.F9));
         Assert.False (menu.IsMenuOpen);
 
-        Assert.True (Application.Top.NewKeyDownEvent (Key.F10.WithShift));
+        Assert.True (top.NewKeyDownEvent (Key.F10.WithShift));
         Assert.True (menu.IsMenuOpen);
-        Assert.True (Application.Top.NewKeyDownEvent (Key.F10.WithShift));
+        Assert.True (top.NewKeyDownEvent (Key.F10.WithShift));
         Assert.False (menu.IsMenuOpen);
     }
 
@@ -1266,12 +1274,13 @@ wo
                                 miCurrent = null;
                             };
         menu.UseKeysUpDownAsKeysLeftRight = true;
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
         foreach (KeyCode key in keys)
         {
-            Application.Top.NewKeyDownEvent (new Key (key));
+            top.NewKeyDownEvent (new Key (key));
             Application.MainLoop.RunIteration ();
         }
 
@@ -1313,12 +1322,13 @@ wo
                             };
         menu.UseKeysUpDownAsKeysLeftRight = true;
 
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
         foreach (KeyCode key in keys)
         {
-            Assert.True (Application.Top.NewKeyDownEvent (new Key (key)));
+            Assert.True (top.NewKeyDownEvent (new Key (key)));
             Application.MainLoop.RunIteration ();
         }
 
@@ -1352,8 +1362,9 @@ wo
             ]
         };
 
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
         menu.OpenMenu ();
         Application.Refresh ();
@@ -1398,8 +1409,9 @@ wo
             ]
         };
 
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
         menu.OpenMenu ();
         Application.Refresh ();
@@ -1457,18 +1469,19 @@ wo
             ]
         };
 
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
         Assert.True (menu.OnMouseEvent (new MouseEvent { X = 1, Y = 0, Flags = MouseFlags.Button1Pressed, View = menu }));
         Assert.True (menu.IsMenuOpen);
-        Application.Top.Draw ();
+        top.Draw ();
 
         TestHelpers.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (0), _output);
 
         Assert.True (menu.OnMouseEvent (new MouseEvent { X = 1, Y = 0, Flags = MouseFlags.Button1Pressed, View = menu }));
         Assert.False (menu.IsMenuOpen);
-        Application.Top.Draw ();
+        top.Draw ();
         TestHelpers.AssertDriverContentsAre (expectedMenu.ClosedMenuText, _output);
     }
 
@@ -1497,7 +1510,7 @@ wo
             ]
         };
         win.Add (menu);
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (win);
         Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (40, 8);
@@ -1605,7 +1618,7 @@ wo
             ]
         };
         win.Add (menu);
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (win);
 
         Application.Iteration += (s, a) =>
@@ -1692,7 +1705,7 @@ wo
                                      Application.RequestStop ();
                                  };
 
-        Application.Run ();
+        Application.Run (top);
     }
 
     [Fact]
@@ -1938,28 +1951,29 @@ wo
             ]
         };
 
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
         // Open first
         Assert.True (menu.NewKeyDownEvent (menu.Key));
         Assert.True (menu.IsMenuOpen);
-        Application.Top.Draw ();
+        top.Draw ();
         TestHelpers.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (0), _output);
 
         // Open second
         Assert.True (Application.Top.Subviews [1].NewKeyDownEvent (Key.CursorRight));
         Assert.True (menu.IsMenuOpen);
-        Application.Top.Draw ();
+        top.Draw ();
         TestHelpers.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (1), _output);
 
         // Close menu
         Assert.True (menu.NewKeyDownEvent (menu.Key));
         Assert.False (menu.IsMenuOpen);
-        Application.Top.Draw ();
+        top.Draw ();
         TestHelpers.AssertDriverContentsAre (expectedMenu.ClosedMenuText, _output);
 
-        Application.Top.Remove (menu);
+        top.Remove (menu);
 
         // Now test WITH HotKeys
         menu = new MenuBar
@@ -1984,16 +1998,16 @@ wo
             ]
         };
 
-        Application.Top.Add (menu);
+        top.Add (menu);
 
         // Open first
         Assert.True (menu.NewKeyDownEvent (menu.Key));
         Assert.True (menu.IsMenuOpen);
-        Application.Top.Draw ();
+        top.Draw ();
         TestHelpers.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (0), _output);
 
         // Open second
-        Assert.True (Application.Top.Subviews [1].NewKeyDownEvent (Key.CursorRight));
+        Assert.True (top.Subviews [1].NewKeyDownEvent (Key.CursorRight));
         Assert.True (menu.IsMenuOpen);
         Application.Top.Draw ();
         TestHelpers.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (1), _output);
@@ -2001,7 +2015,7 @@ wo
         // Close menu
         Assert.True (menu.NewKeyDownEvent (menu.Key));
         Assert.False (menu.IsMenuOpen);
-        Application.Top.Draw ();
+        top.Draw ();
         TestHelpers.AssertDriverContentsAre (expectedMenu.ClosedMenuText, _output);
     }
 
@@ -2064,17 +2078,18 @@ wo
 
         var menu = new MenuBar { Menus = items };
 
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
-        Application.Top.Draw ();
+        top.Draw ();
         TestHelpers.AssertDriverContentsAre (expectedMenu.ClosedMenuText, _output);
 
         for (var i = 0; i < expectedMenu.Menus.Length; i++)
         {
             menu.OpenMenu (i);
             Assert.True (menu.IsMenuOpen);
-            Application.Top.Draw ();
+            top.Draw ();
             TestHelpers.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (i), _output);
         }
     }
@@ -2093,8 +2108,9 @@ wo
             ]
         };
 
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
 #if SUPPORT_ALT_TO_ACTIVATE_MENU
         Assert.True (
@@ -2120,7 +2136,8 @@ wo
                 new MenuBarItem ("Test", "", null)
             ]
         };
-        Application.Top.Add (menu);
+        var top = new Toplevel ();
+        top.Add (menu);
 
         Exception exception = Record.Exception (() => menu.NewKeyDownEvent (Key.Space));
         Assert.Null (exception);
@@ -2166,8 +2183,9 @@ wo
                                mCurrent = menu._openMenu;
                            };
         menu.UseKeysUpDownAsKeysLeftRight = true;
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
         // open the menu
         Assert.True (
@@ -2295,14 +2313,15 @@ wo
                                     isMenuClosed = true;
                                 }
                             };
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
         Assert.True (menu.NewKeyDownEvent (menu.Key));
         Assert.True (menu.IsMenuOpen);
         isMenuClosed = !menu.IsMenuOpen;
         Assert.False (isMenuClosed);
-        Application.Top.Draw ();
+        top.Draw ();
 
         var expected = @"
 Edit
@@ -2316,7 +2335,7 @@ Edit
         Assert.True (menu.NewKeyDownEvent (menu.Key));
         Assert.True (menu.IsMenuOpen);
         Assert.False (isMenuClosed);
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
 Edit
@@ -2330,7 +2349,7 @@ Edit
         Assert.True (menu.NewKeyDownEvent (menu.Key));
         Assert.False (menu.IsMenuOpen);
         Assert.True (isMenuClosed);
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
 Edit
@@ -2369,9 +2388,10 @@ Edit
                                miCurrent = e.MenuItem;
                                mCurrent = menu.openCurrentMenu;
                            };
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
-        
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
+
         // Click on Edit
         Assert.True (
                      menu.OnMouseEvent (
@@ -2470,14 +2490,15 @@ Edit
         var menu = new MenuBar { Menus = items };
 
         var tf = new TextField { Y = 2, Width = 10 };
-        Application.Top.Add (menu, tf);
+        var top = new Toplevel ();
+        top.Add (menu, tf);
 
-        Application.Begin (Application.Top);
+        Application.Begin (top);
         Assert.True (tf.HasFocus);
         Assert.True (menu.NewKeyDownEvent (menu.Key));
         Assert.True (menu.IsMenuOpen);
         Assert.False (tf.HasFocus);
-        Application.Top.Draw ();
+        top.Draw ();
         TestHelpers.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (0), _output);
 
         // Right - Edit has no sub menu; this tests that no sub menu shows
@@ -2487,33 +2508,33 @@ Edit
         Assert.Equal (1, menu._selected);
         Assert.Equal (-1, menu._selectedSub);
         Assert.Null (menu._openSubMenu);
-        Application.Top.Draw ();
+        top.Draw ();
         TestHelpers.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (1), _output);
 
         // Right - Format
         Assert.True (menu._openMenu.NewKeyDownEvent (Key.CursorRight));
         Assert.True (menu.IsMenuOpen);
         Assert.False (tf.HasFocus);
-        Application.Top.Draw ();
+        top.Draw ();
         TestHelpers.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (2), _output);
 
         // Left - Edit
         Assert.True (menu._openMenu.NewKeyDownEvent (Key.CursorLeft));
         Assert.True (menu.IsMenuOpen);
         Assert.False (tf.HasFocus);
-        Application.Top.Draw ();
+        top.Draw ();
         TestHelpers.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (1), _output);
 
         Assert.True (menu._openMenu.NewKeyDownEvent (Key.CursorLeft));
         Assert.True (menu.IsMenuOpen);
         Assert.False (tf.HasFocus);
-        Application.Top.Draw ();
+        top.Draw ();
         TestHelpers.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (0), _output);
 
         Assert.True (menu.NewKeyDownEvent (menu.Key));
         Assert.False (menu.IsMenuOpen);
         Assert.True (tf.HasFocus);
-        Application.Top.Draw ();
+        top.Draw ();
         TestHelpers.AssertDriverContentsAre (expectedMenu.ClosedMenuText, _output);
     }
 
@@ -2564,14 +2585,15 @@ Edit
         };
 
         var tf = new TextField { Y = 2, Width = 10 };
-        Application.Top.Add (menu, tf);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu, tf);
+        Application.Begin (top);
 
         Assert.True (tf.HasFocus);
         Assert.True (menu.OnMouseEvent (new MouseEvent { X = 1, Y = 0, Flags = MouseFlags.Button1Pressed, View = menu }));
         Assert.True (menu.IsMenuOpen);
         Assert.False (tf.HasFocus);
-        Application.Top.Draw ();
+        top.Draw ();
         TestHelpers.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (0), _output);
 
         Assert.True (
@@ -2581,7 +2603,7 @@ Edit
                     );
         Assert.True (menu.IsMenuOpen);
         Assert.False (tf.HasFocus);
-        Application.Top.Draw ();
+        top.Draw ();
         TestHelpers.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (1), _output);
 
         Assert.True (
@@ -2591,7 +2613,7 @@ Edit
                     );
         Assert.True (menu.IsMenuOpen);
         Assert.False (tf.HasFocus);
-        Application.Top.Draw ();
+        top.Draw ();
         TestHelpers.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (2), _output);
 
         Assert.True (
@@ -2601,7 +2623,7 @@ Edit
                     );
         Assert.True (menu.IsMenuOpen);
         Assert.False (tf.HasFocus);
-        Application.Top.Draw ();
+        top.Draw ();
         TestHelpers.AssertDriverContentsAre (expectedMenu.ClosedMenuText, _output);
 
         Assert.True (
@@ -2611,13 +2633,13 @@ Edit
                     );
         Assert.True (menu.IsMenuOpen);
         Assert.False (tf.HasFocus);
-        Application.Top.Draw ();
+        top.Draw ();
         TestHelpers.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (0), _output);
 
         Assert.True (menu.OnMouseEvent (new MouseEvent { X = 8, Y = 0, Flags = MouseFlags.Button1Pressed, View = menu }));
         Assert.False (menu.IsMenuOpen);
         Assert.True (tf.HasFocus);
-        Application.Top.Draw ();
+        top.Draw ();
         TestHelpers.AssertDriverContentsAre (expectedMenu.ClosedMenuText, _output);
     }
 
@@ -2699,8 +2721,9 @@ Edit
                                 )
             ]
         };
-        Application.Top.Add (menu);
-        RunState rs = Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        RunState rs = Application.Begin (top);
 
         menu.OpenMenu ();
         var firstIteration = false;
@@ -2773,8 +2796,9 @@ Edit
             ]
         };
 
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
         Application.OnKeyDown (Key.S.WithCtrl);
         Application.MainLoop.RunIteration ();
@@ -2825,13 +2849,14 @@ Edit
             }
         };
         menu.UseKeysUpDownAsKeysLeftRight = true;
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
         Assert.Equal (Point.Empty, new Point (menu.Frame.X, menu.Frame.Y));
         Assert.False (menu.UseSubMenusSingleFrame);
 
-        Application.Top.Draw ();
+        top.Draw ();
 
         var expected = @"
  Numbers
@@ -2840,7 +2865,7 @@ Edit
         Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
 
         Assert.True (menu.NewKeyDownEvent (menu.Key));
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  Numbers  
@@ -2854,7 +2879,7 @@ Edit
         pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
 
         Assert.True (Application.Top.Subviews [1].NewKeyDownEvent (Key.CursorDown));
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  Numbers                 
@@ -2869,7 +2894,7 @@ Edit
         pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
 
         Assert.True (Application.Top.Subviews [2].NewKeyDownEvent (Key.CursorLeft));
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  Numbers  
@@ -2883,7 +2908,7 @@ Edit
         pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
 
         Assert.True (Application.Top.Subviews [1].NewKeyDownEvent (Key.Esc));
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  Numbers
@@ -2927,13 +2952,14 @@ Edit
             ]
         };
 
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
         Assert.Equal (Point.Empty, new Point (menu.Frame.X, menu.Frame.Y));
         Assert.False (menu.UseSubMenusSingleFrame);
 
-        Application.Top.Draw ();
+        top.Draw ();
 
         var expected = @"
  Numbers
@@ -2947,7 +2973,7 @@ Edit
                                       new MouseEvent { X = 1, Y = 0, Flags = MouseFlags.Button1Pressed, View = menu }
                                      )
                     );
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  Numbers  
@@ -2969,7 +2995,7 @@ Edit
                                        }
                                       )
                      );
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  Numbers                 
@@ -2992,7 +3018,7 @@ Edit
                                        }
                                       )
                      );
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  Numbers  
@@ -3011,7 +3037,7 @@ Edit
                                        new MouseEvent { X = 70, Y = 2, Flags = MouseFlags.Button1Clicked, View = Application.Top }
                                       )
                      );
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  Numbers
@@ -3058,14 +3084,15 @@ Edit
         };
 
         menu.UseKeysUpDownAsKeysLeftRight = true;
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
         Assert.Equal (Point.Empty, new Point (menu.Frame.X, menu.Frame.Y));
         Assert.False (menu.UseSubMenusSingleFrame);
 
         Assert.True (menu.NewKeyDownEvent (menu.Key));
-        Application.Top.Draw ();
+        top.Draw ();
 
         var expected = @"
  Numbers
@@ -3076,7 +3103,7 @@ Edit
         _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
 
         Assert.True (Application.Top.Subviews [1].NewKeyDownEvent (Key.CursorDown));
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  Numbers           
@@ -3122,15 +3149,16 @@ Edit
             ]
         };
 
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
         Assert.Equal (Point.Empty, new Point (menu.Frame.X, menu.Frame.Y));
         Assert.False (menu.UseSubMenusSingleFrame);
         menu.UseSubMenusSingleFrame = true;
         Assert.True (menu.UseSubMenusSingleFrame);
 
-        Application.Top.Draw ();
+        top.Draw ();
 
         var expected = @"
  Numbers
@@ -3140,7 +3168,7 @@ Edit
         Assert.Equal (new Rectangle (1, 0, 8, 1), pos);
 
         Assert.True (menu.NewKeyDownEvent (menu.Key));
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  Numbers  
@@ -3156,7 +3184,7 @@ Edit
 
         Assert.True (Application.Top.Subviews [1].NewKeyDownEvent (Key.CursorDown));
         Assert.True (Application.Top.Subviews [1].NewKeyDownEvent (Key.Enter));
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  Numbers       
@@ -3172,7 +3200,7 @@ Edit
         Assert.Equal (new Rectangle (1, 0, 15, 7), pos);
 
         Assert.True (Application.Top.Subviews [2].NewKeyDownEvent (Key.Enter));
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  Numbers  
@@ -3187,7 +3215,7 @@ Edit
         Assert.Equal (new Rectangle (1, 0, 10, 6), pos);
 
         Assert.True (Application.Top.Subviews [1].NewKeyDownEvent (Key.Esc));
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  Numbers
@@ -3232,15 +3260,16 @@ Edit
             ]
         };
 
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
         Assert.Equal (Point.Empty, new Point (menu.Frame.X, menu.Frame.Y));
         Assert.False (menu.UseSubMenusSingleFrame);
         menu.UseSubMenusSingleFrame = true;
         Assert.True (menu.UseSubMenusSingleFrame);
 
-        Application.Top.Draw ();
+        top.Draw ();
 
         var expected = @"
  Numbers
@@ -3254,7 +3283,7 @@ Edit
                                       new MouseEvent { X = 1, Y = 0, Flags = MouseFlags.Button1Pressed, View = menu }
                                      )
                     );
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  Numbers  
@@ -3273,7 +3302,7 @@ Edit
                                        new MouseEvent { X = 1, Y = 2, Flags = MouseFlags.Button1Clicked, View = Application.Top.Subviews [1] }
                                       )
                      );
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  Numbers       
@@ -3293,7 +3322,7 @@ Edit
                                        new MouseEvent { X = 1, Y = 1, Flags = MouseFlags.Button1Clicked, View = Application.Top.Subviews [2] }
                                       )
                      );
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  Numbers  
@@ -3312,7 +3341,7 @@ Edit
                                        new MouseEvent { X = 70, Y = 2, Flags = MouseFlags.Button1Clicked, View = Application.Top }
                                       )
                      );
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  Numbers
@@ -3357,8 +3386,9 @@ Edit
                                 )
             ]
         };
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
         Assert.Equal (Point.Empty, new Point (menu.Frame.X, menu.Frame.Y));
         Assert.False (menu.UseSubMenusSingleFrame);
@@ -3366,7 +3396,7 @@ Edit
         Assert.True (menu.UseSubMenusSingleFrame);
 
         Assert.True (menu.NewKeyDownEvent (menu.Key));
-        Application.Top.Draw ();
+        top.Draw ();
 
         var expected = @"
  Numbers
@@ -3378,7 +3408,7 @@ Edit
 
         Assert.True (Application.Top.Subviews [1].NewKeyDownEvent (Key.CursorDown));
         Assert.True (Application.Top.Subviews [1].NewKeyDownEvent (Key.Enter));
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  Numbers     
@@ -3427,14 +3457,15 @@ Edit
             ]
         };
 
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
         Assert.Equal (Point.Empty, new Point (menu.Frame.X, menu.Frame.Y));
         Assert.True (menu.UseSubMenusSingleFrame);
         Assert.Equal (LineStyle.None, menu.MenusBorderStyle);
 
-        Application.Top.Draw ();
+        top.Draw ();
 
         var expected = @"
  Numbers
@@ -3448,7 +3479,7 @@ Edit
                                       new MouseEvent { X = 1, Y = 0, Flags = MouseFlags.Button1Pressed, View = menu }
                                      )
                     );
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  Numbers
@@ -3465,7 +3496,7 @@ Edit
                                        new MouseEvent { X = 1, Y = 2, Flags = MouseFlags.Button1Clicked, View = Application.Top.Subviews [1] }
                                       )
                      );
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  Numbers     
@@ -3483,7 +3514,7 @@ Edit
                                        new MouseEvent { X = 1, Y = 1, Flags = MouseFlags.Button1Clicked, View = Application.Top.Subviews [2] }
                                       )
                      );
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  Numbers
@@ -3500,7 +3531,7 @@ Edit
                                        new MouseEvent { X = 70, Y = 2, Flags = MouseFlags.Button1Clicked, View = Application.Top }
                                       )
                      );
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  Numbers
@@ -3521,8 +3552,9 @@ Edit
                 new MenuBarItem ("File", new MenuItem [] { new ("New", "", null) })
             ]
         };
-        Application.Top.Add (menu);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu);
+        Application.Begin (top);
 
         Assert.True (menu.Visible);
         Assert.True (menu.NewKeyDownEvent (menu.Key));
@@ -3674,8 +3706,9 @@ Edit
         var btnClicked = false;
         var btn = new Button { Y = 4, Text = "Test" };
         btn.Accept += (s, e) => btnClicked = true;
-        Application.Top.Add (menu, btn);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (menu, btn);
+        Application.Begin (top);
 
         Application.OnMouseEvent (new (new () { X = 0, Y = 4, Flags = MouseFlags.Button1Clicked }));
         Assert.True (btnClicked);

+ 127 - 25
UnitTests/Views/OverlappedTests.cs

@@ -97,8 +97,8 @@ public class OverlappedTests
 
         Application.Run (overlapped);
 
-        Assert.Null (Application.OverlappedChildren);
-        Assert.Null (Application.OverlappedTop);
+        Assert.Empty (Application.OverlappedChildren);
+        Assert.NotNull (Application.OverlappedTop);
         Assert.NotNull (Application.Top);
     }
 
@@ -194,6 +194,7 @@ public class OverlappedTests
         RunState rs = Application.Begin (top);
 
         Application.End (rs);
+        top.Dispose ();
         Application.Shutdown ();
 
 #if DEBUG_IDISPOSABLE
@@ -210,7 +211,7 @@ public class OverlappedTests
         var overlapped = new Toplevel { IsOverlappedContainer = true };
         RunState rs = Application.Begin (overlapped);
         Application.End (rs);
-
+        overlapped.Dispose ();
         Application.Shutdown ();
     }
 
@@ -334,8 +335,8 @@ public class OverlappedTests
 
         Application.Run (overlapped);
 
-        Assert.Null (Application.OverlappedChildren);
-        Assert.Null (Application.OverlappedTop);
+        Assert.Empty (Application.OverlappedChildren);
+        Assert.NotNull (Application.OverlappedTop);
         Assert.NotNull (Application.Top);
     }
 
@@ -425,8 +426,8 @@ public class OverlappedTests
 
         Application.Run (overlapped);
 
-        Assert.Null (Application.OverlappedChildren);
-        Assert.Null (Application.OverlappedTop);
+        Assert.Empty (Application.OverlappedChildren);
+        Assert.NotNull (Application.OverlappedTop);
         Assert.NotNull (Application.Top);
     }
 
@@ -510,8 +511,8 @@ public class OverlappedTests
 
         Application.Run (overlapped);
 
-        Assert.Null (Application.OverlappedChildren);
-        Assert.Null (Application.OverlappedTop);
+        Assert.Empty (Application.OverlappedChildren);
+        Assert.NotNull (Application.OverlappedTop);
         Assert.NotNull (Application.Top);
     }
 
@@ -605,8 +606,8 @@ public class OverlappedTests
 
         Application.Run (overlapped);
 
-        Assert.Null (Application.OverlappedChildren);
-        Assert.Null (Application.OverlappedTop);
+        Assert.Empty (Application.OverlappedChildren);
+        Assert.NotNull (Application.OverlappedTop);
         Assert.NotNull (Application.Top);
     }
 
@@ -699,8 +700,8 @@ public class OverlappedTests
 
         Application.Run (overlapped);
 
-        Assert.Null (Application.OverlappedChildren);
-        Assert.Null (Application.OverlappedTop);
+        Assert.Empty (Application.OverlappedChildren);
+        Assert.NotNull (Application.OverlappedTop);
         Assert.NotNull (Application.Top);
     }
 
@@ -778,8 +779,8 @@ public class OverlappedTests
 
         Application.Run (overlapped);
 
-        Assert.Null (Application.OverlappedChildren);
-        Assert.Null (Application.OverlappedTop);
+        Assert.Empty (Application.OverlappedChildren);
+        Assert.NotNull (Application.OverlappedTop);
         Assert.NotNull (Application.Top);
     }
 
@@ -856,8 +857,8 @@ public class OverlappedTests
 
         Application.Run (overlapped);
 
-        Assert.Null (Application.OverlappedChildren);
-        Assert.Null (Application.OverlappedTop);
+        Assert.Empty (Application.OverlappedChildren);
+        Assert.NotNull (Application.OverlappedTop);
         Assert.NotNull (Application.Top);
     }
 
@@ -869,12 +870,44 @@ public class OverlappedTests
         var win1 = new Window { Width = 5, Height = 5, Visible = false };
         var win2 = new Window { X = 1, Y = 1, Width = 5, Height = 5 };
         ((FakeDriver)Application.Driver).SetBufferSize (10, 10);
-        RunState rs = Application.Begin (overlapped);
-        Application.Begin (win1);
-        Application.Begin (win2);
-        Assert.Equal (win2, Application.Current);
+        RunState rsOverlapped = Application.Begin (overlapped);
+
+        // Need to fool MainLoop into thinking it's running
+        Application.MainLoop.Running = true;
+
+        // RunIteration must be call on each iteration because
+        // it's using the Begin and not the Run method
         var firstIteration = false;
-        Application.RunIteration (ref rs, ref firstIteration);
+        Application.RunIteration (ref rsOverlapped, ref firstIteration);
+
+        Assert.Equal (overlapped, rsOverlapped.Toplevel);
+        Assert.Equal (Application.Top, rsOverlapped.Toplevel);
+        Assert.Equal (Application.OverlappedTop, rsOverlapped.Toplevel);
+        Assert.Equal (Application.Current, rsOverlapped.Toplevel);
+        Assert.Equal (overlapped, Application.Current);
+
+        RunState rsWin1 = Application.Begin (win1);
+        Application.RunIteration (ref rsOverlapped, ref firstIteration);
+
+        Assert.Equal (overlapped, rsOverlapped.Toplevel);
+        Assert.Equal (Application.Top, rsOverlapped.Toplevel);
+        Assert.Equal (Application.OverlappedTop, rsOverlapped.Toplevel);
+        // The win1 Visible is false and cannot be set as the Current
+        Assert.Equal (Application.Current, rsOverlapped.Toplevel);
+        Assert.Equal (overlapped, Application.Current);
+        Assert.Equal (win1, rsWin1.Toplevel);
+
+        RunState rsWin2 = Application.Begin (win2);
+        Application.RunIteration (ref rsOverlapped, ref firstIteration);
+
+        // Here the Current and the rsOverlapped.Toplevel is now the win2
+        // and not the original overlapped
+        Assert.Equal (win2, rsOverlapped.Toplevel);
+        Assert.Equal (Application.Top, overlapped);
+        Assert.Equal (Application.OverlappedTop, overlapped);
+        Assert.Equal (Application.Current, rsWin2.Toplevel);
+        Assert.Equal (win2, Application.Current);
+        Assert.Equal (win1, rsWin1.Toplevel);
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
@@ -918,6 +951,15 @@ public class OverlappedTests
                                  );
         Assert.Equal (win2.Border, Application.MouseGrabView);
 
+        Application.RunIteration (ref rsOverlapped, ref firstIteration);
+
+        Assert.Equal (win2, rsOverlapped.Toplevel);
+        Assert.Equal (Application.Top, overlapped);
+        Assert.Equal (Application.OverlappedTop, overlapped);
+        Assert.Equal (Application.Current, rsWin2.Toplevel);
+        Assert.Equal (win2, Application.Current);
+        Assert.Equal (win1, rsWin1.Toplevel);
+
         Application.OnMouseEvent (
                                   new MouseEventEventArgs (
                                                            new MouseEvent
@@ -930,9 +972,14 @@ public class OverlappedTests
                                                           )
                                  );
 
-        // Need to fool MainLoop into thinking it's running
-        Application.MainLoop.Running = true;
-        Application.RunIteration (ref rs, ref firstIteration);
+        Application.RunIteration (ref rsOverlapped, ref firstIteration);
+
+        Assert.Equal (win2, rsOverlapped.Toplevel);
+        Assert.Equal (Application.Top, overlapped);
+        Assert.Equal (Application.OverlappedTop, overlapped);
+        Assert.Equal (Application.Current, rsWin2.Toplevel);
+        Assert.Equal (win2, Application.Current);
+        Assert.Equal (win1, rsWin1.Toplevel);
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
@@ -960,6 +1007,61 @@ public class OverlappedTests
                                                attributes
                                               );
 
+        // This will end the win2 and not the overlapped
+        Application.End (rsOverlapped);
+        // rsOverlapped has been disposed and Toplevel property is null
+        // So we must use another valid RunState to iterate
+        Application.RunIteration (ref rsWin1, ref firstIteration);
+#if DEBUG_IDISPOSABLE
+        Assert.True (rsOverlapped.WasDisposed);
+#endif
+        Assert.Null (rsOverlapped.Toplevel);
+        Assert.Equal (Application.Top, overlapped);
+        Assert.Equal (Application.OverlappedTop, overlapped);
+        Assert.Equal (Application.Current, rsWin1.Toplevel);
+        Assert.Equal (win1, Application.Current);
+        Assert.Equal (win1, rsWin1.Toplevel);
+
+        Application.End (rsWin1);
+        // rsWin1 has been disposed and Toplevel property is null
+        // So we must use another valid RunState to iterate
+        Application.RunIteration (ref rsWin2, ref firstIteration);
+#if DEBUG_IDISPOSABLE
+        Assert.True (rsOverlapped.WasDisposed);
+        Assert.True (rsWin1.WasDisposed);
+#endif
+        Assert.Null (rsOverlapped.Toplevel);
+        Assert.Equal (Application.Top, overlapped);
+        Assert.Equal (Application.OverlappedTop, overlapped);
+        Assert.Equal (Application.Current, overlapped);
+        Assert.Null (rsWin1.Toplevel);
+        // See here that the only Toplevel that needs to End is the overlapped
+        // which the rsWin2 now has the Toplevel set to the overlapped
+        Assert.Equal (overlapped, rsWin2.Toplevel);
+
+        Application.End (rsWin2);
+        // There is no more RunState to iteration
+#if DEBUG_IDISPOSABLE
+        Assert.True (rsOverlapped.WasDisposed);
+        Assert.True (rsWin1.WasDisposed);
+        Assert.True (rsWin2.WasDisposed);
+#endif
+        Assert.Null (rsOverlapped.Toplevel);
+        Assert.Equal (Application.Top, overlapped);
+        Assert.Equal (Application.OverlappedTop, overlapped);
+        Assert.Null (Application.Current);
+        Assert.Null (rsWin1.Toplevel);
+        Assert.Null (rsWin2.Toplevel);
+
+#if DEBUG_IDISPOSABLE
+        Assert.False (win2.WasDisposed);
+        Assert.False (win1.WasDisposed);
+        Assert.False (overlapped.WasDisposed);
+#endif
+        // Now dispose all them
+        win2.Dispose ();
+        win1.Dispose ();
+        overlapped.Dispose ();
         Application.Shutdown ();
     }
 

+ 3 - 2
UnitTests/Views/RadioGroupTests.cs

@@ -171,9 +171,10 @@ public class RadioGroupTests
         var rg = new RadioGroup { RadioLabels = new [] { "Test", "New Test 你" } };
         var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
         win.Add (rg);
-        Application.Top.Add (win);
+        var top = new Toplevel ();
+        top.Add (win);
 
-        Application.Begin (Application.Top);
+        Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
 
         Assert.Equal (Orientation.Vertical, rg.Orientation);

+ 8 - 5
UnitTests/Views/RuneCellTests.cs

@@ -72,8 +72,9 @@ public class RuneCellTests
 
         TextView tv = CreateTextView ();
         tv.Load (runeCells);
-        Application.Top.Add (tv);
-        RunState rs = Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tv);
+        RunState rs = Application.Begin (top);
         Assert.True (tv.InheritsPreviousColorScheme);
 
         var expectedText = @"
@@ -197,7 +198,8 @@ ror       ";
             new RuneCell { Rune = new Rune ('t') }
         };
         TextView tv = CreateTextView ();
-        Application.Top.Add (tv);
+        var top = new Toplevel ();
+        top.Add (tv);
         tv.Load (cells);
 
         for (var i = 0; i < tv.Lines; i++)
@@ -240,8 +242,9 @@ ror       ";
 
         tv.Text = $"{TextModel.ToString (text [0])}\n{TextModel.ToString (text [1])}\n";
         Assert.False (tv.WordWrap);
-        Application.Top.Add (tv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tv);
+        Application.Begin (top);
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"

+ 115 - 99
UnitTests/Views/ScrollBarViewTests.cs

@@ -142,7 +142,8 @@ public class ScrollBarViewTests
         var height = 40;
 
         var super = new Window { Id = "super", Width = Dim.Fill (), Height = Dim.Fill () };
-        Application.Top.Add (super);
+        var top = new Toplevel ();
+        top.Add (super);
 
         var horiz = new ScrollBarView
         {
@@ -168,7 +169,7 @@ public class ScrollBarViewTests
         };
         super.Add (vert);
 
-        Application.Begin (Application.Top);
+        Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (width, height);
 
         var expected = @"
@@ -287,10 +288,11 @@ public class ScrollBarViewTests
         var text =
             "This is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test";
         var label = new Label { Text = text };
-        Application.Top.Add (label);
+        var top = new Toplevel ();
+        top.Add (label);
 
         var sbv = new ScrollBarView (label, true, false) { Size = 100, ClearOnVisibleFalse = false };
-        Application.Begin (Application.Top);
+        Application.Begin (top);
 
         Assert.True (sbv.Visible);
 
@@ -308,7 +310,7 @@ This is a tes▼
 
         sbv.Visible = false;
         Assert.False (sbv.Visible);
-        Application.Top.Draw ();
+        top.Draw ();
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
@@ -324,7 +326,7 @@ This is a test
 
         sbv.Visible = true;
         Assert.True (sbv.Visible);
-        Application.Top.Draw ();
+        top.Draw ();
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
@@ -359,85 +361,91 @@ This is a tes
     public void
         Constructor_ShowBothScrollIndicator_False_And_IsVertical_False_Refresh_Does_Not_Throws_An_Object_Null_Exception ()
     {
-        // BUGBUG: v2 - Tig broke these tests; @bdisp help?
-        //var exception = Record.Exception (() => {
-        Application.Init (new FakeDriver ());
-
-        Toplevel top = Application.Top;
-
-        var win = new Window { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
-
-        List<string> source = new ();
-
-        for (var i = 0; i < 50; i++)
-        {
-            var text = $"item {i} - ";
-
-            for (var j = 0; j < 160; j++)
-            {
-                var col = j.ToString ();
-                text += col.Length == 1 ? col [0] : col [1];
-            }
-
-            source.Add (text);
-        }
-
-        var listView = new ListView
-        {
-            X = 0,
-            Y = 0,
-            Width = Dim.Fill (),
-            Height = Dim.Fill (),
-            Source = new ListWrapper (source)
-        };
-        win.Add (listView);
-
-        var newScrollBarView = new ScrollBarView (listView, false, false) { KeepContentAlwaysInViewport = true };
-        win.Add (newScrollBarView);
-
-        newScrollBarView.ChangedPosition += (s, e) =>
-                                            {
-                                                listView.LeftItem = newScrollBarView.Position;
-
-                                                if (listView.LeftItem != newScrollBarView.Position)
-                                                {
-                                                    newScrollBarView.Position = listView.LeftItem;
-                                                }
-
-                                                Assert.Equal (newScrollBarView.Position, listView.LeftItem);
-                                                listView.SetNeedsDisplay ();
-                                            };
-
-        listView.DrawContent += (s, e) =>
-                                {
-                                    newScrollBarView.Size = listView.MaxLength;
-                                    Assert.Equal (newScrollBarView.Size, listView.MaxLength);
-                                    newScrollBarView.Position = listView.LeftItem;
-                                    Assert.Equal (newScrollBarView.Position, listView.LeftItem);
-                                    newScrollBarView.Refresh ();
-                                };
+        var exception = Record.Exception (
+                                          () =>
+                                          {
+                                              Application.Init (new FakeDriver ());
+
+                                              Toplevel top = new ();
+
+                                              var win = new Window { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
+
+                                              List<string> source = new ();
+
+                                              for (var i = 0; i < 50; i++)
+                                              {
+                                                  var text = $"item {i} - ";
+
+                                                  for (var j = 0; j < 160; j++)
+                                                  {
+                                                      var col = j.ToString ();
+                                                      text += col.Length == 1 ? col [0] : col [1];
+                                                  }
+
+                                                  source.Add (text);
+                                              }
+
+                                              var listView = new ListView
+                                              {
+                                                  X = 0,
+                                                  Y = 0,
+                                                  Width = Dim.Fill (),
+                                                  Height = Dim.Fill (),
+                                                  Source = new ListWrapper (source)
+                                              };
+                                              win.Add (listView);
+
+                                              var newScrollBarView = new ScrollBarView (listView, false, false) { KeepContentAlwaysInViewport = true };
+                                              win.Add (newScrollBarView);
+
+                                              newScrollBarView.ChangedPosition += (s, e) =>
+                                                                                  {
+                                                                                      listView.LeftItem = newScrollBarView.Position;
+
+                                                                                      if (listView.LeftItem != newScrollBarView.Position)
+                                                                                      {
+                                                                                          newScrollBarView.Position = listView.LeftItem;
+                                                                                      }
+
+                                                                                      Assert.Equal (newScrollBarView.Position, listView.LeftItem);
+                                                                                      listView.SetNeedsDisplay ();
+                                                                                  };
+
+                                              listView.DrawContent += (s, e) =>
+                                                                      {
+                                                                          newScrollBarView.Size = listView.MaxLength;
+                                                                          Assert.Equal (newScrollBarView.Size, listView.MaxLength);
+                                                                          newScrollBarView.Position = listView.LeftItem;
+                                                                          Assert.Equal (newScrollBarView.Position, listView.LeftItem);
+                                                                          newScrollBarView.Refresh ();
+                                                                      };
+
+                                              top.Ready += (s, e) =>
+                                                           {
+                                                               newScrollBarView.Position = 100;
 
-        top.Ready += (s, e) =>
-                     {
-                         newScrollBarView.Position = 100;
+                                                               Assert.Equal (
+                                                                             newScrollBarView.Position,
+                                                                             newScrollBarView.Size
+                                                                             - listView.LeftItem
+                                                                             + (listView.LeftItem - listView.Bounds.Width));
+                                                               Assert.Equal (newScrollBarView.Position, listView.LeftItem);
 
-                         //Assert.Equal (newScrollBarView.Position, newScrollBarView.Size - listView.LeftItem + (listView.LeftItem - listView.Bounds.Width));
-                         Assert.Equal (newScrollBarView.Position, listView.LeftItem);
+                                                               Assert.Equal (92, newScrollBarView.Position);
+                                                               Assert.Equal (92, listView.LeftItem);
+                                                               Application.RequestStop ();
+                                                           };
 
-                         //Assert.Equal (92, newScrollBarView.Position);
-                         //Assert.Equal (92, listView.LeftItem);
-                         Application.RequestStop ();
-                     };
+                                              top.Add (win);
 
-        top.Add (win);
+                                              Application.Run (top);
 
-        Application.Run ();
+                                              top.Dispose ();
+                                              Application.Shutdown ();
 
-        Application.Shutdown ();
+                                          });
 
-        //});
-
-        //Assert.Null (exception);
+        Assert.Null (exception);
     }
 
     [Fact]
@@ -448,7 +456,7 @@ This is a tes
                                                 () =>
                                                 {
                                                     Application.Init (new FakeDriver ());
-                                                    Toplevel top = Application.Top;
+                                                    Toplevel top = new ();
                                                     var win = new Window { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
                                                     List<string> source = new ();
 
@@ -507,7 +515,8 @@ This is a tes
                                                                      Application.RequestStop ();
                                                                  };
                                                     top.Add (win);
-                                                    Application.Run ();
+                                                    Application.Run (top);
+                                                    top.Dispose ();
                                                     Application.Shutdown ();
                                                 }
                                                );
@@ -522,11 +531,12 @@ This is a tes
         var text =
             "This is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test";
         var label = new Label { Text = text };
-        Application.Top.Add (label);
+        var top = new Toplevel ();
+       top.Add (label);
 
         var sbv = new ScrollBarView (label, true) { Size = 100 };
         sbv.OtherScrollBarView.Size = 100;
-        Application.Begin (Application.Top);
+        Application.Begin (top);
 
         Assert.Equal (100, sbv.Size);
         Assert.Equal (100, sbv.OtherScrollBarView.Size);
@@ -560,7 +570,7 @@ This is a tes▼
         Assert.False (sbv.OtherScrollBarView.ShowScrollIndicator);
         Assert.False (sbv.Visible);
         Assert.False (sbv.OtherScrollBarView.Visible);
-        Application.Top.Draw ();
+        top.Draw ();
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
@@ -582,7 +592,7 @@ This is a test
         Assert.True (sbv.OtherScrollBarView.ShowScrollIndicator);
         Assert.True (sbv.Visible);
         Assert.True (sbv.OtherScrollBarView.Visible);
-        Application.Top.Draw ();
+        top.Draw ();
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
@@ -604,10 +614,11 @@ This is a tes▼
         var text =
             "This is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test";
         var label = new Label { Text = text };
-        Application.Top.Add (label);
+        var top = new Toplevel ();
+        top.Add (label);
 
         var sbv = new ScrollBarView (label, true, false) { Size = 100 };
-        Application.Begin (Application.Top);
+        Application.Begin (top);
 
         Assert.Equal (100, sbv.Size);
         Assert.Null (sbv.OtherScrollBarView);
@@ -630,7 +641,7 @@ This is a tes▼
         Assert.Equal (0, sbv.Size);
         Assert.False (sbv.ShowScrollIndicator);
         Assert.False (sbv.Visible);
-        Application.Top.Draw ();
+        top.Draw ();
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
@@ -670,11 +681,12 @@ This is a test
         var height = 3;
 
         var super = new Window { Id = "super", Width = Dim.Fill (), Height = Dim.Fill () };
-        Application.Top.Add (super);
+        var top = new Toplevel ();
+        top.Add (super);
 
         var sbv = new ScrollBarView { Id = "sbv", Size = width * 2, ShowScrollIndicator = true };
         super.Add (sbv);
-        Application.Begin (Application.Top);
+        Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (width, height);
 
         var expected = @"
@@ -722,7 +734,7 @@ This is a test
 
         _scrollBar = new ScrollBarView (_hostView, true);
 
-        Application.Begin (Application.Top);
+        Application.Begin (_hostView.SuperView as Toplevel);
 
         Assert.True (_scrollBar.IsVertical);
         Assert.False (_scrollBar.OtherScrollBarView.IsVertical);
@@ -820,9 +832,10 @@ This is a test
                                        scrollBar.LayoutSubviews ();
                                        scrollBar.Refresh ();
                                    };
-        Application.Top.Add (win);
+        var top = new Toplevel ();
+        top.Add (win);
 
-        Application.Begin (Application.Top);
+        Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (45, 20);
 
         Assert.True (scrollBar.AutoHideScrollBars);
@@ -959,7 +972,7 @@ This is a test
     [AutoInitShutdown]
     public void Internal_Tests ()
     {
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         Assert.Equal (new Rectangle (0, 0, 80, 25), top.Bounds);
         var view = new View { Width = Dim.Fill (), Height = Dim.Fill () };
         top.Add (view);
@@ -1116,10 +1129,11 @@ This is a test
         var label = new Label { AutoSize = false, Width = 14, Height = 5, Text = text };
         var btn = new Button { X = 14, Text = "Click Me!" };
         btn.Accept += (s, e) => clicked = true;
-        Application.Top.Add (label, btn);
+        var top = new Toplevel ();
+        top.Add (label, btn);
 
         var sbv = new ScrollBarView (label, true, false) { Size = 5 };
-        Application.Begin (Application.Top);
+        Application.Begin (top);
 
         Assert.Equal (5, sbv.Size);
         Assert.Null (sbv.OtherScrollBarView);
@@ -1155,7 +1169,7 @@ This is a test             ",
         Assert.Equal (5, sbv.Size);
         Assert.False (sbv.ShowScrollIndicator);
         Assert.True (sbv.Visible);
-        Application.Top.Draw ();
+        top.Draw ();
         Assert.False (sbv.Visible);
 
         TestHelpers.AssertDriverContentsWithFrameAre (
@@ -1193,7 +1207,8 @@ This is a test             ",
         var height = 40;
 
         var super = new Window { Id = "super", Width = Dim.Fill (), Height = Dim.Fill () };
-        Application.Top.Add (super);
+        var top = new Toplevel ();
+        top.Add (super);
 
         var sbv = new ScrollBarView
         {
@@ -1207,7 +1222,7 @@ This is a test             ",
         };
 
         super.Add (sbv);
-        Application.Begin (Application.Top);
+        Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (width, height);
 
         var expected = @"
@@ -1347,7 +1362,8 @@ This is a test             ",
                 Cols = 100
             };
 
-            Application.Top.Add (_hostView);
+            var top = new Toplevel ();
+            top.Add (_hostView);
         }
     }
 }

+ 36 - 28
UnitTests/Views/ScrollViewTests.cs

@@ -31,8 +31,9 @@ public class ScrollViewTests
         sv.ShowHorizontalScrollIndicator = true;
         sv.ShowVerticalScrollIndicator = true;
 
-        Application.Top.Add (sv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (sv);
+        Application.Begin (top);
 
         Assert.Equal (new Rectangle (0, 0, 10, 10), sv.Bounds);
 
@@ -138,8 +139,9 @@ public class ScrollViewTests
     {
         var sv = new ScrollView { Width = 10, Height = 10 };
 
-        Application.Top.Add (sv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (sv);
+        Application.Begin (top);
 
         Assert.True (sv.AutoHideScrollBars);
         Assert.False (sv.ShowHorizontalScrollIndicator);
@@ -186,8 +188,9 @@ public class ScrollViewTests
             KeepContentAlwaysInViewport = false
         };
         var bottomLabel = new Label { X = 15, Y = 15, Text = "At 15,15" };
-        Application.Top.Add (topLabel, sv, bottomLabel);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (topLabel, sv, bottomLabel);
+        Application.Begin (top);
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
@@ -445,8 +448,9 @@ public class ScrollViewTests
             Width = 10, Height = 10, ContentSize = new (50, 50), ContentOffset = new (25, 25)
         };
 
-        Application.Top.Add (sv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (sv);
+        Application.Begin (top);
 
         Assert.Equal(new(-25,-25),sv.ContentOffset);
         Assert.Equal(new(50,50),sv.ContentSize);
@@ -477,8 +481,9 @@ public class ScrollViewTests
     {
         var sv = new ScrollView { Width = 10, Height = 10, ContentSize = new (50, 50) };
 
-        Application.Top.Add (sv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (sv);
+        Application.Begin (top);
 
         Assert.Equal (50, sv.ContentSize.Width);
         Assert.Equal (50, sv.ContentSize.Height);
@@ -543,8 +548,9 @@ public class ScrollViewTests
         scrollView.Add (view);
         var win = new Window { X = 1, Y = 1, Width = 20, Height = 14 };
         win.Add (scrollView);
-        Application.Top.Add (win);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (win);
+        Application.Begin (top);
 
         var expected = @"
  ┌──────────────────┐
@@ -568,7 +574,7 @@ public class ScrollViewTests
         Assert.Equal (new Rectangle (1, 1, 21, 14), pos);
 
         Assert.True (scrollView.OnKeyDown (Key.CursorRight));
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  ┌──────────────────┐
@@ -592,7 +598,7 @@ public class ScrollViewTests
         Assert.Equal (new Rectangle (1, 1, 21, 14), pos);
 
         Assert.True (scrollView.OnKeyDown (Key.CursorRight));
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  ┌──────────────────┐
@@ -616,7 +622,7 @@ public class ScrollViewTests
         Assert.Equal (new Rectangle (1, 1, 21, 14), pos);
 
         Assert.True (scrollView.OnKeyDown (Key.CursorRight));
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  ┌──────────────────┐
@@ -640,7 +646,7 @@ public class ScrollViewTests
         Assert.Equal (new Rectangle (1, 1, 21, 14), pos);
 
         Assert.True (scrollView.OnKeyDown (Key.CursorRight));
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  ┌──────────────────┐
@@ -664,7 +670,7 @@ public class ScrollViewTests
         Assert.Equal (new Rectangle (1, 1, 21, 14), pos);
 
         Assert.True (scrollView.OnKeyDown (Key.CursorRight));
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  ┌──────────────────┐
@@ -688,7 +694,7 @@ public class ScrollViewTests
         Assert.Equal (new Rectangle (1, 1, 21, 14), pos);
 
         Assert.True (scrollView.OnKeyDown (Key.CursorRight));
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  ┌──────────────────┐
@@ -712,7 +718,7 @@ public class ScrollViewTests
         Assert.Equal (new Rectangle (1, 1, 21, 14), pos);
 
         Assert.True (scrollView.OnKeyDown (Key.CursorRight));
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  ┌──────────────────┐
@@ -735,7 +741,7 @@ public class ScrollViewTests
         Assert.Equal (new Rectangle (1, 1, 21, 14), pos);
 
         Assert.True (scrollView.OnKeyDown (Key.End.WithCtrl));
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  ┌──────────────────┐
@@ -759,7 +765,7 @@ public class ScrollViewTests
 
         Assert.True (scrollView.OnKeyDown (Key.Home.WithCtrl));
         Assert.True (scrollView.OnKeyDown (Key.CursorDown));
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  ┌──────────────────┐
@@ -782,7 +788,7 @@ public class ScrollViewTests
         Assert.Equal (new Rectangle (1, 1, 21, 14), pos);
 
         Assert.True (scrollView.OnKeyDown (Key.CursorDown));
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  ┌──────────────────┐
@@ -805,7 +811,7 @@ public class ScrollViewTests
         Assert.Equal (new Rectangle (1, 1, 21, 14), pos);
 
         Assert.True (scrollView.OnKeyDown (Key.CursorDown));
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  ┌──────────────────┐
@@ -828,7 +834,7 @@ public class ScrollViewTests
         Assert.Equal (new Rectangle (1, 1, 21, 14), pos);
 
         Assert.True (scrollView.OnKeyDown (Key.End));
-        Application.Top.Draw ();
+        top.Draw ();
 
         expected = @"
  ┌──────────────────┐
@@ -870,8 +876,9 @@ public class ScrollViewTests
             sv.Add (new CustomButton ("█", $"Button {i}", 20, 3) { Y = i * 3 });
         }
 
-        Application.Top.Add (sv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (sv);
+        Application.Begin (top);
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
@@ -1052,8 +1059,9 @@ public class ScrollViewTests
                 new View { Y = 51, Width = Dim.Fill (), Height = Dim.Fill (), Id = "View2" }
                );
 
-        Application.Top.Add (sv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (sv);
+        Application.Begin (top);
 
         Assert.Equal (4, sv.Subviews.Count);
         Assert.Equal (2, sv.Subviews [0].Subviews.Count);

+ 3 - 2
UnitTests/Views/SpinnerViewTests.cs

@@ -100,8 +100,9 @@ public class SpinnerViewTests
     {
         var view = new SpinnerView ();
 
-        Application.Top.Add (view);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (view);
+        Application.Begin (top);
 
         Assert.Equal (1, view.Width);
         Assert.Equal (1, view.Height);

+ 9 - 7
UnitTests/Views/StatusBarTests.cs

@@ -62,7 +62,7 @@ public class StatusBarTests
                                                )
                                        }
                                       );
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (statusBar);
 
         bool CanExecuteNew () { return win == null; }
@@ -97,7 +97,8 @@ public class StatusBarTests
                                     new (Application.QuitKey, $"{Application.QuitKey} to Quit!", null)
                                 }
                                );
-        Application.Top.Add (sb);
+        var top = new Toplevel ();
+        top.Add (sb);
 
         sb.OnDrawContent (sb.Bounds);
 
@@ -120,7 +121,8 @@ public class StatusBarTests
                                     new (KeyCode.CtrlMask | KeyCode.Q, "~CTRL-Q~ Quit", null)
                                 }
                                );
-        Application.Top.Add (sb);
+        var top = new Toplevel ();
+        top.Add (sb);
         sb.OnDrawContent (sb.Bounds);
 
         var expected = @$"
@@ -148,8 +150,6 @@ CTRL-O Open {
                                         )
                                 }
                                );
-        Application.Top.Add (sb);
-
         var iteration = 0;
 
         Application.Iteration += (s, a) =>
@@ -217,10 +217,12 @@ CTRL-O Open {
                                      Application.RequestStop ();
                                  };
 
-        Application.Top.Add (sb);
+        var top = new Toplevel ();
+       top.Add (sb);
 
-        Application.Run ();
+        Application.Run (top);
 
+        top.Dispose ();
         Application.Shutdown ();
     }
 

+ 10 - 7
UnitTests/Views/TabViewTests.cs

@@ -137,8 +137,9 @@ public class TabViewTests
 
         tv.TabClicked += (s, e) => { clicked = e.Tab; };
 
-        Application.Top.Add (tv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tv);
+        Application.Begin (top);
 
         MouseEventEventArgs args;
 
@@ -239,8 +240,9 @@ public class TabViewTests
                                      newChanged = e.NewTab;
                                  };
 
-        Application.Top.Add (tv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tv);
+        Application.Begin (top);
 
         // Click the right arrow
         var args = new MouseEventEventArgs (
@@ -332,8 +334,9 @@ public class TabViewTests
                                      newChanged = e.NewTab;
                                  };
 
-        Application.Top.Add (tv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tv);
+        Application.Begin (top);
 
         // Click the right arrow
         var args = new MouseEventEventArgs (
@@ -402,7 +405,7 @@ public class TabViewTests
             Text = "Ok"
         };
 
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (tv, btn);
         Application.Begin (top);
 

+ 18 - 12
UnitTests/Views/TableViewTests.cs

@@ -72,8 +72,9 @@ public class TableViewTests
         tv.Table = new DataTableSource (dt);
         tv.NullSymbol = string.Empty;
 
-        Application.Top.Add (tv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tv);
+        Application.Begin (top);
 
         tv.Draw ();
 
@@ -417,8 +418,9 @@ public class TableViewTests
     {
         var tableView = new TableView ();
 
-        Application.Top.Add (tableView);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tableView);
+        Application.Begin (top);
 
         tableView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
 
@@ -592,6 +594,7 @@ public class TableViewTests
 ";
         TestHelpers.AssertDriverContentsAre (expected, output);
 
+        top.Dispose ();
         Application.Shutdown ();
     }
 
@@ -610,10 +613,11 @@ public class TableViewTests
         tableView.Style.AlwaysShowHeaders = false;
 
         // ensure that TableView has the input focus
-        Application.Top.Add (tableView);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tableView);
+        Application.Begin (top);
 
-        Application.Top.FocusFirst ();
+        top.FocusFirst ();
         Assert.True (tableView.HasFocus);
 
         Assert.Equal (0, tableView.RowOffset);
@@ -1004,8 +1008,9 @@ public class TableViewTests
         var tv = new TableView (BuildTable (1, 1));
         tv.CellActivated += (s, c) => activatedValue = c.Table [c.Row, c.Col].ToString ();
 
-        Application.Top.Add (tv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tv);
+        Application.Begin (top);
 
         // pressing enter should activate the first cell (selected cell)
         tv.NewKeyDownEvent (Key.Enter);
@@ -1625,10 +1630,11 @@ public class TableViewTests
         Assert.Equal (0, tv.SelectedRow);
 
         // ensure that TableView has the input focus
-        Application.Top.Add (tv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tv);
+        Application.Begin (top);
 
-        Application.Top.FocusFirst ();
+        top.FocusFirst ();
         Assert.True (tv.HasFocus);
 
         // already on fish

+ 19 - 14
UnitTests/Views/TextFieldTests.cs

@@ -18,7 +18,7 @@ public class TextFieldTests
     public void Accented_Letter_With_Three_Combining_Unicode_Chars ()
     {
         var tf = new TextField { Width = 3, Text = "ắ" };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (tf);
         Application.Begin (top);
 
@@ -61,8 +61,9 @@ public class TextFieldTests
     public void Adjust_First ()
     {
         var tf = new TextField { Width = Dim.Fill (), Text = "This is a test." };
-        Application.Top.Add (tf);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tf);
+        Application.Begin (top);
 
         Assert.Equal ("This is a test. ", GetContents ());
 
@@ -104,7 +105,7 @@ public class TextFieldTests
     [TextFieldTestsAutoInitShutdown]
     public void CanFocus_False_Wont_Focus_With_Mouse ()
     {
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         var tf = new TextField { Width = Dim.Fill (), CanFocus = false, ReadOnly = true, Text = "some text" };
 
         var fv = new FrameView
@@ -431,8 +432,9 @@ public class TextFieldTests
         tf.TextChanging += (s, e) => newText = e.NewValue;
         tf.TextChanged += (s, e) => oldText = e.OldValue;
 
-        Application.Top.Add (tf);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tf);
+        Application.Begin (top);
 
         Assert.Equal ("-1", tf.Text);
 
@@ -903,8 +905,9 @@ public class TextFieldTests
         Assert.Equal (0, tf.ScrollOffset);
         Assert.Equal (16, tf.CursorPosition);
 
-        Application.Top.Add (tf);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tf);
+        Application.Begin (top);
 
         Assert.Equal (0, tf.ScrollOffset);
         Assert.Equal (16, tf.CursorPosition);
@@ -917,8 +920,9 @@ public class TextFieldTests
         // Proves #3022 is fixed (TextField selected text does not show in v2)
 
         _textField.CursorPosition = 0;
-        Application.Top.Add (_textField);
-        RunState rs = Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (_textField);
+        RunState rs = Application.Begin (top);
 
         Attribute [] attributes =
         {
@@ -1029,8 +1033,9 @@ public class TextFieldTests
         var clickCounter = 0;
         tf.MouseClick += (s, m) => { clickCounter++; };
 
-        Application.Top.Add (tf);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tf);
+        Application.Begin (top);
 
         var mouseEvent = new MouseEvent { Flags = MouseFlags.Button1Clicked, View = tf };
 
@@ -1822,7 +1827,7 @@ public class TextFieldTests
     public void Words_With_Accents_Incorrect_Order_Will_Result_With_Wrong_Accent_Place ()
     {
         var tf = new TextField { Width = 30, Text = "Les Misérables" };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (tf);
         Application.Begin (top);
 
@@ -1857,7 +1862,7 @@ Les Miśerables",
         var tf = new TextField { Width = 10 };
         var tf2 = new TextField { Y = 1, Width = 10 };
 
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (tf);
         top.Add (tf2);
 

+ 88 - 66
UnitTests/Views/TextViewTests.cs

@@ -59,7 +59,8 @@ public class TextViewTests
     [TextViewTestsAutoInitShutdown]
     public void BackTab_Test_Follow_By_Tab ()
     {
-        Application.Top.Add (_textView);
+        var top = new Toplevel ();
+        top.Add (_textView);
 
         Application.Iteration += (s, a) =>
                                  {
@@ -103,7 +104,7 @@ public class TextViewTests
                                      Application.RequestStop ();
                                  };
 
-        Application.Run ();
+        Application.Run (top);
     }
 
     [Fact]
@@ -142,7 +143,7 @@ public class TextViewTests
     [TextViewTestsAutoInitShutdown]
     public void CanFocus_False_Wont_Focus_With_Mouse ()
     {
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         var tv = new TextView { Width = Dim.Fill (), CanFocus = false, ReadOnly = true, Text = "some text" };
 
         var fv = new FrameView
@@ -281,8 +282,9 @@ public class TextViewTests
                                   Assert.Equal (expectedCol, e.Col);
                               };
 
-        Application.Top.Add (tv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tv);
+        Application.Begin (top);
         Assert.Equal (1, eventcount);
     }
 
@@ -368,8 +370,9 @@ public class TextViewTests
 
         Assert.Equal ("abc", tv.Text);
 
-        Application.Top.Add (tv);
-        RunState rs = Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tv);
+        RunState rs = Application.Begin (top);
         Assert.Equal (1, eventcount); // for Initialize
 
         expectedCol = 0;
@@ -396,8 +399,9 @@ public class TextViewTests
                                   Assert.Equal (expectedCol, e.Col);
                               };
 
-        Application.Top.Add (tv);
-        RunState rs = Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tv);
+        RunState rs = Application.Begin (top);
         Assert.Equal (1, eventcount); // for Initialize
 
         expectedCol = 0;
@@ -771,8 +775,9 @@ public class TextViewTests
         const string text = "This is the first line.\nThis is the second line.\n";
         var tv = new TextView { Width = Dim.Fill (), Height = Dim.Fill (), Text = text };
         string envText = tv.Text;
-        Application.Top.Add (tv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tv);
+        Application.Begin (top);
 
         Assert.False (tv.WordWrap);
         Assert.Equal (Point.Empty, tv.CursorPosition);
@@ -841,8 +846,9 @@ This is the second line.
         const string text = "This is the first line.\nThis is the second line.\n";
         var tv = new TextView { Width = Dim.Fill (), Height = Dim.Fill (), Text = text, WordWrap = true };
         string envText = tv.Text;
-        Application.Top.Add (tv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tv);
+        Application.Begin (top);
 
         Assert.True (tv.WordWrap);
         Assert.Equal (Point.Empty, tv.CursorPosition);
@@ -911,8 +917,9 @@ This is the second line.
         const string text = "This is the first line.\nThis is the second line.\n";
         var tv = new TextView { Width = Dim.Fill (), Height = Dim.Fill (), Text = text };
         string envText = tv.Text;
-        Application.Top.Add (tv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tv);
+        Application.Begin (top);
 
         Assert.False (tv.WordWrap);
         Assert.Equal (Point.Empty, tv.CursorPosition);
@@ -981,8 +988,9 @@ This is the second line.
         const string text = "This is the first line.\nThis is the second line.\n";
         var tv = new TextView { Width = Dim.Fill (), Height = Dim.Fill (), Text = text, WordWrap = true };
         string envText = tv.Text;
-        Application.Top.Add (tv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tv);
+        Application.Begin (top);
 
         Assert.True (tv.WordWrap);
         Assert.Equal (Point.Empty, tv.CursorPosition);
@@ -2265,7 +2273,7 @@ This is the second line.
     {
         var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
         var tv = new TextView { Width = 10, Height = 2, Text = text };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (tv);
         Application.Begin (top);
 
@@ -2550,7 +2558,7 @@ This is the second line.
     {
         var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
         var tv = new TextView { Width = 10, Height = 2, Text = text };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (tv);
         Application.Begin (top);
 
@@ -2715,7 +2723,7 @@ This is the second line.
     {
         var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
         var tv = new TextView { Width = 10, Height = 2, Text = text };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (tv);
         Application.Begin (top);
 
@@ -3280,7 +3288,7 @@ This is the second line.
         var text =
             $"This is the first line.{Environment.NewLine}This is the second line.\nThis is the third line.";
         var tv = new TextView { Width = 10, Height = 2, Text = text };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (tv);
         Application.Begin (top);
 
@@ -3460,7 +3468,7 @@ This is the second line.
     {
         var text = "One\nTwo\nThree";
         var tv = new TextView { Width = 10, Height = 2, Text = text };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (tv);
         Application.Begin (top);
 
@@ -3523,7 +3531,7 @@ This is the second line.
     {
         var text = "One\nTwo\nThree\n";
         var tv = new TextView { Width = 10, Height = 2, Text = text };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (tv);
         Application.Begin (top);
 
@@ -3585,7 +3593,7 @@ This is the second line.
     public void HistoryText_Undo_Redo_Multi_Line_Selected_With_Empty_Text ()
     {
         var tv = new TextView { Width = 10, Height = 2 };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (tv);
         Application.Begin (top);
 
@@ -3940,7 +3948,7 @@ This is the second line.
     public void HistoryText_Undo_Redo_Multi_Line_With_Empty_Text ()
     {
         var tv = new TextView { Width = 10, Height = 2 };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (tv);
         Application.Begin (top);
 
@@ -4412,7 +4420,7 @@ This is the second line.
     {
         var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
         var tv = new TextView { Width = 10, Height = 2, Text = text };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (tv);
         Application.Begin (top);
 
@@ -4485,7 +4493,7 @@ This is the second line.
     {
         var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
         var tv = new TextView { Width = 10, Height = 2, Text = text };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (tv);
         Application.Begin (top);
 
@@ -4558,7 +4566,7 @@ This is the second line.
     {
         var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
         var tv = new TextView { Width = 10, Height = 2, Text = text };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (tv);
         Application.Begin (top);
 
@@ -4627,7 +4635,7 @@ This is the second line.
     {
         var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
         var tv = new TextView { Width = 10, Height = 2, Text = text };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (tv);
         Application.Begin (top);
 
@@ -4712,7 +4720,7 @@ This is the second line.
     {
         var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
         var tv = new TextView { Width = 10, Height = 2, Text = text };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (tv);
         Application.Begin (top);
 
@@ -4797,7 +4805,7 @@ This is the second line.
     {
         var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
         var tv = new TextView { Width = 10, Height = 2, Text = text };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (tv);
         Application.Begin (top);
 
@@ -5423,7 +5431,7 @@ This is the second line.
     {
         var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
         var tv = new TextView { Width = 10, Height = 2, Text = text };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (tv);
         Application.Begin (top);
 
@@ -6997,8 +7005,9 @@ This is the second line.
     public void Selected_Text_Shows ()
     {
         // Proves #3022 is fixed (TextField selected text does not show in v2)
-        Application.Top.Add (_textView);
-        RunState rs = Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (_textView);
+        RunState rs = Application.Begin (top);
 
         _textView.CursorPosition = Point.Empty;
         _textView.SelectionStartColumn = 0;
@@ -7107,7 +7116,8 @@ This is the second line.
     [TextViewTestsAutoInitShutdown]
     public void Tab_Test_Follow_By_BackTab ()
     {
-        Application.Top.Add (_textView);
+        var top = new Toplevel ();
+        top.Add (_textView);
 
         Application.Iteration += (s, a) =>
                                  {
@@ -7137,18 +7147,19 @@ This is the second line.
                                          Assert.Equal (leftCol, _textView.LeftColumn);
                                      }
 
-                                     Application.Top.Remove (_textView);
+                                     top.Remove (_textView);
                                      Application.RequestStop ();
                                  };
 
-        Application.Run ();
+        Application.Run (top);
     }
 
     [Fact]
     [TextViewTestsAutoInitShutdown]
     public void Tab_Test_Follow_By_BackTab_With_Text ()
     {
-        Application.Top.Add (_textView);
+        var top = new Toplevel ();
+        top.Add (_textView);
 
         Application.Iteration += (s, a) =>
                                  {
@@ -7178,18 +7189,19 @@ This is the second line.
                                          Assert.Equal (leftCol, _textView.LeftColumn);
                                      }
 
-                                     Application.Top.Remove (_textView);
+                                     top.Remove (_textView);
                                      Application.RequestStop ();
                                  };
 
-        Application.Run ();
+        Application.Run (top);
     }
 
     [Fact]
     [TextViewTestsAutoInitShutdown]
     public void Tab_Test_Follow_By_CursorLeft_And_Then_Follow_By_CursorRight ()
     {
-        Application.Top.Add (_textView);
+        var top = new Toplevel ();
+        top.Add (_textView);
 
         Application.Iteration += (s, a) =>
                                  {
@@ -7228,18 +7240,19 @@ This is the second line.
                                          Assert.Equal (leftCol, _textView.LeftColumn);
                                      }
 
-                                     Application.Top.Remove (_textView);
+                                     top.Remove (_textView);
                                      Application.RequestStop ();
                                  };
 
-        Application.Run ();
+        Application.Run (top);
     }
 
     [Fact]
     [TextViewTestsAutoInitShutdown]
     public void Tab_Test_Follow_By_CursorLeft_And_Then_Follow_By_CursorRight_With_Text ()
     {
-        Application.Top.Add (_textView);
+        var top = new Toplevel ();
+        top.Add (_textView);
 
         Application.Iteration += (s, a) =>
                                  {
@@ -7280,18 +7293,19 @@ This is the second line.
                                          Assert.Equal (leftCol, _textView.LeftColumn);
                                      }
 
-                                     Application.Top.Remove (_textView);
+                                     top.Remove (_textView);
                                      Application.RequestStop ();
                                  };
 
-        Application.Run ();
+        Application.Run (top);
     }
 
     [Fact]
     [TextViewTestsAutoInitShutdown]
     public void Tab_Test_Follow_By_Home_And_Then_Follow_By_End_And_Then_Follow_By_BackTab_With_Text ()
     {
-        Application.Top.Add (_textView);
+        var top = new Toplevel ();
+        top.Add (_textView);
 
         Application.Iteration += (s, a) =>
                                  {
@@ -7348,19 +7362,20 @@ This is the second line.
                                      Assert.Equal ("TAB to jump between text fields.", _textView.Text);
                                      Assert.Equal (32, _textView.Text.Length);
 
-                                     Application.Top.Remove (_textView);
+                                     top.Remove (_textView);
                                      Application.RequestStop ();
                                  };
 
-        Application.Run ();
+        Application.Run (top);
     }
 
     [Fact]
     [TextViewTestsAutoInitShutdown]
     public void TabWidth_Setting_To_Zero_Keeps_AllowsTab ()
     {
-        Application.Top.Add (_textView);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (_textView);
+        Application.Begin (top);
 
         Assert.Equal (4, _textView.TabWidth);
         Assert.True (_textView.AllowsTab);
@@ -7454,8 +7469,9 @@ TAB to jump between text field",
 
         var win = new Window ();
         win.Add (tv);
-        Application.Top.Add (win);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (win);
+        Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (15, 15);
         Application.Refresh ();
 
@@ -7530,8 +7546,9 @@ TAB to jump between text field",
 
         var win = new Window ();
         win.Add (tv);
-        Application.Top.Add (win);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (win);
+        Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (15, 15);
         Application.Refresh ();
 
@@ -7627,8 +7644,9 @@ TAB to jump between text field",
             Width = Dim.Fill (), Height = Dim.Fill (), Text = "This is the first line.\nThis is the second line.\n"
         };
         tv.UnwrappedCursorPosition += (s, e) => { cp = e.Point; };
-        Application.Top.Add (tv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tv);
+        Application.Begin (top);
 
         Assert.False (tv.WordWrap);
         Assert.Equal (Point.Empty, tv.CursorPosition);
@@ -8797,8 +8815,9 @@ line.
     public void WordWrap_Deleting_Backwards ()
     {
         var tv = new TextView { Width = 5, Height = 2, WordWrap = true, Text = "aaaa" };
-        Application.Top.Add (tv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tv);
+        Application.Begin (top);
 
         Assert.Equal (Point.Empty, tv.CursorPosition);
         Assert.Equal (0, tv.LeftColumn);
@@ -8872,10 +8891,11 @@ a
     [InlineData (KeyCode.Delete)]
     public void WordWrap_Draw_Typed_Keys_After_Text_Is_Deleted (KeyCode del)
     {
-        Application.Top.Add (_textView);
+        var top = new Toplevel ();
+        top.Add (_textView);
         _textView.Text = "Line 1.\nLine 2.";
         _textView.WordWrap = true;
-        Application.Begin (Application.Top);
+        Application.Begin (top);
 
         Assert.True (_textView.WordWrap);
 
@@ -8934,7 +8954,7 @@ Line 2.",
     }
 
     [Fact]
-    [TextViewTestsAutoInitShutdown]
+    [AutoInitShutdown]
     public void WordWrap_ReadOnly_CursorPosition_SelectedText_Copy ()
     {
         //          0123456789
@@ -8948,8 +8968,9 @@ Line 2.",
                      );
         tv.WordWrap = true;
 
-        Application.Top.Add (tv);
-        Application.Top.LayoutSubviews ();
+        var top = new Toplevel ();
+        top.Add (tv);
+        top.LayoutSubviews ();
         tv.Draw ();
 
         TestHelpers.AssertDriverContentsWithFrameAre (
@@ -8968,7 +8989,7 @@ line.
         tv.ReadOnly = true;
         tv.CursorPosition = new Point (6, 2);
         Assert.Equal (new Point (5, 2), tv.CursorPosition);
-        Application.Top.LayoutSubviews ();
+        top.LayoutSubviews ();
         tv.Draw ();
 
         TestHelpers.AssertDriverContentsWithFrameAre (
@@ -9052,7 +9073,8 @@ line.
                      );
         tv.WordWrap = true;
 
-        Application.Top.Add (tv);
+        var top = new Toplevel ();
+        top.Add (tv);
 
         tv.LayoutSubviews ();
         tv.Draw ();

+ 8 - 4
UnitTests/Views/TileViewTests.cs

@@ -1575,7 +1575,8 @@ public class TileViewTests
 
         Assert.True (tv.IsRootTileView ());
 
-        Application.Top.Add (tv);
+        var top = new Toplevel ();
+        top.Add (tv);
         tv.BeginInit ();
         tv.EndInit ();
         tv.LayoutSubviews ();
@@ -1624,7 +1625,8 @@ public class TileViewTests
         Assert.True (tv.IsRootTileView ());
         tv.Tiles.ElementAt (1).ContentView.Add (tv2);
 
-        Application.Top.Add (tv);
+        var top = new Toplevel ();
+        top.Add (tv);
         tv.BeginInit ();
         tv.EndInit ();
         tv.LayoutSubviews ();
@@ -2303,7 +2305,8 @@ public class TileViewTests
                             new Label { AutoSize = false, Width = Dim.Fill (), Height = 1, Text = new string ('5', 100) }
                            );
 
-        Application.Top.Add (tv);
+        var top = new Toplevel ();
+        top.Add (tv);
         tv.BeginInit ();
         tv.EndInit ();
         tv.LayoutSubviews ();
@@ -2429,7 +2432,8 @@ public class TileViewTests
         container.Tiles.ElementAt (0).MinSize = 0;
         container.Tiles.ElementAt (1).MinSize = 0;
 
-        Application.Top.Add (container);
+        var top = new Toplevel ();
+        top.Add (container);
         container.ColorScheme = new ColorScheme ();
         container.BeginInit ();
         container.EndInit ();

+ 84 - 58
UnitTests/Views/ToplevelTests.cs

@@ -458,7 +458,7 @@ public class ToplevelTests
         var tf2W2 = new TextField { Id = "tf2W2", X = Pos.Left (tf1W2), Width = Dim.Fill (), Text = "Text2 on Win2" };
         win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
 
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (win1, win2);
         top.Loaded += (s, e) => isRunning = true;
         top.Closing += (s, e) => isRunning = false;
@@ -560,7 +560,7 @@ public class ToplevelTests
     [AutoInitShutdown]
     public void KeyBindings_Command_With_OverlappedTop ()
     {
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         Assert.Null (Application.OverlappedTop);
         top.IsOverlappedContainer = true;
         Application.Begin (top);
@@ -740,6 +740,8 @@ public class ToplevelTests
 #if UNIX_KEY_BINDINGS
         Assert.True (Application.OverlappedChildren [0].ProcessKeyDown (new (Key.L.WithCtrl)));
 #endif
+        win2.Dispose ();
+        win1.Dispose ();
     }
 
     [Fact]
@@ -780,7 +782,7 @@ public class ToplevelTests
         var win = new Window ();
         win.Add (view);
         Application.Init (new FakeDriver ());
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (win);
 
         Assert.True (wasAdded);
@@ -796,19 +798,19 @@ public class ToplevelTests
         Key alternateBackwardKey = KeyCode.Null;
         Key quitKey = KeyCode.Null;
 
+        Toplevel top = new ();
         var view = new View ();
         view.Initialized += View_Initialized;
 
         void View_Initialized (object sender, EventArgs e)
         {
-            Application.Top.AlternateForwardKeyChanged += (s, e) => alternateForwardKey = e.OldKey;
-            Application.Top.AlternateBackwardKeyChanged += (s, e) => alternateBackwardKey = e.OldKey;
-            Application.Top.QuitKeyChanged += (s, e) => quitKey = e.OldKey;
+            top.AlternateForwardKeyChanged += (s, e) => alternateForwardKey = e.OldKey;
+            top.AlternateBackwardKeyChanged += (s, e) => alternateBackwardKey = e.OldKey;
+            top.QuitKeyChanged += (s, e) => quitKey = e.OldKey;
         }
 
         var win = new Window ();
         win.Add (view);
-        Toplevel top = Application.Top;
         top.Add (win);
         Application.Begin (top);
 
@@ -847,7 +849,7 @@ public class ToplevelTests
     public void Mouse_Drag_On_Top_With_Superview_Null ()
     {
         var win = new Window ();
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (win);
         int iterations = -1;
         Window testWindow;
@@ -1003,7 +1005,7 @@ public class ToplevelTests
                                      }
                                  };
 
-        Application.Run ();
+        Application.Run (top);
     }
 
     [Fact]
@@ -1011,7 +1013,7 @@ public class ToplevelTests
     public void Mouse_Drag_On_Top_With_Superview_Not_Null ()
     {
         var win = new Window { X = 3, Y = 2, Width = 10, Height = 5 };
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (win);
 
         int iterations = -1;
@@ -1132,7 +1134,7 @@ public class ToplevelTests
                                      }
                                  };
 
-        Application.Run ();
+        Application.Run (top);
     }
 
     [Fact]
@@ -1158,7 +1160,7 @@ public class ToplevelTests
         var v = new View ();
         v.Enter += (s, _) => isEnter = true;
         v.Leave += (s, _) => isLeave = true;
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (v);
 
         Assert.False (v.CanFocus);
@@ -1168,27 +1170,38 @@ public class ToplevelTests
         Assert.Null (exception);
 
         v.CanFocus = true;
-        Application.Begin (top);
+        RunState rsTop = Application.Begin (top);
 
+        // From the v view
         Assert.True (isEnter);
+        // The Leave event is only raised on the End method
+        // and the top is still running
         Assert.False (isLeave);
 
         isEnter = false;
         var d = new Dialog ();
-        RunState rs = Application.Begin (d);
+        var dv = new View { CanFocus = true };
+        dv.Enter += (s, _) => isEnter = true;
+        dv.Leave += (s, _) => isLeave = true;
+        d.Add (dv);
+        RunState rsDialog = Application.Begin (d);
 
-        Assert.False (isEnter);
-        Assert.True (isLeave);
+        // From the dv view
+        Assert.True (isEnter);
+        Assert.False (isLeave);
+        Assert.True (dv.HasFocus);
 
-        isLeave = false;
-        Application.End (rs);
+        isEnter = false;
 
-        Assert.True (isEnter);
+        Application.End (rsDialog);
 
-        Assert.False (
-                      isLeave
-                     ); // Leave event cannot be trigger because it v.Enter was performed and v is focused
+        // From the v view
+        Assert.True (isEnter);
+        // From the dv view
+        Assert.True (isLeave);
         Assert.True (v.HasFocus);
+
+        Application.End (rsTop);
     }
 
     [Fact]
@@ -1196,11 +1209,11 @@ public class ToplevelTests
     public void OnEnter_OnLeave_Triggered_On_Application_Begin_End_With_More_Toplevels ()
     {
         var iterations = 0;
-        var steps = new int [5];
+        var steps = new int [4];
         var isEnterTop = false;
         var isLeaveTop = false;
         var vt = new View ();
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         var diag = new Dialog ();
 
         vt.Enter += (s, e) =>
@@ -1215,15 +1228,15 @@ public class ToplevelTests
                         }
                         else
                         {
-                            steps [4] = iterations;
+                            steps [3] = iterations;
                             Assert.Equal (diag, e.View);
                         }
                     };
 
         vt.Leave += (s, e) =>
                     {
+                        // This will never be raised
                         iterations++;
-                        steps [1] = iterations;
                         isLeaveTop = true;
                         Assert.Equal (diag, e.View);
                     };
@@ -1236,7 +1249,7 @@ public class ToplevelTests
         Assert.Null (exception);
 
         vt.CanFocus = true;
-        Application.Begin (top);
+        RunState rsTop = Application.Begin (top);
 
         Assert.True (isEnterTop);
         Assert.False (isLeaveTop);
@@ -1249,7 +1262,7 @@ public class ToplevelTests
         vd.Enter += (s, e) =>
                     {
                         iterations++;
-                        steps [2] = iterations;
+                        steps [1] = iterations;
                         isEnterDiag = true;
                         Assert.Null (e.View);
                     };
@@ -1257,7 +1270,7 @@ public class ToplevelTests
         vd.Leave += (s, e) =>
                     {
                         iterations++;
-                        steps [3] = iterations;
+                        steps [2] = iterations;
                         isLeaveDiag = true;
                         Assert.Equal (top, e.View);
                     };
@@ -1270,30 +1283,33 @@ public class ToplevelTests
         Assert.Null (exception);
 
         vd.CanFocus = true;
-        RunState rs = Application.Begin (diag);
+        RunState rsDiag = Application.Begin (diag);
 
         Assert.True (isEnterDiag);
         Assert.False (isLeaveDiag);
         Assert.False (isEnterTop);
-        Assert.True (isLeaveTop);
+        // The Leave event is only raised on the End method
+        // and the top is still running
+        Assert.False (isLeaveTop);
 
         isEnterDiag = false;
         isLeaveTop = false;
-        Application.End (rs);
+        Application.End (rsDiag);
 
         Assert.False (isEnterDiag);
         Assert.True (isLeaveDiag);
         Assert.True (isEnterTop);
-
-        Assert.False (
-                      isLeaveTop
-                     ); // Leave event cannot be trigger because it v.Enter was performed and v is focused
+        // Leave event on top cannot be raised
+        // because Current is null on the End method
+        Assert.False (isLeaveTop);
         Assert.True (vt.HasFocus);
+
+        Application.End (rsTop);
+
         Assert.Equal (1, steps [0]);
         Assert.Equal (2, steps [1]);
         Assert.Equal (3, steps [2]);
-        Assert.Equal (4, steps [3]);
-        Assert.Equal (5, steps [^1]);
+        Assert.Equal (4, steps [^1]);
     }
 
     [Fact]
@@ -1303,8 +1319,9 @@ public class ToplevelTests
         var tf = new TextField { Width = 5, Text = "test" };
         var view = new View { Width = 10, Height = 10 };
         view.Add (tf);
-        Application.Top.Add (view);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (view);
+        Application.Begin (top);
 
         Assert.True (tf.HasFocus);
         Application.Driver.GetCursorVisibility (out CursorVisibility cursor);
@@ -1321,7 +1338,7 @@ public class ToplevelTests
     [AutoInitShutdown]
     public void IsLoaded_Application_Begin ()
     {
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         Assert.False (top.IsLoaded);
 
         Application.Begin (top);
@@ -1332,7 +1349,7 @@ public class ToplevelTests
     [AutoInitShutdown]
     public void IsLoaded_With_Sub_Toplevel_Application_Begin_NeedDisplay ()
     {
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         var subTop = new Toplevel ();
         var view = new View { Frame = new Rectangle (0, 0, 20, 10) };
         subTop.Add (view);
@@ -1380,7 +1397,7 @@ public class ToplevelTests
         };
         var win = new Window { X = 3, Y = 3, Width = Dim.Fill (3), Height = Dim.Fill (3) };
         scrollView.Add (win);
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (scrollView);
         Application.Begin (top);
 
@@ -1514,11 +1531,11 @@ public class ToplevelTests
     [AutoInitShutdown]
     public void Window_Bounds_Bigger_Than_Driver_Cols_And_Rows_Allow_Drag_Beyond_Left_Right_And_Bottom ()
     {
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         var window = new Window { Width = 20, Height = 3 };
-        Application.Begin (top);
+        RunState rsTop = Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
-        Application.Begin (window);
+        RunState rsWindow = Application.Begin (window);
         Application.Refresh ();
         Assert.Equal (new Rectangle (0, 0, 40, 10), top.Frame);
         Assert.Equal (new Rectangle (0, 0, 20, 3), window.Frame);
@@ -1661,6 +1678,9 @@ public class ToplevelTests
         Assert.Equal (new Rectangle (0, 0, 19, 2), top.Frame);
         Assert.Equal (new Rectangle (19, 2, 20, 3), window.Frame);
         TestHelpers.AssertDriverContentsWithFrameAre (@"", _output);
+
+        Application.End (rsWindow);
+        Application.End (rsTop);
     }
 
     [Fact]
@@ -1749,15 +1769,18 @@ public class ToplevelTests
     [AutoInitShutdown]
     public void Begin_With_Window_Sets_Size_Correctly ()
     {
-        Toplevel top = Application.Top;
-        Application.Begin (top);
+        Toplevel top = new ();
+        RunState rsTop = Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (20, 20);
 
         var testWindow = new Window { X = 2, Y = 1, Width = 15, Height = 10 };
         Assert.Equal (new Rectangle (2, 1, 15, 10), testWindow.Frame);
 
-        RunState rs = Application.Begin (testWindow);
+        RunState rsTestWindow = Application.Begin (testWindow);
         Assert.Equal (new Rectangle (2, 1, 15, 10), testWindow.Frame);
+
+        Application.End (rsTestWindow);
+        Application.End (rsTop);
     }
 
     // Don't use Dialog as a Top, use a Window instead - dialog has complex layout behavior that is not needed here.
@@ -1765,10 +1788,10 @@ public class ToplevelTests
     [AutoInitShutdown]
     public void Draw_A_Top_Subview_On_A_Window ()
     {
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         var win = new Window ();
         top.Add (win);
-        Application.Begin (top);
+        RunState rsTop = Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (20, 20);
 
         Assert.Equal (new Rectangle (0, 0, 20, 20), win.Frame);
@@ -1837,7 +1860,7 @@ public class ToplevelTests
                                     Application.Current.DrawContentComplete -= testWindow_DrawContentComplete;
                                 }
                             };
-        RunState rs = Application.Begin (testWindow);
+        RunState rsTestWindow = Application.Begin (testWindow);
 
         Assert.Equal (new Rectangle (2, 1, 15, 10), testWindow.Frame);
 
@@ -1878,7 +1901,7 @@ public class ToplevelTests
         Application.Top.Draw ();
 
         var firstIteration = false;
-        Application.RunIteration (ref rs, ref firstIteration);
+        Application.RunIteration (ref rsTestWindow, ref firstIteration);
 
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @$"
@@ -1909,7 +1932,8 @@ public class ToplevelTests
                                                       _output
                                                      );
 
-        Application.End (rs);
+        Application.End (rsTestWindow);
+        Application.End (rsTop);
     }
 
     [Fact]
@@ -1925,8 +1949,9 @@ public class ToplevelTests
         };
         var topChild = new Toplevel ();
         topChild.Add (menu);
-        Application.Top.Add (topChild);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (topChild);
+        Application.Begin (top);
 
         Exception exception = Record.Exception (() => topChild.NewKeyDownEvent (KeyCode.AltMask));
         Assert.Null (exception);
@@ -1938,7 +1963,7 @@ public class ToplevelTests
     {
         Application.Init (new FakeDriver ());
 
-        Toplevel t = Application.Top;
+        Toplevel t = new ();
         var w = new Window ();
         t.Add (w);
 
@@ -2063,7 +2088,8 @@ public class ToplevelTests
             testWindow.Dispose ();
         }
 
-        Application.Run ();
+        Application.Run (t);
+        t.Dispose ();
         Application.Shutdown ();
     }
 }

+ 8 - 4
UnitTests/Views/TreeTableSourceTests.cs

@@ -150,11 +150,14 @@ public class TreeTableSourceTests : IDisposable
     [AutoInitShutdown]
     public void TestTreeTableSource_CombinedWithCheckboxes ()
     {
+        Toplevel top = new ();
         TableView tv = GetTreeTable (out TreeView<IDescribedThing> treeSource);
 
         CheckBoxTableSourceWrapperByIndex checkSource;
         tv.Table = checkSource = new CheckBoxTableSourceWrapperByIndex (tv, tv.Table);
         tv.Style.GetOrCreateColumnStyle (2).MinAcceptableWidth = 1;
+        top.Add (tv);
+        Application.Begin (top);
 
         tv.Draw ();
 
@@ -181,7 +184,7 @@ public class TreeTableSourceTests : IDisposable
         Assert.Equal (0, tv.SelectedRow);
         Assert.Equal (1, tv.SelectedColumn);
 
-        Application.Top.NewKeyDownEvent (Key.CursorRight);
+        top.NewKeyDownEvent (Key.CursorRight);
 
         tv.Draw ();
 
@@ -280,9 +283,10 @@ public class TreeTableSourceTests : IDisposable
         tableView.EndInit ();
         tableView.LayoutSubviews ();
 
-        Application.Top.Add (tableView);
-        Application.Top.EnsureFocus ();
-        Assert.Equal (tableView, Application.Top.MostFocused);
+        var top = new Toplevel ();
+        top.Add (tableView);
+        top.EnsureFocus ();
+        Assert.Equal (tableView, top.MostFocused);
 
         return tableView;
     }

+ 3 - 2
UnitTests/Views/TreeViewTests.cs

@@ -110,8 +110,9 @@ public class TreeViewTests
         tv.AddObject (n1);
         tv.AddObject (n2);
 
-        Application.Top.Add (tv);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (tv);
+        Application.Begin (top);
 
         Assert.True (tv.MultiSelect);
         Assert.True (tv.HasFocus);

+ 1 - 1
UnitTests/Views/ViewDisposalTest.cs

@@ -35,7 +35,7 @@ public class ViewDisposalTest
     {
         GetSpecialParams ();
         var Container = new View ();
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         List<Type> views = GetViews ();
 
         foreach (Type view in views)

+ 4 - 3
UnitTests/Views/WindowTests.cs

@@ -20,8 +20,9 @@ public class WindowTests
         };
         var win = new Window ();
         win.Add (menu);
-        Application.Top.Add (win);
-        Application.Begin (Application.Top);
+        var top = new Toplevel ();
+        top.Add (win);
+        Application.Begin (top);
 
         Exception exception = Record.Exception (() => win.NewKeyDownEvent (KeyCode.AltMask));
         Assert.Null (exception);
@@ -55,7 +56,7 @@ public class WindowTests
         var fv = new FrameView { Y = 1, Width = Dim.Fill (), Height = Dim.Fill (1), Title = "Frame View" };
         var win = new Window ();
         win.Add (menu, sb, fv);
-        Toplevel top = Application.Top;
+        Toplevel top = new ();
         top.Add (win);
         Application.Begin (top);
         ((FakeDriver)Application.Driver).SetBufferSize (20, 10);

部分文件因为文件数量过多而无法显示