فهرست منبع

Merge pull request #883 from mlaily/fix-nre

Fix  #882
Charlie Kindel 4 سال پیش
والد
کامیت
9954188bf3
3فایلهای تغییر یافته به همراه34 افزوده شده و 9 حذف شده
  1. 4 0
      Terminal.Gui/Core/Toplevel.cs
  2. 5 5
      UnitTests/ScenarioTests.cs
  3. 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);

+ 5 - 5
UnitTests/ScenarioTests.cs

@@ -5,7 +5,7 @@ using Terminal.Gui;
 using UICatalog;
 using Xunit;
 
-// Alais Console to MockConsole so we don't accidentally use Console
+// Alias Console to MockConsole so we don't accidentally use Console
 using Console = Terminal.Gui.FakeConsole;
 
 namespace Terminal.Gui {
@@ -32,18 +32,18 @@ namespace Terminal.Gui {
 		}
 
 		/// <summary>
-		/// This runs through all Sceanrios defined in UI Catalog, calling Init, Setup, and Run.
+		/// This runs through all Scenarios defined in UI Catalog, calling Init, Setup, and Run.
 		/// It puts a Ctrl-Q in the input queue so the Scenario immediately exits. 
 		/// Should find any egregious regressions.
 		/// </summary>
 		[Fact]
-		public void Run_All_Sceanrios ()
+		public void Run_All_Scenarios ()
 		{
 			List<Type> scenarioClasses = Scenario.GetDerivedClasses<Scenario> ();
 			Assert.NotEmpty (scenarioClasses);
 
 			foreach (var scenarioClass in scenarioClasses) {
-				// Setup some fake kepresses 
+				// Setup some fake keypresses 
 				// Passing empty string will cause just a ctrl-q to be fired
 				Console.MockKeyPresses.Clear ();
 				int stackSize = CreateInput ("");
@@ -95,7 +95,7 @@ namespace Terminal.Gui {
 
 			var item = scenarioClasses.FindIndex (t => Scenario.ScenarioMetadata.GetName (t).Equals ("Generic", StringComparison.OrdinalIgnoreCase));
 			var scenarioClass = scenarioClasses[item];
-			// Setup some fake kepresses 
+			// Setup some fake keypresses 
 			// Passing empty string will cause just a ctrl-q to be fired
 			int stackSize = CreateInput ("");
 

+ 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 ()
 		{