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;
|
2021-04-01 14:48:26 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2020-10-26 10:47:39 +00:00
|
|
|
using NUnit.Framework;
|
2020-10-26 12:17:12 +00:00
|
|
|
using osu.Framework.Allocation;
|
2020-11-02 11:41:14 +00:00
|
|
|
using osu.Framework.Bindables;
|
2020-10-28 07:29:06 +00:00
|
|
|
using osu.Framework.Graphics;
|
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;
|
2021-04-01 14:48:26 +00:00
|
|
|
using osu.Game.Database;
|
2020-12-24 08:58:38 +00:00
|
|
|
using osu.Game.Online;
|
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-12-14 08:33:23 +00:00
|
|
|
using osu.Game.Scoring;
|
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
|
|
|
|
2021-04-01 14:48:26 +00:00
|
|
|
[Cached(typeof(UserLookupCache))]
|
|
|
|
private UserLookupCache lookupCache = new TestUserLookupCache();
|
|
|
|
|
2020-10-27 10:23:35 +00:00
|
|
|
// used just to show beatmap card for the time being.
|
|
|
|
protected override bool UseOnlineAPI => true;
|
|
|
|
|
2021-04-01 13:08:52 +00:00
|
|
|
private SoloSpectator spectatorScreen;
|
2020-10-26 12:45:37 +00:00
|
|
|
|
2020-10-26 12:22:01 +00:00
|
|
|
[Resolved]
|
|
|
|
private OsuGameBase game { get; set; }
|
|
|
|
|
2020-10-27 13:30:45 +00:00
|
|
|
private int nextFrame;
|
2020-10-27 09:13:58 +00:00
|
|
|
|
2020-10-28 13:50:57 +00:00
|
|
|
private BeatmapSetInfo importedBeatmap;
|
|
|
|
|
|
|
|
private int importedBeatmapId;
|
|
|
|
|
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-28 13:50:57 +00:00
|
|
|
AddStep("import beatmap", () =>
|
|
|
|
{
|
|
|
|
importedBeatmap = ImportBeatmapTest.LoadOszIntoOsu(game, virtualTrack: true).Result;
|
|
|
|
importedBeatmapId = importedBeatmap.Beatmaps.First(b => b.RulesetID == 0).OnlineBeatmapID ?? -1;
|
|
|
|
});
|
2020-10-26 12:45:37 +00:00
|
|
|
|
|
|
|
AddStep("add streaming client", () =>
|
|
|
|
{
|
|
|
|
Remove(testSpectatorStreamingClient);
|
|
|
|
Add(testSpectatorStreamingClient);
|
|
|
|
});
|
2020-10-28 14:13:42 +00:00
|
|
|
|
|
|
|
finish();
|
2020-10-26 12:45:37 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 10:47:39 +00:00
|
|
|
[Test]
|
2020-10-29 06:10:11 +00:00
|
|
|
public void TestFrameStarvationAndResume()
|
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
|
|
|
|
2021-04-01 13:08:52 +00:00
|
|
|
AddAssert("screen hasn't changed", () => Stack.CurrentScreen is SoloSpectator);
|
2020-10-27 09:28:49 +00:00
|
|
|
|
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-29 05:57:36 +00:00
|
|
|
double? pausedTime = null;
|
|
|
|
|
|
|
|
AddStep("store time", () => pausedTime = currentFrameStableTime);
|
|
|
|
|
2020-10-27 09:13:58 +00:00
|
|
|
sendFrames();
|
2020-10-27 07:28:11 +00:00
|
|
|
|
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-29 05:57:36 +00:00
|
|
|
|
|
|
|
AddAssert("time advanced", () => currentFrameStableTime > pausedTime);
|
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();
|
2020-10-28 13:59:54 +00:00
|
|
|
checkPaused(true);
|
2020-10-27 09:28:49 +00:00
|
|
|
|
2020-10-29 05:57:36 +00:00
|
|
|
sendFrames(1000); // send enough frames to ensure play won't be paused
|
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 09:56:28 +00:00
|
|
|
|
2020-10-27 08:10:48 +00:00
|
|
|
loadSpectatingScreen();
|
2020-10-27 09:56:28 +00:00
|
|
|
|
|
|
|
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 12:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-10-29 06:08:06 +00:00
|
|
|
public void TestHostRetriesWhileWatching()
|
2020-10-26 12:27:05 +00:00
|
|
|
{
|
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-28 13:50:57 +00:00
|
|
|
|
2020-10-29 06:03:38 +00:00
|
|
|
waitForPlayer();
|
|
|
|
|
|
|
|
Player lastPlayer = null;
|
|
|
|
AddStep("store first player", () => lastPlayer = player);
|
|
|
|
|
2020-10-27 09:32:05 +00:00
|
|
|
start();
|
2020-10-27 07:28:11 +00:00
|
|
|
sendFrames();
|
2020-10-29 06:03:38 +00:00
|
|
|
|
|
|
|
waitForPlayer();
|
|
|
|
AddAssert("player is different", () => lastPlayer != player);
|
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-26 12:45:37 +00:00
|
|
|
|
2020-10-28 13:59:54 +00:00
|
|
|
waitForPlayer();
|
|
|
|
checkPaused(true);
|
|
|
|
|
|
|
|
finish();
|
|
|
|
|
|
|
|
checkPaused(false);
|
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-27 10:23:35 +00:00
|
|
|
|
2020-10-26 12:45:37 +00:00
|
|
|
AddStep("stop spectating", () => (Stack.CurrentScreen as Player)?.Exit());
|
2020-10-29 06:08:06 +00:00
|
|
|
AddUntilStep("spectating stopped", () => spectatorScreen.GetChildScreen() == null);
|
2020-10-26 12:45:37 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 06:09:12 +00:00
|
|
|
[Test]
|
|
|
|
public void TestStopWatchingThenHostRetries()
|
|
|
|
{
|
|
|
|
loadSpectatingScreen();
|
|
|
|
|
|
|
|
start();
|
|
|
|
sendFrames();
|
|
|
|
waitForPlayer();
|
|
|
|
|
|
|
|
AddStep("stop spectating", () => (Stack.CurrentScreen as Player)?.Exit());
|
|
|
|
AddUntilStep("spectating stopped", () => spectatorScreen.GetChildScreen() == null);
|
|
|
|
|
|
|
|
// host starts playing a new session
|
|
|
|
start();
|
|
|
|
waitForPlayer();
|
|
|
|
}
|
|
|
|
|
2020-10-26 12:45:37 +00:00
|
|
|
[Test]
|
|
|
|
public void TestWatchingBeatmapThatDoesntExistLocally()
|
|
|
|
{
|
2020-10-27 08:10:48 +00:00
|
|
|
loadSpectatingScreen();
|
2020-10-26 12:45:37 +00:00
|
|
|
|
2020-10-28 13:51:35 +00:00
|
|
|
start(-1234);
|
2020-10-27 07:28:11 +00:00
|
|
|
sendFrames();
|
2020-10-27 10:23:35 +00:00
|
|
|
|
2021-04-01 13:08:52 +00:00
|
|
|
AddAssert("screen didn't change", () => Stack.CurrentScreen is SoloSpectator);
|
2020-10-26 12:27:05 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 06:10:42 +00:00
|
|
|
private OsuFramedReplayInputHandler replayHandler =>
|
|
|
|
(OsuFramedReplayInputHandler)Stack.ChildrenOfType<OsuInputManager>().First().ReplayInputHandler;
|
|
|
|
|
|
|
|
private Player player => Stack.CurrentScreen as Player;
|
|
|
|
|
|
|
|
private double currentFrameStableTime
|
|
|
|
=> player.ChildrenOfType<FrameStabilityContainer>().First().FrameStableClock.CurrentTime;
|
|
|
|
|
2020-10-27 09:32:05 +00:00
|
|
|
private void waitForPlayer() => AddUntilStep("wait for player", () => Stack.CurrentScreen is Player);
|
|
|
|
|
2020-10-28 13:50:57 +00:00
|
|
|
private void start(int? beatmapId = null) => AddStep("start play", () => testSpectatorStreamingClient.StartPlay(beatmapId ?? importedBeatmapId));
|
2020-10-27 09:32:05 +00:00
|
|
|
|
2020-10-28 13:59:54 +00:00
|
|
|
private void finish(int? beatmapId = null) => AddStep("end play", () => testSpectatorStreamingClient.EndPlay(beatmapId ?? importedBeatmapId));
|
|
|
|
|
2020-10-27 09:32:05 +00:00
|
|
|
private void checkPaused(bool state) =>
|
2020-10-28 13:59:54 +00:00
|
|
|
AddUntilStep($"game is {(state ? "paused" : "playing")}", () => player.ChildrenOfType<DrawableRuleset>().First().IsPaused.Value == state);
|
2020-10-27 09:32:05 +00:00
|
|
|
|
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-28 07:29:06 +00:00
|
|
|
private void loadSpectatingScreen()
|
|
|
|
{
|
2021-04-01 13:08:52 +00:00
|
|
|
AddStep("load screen", () => LoadScreen(spectatorScreen = new SoloSpectator(testSpectatorStreamingClient.StreamingUser)));
|
2020-10-28 07:29:06 +00:00
|
|
|
AddUntilStep("wait for screen load", () => spectatorScreen.LoadState == LoadState.Loaded);
|
|
|
|
}
|
2020-10-26 12:45:37 +00:00
|
|
|
|
2020-11-02 12:09:47 +00:00
|
|
|
public class TestSpectatorStreamingClient : SpectatorStreamingClient
|
2020-10-26 12:17:12 +00:00
|
|
|
{
|
2020-12-16 04:00:19 +00:00
|
|
|
public readonly User StreamingUser = new User { Id = 55, Username = "Test user" };
|
2020-10-26 12:17:12 +00:00
|
|
|
|
2020-11-02 11:41:14 +00:00
|
|
|
public new BindableList<int> PlayingUsers => (BindableList<int>)base.PlayingUsers;
|
|
|
|
|
2020-10-28 13:50:57 +00:00
|
|
|
private int beatmapId;
|
|
|
|
|
2020-12-24 08:58:38 +00:00
|
|
|
public TestSpectatorStreamingClient()
|
|
|
|
: base(new DevelopmentEndpointConfiguration())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-10-28 13:50:57 +00:00
|
|
|
public void StartPlay(int beatmapId)
|
|
|
|
{
|
|
|
|
this.beatmapId = beatmapId;
|
|
|
|
sendState(beatmapId);
|
|
|
|
}
|
2020-10-26 12:45:37 +00:00
|
|
|
|
2020-10-28 13:50:57 +00:00
|
|
|
public void EndPlay(int beatmapId)
|
2020-10-26 12:45:37 +00:00
|
|
|
{
|
2020-11-06 03:40:54 +00:00
|
|
|
((ISpectatorClient)this).UserFinishedPlaying(StreamingUser.Id, new SpectatorState
|
2020-10-26 12:45:37 +00:00
|
|
|
{
|
2020-10-28 13:50:57 +00:00
|
|
|
BeatmapID = beatmapId,
|
2020-10-26 12:22:01 +00:00
|
|
|
RulesetID = 0,
|
|
|
|
});
|
2020-10-28 14:13:42 +00:00
|
|
|
|
|
|
|
sentState = false;
|
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
|
|
|
|
2020-12-14 08:33:23 +00:00
|
|
|
var bundle = new FrameDataBundle(new ScoreInfo(), frames);
|
2020-11-06 03:40:54 +00:00
|
|
|
((ISpectatorClient)this).UserSentFrames(StreamingUser.Id, bundle);
|
2020-10-27 08:10:48 +00:00
|
|
|
|
|
|
|
if (!sentState)
|
2020-10-28 13:50:57 +00:00
|
|
|
sendState(beatmapId);
|
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.
|
2020-10-28 13:50:57 +00:00
|
|
|
sendState(beatmapId);
|
2020-10-27 08:10:48 +00:00
|
|
|
}
|
2020-10-27 05:57:23 +00:00
|
|
|
|
|
|
|
base.WatchUser(userId);
|
|
|
|
}
|
|
|
|
|
2020-10-28 13:50:57 +00:00
|
|
|
private void sendState(int beatmapId)
|
2020-10-27 05:57:23 +00:00
|
|
|
{
|
2020-10-27 08:10:48 +00:00
|
|
|
sentState = true;
|
2020-11-06 03:40:54 +00:00
|
|
|
((ISpectatorClient)this).UserBeganPlaying(StreamingUser.Id, new SpectatorState
|
2020-10-27 05:57:23 +00:00
|
|
|
{
|
2020-10-28 13:50:57 +00:00
|
|
|
BeatmapID = beatmapId,
|
2020-10-27 05:57:23 +00:00
|
|
|
RulesetID = 0,
|
|
|
|
});
|
|
|
|
}
|
2020-10-26 10:47:39 +00:00
|
|
|
}
|
2021-04-01 14:48:26 +00:00
|
|
|
|
|
|
|
internal class TestUserLookupCache : UserLookupCache
|
|
|
|
{
|
|
|
|
protected override Task<User> ComputeValueAsync(int lookup, CancellationToken token = default) => Task.FromResult(new User
|
|
|
|
{
|
|
|
|
Id = lookup,
|
|
|
|
Username = $"User {lookup}"
|
|
|
|
});
|
|
|
|
}
|
2020-10-26 10:47:39 +00:00
|
|
|
}
|
|
|
|
}
|