osu/osu.Game/Tests/Visual/OsuTestScene.cs

331 lines
12 KiB
C#
Raw Normal View History

2019-09-17 13:33:27 +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.
2018-04-13 09:19:50 +00:00
2018-07-19 05:07:55 +00:00
using System;
2019-04-08 09:32:05 +00:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
2019-02-21 10:04:31 +00:00
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2018-07-19 05:07:55 +00:00
using osu.Framework.Platform;
2018-04-13 09:19:50 +00:00
using osu.Framework.Testing;
using osu.Framework.Timing;
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Online.API;
using osu.Game.Rulesets;
2019-04-08 09:32:05 +00:00
using osu.Game.Rulesets.Mods;
using osu.Game.Screens;
2019-11-21 09:50:54 +00:00
using osu.Game.Storyboards;
using osu.Game.Tests.Beatmaps;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Tests.Visual
{
public abstract class OsuTestScene : TestScene
2018-04-13 09:19:50 +00:00
{
protected Bindable<WorkingBeatmap> Beatmap { get; private set; }
2019-04-08 09:32:05 +00:00
protected Bindable<RulesetInfo> Ruleset;
2019-12-13 12:45:38 +00:00
protected Bindable<IReadOnlyList<Mod>> SelectedMods;
protected new OsuScreenDependencies Dependencies { get; private set; }
private Lazy<Storage> localStorage;
2018-07-19 05:07:55 +00:00
protected Storage LocalStorage => localStorage.Value;
private readonly Lazy<DatabaseContextFactory> contextFactory;
protected IAPIProvider API
{
get
{
if (UseOnlineAPI)
2019-09-13 13:15:11 +00:00
throw new InvalidOperationException($"Using the {nameof(OsuTestScene)} dummy API is not supported when {nameof(UseOnlineAPI)} is true");
return dummyAPI;
}
}
private DummyAPIAccess dummyAPI;
protected DatabaseContextFactory ContextFactory => contextFactory.Value;
/// <summary>
/// Whether this test scene requires real-world API access.
/// If true, this will bypass the local <see cref="DummyAPIAccess"/> and use the <see cref="OsuGameBase"/> provided one.
/// </summary>
protected virtual bool UseOnlineAPI => false;
2018-07-11 08:07:14 +00:00
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
Dependencies = new OsuScreenDependencies(false, base.CreateChildDependencies(parent));
Beatmap = Dependencies.Beatmap;
Beatmap.SetDefault();
Ruleset = Dependencies.Ruleset;
Ruleset.SetDefault();
2019-12-13 12:45:38 +00:00
SelectedMods = Dependencies.Mods;
SelectedMods.SetDefault();
if (!UseOnlineAPI)
{
dummyAPI = new DummyAPIAccess();
Dependencies.CacheAs<IAPIProvider>(dummyAPI);
Add(dummyAPI);
}
return Dependencies;
}
protected override Container<Drawable> Content => content ?? base.Content;
private readonly Container content;
protected OsuTestScene()
2018-07-19 05:07:55 +00:00
{
RecycleLocalStorage();
contextFactory = new Lazy<DatabaseContextFactory>(() =>
{
var factory = new DatabaseContextFactory(LocalStorage);
factory.ResetDatabase();
using (var usage = factory.Get())
usage.Migrate();
return factory;
});
base.Content.Add(content = new DrawSizePreservingFillContainer());
2018-07-19 05:07:55 +00:00
}
public void RecycleLocalStorage()
{
if (localStorage?.IsValueCreated == true)
{
try
{
localStorage.Value.DeleteDirectory(".");
}
catch
{
// we don't really care if this fails; it will just leave folders lying around from test runs.
}
}
localStorage = new Lazy<Storage>(() => new NativeStorage($"{GetType().Name}-{Guid.NewGuid()}"));
}
[Resolved]
2020-01-02 06:23:41 +00:00
protected AudioManager Audio { get; private set; }
protected virtual IBeatmap CreateBeatmap(RulesetInfo ruleset) => new TestBeatmap(ruleset);
protected WorkingBeatmap CreateWorkingBeatmap(RulesetInfo ruleset) =>
2019-11-21 09:50:54 +00:00
CreateWorkingBeatmap(CreateBeatmap(ruleset), null);
2019-11-21 09:50:54 +00:00
protected virtual WorkingBeatmap CreateWorkingBeatmap(IBeatmap beatmap, Storyboard storyboard = null) =>
2020-01-02 06:23:41 +00:00
new ClockBackedTestWorkingBeatmap(beatmap, storyboard, Clock, Audio);
[BackgroundDependencyLoader]
private void load(RulesetStore rulesets)
{
2018-06-26 09:59:13 +00:00
Ruleset.Value = rulesets.AvailableRulesets.First();
}
2018-05-24 03:53:32 +00:00
protected override void Dispose(bool isDisposing)
{
2018-05-24 03:53:32 +00:00
base.Dispose(isDisposing);
if (Beatmap?.Value.TrackLoaded == true)
Beatmap.Value.Track.Stop();
2018-07-19 05:07:55 +00:00
if (contextFactory.IsValueCreated)
contextFactory.Value.ResetDatabase();
RecycleLocalStorage();
}
protected override ITestSceneTestRunner CreateRunner() => new OsuTestSceneTestRunner();
2018-04-13 09:19:50 +00:00
public class ClockBackedTestWorkingBeatmap : TestWorkingBeatmap
{
private readonly Track track;
private readonly TrackVirtualStore store;
/// <summary>
/// Create an instance which creates a <see cref="TestBeatmap"/> for the provided ruleset when requested.
/// </summary>
/// <param name="ruleset">The target ruleset.</param>
/// <param name="referenceClock">A clock which should be used instead of a stopwatch for virtual time progression.</param>
/// <param name="audio">Audio manager. Required if a reference clock isn't provided.</param>
public ClockBackedTestWorkingBeatmap(RulesetInfo ruleset, IFrameBasedClock referenceClock, AudioManager audio)
2019-11-21 09:50:54 +00:00
: this(new TestBeatmap(ruleset), null, referenceClock, audio)
{
}
/// <summary>
/// Create an instance which provides the <see cref="IBeatmap"/> when requested.
/// </summary>
/// <param name="beatmap">The beatmap</param>
2019-11-21 09:50:54 +00:00
/// <param name="storyboard">The storyboard.</param>
/// <param name="referenceClock">An optional clock which should be used instead of a stopwatch for virtual time progression.</param>
/// <param name="audio">Audio manager. Required if a reference clock isn't provided.</param>
/// <param name="length">The length of the returned virtual track.</param>
2019-11-21 09:50:54 +00:00
public ClockBackedTestWorkingBeatmap(IBeatmap beatmap, Storyboard storyboard, IFrameBasedClock referenceClock, AudioManager audio, double length = 60000)
: base(beatmap, storyboard)
{
if (referenceClock != null)
{
store = new TrackVirtualStore(referenceClock);
audio.AddItem(store);
track = store.GetVirtual(length);
}
else
track = audio?.Tracks.GetVirtual(length);
}
2019-06-27 04:56:36 +00:00
protected override void Dispose(bool isDisposing)
{
2019-06-27 05:08:58 +00:00
base.Dispose(isDisposing);
store?.Dispose();
}
protected override Track GetTrack() => track;
public class TrackVirtualStore : AudioCollectionManager<Track>, ITrackStore
{
private readonly IFrameBasedClock referenceClock;
public TrackVirtualStore(IFrameBasedClock referenceClock)
{
this.referenceClock = referenceClock;
}
public Track Get(string name) => throw new NotImplementedException();
public Task<Track> GetAsync(string name) => throw new NotImplementedException();
public Stream GetStream(string name) => throw new NotImplementedException();
public IEnumerable<string> GetAvailableResources() => throw new NotImplementedException();
2019-11-12 10:33:24 +00:00
public Track GetVirtual(double length = double.PositiveInfinity)
{
var track = new TrackVirtualManual(referenceClock) { Length = length };
AddItem(track);
return track;
}
}
/// <summary>
/// A virtual track which tracks a reference clock.
/// </summary>
public class TrackVirtualManual : Track
{
private readonly IFrameBasedClock referenceClock;
private readonly ManualClock clock = new ManualClock();
private bool running;
/// <summary>
/// Local offset added to the reference clock to resolve correct time.
/// </summary>
private double offset;
public TrackVirtualManual(IFrameBasedClock referenceClock)
{
this.referenceClock = referenceClock;
Length = double.PositiveInfinity;
}
public override bool Seek(double seek)
{
offset = Math.Clamp(seek, 0, Length);
lastReferenceTime = null;
return offset == seek;
}
public override void Start()
{
running = true;
}
public override void Reset()
{
Seek(0);
base.Reset();
}
public override void Stop()
{
if (running)
{
running = false;
// on stopping, the current value should be transferred out of the clock, as we can no longer rely on
// the referenceClock (which will still be counting time).
offset = clock.CurrentTime;
lastReferenceTime = null;
}
}
public override bool IsRunning => running;
private double? lastReferenceTime;
public override double CurrentTime => clock.CurrentTime;
protected override void UpdateState()
{
base.UpdateState();
if (running)
{
double refTime = referenceClock.CurrentTime;
if (!lastReferenceTime.HasValue)
{
// if the clock just started running, the current value should be transferred to the offset
// (to zero the progression of time).
offset -= refTime;
}
lastReferenceTime = refTime;
}
clock.CurrentTime = Math.Min((lastReferenceTime ?? 0) + offset, Length);
if (CurrentTime >= Length)
{
Stop();
RaiseCompleted();
}
}
}
}
public class OsuTestSceneTestRunner : OsuGameBase, ITestSceneTestRunner
2018-04-13 09:19:50 +00:00
{
private TestSceneTestRunner.TestRunner runner;
2018-04-18 06:12:48 +00:00
protected override void LoadAsyncComplete()
2018-04-13 09:19:50 +00:00
{
// this has to be run here rather than LoadComplete because
2019-05-15 09:32:29 +00:00
// TestScene.cs is checking the IsLoaded state (on another thread) and expects
// the runner to be loaded at that point.
Add(runner = new TestSceneTestRunner.TestRunner());
2018-04-13 09:19:50 +00:00
}
2018-04-18 06:12:48 +00:00
public void RunTestBlocking(TestScene test) => runner.RunTestBlocking(test);
2018-04-13 09:19:50 +00:00
}
}
}