2019-09-17 21:33:27 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
2019-01-24 16:43:03 +08:00
|
|
|
// 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;
|
2019-04-08 17:32:05 +08:00
|
|
|
using System.Collections.Generic;
|
2019-05-31 13:40:53 +08:00
|
|
|
using System.IO;
|
2018-06-26 17:24:34 +08:00
|
|
|
using System.Linq;
|
2019-05-31 13:40:53 +08:00
|
|
|
using System.Threading.Tasks;
|
2018-05-23 16:37:39 +08:00
|
|
|
using osu.Framework.Allocation;
|
2018-05-23 17:52:09 +08:00
|
|
|
using osu.Framework.Audio;
|
2019-05-31 13:40:53 +08:00
|
|
|
using osu.Framework.Audio.Track;
|
2019-02-21 18:04:31 +08:00
|
|
|
using osu.Framework.Bindables;
|
2019-11-11 12:49:12 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2019-05-31 13:51:12 +08:00
|
|
|
using osu.Framework.Graphics.Textures;
|
2018-07-19 13:07:55 +08:00
|
|
|
using osu.Framework.Platform;
|
2018-04-13 17:19:50 +08:00
|
|
|
using osu.Framework.Testing;
|
2019-05-31 13:40:53 +08:00
|
|
|
using osu.Framework.Timing;
|
2018-05-23 16:37:39 +08:00
|
|
|
using osu.Game.Beatmaps;
|
2019-07-29 15:55:19 +08:00
|
|
|
using osu.Game.Database;
|
2019-08-01 03:42:23 +08:00
|
|
|
using osu.Game.Online.API;
|
2018-06-26 17:24:34 +08:00
|
|
|
using osu.Game.Rulesets;
|
2019-04-08 17:32:05 +08:00
|
|
|
using osu.Game.Rulesets.Mods;
|
2019-11-21 17:50:54 +08:00
|
|
|
using osu.Game.Storyboards;
|
2019-05-31 13:40:53 +08:00
|
|
|
using osu.Game.Tests.Beatmaps;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Tests.Visual
|
|
|
|
{
|
2019-05-15 03:37:25 +08:00
|
|
|
public abstract class OsuTestScene : TestScene
|
2018-04-13 17:19:50 +08:00
|
|
|
{
|
2019-04-08 17:32:05 +08:00
|
|
|
[Cached(typeof(Bindable<WorkingBeatmap>))]
|
|
|
|
[Cached(typeof(IBindable<WorkingBeatmap>))]
|
2019-10-01 16:24:47 +08:00
|
|
|
private NonNullableBindable<WorkingBeatmap> beatmap;
|
2019-04-08 17:32:05 +08:00
|
|
|
|
2019-10-01 16:24:47 +08:00
|
|
|
protected Bindable<WorkingBeatmap> Beatmap => beatmap;
|
2018-05-23 16:37:39 +08:00
|
|
|
|
2019-04-08 17:32:05 +08:00
|
|
|
[Cached]
|
|
|
|
[Cached(typeof(IBindable<RulesetInfo>))]
|
2018-06-26 17:59:13 +08:00
|
|
|
protected readonly Bindable<RulesetInfo> Ruleset = new Bindable<RulesetInfo>();
|
2018-06-26 17:24:34 +08:00
|
|
|
|
2019-04-08 17:32:05 +08:00
|
|
|
[Cached]
|
2019-04-10 16:13:12 +08:00
|
|
|
[Cached(Type = typeof(IBindable<IReadOnlyList<Mod>>))]
|
|
|
|
protected readonly Bindable<IReadOnlyList<Mod>> Mods = new Bindable<IReadOnlyList<Mod>>(Array.Empty<Mod>());
|
2019-04-08 17:32:05 +08:00
|
|
|
|
2019-04-25 16:36:17 +08:00
|
|
|
protected new DependencyContainer Dependencies { get; private set; }
|
2018-05-23 16:37:39 +08:00
|
|
|
|
2018-07-19 13:07:55 +08:00
|
|
|
private readonly Lazy<Storage> localStorage;
|
|
|
|
protected Storage LocalStorage => localStorage.Value;
|
|
|
|
|
2019-07-29 15:55:19 +08:00
|
|
|
private readonly Lazy<DatabaseContextFactory> contextFactory;
|
2019-09-13 16:15:33 +08:00
|
|
|
|
|
|
|
protected IAPIProvider API
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (UseOnlineAPI)
|
2019-09-13 21:15:11 +08:00
|
|
|
throw new InvalidOperationException($"Using the {nameof(OsuTestScene)} dummy API is not supported when {nameof(UseOnlineAPI)} is true");
|
2019-09-13 16:15:33 +08:00
|
|
|
|
|
|
|
return dummyAPI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private DummyAPIAccess dummyAPI;
|
|
|
|
|
2019-07-29 15:55:19 +08:00
|
|
|
protected DatabaseContextFactory ContextFactory => contextFactory.Value;
|
|
|
|
|
2019-08-01 03:42:23 +08:00
|
|
|
/// <summary>
|
2019-09-13 16:15:33 +08:00
|
|
|
/// Whether this test scene requires real-world API access.
|
2019-09-13 20:55:45 +08:00
|
|
|
/// If true, this will bypass the local <see cref="DummyAPIAccess"/> and use the <see cref="OsuGameBase"/> provided one.
|
2019-08-01 03:42:23 +08:00
|
|
|
/// </summary>
|
2019-09-13 16:15:33 +08:00
|
|
|
protected virtual bool UseOnlineAPI => false;
|
2019-08-01 03:42:23 +08:00
|
|
|
|
2018-07-11 16:07:14 +08:00
|
|
|
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
|
2018-05-23 16:37:39 +08:00
|
|
|
{
|
2018-12-05 19:19:21 +08:00
|
|
|
// This is the earliest we can get OsuGameBase, which is used by the dummy working beatmap to find textures
|
2019-05-31 13:51:12 +08:00
|
|
|
var working = new DummyWorkingBeatmap(parent.Get<AudioManager>(), parent.Get<TextureStore>());
|
|
|
|
|
2019-10-01 16:24:47 +08:00
|
|
|
beatmap = new NonNullableBindable<WorkingBeatmap>(working) { Default = working };
|
2019-10-01 12:48:56 +08:00
|
|
|
beatmap.BindValueChanged(b => ScheduleAfterChildren(() =>
|
2019-05-31 13:40:53 +08:00
|
|
|
{
|
2019-10-01 12:48:56 +08:00
|
|
|
// compare to last beatmap as sometimes the two may share a track representation (optimisation, see WorkingBeatmap.TransferTo)
|
|
|
|
if (b.OldValue?.TrackLoaded == true && b.OldValue?.Track != b.NewValue?.Track)
|
|
|
|
b.OldValue.RecycleTrack();
|
|
|
|
}));
|
2018-12-05 19:19:21 +08:00
|
|
|
|
2019-08-01 03:42:23 +08:00
|
|
|
Dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
|
|
|
|
|
2019-09-13 16:15:33 +08:00
|
|
|
if (!UseOnlineAPI)
|
2019-08-01 03:42:23 +08:00
|
|
|
{
|
2019-09-13 16:15:33 +08:00
|
|
|
dummyAPI = new DummyAPIAccess();
|
2019-08-01 03:42:23 +08:00
|
|
|
Dependencies.CacheAs<IAPIProvider>(dummyAPI);
|
|
|
|
Add(dummyAPI);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Dependencies;
|
2018-05-23 17:52:09 +08:00
|
|
|
}
|
|
|
|
|
2019-11-11 12:49:12 +08:00
|
|
|
protected override Container<Drawable> Content => content ?? base.Content;
|
|
|
|
|
|
|
|
private readonly Container content;
|
|
|
|
|
2019-05-15 03:37:25 +08:00
|
|
|
protected OsuTestScene()
|
2018-07-19 13:07:55 +08:00
|
|
|
{
|
2019-03-27 19:58:07 +08:00
|
|
|
localStorage = new Lazy<Storage>(() => new NativeStorage($"{GetType().Name}-{Guid.NewGuid()}"));
|
2019-07-29 15:55:19 +08:00
|
|
|
contextFactory = new Lazy<DatabaseContextFactory>(() =>
|
|
|
|
{
|
|
|
|
var factory = new DatabaseContextFactory(LocalStorage);
|
|
|
|
factory.ResetDatabase();
|
|
|
|
using (var usage = factory.Get())
|
|
|
|
usage.Migrate();
|
|
|
|
return factory;
|
|
|
|
});
|
2019-11-11 12:49:12 +08:00
|
|
|
|
|
|
|
base.Content.Add(content = new DrawSizePreservingFillContainer());
|
2018-07-19 13:07:55 +08:00
|
|
|
}
|
|
|
|
|
2019-05-31 13:40:53 +08:00
|
|
|
[Resolved]
|
|
|
|
private AudioManager audio { get; set; }
|
|
|
|
|
|
|
|
protected virtual IBeatmap CreateBeatmap(RulesetInfo ruleset) => new TestBeatmap(ruleset);
|
|
|
|
|
2019-05-31 13:51:12 +08:00
|
|
|
protected WorkingBeatmap CreateWorkingBeatmap(RulesetInfo ruleset) =>
|
2019-11-21 17:50:54 +08:00
|
|
|
CreateWorkingBeatmap(CreateBeatmap(ruleset), null);
|
2019-05-31 13:40:53 +08:00
|
|
|
|
2019-11-21 17:50:54 +08:00
|
|
|
protected virtual WorkingBeatmap CreateWorkingBeatmap(IBeatmap beatmap, Storyboard storyboard = null) =>
|
|
|
|
new ClockBackedTestWorkingBeatmap(beatmap, storyboard, Clock, audio);
|
2019-05-31 13:40:53 +08:00
|
|
|
|
2018-05-28 16:55:41 +08:00
|
|
|
[BackgroundDependencyLoader]
|
2019-05-31 13:40:53 +08:00
|
|
|
private void load(RulesetStore rulesets)
|
2018-05-28 16:55:41 +08:00
|
|
|
{
|
2018-06-26 17:59:13 +08:00
|
|
|
Ruleset.Value = rulesets.AvailableRulesets.First();
|
2018-05-28 16:55:41 +08:00
|
|
|
}
|
|
|
|
|
2018-05-24 11:53:32 +08:00
|
|
|
protected override void Dispose(bool isDisposing)
|
2018-05-23 17:52:09 +08:00
|
|
|
{
|
2018-05-24 11:53:32 +08:00
|
|
|
base.Dispose(isDisposing);
|
2018-05-23 17:52:09 +08:00
|
|
|
|
2019-05-31 13:40:53 +08:00
|
|
|
if (beatmap?.Value.TrackLoaded == true)
|
|
|
|
beatmap.Value.Track.Stop();
|
2018-07-19 13:07:55 +08:00
|
|
|
|
2019-07-29 15:55:19 +08:00
|
|
|
if (contextFactory.IsValueCreated)
|
|
|
|
contextFactory.Value.ResetDatabase();
|
|
|
|
|
2018-07-19 13:07:55 +08:00
|
|
|
if (localStorage.IsValueCreated)
|
2018-07-23 16:39:43 +08:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
localStorage.Value.DeleteDirectory(".");
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
// we don't really care if this fails; it will just leave folders lying around from test runs.
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 16:37:39 +08:00
|
|
|
}
|
|
|
|
|
2019-05-15 03:37:25 +08:00
|
|
|
protected override ITestSceneTestRunner CreateRunner() => new OsuTestSceneTestRunner();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2019-05-31 13:40:53 +08: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 17:50:54 +08:00
|
|
|
: this(new TestBeatmap(ruleset), null, referenceClock, audio)
|
2019-05-31 13:40:53 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Create an instance which provides the <see cref="IBeatmap"/> when requested.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="beatmap">The beatmap</param>
|
2019-11-21 17:50:54 +08:00
|
|
|
/// <param name="storyboard">The storyboard.</param>
|
2019-05-31 13:40:53 +08:00
|
|
|
/// <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 17:50:54 +08:00
|
|
|
public ClockBackedTestWorkingBeatmap(IBeatmap beatmap, Storyboard storyboard, IFrameBasedClock referenceClock, AudioManager audio, double length = 60000)
|
|
|
|
: base(beatmap, storyboard)
|
2019-05-31 13:40:53 +08:00
|
|
|
{
|
|
|
|
if (referenceClock != null)
|
|
|
|
{
|
|
|
|
store = new TrackVirtualStore(referenceClock);
|
|
|
|
audio.AddItem(store);
|
|
|
|
track = store.GetVirtual(length);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
track = audio?.Tracks.GetVirtual(length);
|
|
|
|
}
|
|
|
|
|
2019-06-27 12:56:36 +08:00
|
|
|
protected override void Dispose(bool isDisposing)
|
2019-05-31 13:40:53 +08:00
|
|
|
{
|
2019-06-27 13:08:58 +08:00
|
|
|
base.Dispose(isDisposing);
|
2019-05-31 13:40:53 +08:00
|
|
|
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 18:33:24 +08:00
|
|
|
public Track GetVirtual(double length = double.PositiveInfinity)
|
2019-05-31 13:40:53 +08:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2019-11-20 20:19:49 +08:00
|
|
|
offset = Math.Clamp(seek, 0, Length);
|
2019-05-31 13:40:53 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 03:37:25 +08:00
|
|
|
public class OsuTestSceneTestRunner : OsuGameBase, ITestSceneTestRunner
|
2018-04-13 17:19:50 +08:00
|
|
|
{
|
2019-05-14 17:39:56 +08:00
|
|
|
private TestSceneTestRunner.TestRunner runner;
|
2018-04-18 14:12:48 +08:00
|
|
|
|
2018-04-22 03:10:06 +08:00
|
|
|
protected override void LoadAsyncComplete()
|
2018-04-13 17:19:50 +08:00
|
|
|
{
|
2018-04-22 03:10:06 +08:00
|
|
|
// this has to be run here rather than LoadComplete because
|
2019-05-15 17:32:29 +08:00
|
|
|
// TestScene.cs is checking the IsLoaded state (on another thread) and expects
|
2018-04-22 03:10:06 +08:00
|
|
|
// the runner to be loaded at that point.
|
2019-05-14 17:39:56 +08:00
|
|
|
Add(runner = new TestSceneTestRunner.TestRunner());
|
2018-04-13 17:19:50 +08:00
|
|
|
}
|
2018-04-18 14:12:48 +08:00
|
|
|
|
2019-05-14 17:39:56 +08:00
|
|
|
public void RunTestBlocking(TestScene test) => runner.RunTestBlocking(test);
|
2018-04-13 17:19:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|