1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 01:27:29 +08:00
osu-lazer/osu.Game.Tests/Visual/Gameplay/TestSceneSpectator.cs

246 lines
7.4 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;
2021-04-01 22:48:26 +08:00
using System.Threading;
using System.Threading.Tasks;
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.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;
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;
2020-10-26 18:47:39 +08:00
using osu.Game.Screens.Play;
using osu.Game.Tests.Beatmaps.IO;
using osu.Game.Tests.Visual.Multiplayer;
using osu.Game.Tests.Visual.Spectator;
2020-10-26 18:47:39 +08:00
using osu.Game.Users;
namespace osu.Game.Tests.Visual.Gameplay
{
public class TestSceneSpectator : ScreenTestScene
{
private readonly User streamingUser = new User { Id = MultiplayerTestScene.PLAYER_1_ID, Username = "Test user" };
[Cached(typeof(SpectatorClient))]
private TestSpectatorClient testSpectatorClient = new TestSpectatorClient();
2020-10-26 18:47:39 +08:00
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;
2021-04-01 21:08:52 +08:00
private SoloSpectator spectatorScreen;
[Resolved]
private OsuGameBase game { get; set; }
2020-10-27 21:30:45 +08:00
private int nextFrame;
2020-10-28 21:50:57 +08:00
private BeatmapSetInfo importedBeatmap;
private int importedBeatmapId;
public override void SetUpSteps()
{
base.SetUpSteps();
AddStep("reset sent frames", () => nextFrame = 0);
2020-10-28 21:50:57 +08:00
AddStep("import beatmap", () =>
{
importedBeatmap = ImportBeatmapTest.LoadOszIntoOsu(game, virtualTrack: true).Result;
importedBeatmapId = importedBeatmap.Beatmaps.First(b => b.RulesetID == 0).OnlineBeatmapID ?? -1;
});
AddStep("add streaming client", () =>
{
Remove(testSpectatorClient);
Add(testSpectatorClient);
});
finish();
}
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();
2021-04-01 21:08:52 +08:00
AddAssert("screen hasn't changed", () => Stack.CurrentScreen is SoloSpectator);
start();
waitForPlayer();
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 > pausedTime);
2020-10-26 20:17:12 +08:00
}
[Test]
public void TestPlayStartsWithNoFrames()
{
loadSpectatingScreen();
start();
waitForPlayer();
checkPaused(true);
sendFrames(1000); // send enough frames to ensure play won't be paused
checkPaused(false);
}
2020-10-26 20:27:05 +08:00
[Test]
public void TestSpectatingDuringGameplay()
{
start();
loadSpectatingScreen();
2021-06-03 16:27:24 +08:00
waitForPlayer();
AddStep("advance frame count", () => nextFrame = 300);
sendFrames();
AddUntilStep("playing from correct point in time", () => player.ChildrenOfType<DrawableRuleset>().First().FrameStableClock.CurrentTime > 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
waitForPlayer();
Player lastPlayer = null;
AddStep("store first player", () => lastPlayer = player);
start();
sendFrames();
waitForPlayer();
AddAssert("player is different", () => lastPlayer != player);
2020-10-26 20:27:05 +08: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 20:27:05 +08: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 21:08:52 +08:00
AddAssert("screen didn't change", () => Stack.CurrentScreen is SoloSpectator);
2020-10-26 20:27:05 +08: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;
private void waitForPlayer() => AddUntilStep("wait for player", () => (Stack.CurrentScreen as Player)?.IsLoaded == true);
private void start(int? beatmapId = null) => AddStep("start play", () => testSpectatorClient.StartPlay(streamingUser.Id, beatmapId ?? importedBeatmapId));
private void finish() => AddStep("end play", () => testSpectatorClient.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", () =>
{
testSpectatorClient.SendFrames(streamingUser.Id, nextFrame, count);
nextFrame += count;
});
}
private void loadSpectatingScreen()
{
AddStep("load screen", () => LoadScreen(spectatorScreen = new SoloSpectator(streamingUser)));
AddUntilStep("wait for screen load", () => spectatorScreen.LoadState == LoadState.Loaded);
}
2021-04-01 22:48:26 +08: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 18:47:39 +08:00
}
}