1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-30 19:32:54 +08:00
osu-lazer/osu.Game.Tests/Visual/Background/TestSceneUserDimBackgrounds.cs

457 lines
20 KiB
C#
Raw Normal View History

2019-02-15 15:17:01 +08: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.
2022-06-17 15:37:17 +08:00
#nullable disable
using System.Threading;
2019-02-15 15:38:27 +08:00
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Audio;
2019-02-22 19:44:02 +08:00
using osu.Framework.Bindables;
using osu.Framework.Extensions;
using osu.Framework.Graphics;
2019-07-10 11:36:58 +08:00
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Events;
using osu.Framework.Input.States;
using osu.Framework.Platform;
using osu.Framework.Screens;
2022-01-11 22:01:17 +08:00
using osu.Framework.Utils;
using osu.Game.Beatmaps;
using osu.Game.Configuration;
2019-02-15 15:17:01 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Osu.Mods;
using osu.Game.Scoring;
2019-02-15 15:17:01 +08:00
using osu.Game.Screens;
using osu.Game.Screens.Backgrounds;
using osu.Game.Screens.Play;
using osu.Game.Screens.Play.PlayerSettings;
2020-03-17 16:43:16 +08:00
using osu.Game.Screens.Ranking;
using osu.Game.Screens.Select;
using osu.Game.Tests.Resources;
2019-02-28 13:40:31 +08:00
using osuTK;
using osuTK.Graphics;
2019-02-15 15:17:01 +08:00
2019-03-25 00:02:36 +08:00
namespace osu.Game.Tests.Visual.Background
2019-02-15 15:17:01 +08:00
{
2019-02-15 15:38:27 +08:00
[TestFixture]
2022-11-24 13:32:20 +08:00
public partial class TestSceneUserDimBackgrounds : ScreenTestScene
2019-02-15 15:17:01 +08:00
{
private DummySongSelect songSelect;
private TestPlayerLoader playerLoader;
private LoadBlockingTestPlayer player;
private BeatmapManager manager;
private RulesetStore rulesets;
[BackgroundDependencyLoader]
private void load(GameHost host, AudioManager audio)
{
Dependencies.Cache(rulesets = new RealmRulesetStore(Realm));
Dependencies.Cache(manager = new BeatmapManager(LocalStorage, Realm, null, audio, Resources, host, Beatmap.Default));
Dependencies.Cache(new OsuConfigManager(LocalStorage));
Dependencies.Cache(Realm);
manager.Import(TestResources.GetQuickTestBeatmapForImport()).WaitSafely();
2019-03-20 19:47:25 +08:00
Beatmap.SetDefault();
}
public override void SetUpSteps()
{
base.SetUpSteps();
AddStep("push song select", () => Stack.Push(songSelect = new DummySongSelect()));
}
2021-04-15 04:22:38 +08:00
/// <summary>
/// User settings should always be ignored on song select screen.
/// </summary>
[Test]
public void TestUserSettingsIgnoredOnSongSelect()
{
setupUserSettings();
2021-04-15 05:48:15 +08:00
AddUntilStep("Screen is undimmed", () => songSelect.IsBackgroundUndimmed());
AddUntilStep("Screen using background blur", () => songSelect.IsBackgroundBlur());
2021-04-15 04:22:38 +08:00
performFullSetup();
AddStep("Exit to song select", () => player.Exit());
AddUntilStep("Screen is undimmed", () => songSelect.IsBackgroundUndimmed());
AddUntilStep("Screen using background blur", () => songSelect.IsBackgroundBlur());
}
/// <summary>
/// Check if <see cref="PlayerLoader"/> properly triggers the visual settings preview when a user hovers over the visual settings panel.
/// </summary>
[Test]
2020-05-26 10:30:36 +08:00
public void TestPlayerLoaderSettingsHover()
{
2019-02-28 17:26:41 +08:00
setupUserSettings();
AddStep("Start player loader", () => songSelect.Push(playerLoader = new TestPlayerLoader(player = new LoadBlockingTestPlayer { BlockLoad = true })));
2019-03-19 16:24:26 +08:00
AddUntilStep("Wait for Player Loader to load", () => playerLoader?.IsLoaded ?? false);
2024-03-16 12:15:12 +08:00
AddAssert("Background retained from song select", () =>
{
InputManager.MoveMouseTo(playerLoader);
return songSelect.IsBackgroundCurrent();
});
2024-03-16 11:11:42 +08:00
AddUntilStep("Screen is dimmed and blur applied", () =>
{
InputManager.MoveMouseTo(playerLoader.VisualSettingsPos);
2024-03-16 11:11:42 +08:00
return songSelect.IsBackgroundDimmed() && songSelect.IsUserBlurApplied();
});
2024-03-16 11:11:42 +08:00
AddStep("Stop background preview", () => InputManager.MoveMouseTo(playerLoader.ScreenPos));
AddUntilStep("Screen is undimmed and user blur removed", () => songSelect.IsBackgroundUndimmed() && songSelect.CheckBackgroundBlur(playerLoader.ExpectedBackgroundBlur));
}
/// <summary>
/// In the case of a user triggering the dim preview the instant player gets loaded, then moving the cursor off of the visual settings:
/// The OnHover of PlayerLoader will trigger, which could potentially cause visual settings to be unapplied unless checked for in PlayerLoader.
/// We need to check that in this scenario, the dim and blur is still properly applied after entering player.
/// </summary>
[Test]
2020-05-26 10:30:36 +08:00
public void TestPlayerLoaderTransition()
{
performFullSetup();
AddStep("Trigger hover event", () => playerLoader.TriggerOnHover());
AddAssert("Background retained from song select", () => songSelect.IsBackgroundCurrent());
2020-05-26 10:29:56 +08:00
AddUntilStep("Screen is dimmed and blur applied", () => songSelect.IsBackgroundDimmed() && songSelect.IsUserBlurApplied());
}
/// <summary>
/// Make sure the background is fully invisible (Alpha == 0) when the background should be disabled by the storyboard.
/// </summary>
[Test]
2020-05-26 10:30:36 +08:00
public void TestStoryboardBackgroundVisibility()
{
2019-02-28 17:26:41 +08:00
performFullSetup();
AddAssert("Background retained from song select", () => songSelect.IsBackgroundCurrent());
createFakeStoryboard();
2019-08-12 04:19:22 +08:00
AddStep("Enable Storyboard", () =>
{
player.StoryboardReplacesBackground.Value = true;
player.StoryboardEnabled.Value = true;
});
AddUntilStep("Background is black, storyboard is visible", () => songSelect.IsBackgroundVisible() && songSelect.IsBackgroundBlack() && player.IsStoryboardVisible);
2019-08-12 04:19:22 +08:00
AddStep("Disable Storyboard", () =>
{
player.StoryboardReplacesBackground.Value = false;
player.StoryboardEnabled.Value = false;
});
2020-05-26 10:29:56 +08:00
AddUntilStep("Background is visible, storyboard is invisible", () => songSelect.IsBackgroundVisible() && !player.IsStoryboardVisible);
}
/// <summary>
/// When exiting player, the screen that it suspends/exits to needs to have a fully visible (Alpha == 1) background.
/// </summary>
[Test]
2020-05-26 10:30:36 +08:00
public void TestStoryboardTransition()
{
2019-02-28 17:26:41 +08:00
performFullSetup();
createFakeStoryboard();
AddStep("Exit to song select", () => player.Exit());
2020-05-26 10:29:56 +08:00
AddUntilStep("Background is visible", () => songSelect.IsBackgroundVisible());
}
/// <summary>
/// Ensure <see cref="UserDimContainer"/> is properly accepting user-defined visual changes for a background.
/// </summary>
[Test]
2020-05-26 10:30:36 +08:00
public void TestDisableUserDimBackground()
{
2019-02-28 17:26:41 +08:00
performFullSetup();
2020-05-26 10:29:56 +08:00
AddUntilStep("Screen is dimmed and blur applied", () => songSelect.IsBackgroundDimmed() && songSelect.IsUserBlurApplied());
AddStep("Disable user dim", () => songSelect.IgnoreUserSettings.Value = true);
2020-05-26 10:29:56 +08:00
AddUntilStep("Screen is undimmed and user blur removed", () => songSelect.IsBackgroundUndimmed() && songSelect.IsUserBlurDisabled());
AddStep("Enable user dim", () => songSelect.IgnoreUserSettings.Value = false);
2020-05-26 10:29:56 +08:00
AddUntilStep("Screen is dimmed and blur applied", () => songSelect.IsBackgroundDimmed() && songSelect.IsUserBlurApplied());
2019-02-15 15:17:01 +08:00
}
/// <summary>
/// Ensure <see cref="UserDimContainer"/> is properly accepting user-defined visual changes for a storyboard.
/// </summary>
[Test]
2020-05-26 10:30:36 +08:00
public void TestDisableUserDimStoryboard()
{
performFullSetup();
createFakeStoryboard();
2019-08-12 04:19:22 +08:00
AddStep("Enable Storyboard", () =>
{
player.StoryboardReplacesBackground.Value = true;
player.StoryboardEnabled.Value = true;
});
AddStep("Enable user dim", () => player.DimmableStoryboard.IgnoreUserSettings.Value = false);
AddStep("Set dim level to 1", () => songSelect.DimLevel.Value = 1f);
2020-05-26 10:29:56 +08:00
AddUntilStep("Storyboard is invisible", () => !player.IsStoryboardVisible);
AddStep("Disable user dim", () => player.DimmableStoryboard.IgnoreUserSettings.Value = true);
2020-05-26 10:29:56 +08:00
AddUntilStep("Storyboard is visible", () => player.IsStoryboardVisible);
}
2021-04-13 14:56:43 +08:00
[Test]
public void TestStoryboardIgnoreUserSettings()
{
performFullSetup();
createFakeStoryboard();
AddStep("Enable replacing background", () => player.StoryboardReplacesBackground.Value = true);
2021-04-13 14:56:43 +08:00
AddUntilStep("Storyboard is invisible", () => !player.IsStoryboardVisible);
AddUntilStep("Background is visible", () => songSelect.IsBackgroundVisible());
AddStep("Ignore user settings", () =>
{
player.ApplyToBackground(b => b.IgnoreUserSettings.Value = true);
player.DimmableStoryboard.IgnoreUserSettings.Value = true;
2021-04-13 14:56:43 +08:00
});
AddUntilStep("Storyboard is visible", () => player.IsStoryboardVisible);
AddUntilStep("Background is dimmed", () => songSelect.IsBackgroundVisible() && songSelect.IsBackgroundBlack());
2021-04-13 14:56:43 +08:00
AddStep("Disable background replacement", () => player.StoryboardReplacesBackground.Value = false);
2021-04-13 14:56:43 +08:00
AddUntilStep("Storyboard is visible", () => player.IsStoryboardVisible);
AddUntilStep("Background is visible", () => songSelect.IsBackgroundVisible() && !songSelect.IsBackgroundBlack());
2021-04-13 14:56:43 +08:00
}
2019-02-15 15:50:37 +08:00
/// <summary>
/// Check if the visual settings container retains dim and blur when pausing
2019-02-15 15:50:37 +08:00
/// </summary>
2019-02-15 15:38:27 +08:00
[Test]
2020-05-26 10:30:36 +08:00
public void TestPause()
2019-02-15 15:38:27 +08:00
{
2019-02-28 17:26:41 +08:00
performFullSetup(true);
2019-03-18 10:48:11 +08:00
AddStep("Pause", () => player.Pause());
2020-05-26 10:29:56 +08:00
AddUntilStep("Screen is dimmed and blur applied", () => songSelect.IsBackgroundDimmed() && songSelect.IsUserBlurApplied());
2019-03-18 10:48:11 +08:00
AddStep("Unpause", () => player.Resume());
2020-05-26 10:29:56 +08:00
AddUntilStep("Screen is dimmed and blur applied", () => songSelect.IsBackgroundDimmed() && songSelect.IsUserBlurApplied());
}
/// <summary>
2020-03-17 16:43:16 +08:00
/// Check if the visual settings container removes user dim when suspending <see cref="Player"/> for <see cref="ResultsScreen"/>
/// </summary>
[Test]
2020-05-26 10:30:36 +08:00
public void TestTransition()
{
2019-02-28 17:26:41 +08:00
performFullSetup();
2020-06-11 13:55:49 +08:00
FadeAccessibleResults results = null;
2020-06-11 13:55:49 +08:00
AddStep("Transition to Results", () => player.Push(results = new FadeAccessibleResults(TestResources.CreateTestScoreInfo())));
2020-06-11 13:55:49 +08:00
AddUntilStep("Wait for results is current", () => results.IsCurrentScreen());
2020-05-26 10:29:56 +08:00
AddUntilStep("Screen is undimmed, original background retained", () =>
songSelect.IsBackgroundUndimmed() && songSelect.IsBackgroundCurrent() && songSelect.CheckBackgroundBlur(results.ExpectedBackgroundBlur));
}
/// <summary>
/// Check if hovering on the visual settings dialogue after resuming from player still previews the background dim.
/// </summary>
[Test]
2020-05-26 10:30:36 +08:00
public void TestResumeFromPlayer()
{
performFullSetup();
AddStep("Move mouse to Visual Settings location", () => InputManager.MoveMouseTo(playerLoader.ScreenSpaceDrawQuad.TopRight
+ new Vector2(-playerLoader.VisualSettingsPos.ScreenSpaceDrawQuad.Width,
playerLoader.VisualSettingsPos.ScreenSpaceDrawQuad.Height / 2
)));
AddStep("Resume PlayerLoader", () => player.Restart());
2020-05-26 10:29:56 +08:00
AddUntilStep("Screen is dimmed and blur applied", () => songSelect.IsBackgroundDimmed() && songSelect.IsUserBlurApplied());
AddStep("Move mouse to center of screen", () => InputManager.MoveMouseTo(playerLoader.ScreenPos));
AddUntilStep("Screen is undimmed and user blur removed", () => songSelect.IsBackgroundUndimmed() && songSelect.CheckBackgroundBlur(playerLoader.ExpectedBackgroundBlur));
2019-02-15 15:38:27 +08:00
}
private void createFakeStoryboard() => AddStep("Create storyboard", () =>
{
player.StoryboardEnabled.Value = false;
player.StoryboardReplacesBackground.Value = false;
player.DimmableStoryboard.Add(new OsuSpriteText
{
2019-07-10 11:36:58 +08:00
Size = new Vector2(500, 50),
Alpha = 1,
2019-07-10 11:36:58 +08:00
Colour = Color4.White,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Text = "THIS IS A STORYBOARD",
2019-07-10 11:36:58 +08:00
Font = new FontUsage(size: 50)
});
});
2019-02-28 17:26:41 +08:00
private void performFullSetup(bool allowPause = false)
2019-02-26 08:56:22 +08:00
{
2019-02-28 17:26:41 +08:00
setupUserSettings();
2019-02-26 08:56:22 +08:00
AddStep("Start player loader", () => songSelect.Push(playerLoader = new TestPlayerLoader(player = new LoadBlockingTestPlayer(allowPause))));
2019-03-19 16:24:26 +08:00
AddUntilStep("Wait for Player Loader to load", () => playerLoader.IsLoaded);
AddStep("Move mouse to center of screen", () => InputManager.MoveMouseTo(playerLoader.ScreenPos));
2019-03-19 16:24:26 +08:00
AddUntilStep("Wait for player to load", () => player.IsLoaded);
2019-02-26 08:56:22 +08:00
}
2019-02-28 17:26:41 +08:00
private void setupUserSettings()
2019-02-26 08:56:22 +08:00
{
2020-03-20 14:02:13 +08:00
AddUntilStep("Song select is current", () => songSelect.IsCurrentScreen());
AddUntilStep("Song select has selection", () => songSelect.Carousel?.SelectedBeatmapInfo != null);
2019-02-28 17:26:41 +08:00
AddStep("Set default user settings", () =>
{
SelectedMods.Value = new[] { new OsuModNoFail() };
songSelect.DimLevel.Value = 0.7f;
songSelect.BlurLevel.Value = 0.4f;
});
2019-02-26 08:56:22 +08:00
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
rulesets?.Dispose();
}
2022-11-24 13:32:20 +08:00
private partial class DummySongSelect : PlaySongSelect
{
private FadeAccessibleBackground background;
protected override BackgroundScreen CreateBackground()
{
background = new FadeAccessibleBackground(Beatmap.Value);
IgnoreUserSettings.BindTo(background.IgnoreUserSettings);
return background;
}
public readonly Bindable<bool> IgnoreUserSettings = new Bindable<bool>();
public readonly Bindable<double> DimLevel = new BindableDouble();
public readonly Bindable<double> BlurLevel = new BindableDouble();
public new BeatmapCarousel Carousel => base.Carousel;
[BackgroundDependencyLoader]
private void load(OsuConfigManager config)
{
config.BindWith(OsuSetting.DimLevel, DimLevel);
config.BindWith(OsuSetting.BlurLevel, BlurLevel);
}
public bool IsBackgroundBlack() => background.CurrentColour == OsuColour.Gray(0);
public bool IsBackgroundDimmed() => background.CurrentColour == OsuColour.Gray(1f - background.CurrentDim);
public bool IsBackgroundUndimmed() => background.CurrentColour == Color4.White;
2022-01-11 22:01:17 +08:00
public bool IsUserBlurApplied() => Precision.AlmostEquals(background.CurrentBlur, new Vector2((float)BlurLevel.Value * BackgroundScreenBeatmap.USER_BLUR_FACTOR), 0.1f);
public bool IsUserBlurDisabled() => background.CurrentBlur == new Vector2(0);
public bool IsBackgroundVisible() => background.CurrentAlpha == 1;
2022-01-11 22:01:17 +08:00
public bool IsBackgroundBlur() => Precision.AlmostEquals(background.CurrentBlur, new Vector2(BACKGROUND_BLUR), 0.1f);
2022-01-11 22:01:17 +08:00
public bool CheckBackgroundBlur(Vector2 expected) => Precision.AlmostEquals(background.CurrentBlur, expected, 0.1f);
/// <summary>
/// Make sure every time a screen gets pushed, the background doesn't get replaced
/// </summary>
/// <returns>Whether or not the original background (The one created in DummySongSelect) is still the current background</returns>
public bool IsBackgroundCurrent() => background?.IsCurrentScreen() == true;
}
2019-02-15 15:17:01 +08:00
2022-11-24 13:32:20 +08:00
private partial class FadeAccessibleResults : ResultsScreen
{
public FadeAccessibleResults(ScoreInfo score)
: base(score)
{
AllowRetry = true;
}
2021-01-05 15:00:25 +08:00
protected override BackgroundScreen CreateBackground() => new FadeAccessibleBackground(Beatmap.Value);
public Vector2 ExpectedBackgroundBlur => new Vector2(BACKGROUND_BLUR);
}
2022-11-24 13:32:20 +08:00
private partial class LoadBlockingTestPlayer : TestPlayer
2019-02-15 15:17:01 +08:00
{
protected override BackgroundScreen CreateBackground() =>
new FadeAccessibleBackground(Beatmap.Value);
public override void OnEntering(ScreenTransitionEvent e)
{
base.OnEntering(e);
ApplyToBackground(b => StoryboardReplacesBackground.BindTo(b.StoryboardReplacesBackground));
}
2019-02-15 15:17:01 +08:00
public new DimmableStoryboard DimmableStoryboard => base.DimmableStoryboard;
2019-02-28 15:51:17 +08:00
// Whether or not the player should be allowed to load.
2019-03-27 16:34:58 +08:00
public bool BlockLoad;
public Bindable<bool> StoryboardEnabled;
public readonly Bindable<bool> StoryboardReplacesBackground = new Bindable<bool>();
public readonly Bindable<bool> IsPaused = new Bindable<bool>();
public LoadBlockingTestPlayer(bool allowPause = true)
: base(allowPause)
{
}
public bool IsStoryboardVisible => DimmableStoryboard.ContentDisplayed;
[BackgroundDependencyLoader]
2019-03-27 13:33:50 +08:00
private void load(OsuConfigManager config, CancellationToken token)
2019-02-15 15:17:01 +08:00
{
2019-03-27 16:34:58 +08:00
while (BlockLoad && !token.IsCancellationRequested)
Thread.Sleep(1);
2019-03-27 13:33:50 +08:00
if (!LoadedBeatmapSuccessfully)
return;
StoryboardEnabled = config.GetBindable<bool>(OsuSetting.ShowStoryboard);
DrawableRuleset.IsPaused.BindTo(IsPaused);
2019-02-15 15:17:01 +08:00
}
}
2019-02-15 15:17:01 +08:00
2022-11-24 13:32:20 +08:00
private partial class TestPlayerLoader : PlayerLoader
{
private FadeAccessibleBackground background;
public VisualSettings VisualSettingsPos => VisualSettings;
public BackgroundScreen ScreenPos => background;
public TestPlayerLoader(Player player)
2019-02-26 08:56:22 +08:00
: base(() => player)
2019-02-15 15:38:27 +08:00
{
}
2019-02-21 17:19:50 +08:00
public void TriggerOnHover() => OnHover(new HoverEvent(new InputState()));
public Vector2 ExpectedBackgroundBlur => new Vector2(BACKGROUND_BLUR);
protected override BackgroundScreen CreateBackground() => background = new FadeAccessibleBackground(Beatmap.Value);
}
2022-11-24 13:32:20 +08:00
private partial class FadeAccessibleBackground : BackgroundScreenBeatmap
{
protected override DimmableBackground CreateFadeContainer() => dimmable = new TestDimmableBackground { RelativeSizeAxes = Axes.Both };
public Color4 CurrentColour => dimmable.CurrentColour;
public float CurrentAlpha => dimmable.CurrentAlpha;
public float CurrentDim => dimmable.DimLevel;
public Vector2 CurrentBlur => Background?.BlurSigma ?? Vector2.Zero;
private TestDimmableBackground dimmable;
public FadeAccessibleBackground(WorkingBeatmap beatmap)
: base(beatmap)
{
}
}
2022-11-24 13:32:20 +08:00
private partial class TestDimmableBackground : BackgroundScreenBeatmap.DimmableBackground
{
2019-07-12 10:38:15 +08:00
public Color4 CurrentColour => Content.Colour;
public float CurrentAlpha => Content.Alpha;
public new float DimLevel => base.DimLevel;
}
2019-02-15 15:17:01 +08:00
}
}