From a9d8ff19d8c02f342c91f7ab5cde83efb9a8df7d Mon Sep 17 00:00:00 2001 From: BDisp Date: Sat, 19 Jun 2021 00:26:57 +0100 Subject: [PATCH] Fixes #1344. Setting showBothScrollIndicator to false on the constructor don't throws NullReferenceException anymore. --- Terminal.Gui/Views/ScrollBarView.cs | 10 ++-- UnitTests/ScrollBarViewTests.cs | 75 ++++++++++++++++++++++++++++- 2 files changed, 80 insertions(+), 5 deletions(-) diff --git a/Terminal.Gui/Views/ScrollBarView.cs b/Terminal.Gui/Views/ScrollBarView.cs index 1b7a15205..c89070783 100644 --- a/Terminal.Gui/Views/ScrollBarView.cs +++ b/Terminal.Gui/Views/ScrollBarView.cs @@ -298,11 +298,15 @@ namespace Terminal.Gui { } var pending = CheckBothScrollBars (this); - CheckBothScrollBars (otherScrollBarView, pending); + if (otherScrollBarView != null) { + CheckBothScrollBars (otherScrollBarView, pending); + } SetWidthHeight (); SetRelativeLayout (Bounds); - OtherScrollBarView.SetRelativeLayout (OtherScrollBarView.Bounds); + if (otherScrollBarView != null) { + OtherScrollBarView.SetRelativeLayout (OtherScrollBarView.Bounds); + } if (showBothScrollIndicator) { if (contentBottomRightCorner != null) { @@ -321,7 +325,7 @@ namespace Terminal.Gui { if (showScrollIndicator) { Redraw (Bounds); } - if (otherScrollBarView.showScrollIndicator) { + if (otherScrollBarView != null && otherScrollBarView.showScrollIndicator) { otherScrollBarView.Redraw (otherScrollBarView.Bounds); } } diff --git a/UnitTests/ScrollBarViewTests.cs b/UnitTests/ScrollBarViewTests.cs index 7cda55699..36fccd7c8 100644 --- a/UnitTests/ScrollBarViewTests.cs +++ b/UnitTests/ScrollBarViewTests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using Xunit; @@ -6,7 +7,7 @@ using Xunit; namespace Terminal.Gui.Views { public class ScrollBarViewTests { - // This class enables test functions annoated with the [InitShutdown] attribute + // This class enables test functions annotated with the [InitShutdown] attribute // to have a function called before the test function is called and after. // // This is necessary because a) Application is a singleton and Init/Shutdown must be called @@ -55,7 +56,7 @@ namespace Terminal.Gui.Views { private static HostView _hostView; private ScrollBarView _scrollBar; - private bool _added; + private bool _added; private void AddHandlers () { @@ -444,5 +445,75 @@ namespace Terminal.Gui.Views { Assert.Equal ("Dim.Absolute(1)", _scrollBar.OtherScrollBarView.Height.ToString ()); Assert.Equal (1, _scrollBar.OtherScrollBarView.Bounds.Height); } + + [Fact] + public void Constructor_ShowBothScrollIndicator_False_Refresh_Does_Not_Throws_An_Object_Null_Exception () + { + var exception = Record.Exception (() => { + Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true))); + + var top = Application.Top; + + var win = new Window () { + X = 0, + Y = 0, + Width = Dim.Fill (), + Height = Dim.Fill () + }; + + List source = new List (); + + for (int i = 0; i < 50; i++) { + source.Add ($"item {i}"); + } + + var listView = new ListView (source) { + X = 0, + Y = 0, + Width = Dim.Fill (), + Height = Dim.Fill () + }; + win.Add (listView); + + var newScrollBarView = new ScrollBarView (listView, true, false) { + KeepContentAlwaysInViewport = true + }; + win.Add (newScrollBarView); + + newScrollBarView.ChangedPosition += () => { + listView.TopItem = newScrollBarView.Position; + if (listView.TopItem != newScrollBarView.Position) { + newScrollBarView.Position = listView.TopItem; + } + Assert.Equal (newScrollBarView.Position, listView.TopItem); + listView.SetNeedsDisplay (); + }; + + listView.DrawContent += (e) => { + newScrollBarView.Size = listView.Source.Count - 1; + Assert.Equal (newScrollBarView.Size, listView.Source.Count); + newScrollBarView.Position = listView.TopItem; + Assert.Equal (newScrollBarView.Position, listView.TopItem); + newScrollBarView.Refresh (); + }; + + top.Ready += () => { + newScrollBarView.Position = 45; + Assert.Equal (newScrollBarView.Position, newScrollBarView.Size - listView.TopItem + (listView.TopItem - listView.Bounds.Height)); + Assert.Equal (newScrollBarView.Position, listView.TopItem); + Assert.Equal (27, newScrollBarView.Position); + Assert.Equal (27, listView.TopItem); + Application.RequestStop (); + }; + + top.Add (win); + + Application.Run (); + + Application.Shutdown (); + + }); + Assert.Null (exception); + } } }