Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1989. ListView: Ensures SelectedItem visibility on MoveDown and MoveUp. #1990

Merged
merged 2 commits into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Terminal.Gui/Views/ListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,17 @@ public virtual bool MoveDown ()
top++;
} else if (selected < top) {
top = selected;
} else if (selected < top) {
top = selected;
}
OnSelectedChanged ();
SetNeedsDisplay ();
} else if (selected == 0) {
OnSelectedChanged ();
SetNeedsDisplay ();
} else if (selected >= top + Frame.Height) {
top = source.Count - Frame.Height;
SetNeedsDisplay ();
}

return true;
Expand Down Expand Up @@ -561,6 +566,9 @@ public virtual bool MoveUp ()
}
OnSelectedChanged ();
SetNeedsDisplay ();
} else if (selected < top) {
top = selected;
SetNeedsDisplay ();
}
return true;
}
Expand Down
210 changes: 210 additions & 0 deletions UnitTests/ListViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace Terminal.Gui.Views {
public class ListViewTests {
readonly ITestOutputHelper output;

public ListViewTests (ITestOutputHelper output)
{
this.output = output;
}

[Fact]
public void Constructors_Defaults ()
{
Expand Down Expand Up @@ -221,5 +229,207 @@ string GetContents (int line)
return item;
}
}

[Fact]
[AutoInitShutdown]
public void Ensures_Visibility_SelectedItem_On_MoveDown_And_MoveUp ()
{
var source = new List<string> ();
for (int i = 0; i < 20; i++) {
source.Add ($"Line{i}");
}
var lv = new ListView (source) { Width = Dim.Fill (), Height = Dim.Fill () };
var win = new Window ();
win.Add (lv);
Application.Top.Add (win);
Application.Begin (Application.Top);
((FakeDriver)Application.Driver).SetBufferSize (12, 12);
Application.Refresh ();

Assert.Equal (0, lv.SelectedItem);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
┌──────────┐
│Line0 │
│Line1 │
│Line2 │
│Line3 │
│Line4 │
│Line5 │
│Line6 │
│Line7 │
│Line8 │
│Line9 │
└──────────┘", output);

Assert.True (lv.ScrollDown (10));
lv.Redraw (lv.Bounds);
Assert.Equal (0, lv.SelectedItem);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
┌──────────┐
│Line10 │
│Line11 │
│Line12 │
│Line13 │
│Line14 │
│Line15 │
│Line16 │
│Line17 │
│Line18 │
│Line19 │
└──────────┘", output);

Assert.True (lv.MoveDown ());
lv.Redraw (lv.Bounds);
Assert.Equal (1, lv.SelectedItem);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
┌──────────┐
│Line1 │
│Line2 │
│Line3 │
│Line4 │
│Line5 │
│Line6 │
│Line7 │
│Line8 │
│Line9 │
│Line10 │
└──────────┘", output);

Assert.True (lv.MoveEnd ());
lv.Redraw (lv.Bounds);
Assert.Equal (19, lv.SelectedItem);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
┌──────────┐
│Line19 │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────┘", output);

Assert.True (lv.ScrollUp (20));
lv.Redraw (lv.Bounds);
Assert.Equal (19, lv.SelectedItem);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
┌──────────┐
│Line0 │
│Line1 │
│Line2 │
│Line3 │
│Line4 │
│Line5 │
│Line6 │
│Line7 │
│Line8 │
│Line9 │
└──────────┘", output);

Assert.True (lv.MoveDown ());
lv.Redraw (lv.Bounds);
Assert.Equal (19, lv.SelectedItem);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
┌──────────┐
│Line10 │
│Line11 │
│Line12 │
│Line13 │
│Line14 │
│Line15 │
│Line16 │
│Line17 │
│Line18 │
│Line19 │
└──────────┘", output);

Assert.True (lv.ScrollUp (20));
lv.Redraw (lv.Bounds);
Assert.Equal (19, lv.SelectedItem);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
┌──────────┐
│Line0 │
│Line1 │
│Line2 │
│Line3 │
│Line4 │
│Line5 │
│Line6 │
│Line7 │
│Line8 │
│Line9 │
└──────────┘", output);

Assert.True (lv.MoveDown ());
lv.Redraw (lv.Bounds);
Assert.Equal (19, lv.SelectedItem);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
┌──────────┐
│Line10 │
│Line11 │
│Line12 │
│Line13 │
│Line14 │
│Line15 │
│Line16 │
│Line17 │
│Line18 │
│Line19 │
└──────────┘", output);

Assert.True (lv.MoveHome ());
lv.Redraw (lv.Bounds);
Assert.Equal (0, lv.SelectedItem);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
┌──────────┐
│Line0 │
│Line1 │
│Line2 │
│Line3 │
│Line4 │
│Line5 │
│Line6 │
│Line7 │
│Line8 │
│Line9 │
└──────────┘", output);

Assert.True (lv.ScrollDown (20));
lv.Redraw (lv.Bounds);
Assert.Equal (0, lv.SelectedItem);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
┌──────────┐
│Line19 │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────┘", output);

Assert.True (lv.MoveUp ());
lv.Redraw (lv.Bounds);
Assert.Equal (0, lv.SelectedItem);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
┌──────────┐
│Line0 │
│Line1 │
│Line2 │
│Line3 │
│Line4 │
│Line5 │
│Line6 │
│Line7 │
│Line8 │
│Line9 │
└──────────┘", output);
}
}
}