123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740 |
- using Xunit.Abstractions;
- namespace Terminal.Gui.ViewTests;
- public class HasFocusChangeEventTests (ITestOutputHelper _output) : TestsAllViews
- {
- [Fact]
- public void HasFocusChanging_SetFocus_Raises ()
- {
- var hasFocusTrueCount = 0;
- var hasFocusFalseCount = 0;
- var view = new View
- {
- Id = "view",
- CanFocus = true
- };
- view.HasFocusChanging += (s, e) =>
- {
- if (e.NewValue)
- {
- hasFocusTrueCount++;
- }
- else
- {
- hasFocusFalseCount++;
- }
- };
- Assert.True (view.CanFocus);
- Assert.False (view.HasFocus);
- view.SetFocus ();
- Assert.True (view.HasFocus);
- Assert.Equal (1, hasFocusTrueCount);
- Assert.Equal (0, hasFocusFalseCount);
- }
- [Fact]
- public void HasFocusChanging_SetFocus_SubView_SetFocus_Raises ()
- {
- var hasFocusTrueCount = 0;
- var hasFocusFalseCount = 0;
- var view = new View
- {
- Id = "view",
- CanFocus = true
- };
- view.HasFocusChanging += (s, e) =>
- {
- if (e.NewValue)
- {
- hasFocusTrueCount++;
- }
- else
- {
- hasFocusFalseCount++;
- }
- };
- var subviewHasFocusTrueCount = 0;
- var subviewHasFocusFalseCount = 0;
- var subview = new View
- {
- Id = "subview",
- CanFocus = true
- };
- subview.HasFocusChanging += (s, e) =>
- {
- if (e.NewValue)
- {
- subviewHasFocusTrueCount++;
- }
- else
- {
- subviewHasFocusFalseCount++;
- }
- };
- view.Add (subview);
- view.SetFocus ();
- Assert.Equal (1, hasFocusTrueCount);
- Assert.Equal (0, hasFocusFalseCount);
- Assert.Equal (1, subviewHasFocusTrueCount);
- Assert.Equal (0, subviewHasFocusFalseCount);
- }
- [Fact]
- public void HasFocusChanging_SetFocus_On_SubView_SubView_SetFocus_Raises ()
- {
- var hasFocusTrueCount = 0;
- var hasFocusFalseCount = 0;
- var view = new View
- {
- Id = "view",
- CanFocus = true
- };
- view.HasFocusChanging += (s, e) =>
- {
- if (e.NewValue)
- {
- hasFocusTrueCount++;
- }
- else
- {
- hasFocusFalseCount++;
- }
- };
- var subviewHasFocusTrueCount = 0;
- var subviewHasFocusFalseCount = 0;
- var subview = new View
- {
- Id = "subview",
- CanFocus = true
- };
- subview.HasFocusChanging += (s, e) =>
- {
- if (e.NewValue)
- {
- subviewHasFocusTrueCount++;
- }
- else
- {
- subviewHasFocusFalseCount++;
- }
- };
- view.Add (subview);
- subview.SetFocus ();
- Assert.Equal (1, hasFocusTrueCount);
- Assert.Equal (0, hasFocusFalseCount);
- Assert.Equal (1, subviewHasFocusTrueCount);
- Assert.Equal (0, subviewHasFocusFalseCount);
- }
- [Fact]
- public void HasFocusChanging_SetFocus_CompoundSubView_Raises ()
- {
- var hasFocusTrueCount = 0;
- var hasFocusFalseCount = 0;
- var view = new View
- {
- Id = "view",
- CanFocus = true
- };
- view.HasFocusChanging += (s, e) =>
- {
- if (e.NewValue)
- {
- hasFocusTrueCount++;
- }
- else
- {
- hasFocusFalseCount++;
- }
- };
- var subViewEnterCount = 0;
- var subViewLeaveCount = 0;
- var subView = new View
- {
- Id = "subView",
- CanFocus = true
- };
- subView.HasFocusChanging += (s, e) =>
- {
- if (e.NewValue)
- {
- subViewEnterCount++;
- }
- else
- {
- subViewLeaveCount++;
- }
- };
- var subviewSubView1EnterCount = 0;
- var subviewSubView1LeaveCount = 0;
- var subViewSubView1 = new View
- {
- Id = "subViewSubView1",
- CanFocus = false
- };
- subViewSubView1.HasFocusChanging += (s, e) =>
- {
- if (e.NewValue)
- {
- subviewSubView1EnterCount++;
- }
- else
- {
- subviewSubView1LeaveCount++;
- }
- };
- var subviewSubView2EnterCount = 0;
- var subviewSubView2LeaveCount = 0;
- var subViewSubView2 = new View
- {
- Id = "subViewSubView2",
- CanFocus = true
- };
- subViewSubView2.HasFocusChanging += (s, e) =>
- {
- if (e.NewValue)
- {
- subviewSubView2EnterCount++;
- }
- else
- {
- subviewSubView2EnterCount++;
- }
- };
- var subviewSubView3EnterCount = 0;
- var subviewSubView3LeaveCount = 0;
- var subViewSubView3 = new View
- {
- Id = "subViewSubView3",
- CanFocus = false
- };
- subViewSubView3.HasFocusChanging += (s, e) =>
- {
- if (e.NewValue)
- {
- subviewSubView3EnterCount++;
- }
- else
- {
- subviewSubView3LeaveCount++;
- }
- };
- subView.Add (subViewSubView1, subViewSubView2, subViewSubView3);
- view.Add (subView);
- view.SetFocus ();
- Assert.True (view.HasFocus);
- Assert.True (subView.HasFocus);
- Assert.False (subViewSubView1.HasFocus);
- Assert.True (subViewSubView2.HasFocus);
- Assert.False (subViewSubView3.HasFocus);
- Assert.Equal (1, hasFocusTrueCount);
- Assert.Equal (0, hasFocusFalseCount);
- Assert.Equal (1, subViewEnterCount);
- Assert.Equal (0, subViewLeaveCount);
- Assert.Equal (0, subviewSubView1EnterCount);
- Assert.Equal (0, subviewSubView1LeaveCount);
- Assert.Equal (1, subviewSubView2EnterCount);
- Assert.Equal (0, subviewSubView2LeaveCount);
- Assert.Equal (0, subviewSubView3EnterCount);
- Assert.Equal (0, subviewSubView3LeaveCount);
- }
- [Fact]
- public void HasFocusChanging_Can_Cancel ()
- {
- var hasFocusTrueCount = 0;
- var hasFocusFalseCount = 0;
- var view = new View
- {
- Id = "view",
- CanFocus = true
- };
- view.HasFocusChanging += (s, e) =>
- {
- if (e.NewValue)
- {
- hasFocusTrueCount++;
- e.Cancel = true;
- }
- else
- {
- hasFocusFalseCount++;
- }
- };
- var subviewHasFocusTrueCount = 0;
- var subviewHasFocusFalseCount = 0;
- var subview = new View
- {
- Id = "subview",
- CanFocus = true
- };
- subview.HasFocusChanging += (s, e) =>
- {
- if (e.NewValue)
- {
- subviewHasFocusTrueCount++;
- }
- else
- {
- subviewHasFocusFalseCount++;
- }
- };
- view.Add (subview);
- view.SetFocus ();
- Assert.False (view.HasFocus);
- Assert.False (subview.HasFocus);
- Assert.Equal (1, hasFocusTrueCount);
- Assert.Equal (0, hasFocusFalseCount);
- Assert.Equal (0, subviewHasFocusTrueCount);
- Assert.Equal (0, subviewHasFocusFalseCount);
- }
- [Fact]
- public void HasFocusChanging_SetFocus_On_SubView_Can_Cancel ()
- {
- var hasFocusTrueCount = 0;
- var hasFocusFalseCount = 0;
- var view = new View
- {
- Id = "view",
- CanFocus = true
- };
- view.HasFocusChanging += (s, e) =>
- {
- if (e.NewValue)
- {
- hasFocusTrueCount++;
- e.Cancel = true;
- }
- else
- {
- hasFocusFalseCount++;
- }
- };
- var subviewHasFocusTrueCount = 0;
- var subviewHasFocusFalseCount = 0;
- var subview = new View
- {
- Id = "subview",
- CanFocus = true
- };
- subview.HasFocusChanging += (s, e) =>
- {
- if (e.NewValue)
- {
- subviewHasFocusTrueCount++;
- }
- else
- {
- subviewHasFocusFalseCount++;
- }
- };
- view.Add (subview);
- subview.SetFocus ();
- Assert.False (view.HasFocus);
- Assert.False (subview.HasFocus);
- Assert.Equal (1, hasFocusTrueCount);
- Assert.Equal (0, hasFocusFalseCount);
- Assert.Equal (1, subviewHasFocusTrueCount);
- Assert.Equal (0, subviewHasFocusFalseCount);
- }
- [Fact]
- public void HasFocusChanging_SubView_Can_Cancel ()
- {
- var hasFocusTrueCount = 0;
- var hasFocusFalseCount = 0;
- var view = new View
- {
- Id = "view",
- CanFocus = true
- };
- view.HasFocusChanging += (s, e) =>
- {
- if (e.NewValue)
- {
- hasFocusTrueCount++;
- }
- else
- {
- hasFocusFalseCount++;
- }
- };
- var subviewHasFocusTrueCount = 0;
- var subviewHasFocusFalseCount = 0;
- var subview = new View
- {
- Id = "subview",
- CanFocus = true
- };
- subview.HasFocusChanging += (s, e) =>
- {
- if (e.NewValue)
- {
- subviewHasFocusTrueCount++;
- e.Cancel = true;
- }
- else
- {
- subviewHasFocusFalseCount++;
- }
- };
- view.Add (subview);
- view.SetFocus ();
- Assert.True (view.HasFocus);
- Assert.False (subview.HasFocus);
- Assert.Equal (1, hasFocusTrueCount);
- Assert.Equal (0, hasFocusFalseCount);
- Assert.Equal (1, subviewHasFocusTrueCount);
- Assert.Equal (0, subviewHasFocusFalseCount);
- }
- [Fact]
- public void HasFocusChanging_SetFocus_On_Subview_SubView_Can_Cancel ()
- {
- var hasFocusTrueCount = 0;
- var hasFocusFalseCount = 0;
- var view = new View
- {
- Id = "view",
- CanFocus = true
- };
- view.HasFocusChanging += (s, e) =>
- {
- if (e.NewValue)
- {
- hasFocusTrueCount++;
- }
- else
- {
- hasFocusFalseCount++;
- }
- };
- var subviewHasFocusTrueCount = 0;
- var subviewHasFocusFalseCount = 0;
- var subview = new View
- {
- Id = "subview",
- CanFocus = true
- };
- subview.HasFocusChanging += (s, e) =>
- {
- if (e.NewValue)
- {
- subviewHasFocusTrueCount++;
- e.Cancel = true;
- }
- else
- {
- subviewHasFocusFalseCount++;
- }
- };
- view.Add (subview);
- subview.SetFocus ();
- Assert.False (view.HasFocus);
- Assert.False (subview.HasFocus);
- Assert.Equal (0, hasFocusTrueCount);
- Assert.Equal (0, hasFocusFalseCount);
- Assert.Equal (1, subviewHasFocusTrueCount);
- Assert.Equal (0, subviewHasFocusFalseCount);
- }
- [Fact]
- public void RemoveFocus_Raises_HasFocusChanged ()
- {
- var nEnter = 0;
- var nLeave = 0;
- var view = new View
- {
- Id = "view",
- CanFocus = true
- };
- view.HasFocusChanging += (s, e) => nEnter++;
- view.HasFocusChanged += (s, e) => nLeave++;
- Assert.True (view.CanFocus);
- Assert.False (view.HasFocus);
- view.SetFocus ();
- Assert.True (view.HasFocus);
- Assert.Equal (1, nEnter);
- Assert.Equal (0, nLeave);
- view.HasFocus = false;
- Assert.Equal (1, nEnter);
- Assert.Equal (1, nLeave);
- }
- [Fact]
- public void RemoveFocus_SubView_Raises_HasFocusChanged ()
- {
- var viewEnterCount = 0;
- var viewLeaveCount = 0;
- var view = new View
- {
- Id = "view",
- CanFocus = true
- };
- view.HasFocusChanging += (s, e) => viewEnterCount++;
- view.HasFocusChanged += (s, e) => viewLeaveCount++;
- var subviewEnterCount = 0;
- var subviewLeaveCount = 0;
- var subview = new View
- {
- Id = "subview",
- CanFocus = true
- };
- subview.HasFocusChanging += (s, e) => subviewEnterCount++;
- subview.HasFocusChanged += (s, e) => subviewLeaveCount++;
- view.Add (subview);
- view.SetFocus ();
- view.HasFocus = false;
- Assert.Equal (1, viewEnterCount);
- Assert.Equal (1, viewLeaveCount);
- Assert.Equal (1, subviewEnterCount);
- Assert.Equal (1, subviewLeaveCount);
- view.SetFocus ();
- Assert.Equal (2, viewEnterCount);
- Assert.Equal (1, viewLeaveCount);
- Assert.Equal (2, subviewEnterCount);
- Assert.Equal (1, subviewLeaveCount);
- subview.HasFocus = false;
- Assert.Equal (2, viewEnterCount);
- Assert.Equal (2, viewLeaveCount);
- Assert.Equal (2, subviewEnterCount);
- Assert.Equal (2, subviewLeaveCount);
- }
- [Fact]
- public void RemoveFocus_CompoundSubView_Raises_HasFocusChanged ()
- {
- var viewEnterCount = 0;
- var viewLeaveCount = 0;
- var view = new View
- {
- Id = "view",
- CanFocus = true
- };
- view.HasFocusChanging += (s, e) => viewEnterCount++;
- view.HasFocusChanged += (s, e) => viewLeaveCount++;
- var subViewEnterCount = 0;
- var subViewLeaveCount = 0;
- var subView = new View
- {
- Id = "subView",
- CanFocus = true
- };
- subView.HasFocusChanging += (s, e) => subViewEnterCount++;
- subView.HasFocusChanged += (s, e) => subViewLeaveCount++;
- var subviewSubView1EnterCount = 0;
- var subviewSubView1LeaveCount = 0;
- var subViewSubView1 = new View
- {
- Id = "subViewSubView1",
- CanFocus = false
- };
- subViewSubView1.HasFocusChanging += (s, e) => subviewSubView1EnterCount++;
- subViewSubView1.HasFocusChanged += (s, e) => subviewSubView1LeaveCount++;
- var subviewSubView2EnterCount = 0;
- var subviewSubView2LeaveCount = 0;
- var subViewSubView2 = new View
- {
- Id = "subViewSubView2",
- CanFocus = true
- };
- subViewSubView2.HasFocusChanging += (s, e) => subviewSubView2EnterCount++;
- subViewSubView2.HasFocusChanged += (s, e) => subviewSubView2LeaveCount++;
- var subviewSubView3EnterCount = 0;
- var subviewSubView3LeaveCount = 0;
- var subViewSubView3 = new View
- {
- Id = "subViewSubView3",
- CanFocus = false
- };
- subViewSubView3.HasFocusChanging += (s, e) => subviewSubView3EnterCount++;
- subViewSubView3.HasFocusChanged += (s, e) => subviewSubView3LeaveCount++;
- subView.Add (subViewSubView1, subViewSubView2, subViewSubView3);
- view.Add (subView);
- view.SetFocus ();
- Assert.True (view.HasFocus);
- Assert.True (subView.HasFocus);
- Assert.False (subViewSubView1.HasFocus);
- Assert.True (subViewSubView2.HasFocus);
- Assert.False (subViewSubView3.HasFocus);
- view.HasFocus = false;
- Assert.False (view.HasFocus);
- Assert.False (subView.HasFocus);
- Assert.False (subViewSubView1.HasFocus);
- Assert.False (subViewSubView2.HasFocus);
- Assert.False (subViewSubView3.HasFocus);
- Assert.Equal (1, viewEnterCount);
- Assert.Equal (1, viewLeaveCount);
- Assert.Equal (1, subViewEnterCount);
- Assert.Equal (1, subViewLeaveCount);
- Assert.Equal (0, subviewSubView1EnterCount);
- Assert.Equal (0, subviewSubView1LeaveCount);
- Assert.Equal (1, subviewSubView2EnterCount);
- Assert.Equal (1, subviewSubView2LeaveCount);
- Assert.Equal (0, subviewSubView3EnterCount);
- Assert.Equal (0, subviewSubView3LeaveCount);
- }
- [Fact]
- public void HasFocus_False_Leave_Raised ()
- {
- var view = new View
- {
- Id = "view",
- CanFocus = true
- };
- Assert.True (view.CanFocus);
- Assert.False (view.HasFocus);
- var leaveInvoked = 0;
- view.HasFocusChanged += (s, e) => leaveInvoked++;
- view.SetFocus ();
- Assert.True (view.HasFocus);
- Assert.Equal (0, leaveInvoked);
- view.HasFocus = false;
- Assert.False (view.HasFocus);
- Assert.Equal (1, leaveInvoked);
- }
- [Fact]
- public void HasFocus_False_Leave_Raised_ForAllSubViews ()
- {
- var view = new View
- {
- Id = "view",
- CanFocus = true
- };
- var subview = new View
- {
- Id = "subview",
- CanFocus = true
- };
- view.Add (subview);
- var leaveInvoked = 0;
- view.HasFocusChanged += (s, e) => leaveInvoked++;
- subview.HasFocusChanged += (s, e) => leaveInvoked++;
- view.SetFocus ();
- Assert.True (view.HasFocus);
- Assert.Equal (0, leaveInvoked);
- view.HasFocus = false;
- Assert.False (view.HasFocus);
- Assert.False (subview.HasFocus);
- Assert.Equal (2, leaveInvoked);
- }
- }
|