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.
|
|
|
|
|
2020-10-26 20:22:01 +08:00
|
|
|
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;
|
2020-10-26 20:45:37 +08:00
|
|
|
using osu.Framework.Screens;
|
|
|
|
using osu.Framework.Testing;
|
2020-10-26 20:22:01 +08:00
|
|
|
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;
|
2020-10-26 20:45:37 +08:00
|
|
|
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
|
|
|
|
2020-10-26 20:45:37 +08:00
|
|
|
private Spectator spectatorScreen;
|
|
|
|
|
2020-10-26 20:22:01 +08:00
|
|
|
[Resolved]
|
|
|
|
private OsuGameBase game { get; set; }
|
|
|
|
|
2020-10-26 20:45:37 +08:00
|
|
|
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;
|
|
|
|
|
2020-10-27 13:57:23 +08:00
|
|
|
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
|
|
|
{
|
2020-10-26 20:45:37 +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);
|
2020-10-27 13:57:23 +08:00
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2020-10-26 20:45:37 +08:00
|
|
|
AddStep("start play", () => testSpectatorStreamingClient.StartPlay());
|
|
|
|
AddStep("send frames", () => testSpectatorStreamingClient.SendFrames());
|
|
|
|
|
2020-10-26 20:27:05 +08:00
|
|
|
// should seek immediately to available frames
|
2020-10-26 20:45:37 +08:00
|
|
|
beginSpectating();
|
2020-10-26 20:27:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestHostStartsPlayingWhileAlreadyWatching()
|
|
|
|
{
|
2020-10-26 20:45:37 +08:00
|
|
|
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()
|
|
|
|
{
|
2020-10-26 20:45:37 +08:00
|
|
|
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()
|
|
|
|
{
|
2020-10-26 20:45:37 +08:00
|
|
|
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
|
2020-10-26 20:45:37 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-26 20:45:37 +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
|
|
|
|
{
|
2020-10-26 20:22:01 +08:00
|
|
|
[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" };
|
|
|
|
|
2020-10-27 13:57:23 +08:00
|
|
|
public void StartPlay() => sendState();
|
2020-10-26 20:45:37 +08:00
|
|
|
|
|
|
|
public void EndPlay()
|
|
|
|
{
|
|
|
|
((ISpectatorClient)this).UserFinishedPlaying((int)StreamingUser.Id, new SpectatorState
|
|
|
|
{
|
|
|
|
BeatmapID = beatmaps.GetAllUsableBeatmapSets().First().Beatmaps.First(b => b.RulesetID == 0).OnlineBeatmapID,
|
2020-10-26 20:22:01 +08:00
|
|
|
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)
|
|
|
|
}));
|
|
|
|
}
|
2020-10-27 13:57:23 +08:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|