Pārlūkot izejas kodu

Added more tests and fixed GetContentWidth when Bounds have 0 Height

tznind 4 gadi atpakaļ
vecāks
revīzija
1a00376885
2 mainītis faili ar 111 papildinājumiem un 2 dzēšanām
  1. 5 1
      Terminal.Gui/Views/TreeView.cs
  2. 106 1
      UnitTests/TreeViewTests.cs

+ 5 - 1
Terminal.Gui/Views/TreeView.cs

@@ -537,7 +537,11 @@ namespace Terminal.Gui {
 			if(visible){
 			if(visible){
 
 
 				//Somehow we managed to scroll off the end of the control
 				//Somehow we managed to scroll off the end of the control
-				if(ScrollOffsetVertical > map.Length)
+				if(ScrollOffsetVertical >= map.Length)
+					return 0;
+
+				// If control has no height to it then there is no visible area for content
+				if(Bounds.Height == 0)
 					return 0;
 					return 0;
 
 
 				return map.Skip(ScrollOffsetVertical).Take(Bounds.Height).Max(b=>b.GetWidth(Driver));
 				return map.Skip(ScrollOffsetVertical).Take(Bounds.Height).Max(b=>b.GetWidth(Driver));

+ 106 - 1
UnitTests/TreeViewTests.cs

@@ -13,9 +13,17 @@ namespace UnitTests {
 		class Factory
 		class Factory
 		{
 		{
 			public Car[] Cars {get;set;}
 			public Car[] Cars {get;set;}
+			public override string ToString ()
+			{
+				return "Factory";
+			}
 		};
 		};
 		class Car {
 		class Car {
-
+			public string Name{get;set;}
+			public override string ToString ()
+			{
+				return Name;
+			}
 		};
 		};
 		
 		
 		private TreeView<object> CreateTree()
 		private TreeView<object> CreateTree()
@@ -56,6 +64,103 @@ namespace UnitTests {
 			Assert.False(tree.IsExpanded(f));
 			Assert.False(tree.IsExpanded(f));
 		}
 		}
 
 
+		[Fact]
+		public void EmptyTreeView_ContentSizes()
+		{
+			var emptyTree = new TreeView();
+			Assert.Equal(0,emptyTree.ContentHeight);
+			Assert.Equal(0,emptyTree.GetContentWidth(true));
+			Assert.Equal(0,emptyTree.GetContentWidth(false));
+		}
+		[Fact]
+		public void EmptyTreeViewGeneric_ContentSizes()
+		{
+			var emptyTree = new TreeView<string>();
+			Assert.Equal(0,emptyTree.ContentHeight);
+			Assert.Equal(0,emptyTree.GetContentWidth(true));
+			Assert.Equal(0,emptyTree.GetContentWidth(false));
+		}
+		
+		/// <summary>
+		/// Tests that <see cref="TreeView.Expand(object)"/> results in a correct content height
+		/// </summary>
+		[Fact]
+		public void ContentHeight_BiggerAfterExpand()
+		{
+			var tree = CreateTree(out Factory f, out _, out _);
+			Assert.Equal(1,tree.ContentHeight);
+
+			tree.Expand(f);
+			Assert.Equal(3,tree.ContentHeight);
+
+			tree.Collapse(f);
+			Assert.Equal(1,tree.ContentHeight);
+		}
+
+		[Fact]
+		public void ContentWidth_BiggerAfterExpand()
+		{
+			var tree = CreateTree(out Factory f, out Car car1, out _);
+			tree.Bounds = new Rect(0,0,10,10);
+			
+			var driver = new FakeDriver ();
+			Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
+			driver.Init (() => { });
+
+			//-+Factory
+			Assert.Equal(9,tree.GetContentWidth(true));
+
+			car1.Name = "123456789";
+
+			tree.Expand(f);
+
+			//..├-123456789
+			Assert.Equal(13,tree.GetContentWidth(true));
+
+			tree.Collapse(f);
+			//-+Factory
+			Assert.Equal(9,tree.GetContentWidth(true));
+		}
+		
+		[Fact]
+		public void ContentWidth_VisibleVsAll()
+		{
+			var tree = CreateTree(out Factory f, out Car car1, out Car car2);
+			// control only allows 1 row to be viewed at once
+			tree.Bounds = new Rect(0,0,20,1);
+			
+			var driver = new FakeDriver ();
+			Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
+			driver.Init (() => { });
+
+			//-+Factory
+			Assert.Equal(9,tree.GetContentWidth(true));
+			Assert.Equal(9,tree.GetContentWidth(false));
+
+			car1.Name = "123456789";
+			car2.Name = "12345678";
+
+			tree.Expand(f);
+			
+			// Although expanded the bigger (longer) child node is not in the rendered area of the control
+			Assert.Equal(9,tree.GetContentWidth(true));
+			Assert.Equal(13,tree.GetContentWidth(false)); // If you ask for the global max width it includes the longer child
+
+			// Now that we have scrolled down 1 row we should see the big child
+			tree.ScrollOffsetVertical = 1;
+			Assert.Equal(13,tree.GetContentWidth(true));
+			Assert.Equal(13,tree.GetContentWidth(false));
+			
+			// Scroll down so only car2 is visible
+			tree.ScrollOffsetVertical = 2;
+			Assert.Equal(12,tree.GetContentWidth(true));
+			Assert.Equal(13,tree.GetContentWidth(false));
+			
+			// Scroll way down (off bottom of control even)
+			tree.ScrollOffsetVertical = 5;
+			Assert.Equal(0,tree.GetContentWidth(true));
+			Assert.Equal(13,tree.GetContentWidth(false));
+		}
 		/// <summary>
 		/// <summary>
 		/// Tests that <see cref="TreeView.IsExpanded(object)"/> and <see cref="TreeView.Expand(object)"/> behaves correctly when an object cannot be expanded (because it has no children)
 		/// Tests that <see cref="TreeView.IsExpanded(object)"/> and <see cref="TreeView.Expand(object)"/> behaves correctly when an object cannot be expanded (because it has no children)
 		/// </summary>
 		/// </summary>