1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-22 08:52:55 +08:00

Add test coverage

This commit is contained in:
Salman Alshamrani 2025-02-05 04:46:05 -05:00
parent c389dbc711
commit 6037d5d8ce
2 changed files with 115 additions and 17 deletions

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Testing;
@ -8,6 +9,8 @@ using osu.Game.Beatmaps;
using osu.Game.Screens.Select;
using osu.Game.Screens.Select.Filter;
using osu.Game.Screens.SelectV2;
using osuTK;
using osuTK.Input;
namespace osu.Game.Tests.Visual.SongSelect
{
@ -154,5 +157,64 @@ namespace osu.Game.Tests.Visual.SongSelect
SelectPrevGroup();
WaitForGroupSelection(2, 9);
}
[Test]
public void TestInputHandlingWithinGaps()
{
AddBeatmaps(5, 2);
WaitForDrawablePanels();
SelectNextGroup();
clickOnPanel(0, 1, p => p.LayoutRectangle.TopLeft + new Vector2(20f, -1f));
WaitForGroupSelection(0, 1);
clickOnPanel(0, 0, p => p.LayoutRectangle.BottomLeft + new Vector2(20f, 1f));
WaitForGroupSelection(0, 0);
SelectNextPanel();
Select();
WaitForGroupSelection(0, 1);
clickOnGroup(0, p => p.LayoutRectangle.BottomLeft + new Vector2(20f, 1f));
AddAssert("group 0 collapsed", () => this.ChildrenOfType<GroupPanel>().OrderBy(g => g.Y).ElementAt(0).Expanded.Value, () => Is.False);
clickOnGroup(0, p => p.LayoutRectangle.Centre);
AddAssert("group 0 expanded", () => this.ChildrenOfType<GroupPanel>().OrderBy(g => g.Y).ElementAt(0).Expanded.Value, () => Is.True);
AddStep("scroll to end", () => Scroll.ScrollToEnd(false));
clickOnPanel(0, 4, p => p.LayoutRectangle.BottomLeft + new Vector2(20f, 1f));
WaitForGroupSelection(0, 4);
clickOnGroup(1, p => p.LayoutRectangle.TopLeft + new Vector2(20f, -1f));
AddAssert("group 1 expanded", () => this.ChildrenOfType<GroupPanel>().OrderBy(g => g.Y).ElementAt(1).Expanded.Value, () => Is.True);
}
private void clickOnGroup(int group, Func<GroupPanel, Vector2> pos)
{
AddStep($"click on group{group}", () =>
{
var groupingFilter = Carousel.Filters.OfType<BeatmapCarouselFilterGrouping>().Single();
var model = groupingFilter.GroupItems.Keys.ElementAt(group);
var panel = this.ChildrenOfType<GroupPanel>().Single(b => ReferenceEquals(b.Item!.Model, model));
InputManager.MoveMouseTo(panel.ToScreenSpace(pos(panel)));
InputManager.Click(MouseButton.Left);
});
}
private void clickOnPanel(int group, int panel, Func<BeatmapPanel, Vector2> pos)
{
AddStep($"click on group{group} panel{panel}", () =>
{
var groupingFilter = Carousel.Filters.OfType<BeatmapCarouselFilterGrouping>().Single();
var g = groupingFilter.GroupItems.Keys.ElementAt(group);
// offset by one because the group itself is included in the items list.
object model = groupingFilter.GroupItems[g].ElementAt(panel + 1).Model;
var p = this.ChildrenOfType<BeatmapPanel>().Single(b => ReferenceEquals(b.Item!.Model, model));
InputManager.MoveMouseTo(p.ToScreenSpace(pos(p)));
InputManager.Click(MouseButton.Left);
});
}
}
}

View File

@ -1,11 +1,13 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
using osu.Game.Screens.SelectV2;
using osuTK;
using osuTK.Input;
namespace osu.Game.Tests.Visual.SongSelect
@ -94,9 +96,8 @@ namespace osu.Game.Tests.Visual.SongSelect
AddStep("add previous selection", () => BeatmapSets.Add(((BeatmapInfo)selection!).BeatmapSet!));
AddAssert("selection matches original carousel selection", () => selection, () => Is.EqualTo(Carousel.CurrentSelection));
AddUntilStep("drawable selection restored", () => getSelectedPanel()?.Item?.Model, () => Is.EqualTo(selection));
AddAssert("carousel item is visible", () => getSelectedPanel()?.Item?.IsVisible, () => Is.True);
AddAssert("drawable selection matches carousel selection", () => selection, () => Is.EqualTo(Carousel.CurrentSelection));
BeatmapPanel? getSelectedPanel() => Carousel.ChildrenOfType<BeatmapPanel>().SingleOrDefault(p => p.Selected.Value);
}
@ -129,21 +130,6 @@ namespace osu.Game.Tests.Visual.SongSelect
WaitForSelection(0, 0);
}
[Test]
public void TestGroupSelectionOnHeader()
{
AddBeatmaps(10, 3);
WaitForDrawablePanels();
SelectNextGroup();
SelectNextGroup();
WaitForSelection(1, 0);
SelectPrevPanel();
SelectPrevGroup();
WaitForSelection(0, 0);
}
[Test]
public void TestKeyboardSelection()
{
@ -194,6 +180,34 @@ namespace osu.Game.Tests.Visual.SongSelect
CheckNoSelection();
}
[Test]
public void TestInputHandlingWithinGaps()
{
AddBeatmaps(2, 5);
WaitForDrawablePanels();
SelectNextGroup();
clickOnDifficulty(0, 1, p => p.LayoutRectangle.TopLeft + new Vector2(20f, -1f));
WaitForSelection(0, 1);
clickOnDifficulty(0, 0, p => p.LayoutRectangle.BottomLeft + new Vector2(20f, 1f));
WaitForSelection(0, 0);
SelectNextPanel();
Select();
WaitForSelection(0, 1);
clickOnSet(0, p => p.LayoutRectangle.BottomLeft + new Vector2(20f, 1f));
WaitForSelection(0, 0);
AddStep("scroll to end", () => Scroll.ScrollToEnd(false));
clickOnDifficulty(0, 4, p => p.LayoutRectangle.BottomLeft + new Vector2(20f, 1f));
WaitForSelection(0, 4);
clickOnSet(1, p => p.LayoutRectangle.TopLeft + new Vector2(20f, -1f));
WaitForSelection(1, 0);
}
private void checkSelectionIterating(bool isIterating)
{
object? selection = null;
@ -207,5 +221,27 @@ namespace osu.Game.Tests.Visual.SongSelect
AddUntilStep("selection not changed", () => Carousel.CurrentSelection == selection);
}
}
private void clickOnSet(int set, Func<BeatmapSetPanel, Vector2> pos)
{
AddStep($"click on set{set}", () =>
{
var model = BeatmapSets[set];
var panel = this.ChildrenOfType<BeatmapSetPanel>().Single(b => ReferenceEquals(b.Item!.Model, model));
InputManager.MoveMouseTo(panel.ToScreenSpace(pos(panel)));
InputManager.Click(MouseButton.Left);
});
}
private void clickOnDifficulty(int set, int diff, Func<BeatmapPanel, Vector2> pos)
{
AddStep($"click on set{set} diff{diff}", () =>
{
var model = BeatmapSets[set].Beatmaps[diff];
var panel = this.ChildrenOfType<BeatmapPanel>().Single(b => ReferenceEquals(b.Item!.Model, model));
InputManager.MoveMouseTo(panel.ToScreenSpace(pos(panel)));
InputManager.Click(MouseButton.Left);
});
}
}
}