1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-07 19:22:58 +08:00

Improve test code quality & safety

This commit is contained in:
Bartłomiej Dach 2020-05-01 16:59:45 +02:00
parent bb90c8ea4c
commit 2913a81835
2 changed files with 60 additions and 64 deletions

View File

@ -46,7 +46,7 @@ namespace osu.Game.Tests.Visual.Navigation
} }
[SetUpSteps] [SetUpSteps]
public void SetUpSteps() public virtual void SetUpSteps()
{ {
AddStep("Create new game instance", () => AddStep("Create new game instance", () =>
{ {

View File

@ -11,23 +11,25 @@ using osu.Game.Beatmaps;
using osu.Game.Online.API; using osu.Game.Online.API;
using osu.Game.Online.API.Requests; using osu.Game.Online.API.Requests;
using osu.Game.Rulesets; using osu.Game.Rulesets;
using osu.Game.Rulesets.Catch;
using osu.Game.Rulesets.Mania;
using osu.Game.Rulesets.Osu;
using osu.Game.Rulesets.Taiko;
using osu.Game.Screens.Select; using osu.Game.Screens.Select;
using osu.Game.Tests.Visual.Navigation; using osu.Game.Tests.Visual.Navigation;
using osu.Game.Users; using osu.Game.Users;
namespace osu.Game.Tests.Visual.SongSelect namespace osu.Game.Tests.Visual.SongSelect
{ {
[HeadlessTest]
public class TestSceneBeatmapRecommendations : OsuGameTestScene public class TestSceneBeatmapRecommendations : OsuGameTestScene
{ {
protected override bool UseOnlineAPI => false;
[Resolved] [Resolved]
private DifficultyRecommender recommender { get; set; } private DifficultyRecommender recommender { get; set; }
[Resolved]
private RulesetStore rulesets { get; set; }
[SetUpSteps] [SetUpSteps]
public new void SetUpSteps() public override void SetUpSteps()
{ {
AddStep("register request handling", () => AddStep("register request handling", () =>
{ {
@ -42,6 +44,8 @@ namespace osu.Game.Tests.Visual.SongSelect
}; };
}); });
base.SetUpSteps();
// Force recommender to calculate its star ratings again // Force recommender to calculate its star ratings again
AddStep("calculate recommended SRs", () => recommender.APIStateChanged(API, APIState.Online)); AddStep("calculate recommended SRs", () => recommender.APIStateChanged(API, APIState.Online));
@ -83,60 +87,62 @@ namespace osu.Game.Tests.Visual.SongSelect
[Test] [Test]
public void TestPresentedBeatmapIsRecommended() public void TestPresentedBeatmapIsRecommended()
{ {
var importFunctions = new List<Func<BeatmapSetInfo>>(); List<BeatmapSetInfo> beatmapSets = null;
const int import_count = 5;
for (int i = 0; i < 5; i++) AddStep("import 5 maps", () =>
{ {
importFunctions.Add(importBeatmap(i, Enumerable.Repeat(rulesets.GetRuleset(0), 5))); beatmapSets = new List<BeatmapSetInfo>();
}
for (int i = 0; i < 5; i++) for (int i = 0; i < import_count; ++i)
{ {
presentAndConfirm(importFunctions[i], 2); beatmapSets.Add(importBeatmapSet(i, Enumerable.Repeat(new OsuRuleset().RulesetInfo, 5)));
} }
});
AddAssert("all sets imported", () => ensureAllBeatmapSetsImported(beatmapSets));
presentAndConfirm(() => beatmapSets[3], 2);
} }
[Test] [Test]
public void TestBestRulesetIsRecommended() public void TestBestRulesetIsRecommended()
{ {
var osuRuleset = rulesets.GetRuleset(0); BeatmapSetInfo osuSet = null, mixedSet = null;
var taikoRuleset = rulesets.GetRuleset(1);
var catchRuleset = rulesets.GetRuleset(2);
var maniaRuleset = rulesets.GetRuleset(3);
var osuImport = importBeatmap(0, new List<RulesetInfo> { osuRuleset }); AddStep("create osu! beatmapset", () => osuSet = importBeatmapSet(0, new[] { new OsuRuleset().RulesetInfo }));
var mixedImport = importBeatmap(1, new List<RulesetInfo> { taikoRuleset, catchRuleset, maniaRuleset }); AddStep("create mixed beatmapset", () => mixedSet = importBeatmapSet(1,
new[] { new TaikoRuleset().RulesetInfo, new CatchRuleset().RulesetInfo, new ManiaRuleset().RulesetInfo }));
AddAssert("all sets imported", () => ensureAllBeatmapSetsImported(new[] { osuSet, mixedSet }));
// Make sure we are on standard ruleset // Make sure we are on standard ruleset
presentAndConfirm(osuImport, 1); presentAndConfirm(() => osuSet, 1);
// Present mixed difficulty set, expect ruleset with highest star difficulty // Present mixed difficulty set, expect ruleset with highest star difficulty
presentAndConfirm(mixedImport, 3); presentAndConfirm(() => mixedSet, 3);
} }
[Test] [Test]
public void TestSecondBestRulesetIsRecommended() public void TestSecondBestRulesetIsRecommended()
{ {
var osuRuleset = rulesets.GetRuleset(0); BeatmapSetInfo osuSet = null, mixedSet = null;
var taikoRuleset = rulesets.GetRuleset(1);
var catchRuleset = rulesets.GetRuleset(2);
var osuImport = importBeatmap(0, new List<RulesetInfo> { osuRuleset }); AddStep("create osu! beatmapset", () => osuSet = importBeatmapSet(0, new[] { new OsuRuleset().RulesetInfo }));
var mixedImport = importBeatmap(1, new List<RulesetInfo> { taikoRuleset, catchRuleset, taikoRuleset }); AddStep("create mixed beatmapset", () => mixedSet = importBeatmapSet(1,
new[] { new TaikoRuleset().RulesetInfo, new CatchRuleset().RulesetInfo, new TaikoRuleset().RulesetInfo }));
AddAssert("all sets imported", () => ensureAllBeatmapSetsImported(new[] { osuSet, mixedSet }));
// Make sure we are on standard ruleset // Make sure we are on standard ruleset
presentAndConfirm(osuImport, 1); presentAndConfirm(() => osuSet, 1);
// Present mixed difficulty set, expect ruleset with second highest star difficulty // Present mixed difficulty set, expect ruleset with second highest star difficulty
presentAndConfirm(mixedImport, 2); presentAndConfirm(() => mixedSet, 2);
} }
private Func<BeatmapSetInfo> importBeatmap(int importID, IEnumerable<RulesetInfo> rulesetEnumerable) private BeatmapSetInfo importBeatmapSet(int importID, IEnumerable<RulesetInfo> difficultyRulesets)
{ {
BeatmapSetInfo imported = null;
AddStep($"import beatmap {importID}", () =>
{
var difficulty = new BeatmapDifficulty();
var metadata = new BeatmapMetadata var metadata = new BeatmapMetadata
{ {
Artist = "SomeArtist", Artist = "SomeArtist",
@ -144,36 +150,26 @@ namespace osu.Game.Tests.Visual.SongSelect
Title = $"import {importID}" Title = $"import {importID}"
}; };
var beatmaps = new List<BeatmapInfo>(); var beatmapSet = new BeatmapSetInfo
int difficultyID = 1;
foreach (RulesetInfo r in rulesetEnumerable)
{
beatmaps.Add(new BeatmapInfo
{
OnlineBeatmapID = importID + 1024 * difficultyID,
Metadata = metadata,
BaseDifficulty = difficulty,
Ruleset = r ?? rulesets.AvailableRulesets.First(),
StarDifficulty = difficultyID,
});
difficultyID++;
}
imported = Game.BeatmapManager.Import(new BeatmapSetInfo
{ {
Hash = Guid.NewGuid().ToString(), Hash = Guid.NewGuid().ToString(),
OnlineBeatmapSetID = importID, OnlineBeatmapSetID = importID,
Metadata = metadata, Metadata = metadata,
Beatmaps = beatmaps, Beatmaps = difficultyRulesets.Select((ruleset, difficultyIndex) => new BeatmapInfo
}).Result; {
}); OnlineBeatmapID = importID * 1024 + difficultyIndex,
Metadata = metadata,
BaseDifficulty = new BeatmapDifficulty(),
Ruleset = ruleset,
StarDifficulty = difficultyIndex + 1
}).ToList()
};
AddAssert($"import {importID} succeeded", () => imported != null); return Game.BeatmapManager.Import(beatmapSet).Result;
return () => imported;
} }
private bool ensureAllBeatmapSetsImported(IEnumerable<BeatmapSetInfo> beatmapSets) => beatmapSets.All(set => set != null);
private void presentAndConfirm(Func<BeatmapSetInfo> getImport, int expectedDiff) private void presentAndConfirm(Func<BeatmapSetInfo> getImport, int expectedDiff)
{ {
AddStep("present beatmap", () => Game.PresentBeatmap(getImport())); AddStep("present beatmap", () => Game.PresentBeatmap(getImport()));