2021-04-06 20:37:21 +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;
|
2021-04-07 15:19:10 +08:00
|
|
|
using System.Linq;
|
2021-07-02 13:43:48 +08:00
|
|
|
using System.Threading.Tasks;
|
2021-04-06 20:37:21 +08:00
|
|
|
using NUnit.Framework;
|
2021-04-07 15:19:10 +08:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Audio;
|
|
|
|
using osu.Framework.Bindables;
|
2022-01-03 16:02:15 +08:00
|
|
|
using osu.Framework.Extensions;
|
2021-04-06 20:37:21 +08:00
|
|
|
using osu.Framework.Graphics;
|
2021-04-07 15:19:10 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Platform;
|
|
|
|
using osu.Framework.Testing;
|
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
2021-11-04 17:02:44 +08:00
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2021-04-06 20:37:21 +08:00
|
|
|
using osu.Game.Online.Multiplayer;
|
2021-04-07 15:19:10 +08:00
|
|
|
using osu.Game.Online.Rooms;
|
|
|
|
using osu.Game.Rulesets;
|
2021-04-06 20:37:21 +08:00
|
|
|
using osu.Game.Screens.OnlinePlay.Multiplayer.Match;
|
2021-04-07 15:19:10 +08:00
|
|
|
using osu.Game.Tests.Resources;
|
2021-04-06 20:37:21 +08:00
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.Visual.Multiplayer
|
|
|
|
{
|
|
|
|
public class TestSceneMultiplayerSpectateButton : MultiplayerTestScene
|
|
|
|
{
|
2021-04-07 15:19:10 +08:00
|
|
|
private MultiplayerSpectateButton spectateButton;
|
|
|
|
private MultiplayerReadyButton readyButton;
|
|
|
|
|
|
|
|
private readonly Bindable<PlaylistItem> selectedItem = new Bindable<PlaylistItem>();
|
|
|
|
|
|
|
|
private BeatmapSetInfo importedSet;
|
|
|
|
private BeatmapManager beatmaps;
|
|
|
|
private RulesetStore rulesets;
|
|
|
|
|
2021-04-06 20:37:21 +08:00
|
|
|
private IDisposable readyClickOperation;
|
|
|
|
|
2021-04-07 15:19:10 +08:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(GameHost host, AudioManager audio)
|
|
|
|
{
|
|
|
|
Dependencies.Cache(rulesets = new RulesetStore(ContextFactory));
|
2021-05-31 17:37:32 +08:00
|
|
|
Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, ContextFactory, rulesets, null, audio, Resources, host, Beatmap.Default));
|
2022-01-03 16:02:15 +08:00
|
|
|
beatmaps.Import(TestResources.GetQuickTestBeatmapForImport()).WaitSafely();
|
2021-04-07 15:19:10 +08:00
|
|
|
}
|
|
|
|
|
2021-04-06 20:37:21 +08:00
|
|
|
[SetUp]
|
|
|
|
public new void Setup() => Schedule(() =>
|
|
|
|
{
|
2021-06-25 14:00:10 +08:00
|
|
|
AvailabilityTracker.SelectedItem.BindTo(selectedItem);
|
|
|
|
|
2021-12-14 18:47:11 +08:00
|
|
|
importedSet = beatmaps.GetAllUsableBeatmapSetsEnumerable().First();
|
2021-04-07 15:19:10 +08:00
|
|
|
Beatmap.Value = beatmaps.GetWorkingBeatmap(importedSet.Beatmaps.First());
|
|
|
|
selectedItem.Value = new PlaylistItem
|
2021-04-06 20:37:21 +08:00
|
|
|
{
|
2021-04-07 15:19:10 +08:00
|
|
|
Beatmap = { Value = Beatmap.Value.BeatmapInfo },
|
|
|
|
Ruleset = { Value = Beatmap.Value.BeatmapInfo.Ruleset },
|
|
|
|
};
|
|
|
|
|
|
|
|
Child = new FillFlowContainer
|
|
|
|
{
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
Children = new Drawable[]
|
2021-04-06 20:37:21 +08:00
|
|
|
{
|
2021-04-07 15:19:10 +08:00
|
|
|
spectateButton = new MultiplayerSpectateButton
|
|
|
|
{
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Size = new Vector2(200, 50),
|
2021-07-02 15:04:51 +08:00
|
|
|
OnSpectateClick = () =>
|
2021-04-07 15:19:10 +08:00
|
|
|
{
|
|
|
|
readyClickOperation = OngoingOperationTracker.BeginOperation();
|
2021-07-02 15:04:51 +08:00
|
|
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
{
|
|
|
|
await Client.ToggleSpectate();
|
|
|
|
readyClickOperation.Dispose();
|
|
|
|
});
|
|
|
|
}
|
2021-04-07 15:19:10 +08:00
|
|
|
},
|
|
|
|
readyButton = new MultiplayerReadyButton
|
|
|
|
{
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Size = new Vector2(200, 50),
|
2021-07-02 15:04:51 +08:00
|
|
|
OnReadyClick = () =>
|
2021-04-07 15:19:10 +08:00
|
|
|
{
|
|
|
|
readyClickOperation = OngoingOperationTracker.BeginOperation();
|
|
|
|
|
2021-07-02 15:04:51 +08:00
|
|
|
Task.Run(async () =>
|
2021-04-07 15:19:10 +08:00
|
|
|
{
|
2021-07-02 15:04:51 +08:00
|
|
|
if (Client.IsHost && Client.LocalUser?.State == MultiplayerUserState.Ready)
|
|
|
|
{
|
|
|
|
await Client.StartMatch();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await Client.ToggleReady();
|
2021-04-07 15:19:10 +08:00
|
|
|
|
2021-07-02 15:04:51 +08:00
|
|
|
readyClickOperation.Dispose();
|
|
|
|
});
|
|
|
|
}
|
2021-04-07 15:19:10 +08:00
|
|
|
}
|
2021-04-06 20:37:21 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2021-04-22 23:49:14 +08:00
|
|
|
[TestCase(MultiplayerRoomState.Open)]
|
|
|
|
[TestCase(MultiplayerRoomState.WaitingForLoad)]
|
|
|
|
[TestCase(MultiplayerRoomState.Playing)]
|
|
|
|
public void TestEnabledWhenRoomOpenOrInGameplay(MultiplayerRoomState roomState)
|
2021-04-07 15:35:36 +08:00
|
|
|
{
|
2021-04-22 23:49:14 +08:00
|
|
|
AddStep($"change room to {roomState}", () => Client.ChangeRoomState(roomState));
|
2021-04-07 15:35:36 +08:00
|
|
|
assertSpectateButtonEnablement(true);
|
|
|
|
}
|
|
|
|
|
2021-04-06 20:37:21 +08:00
|
|
|
[TestCase(MultiplayerUserState.Idle)]
|
|
|
|
[TestCase(MultiplayerUserState.Ready)]
|
|
|
|
public void TestToggleWhenIdle(MultiplayerUserState initialState)
|
|
|
|
{
|
2021-12-21 12:20:12 +08:00
|
|
|
ClickButtonWhenEnabled<MultiplayerSpectateButton>();
|
2021-07-06 19:28:49 +08:00
|
|
|
AddUntilStep("user is spectating", () => Client.Room?.Users[0].State == MultiplayerUserState.Spectating);
|
2021-04-06 20:37:21 +08:00
|
|
|
|
2021-12-21 12:20:12 +08:00
|
|
|
ClickButtonWhenEnabled<MultiplayerSpectateButton>();
|
2021-07-06 19:28:49 +08:00
|
|
|
AddUntilStep("user is idle", () => Client.Room?.Users[0].State == MultiplayerUserState.Idle);
|
2021-04-06 20:37:21 +08:00
|
|
|
}
|
|
|
|
|
2021-04-07 15:35:36 +08:00
|
|
|
[TestCase(MultiplayerRoomState.Closed)]
|
2021-04-22 23:49:14 +08:00
|
|
|
public void TestDisabledWhenClosed(MultiplayerRoomState roomState)
|
2021-04-07 15:35:36 +08:00
|
|
|
{
|
2021-04-22 23:49:14 +08:00
|
|
|
AddStep($"change room to {roomState}", () => Client.ChangeRoomState(roomState));
|
2021-04-07 15:35:36 +08:00
|
|
|
assertSpectateButtonEnablement(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestReadyButtonDisabledWhenHostAndNoReadyUsers()
|
|
|
|
{
|
2021-12-21 12:20:12 +08:00
|
|
|
ClickButtonWhenEnabled<MultiplayerSpectateButton>();
|
2021-04-07 15:35:36 +08:00
|
|
|
assertReadyButtonEnablement(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestReadyButtonEnabledWhenHostAndUsersReady()
|
|
|
|
{
|
2021-11-04 17:02:44 +08:00
|
|
|
AddStep("add user", () => Client.AddUser(new APIUser { Id = PLAYER_1_ID }));
|
2021-04-26 16:22:16 +08:00
|
|
|
AddStep("set user ready", () => Client.ChangeUserState(PLAYER_1_ID, MultiplayerUserState.Ready));
|
2021-04-07 15:35:36 +08:00
|
|
|
|
2021-12-21 12:20:12 +08:00
|
|
|
ClickButtonWhenEnabled<MultiplayerSpectateButton>();
|
2021-04-07 15:35:36 +08:00
|
|
|
assertReadyButtonEnablement(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestReadyButtonDisabledWhenNotHostAndUsersReady()
|
|
|
|
{
|
|
|
|
AddStep("add user and transfer host", () =>
|
|
|
|
{
|
2021-11-04 17:02:44 +08:00
|
|
|
Client.AddUser(new APIUser { Id = PLAYER_1_ID });
|
2021-04-26 16:22:16 +08:00
|
|
|
Client.TransferHost(PLAYER_1_ID);
|
2021-04-07 15:35:36 +08:00
|
|
|
});
|
|
|
|
|
2021-04-26 16:22:16 +08:00
|
|
|
AddStep("set user ready", () => Client.ChangeUserState(PLAYER_1_ID, MultiplayerUserState.Ready));
|
2021-04-07 15:35:36 +08:00
|
|
|
|
2021-12-21 12:20:12 +08:00
|
|
|
ClickButtonWhenEnabled<MultiplayerSpectateButton>();
|
2021-04-07 15:35:36 +08:00
|
|
|
assertReadyButtonEnablement(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void assertSpectateButtonEnablement(bool shouldBeEnabled)
|
2021-07-06 19:28:49 +08:00
|
|
|
=> AddUntilStep($"spectate button {(shouldBeEnabled ? "is" : "is not")} enabled", () => spectateButton.ChildrenOfType<OsuButton>().Single().Enabled.Value == shouldBeEnabled);
|
2021-04-07 15:35:36 +08:00
|
|
|
|
2021-04-07 15:19:10 +08:00
|
|
|
private void assertReadyButtonEnablement(bool shouldBeEnabled)
|
2021-07-06 19:28:49 +08:00
|
|
|
=> AddUntilStep($"ready button {(shouldBeEnabled ? "is" : "is not")} enabled", () => readyButton.ChildrenOfType<OsuButton>().Single().Enabled.Value == shouldBeEnabled);
|
2021-04-06 20:37:21 +08:00
|
|
|
}
|
|
|
|
}
|