1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 03:47:26 +08:00
osu-lazer/osu.Game.Tests/Visual/Online/TestSceneChatOverlay.cs

325 lines
13 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2019-06-21 14:32:06 +08:00
// See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using System.Linq;
2019-07-01 18:35:04 +08:00
using NUnit.Framework;
2019-06-21 14:32:06 +08:00
using osu.Framework.Allocation;
2019-07-01 18:35:04 +08:00
using osu.Framework.Bindables;
2019-06-21 14:32:06 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2019-07-01 18:35:04 +08:00
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Testing;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
2019-06-21 14:32:06 +08:00
using osu.Game.Online.Chat;
using osu.Game.Overlays;
2019-07-01 18:35:04 +08:00
using osu.Game.Overlays.Chat.Selection;
2019-06-21 14:32:06 +08:00
using osu.Game.Overlays.Chat.Tabs;
2019-07-01 18:35:04 +08:00
using osu.Game.Users;
using osuTK.Input;
2019-06-21 14:32:06 +08:00
namespace osu.Game.Tests.Visual.Online
{
public class TestSceneChatOverlay : OsuManualInputManagerTestScene
2019-06-21 14:32:06 +08:00
{
2019-07-01 18:35:04 +08:00
private TestChatOverlay chatOverlay;
private ChannelManager channelManager;
2019-06-21 14:32:06 +08:00
2020-03-14 01:34:22 +08:00
private IEnumerable<Channel> visibleChannels => chatOverlay.ChannelTabControl.VisibleItems.Where(channel => channel.Name != "+");
private IEnumerable<Channel> joinedChannels => chatOverlay.ChannelTabControl.Items.Where(channel => channel.Name != "+");
2020-01-31 04:17:46 +08:00
private readonly List<Channel> channels;
2020-03-14 01:34:22 +08:00
private Channel currentChannel => channelManager.CurrentChannel.Value;
private Channel nextChannel => joinedChannels.ElementAt(joinedChannels.ToList().IndexOf(currentChannel) + 1);
private Channel previousChannel => joinedChannels.ElementAt(joinedChannels.ToList().IndexOf(currentChannel) - 1);
2020-01-31 04:17:46 +08:00
private Channel channel1 => channels[0];
private Channel channel2 => channels[1];
public TestSceneChatOverlay()
{
channels = Enumerable.Range(1, 10)
2020-03-17 02:44:03 +08:00
.Select(index => new Channel(new User())
2020-01-31 04:17:46 +08:00
{
Name = $"Channel no. {index}",
2020-03-17 02:10:42 +08:00
Topic = index == 3 ? null : $"We talk about the number {index} here",
2020-03-17 02:44:03 +08:00
Type = index % 2 == 0 ? ChannelType.PM : ChannelType.Temporary
2020-01-31 04:17:46 +08:00
})
.ToList();
}
2019-07-01 18:35:04 +08:00
[SetUp]
public void Setup()
{
Schedule(() =>
{
ChannelManagerContainer container;
2020-01-31 04:17:46 +08:00
Child = container = new ChannelManagerContainer(channels)
2019-07-01 18:35:04 +08:00
{
RelativeSizeAxes = Axes.Both,
};
chatOverlay = container.ChatOverlay;
channelManager = container.ChannelManager;
});
}
[SetUpSteps]
public void SetUpSteps()
{
AddStep("register request handling", () =>
{
((DummyAPIAccess)API).HandleRequest = req =>
{
switch (req)
{
case JoinChannelRequest _:
return true;
}
return false;
};
});
}
2019-07-01 18:35:04 +08:00
[Test]
public void TestHideOverlay()
{
AddAssert("Chat overlay is visible", () => chatOverlay.State.Value == Visibility.Visible);
AddAssert("Selector is visible", () => chatOverlay.SelectionOverlayState == Visibility.Visible);
AddStep("Close chat overlay", () => chatOverlay.Hide());
AddAssert("Chat overlay was hidden", () => chatOverlay.State.Value == Visibility.Hidden);
AddAssert("Channel selection overlay was hidden", () => chatOverlay.SelectionOverlayState == Visibility.Hidden);
}
[Test]
public void TestSelectingChannelClosesSelector()
{
AddAssert("Selector is visible", () => chatOverlay.SelectionOverlayState == Visibility.Visible);
AddStep("Join channel 1", () => channelManager.JoinChannel(channel1));
AddStep("Switch to channel 1", () => clickDrawable(chatOverlay.TabMap[channel1]));
2020-03-14 01:34:22 +08:00
AddAssert("Current channel is channel 1", () => currentChannel == channel1);
2019-07-01 18:35:04 +08:00
AddAssert("Channel selector was closed", () => chatOverlay.SelectionOverlayState == Visibility.Hidden);
}
[Test]
public void TestSearchInSelector()
{
2020-03-17 02:10:42 +08:00
AddStep("Search for 'no. 2'", () => chatOverlay.ChildrenOfType<SearchTextBox>().First().Text = "no. 2");
AddUntilStep("Only channel 2 visible", () =>
{
var listItems = chatOverlay.ChildrenOfType<ChannelListItem>().Where(c => c.IsPresent);
return listItems.Count() == 1 && listItems.Single().Channel == channel2;
});
}
2020-01-31 04:17:46 +08:00
[Test]
public void TestChannelShortcutKeys()
{
2020-03-17 02:10:42 +08:00
AddStep("Join channels", () => channels.ForEach(channel => channelManager.JoinChannel(channel)));
2020-11-05 22:41:56 +08:00
AddStep("Close channel selector", () => InputManager.Key(Key.Escape));
2020-03-17 02:10:42 +08:00
AddUntilStep("Wait for close", () => chatOverlay.SelectionOverlayState == Visibility.Hidden);
2020-01-31 04:17:46 +08:00
for (int zeroBasedIndex = 0; zeroBasedIndex < 10; ++zeroBasedIndex)
{
var oneBasedIndex = zeroBasedIndex + 1;
var targetNumberKey = oneBasedIndex % 10;
var targetChannel = channels[zeroBasedIndex];
2020-03-17 02:10:42 +08:00
AddStep($"Press Alt+{targetNumberKey}", () => pressChannelHotkey(targetNumberKey));
AddAssert($"Channel #{oneBasedIndex} is selected", () => currentChannel == targetChannel);
2020-01-31 04:17:46 +08:00
}
}
2020-03-14 01:34:22 +08:00
private Channel expectedChannel;
[Test]
2020-03-17 02:10:42 +08:00
public void TestCloseChannelBehaviour()
2020-03-14 01:34:22 +08:00
{
AddUntilStep("Join until dropdown has channels", () =>
{
if (visibleChannels.Count() < joinedChannels.Count())
return true;
// Using temporary channels because they don't hide their names when not active
2020-03-17 02:10:42 +08:00
channelManager.JoinChannel(new Channel
2020-03-17 02:44:03 +08:00
{
Name = $"Channel no. {joinedChannels.Count() + 11}",
2020-03-17 02:44:03 +08:00
Type = ChannelType.Temporary
2020-03-17 02:10:42 +08:00
});
2020-03-14 01:34:22 +08:00
return false;
});
AddStep("Switch to last tab", () => clickDrawable(chatOverlay.TabMap[visibleChannels.Last()]));
AddAssert("Last visible selected", () => currentChannel == visibleChannels.Last());
// Closing the last channel before dropdown
AddStep("Close current channel", () =>
{
expectedChannel = nextChannel;
chatOverlay.ChannelTabControl.RemoveChannel(currentChannel);
});
AddAssert("Next channel selected", () => currentChannel == expectedChannel);
2020-03-17 02:10:42 +08:00
AddAssert("Selector remained closed", () => chatOverlay.SelectionOverlayState == Visibility.Hidden);
2020-03-14 01:34:22 +08:00
// Depending on the window size, one more channel might need to be closed for the selectorTab to appear
AddUntilStep("Close channels until selector visible", () =>
{
if (chatOverlay.ChannelTabControl.VisibleItems.Last().Name == "+")
return true;
chatOverlay.ChannelTabControl.RemoveChannel(visibleChannels.Last());
return false;
});
AddAssert("Last visible selected", () => currentChannel == visibleChannels.Last());
// Closing the last channel with dropdown no longer present
AddStep("Close last when selector next", () =>
2020-03-14 01:34:22 +08:00
{
expectedChannel = previousChannel;
chatOverlay.ChannelTabControl.RemoveChannel(currentChannel);
});
2020-03-17 02:10:42 +08:00
AddAssert("Previous channel selected", () => currentChannel == expectedChannel);
2020-03-14 01:34:22 +08:00
// Standard channel closing
AddStep("Switch to previous channel", () => chatOverlay.ChannelTabControl.SwitchTab(-1));
AddStep("Close current channel", () =>
2020-03-14 01:34:22 +08:00
{
expectedChannel = nextChannel;
chatOverlay.ChannelTabControl.RemoveChannel(currentChannel);
});
2020-03-17 02:10:42 +08:00
AddAssert("Next channel selected", () => currentChannel == expectedChannel);
// Selector reappearing after all channels closed
AddUntilStep("Close all channels", () =>
{
if (!joinedChannels.Any())
return true;
2020-03-17 02:44:03 +08:00
2020-03-17 02:10:42 +08:00
chatOverlay.ChannelTabControl.RemoveChannel(joinedChannels.Last());
return false;
});
AddAssert("Selector is visible", () => chatOverlay.SelectionOverlayState == Visibility.Visible);
}
[Test]
public void TestChannelCloseButton()
{
2020-03-17 02:44:03 +08:00
AddStep("Join 2 channels", () =>
2020-03-17 02:10:42 +08:00
{
channelManager.JoinChannel(channel1);
2020-03-17 02:44:03 +08:00
channelManager.JoinChannel(channel2);
2020-03-17 02:10:42 +08:00
});
// PM channel close button only appears when active
2020-03-17 02:44:03 +08:00
AddStep("Select PM channel", () => clickDrawable(chatOverlay.TabMap[channel2]));
AddStep("Click PM close button", () => clickDrawable(((TestPrivateChannelTabItem)chatOverlay.TabMap[channel2]).CloseButton.Child));
AddAssert("PM channel closed", () => !channelManager.JoinedChannels.Contains(channel2));
2020-03-17 02:10:42 +08:00
// Non-PM chat channel close button only appears when hovered
AddStep("Hover normal channel tab", () => InputManager.MoveMouseTo(chatOverlay.TabMap[channel1]));
AddStep("Click normal close button", () => clickDrawable(((TestChannelTabItem)chatOverlay.TabMap[channel1]).CloseButton.Child));
AddAssert("All channels closed", () => !channelManager.JoinedChannels.Any());
2020-03-14 01:34:22 +08:00
}
2020-01-31 04:17:46 +08:00
private void pressChannelHotkey(int number)
{
var channelKey = Key.Number0 + number;
InputManager.PressKey(Key.AltLeft);
2020-11-05 22:41:56 +08:00
InputManager.Key(channelKey);
2020-01-31 04:17:46 +08:00
InputManager.ReleaseKey(Key.AltLeft);
}
2019-07-01 18:35:04 +08:00
private void clickDrawable(Drawable d)
2019-06-21 14:32:06 +08:00
{
2019-07-01 18:35:04 +08:00
InputManager.MoveMouseTo(d);
InputManager.Click(MouseButton.Left);
}
private class ChannelManagerContainer : Container
{
public TestChatOverlay ChatOverlay { get; private set; }
[Cached]
public ChannelManager ChannelManager { get; } = new ChannelManager();
private readonly List<Channel> channels;
public ChannelManagerContainer(List<Channel> channels)
{
this.channels = channels;
}
[BackgroundDependencyLoader]
private void load()
{
((BindableList<Channel>)ChannelManager.AvailableChannels).AddRange(channels);
InternalChildren = new Drawable[]
{
ChannelManager,
ChatOverlay = new TestChatOverlay { RelativeSizeAxes = Axes.Both, },
};
2019-07-01 18:35:04 +08:00
ChatOverlay.Show();
}
}
private class TestChatOverlay : ChatOverlay
{
public Visibility SelectionOverlayState => ChannelSelectionOverlay.State.Value;
2020-03-14 01:34:22 +08:00
public new ChannelTabControl ChannelTabControl => base.ChannelTabControl;
2019-07-01 18:35:04 +08:00
public new ChannelSelectionOverlay ChannelSelectionOverlay => base.ChannelSelectionOverlay;
protected override ChannelTabControl CreateChannelTabControl() => new TestTabControl();
public IReadOnlyDictionary<Channel, TabItem<Channel>> TabMap => ((TestTabControl)ChannelTabControl).TabMap;
}
private class TestTabControl : ChannelTabControl
{
2020-03-14 01:33:58 +08:00
protected override TabItem<Channel> CreateTabItem(Channel value)
{
switch (value.Type)
{
case ChannelType.PM:
return new TestPrivateChannelTabItem(value);
default:
return new TestChannelTabItem(value);
}
}
2019-07-01 18:35:04 +08:00
public new IReadOnlyDictionary<Channel, TabItem<Channel>> TabMap => base.TabMap;
}
2020-03-14 01:33:58 +08:00
private class TestChannelTabItem : ChannelTabItem
2019-07-01 18:35:04 +08:00
{
public TestChannelTabItem(Channel channel)
: base(channel)
2019-06-21 14:32:06 +08:00
{
2019-07-01 18:35:04 +08:00
}
public new ClickableContainer CloseButton => base.CloseButton;
2019-06-21 14:32:06 +08:00
}
2020-03-14 01:33:58 +08:00
private class TestPrivateChannelTabItem : PrivateChannelTabItem
{
public TestPrivateChannelTabItem(Channel channel)
: base(channel)
{
}
public new ClickableContainer CloseButton => base.CloseButton;
}
2019-06-21 14:32:06 +08:00
}
}