123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935 |
- using UnitTests;
- namespace Terminal.Gui.ViewsTests;
- public class ToplevelTests
- {
- public ToplevelTests ()
- {
- #if DEBUG_IDISPOSABLE
- View.DebugIDisposable = true;
- #endif
- }
- [Fact]
- public void Constructor_Default ()
- {
- var top = new Toplevel ();
- Assert.Equal (Colors.ColorSchemes ["TopLevel"], top.ColorScheme);
- Assert.Equal ("Fill(Absolute(0))", top.Width.ToString ());
- Assert.Equal ("Fill(Absolute(0))", top.Height.ToString ());
- Assert.False (top.Running);
- Assert.False (top.Modal);
- Assert.Null (top.MenuBar);
- //Assert.Null (top.StatusBar);
- }
- [Fact]
- public void Arrangement_Default_Is_Overlapped ()
- {
- var top = new Toplevel ();
- Assert.Equal (ViewArrangement.Overlapped, top.Arrangement);
- }
- [Fact]
- [AutoInitShutdown]
- public void Internal_Tests ()
- {
- var top = new Toplevel ();
- var eventInvoked = "";
- top.Loaded += (s, e) => eventInvoked = "Loaded";
- top.OnLoaded ();
- Assert.Equal ("Loaded", eventInvoked);
- top.Ready += (s, e) => eventInvoked = "Ready";
- top.OnReady ();
- Assert.Equal ("Ready", eventInvoked);
- top.Unloaded += (s, e) => eventInvoked = "Unloaded";
- top.OnUnloaded ();
- Assert.Equal ("Unloaded", eventInvoked);
- top.Add (new MenuBar ());
- Assert.NotNull (top.MenuBar);
- //top.Add (new StatusBar ());
- //Assert.NotNull (top.StatusBar);
- MenuBar menuBar = top.MenuBar;
- top.Remove (top.MenuBar);
- Assert.Null (top.MenuBar);
- Assert.NotNull (menuBar);
- //var statusBar = top.StatusBar;
- //top.Remove (top.StatusBar);
- //Assert.Null (top.StatusBar);
- //Assert.NotNull (statusBar);
- #if DEBUG_IDISPOSABLE
- Assert.False (menuBar.WasDisposed);
- //Assert.False (statusBar.WasDisposed);
- menuBar.Dispose ();
- //statusBar.Dispose ();
- Assert.True (menuBar.WasDisposed);
- //Assert.True (statusBar.WasDisposed);
- #endif
- Application.Begin (top);
- Assert.Equal (top, Application.Top);
- // Application.Top without menu and status bar.
- View supView = View.GetLocationEnsuringFullVisibility (top, 2, 2, out int nx, out int ny /*, out StatusBar sb*/);
- Assert.Equal (Application.Top, supView);
- Assert.Equal (0, nx);
- Assert.Equal (0, ny);
- //Assert.Null (sb);
- top.Add (new MenuBar ());
- Assert.NotNull (top.MenuBar);
- // Application.Top with a menu and without status bar.
- View.GetLocationEnsuringFullVisibility (top, 2, 2, out nx, out ny /*, out sb*/);
- Assert.Equal (0, nx);
- Assert.Equal (1, ny);
- //Assert.Null (sb);
- //top.Add (new StatusBar ());
- //Assert.NotNull (top.StatusBar);
- // Application.Top with a menu and status bar.
- View.GetLocationEnsuringFullVisibility (top, 2, 2, out nx, out ny /*, out sb*/);
- Assert.Equal (0, nx);
- // The available height is lower than the Application.Top height minus
- // the menu bar and status bar, then the top can go beyond the bottom
- // Assert.Equal (2, ny);
- //Assert.NotNull (sb);
- menuBar = top.MenuBar;
- top.Remove (top.MenuBar);
- Assert.Null (top.MenuBar);
- Assert.NotNull (menuBar);
- // Application.Top without a menu and with a status bar.
- View.GetLocationEnsuringFullVisibility (top, 2, 2, out nx, out ny /*, out sb*/);
- Assert.Equal (0, nx);
- // The available height is lower than the Application.Top height minus
- // the status bar, then the top can go beyond the bottom
- // Assert.Equal (2, ny);
- //Assert.NotNull (sb);
- //statusBar = top.StatusBar;
- //top.Remove (top.StatusBar);
- //Assert.Null (top.StatusBar);
- //Assert.NotNull (statusBar);
- Assert.Null (top.MenuBar);
- var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
- top.Add (win);
- top.LayoutSubViews ();
- // The SuperView is always the same regardless of the caller.
- supView = View.GetLocationEnsuringFullVisibility (win, 0, 0, out nx, out ny /*, out sb*/);
- Assert.Equal (Application.Top, supView);
- supView = View.GetLocationEnsuringFullVisibility (win, 0, 0, out nx, out ny /*, out sb*/);
- Assert.Equal (Application.Top, supView);
- // Application.Top without menu and status bar.
- View.GetLocationEnsuringFullVisibility (win, 0, 0, out nx, out ny /*, out sb*/);
- Assert.Equal (0, nx);
- Assert.Equal (0, ny);
- //Assert.Null (sb);
- top.Add (new MenuBar ());
- Assert.NotNull (top.MenuBar);
- // Application.Top with a menu and without status bar.
- View.GetLocationEnsuringFullVisibility (win, 2, 2, out nx, out ny /*, out sb*/);
- Assert.Equal (0, nx);
- Assert.Equal (1, ny);
- //Assert.Null (sb);
- top.Add (new StatusBar ());
- //Assert.NotNull (top.StatusBar);
- // Application.Top with a menu and status bar.
- View.GetLocationEnsuringFullVisibility (win, 30, 20, out nx, out ny /*, out sb*/);
- Assert.Equal (0, nx);
- // The available height is lower than the Application.Top height minus
- // the menu bar and status bar, then the top can go beyond the bottom
- //Assert.Equal (20, ny);
- //Assert.NotNull (sb);
- menuBar = top.MenuBar;
- //statusBar = top.StatusBar;
- top.Remove (top.MenuBar);
- Assert.Null (top.MenuBar);
- Assert.NotNull (menuBar);
- //top.Remove (top.StatusBar);
- //Assert.Null (top.StatusBar);
- //Assert.NotNull (statusBar);
- top.Remove (win);
- win = new () { Width = 60, Height = 15 };
- top.Add (win);
- // Application.Top without menu and status bar.
- View.GetLocationEnsuringFullVisibility (win, 0, 0, out nx, out ny /*, out sb*/);
- Assert.Equal (0, nx);
- Assert.Equal (0, ny);
- //Assert.Null (sb);
- top.Add (new MenuBar ());
- Assert.NotNull (top.MenuBar);
- // Application.Top with a menu and without status bar.
- View.GetLocationEnsuringFullVisibility (win, 2, 2, out nx, out ny /*, out sb*/);
- Assert.Equal (2, nx);
- Assert.Equal (2, ny);
- //Assert.Null (sb);
- top.Add (new StatusBar ());
- //Assert.NotNull (top.StatusBar);
- // Application.Top with a menu and status bar.
- View.GetLocationEnsuringFullVisibility (win, 30, 20, out nx, out ny /*, out sb*/);
- Assert.Equal (20, nx); // 20+60=80
- //Assert.Equal (9, ny); // 9+15+1(mb)=25
- //Assert.NotNull (sb);
- //Assert.Null (Toplevel._dragPosition);
- win.NewMouseEvent (new () { Position = new (6, 0), Flags = MouseFlags.Button1Pressed });
- // Assert.Equal (new Point (6, 0), Toplevel._dragPosition);
- win.NewMouseEvent (new () { Position = new (6, 0), Flags = MouseFlags.Button1Released });
- //Assert.Null (Toplevel._dragPosition);
- win.CanFocus = false;
- win.NewMouseEvent (new () { Position = new (6, 0), Flags = MouseFlags.Button1Pressed });
- //Assert.Null (Toplevel._dragPosition);
- #if DEBUG_IDISPOSABLE
- Assert.False (top.MenuBar.WasDisposed);
- //Assert.False (top.StatusBar.WasDisposed);
- #endif
- menuBar = top.MenuBar;
- //statusBar = top.StatusBar;
- top.Dispose ();
- Assert.Null (top.MenuBar);
- //Assert.Null (top.StatusBar);
- Assert.NotNull (menuBar);
- //Assert.NotNull (statusBar);
- #if DEBUG_IDISPOSABLE
- Assert.True (menuBar.WasDisposed);
- //Assert.True (statusBar.WasDisposed);
- #endif
- }
- [Fact]
- public void SuperViewChanged_Should_Not_Be_Used_To_Initialize_Toplevel_Events ()
- {
- var wasAdded = false;
- var view = new View ();
- view.SuperViewChanged += SuperViewChanged;
- var win = new Window ();
- win.Add (view);
- Application.Init (new FakeDriver ());
- Toplevel top = new ();
- top.Add (win);
- Assert.True (wasAdded);
- Application.Shutdown ();
- return;
- void SuperViewChanged (object sender, SuperViewChangedEventArgs _)
- {
- Assert.False (wasAdded);
- wasAdded = true;
- view.SuperViewChanged -= SuperViewChanged;
- }
- }
- [Fact]
- [AutoInitShutdown]
- public void Mouse_Drag_On_Top_With_Superview_Null ()
- {
- var win = new Window ();
- Toplevel top = new ();
- top.Add (win);
- int iterations = -1;
- Window testWindow;
- Application.Iteration += (s, a) =>
- {
- iterations++;
- if (iterations == 0)
- {
- ((FakeDriver)Application.Driver!).SetBufferSize (15, 7);
- // Don't use MessageBox here; it's too complicated for this unit test; just use Window
- testWindow = new ()
- {
- Text = "Hello",
- X = 2,
- Y = 2,
- Width = 10,
- Height = 3,
- Arrangement = ViewArrangement.Movable
- };
- Application.Run (testWindow);
- }
- else if (iterations == 1)
- {
- Assert.Equal (new (2, 2), Application.Top!.Frame.Location);
- }
- else if (iterations == 2)
- {
- Assert.Null (Application.MouseGrabView);
- // Grab the mouse
- Application.RaiseMouseEvent (new () { ScreenPosition = new (3, 2), Flags = MouseFlags.Button1Pressed });
- Assert.Equal (Application.Top!.Border, Application.MouseGrabView);
- Assert.Equal (new (2, 2, 10, 3), Application.Top.Frame);
- }
- else if (iterations == 3)
- {
- Assert.Equal (Application.Top!.Border, Application.MouseGrabView);
- // Drag to left
- Application.RaiseMouseEvent (
- new ()
- {
- ScreenPosition = new (2, 2), Flags = MouseFlags.Button1Pressed
- | MouseFlags.ReportMousePosition
- });
- Application.LayoutAndDraw ();
- Assert.Equal (Application.Top.Border, Application.MouseGrabView);
- Assert.Equal (new (1, 2, 10, 3), Application.Top.Frame);
- }
- else if (iterations == 4)
- {
- Assert.Equal (Application.Top!.Border, Application.MouseGrabView);
- Assert.Equal (new (1, 2), Application.Top.Frame.Location);
- Assert.Equal (Application.Top.Border, Application.MouseGrabView);
- }
- else if (iterations == 5)
- {
- Assert.Equal (Application.Top!.Border, Application.MouseGrabView);
- // Drag up
- Application.RaiseMouseEvent (
- new ()
- {
- ScreenPosition = new (2, 1),
- Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
- });
- Application.LayoutAndDraw ();
- Assert.Equal (Application.Top!.Border, Application.MouseGrabView);
- Assert.Equal (new (1, 1, 10, 3), Application.Top.Frame);
- }
- else if (iterations == 6)
- {
- Assert.Equal (Application.Top!.Border, Application.MouseGrabView);
- Assert.Equal (new (1, 1), Application.Top.Frame.Location);
- Assert.Equal (Application.Top.Border, Application.MouseGrabView);
- Assert.Equal (new (1, 1, 10, 3), Application.Top.Frame);
- }
- else if (iterations == 7)
- {
- Assert.Equal (Application.Top!.Border, Application.MouseGrabView);
- // Ungrab the mouse
- Application.RaiseMouseEvent (new () { ScreenPosition = new (2, 1), Flags = MouseFlags.Button1Released });
- Application.LayoutAndDraw ();
- Assert.Null (Application.MouseGrabView);
- }
- else if (iterations == 8)
- {
- Application.RequestStop ();
- }
- else if (iterations == 9)
- {
- Application.RequestStop ();
- }
- };
- Application.Run (top);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Mouse_Drag_On_Top_With_Superview_Not_Null ()
- {
- var win = new Window { X = 3, Y = 2, Width = 10, Height = 5, Arrangement = ViewArrangement.Movable };
- Toplevel top = new ();
- top.Add (win);
- int iterations = -1;
- var movex = 0;
- var movey = 0;
- var location = new Rectangle (win.Frame.X, win.Frame.Y, 7, 3);
- Application.Iteration += (s, a) =>
- {
- iterations++;
- if (iterations == 0)
- {
- ((FakeDriver)Application.Driver!).SetBufferSize (30, 10);
- }
- else if (iterations == 1)
- {
- location = win.Frame;
- Assert.Null (Application.MouseGrabView);
- // Grab the mouse
- Application.RaiseMouseEvent (
- new ()
- {
- ScreenPosition = new (win.Frame.X, win.Frame.Y), Flags = MouseFlags.Button1Pressed
- });
- Assert.Equal (win.Border, Application.MouseGrabView);
- }
- else if (iterations == 2)
- {
- Assert.Equal (win.Border, Application.MouseGrabView);
- // Drag to left
- movex = 1;
- movey = 0;
- Application.RaiseMouseEvent (
- new ()
- {
- ScreenPosition = new (win.Frame.X + movex, win.Frame.Y + movey), Flags =
- MouseFlags.Button1Pressed
- | MouseFlags.ReportMousePosition
- });
- Assert.Equal (win.Border, Application.MouseGrabView);
- }
- else if (iterations == 3)
- {
- // we should have moved +1, +0
- Assert.Equal (win.Border, Application.MouseGrabView);
- Assert.Equal (win.Border, Application.MouseGrabView);
- location.Offset (movex, movey);
- }
- else if (iterations == 4)
- {
- Assert.Equal (win.Border, Application.MouseGrabView);
- // Drag up
- movex = 0;
- movey = -1;
- Application.RaiseMouseEvent (
- new ()
- {
- ScreenPosition = new (win.Frame.X + movex, win.Frame.Y + movey), Flags =
- MouseFlags.Button1Pressed
- | MouseFlags.ReportMousePosition
- });
- Assert.Equal (win.Border, Application.MouseGrabView);
- }
- else if (iterations == 5)
- {
- // we should have moved +0, -1
- Assert.Equal (win.Border, Application.MouseGrabView);
- location.Offset (movex, movey);
- Assert.Equal (location, win.Frame);
- }
- else if (iterations == 6)
- {
- Assert.Equal (win.Border, Application.MouseGrabView);
- // Ungrab the mouse
- movex = 0;
- movey = 0;
- Application.RaiseMouseEvent (
- new ()
- {
- ScreenPosition = new (win.Frame.X + movex, win.Frame.Y + movey),
- Flags = MouseFlags.Button1Released
- });
- Assert.Null (Application.MouseGrabView);
- }
- else if (iterations == 7)
- {
- Application.RequestStop ();
- }
- };
- Application.Run (top);
- top.Dispose ();
- }
- [Fact]
- [SetupFakeDriver]
- public void GetLocationThatFits_With_Border_Null_Not_Throws ()
- {
- var top = new Toplevel ();
- top.BeginInit ();
- top.EndInit ();
- Exception exception = Record.Exception (() => ((FakeDriver)Application.Driver!).SetBufferSize (0, 10));
- Assert.Null (exception);
- exception = Record.Exception (() => ((FakeDriver)Application.Driver!).SetBufferSize (10, 0));
- Assert.Null (exception);
- }
- [Fact]
- [AutoInitShutdown]
- public void PositionCursor_SetCursorVisibility_To_Invisible_If_Focused_Is_Null ()
- {
- var tf = new TextField { Width = 5, Text = "test" };
- var view = new View { Width = 10, Height = 10, CanFocus = true };
- view.Add (tf);
- var top = new Toplevel ();
- top.Add (view);
- Application.Begin (top);
- Assert.True (tf.HasFocus);
- Application.PositionCursor ();
- Application.Driver!.GetCursorVisibility (out CursorVisibility cursor);
- Assert.Equal (CursorVisibility.Default, cursor);
- view.Enabled = false;
- Assert.False (tf.HasFocus);
- Application.PositionCursor ();
- Application.Driver!.GetCursorVisibility (out cursor);
- Assert.Equal (CursorVisibility.Invisible, cursor);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void IsLoaded_Application_Begin ()
- {
- Toplevel top = new ();
- Assert.False (top.IsLoaded);
- Application.Begin (top);
- Assert.True (top.IsLoaded);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void IsLoaded_With_Sub_Toplevel_Application_Begin_NeedDisplay ()
- {
- Toplevel top = new ();
- var subTop = new Toplevel ();
- var view = new View { Frame = new (0, 0, 20, 10) };
- subTop.Add (view);
- top.Add (subTop);
- Assert.False (top.IsLoaded);
- Assert.False (subTop.IsLoaded);
- Assert.Equal (new (0, 0, 20, 10), view.Frame);
- view.SubViewLayout += ViewLayoutStarted;
- void ViewLayoutStarted (object sender, LayoutEventArgs e)
- {
- Assert.Equal (new (0, 0, 20, 10), view._needsDrawRect);
- view.SubViewLayout -= ViewLayoutStarted;
- }
- Application.Begin (top);
- Assert.True (top.IsLoaded);
- Assert.True (subTop.IsLoaded);
- Assert.Equal (new (0, 0, 20, 10), view.Frame);
- view.Frame = new (1, 3, 10, 5);
- Assert.Equal (new (1, 3, 10, 5), view.Frame);
- Assert.Equal (new (0, 0, 10, 5), view._needsDrawRect);
- view.Frame = new (1, 3, 10, 5);
- top.Layout ();
- Assert.Equal (new (1, 3, 10, 5), view.Frame);
- Assert.Equal (new (0, 0, 10, 5), view._needsDrawRect);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Window_Viewport_Bigger_Than_Driver_Cols_And_Rows_Allow_Drag_Beyond_Left_Right_And_Bottom ()
- {
- Toplevel top = new ();
- var window = new Window { Width = 20, Height = 3, Arrangement = ViewArrangement.Movable };
- RunState rsTop = Application.Begin (top);
- ((FakeDriver)Application.Driver!).SetBufferSize (40, 10);
- RunState rsWindow = Application.Begin (window);
- Application.LayoutAndDraw ();
- Assert.Equal (new (0, 0, 40, 10), top.Frame);
- Assert.Equal (new (0, 0, 20, 3), window.Frame);
- Assert.Null (Application.MouseGrabView);
- Application.RaiseMouseEvent (new () { ScreenPosition = new (0, 0), Flags = MouseFlags.Button1Pressed });
- Assert.Equal (window.Border, Application.MouseGrabView);
- Application.RaiseMouseEvent (
- new ()
- {
- ScreenPosition = new (-11, -4), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
- });
- Application.LayoutAndDraw ();
- Assert.Equal (new (0, 0, 40, 10), top.Frame);
- Assert.Equal (new (-11, -4, 20, 3), window.Frame);
- // Changes Top size to same size as Dialog more menu and scroll bar
- ((FakeDriver)Application.Driver!).SetBufferSize (20, 3);
- Application.RaiseMouseEvent (
- new ()
- {
- ScreenPosition = new (-1, -1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
- });
- Application.LayoutAndDraw ();
- Assert.Equal (new (0, 0, 20, 3), top.Frame);
- Assert.Equal (new (-1, -1, 20, 3), window.Frame);
- // Changes Top size smaller than Dialog size
- ((FakeDriver)Application.Driver!).SetBufferSize (19, 2);
- Application.RaiseMouseEvent (
- new ()
- {
- ScreenPosition = new (-1, -1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
- });
- Application.LayoutAndDraw ();
- Assert.Equal (new (0, 0, 19, 2), top.Frame);
- Assert.Equal (new (-1, -1, 20, 3), window.Frame);
- Application.RaiseMouseEvent (
- new ()
- {
- ScreenPosition = new (18, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
- });
- Application.LayoutAndDraw ();
- Assert.Equal (new (0, 0, 19, 2), top.Frame);
- Assert.Equal (new (18, 1, 20, 3), window.Frame);
- // On a real app we can't go beyond the SuperView bounds
- Application.RaiseMouseEvent (
- new ()
- {
- ScreenPosition = new (19, 2), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
- });
- Application.LayoutAndDraw ();
- Assert.Equal (new (0, 0, 19, 2), top.Frame);
- Assert.Equal (new (19, 2, 20, 3), window.Frame);
- //DriverAsserts.AssertDriverContentsWithFrameAre (@"", output);
- Application.End (rsWindow);
- Application.End (rsTop);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Modal_As_Top_Will_Drag_Cleanly ()
- {
- // Don't use Dialog as a Top, use a Window instead - dialog has complex layout behavior that is not needed here.
- var window = new Window { Width = 10, Height = 3, Arrangement = ViewArrangement.Movable };
- window.Add (
- new Label
- {
- X = Pos.Center (),
- Y = Pos.Center (),
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- TextAlignment = Alignment.Center,
- VerticalTextAlignment = Alignment.Center,
- Text = "Test"
- }
- );
- RunState rs = Application.Begin (window);
- Assert.Null (Application.MouseGrabView);
- Assert.Equal (new (0, 0, 10, 3), window.Frame);
- Application.RaiseMouseEvent (new () { ScreenPosition = new (0, 0), Flags = MouseFlags.Button1Pressed });
- var firstIteration = false;
- Application.RunIteration (ref rs, firstIteration);
- Assert.Equal (window.Border, Application.MouseGrabView);
- Assert.Equal (new (0, 0, 10, 3), window.Frame);
- Application.RaiseMouseEvent (
- new ()
- {
- ScreenPosition = new (1, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
- });
- firstIteration = false;
- Application.RunIteration (ref rs, firstIteration);
- Assert.Equal (window.Border, Application.MouseGrabView);
- Assert.Equal (new (1, 1, 10, 3), window.Frame);
- Application.End (rs);
- window.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Begin_With_Window_Sets_Size_Correctly ()
- {
- 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 (2, 1, 15, 10), testWindow.Frame);
- RunState rsTestWindow = Application.Begin (testWindow);
- Assert.Equal (new (2, 1, 15, 10), testWindow.Frame);
- Application.End (rsTestWindow);
- Application.End (rsTop);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Activating_MenuBar_By_Alt_Key_Does_Not_Throw ()
- {
- var menu = new MenuBar
- {
- Menus =
- [
- new ("Child", new MenuItem [] { new ("_Create Child", "", null) })
- ]
- };
- var topChild = new Toplevel ();
- topChild.Add (menu);
- var top = new Toplevel ();
- top.Add (topChild);
- Application.Begin (top);
- Exception exception = Record.Exception (() => topChild.NewKeyDownEvent (KeyCode.AltMask));
- Assert.Null (exception);
- top.Dispose ();
- }
- [Fact]
- [TestRespondersDisposed]
- public void Multi_Thread_Toplevels ()
- {
- Application.Init (new FakeDriver ());
- Toplevel t = new ();
- var w = new Window ();
- t.Add (w);
- int count = 0, count1 = 0, count2 = 0;
- bool log = false, log1 = false, log2 = false;
- var fromTopStillKnowFirstIsRunning = false;
- var fromTopStillKnowSecondIsRunning = false;
- var fromFirstStillKnowSecondIsRunning = false;
- Application.AddTimeout (
- TimeSpan.FromMilliseconds (100),
- () =>
- {
- count++;
- if (count1 == 5)
- {
- log1 = true;
- }
- if (count1 == 14 && count2 == 10 && count == 15)
- {
- // count2 is already stopped
- fromTopStillKnowFirstIsRunning = true;
- }
- if (count1 == 7 && count2 == 7 && count == 8)
- {
- fromTopStillKnowSecondIsRunning = true;
- }
- if (count == 30)
- {
- Assert.Equal (30, count);
- Assert.Equal (20, count1);
- Assert.Equal (10, count2);
- Assert.True (log);
- Assert.True (log1);
- Assert.True (log2);
- Assert.True (fromTopStillKnowFirstIsRunning);
- Assert.True (fromTopStillKnowSecondIsRunning);
- Assert.True (fromFirstStillKnowSecondIsRunning);
- Application.RequestStop ();
- return false;
- }
- return true;
- }
- );
- t.Ready += FirstWindow;
- void FirstWindow (object sender, EventArgs args)
- {
- var firstWindow = new Window ();
- firstWindow.Ready += SecondWindow;
- Application.AddTimeout (
- TimeSpan.FromMilliseconds (100),
- () =>
- {
- count1++;
- if (count2 == 5)
- {
- log2 = true;
- }
- if (count2 == 4 && count1 == 5 && count == 5)
- {
- fromFirstStillKnowSecondIsRunning = true;
- }
- if (count1 == 20)
- {
- Assert.Equal (20, count1);
- Application.RequestStop ();
- return false;
- }
- return true;
- }
- );
- Application.Run (firstWindow);
- firstWindow.Dispose ();
- }
- void SecondWindow (object sender, EventArgs args)
- {
- var testWindow = new Window ();
- Application.AddTimeout (
- TimeSpan.FromMilliseconds (100),
- () =>
- {
- count2++;
- if (count < 30)
- {
- log = true;
- }
- if (count2 == 10)
- {
- Assert.Equal (10, count2);
- Application.RequestStop ();
- return false;
- }
- return true;
- }
- );
- Application.Run (testWindow);
- testWindow.Dispose ();
- }
- Application.Run (t);
- t.Dispose ();
- Application.Shutdown ();
- }
- [Fact]
- public void Remove_Do_Not_Dispose_MenuBar_Or_StatusBar ()
- {
- var mb = new MenuBar ();
- var sb = new StatusBar ();
- var tl = new Toplevel ();
- #if DEBUG
- Assert.False (mb.WasDisposed);
- Assert.False (sb.WasDisposed);
- #endif
- tl.Add (mb, sb);
- Assert.NotNull (tl.MenuBar);
- //Assert.NotNull (tl.StatusBar);
- #if DEBUG
- Assert.False (mb.WasDisposed);
- Assert.False (sb.WasDisposed);
- #endif
- tl.RemoveAll ();
- Assert.Null (tl.MenuBar);
- //Assert.Null (tl.StatusBar);
- #if DEBUG
- Assert.False (mb.WasDisposed);
- Assert.False (sb.WasDisposed);
- #endif
- }
- }
|