2021-11-17 02:06:43 +00: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.
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2021-11-17 02:06:43 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2024-02-02 10:48:13 +00:00
|
|
|
using JetBrains.Annotations;
|
2021-11-17 21:07:24 +00:00
|
|
|
using Moq;
|
2021-11-17 02:06:43 +00:00
|
|
|
using NUnit.Framework;
|
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Rulesets;
|
2021-11-20 16:23:55 +00:00
|
|
|
using osu.Game.Rulesets.Mods;
|
2021-11-17 02:06:43 +00:00
|
|
|
using osu.Game.Rulesets.Objects;
|
|
|
|
using osu.Game.Rulesets.Osu;
|
|
|
|
using osu.Game.Rulesets.Osu.Beatmaps;
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.Beatmaps
|
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
public class WorkingBeatmapTest
|
|
|
|
{
|
|
|
|
[Test]
|
|
|
|
public void TestGetPlayableSuccess()
|
|
|
|
{
|
|
|
|
var working = new TestNeverLoadsWorkingBeatmap();
|
|
|
|
|
|
|
|
working.ResetEvent.Set();
|
|
|
|
|
|
|
|
Assert.NotNull(working.GetPlayableBeatmap(new OsuRuleset().RulesetInfo));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestGetPlayableCancellationToken()
|
|
|
|
{
|
|
|
|
var working = new TestNeverLoadsWorkingBeatmap();
|
|
|
|
|
|
|
|
var cts = new CancellationTokenSource();
|
|
|
|
var loadStarted = new ManualResetEventSlim();
|
|
|
|
var loadCompleted = new ManualResetEventSlim();
|
|
|
|
|
|
|
|
Task.Factory.StartNew(() =>
|
|
|
|
{
|
|
|
|
loadStarted.Set();
|
2021-11-20 16:23:55 +00:00
|
|
|
Assert.Throws<OperationCanceledException>(() => working.GetPlayableBeatmap(new OsuRuleset().RulesetInfo, Array.Empty<Mod>(), cts.Token));
|
2021-11-17 02:06:43 +00:00
|
|
|
loadCompleted.Set();
|
|
|
|
}, TaskCreationOptions.LongRunning);
|
|
|
|
|
|
|
|
Assert.IsTrue(loadStarted.Wait(10000));
|
|
|
|
|
|
|
|
cts.Cancel();
|
|
|
|
|
|
|
|
Assert.IsTrue(loadCompleted.Wait(10000));
|
|
|
|
|
|
|
|
working.ResetEvent.Set();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestGetPlayableDefaultTimeout()
|
|
|
|
{
|
|
|
|
var working = new TestNeverLoadsWorkingBeatmap();
|
|
|
|
|
2021-11-20 16:23:55 +00:00
|
|
|
Assert.Throws(Is.InstanceOf<TimeoutException>(), () => working.GetPlayableBeatmap(new OsuRuleset().RulesetInfo));
|
2021-11-17 02:06:43 +00:00
|
|
|
|
|
|
|
working.ResetEvent.Set();
|
|
|
|
}
|
|
|
|
|
2021-11-17 21:07:24 +00:00
|
|
|
[Test]
|
|
|
|
public void TestGetPlayableRulesetLoadFailure()
|
|
|
|
{
|
|
|
|
var working = new TestWorkingBeatmap(new Beatmap());
|
|
|
|
|
|
|
|
// by default mocks return nulls if not set up, which is actually desired here to simulate a ruleset load failure scenario.
|
|
|
|
var ruleset = new Mock<IRulesetInfo>();
|
|
|
|
|
|
|
|
Assert.Throws<RulesetLoadException>(() => working.GetPlayableBeatmap(ruleset.Object));
|
|
|
|
}
|
|
|
|
|
2021-11-17 02:06:43 +00:00
|
|
|
public class TestNeverLoadsWorkingBeatmap : TestWorkingBeatmap
|
|
|
|
{
|
|
|
|
public ManualResetEventSlim ResetEvent = new ManualResetEventSlim();
|
|
|
|
|
|
|
|
public TestNeverLoadsWorkingBeatmap()
|
|
|
|
: base(new Beatmap())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override IBeatmapConverter CreateBeatmapConverter(IBeatmap beatmap, Ruleset ruleset) => new TestConverter(beatmap, ResetEvent);
|
|
|
|
|
|
|
|
public class TestConverter : IBeatmapConverter
|
|
|
|
{
|
|
|
|
private readonly ManualResetEventSlim resetEvent;
|
|
|
|
|
|
|
|
public TestConverter(IBeatmap beatmap, ManualResetEventSlim resetEvent)
|
|
|
|
{
|
|
|
|
this.resetEvent = resetEvent;
|
|
|
|
Beatmap = beatmap;
|
|
|
|
}
|
|
|
|
|
2024-02-02 10:48:13 +00:00
|
|
|
#pragma warning disable CS0067
|
|
|
|
[CanBeNull]
|
2021-11-17 02:06:43 +00:00
|
|
|
public event Action<HitObject, IEnumerable<HitObject>> ObjectConverted;
|
2024-02-02 10:48:13 +00:00
|
|
|
#pragma warning restore CS0067
|
2021-11-17 02:06:43 +00:00
|
|
|
|
|
|
|
public IBeatmap Beatmap { get; }
|
|
|
|
|
|
|
|
public bool CanConvert() => true;
|
|
|
|
|
|
|
|
public IBeatmap Convert(CancellationToken cancellationToken = default)
|
|
|
|
{
|
|
|
|
resetEvent.Wait(cancellationToken);
|
|
|
|
return new OsuBeatmap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|