12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961 |
- using Xunit.Abstractions;
- namespace Terminal.Gui.ViewTests;
- public class NavigationTests (ITestOutputHelper _output) : TestsAllViews
- {
- [Theory]
- [MemberData (nameof (AllViewTypes))]
- public void AllViews_AtLeastOneNavKey_Leaves (Type viewType)
- {
- View view = CreateInstanceIfNotGeneric (viewType);
- if (view == null)
- {
- _output.WriteLine ($"Ignoring {viewType} - It's a Generic");
- return;
- }
- if (!view.CanFocus)
- {
- _output.WriteLine ($"Ignoring {viewType} - It can't focus.");
- return;
- }
- Application.Init (new FakeDriver ());
- Toplevel top = new ();
- View otherView = new ()
- {
- Id = "otherView",
- CanFocus = true,
- TabStop = view.TabStop
- };
- top.Add (view, otherView);
- Application.Begin (top);
- // Start with the focus on our test view
- view.SetFocus ();
- Key [] navKeys = { Key.Tab, Key.Tab.WithShift, Key.CursorUp, Key.CursorDown, Key.CursorLeft, Key.CursorRight };
- if (view.TabStop == TabBehavior.TabGroup)
- {
- navKeys = new [] { Key.F6, Key.F6.WithShift };
- }
- var left = false;
- foreach (Key key in navKeys)
- {
- switch (view.TabStop)
- {
- case TabBehavior.TabStop:
- case TabBehavior.NoStop:
- case TabBehavior.TabGroup:
- Application.OnKeyDown (key);
- break;
- default:
- Application.OnKeyDown (Key.Tab);
- break;
- }
- if (!view.HasFocus)
- {
- left = true;
- _output.WriteLine ($"{view.GetType ().Name} - {key} Left.");
- view.SetFocus ();
- }
- else
- {
- _output.WriteLine ($"{view.GetType ().Name} - {key} did not Leave.");
- }
- }
- top.Dispose ();
- Application.Shutdown ();
- Assert.True (left);
- }
- [Theory]
- [MemberData (nameof (AllViewTypes))]
- public void AllViews_Enter_Leave_Events (Type viewType)
- {
- View view = CreateInstanceIfNotGeneric (viewType);
- if (view == null)
- {
- _output.WriteLine ($"Ignoring {viewType} - It's a Generic");
- return;
- }
- if (!view.CanFocus)
- {
- _output.WriteLine ($"Ignoring {viewType} - It can't focus.");
- return;
- }
- if (view is Toplevel && ((Toplevel)view).Modal)
- {
- _output.WriteLine ($"Ignoring {viewType} - It's a Modal Toplevel");
- return;
- }
- Application.Init (new FakeDriver ());
- Toplevel top = new ()
- {
- Height = 10,
- Width = 10
- };
- View otherView = new ()
- {
- Id = "otherView",
- X = 0, Y = 0,
- Height = 1,
- Width = 1,
- CanFocus = true,
- TabStop = view.TabStop
- };
- view.X = Pos.Right (otherView);
- view.Y = 0;
- view.Width = 10;
- view.Height = 1;
- var nEnter = 0;
- var nLeave = 0;
- view.Enter += (s, e) => nEnter++;
- view.Leave += (s, e) => nLeave++;
- top.Add (view, otherView);
- Application.Begin (top);
- // Start with the focus on our test view
- view.SetFocus ();
- //Assert.Equal (1, nEnter);
- //Assert.Equal (0, nLeave);
- // Use keyboard to navigate to next view (otherView).
- if (view is TextView)
- {
- Application.OnKeyDown (Key.F6);
- }
- else
- {
- var tries = 0;
- while (view.HasFocus)
- {
- if (++tries > 10)
- {
- Assert.Fail ($"{view} is not leaving.");
- }
- switch (view.TabStop)
- {
- case TabBehavior.NoStop:
- Application.OnKeyDown (Key.Tab);
- break;
- case TabBehavior.TabStop:
- Application.OnKeyDown (Key.Tab);
- break;
- case TabBehavior.TabGroup:
- Application.OnKeyDown (Key.F6);
- break;
- case null:
- Application.OnKeyDown (Key.Tab);
- break;
- default:
- throw new ArgumentOutOfRangeException ();
- }
- }
- }
- //Assert.Equal (1, nEnter);
- //Assert.Equal (1, nLeave);
- //Assert.False (view.HasFocus);
- //Assert.True (otherView.HasFocus);
- // Now navigate back to our test view
- switch (view.TabStop)
- {
- case TabBehavior.NoStop:
- view.SetFocus ();
- break;
- case TabBehavior.TabStop:
- Application.OnKeyDown (Key.Tab);
- break;
- case TabBehavior.TabGroup:
- Application.OnKeyDown (Key.F6);
- break;
- case null:
- Application.OnKeyDown (Key.Tab);
- break;
- default:
- throw new ArgumentOutOfRangeException ();
- }
- // Cache state because Shutdown has side effects.
- // Also ensures other tests can continue running if there's a fail
- bool otherViewHasFocus = otherView.HasFocus;
- bool viewHasFocus = view.HasFocus;
- int enterCount = nEnter;
- int leaveCount = nLeave;
- top.Dispose ();
- Application.Shutdown ();
- Assert.False (otherViewHasFocus);
- Assert.True (viewHasFocus);
- Assert.Equal (2, enterCount);
- Assert.Equal (1, leaveCount);
- }
- [Theory]
- [MemberData (nameof (AllViewTypes))]
- public void AllViews_Enter_Leave_Events_Visible_False (Type viewType)
- {
- View view = CreateInstanceIfNotGeneric (viewType);
- if (view == null)
- {
- _output.WriteLine ($"Ignoring {viewType} - It's a Generic");
- return;
- }
- if (!view.CanFocus)
- {
- _output.WriteLine ($"Ignoring {viewType} - It can't focus.");
- return;
- }
- if (view is Toplevel && ((Toplevel)view).Modal)
- {
- _output.WriteLine ($"Ignoring {viewType} - It's a Modal Toplevel");
- return;
- }
- Application.Init (new FakeDriver ());
- Toplevel top = new ()
- {
- Height = 10,
- Width = 10
- };
- View otherView = new ()
- {
- X = 0, Y = 0,
- Height = 1,
- Width = 1,
- CanFocus = true
- };
- view.Visible = false;
- view.X = Pos.Right (otherView);
- view.Y = 0;
- view.Width = 10;
- view.Height = 1;
- var nEnter = 0;
- var nLeave = 0;
- view.Enter += (s, e) => nEnter++;
- view.Leave += (s, e) => nLeave++;
- top.Add (view, otherView);
- Application.Begin (top);
- // Start with the focus on our test view
- view.SetFocus ();
- Assert.Equal (0, nEnter);
- Assert.Equal (0, nLeave);
- // Use keyboard to navigate to next view (otherView).
- if (view is TextView)
- {
- Application.OnKeyDown (Key.F6);
- }
- else if (view is DatePicker)
- {
- for (var i = 0; i < 4; i++)
- {
- Application.OnKeyDown (Key.F6);
- }
- }
- else
- {
- Application.OnKeyDown (Key.Tab);
- }
- Assert.Equal (0, nEnter);
- Assert.Equal (0, nLeave);
- top.NewKeyDownEvent (Key.Tab);
- Assert.Equal (0, nEnter);
- Assert.Equal (0, nLeave);
- top.Dispose ();
- Application.Shutdown ();
- }
- [Fact]
- public void BringSubviewForward_Subviews_vs_TabIndexes ()
- {
- var r = new View ();
- var v1 = new View { CanFocus = true };
- var v2 = new View { CanFocus = true };
- var v3 = new View { CanFocus = true };
- r.Add (v1, v2, v3);
- r.BringSubviewForward (v1);
- Assert.True (r.Subviews.IndexOf (v1) == 1);
- Assert.True (r.Subviews.IndexOf (v2) == 0);
- Assert.True (r.Subviews.IndexOf (v3) == 2);
- Assert.True (r.TabIndexes.IndexOf (v1) == 0);
- Assert.True (r.TabIndexes.IndexOf (v2) == 1);
- Assert.True (r.TabIndexes.IndexOf (v3) == 2);
- r.Dispose ();
- }
- [Fact]
- public void BringSubviewToFront_Subviews_vs_TabIndexes ()
- {
- var r = new View ();
- var v1 = new View { CanFocus = true };
- var v2 = new View { CanFocus = true };
- var v3 = new View { CanFocus = true };
- r.Add (v1, v2, v3);
- r.BringSubviewToFront (v1);
- Assert.True (r.Subviews.IndexOf (v1) == 2);
- Assert.True (r.Subviews.IndexOf (v2) == 0);
- Assert.True (r.Subviews.IndexOf (v3) == 1);
- Assert.True (r.TabIndexes.IndexOf (v1) == 0);
- Assert.True (r.TabIndexes.IndexOf (v2) == 1);
- Assert.True (r.TabIndexes.IndexOf (v3) == 2);
- r.Dispose ();
- }
- [Fact]
- public void CanFocus_Container_ToFalse_Turns_All_Subviews_ToFalse_Too ()
- {
- Application.Init (new FakeDriver ());
- Toplevel t = new ();
- var w = new Window ();
- var f = new FrameView ();
- var v1 = new View { CanFocus = true };
- var v2 = new View { CanFocus = true };
- f.Add (v1, v2);
- w.Add (f);
- t.Add (w);
- t.Ready += (s, e) =>
- {
- Assert.True (t.CanFocus);
- Assert.True (w.CanFocus);
- Assert.True (f.CanFocus);
- Assert.True (v1.CanFocus);
- Assert.True (v2.CanFocus);
- w.CanFocus = false;
- Assert.False (w.CanFocus);
- Assert.False (f.CanFocus);
- Assert.False (v1.CanFocus);
- Assert.False (v2.CanFocus);
- };
- Application.Iteration += (s, a) => Application.RequestStop ();
- Application.Run (t);
- t.Dispose ();
- Application.Shutdown ();
- }
- [Fact]
- public void CanFocus_Container_Toggling_All_Subviews_To_Old_Value_When_Is_True ()
- {
- Application.Init (new FakeDriver ());
- Toplevel t = new ();
- var w = new Window ();
- var f = new FrameView ();
- var v1 = new View ();
- var v2 = new View { CanFocus = true };
- f.Add (v1, v2);
- w.Add (f);
- t.Add (w);
- t.Ready += (s, e) =>
- {
- Assert.True (t.CanFocus);
- Assert.True (w.CanFocus);
- Assert.True (f.CanFocus);
- Assert.False (v1.CanFocus);
- Assert.True (v2.CanFocus);
- w.CanFocus = false;
- Assert.False (w.CanFocus);
- Assert.False (f.CanFocus);
- Assert.False (v1.CanFocus);
- Assert.False (v2.CanFocus);
- w.CanFocus = true;
- Assert.True (w.CanFocus);
- Assert.True (f.CanFocus);
- Assert.False (v1.CanFocus);
- Assert.True (v2.CanFocus);
- };
- Application.Iteration += (s, a) => Application.RequestStop ();
- Application.Run (t);
- t.Dispose ();
- Application.Shutdown ();
- }
- [Fact]
- [AutoInitShutdown]
- public void CanFocus_Faced_With_Container ()
- {
- var t = new Toplevel ();
- var w = new Window ();
- var f = new FrameView ();
- var v = new View { CanFocus = true };
- f.Add (v);
- w.Add (f);
- t.Add (w);
- Assert.True (t.CanFocus);
- Assert.True (w.CanFocus);
- Assert.True (f.CanFocus);
- Assert.True (v.CanFocus);
- f.CanFocus = false;
- Assert.False (f.CanFocus);
- Assert.True (v.CanFocus);
- v.CanFocus = false;
- Assert.False (f.CanFocus);
- Assert.False (v.CanFocus);
- v.CanFocus = true;
- Assert.False (f.CanFocus);
- Assert.True (v.CanFocus);
- t.Dispose ();
- }
- [Fact]
- public void CanFocus_Faced_With_Container_After_Run ()
- {
- Application.Init (new FakeDriver ());
- Toplevel t = new ();
- var w = new Window ();
- var f = new FrameView ();
- var v = new View { CanFocus = true };
- f.Add (v);
- w.Add (f);
- t.Add (w);
- t.Ready += (s, e) =>
- {
- Assert.True (t.CanFocus);
- Assert.True (w.CanFocus);
- Assert.True (f.CanFocus);
- Assert.True (v.CanFocus);
- f.CanFocus = false;
- Assert.False (f.CanFocus);
- Assert.False (v.CanFocus);
- v.CanFocus = false;
- Assert.False (f.CanFocus);
- Assert.False (v.CanFocus);
- Assert.Throws<InvalidOperationException> (() => v.CanFocus = true);
- Assert.False (f.CanFocus);
- Assert.False (v.CanFocus);
- f.CanFocus = true;
- Assert.True (f.CanFocus);
- Assert.True (v.CanFocus);
- };
- Application.Iteration += (s, a) => Application.RequestStop ();
- Application.Run (t);
- t.Dispose ();
- Application.Shutdown ();
- }
- [Fact]
- public void CanFocus_Faced_With_Container_Before_Run ()
- {
- Application.Init (new FakeDriver ());
- Toplevel t = new ();
- var w = new Window ();
- var f = new FrameView ();
- var v = new View { CanFocus = true };
- f.Add (v);
- w.Add (f);
- t.Add (w);
- Assert.True (t.CanFocus);
- Assert.True (w.CanFocus);
- Assert.True (f.CanFocus);
- Assert.True (v.CanFocus);
- f.CanFocus = false;
- Assert.False (f.CanFocus);
- Assert.True (v.CanFocus);
- v.CanFocus = false;
- Assert.False (f.CanFocus);
- Assert.False (v.CanFocus);
- v.CanFocus = true;
- Assert.False (f.CanFocus);
- Assert.True (v.CanFocus);
- Application.Iteration += (s, a) => Application.RequestStop ();
- Application.Run (t);
- t.Dispose ();
- Application.Shutdown ();
- }
- [Fact]
- public void CanFocus_False_Set_HasFocus_To_False ()
- {
- var view = new View { CanFocus = true };
- var view2 = new View { CanFocus = true };
- view2.Add (view);
- Assert.True (view.CanFocus);
- view.SetFocus ();
- Assert.True (view.HasFocus);
- view.CanFocus = false;
- Assert.False (view.CanFocus);
- Assert.False (view.HasFocus);
- }
- [Fact]
- public void CanFocus_Set_Changes_TabIndex_And_TabStop ()
- {
- var r = new View ();
- var v1 = new View { Text = "1" };
- var v2 = new View { Text = "2" };
- var v3 = new View { Text = "3" };
- r.Add (v1, v2, v3);
- v2.CanFocus = true;
- Assert.Equal (r.TabIndexes.IndexOf (v2), v2.TabIndex);
- Assert.Equal (0, v2.TabIndex);
- Assert.Equal (TabBehavior.TabStop, v2.TabStop);
- v1.CanFocus = true;
- Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
- Assert.Equal (1, v1.TabIndex);
- Assert.Equal (TabBehavior.TabStop, v1.TabStop);
- v1.TabIndex = 2;
- Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
- Assert.Equal (1, v1.TabIndex);
- v3.CanFocus = true;
- Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
- Assert.Equal (1, v1.TabIndex);
- Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
- Assert.Equal (2, v3.TabIndex);
- Assert.Equal (TabBehavior.TabStop, v3.TabStop);
- v2.CanFocus = false;
- Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
- Assert.Equal (1, v1.TabIndex);
- Assert.Equal (TabBehavior.TabStop, v1.TabStop);
- Assert.Equal (r.TabIndexes.IndexOf (v2), v2.TabIndex); // TabIndex is not changed
- Assert.NotEqual (-1, v2.TabIndex);
- Assert.Equal (TabBehavior.TabStop, v2.TabStop); // TabStop is not changed
- Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
- Assert.Equal (2, v3.TabIndex);
- Assert.Equal (TabBehavior.TabStop, v3.TabStop);
- r.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void CanFocus_Sets_To_False_On_Single_View_Focus_View_On_Another_Toplevel ()
- {
- var view1 = new View { Id = "view1", Width = 10, Height = 1, CanFocus = true };
- var win1 = new Window { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
- win1.Add (view1);
- 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);
- 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.OnKeyDown (Key.F6));
- 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.OnKeyDown (Key.F6));
- 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
- view1.CanFocus = false;
- Assert.False (view1.CanFocus);
- Assert.False (view1.HasFocus);
- Assert.True (view2.CanFocus);
- Assert.True (view2.HasFocus);
- Assert.Equal (win2, Application.Current.Focused);
- Assert.Equal (view2, Application.Current.MostFocused);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void CanFocus_Sets_To_False_On_Toplevel_Focus_View_On_Another_Toplevel ()
- {
- var view1 = new View { Id = "view1", Width = 10, Height = 1, CanFocus = true };
- var win1 = new Window { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
- win1.Add (view1);
- 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);
- 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.OnKeyDown (Key.F6));
- 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.OnKeyDown (Key.F6));
- 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
- win1.CanFocus = false;
- Assert.False (view1.CanFocus);
- Assert.False (view1.HasFocus);
- Assert.False (win1.CanFocus);
- Assert.False (win1.HasFocus);
- Assert.True (view2.CanFocus);
- Assert.True (view2.HasFocus);
- Assert.Equal (win2, Application.Current.Focused);
- Assert.Equal (view2, Application.Current.MostFocused);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void CanFocus_Sets_To_False_With_Two_Views_Focus_Another_View_On_The_Same_Toplevel ()
- {
- var view1 = new View { Id = "view1", Width = 10, Height = 1, CanFocus = true };
- var view12 = new View
- {
- Id = "view12",
- Y = 5,
- Width = 10,
- Height = 1,
- CanFocus = true
- };
- var win1 = new Window { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
- win1.Add (view1, view12);
- 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);
- 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.OnKeyDown (Key.F6)); // move to win2
- 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.OnKeyDown (Key.F6));
- 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
- view1.CanFocus = false;
- Assert.False (view1.CanFocus);
- Assert.False (view1.HasFocus);
- Assert.True (view2.CanFocus);
- Assert.False (view2.HasFocus);
- Assert.Equal (win1, Application.Current.Focused);
- Assert.Equal (view12, Application.Current.MostFocused);
- top.Dispose ();
- }
- [Fact]
- public void Enabled_False_Sets_HasFocus_To_False ()
- {
- var wasClicked = false;
- var view = new Button { Text = "Click Me" };
- view.Accept += (s, e) => wasClicked = !wasClicked;
- view.NewKeyDownEvent (Key.Space);
- Assert.True (wasClicked);
- view.NewMouseEvent (new() { Flags = MouseFlags.Button1Clicked });
- Assert.False (wasClicked);
- Assert.True (view.Enabled);
- Assert.True (view.CanFocus);
- Assert.True (view.HasFocus);
- view.Enabled = false;
- view.NewKeyDownEvent (Key.Space);
- Assert.False (wasClicked);
- view.NewMouseEvent (new() { Flags = MouseFlags.Button1Clicked });
- Assert.False (wasClicked);
- Assert.False (view.Enabled);
- Assert.True (view.CanFocus);
- Assert.False (view.HasFocus);
- view.SetFocus ();
- Assert.False (view.HasFocus);
- }
- [Fact]
- [AutoInitShutdown]
- public void Enabled_Sets_Also_Sets_Subviews ()
- {
- var wasClicked = false;
- var button = new Button { Text = "Click Me" };
- button.IsDefault = true;
- button.Accept += (s, e) => wasClicked = !wasClicked;
- var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
- win.Add (button);
- var top = new Toplevel ();
- top.Add (win);
- var iterations = 0;
- Application.Iteration += (s, a) =>
- {
- iterations++;
- win.NewKeyDownEvent (Key.Enter);
- Assert.True (wasClicked);
- button.NewMouseEvent (new() { Flags = MouseFlags.Button1Clicked });
- Assert.False (wasClicked);
- Assert.True (button.Enabled);
- Assert.True (button.CanFocus);
- Assert.True (button.HasFocus);
- Assert.True (win.Enabled);
- Assert.True (win.CanFocus);
- Assert.True (win.HasFocus);
- win.Enabled = false;
- button.NewKeyDownEvent (Key.Enter);
- Assert.False (wasClicked);
- button.NewMouseEvent (new() { Flags = MouseFlags.Button1Clicked });
- Assert.False (wasClicked);
- Assert.False (button.Enabled);
- Assert.True (button.CanFocus);
- Assert.False (button.HasFocus);
- Assert.False (win.Enabled);
- Assert.True (win.CanFocus);
- Assert.False (win.HasFocus);
- button.SetFocus ();
- Assert.False (button.HasFocus);
- Assert.False (win.HasFocus);
- win.SetFocus ();
- Assert.False (button.HasFocus);
- Assert.False (win.HasFocus);
- win.Enabled = true;
- win.FocusDeepest (null, NavigationDirection.Forward);
- Assert.True (button.HasFocus);
- Assert.True (win.HasFocus);
- Application.RequestStop ();
- };
- Application.Run (top);
- Assert.Equal (1, iterations);
- top.Dispose ();
- }
- // View.Focused & View.MostFocused tests
- // View.Focused - No subviews
- [Fact]
- [Trait ("BUGBUG", "Fix in Issue #3444")]
- public void Focused_NoSubviews ()
- {
- var view = new View ();
- Assert.Null (view.Focused);
- view.CanFocus = true;
- view.SetFocus ();
- Assert.True (view.HasFocus);
- Assert.Null (view.Focused); // BUGBUG: Should be view
- }
- [Fact]
- [AutoInitShutdown]
- public void FocusNearestView_Ensure_Focus_Ordered ()
- {
- var top = new Toplevel ();
- var win = new Window ();
- var winSubview = new View { CanFocus = true, Text = "WindowSubview" };
- win.Add (winSubview);
- top.Add (win);
- var frm = new FrameView ();
- var frmSubview = new View { CanFocus = true, Text = "FrameSubview" };
- frm.Add (frmSubview);
- top.Add (frm);
- Application.Begin (top);
- Assert.Equal ("WindowSubview", top.MostFocused.Text);
- Application.OnKeyDown (Key.Tab);
- Assert.Equal ("WindowSubview", top.MostFocused.Text);
- Application.OnKeyDown (Key.F6);
- Assert.Equal ("FrameSubview", top.MostFocused.Text);
- Application.OnKeyDown (Key.Tab);
- Assert.Equal ("FrameSubview", top.MostFocused.Text);
- Application.OnKeyDown (Key.F6);
- Assert.Equal ("WindowSubview", top.MostFocused.Text);
- Application.OnKeyDown (Key.F6.WithShift);
- Assert.Equal ("FrameSubview", top.MostFocused.Text);
- Application.OnKeyDown (Key.F6.WithShift);
- Assert.Equal ("WindowSubview", top.MostFocused.Text);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void FocusNext_Does_Not_Throws_If_A_View_Was_Removed_From_The_Collection ()
- {
- 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 };
- var view2 = new View
- {
- Id = "view2",
- Y = 1,
- Width = 10,
- Height = 5,
- CanFocus = true
- };
- View view3 = null;
- var removed = false;
- view2.Enter += (s, e) =>
- {
- if (!removed)
- {
- removed = true;
- view3 = new() { Id = "view3", Y = 1, Width = 10, Height = 5 };
- Application.Current.Add (view3);
- Application.Current.BringSubviewToFront (view3);
- Assert.False (view3.HasFocus);
- }
- };
- view2.Leave += (s, e) =>
- {
- Application.Current.Remove (view3);
- view3.Dispose ();
- view3 = null;
- };
- top2.Add (view2);
- top1.Add (view1, top2);
- Application.Begin (top1);
- Assert.True (top1.HasFocus);
- Assert.True (view1.HasFocus);
- Assert.False (view2.HasFocus);
- Assert.False (removed);
- Assert.Null (view3);
- Assert.True (Application.OnKeyDown (Key.F6));
- Assert.True (top1.HasFocus);
- Assert.False (view1.HasFocus);
- Assert.True (view2.HasFocus);
- Assert.True (removed);
- Assert.NotNull (view3);
- Exception exception = Record.Exception (() => Application.OnKeyDown (Key.F6));
- Assert.Null (exception);
- Assert.True (removed);
- //Assert.Null (view3);
- top1.Dispose ();
- }
- // View.MostFocused - No subviews
- [Fact]
- [Trait ("BUGBUG", "Fix in Issue #3444")]
- public void Most_Focused_NoSubviews ()
- {
- var view = new View ();
- Assert.Null (view.Focused);
- view.CanFocus = true;
- view.SetFocus ();
- Assert.True (view.HasFocus);
- Assert.Null (view.MostFocused); // BUGBUG: Should be view
- }
- // [Fact]
- // [AutoInitShutdown]
- // public void HotKey_Will_Invoke_KeyPressed_Only_For_The_MostFocused_With_Top_KeyPress_Event ()
- // {
- // var sbQuiting = false;
- // var tfQuiting = false;
- // var topQuiting = false;
- // var sb = new StatusBar (
- // new Shortcut []
- // {
- // new (
- // KeyCode.CtrlMask | KeyCode.Q,
- // "Quit",
- // () => sbQuiting = true
- // )
- // }
- // );
- // var tf = new TextField ();
- // tf.KeyDown += Tf_KeyPressed;
- // void Tf_KeyPressed (object sender, Key obj)
- // {
- // if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
- // {
- // obj.Handled = tfQuiting = true;
- // }
- // }
- // var win = new Window ();
- // win.Add (sb, tf);
- // Toplevel top = new ();
- // top.KeyDown += Top_KeyPress;
- // void Top_KeyPress (object sender, Key obj)
- // {
- // if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
- // {
- // obj.Handled = topQuiting = true;
- // }
- // }
- // top.Add (win);
- // Application.Begin (top);
- // Assert.False (sbQuiting);
- // Assert.False (tfQuiting);
- // Assert.False (topQuiting);
- // Application.Driver?.SendKeys ('Q', ConsoleKey.Q, false, false, true);
- // Assert.False (sbQuiting);
- // Assert.True (tfQuiting);
- // Assert.False (topQuiting);
- //#if BROKE_WITH_2927
- // tf.KeyPressed -= Tf_KeyPress;
- // tfQuiting = false;
- // Application.Driver?.SendKeys ('q', ConsoleKey.Q, false, false, true);
- // Application.MainLoop.RunIteration ();
- // Assert.True (sbQuiting);
- // Assert.False (tfQuiting);
- // Assert.False (topQuiting);
- // sb.RemoveItem (0);
- // sbQuiting = false;
- // Application.Driver?.SendKeys ('q', ConsoleKey.Q, false, false, true);
- // Application.MainLoop.RunIteration ();
- // Assert.False (sbQuiting);
- // Assert.False (tfQuiting);
- //// This test is now invalid because `win` is focused, so it will receive the keypress
- // Assert.True (topQuiting);
- //#endif
- // top.Dispose ();
- // }
- // [Fact]
- // [AutoInitShutdown]
- // public void HotKey_Will_Invoke_KeyPressed_Only_For_The_MostFocused_Without_Top_KeyPress_Event ()
- // {
- // var sbQuiting = false;
- // var tfQuiting = false;
- // var sb = new StatusBar (
- // new Shortcut []
- // {
- // new (
- // KeyCode.CtrlMask | KeyCode.Q,
- // "~^Q~ Quit",
- // () => sbQuiting = true
- // )
- // }
- // );
- // var tf = new TextField ();
- // tf.KeyDown += Tf_KeyPressed;
- // void Tf_KeyPressed (object sender, Key obj)
- // {
- // if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
- // {
- // obj.Handled = tfQuiting = true;
- // }
- // }
- // var win = new Window ();
- // win.Add (sb, tf);
- // Toplevel top = new ();
- // top.Add (win);
- // Application.Begin (top);
- // Assert.False (sbQuiting);
- // Assert.False (tfQuiting);
- // Application.Driver?.SendKeys ('Q', ConsoleKey.Q, false, false, true);
- // Assert.False (sbQuiting);
- // Assert.True (tfQuiting);
- // tf.KeyDown -= Tf_KeyPressed;
- // tfQuiting = false;
- // Application.Driver?.SendKeys ('Q', ConsoleKey.Q, false, false, true);
- // Application.MainLoop.RunIteration ();
- //#if BROKE_WITH_2927
- // Assert.True (sbQuiting);
- // Assert.False (tfQuiting);
- //#endif
- // top.Dispose ();
- // }
- [Fact]
- [SetupFakeDriver]
- public void Navigation_With_Null_Focused_View ()
- {
- // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
- Application.Init (new FakeDriver ());
- var top = new Toplevel ();
- top.Ready += (s, e) => { Assert.Null (top.Focused); };
- // Keyboard navigation with tab
- FakeConsole.MockKeyPresses.Push (new ('\t', ConsoleKey.Tab, false, false, false));
- Application.Iteration += (s, a) => Application.RequestStop ();
- Application.Run (top);
- top.Dispose ();
- Application.Shutdown ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Remove_Does_Not_Change_Focus ()
- {
- var top = new Toplevel ();
- Assert.True (top.CanFocus);
- Assert.False (top.HasFocus);
- var container = new View { Width = 10, Height = 10 };
- var leave = false;
- container.Leave += (s, e) => leave = true;
- Assert.False (container.CanFocus);
- var child = new View { Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
- container.Add (child);
- Assert.True (container.CanFocus);
- Assert.False (container.HasFocus);
- Assert.True (child.CanFocus);
- Assert.False (child.HasFocus);
- top.Add (container);
- Application.Begin (top);
- Assert.True (top.CanFocus);
- Assert.True (top.HasFocus);
- Assert.True (container.CanFocus);
- Assert.True (container.HasFocus);
- Assert.True (child.CanFocus);
- Assert.True (child.HasFocus);
- container.Remove (child);
- child.Dispose ();
- child = null;
- Assert.True (top.HasFocus);
- Assert.True (container.CanFocus);
- Assert.True (container.HasFocus);
- Assert.Null (child);
- Assert.False (leave);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void ScreenToView_ViewToScreen_FindDeepestView_Full_Top ()
- {
- Toplevel top = new ();
- top.BorderStyle = LineStyle.Single;
- var view = new View
- {
- X = 3,
- Y = 2,
- Width = 10,
- Height = 1,
- Text = "0123456789"
- };
- top.Add (view);
- Application.Begin (top);
- Assert.Equal (Application.Current, top);
- Assert.Equal (new (0, 0, 80, 25), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
- Assert.Equal (new (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
- Assert.Equal (new (0, 0, 80, 25), top.Frame);
- ((FakeDriver)Application.Driver!).SetBufferSize (20, 10);
- Assert.Equal (new (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
- Assert.Equal (new (0, 0, 20, 10), top.Frame);
- _ = TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌──────────────────┐
- │ │
- │ │
- │ 0123456789 │
- │ │
- │ │
- │ │
- │ │
- │ │
- └──────────────────┘",
- _output
- );
- // top
- Assert.Equal (Point.Empty, top.ScreenToFrame (new (0, 0)));
- Point screen = top.Margin.ViewportToScreen (new Point (0, 0));
- Assert.Equal (0, screen.X);
- Assert.Equal (0, screen.Y);
- screen = top.Border.ViewportToScreen (new Point (0, 0));
- Assert.Equal (0, screen.X);
- Assert.Equal (0, screen.Y);
- screen = top.Padding.ViewportToScreen (new Point (0, 0));
- Assert.Equal (1, screen.X);
- Assert.Equal (1, screen.Y);
- screen = top.ViewportToScreen (new Point (0, 0));
- Assert.Equal (1, screen.X);
- Assert.Equal (1, screen.Y);
- screen = top.ViewportToScreen (new Point (-1, -1));
- Assert.Equal (0, screen.X);
- Assert.Equal (0, screen.Y);
- var found = View.FindDeepestView (top, new (0, 0));
- Assert.Equal (top.Border, found);
- Assert.Equal (0, found.Frame.X);
- Assert.Equal (0, found.Frame.Y);
- Assert.Equal (new (3, 2), top.ScreenToFrame (new (3, 2)));
- screen = top.ViewportToScreen (new Point (3, 2));
- Assert.Equal (4, screen.X);
- Assert.Equal (3, screen.Y);
- found = View.FindDeepestView (top, new (screen.X, screen.Y));
- Assert.Equal (view, found);
- //Assert.Equal (0, found.FrameToScreen ().X);
- //Assert.Equal (0, found.FrameToScreen ().Y);
- found = View.FindDeepestView (top, new (3, 2));
- Assert.Equal (top, found);
- //Assert.Equal (3, found.FrameToScreen ().X);
- //Assert.Equal (2, found.FrameToScreen ().Y);
- Assert.Equal (new (13, 2), top.ScreenToFrame (new (13, 2)));
- screen = top.ViewportToScreen (new Point (12, 2));
- Assert.Equal (13, screen.X);
- Assert.Equal (3, screen.Y);
- found = View.FindDeepestView (top, new (screen.X, screen.Y));
- Assert.Equal (view, found);
- //Assert.Equal (9, found.FrameToScreen ().X);
- //Assert.Equal (0, found.FrameToScreen ().Y);
- screen = top.ViewportToScreen (new Point (13, 2));
- Assert.Equal (14, screen.X);
- Assert.Equal (3, screen.Y);
- found = View.FindDeepestView (top, new (13, 2));
- Assert.Equal (top, found);
- //Assert.Equal (13, found.FrameToScreen ().X);
- //Assert.Equal (2, found.FrameToScreen ().Y);
- Assert.Equal (new (14, 3), top.ScreenToFrame (new (14, 3)));
- screen = top.ViewportToScreen (new Point (14, 3));
- Assert.Equal (15, screen.X);
- Assert.Equal (4, screen.Y);
- found = View.FindDeepestView (top, new (14, 3));
- Assert.Equal (top, found);
- //Assert.Equal (14, found.FrameToScreen ().X);
- //Assert.Equal (3, found.FrameToScreen ().Y);
- // view
- Assert.Equal (new (-4, -3), view.ScreenToFrame (new (0, 0)));
- screen = view.Margin.ViewportToScreen (new Point (-3, -2));
- Assert.Equal (1, screen.X);
- Assert.Equal (1, screen.Y);
- screen = view.Border.ViewportToScreen (new Point (-3, -2));
- Assert.Equal (1, screen.X);
- Assert.Equal (1, screen.Y);
- screen = view.Padding.ViewportToScreen (new Point (-3, -2));
- Assert.Equal (1, screen.X);
- Assert.Equal (1, screen.Y);
- screen = view.ViewportToScreen (new Point (-3, -2));
- Assert.Equal (1, screen.X);
- Assert.Equal (1, screen.Y);
- screen = view.ViewportToScreen (new Point (-4, -3));
- Assert.Equal (0, screen.X);
- Assert.Equal (0, screen.Y);
- found = View.FindDeepestView (top, new (0, 0));
- Assert.Equal (top.Border, found);
- Assert.Equal (new (-1, -1), view.ScreenToFrame (new (3, 2)));
- screen = view.ViewportToScreen (new Point (0, 0));
- Assert.Equal (4, screen.X);
- Assert.Equal (3, screen.Y);
- found = View.FindDeepestView (top, new (4, 3));
- Assert.Equal (view, found);
- Assert.Equal (new (9, -1), view.ScreenToFrame (new (13, 2)));
- screen = view.ViewportToScreen (new Point (10, 0));
- Assert.Equal (14, screen.X);
- Assert.Equal (3, screen.Y);
- found = View.FindDeepestView (top, new (14, 3));
- Assert.Equal (top, found);
- Assert.Equal (new (10, 0), view.ScreenToFrame (new (14, 3)));
- screen = view.ViewportToScreen (new Point (11, 1));
- Assert.Equal (15, screen.X);
- Assert.Equal (4, screen.Y);
- found = View.FindDeepestView (top, new (15, 4));
- Assert.Equal (top, found);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void ScreenToView_ViewToScreen_FindDeepestView_Smaller_Top ()
- {
- var top = new Toplevel
- {
- X = 3,
- Y = 2,
- Width = 20,
- Height = 10,
- BorderStyle = LineStyle.Single
- };
- var view = new View
- {
- X = 3,
- Y = 2,
- Width = 10,
- Height = 1,
- Text = "0123456789"
- };
- top.Add (view);
- Application.Begin (top);
- Assert.Equal (Application.Current, top);
- Assert.Equal (new (0, 0, 80, 25), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
- Assert.NotEqual (new (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
- Assert.Equal (new (3, 2, 20, 10), top.Frame);
- ((FakeDriver)Application.Driver!).SetBufferSize (30, 20);
- Assert.Equal (new (0, 0, 30, 20), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
- Assert.NotEqual (new (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
- Assert.Equal (new (3, 2, 20, 10), top.Frame);
- Rectangle frame = TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌──────────────────┐
- │ │
- │ │
- │ 0123456789 │
- │ │
- │ │
- │ │
- │ │
- │ │
- └──────────────────┘",
- _output
- );
- // mean the output started at col 3 and line 2
- // which result with a width of 23 and a height of 10 on the output
- Assert.Equal (new (3, 2, 23, 10), frame);
- // top
- Assert.Equal (new (-3, -2), top.ScreenToFrame (new (0, 0)));
- Point screen = top.Margin.ViewportToScreen (new Point (-3, -2));
- Assert.Equal (0, screen.X);
- Assert.Equal (0, screen.Y);
- screen = top.Border.ViewportToScreen (new Point (-3, -2));
- Assert.Equal (0, screen.X);
- Assert.Equal (0, screen.Y);
- screen = top.Padding.ViewportToScreen (new Point (-3, -2));
- Assert.Equal (1, screen.X);
- Assert.Equal (1, screen.Y);
- screen = top.ViewportToScreen (new Point (-3, -2));
- Assert.Equal (1, screen.X);
- Assert.Equal (1, screen.Y);
- screen = top.ViewportToScreen (new Point (-4, -3));
- Assert.Equal (0, screen.X);
- Assert.Equal (0, screen.Y);
- var found = View.FindDeepestView (top, new (-4, -3));
- Assert.Null (found);
- Assert.Equal (Point.Empty, top.ScreenToFrame (new (3, 2)));
- screen = top.ViewportToScreen (new Point (0, 0));
- Assert.Equal (4, screen.X);
- Assert.Equal (3, screen.Y);
- Assert.Equal (top.Border, View.FindDeepestView (top, new (3, 2)));
- //Assert.Equal (0, found.FrameToScreen ().X);
- //Assert.Equal (0, found.FrameToScreen ().Y);
- Assert.Equal (new (10, 0), top.ScreenToFrame (new (13, 2)));
- screen = top.ViewportToScreen (new Point (10, 0));
- Assert.Equal (14, screen.X);
- Assert.Equal (3, screen.Y);
- Assert.Equal (top.Border, View.FindDeepestView (top, new (13, 2)));
- //Assert.Equal (10, found.FrameToScreen ().X);
- //Assert.Equal (0, found.FrameToScreen ().Y);
- Assert.Equal (new (11, 1), top.ScreenToFrame (new (14, 3)));
- screen = top.ViewportToScreen (new Point (11, 1));
- Assert.Equal (15, screen.X);
- Assert.Equal (4, screen.Y);
- Assert.Equal (top, View.FindDeepestView (top, new (14, 3)));
- // view
- Assert.Equal (new (-7, -5), view.ScreenToFrame (new (0, 0)));
- screen = view.Margin.ViewportToScreen (new Point (-6, -4));
- Assert.Equal (1, screen.X);
- Assert.Equal (1, screen.Y);
- screen = view.Border.ViewportToScreen (new Point (-6, -4));
- Assert.Equal (1, screen.X);
- Assert.Equal (1, screen.Y);
- screen = view.Padding.ViewportToScreen (new Point (-6, -4));
- Assert.Equal (1, screen.X);
- Assert.Equal (1, screen.Y);
- screen = view.ViewportToScreen (new Point (-6, -4));
- Assert.Equal (1, screen.X);
- Assert.Equal (1, screen.Y);
- Assert.Null (View.FindDeepestView (top, new (1, 1)));
- Assert.Equal (new (-4, -3), view.ScreenToFrame (new (3, 2)));
- screen = view.ViewportToScreen (new Point (-3, -2));
- Assert.Equal (4, screen.X);
- Assert.Equal (3, screen.Y);
- Assert.Equal (top, View.FindDeepestView (top, new (4, 3)));
- Assert.Equal (new (-1, -1), view.ScreenToFrame (new (6, 4)));
- screen = view.ViewportToScreen (new Point (0, 0));
- Assert.Equal (7, screen.X);
- Assert.Equal (5, screen.Y);
- Assert.Equal (view, View.FindDeepestView (top, new (7, 5)));
- Assert.Equal (new (6, -1), view.ScreenToFrame (new (13, 4)));
- screen = view.ViewportToScreen (new Point (7, 0));
- Assert.Equal (14, screen.X);
- Assert.Equal (5, screen.Y);
- Assert.Equal (view, View.FindDeepestView (top, new (14, 5)));
- Assert.Equal (new (7, -2), view.ScreenToFrame (new (14, 3)));
- screen = view.ViewportToScreen (new Point (8, -1));
- Assert.Equal (15, screen.X);
- Assert.Equal (4, screen.Y);
- Assert.Equal (top, View.FindDeepestView (top, new (15, 4)));
- Assert.Equal (new (16, -2), view.ScreenToFrame (new (23, 3)));
- screen = view.ViewportToScreen (new Point (17, -1));
- Assert.Equal (24, screen.X);
- Assert.Equal (4, screen.Y);
- Assert.Null (View.FindDeepestView (top, new (24, 4)));
- top.Dispose ();
- }
- [Fact]
- public void SendSubviewBackwards_Subviews_vs_TabIndexes ()
- {
- var r = new View ();
- var v1 = new View { CanFocus = true };
- var v2 = new View { CanFocus = true };
- var v3 = new View { CanFocus = true };
- r.Add (v1, v2, v3);
- r.SendSubviewBackwards (v3);
- Assert.True (r.Subviews.IndexOf (v1) == 0);
- Assert.True (r.Subviews.IndexOf (v2) == 2);
- Assert.True (r.Subviews.IndexOf (v3) == 1);
- Assert.True (r.TabIndexes.IndexOf (v1) == 0);
- Assert.True (r.TabIndexes.IndexOf (v2) == 1);
- Assert.True (r.TabIndexes.IndexOf (v3) == 2);
- r.Dispose ();
- }
- [Fact]
- public void SendSubviewToBack_Subviews_vs_TabIndexes ()
- {
- var r = new View ();
- var v1 = new View { CanFocus = true };
- var v2 = new View { CanFocus = true };
- var v3 = new View { CanFocus = true };
- r.Add (v1, v2, v3);
- r.SendSubviewToBack (v3);
- Assert.True (r.Subviews.IndexOf (v1) == 1);
- Assert.True (r.Subviews.IndexOf (v2) == 2);
- Assert.True (r.Subviews.IndexOf (v3) == 0);
- Assert.True (r.TabIndexes.IndexOf (v1) == 0);
- Assert.True (r.TabIndexes.IndexOf (v2) == 1);
- Assert.True (r.TabIndexes.IndexOf (v3) == 2);
- r.Dispose ();
- }
- [Fact]
- public void SetFocus_With_Null_Superview_Does_Not_Throw_Exception ()
- {
- var view = new View ()
- {
- CanFocus = true
- };
- Assert.True (view.CanFocus);
- Assert.False (view.HasFocus);
- Exception exception = Record.Exception (() => view.SetFocus());
- Assert.Null (exception);
- Assert.True (view.CanFocus);
- Assert.True (view.HasFocus);
- }
- [Fact]
- [AutoInitShutdown]
- public void SetHasFocus_Do_Not_Throws_If_OnLeave_Remove_Focused_Changing_To_Null ()
- {
- var view1Leave = false;
- var subView1Leave = false;
- var subView1subView1Leave = false;
- Toplevel top = new ();
- var view1 = new View { CanFocus = true };
- var subView1 = new View { CanFocus = true };
- var subView1subView1 = new View { CanFocus = true };
- view1.Leave += (s, e) => { view1Leave = true; };
- subView1.Leave += (s, e) =>
- {
- subView1.Remove (subView1subView1);
- subView1Leave = true;
- };
- view1.Add (subView1);
- subView1subView1.Leave += (s, e) =>
- {
- // This is never invoked
- subView1subView1Leave = true;
- };
- subView1.Add (subView1subView1);
- var view2 = new View { CanFocus = true };
- top.Add (view1, view2);
- RunState rs = Application.Begin (top);
- view2.SetFocus ();
- Assert.True (view1Leave);
- Assert.True (subView1Leave);
- Assert.False (subView1subView1Leave);
- Application.End (rs);
- subView1subView1.Dispose ();
- top.Dispose ();
- }
- [Fact]
- public void Subviews_TabIndexes_AreEqual ()
- {
- var r = new View ();
- var v1 = new View { CanFocus = true };
- var v2 = new View { CanFocus = true };
- var v3 = new View { CanFocus = true };
- r.Add (v1, v2, v3);
- Assert.True (r.Subviews.IndexOf (v1) == 0);
- Assert.True (r.Subviews.IndexOf (v2) == 1);
- Assert.True (r.Subviews.IndexOf (v3) == 2);
- Assert.True (r.TabIndexes.IndexOf (v1) == 0);
- Assert.True (r.TabIndexes.IndexOf (v2) == 1);
- Assert.True (r.TabIndexes.IndexOf (v3) == 2);
- Assert.Equal (r.Subviews.IndexOf (v1), r.TabIndexes.IndexOf (v1));
- Assert.Equal (r.Subviews.IndexOf (v2), r.TabIndexes.IndexOf (v2));
- Assert.Equal (r.Subviews.IndexOf (v3), r.TabIndexes.IndexOf (v3));
- r.Dispose ();
- }
- [Fact]
- public void TabIndex_Invert_Order ()
- {
- var r = new View ();
- var v1 = new View { Id = "1", CanFocus = true };
- var v2 = new View { Id = "2", CanFocus = true };
- var v3 = new View { Id = "3", CanFocus = true };
- r.Add (v1, v2, v3);
- v1.TabIndex = 2;
- v2.TabIndex = 1;
- v3.TabIndex = 0;
- Assert.True (r.TabIndexes.IndexOf (v1) == 2);
- Assert.True (r.TabIndexes.IndexOf (v2) == 1);
- Assert.True (r.TabIndexes.IndexOf (v3) == 0);
- Assert.True (r.Subviews.IndexOf (v1) == 0);
- Assert.True (r.Subviews.IndexOf (v2) == 1);
- Assert.True (r.Subviews.IndexOf (v3) == 2);
- }
- [Fact]
- public void TabIndex_Invert_Order_Added_One_By_One_Does_Not_Do_What_Is_Expected ()
- {
- var r = new View ();
- var v1 = new View { Id = "1", CanFocus = true };
- r.Add (v1);
- v1.TabIndex = 2;
- var v2 = new View { Id = "2", CanFocus = true };
- r.Add (v2);
- v2.TabIndex = 1;
- var v3 = new View { Id = "3", CanFocus = true };
- r.Add (v3);
- v3.TabIndex = 0;
- Assert.False (r.TabIndexes.IndexOf (v1) == 2);
- Assert.True (r.TabIndexes.IndexOf (v1) == 1);
- Assert.False (r.TabIndexes.IndexOf (v2) == 1);
- Assert.True (r.TabIndexes.IndexOf (v2) == 2);
- // Only the last is in the expected index
- Assert.True (r.TabIndexes.IndexOf (v3) == 0);
- Assert.True (r.Subviews.IndexOf (v1) == 0);
- Assert.True (r.Subviews.IndexOf (v2) == 1);
- Assert.True (r.Subviews.IndexOf (v3) == 2);
- }
- [Fact]
- public void TabIndex_Invert_Order_Mixed ()
- {
- var r = new View ();
- var vl1 = new View { Id = "vl1" };
- var v1 = new View { Id = "v1", CanFocus = true };
- var vl2 = new View { Id = "vl2" };
- var v2 = new View { Id = "v2", CanFocus = true };
- var vl3 = new View { Id = "vl3" };
- var v3 = new View { Id = "v3", CanFocus = true };
- r.Add (vl1, v1, vl2, v2, vl3, v3);
- v1.TabIndex = 2;
- v2.TabIndex = 1;
- v3.TabIndex = 0;
- Assert.True (r.TabIndexes.IndexOf (v1) == 4);
- Assert.True (r.TabIndexes.IndexOf (v2) == 2);
- Assert.True (r.TabIndexes.IndexOf (v3) == 0);
- Assert.True (r.Subviews.IndexOf (v1) == 1);
- Assert.True (r.Subviews.IndexOf (v2) == 3);
- Assert.True (r.Subviews.IndexOf (v3) == 5);
- }
- [Fact]
- public void TabIndex_Set_CanFocus_False ()
- {
- var r = new View ();
- var v1 = new View { CanFocus = true };
- var v2 = new View { CanFocus = true };
- var v3 = new View { CanFocus = true };
- r.Add (v1, v2, v3);
- v1.CanFocus = false;
- v1.TabIndex = 0;
- Assert.True (r.Subviews.IndexOf (v1) == 0);
- Assert.True (r.TabIndexes.IndexOf (v1) == 0);
- Assert.NotEqual (-1, v1.TabIndex);
- r.Dispose ();
- }
- [Fact]
- public void TabIndex_Set_CanFocus_False_To_True ()
- {
- var r = new View ();
- var v1 = new View ();
- var v2 = new View { CanFocus = true };
- var v3 = new View { CanFocus = true };
- r.Add (v1, v2, v3);
- v1.CanFocus = true;
- v1.TabIndex = 1;
- Assert.True (r.Subviews.IndexOf (v1) == 0);
- Assert.True (r.TabIndexes.IndexOf (v1) == 1);
- r.Dispose ();
- }
- [Fact]
- public void TabIndex_Set_CanFocus_HigherValues ()
- {
- var r = new View ();
- var v1 = new View { CanFocus = true };
- var v2 = new View { CanFocus = true };
- var v3 = new View { CanFocus = true };
- r.Add (v1, v2, v3);
- v1.TabIndex = 3;
- Assert.True (r.Subviews.IndexOf (v1) == 0);
- Assert.True (r.TabIndexes.IndexOf (v1) == 2);
- r.Dispose ();
- }
- [Fact]
- public void TabIndex_Set_CanFocus_LowerValues ()
- {
- var r = new View ();
- var v1 = new View { CanFocus = true };
- var v2 = new View { CanFocus = true };
- var v3 = new View { CanFocus = true };
- r.Add (v1, v2, v3);
- //v1.TabIndex = -1;
- Assert.True (r.Subviews.IndexOf (v1) == 0);
- Assert.True (r.TabIndexes.IndexOf (v1) == 0);
- r.Dispose ();
- }
- [Fact]
- public void TabIndex_Set_CanFocus_ValidValues ()
- {
- var r = new View ();
- var v1 = new View { CanFocus = true };
- var v2 = new View { CanFocus = true };
- var v3 = new View { CanFocus = true };
- r.Add (v1, v2, v3);
- v1.TabIndex = 1;
- Assert.True (r.Subviews.IndexOf (v1) == 0);
- Assert.True (r.TabIndexes.IndexOf (v1) == 1);
- v1.TabIndex = 2;
- Assert.True (r.Subviews.IndexOf (v1) == 0);
- Assert.True (r.TabIndexes.IndexOf (v1) == 2);
- r.Dispose ();
- }
- [Fact]
- public void TabStop_And_CanFocus_Are_All_True ()
- {
- var r = new View ();
- var v1 = new View { CanFocus = true };
- var v2 = new View { CanFocus = true };
- var v3 = new View { CanFocus = true };
- r.Add (v1, v2, v3);
- r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
- Assert.True (v1.HasFocus);
- Assert.False (v2.HasFocus);
- Assert.False (v3.HasFocus);
- r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
- Assert.False (v1.HasFocus);
- Assert.True (v2.HasFocus);
- Assert.False (v3.HasFocus);
- r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
- Assert.False (v1.HasFocus);
- Assert.False (v2.HasFocus);
- Assert.True (v3.HasFocus);
- r.Dispose ();
- }
- [Theory]
- [CombinatorialData]
- public void TabStop_And_CanFocus_Are_Decoupled (bool canFocus, TabBehavior tabStop)
- {
- var view = new View { CanFocus = canFocus, TabStop = tabStop };
- Assert.Equal (canFocus, view.CanFocus);
- Assert.Equal (tabStop, view.TabStop);
- }
- [Fact]
- public void TabStop_And_CanFocus_Mixed ()
- {
- var r = new View ();
- var v1 = new View { CanFocus = true, TabStop = TabBehavior.NoStop };
- var v2 = new View { CanFocus = false, TabStop = TabBehavior.TabStop };
- var v3 = new View { CanFocus = false, TabStop = TabBehavior.NoStop };
- r.Add (v1, v2, v3);
- r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
- Assert.False (v1.HasFocus);
- Assert.False (v2.HasFocus);
- Assert.False (v3.HasFocus);
- r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
- Assert.False (v1.HasFocus);
- Assert.False (v2.HasFocus);
- Assert.False (v3.HasFocus);
- r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
- Assert.False (v1.HasFocus);
- Assert.False (v2.HasFocus);
- Assert.False (v3.HasFocus);
- r.Dispose ();
- }
- [Theory]
- [CombinatorialData]
- public void TabStop_Change_CanFocus_Works ([CombinatorialValues (TabBehavior.NoStop, TabBehavior.TabStop, TabBehavior.TabGroup)] TabBehavior behavior)
- {
- var r = new View ();
- var v1 = new View ();
- var v2 = new View ();
- var v3 = new View ();
- Assert.False (v1.CanFocus);
- Assert.False (v2.CanFocus);
- Assert.False (v3.CanFocus);
- r.Add (v1, v2, v3);
- r.AdvanceFocus (NavigationDirection.Forward, behavior);
- Assert.False (v1.HasFocus);
- Assert.False (v2.HasFocus);
- Assert.False (v3.HasFocus);
- v1.CanFocus = true;
- v1.TabStop = behavior;
- r.AdvanceFocus (NavigationDirection.Forward, behavior);
- Assert.True (v1.HasFocus);
- Assert.False (v2.HasFocus);
- Assert.False (v3.HasFocus);
- v2.CanFocus = true;
- v2.TabStop = behavior;
- r.AdvanceFocus (NavigationDirection.Forward, behavior);
- Assert.False (v1.HasFocus);
- Assert.True (v2.HasFocus);
- Assert.False (v3.HasFocus);
- v3.CanFocus = true;
- v3.TabStop = behavior;
- r.AdvanceFocus (NavigationDirection.Forward, behavior);
- Assert.False (v1.HasFocus);
- Assert.False (v2.HasFocus);
- Assert.True (v3.HasFocus);
- r.Dispose ();
- }
- [Fact]
- public void TabStop_NoStop_And_CanFocus_True_No_Focus ()
- {
- var r = new View ();
- var v1 = new View { CanFocus = true, TabStop = TabBehavior.NoStop };
- var v2 = new View { CanFocus = true, TabStop = TabBehavior.NoStop };
- var v3 = new View { CanFocus = true, TabStop = TabBehavior.NoStop };
- r.Add (v1, v2, v3);
- r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
- Assert.False (v1.HasFocus);
- Assert.False (v2.HasFocus);
- Assert.False (v3.HasFocus);
- r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
- Assert.False (v1.HasFocus);
- Assert.False (v2.HasFocus);
- Assert.False (v3.HasFocus);
- r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
- Assert.False (v1.HasFocus);
- Assert.False (v2.HasFocus);
- Assert.False (v3.HasFocus);
- r.Dispose ();
- }
- [Fact]
- public void TabStop_NoStop_Change_Enables_Stop ()
- {
- var r = new View ();
- var v1 = new View { CanFocus = true, TabStop = TabBehavior.NoStop };
- var v2 = new View { CanFocus = true, TabStop = TabBehavior.NoStop };
- var v3 = new View { CanFocus = true, TabStop = TabBehavior.NoStop };
- r.Add (v1, v2, v3);
- v1.TabStop = TabBehavior.TabStop;
- r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
- Assert.True (v1.HasFocus);
- Assert.False (v2.HasFocus);
- Assert.False (v3.HasFocus);
- v2.TabStop = TabBehavior.TabStop;
- r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
- Assert.False (v1.HasFocus);
- Assert.True (v2.HasFocus);
- Assert.False (v3.HasFocus);
- v3.TabStop = TabBehavior.TabStop;
- r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
- Assert.False (v1.HasFocus);
- Assert.False (v2.HasFocus);
- Assert.True (v3.HasFocus);
- r.Dispose ();
- }
- [Fact]
- public void TabStop_NoStop_Prevents_Stop ()
- {
- var r = new View ();
- var v1 = new View { CanFocus = true, TabStop = TabBehavior.NoStop };
- var v2 = new View { CanFocus = true, TabStop = TabBehavior.NoStop };
- var v3 = new View { CanFocus = true, TabStop = TabBehavior.NoStop };
- r.Add (v1, v2, v3);
- r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
- Assert.False (v1.HasFocus);
- Assert.False (v2.HasFocus);
- Assert.False (v3.HasFocus);
- }
- [Fact]
- public void TabStop_Null_And_CanFocus_False_No_Advance ()
- {
- var r = new View ();
- var v1 = new View ();
- var v2 = new View ();
- var v3 = new View ();
- Assert.False (v1.CanFocus);
- Assert.Null (v1.TabStop);
- r.Add (v1, v2, v3);
- r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
- Assert.False (v1.HasFocus);
- Assert.False (v2.HasFocus);
- Assert.False (v3.HasFocus);
- r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
- Assert.False (v1.HasFocus);
- Assert.False (v2.HasFocus);
- Assert.False (v3.HasFocus);
- r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
- Assert.False (v1.HasFocus);
- Assert.False (v2.HasFocus);
- Assert.False (v3.HasFocus);
- r.Dispose ();
- }
- [Fact (Skip = "Causes crash on Ubuntu in Github Action. Bogus test anyway.")]
- public void WindowDispose_CanFocusProblem ()
- {
- // Arrange
- Application.Init ();
- using var top = new Toplevel ();
- using var view = new View { X = 0, Y = 1, Text = nameof (WindowDispose_CanFocusProblem) };
- using var window = new Window ();
- top.Add (window);
- window.Add (view);
- // Act
- RunState rs = Application.Begin (top);
- Application.End (rs);
- top.Dispose ();
- Application.Shutdown ();
- // Assert does Not throw NullReferenceException
- top.SetFocus ();
- }
- }
|