osu/osu.Game.Tests/Visual/Gameplay/TestSceneSpectator.cs

264 lines
8.3 KiB
C#
Raw Normal View History

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.
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;
using osu.Framework.Extensions;
using osu.Framework.Graphics;
using osu.Framework.Screens;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
2021-04-01 14:48:26 +00:00
using osu.Game.Database;
using osu.Game.Online.API.Requests.Responses;
2020-10-26 12:17:12 +00:00
using osu.Game.Online.Spectator;
2020-10-27 05:47:15 +00: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 10:47:39 +00:00
using osu.Game.Screens.Play;
using osu.Game.Tests.Beatmaps;
using osu.Game.Tests.Beatmaps.IO;
using osu.Game.Tests.Visual.Multiplayer;
using osu.Game.Tests.Visual.Spectator;
using osuTK;
2020-10-26 10:47:39 +00:00
namespace osu.Game.Tests.Visual.Gameplay
{
public class TestSceneSpectator : ScreenTestScene
{
private readonly APIUser streamingUser = new APIUser { Id = MultiplayerTestScene.PLAYER_1_ID, Username = "Test user" };
2021-04-01 14:48:26 +00: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]
private OsuGameBase game { get; set; }
private TestSpectatorClient spectatorClient;
private SoloSpectator spectatorScreen;
2020-10-28 13:50:57 +00:00
private BeatmapSetInfo importedBeatmap;
2020-10-28 13:50:57 +00:00
private int importedBeatmapId;
[SetUpSteps]
public void SetupSteps()
{
DependenciesScreen dependenciesScreen = null;
AddStep("load dependencies", () =>
2020-10-28 13:50:57 +00:00
{
spectatorClient = new TestSpectatorClient();
// The screen gets suspended so it stops receiving updates.
Child = spectatorClient;
LoadScreen(dependenciesScreen = new DependenciesScreen(spectatorClient));
2020-10-28 13:50:57 +00:00
});
AddUntilStep("wait for dependencies to load", () => dependenciesScreen.IsLoaded);
AddStep("import beatmap", () =>
{
2021-12-17 09:26:12 +00:00
importedBeatmap = BeatmapImportHelper.LoadOszIntoOsu(game, virtualTrack: true).GetResultSafely();
importedBeatmapId = importedBeatmap.Beatmaps.First(b => b.Ruleset.OnlineID == 0).OnlineID;
});
}
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
{
loadSpectatingScreen();
2021-04-01 13:08:52 +00:00
AddAssert("screen hasn't changed", () => Stack.CurrentScreen is SoloSpectator);
start();
waitForPlayer();
2021-06-03 08:27:24 +00:00
sendFrames();
2020-10-27 05:47:15 +00:00
AddAssert("ensure frames arrived", () => replayHandler.HasFrames);
2021-04-12 09:50:25 +00:00
AddUntilStep("wait for frame starvation", () => replayHandler.WaitingForFrame);
checkPaused(true);
double? pausedTime = null;
AddStep("store time", () => pausedTime = currentFrameStableTime);
sendFrames();
2021-04-12 09:50:25 +00:00
AddUntilStep("wait for frame starvation", () => replayHandler.WaitingForFrame);
checkPaused(true);
AddAssert("time advanced", () => currentFrameStableTime > pausedTime);
2020-10-26 12:17:12 +00:00
}
[Test]
public void TestPlayStartsWithNoFrames()
{
loadSpectatingScreen();
start();
waitForPlayer();
checkPaused(true);
2021-06-10 13:41:38 +00:00
// send enough frames to ensure play won't be paused
sendFrames(100);
checkPaused(false);
}
2020-10-26 12:27:05 +00:00
[Test]
public void TestSpectatingDuringGameplay()
{
start();
2021-06-10 13:41:38 +00:00
sendFrames(300);
loadSpectatingScreen();
2021-06-03 08:27:24 +00:00
waitForPlayer();
2021-06-10 13:41:38 +00:00
sendFrames(300);
AddUntilStep("playing from correct point in time", () => player.ChildrenOfType<DrawableRuleset>().First().FrameStableClock.CurrentTime > 30000);
2020-10-26 12:27:05 +00:00
}
[Test]
public void TestHostRetriesWhileWatching()
2020-10-26 12:27:05 +00:00
{
loadSpectatingScreen();
start();
sendFrames();
2020-10-28 13:50:57 +00:00
waitForPlayer();
Player lastPlayer = null;
AddStep("store first player", () => lastPlayer = player);
start();
sendFrames();
waitForPlayer();
AddAssert("player is different", () => lastPlayer != player);
2020-10-26 12:27:05 +00:00
}
[Test]
public void TestHostFails()
{
loadSpectatingScreen();
start();
waitForPlayer();
checkPaused(true);
finish();
checkPaused(false);
// TODO: should replay until running out of frames then fail
2020-10-26 12:27:05 +00:00
}
[Test]
public void TestStopWatchingDuringPlay()
{
loadSpectatingScreen();
start();
sendFrames();
waitForPlayer();
AddStep("stop spectating", () => (Stack.CurrentScreen as Player)?.Exit());
AddUntilStep("spectating stopped", () => spectatorScreen.GetChildScreen() == null);
}
[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();
}
[Test]
public void TestWatchingBeatmapThatDoesntExistLocally()
{
loadSpectatingScreen();
start(-1234);
sendFrames();
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
}
[Test]
public void TestFinalFramesPurgedBeforeEndingPlay()
{
AddStep("begin playing", () => spectatorClient.BeginPlaying(new GameplayState(new TestBeatmap(new OsuRuleset().RulesetInfo), new OsuRuleset()), new Score()));
AddStep("send frames and finish play", () =>
{
spectatorClient.HandleFrame(new OsuReplayFrame(1000, Vector2.Zero));
spectatorClient.EndPlaying();
});
// We can't access API because we're an "online" test.
AddAssert("last received frame has time = 1000", () => spectatorClient.LastReceivedFrame.First().Value.Time == 1000);
}
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;
private void waitForPlayer() => AddUntilStep("wait for player", () => (Stack.CurrentScreen as Player)?.IsLoaded == true);
private void start(int? beatmapId = null) => AddStep("start play", () => spectatorClient.StartPlay(streamingUser.Id, beatmapId ?? importedBeatmapId));
private void finish() => AddStep("end play", () => spectatorClient.EndPlay(streamingUser.Id));
private void checkPaused(bool state) =>
AddUntilStep($"game is {(state ? "paused" : "playing")}", () => player.ChildrenOfType<DrawableRuleset>().First().IsPaused.Value == state);
private void sendFrames(int count = 10)
{
AddStep("send frames", () => spectatorClient.SendFrames(streamingUser.Id, count));
}
private void loadSpectatingScreen()
{
AddStep("load spectator", () => LoadScreen(spectatorScreen = new SoloSpectator(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>
private class DependenciesScreen : OsuScreen
{
[Cached(typeof(SpectatorClient))]
public readonly TestSpectatorClient Client;
public DependenciesScreen(TestSpectatorClient client)
{
Client = client;
}
}
2020-10-26 10:47:39 +00:00
}
}