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

231 lines
8.7 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.
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-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-05-10 17:21:07 +08:00
using osu.Framework.Bindables;
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;
2019-07-05 14:50:31 +08:00
using osu.Framework.MathUtils;
2019-01-23 19:52:00 +08:00
using osu.Framework.Screens;
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-04-09 14:05:03 +08:00
using osu.Game.Rulesets.Scoring;
using osu.Game.Scoring;
using osu.Game.Screens;
using osu.Game.Screens.Play;
2019-07-05 14:50:31 +08:00
using osu.Game.Screens.Play.PlayerSettings;
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 : ManualInputManagerTestScene
{
2019-07-05 14:50:31 +08:00
private TestPlayerLoader loader;
2019-09-25 18:24:05 +08:00
private TestPlayerLoaderContainer container;
private TestPlayer player;
[Resolved]
private AudioManager audioManager { get; set; }
/// <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>
public void ResetPlayer(bool interactive, Action beforeLoadAction = null)
{
2019-09-25 18:24:05 +08:00
audioManager.Volume.SetDefault();
InputManager.Clear();
beforeLoadAction?.Invoke();
Beatmap.Value = CreateWorkingBeatmap(new OsuRuleset().RulesetInfo);
2019-09-25 18:24:05 +08:00
InputManager.Child = container = new TestPlayerLoaderContainer(
loader = new TestPlayerLoader(() => player = new TestPlayer(interactive, interactive)));
}
2019-07-05 14:50:31 +08:00
[Test]
public void TestBlockLoadViaMouseMovement()
{
2019-09-25 18:24:05 +08:00
AddStep("load dummy beatmap", () => ResetPlayer(false));
AddUntilStep("wait for current", () => loader.IsCurrentScreen());
2019-07-05 14:50:31 +08:00
AddRepeatStep("move mouse", () => InputManager.MoveMouseTo(loader.VisualSettings.ScreenSpaceDrawQuad.TopLeft + (loader.VisualSettings.ScreenSpaceDrawQuad.BottomRight - loader.VisualSettings.ScreenSpaceDrawQuad.TopLeft) * RNG.NextSingle()), 20);
AddAssert("loader still active", () => loader.IsCurrentScreen());
AddUntilStep("loads after idle", () => !loader.IsCurrentScreen());
}
2019-04-09 12:50:54 +08:00
[Test]
public void TestLoadContinuation()
{
SlowLoadPlayer slowPlayer = null;
2019-09-25 18:24:05 +08:00
AddStep("load dummy beatmap", () => ResetPlayer(false));
2019-03-19 16:24:26 +08:00
AddUntilStep("wait for current", () => loader.IsCurrentScreen());
AddStep("mouse in centre", () => InputManager.MoveMouseTo(loader.ScreenSpaceDrawQuad.Centre));
AddUntilStep("wait for player to be current", () => player.IsCurrentScreen());
AddStep("load slow dummy beatmap", () =>
{
2019-09-25 18:24:05 +08:00
InputManager.Child = container = new TestPlayerLoaderContainer(
loader = new TestPlayerLoader(() => slowPlayer = new SlowLoadPlayer(false, false)));
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;
2019-09-25 18:24:05 +08:00
AddStep("load player", () => { ResetPlayer(true, () => Mods.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.Mods.Value.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.Mods.Value.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);
}
2019-09-25 18:24:05 +08:00
[Test]
public void TestMutedNotification()
{
AddStep("reset notification", PlayerLoader.ResetNotificationLock);
AddStep("load player", () => ResetPlayer(false, () => audioManager.Volume.Value = 0));
AddUntilStep("wait for player", () => player.IsLoaded);
AddAssert("check for notification", () => container.NotificationOverlay.UnreadCount.Value == 1);
AddStep("click notification", () =>
{
var scrollContainer = (OsuScrollContainer)container.NotificationOverlay.Children.Last();
var flowContainer = scrollContainer.Children.OfType<FillFlowContainer<NotificationSection>>().First();
var notification = flowContainer.First();
InputManager.MoveMouseTo(notification);
InputManager.Click(MouseButton.Left);
});
AddAssert("check master volume", () => audioManager.Volume.IsDefault);
AddStep("restart player", () =>
{
var lastPlayer = player;
player = null;
lastPlayer.Restart();
});
}
private class TestPlayerLoaderContainer : Container
{
[Cached]
public readonly NotificationOverlay NotificationOverlay;
[Cached]
public readonly VolumeOverlay VolumeOverlay;
public TestPlayerLoaderContainer(IScreen screen)
{
RelativeSizeAxes = Axes.Both;
InternalChildren = new Drawable[]
{
new OsuScreenStack(screen)
{
RelativeSizeAxes = Axes.Both,
},
NotificationOverlay = new NotificationOverlay
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
},
VolumeOverlay = new VolumeOverlay
{
Anchor = Anchor.TopLeft,
Origin = Anchor.TopLeft,
}
};
}
}
2019-07-05 14:50:31 +08:00
private class TestPlayerLoader : PlayerLoader
{
public new VisualSettings VisualSettings => base.VisualSettings;
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 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
}
2019-05-10 17:21:07 +08:00
private class TestPlayer : Visual.TestPlayer
{
public new Bindable<IReadOnlyList<Mod>> Mods => base.Mods;
public TestPlayer(bool allowPause = true, bool showResults = true)
: base(allowPause, showResults)
{
}
}
protected class SlowLoadPlayer : Visual.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();
}
}
}
}