Browse Source

Fix #882

Fix NullReferenceException during keyboard navigation
when `Focused` is null.
Melvyn Laïly 5 years ago
parent
commit
49ae8f1b21
2 changed files with 29 additions and 4 deletions
  1. 4 0
      Terminal.Gui/Core/Toplevel.cs
  2. 25 4
      UnitTests/ViewTests.cs

+ 4 - 0
Terminal.Gui/Core/Toplevel.cs

@@ -215,6 +215,10 @@ namespace Terminal.Gui {
 
 		View GetDeepestFocusedSubview (View view)
 		{
+			if (view == null) {
+				return null;
+			}
+
 			foreach (var v in view.Subviews) {
 				if (v.HasFocus) {
 					return GetDeepestFocusedSubview (v);

+ 25 - 4
UnitTests/ViewTests.cs

@@ -35,7 +35,7 @@ namespace Terminal.Gui {
 			Assert.Empty (r.Subviews);
 			Assert.False (r.WantContinuousButtonPressed);
 			Assert.False (r.WantMousePositionReports);
-			Assert.Null (r.GetEnumerator().Current);
+			Assert.Null (r.GetEnumerator ().Current);
 			Assert.Null (r.SuperView);
 			Assert.Null (r.MostFocused);
 
@@ -64,7 +64,7 @@ namespace Terminal.Gui {
 			Assert.Null (r.MostFocused);
 
 			// Rect with values
-			r = new View (new Rect(1, 2, 3, 4));
+			r = new View (new Rect (1, 2, 3, 4));
 			Assert.NotNull (r);
 			Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
 			Assert.Equal ("View()({X=1,Y=2,Width=3,Height=4})", r.ToString ());
@@ -115,7 +115,7 @@ namespace Terminal.Gui {
 			var sub1 = new View ();
 			root.Add (sub1);
 			var sub2 = new View ();
-			sub1.Width = Dim.Width(sub2);
+			sub1.Width = Dim.Width (sub2);
 
 			Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
 
@@ -551,7 +551,7 @@ namespace Terminal.Gui {
 
 			var t = new Toplevel () { Id = "0", };
 
-			var w = new Window () {Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
+			var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
 			var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
 			var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
 			var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
@@ -900,6 +900,27 @@ namespace Terminal.Gui {
 			Application.Shutdown ();
 		}
 
+
+		[Fact]
+		public void Navigation_With_Null_Focused_View ()
+		{
+			// Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
+
+			Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
+
+			Application.Top.Ready += () => {
+				Assert.Null (Application.Top.Focused);
+			};
+
+			// Keyboard navigation with tab
+			Console.MockKeyPresses.Push (new ConsoleKeyInfo ('\t', ConsoleKey.Tab, false, false, false));
+
+			Application.Iteration += () => Application.RequestStop ();
+
+			Application.Run ();
+			Application.Shutdown ();
+		}
+
 		[Fact]
 		public void Multi_Thread_Toplevels ()
 		{