Browse Source

Avoiding throwing exception on the "All Views Tester" scenario.

BDisp 4 years ago
parent
commit
0466a6ebc3
2 changed files with 15 additions and 2 deletions
  1. 7 2
      Terminal.Gui/Views/ScrollBarView.cs
  2. 8 0
      UnitTests/ScrollBarViewTests.cs

+ 7 - 2
Terminal.Gui/Views/ScrollBarView.cs

@@ -479,8 +479,10 @@ namespace Terminal.Gui {
 					Position = pos + 1;
 				}
 			} else if (location > 0 && location < barsize + 1) {
-				var b1 = pos * barsize / Size;
-				var b2 = KeepContentAlwaysInViewport ? Math.Min (((pos + barsize) * barsize / Size) + 1, barsize - 1) : (pos + barsize) * barsize / Size;
+				var b1 = pos * (Size > 0 ? barsize / Size : 0);
+				var b2 = Size > 0
+					? (KeepContentAlwaysInViewport ? Math.Min (((pos + barsize) * barsize / Size) + 1, barsize - 1) : (pos + barsize) * barsize / Size)
+					: 0;
 				if (KeepContentAlwaysInViewport && b1 == b2) {
 					b1 = Math.Max (b1 - 1, 0);
 				}
@@ -527,6 +529,9 @@ namespace Terminal.Gui {
 
 		internal bool CanScroll (int n, out int max, bool isVertical = false)
 		{
+			if (Host == null) {
+				throw new ArgumentNullException ("The host can't be null.");
+			}
 			var s = isVertical ?
 				(KeepContentAlwaysInViewport ? Host.Bounds.Height + (showBothScrollIndicator ? -2 : -1) : 0) :
 				(KeepContentAlwaysInViewport ? Host.Bounds.Width + (showBothScrollIndicator ? -2 : -1) : 0);

+ 8 - 0
UnitTests/ScrollBarViewTests.cs

@@ -117,6 +117,14 @@ namespace Terminal.Gui {
 			Assert.Throws<ArgumentException> (null, () => h.OtherScrollBarView = v);
 		}
 
+		[Fact]
+		public void Scrolling_With_Default_Constructor_Throws_ArgumentNullException ()
+		{
+			var sbv = new ScrollBarView ();
+
+			Assert.Throws<ArgumentNullException> ("The host can't be null.", () => sbv.Position = 1);
+		}
+
 		[Fact]
 		public void Hosting_Two_Horizontal_ScrollBarView_Throws_ArgumentException ()
 		{