123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- using System;
- using Xunit;
- namespace Terminal.Gui {
- public class ScrollBarViewTests {
- public class HostView : View {
- public int Top { get; set; }
- public int Lines { get; set; }
- public int Left { get; set; }
- public int Cols { get; set; }
- }
- private HostView _hostView;
- private ScrollBarView _vertical;
- private ScrollBarView _horizontal;
- private bool _added;
- public ScrollBarViewTests ()
- {
- Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
- var top = Application.Top;
- _hostView = new HostView () {
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- Top = 0,
- Lines = 30,
- Left = 0,
- Cols = 100
- };
- top.Add (_hostView);
- }
- private void AddHandlers ()
- {
- if (!_added) {
- _hostView.DrawContent += _hostView_DrawContent;
- _vertical.ChangedPosition += _vertical_ChangedPosition;
- _horizontal.ChangedPosition += _horizontal_ChangedPosition;
- }
- _added = true;
- }
- private void RemoveHandlers ()
- {
- if (_added) {
- _hostView.DrawContent -= _hostView_DrawContent;
- _vertical.ChangedPosition -= _vertical_ChangedPosition;
- _horizontal.ChangedPosition -= _horizontal_ChangedPosition;
- }
- _added = false;
- }
- private void _hostView_DrawContent (Rect obj)
- {
- _vertical.Size = _hostView.Lines;
- _vertical.Position = _hostView.Top;
- _horizontal.Size = _hostView.Cols;
- _horizontal.Position = _hostView.Left;
- _vertical.ColorScheme = _horizontal.ColorScheme = _hostView.ColorScheme;
- if (_vertical.ShowScrollIndicator) {
- _vertical.Redraw (obj);
- }
- if (_horizontal.ShowScrollIndicator) {
- _horizontal.Redraw (obj);
- }
- }
- private void _vertical_ChangedPosition ()
- {
- _hostView.Top = _vertical.Position;
- if (_hostView.Top != _vertical.Position) {
- _vertical.Position = _hostView.Top;
- }
- _hostView.SetNeedsDisplay ();
- }
- private void _horizontal_ChangedPosition ()
- {
- _hostView.Left = _horizontal.Position;
- if (_hostView.Left != _horizontal.Position) {
- _horizontal.Position = _hostView.Left;
- }
- _hostView.SetNeedsDisplay ();
- }
- [Fact]
- public void Hosting_A_Null_View_To_A_ScrollBarView_Throws_ArgumentNullException ()
- {
- Assert.Throws<ArgumentNullException> ("The host parameter can't be null.",
- () => new ScrollBarView (null, true));
- Assert.Throws<ArgumentNullException> ("The host parameter can't be null.",
- () => new ScrollBarView (null, false));
- }
- [Fact]
- public void Hosting_A_Null_SuperView_View_To_A_ScrollBarView_Throws_ArgumentNullException ()
- {
- Assert.Throws<ArgumentNullException> ("The host SuperView parameter can't be null.",
- () => new ScrollBarView (new View (), true));
- Assert.Throws<ArgumentNullException> ("The host SuperView parameter can't be null.",
- () => new ScrollBarView (new View (), false));
- }
- [Fact]
- public void Hosting_Two_Vertical_ScrollBarView_Throws_ArgumentException ()
- {
- var top = new Toplevel ();
- var host = new View ();
- top.Add (host);
- var v = new ScrollBarView (host, true);
- var h = new ScrollBarView (host, true);
- Assert.Throws<ArgumentException> (null, () => v.OtherScrollBarView = h);
- 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 ()
- {
- var top = new Toplevel ();
- var host = new View ();
- top.Add (host);
- var v = new ScrollBarView (host, false);
- var h = new ScrollBarView (host, false);
- Assert.Throws<ArgumentException> (null, () => v.OtherScrollBarView = h);
- Assert.Throws<ArgumentException> (null, () => h.OtherScrollBarView = v);
- }
- [Fact]
- public void Hosting_A_View_To_A_ScrollBarView ()
- {
- RemoveHandlers ();
- _vertical = new ScrollBarView (_hostView, true);
- _horizontal = new ScrollBarView (_hostView, false);
- _vertical.OtherScrollBarView = _horizontal;
- _horizontal.OtherScrollBarView = _vertical;
- Assert.True (_vertical.IsVertical);
- Assert.False (_horizontal.IsVertical);
- Assert.Equal (_vertical.Position, _hostView.Top);
- Assert.NotEqual (_vertical.Size, _hostView.Lines);
- Assert.Equal (_horizontal.Position, _hostView.Left);
- Assert.NotEqual (_horizontal.Size, _hostView.Cols);
- AddHandlers ();
- _hostView.SuperView.LayoutSubviews ();
- _hostView.Redraw (_hostView.Bounds);
- Assert.Equal (_vertical.Position, _hostView.Top);
- Assert.Equal (_vertical.Size, _hostView.Lines);
- Assert.Equal (_horizontal.Position, _hostView.Left);
- Assert.Equal (_horizontal.Size, _hostView.Cols);
- }
- [Fact]
- public void ChangedPosition_Update_The_Hosted_View ()
- {
- Hosting_A_View_To_A_ScrollBarView ();
- AddHandlers ();
- _vertical.Position = 2;
- Assert.Equal (_vertical.Position, _hostView.Top);
- _horizontal.Position = 5;
- Assert.Equal (_horizontal.Position, _hostView.Left);
- }
- [Fact]
- public void ChangedPosition_Scrolling ()
- {
- Hosting_A_View_To_A_ScrollBarView ();
- AddHandlers ();
- for (int i = 0; i < _vertical.Size; i++) {
- _vertical.Position += 1;
- Assert.Equal (_vertical.Position, _hostView.Top);
- }
- for (int i = _vertical.Size - 1; i >= 0; i--) {
- _vertical.Position -= 1;
- Assert.Equal (_vertical.Position, _hostView.Top);
- }
- for (int i = 0; i < _horizontal.Size; i++) {
- _horizontal.Position += i;
- Assert.Equal (_horizontal.Position, _hostView.Left);
- }
- for (int i = _horizontal.Size - 1; i >= 0; i--) {
- _horizontal.Position -= 1;
- Assert.Equal (_horizontal.Position, _hostView.Left);
- }
- }
- [Fact]
- public void ChangedPosition_Negative_Value ()
- {
- Hosting_A_View_To_A_ScrollBarView ();
- AddHandlers ();
- _vertical.Position = -20;
- Assert.Equal (0, _vertical.Position);
- Assert.Equal (_vertical.Position, _hostView.Top);
- _horizontal.Position = -50;
- Assert.Equal (0, _horizontal.Position);
- Assert.Equal (_horizontal.Position, _hostView.Left);
- }
- [Fact]
- public void DrawContent_Update_The_ScrollBarView_Position ()
- {
- Hosting_A_View_To_A_ScrollBarView ();
- AddHandlers ();
- _hostView.Top = 3;
- _hostView.Redraw (_hostView.Bounds);
- Assert.Equal (_vertical.Position, _hostView.Top);
- _hostView.Left = 6;
- _hostView.Redraw (_hostView.Bounds);
- Assert.Equal (_horizontal.Position, _hostView.Left);
- }
- [Fact]
- public void OtherScrollBarView_Not_Null ()
- {
- Hosting_A_View_To_A_ScrollBarView ();
- AddHandlers ();
- Assert.Equal (_vertical.OtherScrollBarView, _horizontal);
- Assert.Equal (_horizontal.OtherScrollBarView, _vertical);
- }
- [Fact]
- public void ShowScrollIndicator_Check ()
- {
- Hosting_A_View_To_A_ScrollBarView ();
- AddHandlers ();
- Assert.True (_vertical.ShowScrollIndicator);
- Assert.True (_horizontal.ShowScrollIndicator);
- }
- [Fact]
- public void KeepContentAlwaysInViewport_True ()
- {
- Hosting_A_View_To_A_ScrollBarView ();
- AddHandlers ();
- _vertical.Position = 50;
- Assert.Equal (_vertical.Position, _vertical.Size - _vertical.Bounds.Height + 1);
- Assert.Equal (_vertical.Position, _hostView.Top);
- _horizontal.Position = 150;
- Assert.Equal (_horizontal.Position, _horizontal.Size - _horizontal.Bounds.Width + 1);
- Assert.Equal (_horizontal.Position, _hostView.Left);
- }
- [Fact]
- public void KeepContentAlwaysInViewport_False ()
- {
- Hosting_A_View_To_A_ScrollBarView ();
- AddHandlers ();
- _vertical.KeepContentAlwaysInViewport = false;
- _vertical.Position = 50;
- Assert.Equal (_vertical.Position, _vertical.Size - 1);
- Assert.Equal (_vertical.Position, _hostView.Top);
- _horizontal.Position = 150;
- Assert.Equal (_horizontal.Position, _horizontal.Size - 1);
- Assert.Equal (_horizontal.Position, _hostView.Left);
- }
- [Fact]
- public void AutoHideScrollBars_Check ()
- {
- Hosting_A_View_To_A_ScrollBarView ();
- AddHandlers ();
- _hostView.Lines = 10;
- _hostView.Redraw (_hostView.Bounds);
- Assert.False (_vertical.ShowScrollIndicator);
- _hostView.Cols = 60;
- _hostView.Redraw (_hostView.Bounds);
- Assert.False (_horizontal.ShowScrollIndicator);
- _hostView.Lines = 40;
- _hostView.Redraw (_hostView.Bounds);
- Assert.True (_vertical.ShowScrollIndicator);
- _hostView.Cols = 120;
- _hostView.Redraw (_hostView.Bounds);
- Assert.True (_horizontal.ShowScrollIndicator);
- }
- }
- }
|