1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 21:43:21 +08:00
osu-lazer/osu.Game/Tests/Visual/OsuTestCase.cs

111 lines
3.7 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.
2018-04-13 17:19:50 +08:00
2018-07-19 13:07:55 +08:00
using System;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Audio;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-07-19 13:07:55 +08:00
using osu.Framework.Platform;
2018-04-13 17:19:50 +08:00
using osu.Framework.Testing;
using osu.Game.Beatmaps;
using osu.Game.Rulesets;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Tests.Visual
{
public abstract class OsuTestCase : TestCase
{
private readonly OsuTestBeatmap beatmap = new OsuTestBeatmap(new DummyWorkingBeatmap());
2018-06-06 19:16:20 +08:00
protected BindableBeatmap Beatmap => beatmap;
2018-06-26 17:59:13 +08:00
protected readonly Bindable<RulesetInfo> Ruleset = new Bindable<RulesetInfo>();
protected DependencyContainer Dependencies { get; private set; }
2018-07-19 13:07:55 +08:00
private readonly Lazy<Storage> localStorage;
protected Storage LocalStorage => localStorage.Value;
2018-07-11 16:07:14 +08:00
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
2018-07-11 16:07:14 +08:00
Dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
// This is the earliest we can get OsuGameBase, which is used by the dummy working beatmap to find textures
beatmap.Default = new DummyWorkingBeatmap(Dependencies.Get<OsuGameBase>());
2019-02-12 18:53:08 +08:00
Dependencies.CacheAs<Bindable<WorkingBeatmap>>(beatmap);
2019-02-01 14:42:15 +08:00
Dependencies.CacheAs<IBindable<WorkingBeatmap>>(beatmap);
2018-06-26 17:59:13 +08:00
Dependencies.CacheAs(Ruleset);
2018-07-02 14:07:13 +08:00
Dependencies.CacheAs<IBindable<RulesetInfo>>(Ruleset);
return Dependencies;
}
2018-07-19 13:07:55 +08:00
protected OsuTestCase()
{
localStorage = new Lazy<Storage>(() => new DesktopStorage($"{GetType().Name}-{Guid.NewGuid()}", null));
}
[BackgroundDependencyLoader]
private void load(AudioManager audioManager, RulesetStore rulesets)
{
beatmap.SetAudioManager(audioManager);
2018-06-26 17:59:13 +08:00
Ruleset.Value = rulesets.AvailableRulesets.First();
}
2018-05-24 11:53:32 +08:00
protected override void Dispose(bool isDisposing)
{
2018-05-24 11:53:32 +08:00
base.Dispose(isDisposing);
2019-02-13 14:14:34 +08:00
beatmap?.Value.Track.Stop();
2018-07-19 13:07:55 +08:00
if (localStorage.IsValueCreated)
{
try
{
localStorage.Value.DeleteDirectory(".");
}
catch
{
// we don't really care if this fails; it will just leave folders lying around from test runs.
}
}
}
2018-04-18 14:12:48 +08:00
protected override ITestCaseTestRunner CreateRunner() => new OsuTestCaseTestRunner();
2018-04-13 17:19:50 +08:00
2018-04-18 14:12:48 +08:00
public class OsuTestCaseTestRunner : OsuGameBase, ITestCaseTestRunner
2018-04-13 17:19:50 +08:00
{
private TestCaseTestRunner.TestRunner runner;
2018-04-18 14:12:48 +08:00
protected override void LoadAsyncComplete()
2018-04-13 17:19:50 +08:00
{
// this has to be run here rather than LoadComplete because
// TestCase.cs is checking the IsLoaded state (on another thread) and expects
// the runner to be loaded at that point.
2018-04-21 17:15:27 +08:00
Add(runner = new TestCaseTestRunner.TestRunner());
2018-04-13 17:19:50 +08:00
}
2018-04-18 14:12:48 +08:00
public void RunTestBlocking(TestCase test) => runner.RunTestBlocking(test);
2018-04-13 17:19:50 +08:00
}
2018-06-06 19:16:20 +08:00
private class OsuTestBeatmap : BindableBeatmap
{
public OsuTestBeatmap(WorkingBeatmap defaultValue)
: base(defaultValue)
{
}
public void SetAudioManager(AudioManager audioManager) => RegisterAudioManager(audioManager);
2018-06-06 19:16:20 +08:00
public override BindableBeatmap GetBoundCopy()
{
var copy = new OsuTestBeatmap(Default);
copy.BindTo(this);
return copy;
}
}
2018-04-13 17:19:50 +08:00
}
}