Browse Source

Add test for ensuring PosFactor preserves its type during keyboard splitter movement

tznind 2 years ago
parent
commit
a4d49d00eb
2 changed files with 73 additions and 24 deletions
  1. 11 9
      Terminal.Gui/Views/SplitContainer.cs
  2. 62 15
      UnitTests/SplitContainerTests.cs

+ 11 - 9
Terminal.Gui/Views/SplitContainer.cs

@@ -58,7 +58,7 @@ namespace Terminal.Gui {
 		/// </summary>
 		/// </summary>
 		protected virtual void OnSplitterMoved ()
 		protected virtual void OnSplitterMoved ()
 		{
 		{
-			SplitterMoved?.Invoke (this,new SplitterEventArgs(this,splitterDistance));
+			SplitterMoved?.Invoke (this, new SplitterEventArgs (this, splitterDistance));
 		}
 		}
 
 
 
 
@@ -127,15 +127,17 @@ namespace Terminal.Gui {
 
 
 
 
 		/// <summary>
 		/// <summary>
-		/// Distance Horizontally or Vertically to the splitter line when
+		/// <para>Distance Horizontally or Vertically to the splitter line when
 		/// neither panel is collapsed.
 		/// neither panel is collapsed.
+		/// </para>
+		/// <para>Only absolute values (e.g. 10) and percent values (i.e. <see cref="Pos.Percent(float)"/>)
+		/// are supported for this property.</para>
 		/// </summary>
 		/// </summary>
 		public Pos SplitterDistance {
 		public Pos SplitterDistance {
 			get { return splitterDistance; }
 			get { return splitterDistance; }
 			set {
 			set {
-				if(!(value is Pos.PosAbsolute) && !(value is Pos.PosFactor))
-				{
-					throw new ArgumentException($"Only Percent and Absolute values are supported for {nameof(SplitterDistance)} property.  Passed value was {value.GetType().Name}");
+				if (!(value is Pos.PosAbsolute) && !(value is Pos.PosFactor)) {
+					throw new ArgumentException ($"Only Percent and Absolute values are supported for {nameof (SplitterDistance)} property.  Passed value was {value.GetType ().Name}");
 				}
 				}
 
 
 				splitterDistance = value;
 				splitterDistance = value;
@@ -258,14 +260,14 @@ namespace Terminal.Gui {
 				// TODO: we should preserve Absolute/Percent status here not just force it to absolute
 				// TODO: we should preserve Absolute/Percent status here not just force it to absolute
 				return (Pos)Math.Min (panel1MinSizeAbs, availableSpace);
 				return (Pos)Math.Min (panel1MinSizeAbs, availableSpace);
 			}
 			}
-			
+
 			// bad position because not enough space for panel2
 			// bad position because not enough space for panel2
-			if(availableSpace - idealPosition <= panel2MinSizeAbs) {
+			if (availableSpace - idealPosition <= panel2MinSizeAbs) {
 
 
 				// TODO: we should preserve Absolute/Percent status here not just force it to absolute
 				// TODO: we should preserve Absolute/Percent status here not just force it to absolute
 
 
 				// +1 is to allow space for the splitter
 				// +1 is to allow space for the splitter
-				return (Pos)Math.Max (availableSpace - (panel2MinSizeAbs+1), 0);
+				return (Pos)Math.Max (availableSpace - (panel2MinSizeAbs + 1), 0);
 			}
 			}
 
 
 			// this splitter position is fine, there is enough space for everyone
 			// this splitter position is fine, there is enough space for everyone
@@ -278,7 +280,7 @@ namespace Terminal.Gui {
 			View toFullSize = panel1Collapsed ? Panel2 : Panel1;
 			View toFullSize = panel1Collapsed ? Panel2 : Panel1;
 
 
 			if (this.Subviews.Contains (splitterLine)) {
 			if (this.Subviews.Contains (splitterLine)) {
-				this.Remove(splitterLine);
+				this.Remove (splitterLine);
 			}
 			}
 			if (this.Subviews.Contains (toRemove)) {
 			if (this.Subviews.Contains (toRemove)) {
 				this.Remove (toRemove);
 				this.Remove (toRemove);

+ 62 - 15
UnitTests/SplitContainerTests.cs

@@ -14,8 +14,8 @@ namespace UnitTests {
 		}
 		}
 
 
 
 
-		[Fact,AutoInitShutdown]
-		public void TestSplitContainer_Vertical()
+		[Fact, AutoInitShutdown]
+		public void TestSplitContainer_Vertical ()
 		{
 		{
 			var splitContainer = Get11By3SplitContainer ();
 			var splitContainer = Get11By3SplitContainer ();
 			splitContainer.Redraw (splitContainer.Bounds);
 			splitContainer.Redraw (splitContainer.Bounds);
@@ -74,6 +74,53 @@ namespace UnitTests {
 			TestHelpers.AssertDriverContentsAre (looksLike, output);
 			TestHelpers.AssertDriverContentsAre (looksLike, output);
 		}
 		}
 
 
+		[Fact, AutoInitShutdown]
+		public void TestSplitContainer_Vertical_Focused_50PercentSplit ()
+		{
+			var splitContainer = Get11By3SplitContainer ();
+			splitContainer.EnsureFocus ();
+			splitContainer.FocusFirst ();
+			splitContainer.SplitterDistance = Pos.Percent (50);
+			Assert.IsType<Pos.PosFactor> (splitContainer.SplitterDistance);
+			splitContainer.Redraw (splitContainer.Bounds);
+
+			string looksLike =
+@"
+11111│22222
+     ◊
+     │     ";
+			TestHelpers.AssertDriverContentsAre (looksLike, output);
+
+			// Now while focused move the splitter 1 unit right
+			splitContainer.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ()));
+			splitContainer.Redraw (splitContainer.Bounds);
+
+			looksLike =
+@"
+111111│2222
+      ◊
+      │     ";
+			TestHelpers.AssertDriverContentsAre (looksLike, output);
+
+			// Even when moving the splitter location it should stay a Percentage based one
+			Assert.IsType<Pos.PosFactor> (splitContainer.SplitterDistance);
+
+
+			// and 2 to the left
+			splitContainer.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ()));
+			splitContainer.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ()));
+			splitContainer.Redraw (splitContainer.Bounds);
+
+			looksLike =
+@"
+1111│222222
+    ◊
+    │     ";
+			TestHelpers.AssertDriverContentsAre (looksLike, output);
+			// Even when moving the splitter location it should stay a Percentage based one
+			Assert.IsType<Pos.PosFactor> (splitContainer.SplitterDistance);
+		}
+
 		[Fact, AutoInitShutdown]
 		[Fact, AutoInitShutdown]
 		public void TestSplitContainer_Horizontal ()
 		public void TestSplitContainer_Horizontal ()
 		{
 		{
@@ -100,7 +147,7 @@ namespace UnitTests {
 		public void TestSplitContainer_Vertical_Panel1MinSize_Absolute ()
 		public void TestSplitContainer_Vertical_Panel1MinSize_Absolute ()
 		{
 		{
 			var splitContainer = Get11By3SplitContainer ();
 			var splitContainer = Get11By3SplitContainer ();
-			
+
 			splitContainer.EnsureFocus ();
 			splitContainer.EnsureFocus ();
 			splitContainer.FocusFirst ();
 			splitContainer.FocusFirst ();
 			splitContainer.Panel1MinSize = 6;
 			splitContainer.Panel1MinSize = 6;
@@ -109,7 +156,7 @@ namespace UnitTests {
 			splitContainer.SplitterDistance = 2;
 			splitContainer.SplitterDistance = 2;
 
 
 			// Should bound the value to the minimum distance
 			// Should bound the value to the minimum distance
-			Assert.Equal(6,splitContainer.SplitterDistance);
+			Assert.Equal (6, splitContainer.SplitterDistance);
 
 
 			splitContainer.Redraw (splitContainer.Bounds);
 			splitContainer.Redraw (splitContainer.Bounds);
 
 
@@ -132,7 +179,7 @@ namespace UnitTests {
 			splitContainer.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ()));
 			splitContainer.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ()));
 			splitContainer.SetNeedsDisplay ();
 			splitContainer.SetNeedsDisplay ();
 			splitContainer.Redraw (splitContainer.Bounds);
 			splitContainer.Redraw (splitContainer.Bounds);
-	
+
 			looksLike =
 			looksLike =
 @"
 @"
 1111111│222
 1111111│222
@@ -148,8 +195,8 @@ namespace UnitTests {
 			var splitContainer = Get11By3SplitContainer ();
 			var splitContainer = Get11By3SplitContainer ();
 
 
 			splitContainer.Orientation = Terminal.Gui.Graphs.Orientation.Horizontal;
 			splitContainer.Orientation = Terminal.Gui.Graphs.Orientation.Horizontal;
-			splitContainer.EnsureFocus();
-			splitContainer.FocusFirst();
+			splitContainer.EnsureFocus ();
+			splitContainer.FocusFirst ();
 
 
 			splitContainer.Redraw (splitContainer.Bounds);
 			splitContainer.Redraw (splitContainer.Bounds);
 
 
@@ -194,7 +241,7 @@ namespace UnitTests {
 
 
 			// 0 should not be allowed because it brings us below minimum size of Panel1
 			// 0 should not be allowed because it brings us below minimum size of Panel1
 			splitContainer.SplitterDistance = 0;
 			splitContainer.SplitterDistance = 0;
-			Assert.Equal((Pos)1,splitContainer.SplitterDistance);
+			Assert.Equal ((Pos)1, splitContainer.SplitterDistance);
 
 
 			splitContainer.Redraw (splitContainer.Bounds);
 			splitContainer.Redraw (splitContainer.Bounds);
 
 
@@ -232,16 +279,16 @@ namespace UnitTests {
 		{
 		{
 			var splitContainer = Get11By3SplitContainer ();
 			var splitContainer = Get11By3SplitContainer ();
 
 
-			var ex = Assert.Throws<ArgumentException>(()=>splitContainer.SplitterDistance = Pos.Right(splitContainer));
-			Assert.Equal("Only Percent and Absolute values are supported for SplitterDistance property.  Passed value was PosCombine",ex.Message);
+			var ex = Assert.Throws<ArgumentException> (() => splitContainer.SplitterDistance = Pos.Right (splitContainer));
+			Assert.Equal ("Only Percent and Absolute values are supported for SplitterDistance property.  Passed value was PosCombine", ex.Message);
 
 
 
 
-			ex = Assert.Throws<ArgumentException>(()=>splitContainer.SplitterDistance = Pos.Function(()=>1));
-			Assert.Equal("Only Percent and Absolute values are supported for SplitterDistance property.  Passed value was PosFunc",ex.Message);
+			ex = Assert.Throws<ArgumentException> (() => splitContainer.SplitterDistance = Pos.Function (() => 1));
+			Assert.Equal ("Only Percent and Absolute values are supported for SplitterDistance property.  Passed value was PosFunc", ex.Message);
 
 
 			// Also not allowed because this results in a PosCombine
 			// Also not allowed because this results in a PosCombine
-			ex = Assert.Throws<ArgumentException>(()=>splitContainer.SplitterDistance = Pos.Percent(50) - 1);
-			Assert.Equal("Only Percent and Absolute values are supported for SplitterDistance property.  Passed value was PosCombine",ex.Message);
+			ex = Assert.Throws<ArgumentException> (() => splitContainer.SplitterDistance = Pos.Percent (50) - 1);
+			Assert.Equal ("Only Percent and Absolute values are supported for SplitterDistance property.  Passed value was PosCombine", ex.Message);
 		}
 		}
 
 
 		private SplitContainer Get11By3SplitContainer ()
 		private SplitContainer Get11By3SplitContainer ()
@@ -250,7 +297,7 @@ namespace UnitTests {
 				Width = 11,
 				Width = 11,
 				Height = 3,
 				Height = 3,
 			};
 			};
-			
+
 			container.Panel1.Add (new Label (new string ('1', 100)));
 			container.Panel1.Add (new Label (new string ('1', 100)));
 			container.Panel2.Add (new Label (new string ('2', 100)));
 			container.Panel2.Add (new Label (new string ('2', 100)));