2019-09-17 13:33:27 +00:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
2019-01-24 08:43:03 +00:00
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 09:19:50 +00:00
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2018-07-19 05:07:55 +00:00
|
|
|
using System;
|
2019-04-08 09:32:05 +00:00
|
|
|
using System.Collections.Generic;
|
2021-11-24 04:25:26 +00:00
|
|
|
using System.Diagnostics;
|
2019-05-31 05:40:53 +00:00
|
|
|
using System.IO;
|
2018-06-26 09:24:34 +00:00
|
|
|
using System.Linq;
|
2021-12-23 09:33:17 +00:00
|
|
|
using System.Threading;
|
2019-05-31 05:40:53 +00:00
|
|
|
using System.Threading.Tasks;
|
2020-04-23 10:24:18 +00:00
|
|
|
using JetBrains.Annotations;
|
2018-05-23 08:37:39 +00:00
|
|
|
using osu.Framework.Allocation;
|
2018-05-23 09:52:09 +00:00
|
|
|
using osu.Framework.Audio;
|
2019-05-31 05:40:53 +00:00
|
|
|
using osu.Framework.Audio.Track;
|
2019-02-21 10:04:31 +00:00
|
|
|
using osu.Framework.Bindables;
|
2019-11-11 04:49:12 +00:00
|
|
|
using osu.Framework.Graphics;
|
2023-10-10 09:48:50 +00:00
|
|
|
using osu.Framework.Graphics.Colour;
|
2019-11-11 04:49:12 +00:00
|
|
|
using osu.Framework.Graphics.Containers;
|
2021-05-31 09:37:32 +00:00
|
|
|
using osu.Framework.IO.Stores;
|
2018-07-19 05:07:55 +00:00
|
|
|
using osu.Framework.Platform;
|
2017-09-18 13:32:49 +00:00
|
|
|
using osu.Framework.Testing;
|
2019-05-31 05:40:53 +00:00
|
|
|
using osu.Framework.Timing;
|
2018-05-23 08:37:39 +00:00
|
|
|
using osu.Game.Beatmaps;
|
2019-07-29 07:55:19 +00:00
|
|
|
using osu.Game.Database;
|
2023-10-10 09:48:50 +00:00
|
|
|
using osu.Game.Graphics;
|
2019-07-31 19:42:23 +00:00
|
|
|
using osu.Game.Online.API;
|
2021-10-25 04:47:12 +00:00
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2020-08-04 12:53:00 +00:00
|
|
|
using osu.Game.Overlays;
|
2018-06-26 09:24:34 +00:00
|
|
|
using osu.Game.Rulesets;
|
2019-04-08 09:32:05 +00:00
|
|
|
using osu.Game.Rulesets.Mods;
|
2020-10-05 04:17:13 +00:00
|
|
|
using osu.Game.Rulesets.Objects;
|
2020-04-11 01:23:31 +00:00
|
|
|
using osu.Game.Rulesets.UI;
|
2019-11-21 09:50:54 +00:00
|
|
|
using osu.Game.Storyboards;
|
2019-05-31 05:40:53 +00:00
|
|
|
using osu.Game.Tests.Beatmaps;
|
2021-12-23 18:18:26 +00:00
|
|
|
using osu.Game.Tests.Rulesets;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
2017-09-18 13:32:49 +00:00
|
|
|
namespace osu.Game.Tests.Visual
|
|
|
|
{
|
2019-05-14 19:37:25 +00:00
|
|
|
public abstract partial class OsuTestScene : TestScene
|
2017-09-18 13:32:49 +00:00
|
|
|
{
|
2022-01-18 04:22:37 +00:00
|
|
|
[Cached]
|
|
|
|
protected Bindable<WorkingBeatmap> Beatmap { get; } = new Bindable<WorkingBeatmap>();
|
2019-04-08 09:32:05 +00:00
|
|
|
|
2022-01-18 04:22:37 +00:00
|
|
|
[Cached]
|
|
|
|
protected Bindable<RulesetInfo> Ruleset { get; } = new Bindable<RulesetInfo>();
|
2018-05-23 08:37:39 +00:00
|
|
|
|
2022-01-18 04:22:37 +00:00
|
|
|
[Cached]
|
|
|
|
protected Bindable<IReadOnlyList<Mod>> SelectedMods { get; } = new Bindable<IReadOnlyList<Mod>>(Array.Empty<Mod>());
|
2018-06-26 09:24:34 +00:00
|
|
|
|
2022-01-18 04:22:37 +00:00
|
|
|
protected new DependencyContainer Dependencies { get; private set; }
|
2018-05-23 08:37:39 +00:00
|
|
|
|
2021-05-31 09:37:32 +00:00
|
|
|
protected IResourceStore<byte[]> Resources;
|
|
|
|
|
2019-09-13 08:15:33 +00:00
|
|
|
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");
|
2019-09-13 08:15:33 +00:00
|
|
|
|
|
|
|
return dummyAPI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private DummyAPIAccess dummyAPI;
|
|
|
|
|
2019-07-31 19:42:23 +00:00
|
|
|
/// <summary>
|
2019-09-13 08:15:33 +00:00
|
|
|
/// Whether this test scene requires real-world API access.
|
2019-09-13 12:55:45 +00:00
|
|
|
/// If true, this will bypass the local <see cref="DummyAPIAccess"/> and use the <see cref="OsuGameBase"/> provided one.
|
2019-07-31 19:42:23 +00:00
|
|
|
/// </summary>
|
2019-09-13 08:15:33 +00:00
|
|
|
protected virtual bool UseOnlineAPI => false;
|
2019-07-31 19:42:23 +00:00
|
|
|
|
2021-11-23 05:14:53 +00:00
|
|
|
/// <summary>
|
|
|
|
/// A database context factory to be used by test runs. Can be isolated and reset by setting <see cref="UseFreshStoragePerRun"/> to <c>true</c>.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// In interactive runs (ie. VisualTests) this will use the user's database if <see cref="UseFreshStoragePerRun"/> is not set to <c>true</c>.
|
|
|
|
/// </remarks>
|
2022-01-25 03:58:15 +00:00
|
|
|
protected RealmAccess Realm => realm.Value;
|
2021-11-23 05:11:44 +00:00
|
|
|
|
2022-01-25 03:58:15 +00:00
|
|
|
private Lazy<RealmAccess> realm;
|
2021-11-23 05:11:44 +00:00
|
|
|
|
2021-11-23 05:14:53 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Whether a fresh storage should be initialised per test (method) run.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// By default (ie. if not set to <c>true</c>):
|
|
|
|
/// - in interactive runs, the user's storage will be used
|
|
|
|
/// - in headless runs, a shared temporary storage will be used per test class.
|
|
|
|
/// </remarks>
|
2021-11-23 05:11:44 +00:00
|
|
|
protected virtual bool UseFreshStoragePerRun => false;
|
|
|
|
|
2020-09-16 10:36:36 +00:00
|
|
|
/// <summary>
|
2021-11-23 05:11:44 +00:00
|
|
|
/// A storage to be used by test runs. Can be isolated by setting <see cref="UseFreshStoragePerRun"/> to <c>true</c>.
|
2020-09-16 10:36:36 +00:00
|
|
|
/// </summary>
|
2021-11-23 05:14:53 +00:00
|
|
|
/// <remarks>
|
|
|
|
/// In interactive runs (ie. VisualTests) this will use the user's storage if <see cref="UseFreshStoragePerRun"/> is not set to <c>true</c>.
|
|
|
|
/// </remarks>
|
2021-11-23 05:11:44 +00:00
|
|
|
protected Storage LocalStorage => localStorage.Value;
|
|
|
|
|
2021-12-23 18:34:41 +00:00
|
|
|
/// <summary>
|
|
|
|
/// A cache for ruleset configurations to be used in this test scene.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// This <see cref="IRulesetConfigCache"/> instance is provided to the children of this test scene via DI.
|
|
|
|
/// It is only exposed so that test scenes themselves can access the ruleset config cache in a safe manner
|
|
|
|
/// (<see cref="OsuTestScene"/>s cannot use DI themselves, as they will end up accessing the real cached instance from <see cref="OsuGameBase"/>).
|
|
|
|
/// </remarks>
|
|
|
|
protected IRulesetConfigCache RulesetConfigs { get; private set; }
|
|
|
|
|
2021-11-23 05:11:44 +00:00
|
|
|
private Lazy<Storage> localStorage;
|
|
|
|
|
|
|
|
private Storage headlessHostStorage;
|
|
|
|
|
|
|
|
private DrawableRulesetDependencies rulesetDependencies;
|
2020-09-16 10:36:36 +00:00
|
|
|
|
2018-07-11 08:07:14 +00:00
|
|
|
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
|
2018-05-23 08:37:39 +00:00
|
|
|
{
|
2022-03-25 07:35:32 +00:00
|
|
|
var host = parent.Get<GameHost>();
|
|
|
|
|
|
|
|
headlessHostStorage = (host as HeadlessGameHost)?.Storage;
|
2020-09-16 10:36:36 +00:00
|
|
|
|
2021-05-31 09:37:32 +00:00
|
|
|
Resources = parent.Get<OsuGameBase>().Resources;
|
|
|
|
|
2022-03-30 04:34:48 +00:00
|
|
|
realm = new Lazy<RealmAccess>(() => new RealmAccess(LocalStorage, OsuGameBase.CLIENT_DATABASE_FILENAME, host.UpdateThread));
|
2020-09-15 05:03:36 +00:00
|
|
|
|
2021-08-18 07:23:02 +00:00
|
|
|
RecycleLocalStorage(false);
|
2020-09-15 05:03:36 +00:00
|
|
|
|
2020-04-11 01:23:31 +00:00
|
|
|
var baseDependencies = base.CreateChildDependencies(parent);
|
|
|
|
|
2021-12-23 18:18:26 +00:00
|
|
|
// to isolate ruleset configs in tests from the actual database and avoid state pollution problems,
|
|
|
|
// as well as problems due to the implementation details of the "real" implementation (the configs only being available at `LoadComplete()`),
|
|
|
|
// cache a test implementation of the ruleset config cache over the "real" one.
|
|
|
|
var isolatedBaseDependencies = new DependencyContainer(baseDependencies);
|
2021-12-23 18:34:41 +00:00
|
|
|
isolatedBaseDependencies.CacheAs(RulesetConfigs = new TestRulesetConfigCache());
|
2021-12-23 18:18:26 +00:00
|
|
|
baseDependencies = isolatedBaseDependencies;
|
|
|
|
|
2020-04-17 00:38:12 +00:00
|
|
|
var providedRuleset = CreateRuleset();
|
|
|
|
if (providedRuleset != null)
|
2022-01-18 04:22:37 +00:00
|
|
|
isolatedBaseDependencies = rulesetDependencies = new DrawableRulesetDependencies(providedRuleset, baseDependencies);
|
2020-04-11 01:23:31 +00:00
|
|
|
|
2022-01-18 04:22:37 +00:00
|
|
|
Dependencies = isolatedBaseDependencies;
|
2019-05-31 05:51:12 +00:00
|
|
|
|
2022-01-18 04:22:37 +00:00
|
|
|
Beatmap.Default = parent.Get<Bindable<WorkingBeatmap>>().Default;
|
2019-12-13 11:05:38 +00:00
|
|
|
Beatmap.SetDefault();
|
|
|
|
|
2022-01-18 04:22:37 +00:00
|
|
|
Ruleset.Value = CreateRuleset()?.RulesetInfo ?? parent.Get<RulesetStore>().AvailableRulesets.First();
|
2018-12-05 11:19:21 +00:00
|
|
|
|
2019-12-13 12:45:38 +00:00
|
|
|
SelectedMods.SetDefault();
|
2019-07-31 19:42:23 +00:00
|
|
|
|
2019-09-13 08:15:33 +00:00
|
|
|
if (!UseOnlineAPI)
|
2019-07-31 19:42:23 +00:00
|
|
|
{
|
2019-09-13 08:15:33 +00:00
|
|
|
dummyAPI = new DummyAPIAccess();
|
2019-07-31 19:42:23 +00:00
|
|
|
Dependencies.CacheAs<IAPIProvider>(dummyAPI);
|
2021-06-05 01:30:21 +00:00
|
|
|
base.Content.Add(dummyAPI);
|
2019-07-31 19:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Dependencies;
|
2018-05-23 09:52:09 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 09:48:50 +00:00
|
|
|
[Resolved]
|
|
|
|
private OsuColour colours { get; set; }
|
|
|
|
|
2022-01-18 04:22:37 +00:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2023-10-10 09:48:50 +00:00
|
|
|
ChangeBackgroundColour(ColourInfo.GradientVertical(colours.GreyCarmine, colours.GreyCarmineDarker));
|
|
|
|
|
2023-10-17 08:40:44 +00:00
|
|
|
var parentBeatmap = Parent!.Dependencies.Get<Bindable<WorkingBeatmap>>();
|
2022-01-18 04:22:37 +00:00
|
|
|
parentBeatmap.Value = Beatmap.Value;
|
|
|
|
Beatmap.BindTo(parentBeatmap);
|
|
|
|
|
2023-10-17 08:40:44 +00:00
|
|
|
var parentRuleset = Parent!.Dependencies.Get<Bindable<RulesetInfo>>();
|
2022-01-18 04:22:37 +00:00
|
|
|
parentRuleset.Value = Ruleset.Value;
|
|
|
|
Ruleset.BindTo(parentRuleset);
|
|
|
|
|
2023-10-17 08:40:44 +00:00
|
|
|
var parentMods = Parent!.Dependencies.Get<Bindable<IReadOnlyList<Mod>>>();
|
2022-01-18 04:22:37 +00:00
|
|
|
parentMods.Value = SelectedMods.Value;
|
|
|
|
SelectedMods.BindTo(parentMods);
|
|
|
|
}
|
|
|
|
|
2019-11-11 04:49:12 +00:00
|
|
|
protected override Container<Drawable> Content => content ?? base.Content;
|
|
|
|
|
|
|
|
private readonly Container content;
|
|
|
|
|
2019-05-14 19:37:25 +00:00
|
|
|
protected OsuTestScene()
|
2018-07-19 05:07:55 +00:00
|
|
|
{
|
2019-11-11 04:49:12 +00:00
|
|
|
base.Content.Add(content = new DrawSizePreservingFillContainer());
|
2018-07-19 05:07:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 07:23:02 +00:00
|
|
|
public virtual void RecycleLocalStorage(bool isDisposing)
|
2020-01-28 10:44:32 +00:00
|
|
|
{
|
|
|
|
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.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 05:11:27 +00:00
|
|
|
localStorage = new Lazy<Storage>(() =>
|
|
|
|
{
|
|
|
|
// When running headless, there is an opportunity to use the host storage rather than creating a second isolated one.
|
|
|
|
// This is because the host is recycled per TestScene execution in headless at an nunit level.
|
|
|
|
// Importantly, we can't use this optimisation when `UseFreshStoragePerRun` is true, as it doesn't reset per test method.
|
2021-11-23 05:11:44 +00:00
|
|
|
if (!UseFreshStoragePerRun && headlessHostStorage != null)
|
|
|
|
return headlessHostStorage;
|
2021-11-23 05:11:27 +00:00
|
|
|
|
|
|
|
return new TemporaryNativeStorage($"{GetType().Name}-{Guid.NewGuid()}");
|
|
|
|
});
|
2020-01-28 10:44:32 +00:00
|
|
|
}
|
|
|
|
|
2019-05-31 05:40:53 +00:00
|
|
|
[Resolved]
|
2020-01-02 06:23:41 +00:00
|
|
|
protected AudioManager Audio { get; private set; }
|
2019-05-31 05:40:53 +00:00
|
|
|
|
2020-08-04 12:53:00 +00:00
|
|
|
[Resolved]
|
|
|
|
protected MusicController MusicController { get; private set; }
|
|
|
|
|
2020-04-17 03:17:15 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Creates the ruleset to be used for this test scene.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
2020-06-03 21:44:28 +00:00
|
|
|
/// When testing against ruleset-specific components, this method must be overriden to their corresponding ruleset.
|
2020-04-17 03:17:15 +00:00
|
|
|
/// </remarks>
|
2020-04-23 10:24:18 +00:00
|
|
|
[CanBeNull]
|
2020-04-17 03:17:15 +00:00
|
|
|
protected virtual Ruleset CreateRuleset() => null;
|
|
|
|
|
2019-05-31 05:40:53 +00:00
|
|
|
protected virtual IBeatmap CreateBeatmap(RulesetInfo ruleset) => new TestBeatmap(ruleset);
|
|
|
|
|
2021-11-01 07:42:22 +00:00
|
|
|
/// <summary>
|
2022-02-15 12:53:56 +00:00
|
|
|
/// Returns a sample API beatmap with a populated beatmap set.
|
2021-11-01 07:42:22 +00:00
|
|
|
/// </summary>
|
2021-11-01 07:43:39 +00:00
|
|
|
/// <param name="ruleset">The ruleset to create the sample model using. osu! ruleset will be used if not specified.</param>
|
2022-02-15 12:53:56 +00:00
|
|
|
protected APIBeatmap CreateAPIBeatmap(RulesetInfo ruleset = null) => CreateAPIBeatmap(CreateBeatmap(ruleset ?? Ruleset.Value).BeatmapInfo);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Constructs a sample API beatmap set containing a beatmap.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="ruleset">The ruleset to create the sample model using. osu! ruleset will be used if not specified.</param>
|
|
|
|
protected APIBeatmapSet CreateAPIBeatmapSet(RulesetInfo ruleset = null) => CreateAPIBeatmapSet(CreateBeatmap(ruleset ?? Ruleset.Value).BeatmapInfo);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Constructs a sample API beatmap with a populated beatmap set from a given source beatmap.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="original">The source beatmap.</param>
|
|
|
|
public static APIBeatmap CreateAPIBeatmap(IBeatmapInfo original)
|
2021-11-01 07:42:22 +00:00
|
|
|
{
|
2022-02-15 12:53:56 +00:00
|
|
|
var beatmapSet = CreateAPIBeatmapSet(original);
|
2021-11-01 07:42:22 +00:00
|
|
|
|
|
|
|
// Avoid circular reference.
|
|
|
|
var beatmap = beatmapSet.Beatmaps.First();
|
|
|
|
beatmapSet.Beatmaps = Array.Empty<APIBeatmap>();
|
|
|
|
|
|
|
|
// Populate the set as that's generally what we expect from the API.
|
|
|
|
beatmap.BeatmapSet = beatmapSet;
|
|
|
|
|
|
|
|
return beatmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2022-02-15 12:53:56 +00:00
|
|
|
/// Constructs a sample API beatmap set containing a beatmap from a given source beatmap.
|
2021-11-01 07:42:22 +00:00
|
|
|
/// </summary>
|
2022-02-15 12:53:56 +00:00
|
|
|
/// <param name="original">The source beatmap.</param>
|
|
|
|
public static APIBeatmapSet CreateAPIBeatmapSet(IBeatmapInfo original)
|
2021-10-25 04:47:12 +00:00
|
|
|
{
|
2022-02-15 12:53:56 +00:00
|
|
|
Debug.Assert(original.BeatmapSet != null);
|
2021-11-24 04:25:26 +00:00
|
|
|
|
2023-09-18 11:54:57 +00:00
|
|
|
var result = new APIBeatmapSet
|
2021-10-25 04:47:12 +00:00
|
|
|
{
|
2022-02-15 12:53:56 +00:00
|
|
|
OnlineID = original.BeatmapSet.OnlineID,
|
2021-11-24 09:42:47 +00:00
|
|
|
Status = BeatmapOnlineStatus.Ranked,
|
2021-11-01 06:42:12 +00:00
|
|
|
Covers = new BeatmapSetOnlineCovers
|
|
|
|
{
|
|
|
|
Cover = "https://assets.ppy.sh/beatmaps/163112/covers/cover.jpg",
|
|
|
|
Card = "https://assets.ppy.sh/beatmaps/163112/covers/card.jpg",
|
|
|
|
List = "https://assets.ppy.sh/beatmaps/163112/covers/list.jpg"
|
|
|
|
},
|
2022-02-15 12:53:56 +00:00
|
|
|
Title = original.Metadata.Title,
|
|
|
|
TitleUnicode = original.Metadata.TitleUnicode,
|
|
|
|
Artist = original.Metadata.Artist,
|
|
|
|
ArtistUnicode = original.Metadata.ArtistUnicode,
|
2021-11-24 04:25:26 +00:00
|
|
|
Author = new APIUser
|
|
|
|
{
|
2022-02-15 12:53:56 +00:00
|
|
|
Username = original.Metadata.Author.Username,
|
|
|
|
Id = original.Metadata.Author.OnlineID
|
2021-11-24 04:25:26 +00:00
|
|
|
},
|
2022-02-15 12:53:56 +00:00
|
|
|
Source = original.Metadata.Source,
|
|
|
|
Tags = original.Metadata.Tags,
|
2021-10-25 04:47:12 +00:00
|
|
|
Beatmaps = new[]
|
|
|
|
{
|
|
|
|
new APIBeatmap
|
|
|
|
{
|
2022-02-15 12:53:56 +00:00
|
|
|
OnlineID = original.OnlineID,
|
|
|
|
OnlineBeatmapSetID = original.BeatmapSet.OnlineID,
|
|
|
|
Status = ((BeatmapInfo)original).Status,
|
|
|
|
Checksum = original.MD5Hash,
|
|
|
|
AuthorID = original.Metadata.Author.OnlineID,
|
|
|
|
RulesetID = original.Ruleset.OnlineID,
|
|
|
|
StarRating = original.StarRating,
|
|
|
|
DifficultyName = original.DifficultyName,
|
2021-10-25 04:47:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2023-09-18 11:54:57 +00:00
|
|
|
|
|
|
|
foreach (var beatmap in result.Beatmaps)
|
|
|
|
beatmap.BeatmapSet = result;
|
|
|
|
|
|
|
|
return result;
|
2021-10-25 04:47:12 +00:00
|
|
|
}
|
|
|
|
|
2019-05-31 05:51:12 +00:00
|
|
|
protected WorkingBeatmap CreateWorkingBeatmap(RulesetInfo ruleset) =>
|
2021-07-05 15:52:39 +00:00
|
|
|
CreateWorkingBeatmap(CreateBeatmap(ruleset));
|
2019-05-31 05:40:53 +00:00
|
|
|
|
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);
|
2019-05-31 05:40:53 +00:00
|
|
|
|
2018-05-24 03:53:32 +00:00
|
|
|
protected override void Dispose(bool isDisposing)
|
2018-05-23 09:52:09 +00:00
|
|
|
{
|
2018-05-24 03:53:32 +00:00
|
|
|
base.Dispose(isDisposing);
|
2018-05-23 09:52:09 +00:00
|
|
|
|
2020-04-11 01:23:31 +00:00
|
|
|
rulesetDependencies?.Dispose();
|
|
|
|
|
2020-08-04 12:53:00 +00:00
|
|
|
if (MusicController?.TrackLoaded == true)
|
2020-11-02 05:56:50 +00:00
|
|
|
MusicController.Stop();
|
2018-07-19 05:07:55 +00:00
|
|
|
|
2021-08-18 07:23:02 +00:00
|
|
|
RecycleLocalStorage(true);
|
2018-05-23 08:37:39 +00:00
|
|
|
}
|
|
|
|
|
2019-05-14 19:37:25 +00:00
|
|
|
protected override ITestSceneTestRunner CreateRunner() => new OsuTestSceneTestRunner();
|
2018-04-13 09:19:50 +00:00
|
|
|
|
2019-05-31 05:40:53 +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)
|
2019-05-31 05:40:53 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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>
|
2019-05-31 05:40:53 +00: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>
|
2020-10-05 04:17:13 +00:00
|
|
|
public ClockBackedTestWorkingBeatmap(IBeatmap beatmap, Storyboard storyboard, IFrameBasedClock referenceClock, AudioManager audio)
|
2020-04-14 12:05:42 +00:00
|
|
|
: base(beatmap, storyboard, audio)
|
2019-05-31 05:40:53 +00:00
|
|
|
{
|
2020-10-05 05:04:04 +00:00
|
|
|
double trackLength = 60000;
|
2020-10-05 04:29:36 +00:00
|
|
|
|
2020-10-05 05:04:04 +00:00
|
|
|
if (beatmap.HitObjects.Count > 0)
|
|
|
|
// add buffer after last hitobject to allow for final replay frames etc.
|
2020-10-29 08:52:58 +00:00
|
|
|
trackLength = Math.Max(trackLength, beatmap.HitObjects.Max(h => h.GetEndTime()) + 2000);
|
2020-10-05 04:17:13 +00:00
|
|
|
|
2019-05-31 05:40:53 +00:00
|
|
|
if (referenceClock != null)
|
|
|
|
{
|
|
|
|
store = new TrackVirtualStore(referenceClock);
|
|
|
|
audio.AddItem(store);
|
2020-10-05 04:29:36 +00:00
|
|
|
track = store.GetVirtual(trackLength);
|
2019-05-31 05:40:53 +00:00
|
|
|
}
|
|
|
|
else
|
2020-10-05 04:29:36 +00:00
|
|
|
track = audio?.Tracks.GetVirtual(trackLength);
|
2022-07-28 08:58:13 +00:00
|
|
|
|
|
|
|
// We are guaranteed to have a virtual track.
|
|
|
|
// To ease testability, ensure the track is available from point of construction.
|
|
|
|
// (Usually this would be done by MusicController for us).
|
|
|
|
LoadTrack();
|
2019-05-31 05:40:53 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 08:01:41 +00:00
|
|
|
~ClockBackedTestWorkingBeatmap()
|
2019-05-31 05:40:53 +00:00
|
|
|
{
|
2020-02-10 08:01:41 +00:00
|
|
|
// Remove the track store from the audio manager
|
2019-05-31 05:40:53 +00:00
|
|
|
store?.Dispose();
|
|
|
|
}
|
|
|
|
|
2020-08-07 13:31:41 +00:00
|
|
|
protected override Track GetBeatmapTrack() => track;
|
2019-05-31 05:40:53 +00:00
|
|
|
|
2022-05-20 11:43:50 +00:00
|
|
|
public override bool TryTransferTrack(WorkingBeatmap target)
|
|
|
|
{
|
|
|
|
// Our track comes from a local track store that's disposed on finalizer,
|
|
|
|
// therefore it's unsafe to transfer it to another working beatmap.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-31 05:40:53 +00:00
|
|
|
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();
|
|
|
|
|
2021-12-23 09:33:17 +00:00
|
|
|
public Task<Track> GetAsync(string name, CancellationToken cancellationToken = default) => throw new NotImplementedException();
|
2019-05-31 05:40:53 +00:00
|
|
|
|
|
|
|
public Stream GetStream(string name) => throw new NotImplementedException();
|
|
|
|
|
|
|
|
public IEnumerable<string> GetAvailableResources() => throw new NotImplementedException();
|
|
|
|
|
2022-09-01 14:05:07 +00:00
|
|
|
public Track GetVirtual(double length = double.PositiveInfinity, string name = "virtual")
|
2019-05-31 05:40:53 +00:00
|
|
|
{
|
2022-09-01 14:05:07 +00:00
|
|
|
var track = new TrackVirtualManual(referenceClock, name) { Length = length };
|
2019-05-31 05:40:53 +00:00
|
|
|
AddItem(track);
|
|
|
|
return track;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// A virtual track which tracks a reference clock.
|
|
|
|
/// </summary>
|
|
|
|
public class TrackVirtualManual : Track
|
|
|
|
{
|
2020-03-03 03:59:41 +00:00
|
|
|
private readonly IFrameBasedClock referenceClock;
|
2019-05-31 05:40:53 +00:00
|
|
|
|
2020-03-05 16:28:59 +00:00
|
|
|
private bool running;
|
|
|
|
|
2023-10-23 13:18:38 +00:00
|
|
|
public override double Rate => base.Rate
|
|
|
|
// This is mainly to allow some tests to override the rate to zero
|
|
|
|
// and avoid interpolation.
|
|
|
|
* referenceClock.Rate;
|
2023-10-23 11:28:12 +00:00
|
|
|
|
2022-09-01 14:05:07 +00:00
|
|
|
public TrackVirtualManual(IFrameBasedClock referenceClock, string name = "virtual")
|
|
|
|
: base(name)
|
2019-05-31 05:40:53 +00:00
|
|
|
{
|
|
|
|
this.referenceClock = referenceClock;
|
|
|
|
Length = double.PositiveInfinity;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool Seek(double seek)
|
|
|
|
{
|
2020-03-06 13:44:11 +00:00
|
|
|
accumulated = Math.Clamp(seek, 0, Length);
|
2020-03-05 16:28:59 +00:00
|
|
|
lastReferenceTime = null;
|
2020-03-03 03:59:41 +00:00
|
|
|
|
2020-03-05 16:28:59 +00:00
|
|
|
return accumulated == seek;
|
2019-05-31 05:40:53 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 07:32:53 +00:00
|
|
|
public override Task<bool> SeekAsync(double seek) => Task.FromResult(Seek(seek));
|
|
|
|
|
2020-03-05 16:28:59 +00:00
|
|
|
public override void Start()
|
|
|
|
{
|
|
|
|
running = true;
|
|
|
|
}
|
2019-05-31 05:40:53 +00:00
|
|
|
|
2022-07-06 07:32:53 +00:00
|
|
|
public override Task StartAsync()
|
|
|
|
{
|
|
|
|
Start();
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2019-05-31 05:40:53 +00:00
|
|
|
public override void Reset()
|
|
|
|
{
|
2020-03-05 16:28:59 +00:00
|
|
|
Seek(0);
|
2019-05-31 05:40:53 +00:00
|
|
|
base.Reset();
|
|
|
|
}
|
|
|
|
|
2020-03-05 16:28:59 +00:00
|
|
|
public override void Stop()
|
|
|
|
{
|
|
|
|
if (running)
|
|
|
|
{
|
|
|
|
running = false;
|
|
|
|
lastReferenceTime = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 07:32:53 +00:00
|
|
|
public override Task StopAsync()
|
|
|
|
{
|
|
|
|
Stop();
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2020-03-05 16:28:59 +00:00
|
|
|
public override bool IsRunning => running;
|
2019-05-31 05:40:53 +00:00
|
|
|
|
2020-03-05 16:28:59 +00:00
|
|
|
private double? lastReferenceTime;
|
2019-05-31 05:40:53 +00:00
|
|
|
|
2020-03-05 16:28:59 +00:00
|
|
|
private double accumulated;
|
|
|
|
|
|
|
|
public override double CurrentTime => Math.Min(accumulated, Length);
|
2019-05-31 05:40:53 +00:00
|
|
|
|
|
|
|
protected override void UpdateState()
|
|
|
|
{
|
|
|
|
base.UpdateState();
|
|
|
|
|
2020-03-05 16:28:59 +00:00
|
|
|
if (running)
|
|
|
|
{
|
|
|
|
double refTime = referenceClock.CurrentTime;
|
|
|
|
|
2020-07-28 03:08:15 +00:00
|
|
|
double? lastRefTime = lastReferenceTime;
|
|
|
|
|
|
|
|
if (lastRefTime != null)
|
|
|
|
accumulated += (refTime - lastRefTime.Value) * Rate;
|
2020-03-05 16:28:59 +00:00
|
|
|
|
|
|
|
lastReferenceTime = refTime;
|
|
|
|
}
|
|
|
|
|
2019-05-31 05:40:53 +00:00
|
|
|
if (CurrentTime >= Length)
|
|
|
|
{
|
|
|
|
Stop();
|
2021-06-16 08:44:21 +00:00
|
|
|
// `RaiseCompleted` is not called here to prevent transitioning to the next song.
|
2019-05-31 05:40:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 19:37:25 +00:00
|
|
|
public partial class OsuTestSceneTestRunner : OsuGameBase, ITestSceneTestRunner
|
2017-09-18 13:32:49 +00:00
|
|
|
{
|
2019-05-14 09:39:56 +00:00
|
|
|
private TestSceneTestRunner.TestRunner runner;
|
2018-04-18 06:12:48 +00:00
|
|
|
|
2018-04-21 19:10:06 +00:00
|
|
|
protected override void LoadAsyncComplete()
|
2017-09-18 13:32:49 +00:00
|
|
|
{
|
2018-04-21 19:10:06 +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
|
2018-04-21 19:10:06 +00:00
|
|
|
// the runner to be loaded at that point.
|
2019-05-14 09:39:56 +00:00
|
|
|
Add(runner = new TestSceneTestRunner.TestRunner());
|
2017-09-18 13:32:49 +00:00
|
|
|
}
|
2018-04-18 06:12:48 +00:00
|
|
|
|
2021-09-06 15:45:53 +00:00
|
|
|
protected override void InitialiseFonts()
|
|
|
|
{
|
|
|
|
// skip fonts load as it's not required for testing purposes.
|
|
|
|
}
|
|
|
|
|
2019-05-14 09:39:56 +00:00
|
|
|
public void RunTestBlocking(TestScene test) => runner.RunTestBlocking(test);
|
2017-09-18 13:32:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|