2020-10-26 10:47:39 +00: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-27 07:28:11 +00:00
|
|
|
using System.Collections.Generic;
|
2020-10-26 12:22:01 +00:00
|
|
|
using System.Linq;
|
2020-10-26 10:47:39 +00:00
|
|
|
using NUnit.Framework;
|
2020-10-26 12:17:12 +00:00
|
|
|
using osu.Framework.Allocation;
|
2020-10-26 12:45:37 +00:00
|
|
|
using osu.Framework.Screens;
|
|
|
|
using osu.Framework.Testing;
|
2020-10-27 07:28:11 +00:00
|
|
|
using osu.Framework.Utils;
|
2020-10-26 12:22:01 +00:00
|
|
|
using osu.Game.Beatmaps;
|
2020-10-26 12:17:12 +00:00
|
|
|
using osu.Game.Online.Spectator;
|
|
|
|
using osu.Game.Replays.Legacy;
|
2020-10-27 05:47:15 +00:00
|
|
|
using osu.Game.Rulesets.Osu;
|
|
|
|
using osu.Game.Rulesets.Osu.Replays;
|
2020-10-27 07:28:11 +00:00
|
|
|
using osu.Game.Rulesets.UI;
|
2020-10-26 10:47:39 +00:00
|
|
|
using osu.Game.Screens.Play;
|
2020-10-26 12:45:37 +00:00
|
|
|
using osu.Game.Tests.Beatmaps.IO;
|
2020-10-26 10:47:39 +00:00
|
|
|
using osu.Game.Users;
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.Visual.Gameplay
|
|
|
|
{
|
|
|
|
public class TestSceneSpectator : ScreenTestScene
|
|
|
|
{
|
2020-10-26 12:17:12 +00:00
|
|
|
[Cached(typeof(SpectatorStreamingClient))]
|
|
|
|
private TestSpectatorStreamingClient testSpectatorStreamingClient = new TestSpectatorStreamingClient();
|
2020-10-26 10:47:39 +00:00
|
|
|
|
2020-10-26 12:45:37 +00:00
|
|
|
private Spectator spectatorScreen;
|
|
|
|
|
2020-10-26 12:22:01 +00:00
|
|
|
[Resolved]
|
|
|
|
private OsuGameBase game { get; set; }
|
|
|
|
|
2020-10-27 09:13:58 +00:00
|
|
|
private int nextFrame = 0;
|
|
|
|
|
2020-10-26 12:45:37 +00:00
|
|
|
public override void SetUpSteps()
|
|
|
|
{
|
|
|
|
base.SetUpSteps();
|
|
|
|
|
2020-10-27 09:13:58 +00:00
|
|
|
AddStep("reset sent frames", () => nextFrame = 0);
|
|
|
|
|
2020-10-26 12:45:37 +00:00
|
|
|
AddStep("import beatmap", () => ImportBeatmapTest.LoadOszIntoOsu(game, virtualTrack: true).Wait());
|
|
|
|
|
|
|
|
AddStep("add streaming client", () =>
|
|
|
|
{
|
|
|
|
Remove(testSpectatorStreamingClient);
|
|
|
|
Add(testSpectatorStreamingClient);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-27 05:47:15 +00:00
|
|
|
private OsuFramedReplayInputHandler replayHandler =>
|
|
|
|
(OsuFramedReplayInputHandler)Stack.ChildrenOfType<OsuInputManager>().First().ReplayInputHandler;
|
|
|
|
|
2020-10-27 05:57:23 +00:00
|
|
|
private Player player => Stack.CurrentScreen as Player;
|
|
|
|
|
2020-10-26 10:47:39 +00:00
|
|
|
[Test]
|
2020-10-26 12:27:05 +00:00
|
|
|
public void TestBasicSpectatingFlow()
|
2020-10-26 10:47:39 +00:00
|
|
|
{
|
2020-10-27 08:10:48 +00:00
|
|
|
loadSpectatingScreen();
|
2020-10-27 09:28:49 +00:00
|
|
|
|
|
|
|
AddAssert("screen hasn't changed", () => Stack.CurrentScreen is Spectator);
|
|
|
|
|
2020-10-27 09:32:05 +00:00
|
|
|
start();
|
2020-10-27 07:28:11 +00:00
|
|
|
sendFrames();
|
2020-10-27 09:13:58 +00:00
|
|
|
|
2020-10-27 09:32:05 +00:00
|
|
|
waitForPlayer();
|
2020-10-27 05:47:15 +00:00
|
|
|
AddAssert("ensure frames arrived", () => replayHandler.HasFrames);
|
2020-10-27 05:57:23 +00:00
|
|
|
|
|
|
|
AddUntilStep("wait for frame starvation", () => replayHandler.NextFrame == null);
|
2020-10-27 09:32:05 +00:00
|
|
|
checkPaused(true);
|
|
|
|
|
2020-10-27 09:13:58 +00:00
|
|
|
sendFrames();
|
2020-10-27 07:28:11 +00:00
|
|
|
|
2020-10-27 09:32:05 +00:00
|
|
|
checkPaused(false);
|
2020-10-27 09:13:58 +00:00
|
|
|
AddUntilStep("wait for frame starvation", () => replayHandler.NextFrame == null);
|
2020-10-27 09:32:05 +00:00
|
|
|
checkPaused(true);
|
2020-10-26 12:17:12 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 09:28:49 +00:00
|
|
|
[Test]
|
|
|
|
public void TestPlayStartsWithNoFrames()
|
|
|
|
{
|
|
|
|
loadSpectatingScreen();
|
|
|
|
|
2020-10-27 09:32:05 +00:00
|
|
|
start();
|
|
|
|
waitForPlayer();
|
|
|
|
AddUntilStep("game is paused", () => player.ChildrenOfType<DrawableRuleset>().First().IsPaused.Value);
|
2020-10-27 09:28:49 +00:00
|
|
|
|
2020-10-27 09:32:05 +00:00
|
|
|
sendFrames();
|
2020-10-27 09:28:49 +00:00
|
|
|
|
2020-10-27 09:32:05 +00:00
|
|
|
checkPaused(false);
|
2020-10-27 09:28:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 12:27:05 +00:00
|
|
|
[Test]
|
|
|
|
public void TestSpectatingDuringGameplay()
|
|
|
|
{
|
2020-10-27 09:32:05 +00:00
|
|
|
start();
|
2020-10-27 07:28:11 +00:00
|
|
|
sendFrames();
|
2020-10-26 12:27:05 +00:00
|
|
|
// should seek immediately to available frames
|
2020-10-27 08:10:48 +00:00
|
|
|
loadSpectatingScreen();
|
2020-10-26 12:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestHostStartsPlayingWhileAlreadyWatching()
|
|
|
|
{
|
2020-10-27 08:10:48 +00:00
|
|
|
loadSpectatingScreen();
|
2020-10-26 12:45:37 +00:00
|
|
|
|
2020-10-27 09:32:05 +00:00
|
|
|
start();
|
2020-10-27 07:28:11 +00:00
|
|
|
sendFrames();
|
2020-10-27 09:32:05 +00:00
|
|
|
start();
|
2020-10-27 07:28:11 +00:00
|
|
|
sendFrames();
|
2020-10-26 12:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestHostFails()
|
|
|
|
{
|
2020-10-27 08:10:48 +00:00
|
|
|
loadSpectatingScreen();
|
2020-10-26 12:45:37 +00:00
|
|
|
|
2020-10-27 09:32:05 +00:00
|
|
|
start();
|
2020-10-27 07:28:11 +00:00
|
|
|
sendFrames();
|
2020-10-26 12:45:37 +00:00
|
|
|
|
2020-10-27 09:32:05 +00:00
|
|
|
// TODO: should replay until running out of frames then fail
|
2020-10-26 12:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestStopWatchingDuringPlay()
|
|
|
|
{
|
2020-10-27 08:10:48 +00:00
|
|
|
loadSpectatingScreen();
|
2020-10-26 12:45:37 +00:00
|
|
|
|
2020-10-27 09:32:05 +00:00
|
|
|
start();
|
2020-10-27 07:28:11 +00:00
|
|
|
sendFrames();
|
2020-10-27 09:32:05 +00:00
|
|
|
waitForPlayer();
|
2020-10-26 12:27:05 +00:00
|
|
|
// should immediately exit and unbind from streaming client
|
2020-10-26 12:45:37 +00:00
|
|
|
AddStep("stop spectating", () => (Stack.CurrentScreen as Player)?.Exit());
|
|
|
|
|
|
|
|
AddUntilStep("spectating stopped", () => spectatorScreen.GetParentScreen() == null);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestWatchingBeatmapThatDoesntExistLocally()
|
|
|
|
{
|
2020-10-27 08:10:48 +00:00
|
|
|
loadSpectatingScreen();
|
2020-10-26 12:45:37 +00:00
|
|
|
|
2020-10-27 09:32:05 +00:00
|
|
|
start();
|
2020-10-27 07:28:11 +00:00
|
|
|
sendFrames();
|
2020-10-26 12:45:37 +00:00
|
|
|
// player should never arrive.
|
2020-10-26 12:27:05 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 09:32:05 +00:00
|
|
|
private void waitForPlayer() => AddUntilStep("wait for player", () => Stack.CurrentScreen is Player);
|
|
|
|
|
|
|
|
private void start() => AddStep("start play", () => testSpectatorStreamingClient.StartPlay());
|
|
|
|
|
|
|
|
private void checkPaused(bool state) =>
|
|
|
|
AddAssert($"game is {(state ? "paused" : "playing")}", () => player.ChildrenOfType<DrawableRuleset>().First().IsPaused.Value == state);
|
|
|
|
|
2020-10-27 09:13:58 +00:00
|
|
|
private void sendFrames(int count = 10)
|
|
|
|
{
|
|
|
|
AddStep("send frames", () =>
|
|
|
|
{
|
|
|
|
testSpectatorStreamingClient.SendFrames(nextFrame, count);
|
|
|
|
nextFrame += count;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-27 08:10:48 +00:00
|
|
|
private void loadSpectatingScreen() =>
|
2020-10-26 12:45:37 +00:00
|
|
|
AddStep("load screen", () => LoadScreen(spectatorScreen = new Spectator(testSpectatorStreamingClient.StreamingUser)));
|
|
|
|
|
2020-10-26 12:17:12 +00:00
|
|
|
internal class TestSpectatorStreamingClient : SpectatorStreamingClient
|
|
|
|
{
|
2020-10-26 12:22:01 +00:00
|
|
|
[Resolved]
|
|
|
|
private BeatmapManager beatmaps { get; set; }
|
|
|
|
|
2020-10-26 12:17:12 +00:00
|
|
|
public readonly User StreamingUser = new User { Id = 1234, Username = "Test user" };
|
|
|
|
|
2020-10-27 05:57:23 +00:00
|
|
|
public void StartPlay() => sendState();
|
2020-10-26 12:45:37 +00: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 12:22:01 +00:00
|
|
|
RulesetID = 0,
|
|
|
|
});
|
2020-10-26 12:17:12 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 08:10:48 +00:00
|
|
|
private bool sentState;
|
|
|
|
|
2020-10-27 09:13:58 +00:00
|
|
|
public void SendFrames(int index, int count)
|
2020-10-26 12:17:12 +00:00
|
|
|
{
|
2020-10-27 07:28:11 +00:00
|
|
|
var frames = new List<LegacyReplayFrame>();
|
|
|
|
|
2020-10-27 09:13:58 +00:00
|
|
|
for (int i = index; i < index + count; i++)
|
2020-10-26 12:17:12 +00:00
|
|
|
{
|
2020-10-27 09:13:58 +00:00
|
|
|
var buttonState = i == index + count - 1 ? ReplayButtonState.None : ReplayButtonState.Left1;
|
2020-10-27 07:28:11 +00:00
|
|
|
|
2020-10-27 09:13:58 +00:00
|
|
|
frames.Add(new LegacyReplayFrame(i * 100, RNG.Next(0, 512), RNG.Next(0, 512), buttonState));
|
|
|
|
}
|
2020-10-27 07:28:11 +00:00
|
|
|
|
|
|
|
var bundle = new FrameDataBundle(frames);
|
|
|
|
((ISpectatorClient)this).UserSentFrames((int)StreamingUser.Id, bundle);
|
2020-10-27 08:10:48 +00:00
|
|
|
|
|
|
|
if (!sentState)
|
|
|
|
sendState();
|
2020-10-26 12:17:12 +00:00
|
|
|
}
|
2020-10-27 05:57:23 +00:00
|
|
|
|
|
|
|
public override void WatchUser(int userId)
|
|
|
|
{
|
2020-10-27 08:10:48 +00:00
|
|
|
if (sentState)
|
|
|
|
{
|
|
|
|
// usually the server would do this.
|
|
|
|
sendState();
|
|
|
|
}
|
2020-10-27 05:57:23 +00:00
|
|
|
|
|
|
|
base.WatchUser(userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendState()
|
|
|
|
{
|
2020-10-27 08:10:48 +00:00
|
|
|
sentState = true;
|
2020-10-27 05:57:23 +00:00
|
|
|
((ISpectatorClient)this).UserBeganPlaying((int)StreamingUser.Id, new SpectatorState
|
|
|
|
{
|
|
|
|
BeatmapID = beatmaps.GetAllUsableBeatmapSets().First().Beatmaps.First(b => b.RulesetID == 0).OnlineBeatmapID,
|
|
|
|
RulesetID = 0,
|
|
|
|
});
|
|
|
|
}
|
2020-10-26 10:47:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|