1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-26 05:12:57 +08:00
osu-lazer/osu.Game.Tests/Visual/SongSelect/TestSceneBeatmapMetadataDisplay.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

153 lines
5.0 KiB
C#
Raw Normal View History

2021-05-08 16:46:42 +08:00
// 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 System.Threading;
using System.Threading.Tasks;
2021-05-08 16:46:42 +08:00
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Utils;
using osu.Game.Beatmaps;
using osu.Game.Rulesets;
2021-05-08 16:46:42 +08:00
using osu.Game.Rulesets.Mods;
using osu.Game.Screens.Menu;
2021-05-08 16:46:42 +08:00
using osu.Game.Screens.Play;
using osuTK;
namespace osu.Game.Tests.Visual.SongSelect
{
public class TestSceneBeatmapMetadataDisplay : OsuTestScene
{
private BeatmapMetadataDisplay display;
[Resolved]
private BeatmapManager manager { get; set; }
[Cached(typeof(BeatmapDifficultyCache))]
private readonly TestBeatmapDifficultyCache testDifficultyCache = new TestBeatmapDifficultyCache();
2021-05-08 16:46:42 +08:00
[Test]
2021-07-12 17:08:19 +08:00
public void TestLocal(
[Values("Beatmap", "Some long title and stuff")]
string title,
[Values("Trial", "Some1's very hardest difficulty")]
string version)
2021-05-08 16:46:42 +08:00
{
2021-05-10 11:34:21 +08:00
showMetadataForBeatmap(() => CreateWorkingBeatmap(new Beatmap
2021-05-08 16:46:42 +08:00
{
BeatmapInfo =
{
Metadata = new BeatmapMetadata
{
Title = title,
},
DifficultyName = version,
StarRating = RNG.NextDouble(0, 10),
2021-05-08 16:46:42 +08:00
}
}));
}
[Test]
public void TestDelayedStarRating()
{
AddStep("block calculation", () => testDifficultyCache.BlockCalculation = true);
showMetadataForBeatmap(() => CreateWorkingBeatmap(new Beatmap
{
BeatmapInfo =
{
Metadata = new BeatmapMetadata
{
Title = "Heavy beatmap",
},
DifficultyName = "10k objects",
StarRating = 99.99f,
}
}));
AddStep("allow calculation", () => testDifficultyCache.BlockCalculation = false);
}
2021-05-08 16:46:42 +08:00
[Test]
public void TestRandomFromDatabase()
{
2021-05-10 11:34:21 +08:00
showMetadataForBeatmap(() =>
2021-05-08 16:46:42 +08:00
{
var allBeatmapSets = manager.GetAllUsableBeatmapSets();
2021-05-08 18:03:50 +08:00
if (allBeatmapSets.Count == 0)
return manager.DefaultBeatmap;
2021-05-08 16:46:42 +08:00
var randomBeatmapSet = allBeatmapSets[RNG.Next(0, allBeatmapSets.Count - 1)];
var randomBeatmap = randomBeatmapSet.Beatmaps[RNG.Next(0, randomBeatmapSet.Beatmaps.Count - 1)];
return manager.GetWorkingBeatmap(randomBeatmap);
});
}
private void showMetadataForBeatmap(Func<IWorkingBeatmap> getBeatmap)
2021-05-10 11:34:21 +08:00
{
AddStep("setup display", () =>
{
var randomMods = Ruleset.Value.CreateInstance().CreateAllMods().OrderBy(_ => RNG.Next()).Take(5).ToList();
2021-05-10 11:34:21 +08:00
OsuLogo logo = new OsuLogo { Scale = new Vector2(0.15f) };
Remove(testDifficultyCache);
Children = new Drawable[]
2021-05-10 11:34:21 +08:00
{
testDifficultyCache,
display = new BeatmapMetadataDisplay(getBeatmap(), new Bindable<IReadOnlyList<Mod>>(randomMods), logo)
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Alpha = 0f,
}
2021-05-10 11:34:21 +08:00
};
display.FadeIn(400, Easing.OutQuint);
2021-05-10 11:34:21 +08:00
});
AddWaitStep("wait a bit", 5);
AddStep("finish loading", () => display.Loading = false);
2021-05-10 11:34:21 +08:00
}
private class TestBeatmapDifficultyCache : BeatmapDifficultyCache
{
private TaskCompletionSource<bool> calculationBlocker;
private bool blockCalculation;
public bool BlockCalculation
{
get => blockCalculation;
set
{
if (value == blockCalculation)
return;
blockCalculation = value;
if (value)
calculationBlocker = new TaskCompletionSource<bool>();
else
calculationBlocker?.SetResult(false);
}
}
public override async Task<StarDifficulty?> GetDifficultyAsync(IBeatmapInfo beatmapInfo, IRulesetInfo rulesetInfo = null, IEnumerable<Mod> mods = null, CancellationToken cancellationToken = default)
{
if (blockCalculation)
await calculationBlocker.Task.ConfigureAwait(false);
return await base.GetDifficultyAsync(beatmapInfo, rulesetInfo, mods, cancellationToken).ConfigureAwait(false);
}
}
2021-05-08 16:46:42 +08:00
}
}