1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 11:27:24 +08:00
osu-lazer/osu.Game/Tests/Visual/PlayerTestScene.cs
Salman Ahmed 136e10086a
Set the ruleset bindable value at the BDL for its subclasses usages
There are test scenes using current value of ruleset bindable on their BDL (example in TestSceneSliderSnaking's BDL)
2020-06-04 00:37:06 +03:00

103 lines
3.1 KiB
C#

// 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.
using System;
using System.Linq;
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Testing;
using osu.Game.Configuration;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
namespace osu.Game.Tests.Visual
{
public abstract class PlayerTestScene : RateAdjustedBeatmapTestScene
{
/// <summary>
/// Whether custom test steps are provided. Custom tests should invoke <see cref="CreateTest"/> to create the test steps.
/// </summary>
protected virtual bool HasCustomSteps { get; } = false;
protected TestPlayer Player;
protected OsuConfigManager LocalConfig;
private readonly Ruleset ruleset;
protected PlayerTestScene()
{
ruleset = CreatePlayerRuleset();
}
[BackgroundDependencyLoader]
private void load()
{
Ruleset.Value = ruleset.RulesetInfo;
Dependencies.Cache(LocalConfig = new OsuConfigManager(LocalStorage));
LocalConfig.GetBindable<double>(OsuSetting.DimLevel).Value = 1.0;
}
[SetUpSteps]
public override void SetUpSteps()
{
base.SetUpSteps();
if (!HasCustomSteps)
CreateTest(null);
}
protected void CreateTest(Action action)
{
if (action != null && !HasCustomSteps)
throw new InvalidOperationException($"Cannot add custom test steps without {nameof(HasCustomSteps)} being set.");
action?.Invoke();
AddStep(ruleset.Description, LoadPlayer);
AddUntilStep("player loaded", () => Player.IsLoaded && Player.Alpha == 1);
}
protected virtual bool AllowFail => false;
protected virtual bool Autoplay => false;
protected void LoadPlayer()
{
var beatmap = CreateBeatmap(ruleset.RulesetInfo);
Beatmap.Value = CreateWorkingBeatmap(beatmap);
SelectedMods.Value = Array.Empty<Mod>();
if (!AllowFail)
{
var noFailMod = ruleset.GetAllMods().FirstOrDefault(m => m is ModNoFail);
if (noFailMod != null)
SelectedMods.Value = new[] { noFailMod };
}
if (Autoplay)
{
var mod = ruleset.GetAutoplayMod();
if (mod != null)
SelectedMods.Value = SelectedMods.Value.Concat(mod.Yield()).ToArray();
}
Player = CreatePlayer(ruleset);
LoadScreen(Player);
}
/// <summary>
/// Creates the ruleset for setting up the <see cref="Player"/> component.
/// </summary>
[NotNull]
protected abstract Ruleset CreatePlayerRuleset();
protected sealed override Ruleset CreateRuleset() => CreatePlayerRuleset();
protected virtual TestPlayer CreatePlayer(Ruleset ruleset) => new TestPlayer(false, false);
}
}