12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499 |
- using System;
- using Terminal.Gui;
- using Xunit;
- using Xunit.Abstractions;
- namespace Terminal.Gui.ViewsTests {
- public class ToplevelTests {
- readonly ITestOutputHelper output;
- public ToplevelTests (ITestOutputHelper output)
- {
- this.output = output;
- }
- [Fact]
- [AutoInitShutdown]
- public void Constructor_Default ()
- {
- var top = new Toplevel ();
- Assert.Equal (Colors.TopLevel, top.ColorScheme);
- Assert.Equal ("Fill(0)", top.Width.ToString ());
- Assert.Equal ("Fill(0)", top.Height.ToString ());
- Assert.False (top.Running);
- Assert.False (top.Modal);
- Assert.Null (top.MenuBar);
- Assert.Null (top.StatusBar);
- Assert.False (top.IsMdiContainer);
- Assert.False (top.IsMdiChild);
- }
- [Fact]
- [AutoInitShutdown]
- public void Create_Toplevel ()
- {
- var top = Toplevel.Create ();
- top.BeginInit ();
- top.EndInit ();
- Assert.Equal (new Rect (0, 0, Application.Driver.Cols, Application.Driver.Rows), top.Bounds);
- }
- [Fact]
- [AutoInitShutdown]
- public void Application_Top_GetLocationThatFits_To_Driver_Rows_And_Cols ()
- {
- var iterations = 0;
- Application.Iteration += () => {
- if (iterations == 0) {
- Assert.False (Application.Top.AutoSize);
- Assert.Equal ("Top1", Application.Top.Text);
- Assert.Equal (0, Application.Top.Frame.X);
- Assert.Equal (0, Application.Top.Frame.Y);
- Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
- Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
- Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.R, new KeyModifiers ()));
- } else if (iterations == 1) {
- Assert.Equal ("Top2", Application.Top.Text);
- Assert.Equal (0, Application.Top.Frame.X);
- Assert.Equal (0, Application.Top.Frame.Y);
- Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
- Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
- Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.C, new KeyModifiers ()));
- } else if (iterations == 3) {
- Assert.Equal ("Top1", Application.Top.Text);
- Assert.Equal (0, Application.Top.Frame.X);
- Assert.Equal (0, Application.Top.Frame.Y);
- Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
- Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
- Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.R, new KeyModifiers ()));
- } else if (iterations == 4) {
- Assert.Equal ("Top2", Application.Top.Text);
- Assert.Equal (0, Application.Top.Frame.X);
- Assert.Equal (0, Application.Top.Frame.Y);
- Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
- Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
- Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.C, new KeyModifiers ()));
- } else if (iterations == 6) {
- Assert.Equal ("Top1", Application.Top.Text);
- Assert.Equal (0, Application.Top.Frame.X);
- Assert.Equal (0, Application.Top.Frame.Y);
- Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
- Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
- Application.Top.ProcessHotKey (new KeyEvent (Key.CtrlMask | Key.Q, new KeyModifiers ()));
- }
- iterations++;
- };
- Application.Run (Top1 ());
- Toplevel Top1 ()
- {
- var top = Application.Top;
- top.Text = "Top1";
- var menu = new MenuBar (new MenuBarItem [] {
- new MenuBarItem ("_Options", new MenuItem [] {
- new MenuItem ("_Run Top2", "", () => Application.Run (Top2 ()), null, null, Key.CtrlMask | Key.R),
- new MenuItem ("_Quit", "", () => Application.RequestStop(), null, null, Key.CtrlMask | Key.Q)
- })
- });
- top.Add (menu);
- var statusBar = new StatusBar (new [] {
- new StatusItem(Key.CtrlMask | Key.R, "~^R~ Run Top2", () => Application.Run (Top2 ())),
- new StatusItem(Application.QuitKey, $"{Application.QuitKey} to Quit", () => Application.RequestStop())
- });
- top.Add (statusBar);
- var t1 = new Toplevel ();
- top.Add (t1);
- return top;
- }
- Toplevel Top2 ()
- {
- var top = new Toplevel (Application.Top.Frame);
- top.Text = "Top2";
- var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
- var menu = new MenuBar (new MenuBarItem [] {
- new MenuBarItem ("_Stage", new MenuItem [] {
- new MenuItem ("_Close", "", () => Application.RequestStop(), null, null, Key.CtrlMask | Key.C)
- })
- });
- top.Add (menu);
- var statusBar = new StatusBar (new [] {
- new StatusItem(Key.CtrlMask | Key.C, "~^C~ Close", () => Application.RequestStop()),
- });
- top.Add (statusBar);
- win.Add (new ListView () {
- X = 0,
- Y = 0,
- Width = Dim.Fill (),
- Height = Dim.Fill ()
- });
- top.Add (win);
- return top;
- }
- }
- [Fact]
- [AutoInitShutdown]
- public void Internal_Tests ()
- {
- var top = new Toplevel ();
- var eventInvoked = "";
- top.ChildUnloaded += (s, e) => eventInvoked = "ChildUnloaded";
- top.OnChildUnloaded (top);
- Assert.Equal ("ChildUnloaded", eventInvoked);
- top.ChildLoaded += (s, e) => eventInvoked = "ChildLoaded";
- top.OnChildLoaded (top);
- Assert.Equal ("ChildLoaded", eventInvoked);
- top.Closed += (s, e) => eventInvoked = "Closed";
- top.OnClosed (top);
- Assert.Equal ("Closed", eventInvoked);
- top.Closing += (s, e) => eventInvoked = "Closing";
- top.OnClosing (new ToplevelClosingEventArgs (top));
- Assert.Equal ("Closing", eventInvoked);
- top.AllChildClosed += (s, e) => eventInvoked = "AllChildClosed";
- top.OnAllChildClosed ();
- Assert.Equal ("AllChildClosed", eventInvoked);
- top.ChildClosed += (s, e) => eventInvoked = "ChildClosed";
- top.OnChildClosed (top);
- Assert.Equal ("ChildClosed", eventInvoked);
- top.Deactivate += (s, e) => eventInvoked = "Deactivate";
- top.OnDeactivate (top);
- Assert.Equal ("Deactivate", eventInvoked);
- top.Activate += (s, e) => eventInvoked = "Activate";
- top.OnActivate (top);
- Assert.Equal ("Activate", 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.AddMenuStatusBar (new MenuBar ());
- Assert.NotNull (top.MenuBar);
- top.AddMenuStatusBar (new StatusBar ());
- Assert.NotNull (top.StatusBar);
- top.RemoveMenuStatusBar (top.MenuBar);
- Assert.Null (top.MenuBar);
- top.RemoveMenuStatusBar (top.StatusBar);
- Assert.Null (top.StatusBar);
- Application.Begin (top);
- Assert.Equal (top, Application.Top);
- // Application.Top without menu and status bar.
- var supView = top.GetLocationThatFits (top, 2, 2, out int nx, out int ny, out MenuBar mb, out StatusBar sb);
- Assert.Equal (Application.Top, supView);
- Assert.Equal (0, nx);
- Assert.Equal (0, ny);
- Assert.Null (mb);
- Assert.Null (sb);
- top.AddMenuStatusBar (new MenuBar ());
- Assert.NotNull (top.MenuBar);
- // Application.Top with a menu and without status bar.
- top.GetLocationThatFits (top, 2, 2, out nx, out ny, out mb, out sb);
- Assert.Equal (0, nx);
- Assert.Equal (1, ny);
- Assert.NotNull (mb);
- Assert.Null (sb);
- top.AddMenuStatusBar (new StatusBar ());
- Assert.NotNull (top.StatusBar);
- // Application.Top with a menu and status bar.
- top.GetLocationThatFits (top, 2, 2, out nx, out ny, out mb, 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 (mb);
- Assert.NotNull (sb);
- top.RemoveMenuStatusBar (top.MenuBar);
- Assert.Null (top.MenuBar);
- // Application.Top without a menu and with a status bar.
- top.GetLocationThatFits (top, 2, 2, out nx, out ny, out mb, 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.Null (mb);
- Assert.NotNull (sb);
- top.RemoveMenuStatusBar (top.StatusBar);
- Assert.Null (top.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 = top.GetLocationThatFits (win, 0, 0, out nx, out ny, out mb, out sb);
- Assert.Equal (Application.Top, supView);
- supView = win.GetLocationThatFits (win, 0, 0, out nx, out ny, out mb, out sb);
- Assert.Equal (Application.Top, supView);
- // Application.Top without menu and status bar.
- top.GetLocationThatFits (win, 0, 0, out nx, out ny, out mb, out sb);
- Assert.Equal (0, nx);
- Assert.Equal (0, ny);
- Assert.Null (mb);
- Assert.Null (sb);
- top.AddMenuStatusBar (new MenuBar ());
- Assert.NotNull (top.MenuBar);
- // Application.Top with a menu and without status bar.
- top.GetLocationThatFits (win, 2, 2, out nx, out ny, out mb, out sb);
- Assert.Equal (0, nx);
- Assert.Equal (1, ny);
- Assert.NotNull (mb);
- Assert.Null (sb);
- top.AddMenuStatusBar (new StatusBar ());
- Assert.NotNull (top.StatusBar);
- // Application.Top with a menu and status bar.
- top.GetLocationThatFits (win, 30, 20, out nx, out ny, out mb, 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 (mb);
- Assert.NotNull (sb);
- top.RemoveMenuStatusBar (top.MenuBar);
- top.RemoveMenuStatusBar (top.StatusBar);
- Assert.Null (top.StatusBar);
- Assert.Null (top.MenuBar);
- top.Remove (win);
- win = new Window () { Width = 60, Height = 15 };
- top.Add (win);
- // Application.Top without menu and status bar.
- top.GetLocationThatFits (win, 0, 0, out nx, out ny, out mb, out sb);
- Assert.Equal (0, nx);
- Assert.Equal (0, ny);
- Assert.Null (mb);
- Assert.Null (sb);
- top.AddMenuStatusBar (new MenuBar ());
- Assert.NotNull (top.MenuBar);
- // Application.Top with a menu and without status bar.
- top.GetLocationThatFits (win, 2, 2, out nx, out ny, out mb, out sb);
- Assert.Equal (2, nx);
- Assert.Equal (2, ny);
- Assert.NotNull (mb);
- Assert.Null (sb);
- top.AddMenuStatusBar (new StatusBar ());
- Assert.NotNull (top.StatusBar);
- // Application.Top with a menu and status bar.
- top.GetLocationThatFits (win, 30, 20, out nx, out ny, out mb, out sb);
- Assert.Equal (20, nx); // 20+60=80
- Assert.Equal (9, ny); // 9+15+1(mb)=25
- Assert.NotNull (mb);
- Assert.NotNull (sb);
- top.PositionToplevels ();
- Assert.Equal (new Rect (0, 1, 60, 15), win.Frame);
- Assert.Null (Toplevel.dragPosition);
- win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Pressed });
- Assert.Equal (new Point (6, 0), Toplevel.dragPosition);
- win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Released });
- Assert.Null (Toplevel.dragPosition);
- win.CanFocus = false;
- win.MouseEvent (new MouseEvent () { X = 6, Y = 0, Flags = MouseFlags.Button1Pressed });
- Assert.Null (Toplevel.dragPosition);
- }
- [Fact]
- [AutoInitShutdown]
- public void KeyBindings_Command ()
- {
- var isRunning = false;
- var win1 = new Window () { Id = "win1", Width = Dim.Percent (50f), Height = Dim.Fill () };
- var lblTf1W1 = new Label ("Enter text in TextField on Win1:") { Id = "lblTf1W1" };
- var tf1W1 = new TextField ("Text1 on Win1") { Id = "tf1W1", X = Pos.Right (lblTf1W1) + 1, Width = Dim.Fill () };
- var lblTvW1 = new Label ("Enter text in TextView on Win1:") { Id = "lblTvW1", Y = Pos.Bottom (lblTf1W1) + 1 };
- var tvW1 = new TextView () { Id = "tvW1", X = Pos.Left (tf1W1), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win1" };
- var lblTf2W1 = new Label ("Enter text in TextField on Win1:") { Id = "lblTf2W1", Y = Pos.Bottom (lblTvW1) + 1 };
- var tf2W1 = new TextField ("Text2 on Win1") { Id = "tf2W1", X = Pos.Left (tf1W1), Width = Dim.Fill () };
- win1.Add (lblTf1W1, tf1W1, lblTvW1, tvW1, lblTf2W1, tf2W1);
- var win2 = new Window () { Id = "win2", X = Pos.Right (win1) + 1, Width = Dim.Percent (50f), Height = Dim.Fill () };
- var lblTf1W2 = new Label ("Enter text in TextField on Win2:") { Id = "lblTf1W2" };
- var tf1W2 = new TextField ("Text1 on Win2") { Id = "tf1W2", X = Pos.Right (lblTf1W2) + 1, Width = Dim.Fill () };
- var lblTvW2 = new Label ("Enter text in TextView on Win2:") { Id = "lblTvW2", Y = Pos.Bottom (lblTf1W2) + 1 };
- var tvW2 = new TextView () { Id = "tvW2", X = Pos.Left (tf1W2), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win2" };
- var lblTf2W2 = new Label ("Enter text in TextField on Win2:") { Id = "lblTf2W2", Y = Pos.Bottom (lblTvW2) + 1 };
- var tf2W2 = new TextField ("Text2 on Win2") { Id = "tf2W2", X = Pos.Left (tf1W2), Width = Dim.Fill () };
- win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
- var top = Application.Top;
- top.Add (win1, win2);
- top.Loaded += (s, e) => isRunning = true;
- top.Closing += (s, e) => isRunning = false;
- Application.Begin (top);
- top.Running = true;
- Assert.Equal (new Rect (0, 0, 40, 25), win1.Frame);
- Assert.Equal (new Rect (41, 0, 40, 25), win2.Frame);
- Assert.Equal (win1, top.Focused);
- Assert.Equal (tf1W1, top.MostFocused);
- Assert.True (isRunning);
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.QuitKey, new KeyModifiers ())));
- Assert.False (isRunning);
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Z | Key.CtrlMask, new KeyModifiers ())));
- Assert.False (top.Focused.ProcessKey (new KeyEvent (Key.F5, new KeyModifiers ())));
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
- Assert.Equal (win1, top.Focused);
- Assert.Equal (tvW1, top.MostFocused);
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
- Assert.Equal ($"\tFirst line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
- Assert.Equal ($"First line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
- Assert.Equal (win1, top.Focused);
- Assert.Equal (tf2W1, top.MostFocused);
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
- Assert.Equal (win1, top.Focused);
- Assert.Equal (tf1W1, top.MostFocused);
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
- Assert.Equal (win1, top.Focused);
- Assert.Equal (tf1W1, top.MostFocused);
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
- Assert.Equal (win1, top.Focused);
- Assert.Equal (tvW1, top.MostFocused);
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.I | Key.CtrlMask, new KeyModifiers ())));
- Assert.Equal (win1, top.Focused);
- Assert.Equal (tf2W1, top.MostFocused);
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
- Assert.Equal (win1, top.Focused);
- Assert.Equal (tvW1, top.MostFocused);
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
- Assert.Equal (win1, top.Focused);
- Assert.Equal (tf1W1, top.MostFocused);
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
- Assert.Equal (win1, top.Focused);
- Assert.Equal (tf2W1, top.MostFocused);
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
- Assert.Equal (win2, top.Focused);
- Assert.Equal (tf1W2, top.MostFocused);
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ())));
- Assert.Equal (win1, top.Focused);
- Assert.Equal (tf2W1, top.MostFocused);
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.AlternateForwardKey, new KeyModifiers ())));
- Assert.Equal (win2, top.Focused);
- Assert.Equal (tf1W2, top.MostFocused);
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Application.AlternateBackwardKey, new KeyModifiers ())));
- Assert.Equal (win1, top.Focused);
- Assert.Equal (tf2W1, top.MostFocused);
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
- Assert.Equal (win1, top.Focused);
- Assert.Equal (tvW1, top.MostFocused);
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.B | Key.CtrlMask, new KeyModifiers ())));
- Assert.Equal (win1, top.Focused);
- Assert.Equal (tf1W1, top.MostFocused);
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
- Assert.Equal (win1, top.Focused);
- Assert.Equal (tvW1, top.MostFocused);
- Assert.Equal (new Point (0, 0), tvW1.CursorPosition);
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
- Assert.Equal (win1, top.Focused);
- Assert.Equal (tvW1, top.MostFocused);
- Assert.Equal (new Point (16, 1), tvW1.CursorPosition);
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.F | Key.CtrlMask, new KeyModifiers ())));
- Assert.Equal (win1, top.Focused);
- Assert.Equal (tf2W1, top.MostFocused);
- Assert.True (top.Focused.ProcessKey (new KeyEvent (Key.L | Key.CtrlMask, new KeyModifiers ())));
- }
- [Fact]
- [AutoInitShutdown]
- public void KeyBindings_Command_With_MdiTop ()
- {
- var top = Application.Top;
- Assert.Null (Application.MdiTop);
- top.IsMdiContainer = true;
- Application.Begin (top);
- Assert.Equal (Application.Top, Application.MdiTop);
- var isRunning = true;
- var win1 = new Window () { Id = "win1", Width = Dim.Percent (50f), Height = Dim.Fill () };
- var lblTf1W1 = new Label ("Enter text in TextField on Win1:");
- var tf1W1 = new TextField ("Text1 on Win1") { X = Pos.Right (lblTf1W1) + 1, Width = Dim.Fill () };
- var lblTvW1 = new Label ("Enter text in TextView on Win1:") { Y = Pos.Bottom (lblTf1W1) + 1 };
- var tvW1 = new TextView () { X = Pos.Left (tf1W1), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win1" };
- var lblTf2W1 = new Label ("Enter text in TextField on Win1:") { Y = Pos.Bottom (lblTvW1) + 1 };
- var tf2W1 = new TextField ("Text2 on Win1") { X = Pos.Left (tf1W1), Width = Dim.Fill () };
- win1.Add (lblTf1W1, tf1W1, lblTvW1, tvW1, lblTf2W1, tf2W1);
- var win2 = new Window () { Id = "win2", Width = Dim.Percent (50f), Height = Dim.Fill () };
- var lblTf1W2 = new Label ("Enter text in TextField on Win2:");
- var tf1W2 = new TextField ("Text1 on Win2") { X = Pos.Right (lblTf1W2) + 1, Width = Dim.Fill () };
- var lblTvW2 = new Label ("Enter text in TextView on Win2:") { Y = Pos.Bottom (lblTf1W2) + 1 };
- var tvW2 = new TextView () { X = Pos.Left (tf1W2), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win2" };
- var lblTf2W2 = new Label ("Enter text in TextField on Win2:") { Y = Pos.Bottom (lblTvW2) + 1 };
- var tf2W2 = new TextField ("Text2 on Win2") { X = Pos.Left (tf1W2), Width = Dim.Fill () };
- win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
- win1.Closing += (s, e) => isRunning = false;
- Assert.Null (top.Focused);
- Assert.Equal (top, Application.Current);
- Assert.True (top.IsCurrentTop);
- Assert.Equal (top, Application.MdiTop);
- Application.Begin (win1);
- Assert.Equal (new Rect (0, 0, 40, 25), win1.Frame);
- Assert.NotEqual (top, Application.Current);
- Assert.False (top.IsCurrentTop);
- Assert.Equal (win1, Application.Current);
- Assert.True (win1.IsCurrentTop);
- Assert.True (win1.IsMdiChild);
- Assert.Null (top.Focused);
- Assert.Null (top.MostFocused);
- Assert.Equal (tf1W1, win1.MostFocused);
- Assert.True (win1.IsMdiChild);
- Assert.Single (Application.MdiChildren);
- Application.Begin (win2);
- Assert.Equal (new Rect (0, 0, 40, 25), win2.Frame);
- Assert.NotEqual (top, Application.Current);
- Assert.False (top.IsCurrentTop);
- Assert.Equal (win2, Application.Current);
- Assert.True (win2.IsCurrentTop);
- Assert.True (win2.IsMdiChild);
- Assert.Null (top.Focused);
- Assert.Null (top.MostFocused);
- Assert.Equal (tf1W2, win2.MostFocused);
- Assert.Equal (2, Application.MdiChildren.Count);
- Application.ShowChild (win1);
- Assert.Equal (win1, Application.Current);
- Assert.Equal (win1, Application.MdiChildren [0]);
- win1.Running = true;
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Application.QuitKey, new KeyModifiers ())));
- Assert.False (isRunning);
- Assert.False (win1.Running);
- Assert.Equal (win1, Application.MdiChildren [0]);
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Key.Z | Key.CtrlMask, new KeyModifiers ())));
- Assert.False (Application.MdiChildren [0].ProcessKey (new KeyEvent (Key.F5, new KeyModifiers ())));
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
- Assert.True (win1.IsCurrentTop);
- Assert.Equal (tvW1, win1.MostFocused);
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
- Assert.Equal ($"\tFirst line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
- Assert.Equal ($"First line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
- Assert.Equal (win1, Application.MdiChildren [0]);
- Assert.Equal (tf2W1, win1.MostFocused);
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
- Assert.Equal (win1, Application.MdiChildren [0]);
- Assert.Equal (tf1W1, win1.MostFocused);
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
- Assert.Equal (win1, Application.MdiChildren [0]);
- Assert.Equal (tf1W1, win1.MostFocused);
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
- Assert.Equal (win1, Application.MdiChildren [0]);
- Assert.Equal (tvW1, win1.MostFocused);
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Key.I | Key.CtrlMask, new KeyModifiers ())));
- Assert.Equal (win1, Application.MdiChildren [0]);
- Assert.Equal (tf2W1, win1.MostFocused);
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ())));
- Assert.Equal (win1, Application.MdiChildren [0]);
- Assert.Equal (tvW1, win1.MostFocused);
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
- Assert.Equal (win1, Application.MdiChildren [0]);
- Assert.Equal (tf1W1, win1.MostFocused);
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
- Assert.Equal (win1, Application.MdiChildren [0]);
- Assert.Equal (tf2W1, win1.MostFocused);
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
- Assert.Equal (win1, Application.MdiChildren [0]);
- Assert.Equal (tf1W1, win1.MostFocused);
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
- Assert.Equal (win2, Application.MdiChildren [0]);
- Assert.Equal (tf1W2, win2.MostFocused);
- tf2W2.SetFocus ();
- Assert.True (tf2W2.HasFocus);
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ())));
- Assert.Equal (win1, Application.MdiChildren [0]);
- Assert.Equal (tf1W1, win1.MostFocused);
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Application.AlternateForwardKey, new KeyModifiers ())));
- Assert.Equal (win2, Application.MdiChildren [0]);
- Assert.Equal (tf2W2, win2.MostFocused);
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Application.AlternateBackwardKey, new KeyModifiers ())));
- Assert.Equal (win1, Application.MdiChildren [0]);
- Assert.Equal (tf1W1, win1.MostFocused);
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
- Assert.Equal (win1, Application.MdiChildren [0]);
- Assert.Equal (tvW1, win1.MostFocused);
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Key.B | Key.CtrlMask, new KeyModifiers ())));
- Assert.Equal (win1, Application.MdiChildren [0]);
- Assert.Equal (tf1W1, win1.MostFocused);
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
- Assert.Equal (win1, Application.MdiChildren [0]);
- Assert.Equal (tvW1, win1.MostFocused);
- Assert.Equal (new Point (0, 0), tvW1.CursorPosition);
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
- Assert.Equal (win1, Application.MdiChildren [0]);
- Assert.Equal (tvW1, win1.MostFocused);
- Assert.Equal (new Point (16, 1), tvW1.CursorPosition);
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Key.F | Key.CtrlMask, new KeyModifiers ())));
- Assert.Equal (win1, Application.MdiChildren [0]);
- Assert.Equal (tf2W1, win1.MostFocused);
- Assert.True (Application.MdiChildren [0].ProcessKey (new KeyEvent (Key.L | Key.CtrlMask, new KeyModifiers ())));
- }
- [Fact]
- public void Added_Event_Should_Not_Be_Used_To_Initialize_Toplevel_Events ()
- {
- Key alternateForwardKey = default;
- Key alternateBackwardKey = default;
- Key quitKey = default;
- var wasAdded = false;
- var view = new View ();
- view.Added += View_Added;
- void View_Added (object sender, SuperViewChangedEventArgs e)
- {
- Assert.Throws<NullReferenceException> (() => Application.Top.AlternateForwardKeyChanged += (s, e) => alternateForwardKey = e.OldKey);
- Assert.Throws<NullReferenceException> (() => Application.Top.AlternateBackwardKeyChanged += (s, e) => alternateBackwardKey = e.OldKey);
- Assert.Throws<NullReferenceException> (() => Application.Top.QuitKeyChanged += (s, e) => quitKey = e.OldKey);
- Assert.False (wasAdded);
- wasAdded = true;
- view.Added -= View_Added;
- }
- var win = new Window ();
- win.Add (view);
- Application.Init (new FakeDriver ());
- var top = Application.Top;
- top.Add (win);
- Assert.True (wasAdded);
- Application.Shutdown ();
- }
- [Fact]
- [AutoInitShutdown]
- public void AlternateForwardKeyChanged_AlternateBackwardKeyChanged_QuitKeyChanged_Events ()
- {
- Key alternateForwardKey = default;
- Key alternateBackwardKey = default;
- Key quitKey = default;
- 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;
- }
- var win = new Window ();
- win.Add (view);
- var top = Application.Top;
- top.Add (win);
- Application.Begin (top);
- Assert.Equal (Key.Null, alternateForwardKey);
- Assert.Equal (Key.Null, alternateBackwardKey);
- Assert.Equal (Key.Null, quitKey);
- Assert.Equal (Key.PageDown | Key.CtrlMask, Application.AlternateForwardKey);
- Assert.Equal (Key.PageUp | Key.CtrlMask, Application.AlternateBackwardKey);
- Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
- Application.AlternateForwardKey = Key.A;
- Application.AlternateBackwardKey = Key.B;
- Application.QuitKey = Key.C;
- Assert.Equal (Key.PageDown | Key.CtrlMask, alternateForwardKey);
- Assert.Equal (Key.PageUp | Key.CtrlMask, alternateBackwardKey);
- Assert.Equal (Key.Q | Key.CtrlMask, quitKey);
- Assert.Equal (Key.A, Application.AlternateForwardKey);
- Assert.Equal (Key.B, Application.AlternateBackwardKey);
- Assert.Equal (Key.C, Application.QuitKey);
- // Replacing the defaults keys to avoid errors on others unit tests that are using it.
- Application.AlternateForwardKey = Key.PageDown | Key.CtrlMask;
- Application.AlternateBackwardKey = Key.PageUp | Key.CtrlMask;
- Application.QuitKey = Key.Q | Key.CtrlMask;
- Assert.Equal (Key.PageDown | Key.CtrlMask, Application.AlternateForwardKey);
- Assert.Equal (Key.PageUp | Key.CtrlMask, Application.AlternateBackwardKey);
- Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
- }
- [Fact, AutoInitShutdown]
- public void Mouse_Drag_On_Top_With_Superview_Null ()
- {
- var win = new Window ();
- var top = Application.Top;
- top.Add (win);
- var iterations = -1;
- Application.Iteration = () => {
- iterations++;
- if (iterations == 0) {
- ((FakeDriver)Application.Driver).SetBufferSize (40, 15);
- MessageBox.Query ("", "Hello Word", "Ok");
- } else if (iterations == 1) {
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌──────────────────────────────────────┐
- │ │
- │ │
- │ │
- │ │
- │ ┌──────────────────────┐ │
- │ │ Hello Word │ │
- │ │ │ │
- │ │ [◦ Ok ◦] │ │
- │ └──────────────────────┘ │
- │ │
- │ │
- │ │
- │ │
- └──────────────────────────────────────┘
- ", output);
- } else if (iterations == 2) {
- Assert.Null (Application.MouseGrabView);
- // Grab the mouse
- ReflectionTools.InvokePrivate (
- typeof (Application),
- "ProcessMouseEvent",
- new MouseEvent () {
- X = 8,
- Y = 5,
- Flags = MouseFlags.Button1Pressed
- });
- Assert.Equal (Application.Current, Application.MouseGrabView);
- Assert.Equal (new Rect (8, 5, 24, 5), Application.MouseGrabView.Frame);
- } else if (iterations == 3) {
- Assert.Equal (Application.Current, Application.MouseGrabView);
- // Drag to left
- ReflectionTools.InvokePrivate (
- typeof (Application),
- "ProcessMouseEvent",
- new MouseEvent () {
- X = 7,
- Y = 5,
- Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
- });
- Assert.Equal (Application.Current, Application.MouseGrabView);
- Assert.Equal (new Rect (7, 5, 24, 5), Application.MouseGrabView.Frame);
- } else if (iterations == 4) {
- Assert.Equal (Application.Current, Application.MouseGrabView);
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌──────────────────────────────────────┐
- │ │
- │ │
- │ │
- │ │
- │ ┌──────────────────────┐ │
- │ │ Hello Word │ │
- │ │ │ │
- │ │ [◦ Ok ◦] │ │
- │ └──────────────────────┘ │
- │ │
- │ │
- │ │
- │ │
- └──────────────────────────────────────┘", output);
- Assert.Equal (Application.Current, Application.MouseGrabView);
- } else if (iterations == 5) {
- Assert.Equal (Application.Current, Application.MouseGrabView);
- // Drag up
- ReflectionTools.InvokePrivate (
- typeof (Application),
- "ProcessMouseEvent",
- new MouseEvent () {
- X = 7,
- Y = 4,
- Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
- });
- Assert.Equal (Application.Current, Application.MouseGrabView);
- Assert.Equal (new Rect (7, 4, 24, 5), Application.MouseGrabView.Frame);
- } else if (iterations == 6) {
- Assert.Equal (Application.Current, Application.MouseGrabView);
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌──────────────────────────────────────┐
- │ │
- │ │
- │ │
- │ ┌──────────────────────┐ │
- │ │ Hello Word │ │
- │ │ │ │
- │ │ [◦ Ok ◦] │ │
- │ └──────────────────────┘ │
- │ │
- │ │
- │ │
- │ │
- │ │
- └──────────────────────────────────────┘", output);
- Assert.Equal (Application.Current, Application.MouseGrabView);
- Assert.Equal (new Rect (7, 4, 24, 5), Application.MouseGrabView.Frame);
- } else if (iterations == 7) {
- Assert.Equal (Application.Current, Application.MouseGrabView);
- // Ungrab the mouse
- ReflectionTools.InvokePrivate (
- typeof (Application),
- "ProcessMouseEvent",
- new MouseEvent () {
- X = 7,
- Y = 4,
- Flags = MouseFlags.Button1Released
- });
- Assert.Null (Application.MouseGrabView);
- } else if (iterations == 8) Application.RequestStop ();
- else if (iterations == 9) Application.RequestStop ();
- };
- Application.Run ();
- }
- [Fact, AutoInitShutdown]
- public void Mouse_Drag_On_Top_With_Superview_Not_Null ()
- {
- var win = new Window () {
- X = 3,
- Y = 2,
- Width = 10,
- Height = 5
- };
- var top = Application.Top;
- top.Add (win);
- var iterations = -1;
- int movex = 0;
- int movey = 0;
- var location = new Rect (win.Frame.X, win.Frame.Y, 7, 3);
- Application.Iteration = () => {
- iterations++;
- if (iterations == 0) {
- ((FakeDriver)Application.Driver).SetBufferSize (30, 10);
- } else if (iterations == 1) {
- location = win.Frame;
- Assert.Null (Application.MouseGrabView);
- // Grab the mouse
- ReflectionTools.InvokePrivate (
- typeof (Application),
- "ProcessMouseEvent",
- new MouseEvent () {
- X = win.Frame.X,
- Y = win.Frame.Y,
- Flags = MouseFlags.Button1Pressed
- });
- Assert.Equal (win, Application.MouseGrabView);
- Assert.Equal (location, Application.MouseGrabView.Frame);
- } else if (iterations == 2) {
- Assert.Equal (win, Application.MouseGrabView);
- // Drag to left
- movex = 1;
- movey = 0;
- ReflectionTools.InvokePrivate (
- typeof (Application),
- "ProcessMouseEvent",
- new MouseEvent () {
- X = win.Frame.X + movex,
- Y = win.Frame.Y + movey,
- Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
- });
- Assert.Equal (win, Application.MouseGrabView);
- } else if (iterations == 3) {
- // we should have moved +1, +0
- Assert.Equal (win, Application.MouseGrabView);
- Assert.Equal (win, Application.MouseGrabView);
- location.Offset (movex, movey);
- Assert.Equal (location, Application.MouseGrabView.Frame);
- } else if (iterations == 4) {
- Assert.Equal (win, Application.MouseGrabView);
- // Drag up
- movex = 0;
- movey = -1;
- ReflectionTools.InvokePrivate (
- typeof (Application),
- "ProcessMouseEvent",
- new MouseEvent () {
- X = win.Frame.X + movex,
- Y = win.Frame.Y + movey,
- Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
- });
- Assert.Equal (win, Application.MouseGrabView);
- } else if (iterations == 5) {
- // we should have moved +0, -1
- Assert.Equal (win, Application.MouseGrabView);
- location.Offset (movex, movey);
- Assert.Equal (location, Application.MouseGrabView.Frame);
- } else if (iterations == 6) {
- Assert.Equal (win, Application.MouseGrabView);
- // Ungrab the mouse
- movex = 0;
- movey = 0;
- ReflectionTools.InvokePrivate (
- typeof (Application),
- "ProcessMouseEvent",
- new MouseEvent () {
- X = win.Frame.X + movex,
- Y = win.Frame.Y + movey,
- Flags = MouseFlags.Button1Released
- });
- Assert.Null (Application.MouseGrabView);
- } else if (iterations == 7) {
- Application.RequestStop ();
- }
- };
- Application.Run ();
- }
- [Fact, AutoInitShutdown]
- public void GetLocationThatFits_With_Border_Null_Not_Throws ()
- {
- var top = new Toplevel ();
- Application.Begin (top);
- var 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 OnEnter_OnLeave_Triggered_On_Application_Begin_End ()
- {
- var isEnter = false;
- var isLeave = false;
- var v = new View ();
- v.Enter += (s, _) => isEnter = true;
- v.Leave += (s, _) => isLeave = true;
- var top = Application.Top;
- top.Add (v);
- Assert.False (v.CanFocus);
- var exception = Record.Exception (() => top.OnEnter (top));
- Assert.Null (exception);
- exception = Record.Exception (() => top.OnLeave (top));
- Assert.Null (exception);
- v.CanFocus = true;
- Application.Begin (top);
- Assert.True (isEnter);
- Assert.False (isLeave);
- isEnter = false;
- var d = new Dialog ();
- var rs = Application.Begin (d);
- Assert.False (isEnter);
- Assert.True (isLeave);
- isLeave = false;
- Application.End (rs);
- Assert.True (isEnter);
- Assert.False (isLeave);
- }
- [Fact, AutoInitShutdown]
- public void PositionCursor_SetCursorVisibility_To_Invisible_If_Focused_Is_Null ()
- {
- var tf = new TextField ("test") { Width = 5 };
- var view = new View () { Width = 10, Height = 10 };
- view.Add (tf);
- Application.Top.Add (view);
- Application.Begin (Application.Top);
- Assert.True (tf.HasFocus);
- Application.Driver.GetCursorVisibility (out CursorVisibility cursor);
- Assert.Equal (CursorVisibility.Default, cursor);
- view.Enabled = false;
- Assert.False (tf.HasFocus);
- Application.Refresh ();
- Application.Driver.GetCursorVisibility (out cursor);
- Assert.Equal (CursorVisibility.Invisible, cursor);
- }
- [Fact, AutoInitShutdown]
- public void IsLoaded_Application_Begin ()
- {
- var top = Application.Top;
- Assert.False (top.IsLoaded);
- Application.Begin (top);
- Assert.True (top.IsLoaded);
- }
- [Fact, AutoInitShutdown]
- public void IsLoaded_With_Sub_Toplevel_Application_Begin_NeedDisplay ()
- {
- var top = Application.Top;
- var subTop = new Toplevel ();
- var view = new View (new Rect (0, 0, 20, 10));
- subTop.Add (view);
- top.Add (subTop);
- Assert.False (top.IsLoaded);
- Assert.False (subTop.IsLoaded);
- Assert.Equal (new Rect (0, 0, 20, 10), view.Frame);
- view.LayoutStarted += view_LayoutStarted;
- void view_LayoutStarted (object sender, LayoutEventArgs e)
- {
- Assert.Equal (new Rect (0, 0, 20, 10), view._needsDisplay);
- view.LayoutStarted -= view_LayoutStarted;
- }
- Application.Begin (top);
- Assert.True (top.IsLoaded);
- Assert.True (subTop.IsLoaded);
- Assert.Equal (new Rect (0, 0, 20, 10), view.Frame);
- view.Frame = new Rect (1, 3, 10, 5);
- Assert.Equal (new Rect (1, 3, 10, 5), view.Frame);
- Assert.Equal (new Rect (0, 0, 10, 5), view._needsDisplay);
- view.Redraw (view.Bounds);
- view.Frame = new Rect (1, 3, 10, 5);
- Assert.Equal (new Rect (1, 3, 10, 5), view.Frame);
- Assert.Equal (new Rect (0, 0, 10, 5), view._needsDisplay);
- }
- // BUGBUG: Broke this test with #2483 - @bdisp I need your help figuring out why
- //[Fact, AutoInitShutdown]
- //public void Toplevel_Inside_ScrollView_MouseGrabView ()
- //{
- // var scrollView = new ScrollView () {
- // X = 3,
- // Y = 3,
- // Width = 40,
- // Height = 16,
- // ContentSize = new Size (200, 100)
- // };
- // var win = new Window () { X = 3, Y = 3, Width = Dim.Fill (3), Height = Dim.Fill (3) };
- // scrollView.Add (win);
- // var top = Application.Top;
- // top.Add (scrollView);
- // Application.Begin (top);
- // Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
- // Assert.Equal (new Rect (3, 3, 40, 16), scrollView.Frame);
- // Assert.Equal (new Rect (0, 0, 200, 100), scrollView.Subviews [0].Frame);
- // Assert.Equal (new Rect (3, 3, 194, 94), win.Frame);
- // TestHelpers.AssertDriverContentsWithFrameAre (@"
- // ▲
- // ┬
- // │
- // ┌───────────────────────────────────┴
- // │ ░
- // │ ░
- // │ ░
- // │ ░
- // │ ░
- // │ ░
- // │ ░
- // │ ░
- // │ ░
- // │ ░
- // │ ▼
- // ◄├──────┤░░░░░░░░░░░░░░░░░░░░░░░░░░░░░► ", output);
- // ReflectionTools.InvokePrivate (
- // typeof (Application),
- // "ProcessMouseEvent",
- // new MouseEvent () {
- // X = 6,
- // Y = 6,
- // Flags = MouseFlags.Button1Pressed
- // });
- // Assert.Equal (win, Application.MouseGrabView);
- // Assert.Equal (new Rect (3, 3, 194, 94), win.Frame);
- // ReflectionTools.InvokePrivate (
- // typeof (Application),
- // "ProcessMouseEvent",
- // new MouseEvent () {
- // X = 9,
- // Y = 9,
- // Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
- // });
- // Assert.Equal (win, Application.MouseGrabView);
- // top.SetNeedsLayout ();
- // top.LayoutSubviews ();
- // Assert.Equal (new Rect (6, 6, 191, 91), win.Frame);
- // Application.Refresh ();
- // TestHelpers.AssertDriverContentsWithFrameAre (@"
- // ▲
- // ┬
- // │
- // ┴
- // ░
- // ░
- // ┌────────────────────────────────░
- // │ ░
- // │ ░
- // │ ░
- // │ ░
- // │ ░
- // │ ░
- // │ ░
- // │ ▼
- // ◄├──────┤░░░░░░░░░░░░░░░░░░░░░░░░░░░░░► ", output);
- // ReflectionTools.InvokePrivate (
- // typeof (Application),
- // "ProcessMouseEvent",
- // new MouseEvent () {
- // X = 5,
- // Y = 5,
- // Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
- // });
- // Assert.Equal (win, Application.MouseGrabView);
- // top.SetNeedsLayout ();
- // top.LayoutSubviews ();
- // Assert.Equal (new Rect (2, 2, 195, 95), win.Frame);
- // Application.Refresh ();
- // TestHelpers.AssertDriverContentsWithFrameAre (@"
- // ▲
- // ┬
- // ┌────────────────────────────────────│
- // │ ┴
- // │ ░
- // │ ░
- // │ ░
- // │ ░
- // │ ░
- // │ ░
- // │ ░
- // │ ░
- // │ ░
- // │ ░
- // │ ▼
- // ◄├──────┤░░░░░░░░░░░░░░░░░░░░░░░░░░░░░► ", output);
- // ReflectionTools.InvokePrivate (
- // typeof (Application),
- // "ProcessMouseEvent",
- // new MouseEvent () {
- // X = 5,
- // Y = 5,
- // Flags = MouseFlags.Button1Released
- // });
- // Assert.Null (Application.MouseGrabView);
- // ReflectionTools.InvokePrivate (
- // typeof (Application),
- // "ProcessMouseEvent",
- // new MouseEvent () {
- // X = 4,
- // Y = 4,
- // Flags = MouseFlags.ReportMousePosition
- // });
- // Assert.Equal (scrollView, Application.MouseGrabView);
- //}
- [Fact, AutoInitShutdown]
- public void Dialog_Bounds_Bigger_Than_Driver_Cols_And_Rows_Allow_Drag_Beyond_Left_Right_And_Bottom ()
- {
- var top = Application.Top;
- var dialog = new Dialog (new Button ("Ok")) { Width = 20, Height = 3 };
- Application.Begin (top);
- ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
- Application.Begin (dialog);
- Application.Refresh ();
- Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
- Assert.Equal (new Rect (10, 3, 20, 3), dialog.Frame);
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌──────────────────┐
- │ [ Ok ] │
- └──────────────────┘
- ", output);
- Assert.Null (Application.MouseGrabView);
- ReflectionTools.InvokePrivate (
- typeof (Application),
- "ProcessMouseEvent",
- new MouseEvent () {
- X = 10,
- Y = 3,
- Flags = MouseFlags.Button1Pressed
- });
- Assert.Equal (dialog, Application.MouseGrabView);
- ReflectionTools.InvokePrivate (
- typeof (Application),
- "ProcessMouseEvent",
- new MouseEvent () {
- X = -11,
- Y = -4,
- Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
- });
- Application.Refresh ();
- Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
- Assert.Equal (new Rect (0, 0, 20, 3), dialog.Frame);
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌──────────────────┐
- │ [ Ok ] │
- └──────────────────┘
- ", output);
- // Changes Top size to same size as Dialog more menu and scroll bar
- ((FakeDriver)Application.Driver).SetBufferSize (20, 3);
- ReflectionTools.InvokePrivate (
- typeof (Application),
- "ProcessMouseEvent",
- new MouseEvent () {
- X = -1,
- Y = -1,
- Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
- });
- Application.Refresh ();
- Assert.Equal (new Rect (0, 0, 20, 3), top.Frame);
- Assert.Equal (new Rect (0, 0, 20, 3), dialog.Frame);
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌──────────────────┐
- │ [ Ok ] │
- └──────────────────┘
- ", output);
- // Changes Top size smaller than Dialog size
- ((FakeDriver)Application.Driver).SetBufferSize (19, 2);
- ReflectionTools.InvokePrivate (
- typeof (Application),
- "ProcessMouseEvent",
- new MouseEvent () {
- X = -1,
- Y = -1,
- Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
- });
- Application.Refresh ();
- Assert.Equal (new Rect (0, 0, 19, 2), top.Frame);
- Assert.Equal (new Rect (-1, 0, 20, 3), dialog.Frame);
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ──────────────────┐
- [ Ok ] │
- ", output);
- ReflectionTools.InvokePrivate (
- typeof (Application),
- "ProcessMouseEvent",
- new MouseEvent () {
- X = 18,
- Y = 1,
- Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
- });
- Application.Refresh ();
- Assert.Equal (new Rect (0, 0, 19, 2), top.Frame);
- Assert.Equal (new Rect (18, 1, 20, 3), dialog.Frame);
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌", output);
- // On a real app we can't go beyond the SuperView bounds
- ReflectionTools.InvokePrivate (
- typeof (Application),
- "ProcessMouseEvent",
- new MouseEvent () {
- X = 19,
- Y = 2,
- Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
- });
- Application.Refresh ();
- Assert.Equal (new Rect (0, 0, 19, 2), top.Frame);
- Assert.Equal (new Rect (19, 2, 20, 3), dialog.Frame);
- TestHelpers.AssertDriverContentsWithFrameAre (@"", output);
- }
- [Fact, AutoInitShutdown]
- public void Single_Smaller_Top_Will_Have_Cleaning_Trails_Chunk_On_Move ()
- {
- var dialog = new Dialog () { Width = 30, Height = 10 };
- dialog.Add (new Label (
- "How should I've to react. Cleaning all chunk trails or setting the 'Cols' and 'Rows' to this dialog length?\n" +
- "Cleaning is more easy to fix this.") {
- X = Pos.Center (),
- Y = Pos.Center (),
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- TextAlignment = TextAlignment.Centered,
- VerticalTextAlignment = VerticalTextAlignment.Middle,
- AutoSize = false
- });
- var rs = Application.Begin (dialog);
- Assert.Null (Application.MouseGrabView);
- Assert.Equal (new Rect (25, 7, 30, 10), dialog.Frame);
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌────────────────────────────┐
- │ How should I've to react. │
- │Cleaning all chunk trails or│
- │ setting the 'Cols' and │
- │ 'Rows' to this dialog │
- │ length? │
- │Cleaning is more easy to fix│
- │ this. │
- │ │
- └────────────────────────────┘", output);
- ReflectionTools.InvokePrivate (
- typeof (Application),
- "ProcessMouseEvent",
- new MouseEvent () {
- X = 25,
- Y = 7,
- Flags = MouseFlags.Button1Pressed
- });
- var firstIteration = false;
- Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
- Assert.Equal (dialog, Application.MouseGrabView);
- Assert.Equal (new Rect (25, 7, 30, 10), dialog.Frame);
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌────────────────────────────┐
- │ How should I've to react. │
- │Cleaning all chunk trails or│
- │ setting the 'Cols' and │
- │ 'Rows' to this dialog │
- │ length? │
- │Cleaning is more easy to fix│
- │ this. │
- │ │
- └────────────────────────────┘", output);
- ReflectionTools.InvokePrivate (
- typeof (Application),
- "ProcessMouseEvent",
- new MouseEvent () {
- X = 20,
- Y = 10,
- Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
- });
- firstIteration = false;
- Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
- Assert.Equal (dialog, Application.MouseGrabView);
- Assert.Equal (new Rect (20, 10, 30, 10), dialog.Frame);
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌────────────────────────────┐
- │ How should I've to react. │
- │Cleaning all chunk trails or│
- │ setting the 'Cols' and │
- │ 'Rows' to this dialog │
- │ length? │
- │Cleaning is more easy to fix│
- │ this. │
- │ │
- └────────────────────────────┘", output);
- Application.End (rs);
- }
-
- // BUGBUG: Broke this test with #2483 - @bdisp I need your help figuring out why
- // [Fact, AutoInitShutdown]
- // public void Draw_A_Top_Subview_On_A_Dialog ()
- // {
- // var top = Application.Top;
- // var win = new Window () ;
- // top.Add (win);
- // Application.Begin (top);
- // ((FakeDriver)Application.Driver).SetBufferSize (20, 20);
- // Assert.Equal (new Rect (0, 0, 20, 20), win.Frame);
- // TestHelpers.AssertDriverContentsWithFrameAre (@"
- //┌──────────────────┐
- //│ │
- //│ │
- //│ │
- //│ │
- //│ │
- //│ │
- //│ │
- //│ │
- //│ │
- //│ │
- //│ │
- //│ │
- //│ │
- //│ │
- //│ │
- //│ │
- //│ │
- //│ │
- //└──────────────────┘", output);
- // var btnPopup = new Button ("Popup");
- // btnPopup.Clicked += (s, e) => {
- // var viewToScreen = btnPopup.ViewToScreen (top.Frame);
- // var view = new View () {
- // X = 1,
- // Y = viewToScreen.Y + 1,
- // Width = 18,
- // Height = 5,
- // BorderStyle = LineStyle.Single
- // };
- // Application.Current.DrawContentComplete += Current_DrawContentComplete;
- // top.Add (view);
- // void Current_DrawContentComplete (object sender, DrawEventArgs e)
- // {
- // Assert.Equal (new Rect (1, 14, 18, 5), view.Frame);
- // var savedClip = Application.Driver.Clip;
- // Application.Driver.Clip = top.Frame;
- // view.Redraw (view.Bounds);
- // top.Move (2, 15);
- // View.Driver.AddStr ("One");
- // top.Move (2, 16);
- // View.Driver.AddStr ("Two");
- // top.Move (2, 17);
- // View.Driver.AddStr ("Three");
- // Application.Driver.Clip = savedClip;
- // Application.Current.DrawContentComplete -= Current_DrawContentComplete;
- // }
- // };
- // var dialog = new Dialog (btnPopup) { Width = 15, Height = 10 };
- // var rs = Application.Begin (dialog);
- // Assert.Equal (new Rect (2, 5, 15, 10), dialog.Frame);
- // TestHelpers.AssertDriverContentsWithFrameAre (@"
- //┌──────────────────┐
- //│ │
- //│ │
- //│ │
- //│ │
- //│ ┌─────────────┐ │
- //│ │ │ │
- //│ │ │ │
- //│ │ │ │
- //│ │ │ │
- //│ │ │ │
- //│ │ │ │
- //│ │ │ │
- //│ │ [ Popup ] │ │
- //│ └─────────────┘ │
- //│ │
- //│ │
- //│ │
- //│ │
- //└──────────────────┘", output);
- // ReflectionTools.InvokePrivate (
- // typeof (Application),
- // "ProcessMouseEvent",
- // new MouseEvent () {
- // X = 9,
- // Y = 13,
- // Flags = MouseFlags.Button1Clicked
- // });
- // var firstIteration = false;
- // Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
- // TestHelpers.AssertDriverContentsWithFrameAre (@"
- //┌──────────────────┐
- //│ │
- //│ │
- //│ │
- //│ │
- //│ ┌─────────────┐ │
- //│ │ │ │
- //│ │ │ │
- //│ │ │ │
- //│ │ │ │
- //│ │ │ │
- //│ │ │ │
- //│ │ │ │
- //│ │ [ Popup ] │ │
- //│┌────────────────┐│
- //││One ││
- //││Two ││
- //││Three ││
- //│└────────────────┘│
- //└──────────────────┘", output);
- // Application.End (rs);
- // }
- }
- }
|