1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 19:12:57 +08:00
osu-lazer/osu.Game.Tests/Visual/Online/TestSceneBeatmapListingOverlay.cs

303 lines
12 KiB
C#
Raw Normal View History

// 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.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Graphics.Containers;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
2021-09-10 00:57:33 +08:00
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Overlays;
using osu.Game.Overlays.BeatmapListing;
using osu.Game.Rulesets;
using osu.Game.Scoring;
2021-06-20 17:17:07 +08:00
using osu.Game.Users;
2021-09-10 00:57:33 +08:00
using osuTK.Input;
namespace osu.Game.Tests.Visual.Online
{
2021-09-10 00:57:33 +08:00
public class TestSceneBeatmapListingOverlay : OsuManualInputManagerTestScene
{
private readonly List<APIBeatmapSet> setsForResponse = new List<APIBeatmapSet>();
private BeatmapListingOverlay overlay;
2021-06-22 13:53:21 +08:00
private BeatmapListingSearchControl searchControl => overlay.ChildrenOfType<BeatmapListingSearchControl>().Single();
2021-09-10 00:57:33 +08:00
[SetUpSteps]
public void SetUpSteps()
{
2021-09-10 00:57:33 +08:00
AddStep("setup overlay", () =>
{
Child = overlay = new BeatmapListingOverlay { State = { Value = Visibility.Visible } };
setsForResponse.Clear();
});
2021-09-10 00:57:33 +08:00
AddStep("initialize dummy", () =>
{
2021-09-10 00:57:33 +08:00
var api = (DummyAPIAccess)API;
2021-09-10 00:57:33 +08:00
api.HandleRequest = req =>
{
2021-09-10 00:57:33 +08:00
if (!(req is SearchBeatmapSetsRequest searchBeatmapSetsRequest)) return false;
2021-09-10 00:57:33 +08:00
searchBeatmapSetsRequest.TriggerSuccess(new SearchBeatmapSetsResponse
{
BeatmapSets = setsForResponse,
});
return true;
};
2021-06-20 18:28:43 +08:00
// non-supporter user
2021-09-10 00:57:33 +08:00
api.LocalUser.Value = new User
2021-06-20 18:28:43 +08:00
{
Username = "TestBot",
Id = API.LocalUser.Value.Id + 1,
};
});
}
2021-09-10 00:57:33 +08:00
[Test]
public void TestHideViaBack()
{
AddAssert("is visible", () => overlay.State.Value == Visibility.Visible);
AddStep("hide", () => InputManager.Key(Key.Escape));
AddUntilStep("is hidden", () => overlay.State.Value == Visibility.Hidden);
}
[Test]
public void TestHideViaBackWithSearch()
{
AddAssert("is visible", () => overlay.State.Value == Visibility.Visible);
AddStep("search something", () => overlay.ChildrenOfType<SearchTextBox>().First().Text = "search");
AddStep("kill search", () => InputManager.Key(Key.Escape));
AddAssert("search textbox empty", () => string.IsNullOrEmpty(overlay.ChildrenOfType<SearchTextBox>().First().Text));
AddAssert("is visible", () => overlay.State.Value == Visibility.Visible);
AddStep("hide", () => InputManager.Key(Key.Escape));
AddUntilStep("is hidden", () => overlay.State.Value == Visibility.Hidden);
}
[Test]
public void TestHideViaBackWithScrolledSearch()
{
AddAssert("is visible", () => overlay.State.Value == Visibility.Visible);
AddStep("show many results", () => fetchFor(Enumerable.Repeat(CreateBeatmap(Ruleset.Value).BeatmapInfo.BeatmapSet, 100).ToArray()));
AddUntilStep("placeholder hidden", () => !overlay.ChildrenOfType<BeatmapListingOverlay.NotFoundDrawable>().Any(d => d.IsPresent));
2021-09-10 00:57:33 +08:00
AddStep("scroll to bottom", () => overlay.ChildrenOfType<OverlayScrollContainer>().First().ScrollToEnd());
AddStep("kill search", () => InputManager.Key(Key.Escape));
AddUntilStep("search textbox empty", () => string.IsNullOrEmpty(overlay.ChildrenOfType<SearchTextBox>().First().Text));
AddUntilStep("is scrolled to top", () => overlay.ChildrenOfType<OverlayScrollContainer>().First().Current == 0);
AddAssert("is visible", () => overlay.State.Value == Visibility.Visible);
AddStep("hide", () => InputManager.Key(Key.Escape));
AddUntilStep("is hidden", () => overlay.State.Value == Visibility.Hidden);
}
[Test]
public void TestNoBeatmapsPlaceholder()
{
AddStep("fetch for 0 beatmaps", () => fetchFor());
AddUntilStep("placeholder shown", () => overlay.ChildrenOfType<BeatmapListingOverlay.NotFoundDrawable>().SingleOrDefault()?.IsPresent == true);
AddStep("fetch for 1 beatmap", () => fetchFor(CreateBeatmap(Ruleset.Value).BeatmapInfo.BeatmapSet));
AddUntilStep("placeholder hidden", () => !overlay.ChildrenOfType<BeatmapListingOverlay.NotFoundDrawable>().Any(d => d.IsPresent));
AddStep("fetch for 0 beatmaps", () => fetchFor());
AddUntilStep("placeholder shown", () => overlay.ChildrenOfType<BeatmapListingOverlay.NotFoundDrawable>().SingleOrDefault()?.IsPresent == true);
// fetch once more to ensure nothing happens in displaying placeholder again when it already is present.
AddStep("fetch for 0 beatmaps again", () => fetchFor());
AddUntilStep("placeholder shown", () => overlay.ChildrenOfType<BeatmapListingOverlay.NotFoundDrawable>().SingleOrDefault()?.IsPresent == true);
}
2021-06-20 17:17:07 +08:00
[Test]
2021-06-27 01:27:34 +08:00
public void TestUserWithoutSupporterUsesSupporterOnlyFiltersWithoutResults()
2021-06-20 18:28:43 +08:00
{
AddStep("fetch for 0 beatmaps", () => fetchFor());
2021-06-20 18:28:43 +08:00
AddStep("set dummy as non-supporter", () => ((DummyAPIAccess)API).LocalUser.Value.IsSupporter = false);
2021-06-20 17:17:07 +08:00
// only Rank Achieved filter
setRankAchievedFilter(new[] { ScoreRank.XH });
2021-06-22 13:53:21 +08:00
supporterRequiredPlaceholderShown();
2021-06-20 17:17:07 +08:00
setRankAchievedFilter(Array.Empty<ScoreRank>());
2021-06-22 13:53:21 +08:00
notFoundPlaceholderShown();
2021-06-20 17:17:07 +08:00
// only Played filter
setPlayedFilter(SearchPlayed.Played);
2021-06-22 13:53:21 +08:00
supporterRequiredPlaceholderShown();
setPlayedFilter(SearchPlayed.Any);
2021-06-22 13:53:21 +08:00
notFoundPlaceholderShown();
// both RankAchieved and Played filters
setRankAchievedFilter(new[] { ScoreRank.XH });
setPlayedFilter(SearchPlayed.Played);
2021-06-22 13:53:21 +08:00
supporterRequiredPlaceholderShown();
2021-06-20 17:17:07 +08:00
setRankAchievedFilter(Array.Empty<ScoreRank>());
setPlayedFilter(SearchPlayed.Any);
2021-06-22 13:53:21 +08:00
notFoundPlaceholderShown();
}
2021-06-20 17:17:07 +08:00
2021-06-22 13:53:21 +08:00
[Test]
2021-06-27 01:27:34 +08:00
public void TestUserWithSupporterUsesSupporterOnlyFiltersWithoutResults()
2021-06-22 13:53:21 +08:00
{
AddStep("fetch for 0 beatmaps", () => fetchFor());
2021-06-20 18:28:43 +08:00
AddStep("set dummy as supporter", () => ((DummyAPIAccess)API).LocalUser.Value.IsSupporter = true);
2021-06-20 17:17:07 +08:00
// only Rank Achieved filter
setRankAchievedFilter(new[] { ScoreRank.XH });
2021-06-22 13:53:21 +08:00
notFoundPlaceholderShown();
2021-06-20 17:17:07 +08:00
setRankAchievedFilter(Array.Empty<ScoreRank>());
2021-06-22 13:53:21 +08:00
notFoundPlaceholderShown();
2021-06-20 17:17:07 +08:00
// only Played filter
setPlayedFilter(SearchPlayed.Played);
2021-06-22 13:53:21 +08:00
notFoundPlaceholderShown();
2021-06-20 17:17:07 +08:00
setPlayedFilter(SearchPlayed.Any);
2021-06-22 13:53:21 +08:00
notFoundPlaceholderShown();
// both Rank Achieved and Played filters
setRankAchievedFilter(new[] { ScoreRank.XH });
setPlayedFilter(SearchPlayed.Played);
2021-06-22 13:53:21 +08:00
notFoundPlaceholderShown();
setRankAchievedFilter(Array.Empty<ScoreRank>());
setPlayedFilter(SearchPlayed.Any);
2021-06-22 13:53:21 +08:00
notFoundPlaceholderShown();
2021-06-20 18:28:43 +08:00
}
[Test]
2021-06-27 01:27:34 +08:00
public void TestUserWithoutSupporterUsesSupporterOnlyFiltersWithResults()
2021-06-20 18:28:43 +08:00
{
AddStep("fetch for 1 beatmap", () => fetchFor(CreateBeatmap(Ruleset.Value).BeatmapInfo.BeatmapSet));
AddStep("set dummy as non-supporter", () => ((DummyAPIAccess)API).LocalUser.Value.IsSupporter = false);
// only Rank Achieved filter
setRankAchievedFilter(new[] { ScoreRank.XH });
2021-06-22 13:53:21 +08:00
supporterRequiredPlaceholderShown();
2021-06-20 17:17:07 +08:00
setRankAchievedFilter(Array.Empty<ScoreRank>());
2021-06-22 13:53:21 +08:00
noPlaceholderShown();
2021-06-20 17:17:07 +08:00
// only Played filter
setPlayedFilter(SearchPlayed.Played);
2021-06-22 13:53:21 +08:00
supporterRequiredPlaceholderShown();
setPlayedFilter(SearchPlayed.Any);
2021-06-22 13:53:21 +08:00
noPlaceholderShown();
2021-06-20 18:28:43 +08:00
// both Rank Achieved and Played filters
setRankAchievedFilter(new[] { ScoreRank.XH });
setPlayedFilter(SearchPlayed.Played);
2021-06-22 13:53:21 +08:00
supporterRequiredPlaceholderShown();
setRankAchievedFilter(Array.Empty<ScoreRank>());
setPlayedFilter(SearchPlayed.Any);
2021-06-22 13:53:21 +08:00
noPlaceholderShown();
}
2021-06-20 17:17:07 +08:00
2021-06-22 13:53:21 +08:00
[Test]
2021-06-27 01:27:34 +08:00
public void TestUserWithSupporterUsesSupporterOnlyFiltersWithResults()
2021-06-22 13:53:21 +08:00
{
AddStep("fetch for 1 beatmap", () => fetchFor(CreateBeatmap(Ruleset.Value).BeatmapInfo.BeatmapSet));
2021-06-20 18:28:43 +08:00
AddStep("set dummy as supporter", () => ((DummyAPIAccess)API).LocalUser.Value.IsSupporter = true);
2021-06-20 17:17:07 +08:00
// only Rank Achieved filter
setRankAchievedFilter(new[] { ScoreRank.XH });
2021-06-22 13:53:21 +08:00
noPlaceholderShown();
2021-06-20 17:17:07 +08:00
setRankAchievedFilter(Array.Empty<ScoreRank>());
2021-06-22 13:53:21 +08:00
noPlaceholderShown();
2021-06-20 18:28:43 +08:00
// only Played filter
setPlayedFilter(SearchPlayed.Played);
2021-06-22 13:53:21 +08:00
noPlaceholderShown();
2021-06-20 18:28:43 +08:00
setPlayedFilter(SearchPlayed.Any);
2021-06-22 13:53:21 +08:00
noPlaceholderShown();
// both Rank Achieved and Played filters
setRankAchievedFilter(new[] { ScoreRank.XH });
setPlayedFilter(SearchPlayed.Played);
2021-06-22 13:53:21 +08:00
noPlaceholderShown();
setRankAchievedFilter(Array.Empty<ScoreRank>());
setPlayedFilter(SearchPlayed.Any);
2021-06-22 13:53:21 +08:00
noPlaceholderShown();
2021-06-20 17:17:07 +08:00
}
2021-09-10 00:57:33 +08:00
private static int searchCount;
private void fetchFor(params BeatmapSetInfo[] beatmaps)
{
setsForResponse.Clear();
setsForResponse.AddRange(beatmaps.Select(b => new TestAPIBeatmapSet(b)));
// trigger arbitrary change for fetching.
2021-09-10 00:57:33 +08:00
searchControl.Query.Value = $"search {searchCount++}";
}
private void setRankAchievedFilter(ScoreRank[] ranks)
2021-06-20 18:28:43 +08:00
{
AddStep($"set Rank Achieved filter to [{string.Join(',', ranks)}]", () =>
2021-06-20 18:28:43 +08:00
{
2021-06-22 13:53:21 +08:00
searchControl.Ranks.Clear();
searchControl.Ranks.AddRange(ranks);
2021-06-20 18:28:43 +08:00
});
}
private void setPlayedFilter(SearchPlayed played)
2021-06-20 18:28:43 +08:00
{
AddStep($"set Played filter to {played}", () => searchControl.Played.Value = played);
2021-06-20 18:28:43 +08:00
}
2021-06-22 13:53:21 +08:00
private void supporterRequiredPlaceholderShown()
{
2021-06-27 02:27:15 +08:00
AddUntilStep("\"supporter required\" placeholder shown", () => overlay.ChildrenOfType<BeatmapListingOverlay.SupporterRequiredDrawable>().SingleOrDefault()?.IsPresent == true);
2021-06-22 13:53:21 +08:00
}
2021-06-22 13:53:21 +08:00
private void notFoundPlaceholderShown()
{
2021-06-27 02:27:15 +08:00
AddUntilStep("\"no maps found\" placeholder shown", () => overlay.ChildrenOfType<BeatmapListingOverlay.NotFoundDrawable>().SingleOrDefault()?.IsPresent == true);
2021-06-22 13:53:21 +08:00
}
private void noPlaceholderShown()
{
2021-06-27 02:27:15 +08:00
AddUntilStep("no placeholder shown", () =>
!overlay.ChildrenOfType<BeatmapListingOverlay.SupporterRequiredDrawable>().Any(d => d.IsPresent)
&& !overlay.ChildrenOfType<BeatmapListingOverlay.NotFoundDrawable>().Any(d => d.IsPresent));
}
private class TestAPIBeatmapSet : APIBeatmapSet
{
private readonly BeatmapSetInfo beatmapSet;
public TestAPIBeatmapSet(BeatmapSetInfo beatmapSet)
{
this.beatmapSet = beatmapSet;
}
public override BeatmapSetInfo ToBeatmapSet(RulesetStore rulesets) => beatmapSet;
}
}
}