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-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;
|
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-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-07 15:35:36 +08:00
|
|
|
using osu.Game.Users;
|
2021-04-06 20:37:21 +08:00
|
|
|
using osuTK;
|
|
|
|
using osuTK.Input;
|
|
|
|
|
|
|
|
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
|
|
|
protected override Container<Drawable> Content => content;
|
|
|
|
private readonly Container content;
|
|
|
|
|
|
|
|
public TestSceneMultiplayerSpectateButton()
|
|
|
|
{
|
|
|
|
base.Content.Add(content = new Container
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
|
|
|
|
{
|
|
|
|
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
|
|
|
|
|
|
|
|
return dependencies;
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(GameHost host, AudioManager audio)
|
|
|
|
{
|
|
|
|
Dependencies.Cache(rulesets = new RulesetStore(ContextFactory));
|
|
|
|
Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, ContextFactory, rulesets, null, audio, host, Beatmap.Default));
|
|
|
|
|
2021-04-07 21:28:22 +08:00
|
|
|
var beatmapTracker = new OnlinePlayBeatmapAvailabilityTracker { SelectedItem = { BindTarget = selectedItem } };
|
2021-04-07 15:19:10 +08:00
|
|
|
base.Content.Add(beatmapTracker);
|
|
|
|
Dependencies.Cache(beatmapTracker);
|
|
|
|
|
|
|
|
beatmaps.Import(TestResources.GetQuickTestBeatmapForImport()).Wait();
|
|
|
|
}
|
|
|
|
|
2021-04-06 20:37:21 +08:00
|
|
|
[SetUp]
|
|
|
|
public new void Setup() => Schedule(() =>
|
|
|
|
{
|
2021-04-07 15:19:10 +08:00
|
|
|
importedSet = beatmaps.GetAllUsableBeatmapSetsEnumerable(IncludedDetails.All).First();
|
|
|
|
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),
|
|
|
|
OnSpectateClick = async () =>
|
|
|
|
{
|
|
|
|
readyClickOperation = OngoingOperationTracker.BeginOperation();
|
|
|
|
await Client.ToggleSpectate();
|
|
|
|
readyClickOperation.Dispose();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
readyButton = new MultiplayerReadyButton
|
|
|
|
{
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Size = new Vector2(200, 50),
|
|
|
|
OnReadyClick = async () =>
|
|
|
|
{
|
|
|
|
readyClickOperation = OngoingOperationTracker.BeginOperation();
|
|
|
|
|
|
|
|
if (Client.IsHost && Client.LocalUser?.State == MultiplayerUserState.Ready)
|
|
|
|
{
|
|
|
|
await Client.StartMatch();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await Client.ToggleReady();
|
|
|
|
readyClickOperation.Dispose();
|
|
|
|
}
|
|
|
|
}
|
2021-04-06 20:37:21 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2021-04-07 15:35:36 +08:00
|
|
|
[Test]
|
|
|
|
public void TestEnabledWhenRoomOpen()
|
|
|
|
{
|
|
|
|
assertSpectateButtonEnablement(true);
|
|
|
|
}
|
|
|
|
|
2021-04-06 20:37:21 +08:00
|
|
|
[TestCase(MultiplayerUserState.Idle)]
|
|
|
|
[TestCase(MultiplayerUserState.Ready)]
|
|
|
|
public void TestToggleWhenIdle(MultiplayerUserState initialState)
|
|
|
|
{
|
2021-04-07 15:19:10 +08:00
|
|
|
addClickSpectateButtonStep();
|
2021-04-06 20:37:21 +08:00
|
|
|
AddAssert("user is spectating", () => Client.Room?.Users[0].State == MultiplayerUserState.Spectating);
|
|
|
|
|
2021-04-07 15:19:10 +08:00
|
|
|
addClickSpectateButtonStep();
|
2021-04-06 20:37:21 +08:00
|
|
|
AddAssert("user is idle", () => Client.Room?.Users[0].State == MultiplayerUserState.Idle);
|
|
|
|
}
|
|
|
|
|
2021-04-07 15:35:36 +08:00
|
|
|
[TestCase(MultiplayerRoomState.WaitingForLoad)]
|
|
|
|
[TestCase(MultiplayerRoomState.Playing)]
|
|
|
|
[TestCase(MultiplayerRoomState.Closed)]
|
|
|
|
public void TestDisabledDuringGameplayOrClosed(MultiplayerRoomState roomState)
|
|
|
|
{
|
|
|
|
AddStep($"change user to {roomState}", () => Client.ChangeRoomState(roomState));
|
|
|
|
assertSpectateButtonEnablement(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestReadyButtonDisabledWhenHostAndNoReadyUsers()
|
|
|
|
{
|
|
|
|
addClickSpectateButtonStep();
|
|
|
|
assertReadyButtonEnablement(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestReadyButtonEnabledWhenHostAndUsersReady()
|
|
|
|
{
|
|
|
|
AddStep("add user", () => Client.AddUser(new User { Id = 55 }));
|
|
|
|
AddStep("set user ready", () => Client.ChangeUserState(55, MultiplayerUserState.Ready));
|
|
|
|
|
|
|
|
addClickSpectateButtonStep();
|
|
|
|
assertReadyButtonEnablement(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestReadyButtonDisabledWhenNotHostAndUsersReady()
|
|
|
|
{
|
|
|
|
AddStep("add user and transfer host", () =>
|
|
|
|
{
|
|
|
|
Client.AddUser(new User { Id = 55 });
|
|
|
|
Client.TransferHost(55);
|
|
|
|
});
|
|
|
|
|
|
|
|
AddStep("set user ready", () => Client.ChangeUserState(55, MultiplayerUserState.Ready));
|
|
|
|
|
|
|
|
addClickSpectateButtonStep();
|
|
|
|
assertReadyButtonEnablement(false);
|
|
|
|
}
|
|
|
|
|
2021-04-07 15:19:10 +08:00
|
|
|
private void addClickSpectateButtonStep() => AddStep("click spectate button", () =>
|
|
|
|
{
|
|
|
|
InputManager.MoveMouseTo(spectateButton);
|
|
|
|
InputManager.Click(MouseButton.Left);
|
|
|
|
});
|
|
|
|
|
2021-04-07 15:35:36 +08:00
|
|
|
private void assertSpectateButtonEnablement(bool shouldBeEnabled)
|
|
|
|
=> AddAssert($"spectate button {(shouldBeEnabled ? "is" : "is not")} enabled", () => spectateButton.ChildrenOfType<OsuButton>().Single().Enabled.Value == shouldBeEnabled);
|
|
|
|
|
2021-04-07 15:19:10 +08:00
|
|
|
private void assertReadyButtonEnablement(bool shouldBeEnabled)
|
|
|
|
=> AddAssert($"ready button {(shouldBeEnabled ? "is" : "is not")} enabled", () => readyButton.ChildrenOfType<OsuButton>().Single().Enabled.Value == shouldBeEnabled);
|
2021-04-06 20:37:21 +08:00
|
|
|
}
|
|
|
|
}
|