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

237 lines
7.3 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.Collections.Generic;
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.Framework.Utils;
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;
using osu.Game.Rulesets.UI;
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
// used just to show beatmap card for the time being.
protected override bool UseOnlineAPI => true;
private Spectator spectatorScreen;
[Resolved]
private OsuGameBase game { get; set; }
private int nextFrame = 0;
public override void SetUpSteps()
{
base.SetUpSteps();
AddStep("reset sent frames", () => nextFrame = 0);
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
{
loadSpectatingScreen();
AddAssert("screen hasn't changed", () => Stack.CurrentScreen is Spectator);
start();
sendFrames();
waitForPlayer();
2020-10-27 13:47:15 +08:00
AddAssert("ensure frames arrived", () => replayHandler.HasFrames);
AddUntilStep("wait for frame starvation", () => replayHandler.NextFrame == null);
checkPaused(true);
sendFrames();
checkPaused(false);
AddUntilStep("wait for frame starvation", () => replayHandler.NextFrame == null);
checkPaused(true);
2020-10-26 20:17:12 +08:00
}
[Test]
public void TestPlayStartsWithNoFrames()
{
loadSpectatingScreen();
start();
waitForPlayer();
AddUntilStep("game is paused", () => player.ChildrenOfType<DrawableRuleset>().First().IsPaused.Value);
sendFrames();
checkPaused(false);
}
2020-10-26 20:27:05 +08:00
[Test]
public void TestSpectatingDuringGameplay()
{
start();
loadSpectatingScreen();
AddStep("advance frame count", () => nextFrame = 300);
sendFrames();
waitForPlayer();
AddUntilStep("playing from correct point in time", () => player.ChildrenOfType<DrawableRuleset>().First().FrameStableClock.CurrentTime > 30000);
2020-10-26 20:27:05 +08:00
}
[Test]
public void TestHostStartsPlayingWhileAlreadyWatching()
{
loadSpectatingScreen();
start();
sendFrames();
start();
sendFrames();
2020-10-26 20:27:05 +08:00
}
[Test]
public void TestHostFails()
{
loadSpectatingScreen();
start();
sendFrames();
// TODO: should replay until running out of frames then fail
2020-10-26 20:27:05 +08:00
}
[Test]
public void TestStopWatchingDuringPlay()
{
loadSpectatingScreen();
start();
sendFrames();
waitForPlayer();
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()
{
loadSpectatingScreen();
start(88);
sendFrames();
AddAssert("screen didn't change", () => Stack.CurrentScreen is Spectator);
2020-10-26 20:27:05 +08:00
}
private void waitForPlayer() => AddUntilStep("wait for player", () => Stack.CurrentScreen is Player);
private void start(int? beatmapId = null) => AddStep("start play", () => testSpectatorStreamingClient.StartPlay(beatmapId));
private void checkPaused(bool state) =>
AddAssert($"game is {(state ? "paused" : "playing")}", () => player.ChildrenOfType<DrawableRuleset>().First().IsPaused.Value == state);
private void sendFrames(int count = 10)
{
AddStep("send frames", () =>
{
testSpectatorStreamingClient.SendFrames(nextFrame, count);
nextFrame += count;
});
}
private void loadSpectatingScreen() =>
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(int? beatmapId = null) => sendState(beatmapId);
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
}
private bool sentState;
public void SendFrames(int index, int count)
2020-10-26 20:17:12 +08:00
{
var frames = new List<LegacyReplayFrame>();
for (int i = index; i < index + count; i++)
2020-10-26 20:17:12 +08:00
{
var buttonState = i == index + count - 1 ? ReplayButtonState.None : ReplayButtonState.Left1;
frames.Add(new LegacyReplayFrame(i * 100, RNG.Next(0, 512), RNG.Next(0, 512), buttonState));
}
var bundle = new FrameDataBundle(frames);
((ISpectatorClient)this).UserSentFrames((int)StreamingUser.Id, bundle);
if (!sentState)
sendState();
2020-10-26 20:17:12 +08:00
}
public override void WatchUser(int userId)
{
if (sentState)
{
// usually the server would do this.
sendState();
}
base.WatchUser(userId);
}
private void sendState(int? beatmapId = null)
{
sentState = true;
((ISpectatorClient)this).UserBeganPlaying((int)StreamingUser.Id, new SpectatorState
{
BeatmapID = beatmapId ?? beatmaps.GetAllUsableBeatmapSets().First().Beatmaps.First(b => b.RulesetID == 0).OnlineBeatmapID,
RulesetID = 0,
});
}
2020-10-26 18:47:39 +08:00
}
}
}