diff --git a/Terminal.Gui/Views/TabView.cs b/Terminal.Gui/Views/TabView.cs index 8c7638a1d..aac2bcf94 100644 --- a/Terminal.Gui/Views/TabView.cs +++ b/Terminal.Gui/Views/TabView.cs @@ -177,7 +177,7 @@ public class TabView : View { if (old?.HasFocus == true) { - SelectedTab.SetFocus (); + SelectedTab?.SetFocus (); } OnSelectedTabChanged (old, value); diff --git a/UnitTests/Views/TabViewTests.cs b/UnitTests/Views/TabViewTests.cs index e798fda2d..fd3fa15c3 100644 --- a/UnitTests/Views/TabViewTests.cs +++ b/UnitTests/Views/TabViewTests.cs @@ -1269,6 +1269,26 @@ public class TabViewTests Application.Shutdown (); } + [Fact] + public void RemoveTab_ThatHasFocus () + { + TabView tv = GetTabView (out Tab _, out Tab tab2); + + tv.SelectedTab = tab2; + tab2.HasFocus = true; + + Assert.Equal (2, tv.Tabs.Count); + foreach (var t in tv.Tabs.ToArray ()) + { + tv.RemoveTab (t); + } + + Assert.Empty (tv.Tabs); + + // Shutdown must be called to safely clean up Application if Init has been called + Application.Shutdown (); + } + private TabView GetTabView () { return GetTabView (out _, out _); } private TabView GetTabView (out Tab tab1, out Tab tab2, bool initFakeDriver = true)