1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 06:47:25 +08:00
osu-lazer/osu.Game.Tests/Visual/Gameplay/TestScenePlayerLoader.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

430 lines
17 KiB
C#
Raw Normal View History

// 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;
2019-05-10 17:21:07 +08:00
using System.Collections.Generic;
2019-04-09 14:05:03 +08:00
using System.Linq;
using System.Threading;
2019-12-06 12:47:34 +08:00
using System.Threading.Tasks;
2019-04-09 12:50:54 +08:00
using NUnit.Framework;
using osu.Framework.Allocation;
2019-09-25 18:24:05 +08:00
using osu.Framework.Audio;
2019-02-12 18:53:08 +08:00
using osu.Framework.Graphics;
2019-09-25 18:24:05 +08:00
using osu.Framework.Graphics.Containers;
2020-01-09 12:43:44 +08:00
using osu.Framework.Utils;
2019-01-23 19:52:00 +08:00
using osu.Framework.Screens;
2020-10-20 06:08:26 +08:00
using osu.Framework.Testing;
2019-10-02 00:15:40 +08:00
using osu.Game.Configuration;
2019-09-25 18:24:05 +08:00
using osu.Game.Graphics.Containers;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
2019-04-09 14:05:03 +08:00
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Osu;
2019-12-06 12:47:34 +08:00
using osu.Game.Rulesets.Osu.Mods;
2019-04-09 14:05:03 +08:00
using osu.Game.Rulesets.Scoring;
using osu.Game.Scoring;
using osu.Game.Screens.Play;
2019-07-05 14:50:31 +08:00
using osu.Game.Screens.Play.PlayerSettings;
using osu.Game.Utils;
2019-09-25 18:24:05 +08:00
using osuTK.Input;
2019-03-25 00:02:36 +08:00
namespace osu.Game.Tests.Visual.Gameplay
{
public class TestScenePlayerLoader : ScreenTestScene
{
2019-07-05 14:50:31 +08:00
private TestPlayerLoader loader;
2019-09-25 18:24:05 +08:00
private TestPlayer player;
2020-10-20 05:53:41 +08:00
private bool epilepsyWarning;
2020-07-21 16:48:11 +08:00
2019-09-25 18:24:05 +08:00
[Resolved]
private AudioManager audioManager { get; set; }
2019-10-02 00:15:40 +08:00
[Resolved]
private SessionStatics sessionStatics { get; set; }
[Cached(typeof(INotificationOverlay))]
private readonly NotificationOverlay notificationOverlay;
[Cached]
private readonly VolumeOverlay volumeOverlay;
[Cached(typeof(BatteryInfo))]
private readonly LocalBatteryInfo batteryInfo = new LocalBatteryInfo();
private readonly ChangelogOverlay changelogOverlay;
public TestScenePlayerLoader()
{
AddRange(new Drawable[]
{
notificationOverlay = new NotificationOverlay
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
},
volumeOverlay = new VolumeOverlay
{
Anchor = Anchor.TopLeft,
Origin = Anchor.TopLeft,
},
changelogOverlay = new ChangelogOverlay()
});
}
[SetUp]
public void Setup() => Schedule(() =>
{
player = null;
audioManager.Volume.SetDefault();
});
2019-09-25 18:24:05 +08:00
/// <summary>
/// Sets the input manager child to a new test player loader container instance.
/// </summary>
/// <param name="interactive">If the test player should behave like the production one.</param>
/// <param name="beforeLoadAction">An action to run before player load but after bindable leases are returned.</param>
private void resetPlayer(bool interactive, Action beforeLoadAction = null)
{
2019-09-25 18:24:05 +08:00
beforeLoadAction?.Invoke();
prepareBeatmap();
LoadScreen(loader = new TestPlayerLoader(() => player = new TestPlayer(interactive, interactive)));
}
private void prepareBeatmap()
{
var workingBeatmap = CreateWorkingBeatmap(new OsuRuleset().RulesetInfo);
workingBeatmap.BeatmapInfo.EpilepsyWarning = epilepsyWarning;
Beatmap.Value = workingBeatmap;
2019-09-25 18:24:05 +08:00
2019-12-13 20:45:38 +08:00
foreach (var mod in SelectedMods.Value.OfType<IApplicableToTrack>())
mod.ApplyToTrack(Beatmap.Value.Track);
2019-09-25 18:24:05 +08:00
}
[Test]
public void TestEarlyExitBeforePlayerConstruction()
{
AddStep("load dummy beatmap", () => resetPlayer(false, () => SelectedMods.Value = new[] { new OsuModNightcore() }));
AddUntilStep("wait for current", () => loader.IsCurrentScreen());
AddStep("exit loader", () => loader.Exit());
AddUntilStep("wait for not current", () => !loader.IsCurrentScreen());
AddAssert("player did not load", () => player == null);
AddUntilStep("player disposed", () => loader.DisposalTask == null);
AddAssert("mod rate still applied", () => Beatmap.Value.Track.Rate != 1);
2019-09-25 18:24:05 +08:00
}
2019-12-06 12:47:34 +08:00
/// <summary>
/// When <see cref="PlayerLoader"/> exits early, it has to wait for the player load task
/// to complete before running disposal on player. This previously caused an issue where mod
/// speed adjustments were undone too late, causing cross-screen pollution.
/// </summary>
[Test]
public void TestEarlyExitAfterPlayerConstruction()
2019-12-06 12:47:34 +08:00
{
AddStep("load dummy beatmap", () => resetPlayer(false, () => SelectedMods.Value = new[] { new OsuModNightcore() }));
2019-12-06 12:47:34 +08:00
AddUntilStep("wait for current", () => loader.IsCurrentScreen());
AddAssert("mod rate applied", () => Beatmap.Value.Track.Rate != 1);
AddUntilStep("wait for non-null player", () => player != null);
2019-12-06 12:47:34 +08:00
AddStep("exit loader", () => loader.Exit());
AddUntilStep("wait for not current", () => !loader.IsCurrentScreen());
AddAssert("player did not load", () => !player.IsLoaded);
AddUntilStep("player disposed", () => loader.DisposalTask?.IsCompleted == true);
AddAssert("mod rate still applied", () => Beatmap.Value.Track.Rate != 1);
}
2019-07-05 14:50:31 +08:00
[Test]
public void TestBlockLoadViaMouseMovement()
{
AddStep("load dummy beatmap", () => resetPlayer(false));
AddUntilStep("wait for current", () => loader.IsCurrentScreen());
2020-02-14 18:02:11 +08:00
AddUntilStep("wait for load ready", () =>
{
moveMouse();
return player?.LoadState == LoadState.Ready;
2020-02-14 18:02:11 +08:00
});
AddRepeatStep("move mouse", moveMouse, 20);
AddAssert("loader still active", () => loader.IsCurrentScreen());
AddUntilStep("loads after idle", () => !loader.IsCurrentScreen());
void moveMouse()
{
InputManager.MoveMouseTo(
loader.VisualSettings.ScreenSpaceDrawQuad.TopLeft
+ (loader.VisualSettings.ScreenSpaceDrawQuad.BottomRight - loader.VisualSettings.ScreenSpaceDrawQuad.TopLeft)
* RNG.NextSingle());
}
}
[Test]
public void TestBlockLoadViaFocus()
{
AddStep("load dummy beatmap", () => resetPlayer(false));
2020-02-14 18:02:11 +08:00
AddUntilStep("wait for current", () => loader.IsCurrentScreen());
AddStep("show focused overlay", () => changelogOverlay.Show());
AddUntilStep("overlay visible", () => changelogOverlay.IsPresent);
2020-02-14 18:02:11 +08:00
AddUntilStep("wait for load ready", () => player?.LoadState == LoadState.Ready);
2020-02-14 18:02:11 +08:00
AddRepeatStep("twiddle thumbs", () => { }, 20);
2019-07-05 14:50:31 +08:00
AddAssert("loader still active", () => loader.IsCurrentScreen());
2020-02-14 18:02:11 +08:00
AddStep("hide overlay", () => changelogOverlay.Hide());
2019-07-05 14:50:31 +08:00
AddUntilStep("loads after idle", () => !loader.IsCurrentScreen());
}
2019-04-09 12:50:54 +08:00
[Test]
public void TestLoadContinuation()
{
SlowLoadPlayer slowPlayer = null;
AddStep("load slow dummy beatmap", () =>
{
prepareBeatmap();
slowPlayer = new SlowLoadPlayer(false, false);
LoadScreen(loader = new TestPlayerLoader(() => slowPlayer));
});
AddStep("schedule slow load", () => Scheduler.AddDelayed(() => slowPlayer.AllowLoad.Set(), 5000));
AddUntilStep("wait for player to be current", () => slowPlayer.IsCurrentScreen());
}
2019-04-09 14:05:03 +08:00
[Test]
public void TestModReinstantiation()
{
TestMod gameMod = null;
TestMod playerMod1 = null;
TestMod playerMod2 = null;
AddStep("load player", () => { resetPlayer(true, () => SelectedMods.Value = new[] { gameMod = new TestMod() }); });
2019-04-09 14:05:03 +08:00
AddUntilStep("wait for loader to become current", () => loader.IsCurrentScreen());
AddStep("mouse in centre", () => InputManager.MoveMouseTo(loader.ScreenSpaceDrawQuad.Centre));
AddUntilStep("wait for player to be current", () => player.IsCurrentScreen());
AddStep("retrieve mods", () => playerMod1 = (TestMod)player.GameplayState.Mods.Single());
2019-04-09 14:05:03 +08:00
AddAssert("game mods not applied", () => gameMod.Applied == false);
AddAssert("player mods applied", () => playerMod1.Applied);
AddStep("restart player", () =>
{
var lastPlayer = player;
2019-04-09 14:05:03 +08:00
player = null;
lastPlayer.Restart();
2019-04-09 14:05:03 +08:00
});
AddUntilStep("wait for player to be current", () => player.IsCurrentScreen());
AddStep("retrieve mods", () => playerMod2 = (TestMod)player.GameplayState.Mods.Single());
2019-04-09 14:05:03 +08:00
AddAssert("game mods not applied", () => gameMod.Applied == false);
AddAssert("player has different mods", () => playerMod1 != playerMod2);
AddAssert("player mods applied", () => playerMod2.Applied);
}
[Test]
public void TestModDisplayChanges()
{
var testMod = new TestMod();
AddStep("load player", () => resetPlayer(true));
AddUntilStep("wait for loader to become current", () => loader.IsCurrentScreen());
AddStep("set test mod in loader", () => loader.Mods.Value = new[] { testMod });
AddAssert("test mod is displayed", () => (TestMod)loader.DisplayedMods.Single() == testMod);
}
2019-09-25 18:24:05 +08:00
[Test]
2020-02-14 18:27:32 +08:00
public void TestMutedNotificationMasterVolume()
{
addVolumeSteps("master volume", () => audioManager.Volume.Value = 0, () => audioManager.Volume.IsDefault);
2020-02-14 18:27:32 +08:00
}
[Test]
2020-02-14 18:27:32 +08:00
public void TestMutedNotificationTrackVolume()
{
addVolumeSteps("music volume", () => audioManager.VolumeTrack.Value = 0, () => audioManager.VolumeTrack.IsDefault);
2020-02-14 18:27:32 +08:00
}
[Test]
2020-02-14 18:27:32 +08:00
public void TestMutedNotificationMuteButton()
{
addVolumeSteps("mute button", () =>
{
// Importantly, in the case the volume is muted but the user has a volume level set, it should be retained.
audioManager.VolumeTrack.Value = 0.5f;
volumeOverlay.IsMuted.Value = true;
}, () => !volumeOverlay.IsMuted.Value && audioManager.VolumeTrack.Value == 0.5f);
2020-02-14 18:27:32 +08:00
}
/// <remarks>
/// Created for avoiding copy pasting code for the same steps.
/// </remarks>
/// <param name="volumeName">What part of the volume system is checked</param>
/// <param name="beforeLoad">The action to be invoked to set the volume before loading</param>
/// <param name="assert">The function to be invoked and checked</param>
private void addVolumeSteps(string volumeName, Action beforeLoad, Func<bool> assert)
2019-09-25 18:24:05 +08:00
{
2019-10-02 00:15:40 +08:00
AddStep("reset notification lock", () => sessionStatics.GetBindable<bool>(Static.MutedAudioNotificationShownOnce).Value = false);
2019-09-25 18:24:05 +08:00
AddStep("load player", () => resetPlayer(false, beforeLoad));
AddUntilStep("wait for player", () => player?.LoadState == LoadState.Ready);
2019-09-25 18:24:05 +08:00
AddAssert("check for notification", () => notificationOverlay.UnreadCount.Value == 1);
2019-09-25 18:24:05 +08:00
AddStep("click notification", () =>
{
var scrollContainer = (OsuScrollContainer)notificationOverlay.Children.Last();
2019-09-25 18:24:05 +08:00
var flowContainer = scrollContainer.Children.OfType<FillFlowContainer<NotificationSection>>().First();
var notification = flowContainer.First();
InputManager.MoveMouseTo(notification);
InputManager.Click(MouseButton.Left);
});
AddAssert("check " + volumeName, assert);
2020-02-14 18:27:32 +08:00
AddUntilStep("wait for player load", () => player.IsLoaded);
2019-09-25 18:24:05 +08:00
}
2020-07-21 16:48:11 +08:00
[TestCase(true)]
[TestCase(false)]
public void TestEpilepsyWarning(bool warning)
{
2020-10-20 05:53:41 +08:00
AddStep("change epilepsy warning", () => epilepsyWarning = warning);
AddStep("load dummy beatmap", () => resetPlayer(false));
2020-10-20 06:08:26 +08:00
2020-07-21 16:48:11 +08:00
AddUntilStep("wait for current", () => loader.IsCurrentScreen());
2020-10-20 06:08:26 +08:00
AddAssert($"epilepsy warning {(warning ? "present" : "absent")}", () => (getWarning() != null) == warning);
2020-10-21 00:27:03 +08:00
if (warning)
{
AddUntilStep("sound volume decreased", () => Beatmap.Value.Track.AggregateVolume.Value == 0.25);
AddUntilStep("sound volume restored", () => Beatmap.Value.Track.AggregateVolume.Value == 1);
}
}
[TestCase(false, 1.0, false)] // not charging, above cutoff --> no warning
[TestCase(true, 0.1, false)] // charging, below cutoff --> no warning
2021-04-12 23:11:22 +08:00
[TestCase(false, 0.25, true)] // not charging, at cutoff --> warning
public void TestLowBatteryNotification(bool isCharging, double chargeLevel, bool shouldWarn)
{
AddStep("reset notification lock", () => sessionStatics.GetBindable<bool>(Static.LowBatteryNotificationShownOnce).Value = false);
// set charge status and level
2021-04-09 08:28:23 +08:00
AddStep("load player", () => resetPlayer(false, () =>
2021-04-09 08:38:16 +08:00
{
batteryInfo.SetCharging(isCharging);
batteryInfo.SetChargeLevel(chargeLevel);
2021-04-09 08:28:23 +08:00
}));
AddUntilStep("wait for player", () => player?.LoadState == LoadState.Ready);
AddAssert($"notification {(shouldWarn ? "triggered" : "not triggered")}", () => notificationOverlay.UnreadCount.Value == (shouldWarn ? 1 : 0));
AddStep("click notification", () =>
{
var scrollContainer = (OsuScrollContainer)notificationOverlay.Children.Last();
var flowContainer = scrollContainer.Children.OfType<FillFlowContainer<NotificationSection>>().First();
var notification = flowContainer.First();
InputManager.MoveMouseTo(notification);
InputManager.Click(MouseButton.Left);
});
AddUntilStep("wait for player load", () => player.IsLoaded);
}
2020-10-21 00:27:03 +08:00
[Test]
public void TestEpilepsyWarningEarlyExit()
{
AddStep("set epilepsy warning", () => epilepsyWarning = true);
AddStep("load dummy beatmap", () => resetPlayer(false));
2020-10-21 00:27:03 +08:00
AddUntilStep("wait for current", () => loader.IsCurrentScreen());
AddUntilStep("wait for epilepsy warning", () => getWarning().Alpha > 0);
AddUntilStep("warning is shown", () => getWarning().State.Value == Visibility.Visible);
2020-10-21 00:27:03 +08:00
AddStep("exit early", () => loader.Exit());
AddUntilStep("warning is hidden", () => getWarning().State.Value == Visibility.Hidden);
2020-10-21 00:27:03 +08:00
AddUntilStep("sound volume restored", () => Beatmap.Value.Track.AggregateVolume.Value == 1);
2020-07-21 16:48:11 +08:00
}
private EpilepsyWarning getWarning() => loader.ChildrenOfType<EpilepsyWarning>().SingleOrDefault();
2019-07-05 14:50:31 +08:00
private class TestPlayerLoader : PlayerLoader
{
public new VisualSettings VisualSettings => base.VisualSettings;
2019-12-06 12:47:34 +08:00
public new Task DisposalTask => base.DisposalTask;
public IReadOnlyList<Mod> DisplayedMods => MetadataInfo.Mods.Value;
2019-07-05 14:50:31 +08:00
public TestPlayerLoader(Func<Player> createPlayer)
: base(createPlayer)
{
}
}
2019-04-09 14:05:03 +08:00
private class TestMod : Mod, IApplicableToScoreProcessor
{
public override string Name => string.Empty;
public override string Acronym => string.Empty;
public override double ScoreMultiplier => 1;
public override string Description => string.Empty;
2019-04-09 14:05:03 +08:00
public bool Applied { get; private set; }
public void ApplyToScoreProcessor(ScoreProcessor scoreProcessor)
{
Applied = true;
}
public ScoreRank AdjustRank(ScoreRank rank, double accuracy) => rank;
2019-04-09 14:05:03 +08:00
}
protected class SlowLoadPlayer : TestPlayer
{
public readonly ManualResetEventSlim AllowLoad = new ManualResetEventSlim(false);
public SlowLoadPlayer(bool allowPause = true, bool showResults = true)
: base(allowPause, showResults)
{
}
[BackgroundDependencyLoader]
private void load()
{
if (!AllowLoad.Wait(TimeSpan.FromSeconds(10)))
throw new TimeoutException();
}
}
/// <summary>
/// Mutable dummy BatteryInfo class for <see cref="TestScenePlayerLoader.TestLowBatteryNotification"/>
/// </summary>
/// <inheritdoc/>
private class LocalBatteryInfo : BatteryInfo
{
private bool isCharging = true;
private double chargeLevel = 1;
public override bool IsCharging => isCharging;
public override double ChargeLevel => chargeLevel;
public void SetCharging(bool value)
{
isCharging = value;
}
public void SetChargeLevel(double value)
{
chargeLevel = value;
}
}
}
}