123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956 |
- using Xunit.Abstractions;
- namespace Terminal.Gui.ViewsTests;
- public class ScrollTests
- {
- public ScrollTests (ITestOutputHelper output) { _output = output; }
- private readonly ITestOutputHelper _output;
- [Theory]
- [AutoInitShutdown]
- [InlineData (
- 20,
- @"
- █
- █
- █
- █
- █
- ░
- ░
- ░
- ░
- ░",
- @"
- ░
- ░
- █
- █
- █
- █
- █
- ░
- ░
- ░",
- @"
- ░
- ░
- ░
- ░
- ░
- █
- █
- █
- █
- █",
- @"
- ░
- ░
- █
- █
- █
- ░
- ░
- ░
- ░
- ░",
- @"
- █████░░░░░",
- @"
- ░░█████░░░",
- @"
- ░░░░░█████",
- @"
- ░░███░░░░░")]
- [InlineData (
- 40,
- @"
- █
- █
- █
- ░
- ░
- ░
- ░
- ░
- ░
- ░",
- @"
- ░
- █
- █
- █
- ░
- ░
- ░
- ░
- ░
- ░",
- @"
- ░
- ░
- █
- █
- █
- ░
- ░
- ░
- ░
- ░",
- @"
- ░
- █
- █
- ░
- ░
- ░
- ░
- ░
- ░
- ░",
- @"
- ███░░░░░░░",
- @"
- ░███░░░░░░",
- @"
- ░░███░░░░░",
- @"
- ░██░░░░░░░")]
- public void Changing_Position_Size_Orientation_Draws_Correctly (
- int size,
- string firstVertExpected,
- string middleVertExpected,
- string endVertExpected,
- string sizeVertExpected,
- string firstHoriExpected,
- string middleHoriExpected,
- string endHoriExpected,
- string sizeHoriExpected
- )
- {
- var scroll = new Scroll
- {
- Orientation = Orientation.Vertical,
- Size = size,
- Height = 10
- };
- var top = new Toplevel ();
- top.Add (scroll);
- Application.Begin (top);
- _ = TestHelpers.AssertDriverContentsWithFrameAre (firstVertExpected, _output);
- scroll.Position = 4;
- Application.Refresh ();
- _ = TestHelpers.AssertDriverContentsWithFrameAre (middleVertExpected, _output);
- scroll.Position = 10;
- Application.Refresh ();
- _ = TestHelpers.AssertDriverContentsWithFrameAre (endVertExpected, _output);
- scroll.Size = size * 2;
- Application.Refresh ();
- _ = TestHelpers.AssertDriverContentsWithFrameAre (sizeVertExpected, _output);
- scroll.Orientation = Orientation.Horizontal;
- scroll.Width = 10;
- scroll.Height = 1;
- scroll.Position = 0;
- scroll.Size = size;
- Application.Refresh ();
- _ = TestHelpers.AssertDriverContentsWithFrameAre (firstHoriExpected, _output);
- scroll.Position = 4;
- Application.Refresh ();
- _ = TestHelpers.AssertDriverContentsWithFrameAre (middleHoriExpected, _output);
- scroll.Position = 10;
- Application.Refresh ();
- _ = TestHelpers.AssertDriverContentsWithFrameAre (endHoriExpected, _output);
- scroll.Size = size * 2;
- Application.Refresh ();
- _ = TestHelpers.AssertDriverContentsWithFrameAre (sizeHoriExpected, _output);
- }
- [Fact]
- public void Constructor_Defaults ()
- {
- var scroll = new Scroll ();
- Assert.True (scroll.WantContinuousButtonPressed);
- Assert.False (scroll.CanFocus);
- Assert.Equal (Orientation.Vertical, scroll.Orientation);
- Assert.Equal (0, scroll.Size);
- Assert.Equal (0, scroll.Position);
- }
- [Theory]
- [AutoInitShutdown]
- [InlineData (
- Orientation.Vertical,
- 20,
- 10,
- 4,
- @"
- ░
- ░
- ░
- ░
- ░
- █
- █
- █
- █
- █",
- 0,
- @"
- █
- █
- █
- █
- █
- ░
- ░
- ░
- ░
- ░")]
- [InlineData (
- Orientation.Vertical,
- 40,
- 10,
- 5,
- @"
- ░
- ░
- █
- █
- █
- ░
- ░
- ░
- ░
- ░",
- 20,
- @"
- ░
- ░
- ░
- ░
- ░
- █
- █
- █
- ░
- ░")]
- [InlineData (
- Orientation.Horizontal,
- 20,
- 10,
- 4,
- @"
- ░░░░░█████",
- 0,
- @"
- █████░░░░░")]
- [InlineData (
- Orientation.Horizontal,
- 40,
- 10,
- 5,
- @"
- ░░███░░░░░",
- 20,
- @"
- ░░░░░███░░")]
- public void Mouse_On_The_Container (Orientation orientation, int size, int position, int location, string output, int expectedPos, string expectedOut)
- {
- var scroll = new Scroll
- {
- Width = orientation == Orientation.Vertical ? 1 : 10,
- Height = orientation == Orientation.Vertical ? 10 : 1,
- Orientation = orientation, Size = size,
- Position = position
- };
- var top = new Toplevel ();
- top.Add (scroll);
- Application.Begin (top);
- _ = TestHelpers.AssertDriverContentsWithFrameAre (output, _output);
- Application.OnMouseEvent (
- new ()
- {
- Position = orientation == Orientation.Vertical ? new (0, location) : new Point (location, 0),
- Flags = MouseFlags.Button1Pressed
- });
- Assert.Equal (expectedPos, scroll.Position);
- Application.Refresh ();
- _ = TestHelpers.AssertDriverContentsWithFrameAre (expectedOut, _output);
- }
- [Theory]
- [AutoInitShutdown]
- [InlineData (
- Orientation.Vertical,
- 20,
- 10,
- 5,
- 5,
- @"
- ░
- ░
- ░
- ░
- ░
- █
- █
- █
- █
- █",
- MouseFlags.Button1Pressed,
- 10,
- @"
- ░
- ░
- ░
- ░
- ░
- █
- █
- █
- █
- █")]
- [InlineData (
- Orientation.Vertical,
- 40,
- 10,
- 3,
- 3,
- @"
- ░
- ░
- █
- █
- █
- ░
- ░
- ░
- ░
- ░",
- MouseFlags.Button1Pressed,
- 10,
- @"
- ░
- ░
- █
- █
- █
- ░
- ░
- ░
- ░
- ░")]
- [InlineData (
- Orientation.Horizontal,
- 20,
- 10,
- 5,
- 5,
- @"
- ░░░░░█████",
- MouseFlags.Button1Pressed,
- 10,
- @"
- ░░░░░█████")]
- [InlineData (
- Orientation.Horizontal,
- 40,
- 10,
- 3,
- 3,
- @"
- ░░███░░░░░",
- MouseFlags.Button1Pressed,
- 10,
- @"
- ░░███░░░░░")]
- [InlineData (
- Orientation.Vertical,
- 20,
- 10,
- 5,
- 4,
- @"
- ░
- ░
- ░
- ░
- ░
- █
- █
- █
- █
- █",
- MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
- 8,
- @"
- ░
- ░
- ░
- ░
- █
- █
- █
- █
- █
- ░")]
- [InlineData (
- Orientation.Horizontal,
- 20,
- 10,
- 5,
- 4,
- @"
- ░░░░░█████",
- MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
- 8,
- @"
- ░░░░█████░")]
- [InlineData (
- Orientation.Vertical,
- 20,
- 10,
- 5,
- 6,
- @"
- ░
- ░
- ░
- ░
- ░
- █
- █
- █
- █
- █",
- MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
- 10,
- @"
- ░
- ░
- ░
- ░
- ░
- █
- █
- █
- █
- █")]
- [InlineData (
- Orientation.Horizontal,
- 20,
- 10,
- 5,
- 6,
- @"
- ░░░░░█████",
- MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
- 10,
- @"
- ░░░░░█████")]
- [InlineData (
- Orientation.Vertical,
- 40,
- 10,
- 2,
- 1,
- @"
- ░
- ░
- █
- █
- █
- ░
- ░
- ░
- ░
- ░",
- MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
- 4,
- @"
- ░
- █
- █
- █
- ░
- ░
- ░
- ░
- ░
- ░")]
- [InlineData (
- Orientation.Horizontal,
- 40,
- 10,
- 2,
- 1,
- @"
- ░░███░░░░░",
- MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
- 4,
- @"
- ░███░░░░░░")]
- [InlineData (
- Orientation.Vertical,
- 40,
- 10,
- 3,
- 4,
- @"
- ░
- ░
- █
- █
- █
- ░
- ░
- ░
- ░
- ░",
- MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
- 12,
- @"
- ░
- ░
- ░
- █
- █
- █
- ░
- ░
- ░
- ░")]
- [InlineData (
- Orientation.Horizontal,
- 40,
- 10,
- 3,
- 4,
- @"
- ░░███░░░░░",
- MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
- 12,
- @"
- ░░░███░░░░")]
- [InlineData (
- Orientation.Vertical,
- 40,
- 10,
- 2,
- 3,
- @"
- ░
- ░
- █
- █
- █
- ░
- ░
- ░
- ░
- ░",
- MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
- 12,
- @"
- ░
- ░
- ░
- █
- █
- █
- ░
- ░
- ░
- ░")]
- [InlineData (
- Orientation.Horizontal,
- 40,
- 10,
- 2,
- 3,
- @"
- ░░███░░░░░",
- MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
- 12,
- @"
- ░░░███░░░░")]
- [InlineData (
- Orientation.Vertical,
- 40,
- 10,
- 2,
- 4,
- @"
- ░
- ░
- █
- █
- █
- ░
- ░
- ░
- ░
- ░",
- MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
- 16,
- @"
- ░
- ░
- ░
- ░
- █
- █
- █
- ░
- ░
- ░")]
- [InlineData (
- Orientation.Horizontal,
- 40,
- 10,
- 2,
- 4,
- @"
- ░░███░░░░░",
- MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
- 16,
- @"
- ░░░░███░░░")]
- public void Mouse_On_The_Slider (
- Orientation orientation,
- int size,
- int position,
- int startLocation,
- int endLocation,
- string output,
- MouseFlags mouseFlags,
- int expectedPos,
- string expectedOut
- )
- {
- var scroll = new Scroll
- {
- Width = orientation == Orientation.Vertical ? 1 : 10,
- Height = orientation == Orientation.Vertical ? 10 : 1,
- Orientation = orientation,
- Size = size, Position = position
- };
- var top = new Toplevel ();
- top.Add (scroll);
- Application.Begin (top);
- _ = TestHelpers.AssertDriverContentsWithFrameAre (output, _output);
- Assert.Null (Application.MouseGrabView);
- if (mouseFlags.HasFlag (MouseFlags.ReportMousePosition))
- {
- MouseFlags mf = mouseFlags & ~MouseFlags.ReportMousePosition;
- Application.OnMouseEvent (
- new ()
- {
- Position = orientation == Orientation.Vertical ? new (0, startLocation) : new (startLocation, 0),
- Flags = mf
- });
- Application.OnMouseEvent (
- new ()
- {
- Position = orientation == Orientation.Vertical ? new (0, endLocation) : new (endLocation, 0),
- Flags = mouseFlags
- });
- }
- else
- {
- Assert.Equal (startLocation, endLocation);
- Application.OnMouseEvent (
- new ()
- {
- Position = orientation == Orientation.Vertical ? new (0, startLocation) : new (startLocation, 0),
- Flags = mouseFlags
- });
- }
- Assert.Equal ("scrollSlider", Application.MouseGrabView?.Id);
- Assert.Equal (expectedPos, scroll.Position);
- Application.Refresh ();
- _ = TestHelpers.AssertDriverContentsWithFrameAre (expectedOut, _output);
- Application.OnMouseEvent (
- new ()
- {
- Position = orientation == Orientation.Vertical ? new (0, startLocation) : new (startLocation, 0),
- Flags = MouseFlags.Button1Released
- });
- Assert.Null (Application.MouseGrabView);
- }
- [Theory]
- [AutoInitShutdown]
- [InlineData (Orientation.Vertical)]
- [InlineData (Orientation.Horizontal)]
- public void Moving_Mouse_Outside_Host_Ensures_Correct_Location (Orientation orientation)
- {
- var scroll = new Scroll
- {
- X = 10, Y = 10, Width = orientation == Orientation.Vertical ? 1 : 10, Height = orientation == Orientation.Vertical ? 10 : 1, Size = 20,
- Position = 5, Orientation = orientation
- };
- var top = new Toplevel ();
- top.Add (scroll);
- Application.Begin (top);
- Rectangle scrollSliderFrame = scroll.Subviews.FirstOrDefault (x => x.Id == "scrollSlider")!.Frame;
- Assert.Equal (scrollSliderFrame, orientation == Orientation.Vertical ? new (0, 2, 1, 5) : new (2, 0, 5, 1));
- Application.OnMouseEvent (new () { Position = orientation == Orientation.Vertical ? new (10, 12) : new (12, 10), Flags = MouseFlags.Button1Pressed });
- Application.OnMouseEvent (
- new ()
- {
- Position = orientation == Orientation.Vertical ? new (10, 0) : new (0, 10),
- Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
- });
- Assert.Equal (new (0, 0), scroll.Subviews.FirstOrDefault (x => x.Id == "scrollSlider")!.Frame.Location);
- Application.OnMouseEvent (
- new ()
- {
- Position = orientation == Orientation.Vertical ? new (0, 25) : new (80, 0),
- Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
- });
- Assert.Equal (
- orientation == Orientation.Vertical ? new (0, 5) : new (5, 0),
- scroll.Subviews.FirstOrDefault (x => x.Id == "scrollSlider")!.Frame.Location);
- }
- [Theory]
- [InlineData (Orientation.Vertical, 20, 10)]
- [InlineData (Orientation.Vertical, 40, 30)]
- public void Position_Cannot_Be_Negative_Nor_Greater_Than_Size_Minus_Frame_Length (Orientation orientation, int size, int expectedPos)
- {
- var scroll = new Scroll { Orientation = orientation, Height = 10, Size = size };
- Assert.Equal (0, scroll.Position);
- scroll.Position = -1;
- Assert.Equal (0, scroll.Position);
- scroll.Position = size;
- Assert.Equal (0, scroll.Position);
- scroll.Position = expectedPos;
- Assert.Equal (expectedPos, scroll.Position);
- }
- [Fact]
- public void PositionChanging_Cancelable_And_PositionChanged_Events ()
- {
- var changingCount = 0;
- var changedCount = 0;
- var scroll = new Scroll { Size = 10 };
- scroll.PositionChanging += (s, e) =>
- {
- if (changingCount == 0)
- {
- e.Cancel = true;
- }
- changingCount++;
- };
- scroll.PositionChanged += (s, e) => changedCount++;
- scroll.Position = 1;
- Assert.Equal (0, scroll.Position);
- Assert.Equal (1, changingCount);
- Assert.Equal (0, changedCount);
- scroll.Position = 1;
- Assert.Equal (1, scroll.Position);
- Assert.Equal (2, changingCount);
- Assert.Equal (1, changedCount);
- }
- [Fact]
- public void PositionChanging_PositionChanged_Events_Only_Raises_Once_If_Position_Was_Really_Changed ()
- {
- var changing = 0;
- var cancel = false;
- var changed = 0;
- var scroll = new Scroll { Height = 10, Size = 20 };
- scroll.PositionChanging += Scroll_PositionChanging;
- scroll.PositionChanged += Scroll_PositionChanged;
- Assert.Equal (Orientation.Vertical, scroll.Orientation);
- Assert.Equal (new (0, 0, 1, 10), scroll.Viewport);
- Assert.Equal (0, scroll.Position);
- Assert.Equal (0, changing);
- Assert.Equal (0, changed);
- scroll.Position = 0;
- Assert.Equal (0, scroll.Position);
- Assert.Equal (0, changing);
- Assert.Equal (0, changed);
- scroll.Position = 1;
- Assert.Equal (1, scroll.Position);
- Assert.Equal (1, changing);
- Assert.Equal (1, changed);
- Reset ();
- cancel = true;
- scroll.Position = 2;
- Assert.Equal (1, scroll.Position);
- Assert.Equal (1, changing);
- Assert.Equal (0, changed);
- Reset ();
- scroll.Position = 10;
- Assert.Equal (10, scroll.Position);
- Assert.Equal (1, changing);
- Assert.Equal (1, changed);
- Reset ();
- scroll.Position = 11;
- Assert.Equal (10, scroll.Position);
- Assert.Equal (0, changing);
- Assert.Equal (0, changed);
- Reset ();
- scroll.Position = 0;
- Assert.Equal (0, scroll.Position);
- Assert.Equal (1, changing);
- Assert.Equal (1, changed);
- scroll.PositionChanging -= Scroll_PositionChanging;
- scroll.PositionChanged -= Scroll_PositionChanged;
- void Scroll_PositionChanging (object sender, CancelEventArgs<int> e)
- {
- changing++;
- e.Cancel = cancel;
- }
- void Scroll_PositionChanged (object sender, EventArgs<int> e) { changed++; }
- void Reset ()
- {
- changing = 0;
- cancel = false;
- changed = 0;
- }
- }
- [Fact]
- public void SizeChanged_Event ()
- {
- var count = 0;
- var scroll = new Scroll ();
- scroll.SizeChanged += (s, e) => count++;
- scroll.Size = 10;
- Assert.Equal (10, scroll.Size);
- Assert.Equal (1, count);
- }
- [Theory]
- [AutoInitShutdown]
- [InlineData (
- 3,
- 10,
- 1,
- Orientation.Vertical,
- @"
- ┌─┐
- │█│
- │█│
- │█│
- │█│
- │░│
- │░│
- │░│
- │░│
- └─┘")]
- [InlineData (
- 10,
- 3,
- 1,
- Orientation.Horizontal,
- @"
- ┌────────┐
- │████░░░░│
- └────────┘")]
- [InlineData (
- 3,
- 10,
- 3,
- Orientation.Vertical,
- @"
- ┌───┐
- │███│
- │███│
- │███│
- │███│
- │░░░│
- │░░░│
- │░░░│
- │░░░│
- └───┘")]
- [InlineData (
- 10,
- 3,
- 3,
- Orientation.Horizontal,
- @"
- ┌────────┐
- │████░░░░│
- │████░░░░│
- │████░░░░│
- └────────┘")]
- public void Vertical_Horizontal_Draws_Correctly (int sizeWidth, int sizeHeight, int widthHeight, Orientation orientation, string expected)
- {
- var super = new Window { Id = "super", Width = Dim.Fill (), Height = Dim.Fill () };
- var top = new Toplevel ();
- top.Add (super);
- var scroll = new Scroll
- {
- Orientation = orientation,
- Size = orientation == Orientation.Vertical ? sizeHeight * 2 : sizeWidth * 2,
- Width = orientation == Orientation.Vertical ? widthHeight : Dim.Fill (),
- Height = orientation == Orientation.Vertical ? Dim.Fill () : widthHeight
- };
- super.Add (scroll);
- Application.Begin (top);
- ((FakeDriver)Application.Driver)!.SetBufferSize (
- sizeWidth + (orientation == Orientation.Vertical ? widthHeight - 1 : 0),
- sizeHeight + (orientation == Orientation.Vertical ? 0 : widthHeight - 1));
- _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
- }
- }
|