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-27 15:28:11 +08:00
|
|
|
using System.Collections.Generic;
|
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-27 15:28:11 +08:00
|
|
|
using osu.Framework.Utils;
|
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-27 15:28:11 +08:00
|
|
|
using osu.Game.Rulesets.UI;
|
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());
|
2020-10-27 15:28:11 +08:00
|
|
|
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);
|
2020-10-27 15:28:11 +08:00
|
|
|
AddAssert("game is paused", () => player.ChildrenOfType<DrawableRuleset>().First().IsPaused.Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendFrames(int count = 10)
|
|
|
|
{
|
|
|
|
AddStep("send frames", () => testSpectatorStreamingClient.SendFrames(count));
|
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());
|
2020-10-27 15:28:11 +08:00
|
|
|
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());
|
2020-10-27 15:28:11 +08:00
|
|
|
sendFrames();
|
2020-10-26 20:45:37 +08:00
|
|
|
AddStep("start play", () => testSpectatorStreamingClient.StartPlay());
|
2020-10-27 15:28:11 +08:00
|
|
|
sendFrames();
|
2020-10-26 20:27:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestHostFails()
|
|
|
|
{
|
2020-10-26 20:45:37 +08:00
|
|
|
beginSpectating();
|
|
|
|
|
|
|
|
AddStep("start play", () => testSpectatorStreamingClient.StartPlay());
|
2020-10-27 15:28:11 +08:00
|
|
|
sendFrames();
|
2020-10-26 20:45:37 +08:00
|
|
|
|
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());
|
2020-10-27 15:28:11 +08:00
|
|
|
sendFrames();
|
2020-10-26 20:45:37 +08:00
|
|
|
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());
|
2020-10-27 15:28:11 +08:00
|
|
|
sendFrames();
|
2020-10-26 20:45:37 +08:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2020-10-27 15:28:11 +08:00
|
|
|
public void SendFrames(int count)
|
2020-10-26 20:17:12 +08:00
|
|
|
{
|
2020-10-27 15:28:11 +08:00
|
|
|
var frames = new List<LegacyReplayFrame>();
|
|
|
|
|
|
|
|
for (int i = 0; i < count; i++)
|
2020-10-26 20:17:12 +08:00
|
|
|
{
|
2020-10-27 15:28:11 +08:00
|
|
|
frames.Add(new LegacyReplayFrame(i * 100, RNG.Next(0, 512), RNG.Next(0, 512), ReplayButtonState.Left1));
|
|
|
|
}
|
|
|
|
|
|
|
|
frames.Add(new LegacyReplayFrame(count * 100, 0, 0, ReplayButtonState.None));
|
|
|
|
|
|
|
|
var bundle = new FrameDataBundle(frames);
|
|
|
|
((ISpectatorClient)this).UserSentFrames((int)StreamingUser.Id, bundle);
|
2020-10-26 20:17:12 +08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|