浏览代码

Fixed TreeTests

Tigger Kindel 2 年之前
父节点
当前提交
fa87e6dda5
共有 3 个文件被更改,包括 34 次插入16 次删除
  1. 9 6
      Terminal.Gui/Views/TextView.cs
  2. 6 1
      UnitTests/Views/TextViewTests.cs
  3. 19 9
      UnitTests/Views/TreeViewTests.cs

+ 9 - 6
Terminal.Gui/Views/TextView.cs

@@ -1412,9 +1412,10 @@ namespace Terminal.Gui {
 		void TextView_Initialized (object sender, EventArgs e)
 		{
 			Autocomplete.HostControl = this;
-
-			Application.Top.AlternateForwardKeyChanged += Top_AlternateForwardKeyChanged;
-			Application.Top.AlternateBackwardKeyChanged += Top_AlternateBackwardKeyChanged;
+			if (Application.Top != null) {
+				Application.Top.AlternateForwardKeyChanged += Top_AlternateForwardKeyChanged;
+				Application.Top.AlternateBackwardKeyChanged += Top_AlternateBackwardKeyChanged;
+			}
 			OnContentsChanged ();
 		}
 
@@ -1476,8 +1477,10 @@ namespace Terminal.Gui {
 			get => base.Frame;
 			set {
 				base.Frame = value;
-				WrapTextModel ();
-				Adjust ();
+				if (IsInitialized) {
+					WrapTextModel ();
+					Adjust ();
+				}
 			}
 		}
 
@@ -1855,7 +1858,7 @@ namespace Terminal.Gui {
 		/// </summary>
 		public override void PositionCursor ()
 		{
-			if (!CanFocus || !Enabled) {
+			if (!CanFocus || !Enabled || Application.Driver == null) {
 				return;
 			}
 

+ 6 - 1
UnitTests/Views/TextViewTests.cs

@@ -2028,6 +2028,7 @@ namespace Terminal.Gui.ViewTests {
 
 			Application.Top.Add (tv);
 
+			tv.LayoutSubviews ();
 			tv.Redraw (tv.Bounds);
 
 			TestHelpers.AssertDriverContentsWithFrameAre (@"
@@ -2110,7 +2111,7 @@ a
 			tv.WordWrap = true;
 
 			Application.Top.Add (tv);
-
+			Application.Top.LayoutSubviews ();
 			tv.Redraw (tv.Bounds);
 			TestHelpers.AssertDriverContentsWithFrameAre (@"
 This is  
@@ -2125,6 +2126,7 @@ line.
 			tv.ReadOnly = true;
 			tv.CursorPosition = new Point (6, 2);
 			Assert.Equal (new Point (5, 2), tv.CursorPosition);
+			Application.Top.LayoutSubviews ();
 			tv.Redraw (tv.Bounds);
 			TestHelpers.AssertDriverContentsWithFrameAre (@"
 This is  
@@ -6804,6 +6806,9 @@ This is the second line.
 				Width = 50,
 				Height = 10,
 			};
+			// BUGBUG: v2 - views must be initialzed before doing things. 
+			tv.BeginInit (); tv.EndInit ();
+			
 			tv.ContentsChanged += (s, e) => {
 				eventcount++;
 			};

+ 19 - 9
UnitTests/Views/TreeViewTests.cs

@@ -109,6 +109,8 @@ namespace Terminal.Gui.ViewTests {
 		public void ContentWidth_BiggerAfterExpand ()
 		{
 			var tree = CreateTree (out Factory f, out Car car1, out _);
+			tree.BeginInit (); tree.EndInit ();
+
 			tree.Bounds = new Rect (0, 0, 10, 10);
 
 			InitFakeDriver ();
@@ -134,6 +136,8 @@ namespace Terminal.Gui.ViewTests {
 		public void ContentWidth_VisibleVsAll ()
 		{
 			var tree = CreateTree (out Factory f, out Car car1, out Car car2);
+			tree.BeginInit (); tree.EndInit ();
+			
 			// control only allows 1 row to be viewed at once
 			tree.Bounds = new Rect (0, 0, 20, 1);
 
@@ -492,7 +496,7 @@ namespace Terminal.Gui.ViewTests {
 			bool called = false;
 
 			// register for the event
-			tree.ObjectActivated += (s,e) => {
+			tree.ObjectActivated += (s, e) => {
 				activated = e.ActivatedObject;
 				called = true;
 			};
@@ -521,6 +525,7 @@ namespace Terminal.Gui.ViewTests {
 		public void GoTo_OnlyAppliesToExposedObjects ()
 		{
 			var tree = CreateTree (out Factory f, out Car car1, out _);
+			tree.BeginInit (); tree.EndInit ();
 
 			// Make tree bounds 1 in height so that EnsureVisible always requires updating scroll offset
 			tree.Bounds = new Rect (0, 0, 50, 1);
@@ -565,7 +570,7 @@ namespace Terminal.Gui.ViewTests {
 			bool called = false;
 
 			// register for the event
-			tree.ObjectActivated += (s,e) => {
+			tree.ObjectActivated += (s, e) => {
 				activated = e.ActivatedObject;
 				called = true;
 			};
@@ -638,7 +643,7 @@ namespace Terminal.Gui.ViewTests {
 			bool called = false;
 
 			// register for the event
-			tree.ObjectActivated += (s,e) => {
+			tree.ObjectActivated += (s, e) => {
 				activated = e.ActivatedObject;
 				called = true;
 			};
@@ -670,7 +675,7 @@ namespace Terminal.Gui.ViewTests {
 			bool called = false;
 
 			// register for the event
-			tree.ObjectActivated += (s,e) => {
+			tree.ObjectActivated += (s, e) => {
 				activated = e.ActivatedObject;
 				called = true;
 			};
@@ -733,7 +738,7 @@ namespace Terminal.Gui.ViewTests {
 		public void TestGetObjectOnRow ()
 		{
 			var tv = new TreeView { Width = 20, Height = 10 };
-
+			tv.BeginInit (); tv.EndInit ();
 			var n1 = new TreeNode ("normal");
 			var n1_1 = new TreeNode ("pink");
 			var n1_2 = new TreeNode ("normal");
@@ -746,6 +751,7 @@ namespace Terminal.Gui.ViewTests {
 			tv.Expand (n1);
 
 			tv.ColorScheme = new ColorScheme ();
+			tv.LayoutSubviews ();
 			tv.Redraw (tv.Bounds);
 
 			TestHelpers.AssertDriverContentsAre (
@@ -795,6 +801,7 @@ namespace Terminal.Gui.ViewTests {
 			tv.Expand (n1);
 
 			tv.ColorScheme = new ColorScheme ();
+			tv.LayoutSubviews ();
 			tv.Redraw (tv.Bounds);
 
 			TestHelpers.AssertDriverContentsAre (
@@ -811,6 +818,7 @@ namespace Terminal.Gui.ViewTests {
 
 			tv.Collapse (n1);
 
+			tv.LayoutSubviews ();
 			tv.Redraw (tv.Bounds);
 
 
@@ -827,6 +835,7 @@ namespace Terminal.Gui.ViewTests {
 			// scroll down 1
 			tv.ScrollOffsetVertical = 1;
 
+			tv.LayoutSubviews ();
 			tv.Redraw (tv.Bounds);
 
 
@@ -858,17 +867,18 @@ namespace Terminal.Gui.ViewTests {
 			var hotpink = new Attribute (Color.BrightMagenta, Color.Black);
 
 			tv.ColorScheme = new ColorScheme ();
+			tv.LayoutSubviews ();
 			tv.Redraw (tv.Bounds);
 
 			// Normal drawing of the tree view
-			TestHelpers.AssertDriverContentsAre(
+			TestHelpers.AssertDriverContentsAre (
 @"├-normal
 │ ├─pink
 │ └─normal
 └─pink
 ", output);
 			// Should all be the same color
-			TestHelpers.AssertDriverColorsAre(
+			TestHelpers.AssertDriverColorsAre (
 @"00000000
 00000000
 0000000000
@@ -891,7 +901,7 @@ namespace Terminal.Gui.ViewTests {
 			tv.Redraw (tv.Bounds);
 
 			// Same text
-			TestHelpers.AssertDriverContentsAre(
+			TestHelpers.AssertDriverContentsAre (
 @"├-normal
 │ ├─pink
 │ └─normal
@@ -899,7 +909,7 @@ namespace Terminal.Gui.ViewTests {
 ", output);
 			// but now the item (only not lines) appear
 			// in pink when they are the word "pink"
-			TestHelpers.AssertDriverColorsAre(
+			TestHelpers.AssertDriverColorsAre (
 @"00000000
 00001111
 0000000000