1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 13:37:25 +08:00
osu-lazer/osu.Game.Tests/Visual/Gameplay/TestSceneSpectator.cs

174 lines
6.1 KiB
C#
Raw Normal View History

2020-10-26 18:47:39 +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.
using System.Linq;
2020-10-26 18:47:39 +08:00
using NUnit.Framework;
2020-10-26 20:17:12 +08:00
using osu.Framework.Allocation;
using osu.Framework.Screens;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
2020-10-26 20:17:12 +08:00
using osu.Game.Online.Spectator;
using osu.Game.Replays.Legacy;
2020-10-27 13:47:15 +08:00
using osu.Game.Rulesets.Osu;
using osu.Game.Rulesets.Osu.Replays;
2020-10-26 18:47:39 +08:00
using osu.Game.Screens.Play;
using osu.Game.Tests.Beatmaps.IO;
2020-10-26 18:47:39 +08:00
using osu.Game.Users;
namespace osu.Game.Tests.Visual.Gameplay
{
public class TestSceneSpectator : ScreenTestScene
{
2020-10-26 20:17:12 +08:00
[Cached(typeof(SpectatorStreamingClient))]
private TestSpectatorStreamingClient testSpectatorStreamingClient = new TestSpectatorStreamingClient();
2020-10-26 18:47:39 +08:00
private Spectator spectatorScreen;
[Resolved]
private OsuGameBase game { get; set; }
public override void SetUpSteps()
{
base.SetUpSteps();
AddStep("import beatmap", () => ImportBeatmapTest.LoadOszIntoOsu(game, virtualTrack: true).Wait());
AddStep("add streaming client", () =>
{
Remove(testSpectatorStreamingClient);
Add(testSpectatorStreamingClient);
});
}
2020-10-27 13:47:15 +08:00
private OsuFramedReplayInputHandler replayHandler =>
(OsuFramedReplayInputHandler)Stack.ChildrenOfType<OsuInputManager>().First().ReplayInputHandler;
private Player player => Stack.CurrentScreen as Player;
2020-10-26 18:47:39 +08:00
[Test]
2020-10-26 20:27:05 +08:00
public void TestBasicSpectatingFlow()
2020-10-26 18:47:39 +08:00
{
beginSpectating();
2020-10-26 20:17:12 +08:00
AddStep("start play", () => testSpectatorStreamingClient.StartPlay());
AddStep("send frames", () => testSpectatorStreamingClient.SendFrames());
2020-10-27 13:47:15 +08:00
AddUntilStep("wait for player", () => Stack.CurrentScreen is Player);
AddAssert("ensure frames arrived", () => replayHandler.HasFrames);
AddUntilStep("wait for frame starvation", () => replayHandler.NextFrame == null);
AddAssert("game is paused", () => !player.ChildrenOfType<GameplayClockContainer>().First().GameplayClock.IsRunning);
2020-10-26 20:17:12 +08:00
}
2020-10-26 20:27:05 +08:00
[Test]
public void TestSpectatingDuringGameplay()
{
AddStep("start play", () => testSpectatorStreamingClient.StartPlay());
AddStep("send frames", () => testSpectatorStreamingClient.SendFrames());
2020-10-26 20:27:05 +08:00
// should seek immediately to available frames
beginSpectating();
2020-10-26 20:27:05 +08:00
}
[Test]
public void TestHostStartsPlayingWhileAlreadyWatching()
{
beginSpectating();
AddStep("start play", () => testSpectatorStreamingClient.StartPlay());
AddStep("send frames", () => testSpectatorStreamingClient.SendFrames());
AddStep("start play", () => testSpectatorStreamingClient.StartPlay());
AddStep("send frames", () => testSpectatorStreamingClient.SendFrames());
2020-10-26 20:27:05 +08:00
// should restart either immediately or after running out of frames
}
[Test]
public void TestHostFails()
{
beginSpectating();
AddStep("start play", () => testSpectatorStreamingClient.StartPlay());
AddStep("send frames", () => testSpectatorStreamingClient.SendFrames());
// todo: send fail state
2020-10-26 20:27:05 +08:00
// should replay until running out of frames then fail
}
[Test]
public void TestStopWatchingDuringPlay()
{
beginSpectating();
AddStep("start play", () => testSpectatorStreamingClient.StartPlay());
AddStep("send frames", () => testSpectatorStreamingClient.SendFrames());
AddUntilStep("wait for player", () => Stack.CurrentScreen is Player);
2020-10-26 20:27:05 +08:00
// should immediately exit and unbind from streaming client
AddStep("stop spectating", () => (Stack.CurrentScreen as Player)?.Exit());
AddUntilStep("spectating stopped", () => spectatorScreen.GetParentScreen() == null);
}
[Test]
public void TestWatchingBeatmapThatDoesntExistLocally()
{
beginSpectating();
AddStep("start play", () => testSpectatorStreamingClient.StartPlay());
AddStep("send frames", () => testSpectatorStreamingClient.SendFrames());
// player should never arrive.
2020-10-26 20:27:05 +08:00
}
private void beginSpectating() =>
AddStep("load screen", () => LoadScreen(spectatorScreen = new Spectator(testSpectatorStreamingClient.StreamingUser)));
2020-10-26 20:17:12 +08:00
internal class TestSpectatorStreamingClient : SpectatorStreamingClient
{
[Resolved]
private BeatmapManager beatmaps { get; set; }
2020-10-26 20:17:12 +08:00
public readonly User StreamingUser = new User { Id = 1234, Username = "Test user" };
public void StartPlay() => sendState();
public void EndPlay()
{
((ISpectatorClient)this).UserFinishedPlaying((int)StreamingUser.Id, new SpectatorState
{
BeatmapID = beatmaps.GetAllUsableBeatmapSets().First().Beatmaps.First(b => b.RulesetID == 0).OnlineBeatmapID,
RulesetID = 0,
});
2020-10-26 20:17:12 +08:00
}
public void SendFrames()
{
((ISpectatorClient)this).UserSentFrames((int)StreamingUser.Id, new FrameDataBundle(new[]
{
// todo: populate more frames
new LegacyReplayFrame(0, 0, 0, ReplayButtonState.Left1)
}));
}
public override void WatchUser(int userId)
{
// usually the server would do this.
sendState();
base.WatchUser(userId);
}
private void sendState()
{
((ISpectatorClient)this).UserBeganPlaying((int)StreamingUser.Id, new SpectatorState
{
BeatmapID = beatmaps.GetAllUsableBeatmapSets().First().Beatmaps.First(b => b.RulesetID == 0).OnlineBeatmapID,
RulesetID = 0,
});
}
2020-10-26 18:47:39 +08:00
}
}
}