1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 14:17:27 +08:00
osu-lazer/osu.Game.Tests/Visual/Gameplay/TestSceneSpectator.cs

432 lines
15 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.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.Extensions;
using osu.Framework.Graphics;
using osu.Framework.Screens;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
2021-04-01 22:48:26 +08:00
using osu.Game.Database;
2024-02-29 05:09:48 +08:00
using osu.Game.Localisation;
using osu.Game.Online.API.Requests.Responses;
2020-10-26 20:17:12 +08:00
using osu.Game.Online.Spectator;
2020-10-27 13:47:15 +08:00
using osu.Game.Rulesets.Osu;
using osu.Game.Rulesets.Osu.Replays;
using osu.Game.Rulesets.UI;
using osu.Game.Scoring;
using osu.Game.Screens;
2020-10-26 18:47:39 +08:00
using osu.Game.Screens.Play;
using osu.Game.Tests.Beatmaps.IO;
using osu.Game.Tests.Gameplay;
using osu.Game.Tests.Visual.Multiplayer;
using osu.Game.Tests.Visual.Spectator;
using osuTK;
2020-10-26 18:47:39 +08:00
namespace osu.Game.Tests.Visual.Gameplay
{
2022-11-24 13:32:20 +08:00
public partial class TestSceneSpectator : ScreenTestScene
2020-10-26 18:47:39 +08:00
{
private readonly APIUser streamingUser = new APIUser { Id = MultiplayerTestScene.PLAYER_1_ID, Username = "Test user" };
2021-04-01 22:48:26 +08:00
[Cached(typeof(UserLookupCache))]
private UserLookupCache lookupCache = new TestUserLookupCache();
// used just to show beatmap card for the time being.
protected override bool UseOnlineAPI => true;
[Resolved]
2024-02-28 15:24:04 +08:00
private OsuGameBase game { get; set; } = null!;
private TestSpectatorClient spectatorClient => dependenciesScreen.SpectatorClient;
2024-02-28 15:24:04 +08:00
private DependenciesScreen dependenciesScreen = null!;
private SoloSpectatorScreen spectatorScreen = null!;
2020-10-28 21:50:57 +08:00
2024-02-28 15:24:04 +08:00
private BeatmapSetInfo importedBeatmap = null!;
2020-10-28 21:50:57 +08:00
private int importedBeatmapId;
[SetUpSteps]
public void SetupSteps()
{
AddStep("load dependencies", () =>
2020-10-28 21:50:57 +08:00
{
LoadScreen(dependenciesScreen = new DependenciesScreen());
// The dependencies screen gets suspended so it stops receiving updates. So its children are manually added to the test scene instead.
Children = new Drawable[]
{
dependenciesScreen.UserLookupCache,
dependenciesScreen.SpectatorClient,
};
2020-10-28 21:50:57 +08:00
});
AddUntilStep("wait for dependencies to load", () => dependenciesScreen.IsLoaded);
AddStep("import beatmap", () =>
{
2021-12-17 17:26:12 +08:00
importedBeatmap = BeatmapImportHelper.LoadOszIntoOsu(game, virtualTrack: true).GetResultSafely();
importedBeatmapId = importedBeatmap.Beatmaps.First(b => b.Ruleset.OnlineID == 0).OnlineID;
});
}
[Test]
public void TestSeekToGameplayStartFramesArriveAfterPlayerLoad()
{
const double gameplay_start = 10000;
loadSpectatingScreen();
start();
waitForPlayerCurrent();
sendFrames(startTime: gameplay_start);
AddAssert("time is greater than seek target", () => currentFrameStableTime, () => Is.GreaterThan(gameplay_start));
}
/// <summary>
/// Tests the same as <see cref="TestSeekToGameplayStartFramesArriveAfterPlayerLoad"/> but with the frames arriving just as <see cref="Player"/> is transitioning into existence.
/// </summary>
[Test]
public void TestSeekToGameplayStartFramesArriveAsPlayerLoaded()
{
const double gameplay_start = 10000;
loadSpectatingScreen();
start();
AddUntilStep("wait for player loader", () => this.ChildrenOfType<PlayerLoader>().SingleOrDefault()?.IsLoaded == true);
AddUntilStep("queue send frames on player load", () =>
{
var loadingPlayer = this.ChildrenOfType<PlayerLoader>().SingleOrDefault()?.CurrentPlayer;
if (loadingPlayer == null)
return false;
loadingPlayer.OnLoadComplete += _ =>
spectatorClient.SendFramesFromUser(streamingUser.Id, 10, gameplay_start);
return true;
});
waitForPlayerCurrent();
AddUntilStep("state is playing", () => spectatorClient.WatchedUserStates[streamingUser.Id].State == SpectatedUserState.Playing);
AddAssert("time is greater than seek target", () => currentFrameStableTime, () => Is.GreaterThan(gameplay_start));
}
2020-10-26 18:47:39 +08:00
[Test]
2020-10-29 14:10:11 +08:00
public void TestFrameStarvationAndResume()
2020-10-26 18:47:39 +08:00
{
loadSpectatingScreen();
AddAssert("screen hasn't changed", () => Stack.CurrentScreen is SoloSpectatorScreen);
start();
waitForPlayerCurrent();
2021-06-03 16:27:24 +08:00
sendFrames();
2020-10-27 13:47:15 +08:00
AddAssert("ensure frames arrived", () => replayHandler.HasFrames);
2021-04-12 17:50:25 +08:00
AddUntilStep("wait for frame starvation", () => replayHandler.WaitingForFrame);
checkPaused(true);
double? pausedTime = null;
AddStep("store time", () => pausedTime = currentFrameStableTime);
sendFrames();
2021-04-12 17:50:25 +08:00
AddUntilStep("wait for frame starvation", () => replayHandler.WaitingForFrame);
checkPaused(true);
AddAssert("time advanced", () => currentFrameStableTime, () => Is.GreaterThan(pausedTime));
2020-10-26 20:17:12 +08:00
}
[Test]
public void TestPlayStartsWithNoFrames()
{
loadSpectatingScreen();
start();
waitForPlayerCurrent();
checkPaused(true);
2021-06-10 21:41:38 +08:00
// send enough frames to ensure play won't be paused
sendFrames(100);
checkPaused(false);
}
2020-10-26 20:27:05 +08:00
[Test]
public void TestSpectatingDuringGameplay()
{
start();
2021-06-10 21:41:38 +08:00
sendFrames(300);
loadSpectatingScreen();
waitForPlayerCurrent();
2021-06-10 21:41:38 +08:00
sendFrames(300);
AddUntilStep("playing from correct point in time", () => player.ChildrenOfType<DrawableRuleset>().First().FrameStableClock.CurrentTime, () => Is.GreaterThan(30000));
2020-10-26 20:27:05 +08:00
}
[Test]
public void TestHostRetriesWhileWatching()
2020-10-26 20:27:05 +08:00
{
loadSpectatingScreen();
start();
sendFrames();
2020-10-28 21:50:57 +08:00
waitForPlayerCurrent();
2024-02-28 15:24:04 +08:00
Player lastPlayer = null!;
AddStep("store first player", () => lastPlayer = player);
start();
sendFrames();
waitForPlayerCurrent();
AddAssert("player is different", () => lastPlayer != player);
2020-10-26 20:27:05 +08:00
}
[Test]
public void TestHostFails()
{
loadSpectatingScreen();
start();
waitForPlayerCurrent();
checkPaused(true);
sendFrames();
2022-02-09 11:09:04 +08:00
finish(SpectatedUserState.Failed);
checkPaused(false); // Should continue playing until out of frames
2022-02-08 20:20:33 +08:00
checkPaused(true); // And eventually stop after running out of frames and fail.
// Todo: Should check for + display a failed message.
2024-02-28 15:16:20 +08:00
2024-02-29 05:09:48 +08:00
AddAssert("fail overlay present", () => player.ChildrenOfType<FailOverlay>().Single().IsPresent);
AddAssert("overlay can only quit", () => player.ChildrenOfType<FailOverlay>().Single().Buttons.Single().Text == GameplayMenuOverlayStrings.Quit);
AddStep("press quit button", () => player.ChildrenOfType<FailOverlay>().Single().Buttons.Single().TriggerClick());
2024-02-28 15:16:20 +08:00
AddAssert("player exited", () => Stack.CurrentScreen is SoloSpectatorScreen);
2020-10-26 20:27:05 +08:00
}
[Test]
public void TestStopWatchingDuringPlay()
{
loadSpectatingScreen();
start();
sendFrames();
waitForPlayerCurrent();
AddStep("stop spectating", () => (Stack.CurrentScreen as Player)?.Exit());
AddUntilStep("spectating stopped", () => spectatorScreen.GetChildScreen() == null);
}
[Test]
public void TestStopWatchingThenHostRetries()
{
loadSpectatingScreen();
start();
sendFrames();
waitForPlayerCurrent();
AddStep("stop spectating", () => (Stack.CurrentScreen as Player)?.Exit());
AddUntilStep("spectating stopped", () => spectatorScreen.GetChildScreen() == null);
// host starts playing a new session
start();
waitForPlayerCurrent();
}
[Test]
public void TestWatchingBeatmapThatDoesntExistLocally()
{
loadSpectatingScreen();
start(-1234);
sendFrames();
AddAssert("screen didn't change", () => Stack.CurrentScreen is SoloSpectatorScreen);
2020-10-26 20:27:05 +08:00
}
[Test]
public void TestFinalFramesPurgedBeforeEndingPlay()
{
2022-12-12 12:59:27 +08:00
AddStep("begin playing", () => spectatorClient.BeginPlaying(0, TestGameplayState.Create(new OsuRuleset()), new Score()));
AddStep("send frames and finish play", () =>
{
spectatorClient.HandleFrame(new OsuReplayFrame(1000, Vector2.Zero));
var completedGameplayState = TestGameplayState.Create(new OsuRuleset());
completedGameplayState.HasPassed = true;
spectatorClient.EndPlaying(completedGameplayState);
});
// We can't access API because we're an "online" test.
AddAssert("last received frame has time = 1000", () => spectatorClient.LastReceivedUserFrames.First().Value.Time == 1000);
}
[Test]
public void TestFinalFrameInBundleHasHeader()
{
2024-02-28 15:24:04 +08:00
FrameDataBundle? lastBundle = null;
AddStep("bind to client", () => spectatorClient.OnNewFrames += (_, bundle) => lastBundle = bundle);
start(-1234);
sendFrames();
finish();
AddUntilStep("bundle received", () => lastBundle != null);
2024-02-28 15:24:04 +08:00
AddAssert("first frame does not have header", () => lastBundle?.Frames[0].Header == null);
AddAssert("last frame has header", () => lastBundle?.Frames[^1].Header != null);
}
2022-02-08 19:27:08 +08:00
[Test]
public void TestPlayingState()
{
loadSpectatingScreen();
start();
sendFrames();
waitForPlayerCurrent();
2022-02-09 11:09:04 +08:00
AddUntilStep("state is playing", () => spectatorClient.WatchedUserStates[streamingUser.Id].State == SpectatedUserState.Playing);
2022-02-08 19:27:08 +08:00
}
[Test]
2022-02-08 19:29:49 +08:00
public void TestPassedState()
2022-02-08 19:27:08 +08:00
{
loadSpectatingScreen();
start();
sendFrames();
waitForPlayerCurrent();
2022-02-08 19:27:08 +08:00
AddStep("send passed", () => spectatorClient.SendEndPlay(streamingUser.Id, SpectatedUserState.Passed));
2022-02-09 11:09:04 +08:00
AddUntilStep("state is passed", () => spectatorClient.WatchedUserStates[streamingUser.Id].State == SpectatedUserState.Passed);
2022-02-08 19:27:08 +08:00
start();
sendFrames();
waitForPlayerCurrent();
2022-02-09 11:09:04 +08:00
AddUntilStep("state is playing", () => spectatorClient.WatchedUserStates[streamingUser.Id].State == SpectatedUserState.Playing);
2022-02-08 19:27:08 +08:00
}
[Test]
public void TestQuitState()
{
loadSpectatingScreen();
start();
sendFrames();
waitForPlayerCurrent();
2022-02-08 19:27:08 +08:00
AddStep("send quit", () => spectatorClient.SendEndPlay(streamingUser.Id));
2022-02-09 11:09:04 +08:00
AddUntilStep("state is quit", () => spectatorClient.WatchedUserStates[streamingUser.Id].State == SpectatedUserState.Quit);
2022-02-08 19:27:08 +08:00
AddAssert("wait for player exit", () => Stack.CurrentScreen is SoloSpectatorScreen);
2022-02-08 19:27:08 +08:00
start();
sendFrames();
waitForPlayerCurrent();
2022-02-09 11:09:04 +08:00
AddUntilStep("state is playing", () => spectatorClient.WatchedUserStates[streamingUser.Id].State == SpectatedUserState.Playing);
2022-02-08 19:27:08 +08:00
}
[Test]
public void TestFailedStateDuringPlay()
2022-02-08 19:27:08 +08:00
{
loadSpectatingScreen();
start();
sendFrames();
waitForPlayerCurrent();
2022-02-08 19:27:08 +08:00
AddStep("send failed", () => spectatorClient.SendEndPlay(streamingUser.Id, SpectatedUserState.Failed));
2022-02-09 11:09:04 +08:00
AddUntilStep("state is failed", () => spectatorClient.WatchedUserStates[streamingUser.Id].State == SpectatedUserState.Failed);
2022-02-08 19:27:08 +08:00
AddUntilStep("wait for player to fail", () => player.GameplayState.HasFailed);
2022-02-08 19:27:08 +08:00
start();
sendFrames();
waitForPlayerCurrent();
AddUntilStep("state is playing", () => spectatorClient.WatchedUserStates[streamingUser.Id].State == SpectatedUserState.Playing);
}
[Test]
public void TestFailedStateDuringLoading()
{
loadSpectatingScreen();
start();
sendFrames();
waitForPlayerLoader();
AddStep("send failed", () => spectatorClient.SendEndPlay(streamingUser.Id, SpectatedUserState.Failed));
AddUntilStep("state is failed", () => spectatorClient.WatchedUserStates[streamingUser.Id].State == SpectatedUserState.Failed);
AddAssert("wait for player exit", () => Stack.CurrentScreen is SoloSpectatorScreen);
start();
sendFrames();
waitForPlayerCurrent();
2022-02-09 11:09:04 +08:00
AddUntilStep("state is playing", () => spectatorClient.WatchedUserStates[streamingUser.Id].State == SpectatedUserState.Playing);
2022-02-08 19:27:08 +08:00
}
private OsuFramedReplayInputHandler replayHandler =>
2024-02-28 15:24:04 +08:00
(OsuFramedReplayInputHandler)Stack.ChildrenOfType<OsuInputManager>().First().ReplayInputHandler!;
private Player player => this.ChildrenOfType<Player>().Single();
private double currentFrameStableTime
=> player.ChildrenOfType<FrameStabilityContainer>().First().CurrentTime;
private void waitForPlayerLoader() => AddUntilStep("wait for loading", () => this.ChildrenOfType<SpectatorPlayerLoader>().SingleOrDefault()?.IsLoaded == true);
private void waitForPlayerCurrent() => AddUntilStep("wait for player current", () => this.ChildrenOfType<Player>().SingleOrDefault()?.IsCurrentScreen() == true);
private void start(int? beatmapId = null) => AddStep("start play", () => spectatorClient.SendStartPlay(streamingUser.Id, beatmapId ?? importedBeatmapId));
private void finish(SpectatedUserState state = SpectatedUserState.Quit) => AddStep("end play", () => spectatorClient.SendEndPlay(streamingUser.Id, state));
private void checkPaused(bool state) =>
AddUntilStep($"game is {(state ? "paused" : "playing")}", () => player.ChildrenOfType<DrawableRuleset>().First().IsPaused.Value == state);
private void sendFrames(int count = 10, double startTime = 0)
{
AddStep("send frames", () => spectatorClient.SendFramesFromUser(streamingUser.Id, count, startTime));
}
private void loadSpectatingScreen()
{
AddStep("load spectator", () => LoadScreen(spectatorScreen = new SoloSpectatorScreen(streamingUser)));
AddUntilStep("wait for screen load", () => spectatorScreen.LoadState == LoadState.Loaded);
}
/// <summary>
/// Used for the sole purpose of adding <see cref="TestSpectatorClient"/> as a resolvable dependency.
/// </summary>
2022-11-24 13:32:20 +08:00
private partial class DependenciesScreen : OsuScreen
{
[Cached(typeof(SpectatorClient))]
public readonly TestSpectatorClient SpectatorClient = new TestSpectatorClient();
[Cached(typeof(UserLookupCache))]
public readonly TestUserLookupCache UserLookupCache = new TestUserLookupCache();
}
2020-10-26 18:47:39 +08:00
}
}