From caddcdb846f49ff40ad916dd1db9e3cd4bb297d4 Mon Sep 17 00:00:00 2001 From: tznind Date: Sun, 14 Apr 2024 21:46:50 +0100 Subject: [PATCH] Fix null reference removing tab --- Terminal.Gui/Views/TabView.cs | 2 +- UnitTests/Views/TabViewTests.cs | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Terminal.Gui/Views/TabView.cs b/Terminal.Gui/Views/TabView.cs index 8c7638a1da..aac2bcf94d 100644 --- a/Terminal.Gui/Views/TabView.cs +++ b/Terminal.Gui/Views/TabView.cs @@ -177,7 +177,7 @@ public Tab SelectedTab { if (old?.HasFocus == true) { - SelectedTab.SetFocus (); + SelectedTab?.SetFocus (); } OnSelectedTabChanged (old, value); diff --git a/UnitTests/Views/TabViewTests.cs b/UnitTests/Views/TabViewTests.cs index e798fda2de..fd3fa15c32 100644 --- a/UnitTests/Views/TabViewTests.cs +++ b/UnitTests/Views/TabViewTests.cs @@ -1269,6 +1269,26 @@ public void SwitchTabBy_OutOfTabsRange () 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)