osu/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectat...

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

157 lines
6.2 KiB
C#
Raw Normal View History

2021-04-06 12:37:21 +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.
2022-06-17 07:37:17 +00:00
#nullable disable
2021-04-07 07:19:10 +00:00
using System.Linq;
2021-04-06 12:37:21 +00:00
using NUnit.Framework;
2021-04-07 07:19:10 +00:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Bindables;
using osu.Framework.Extensions;
2021-04-06 12:37:21 +00:00
using osu.Framework.Graphics;
2021-04-07 07:19:10 +00:00
using osu.Framework.Graphics.Containers;
2022-03-17 10:05:28 +00:00
using osu.Framework.Graphics.Cursor;
2021-04-07 07:19:10 +00:00
using osu.Framework.Platform;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API.Requests.Responses;
2021-04-06 12:37:21 +00:00
using osu.Game.Online.Multiplayer;
2021-04-07 07:19:10 +00:00
using osu.Game.Online.Rooms;
using osu.Game.Rulesets;
2021-04-06 12:37:21 +00:00
using osu.Game.Screens.OnlinePlay.Multiplayer.Match;
2021-04-07 07:19:10 +00:00
using osu.Game.Tests.Resources;
2021-04-06 12:37:21 +00:00
using osuTK;
namespace osu.Game.Tests.Visual.Multiplayer
{
public partial class TestSceneMultiplayerSpectateButton : MultiplayerTestScene
{
2021-04-07 07:19:10 +00:00
private MultiplayerSpectateButton spectateButton;
2022-03-23 01:37:53 +00:00
private MatchStartControl startControl;
2021-04-07 07:19:10 +00:00
private readonly Bindable<PlaylistItem> selectedItem = new Bindable<PlaylistItem>();
private BeatmapSetInfo importedSet;
private BeatmapManager beatmaps;
[BackgroundDependencyLoader]
private void load(GameHost host, AudioManager audio)
{
Dependencies.Cache(new RealmRulesetStore(Realm));
Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, Realm, null, audio, Resources, host, Beatmap.Default));
Dependencies.Cache(Realm);
beatmaps.Import(TestResources.GetQuickTestBeatmapForImport()).WaitSafely();
2021-04-07 07:19:10 +00:00
}
public override void SetUpSteps()
2021-04-06 12:37:21 +00:00
{
base.SetUpSteps();
AddStep("create button", () =>
2021-04-06 12:37:21 +00:00
{
AvailabilityTracker.SelectedItem.BindTo(selectedItem);
2021-04-07 07:19:10 +00:00
importedSet = beatmaps.GetAllUsableBeatmapSets().First();
Beatmap.Value = beatmaps.GetWorkingBeatmap(importedSet.Beatmaps.First());
selectedItem.Value = new PlaylistItem(Beatmap.Value.BeatmapInfo)
{
RulesetID = Beatmap.Value.BeatmapInfo.Ruleset.OnlineID,
};
Child = new PopoverContainer
2021-04-06 12:37:21 +00:00
{
RelativeSizeAxes = Axes.Both,
Child = new FillFlowContainer
2021-04-07 07:19:10 +00:00
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Children = new Drawable[]
2022-03-17 10:05:28 +00:00
{
spectateButton = new MultiplayerSpectateButton
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(200, 50),
},
startControl = new MatchStartControl
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(200, 50),
}
2022-03-17 10:05:28 +00:00
}
2021-04-07 07:19:10 +00:00
}
};
});
}
2021-04-06 12:37:21 +00:00
2021-04-22 15:49:14 +00:00
[TestCase(MultiplayerRoomState.Open)]
[TestCase(MultiplayerRoomState.WaitingForLoad)]
[TestCase(MultiplayerRoomState.Playing)]
public void TestEnabledWhenRoomOpenOrInGameplay(MultiplayerRoomState roomState)
2021-04-07 07:35:36 +00:00
{
AddStep($"change room to {roomState}", () => MultiplayerClient.ChangeRoomState(roomState));
2021-04-07 07:35:36 +00:00
assertSpectateButtonEnablement(true);
}
2021-04-06 12:37:21 +00:00
[TestCase(MultiplayerUserState.Idle)]
[TestCase(MultiplayerUserState.Ready)]
public void TestToggleWhenIdle(MultiplayerUserState initialState)
{
ClickButtonWhenEnabled<MultiplayerSpectateButton>();
AddUntilStep("user is spectating", () => MultiplayerClient.ClientRoom?.Users[0].State == MultiplayerUserState.Spectating);
2021-04-06 12:37:21 +00:00
ClickButtonWhenEnabled<MultiplayerSpectateButton>();
AddUntilStep("user is idle", () => MultiplayerClient.ClientRoom?.Users[0].State == MultiplayerUserState.Idle);
2021-04-06 12:37:21 +00:00
}
2021-04-07 07:35:36 +00:00
[TestCase(MultiplayerRoomState.Closed)]
2021-04-22 15:49:14 +00:00
public void TestDisabledWhenClosed(MultiplayerRoomState roomState)
2021-04-07 07:35:36 +00:00
{
AddStep($"change room to {roomState}", () => MultiplayerClient.ChangeRoomState(roomState));
2021-04-07 07:35:36 +00:00
assertSpectateButtonEnablement(false);
}
[Test]
public void TestReadyButtonDisabledWhenHostAndNoReadyUsers()
{
ClickButtonWhenEnabled<MultiplayerSpectateButton>();
2021-04-07 07:35:36 +00:00
assertReadyButtonEnablement(false);
}
[Test]
public void TestReadyButtonEnabledWhenHostAndUsersReady()
{
AddStep("add user", () => MultiplayerClient.AddUser(new APIUser { Id = PLAYER_1_ID }));
AddStep("set user ready", () => MultiplayerClient.ChangeUserState(PLAYER_1_ID, MultiplayerUserState.Ready));
2021-04-07 07:35:36 +00:00
ClickButtonWhenEnabled<MultiplayerSpectateButton>();
2021-04-07 07:35:36 +00:00
assertReadyButtonEnablement(true);
}
[Test]
public void TestReadyButtonDisabledWhenNotHostAndUsersReady()
{
AddStep("add user and transfer host", () =>
{
MultiplayerClient.AddUser(new APIUser { Id = PLAYER_1_ID });
MultiplayerClient.TransferHost(PLAYER_1_ID);
2021-04-07 07:35:36 +00:00
});
AddStep("set user ready", () => MultiplayerClient.ChangeUserState(PLAYER_1_ID, MultiplayerUserState.Ready));
2021-04-07 07:35:36 +00:00
ClickButtonWhenEnabled<MultiplayerSpectateButton>();
2021-04-07 07:35:36 +00:00
assertReadyButtonEnablement(false);
}
private void assertSpectateButtonEnablement(bool shouldBeEnabled)
=> AddUntilStep($"spectate button {(shouldBeEnabled ? "is" : "is not")} enabled", () => spectateButton.ChildrenOfType<OsuButton>().Single().Enabled.Value == shouldBeEnabled);
2021-04-07 07:35:36 +00:00
2021-04-07 07:19:10 +00:00
private void assertReadyButtonEnablement(bool shouldBeEnabled)
=> AddUntilStep($"ready button {(shouldBeEnabled ? "is" : "is not")} enabled", () => startControl.ChildrenOfType<MultiplayerReadyButton>().Single().Enabled.Value == shouldBeEnabled);
2021-04-06 12:37:21 +00:00
}
}