2020-01-29 09:18:46 +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.
|
|
|
|
|
|
2020-02-04 06:02:58 +08:00
|
|
|
|
using System.Linq;
|
2020-01-29 09:18:46 +08:00
|
|
|
|
using NUnit.Framework;
|
2020-01-29 09:06:10 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2020-02-04 06:02:58 +08:00
|
|
|
|
using osu.Framework.Testing;
|
2021-06-05 17:10:16 +08:00
|
|
|
|
using osu.Game.Online.API;
|
2020-01-29 09:06:10 +08:00
|
|
|
|
using osu.Game.Online.Chat;
|
|
|
|
|
using osu.Game.Overlays;
|
2020-02-04 06:02:58 +08:00
|
|
|
|
using osu.Game.Overlays.Notifications;
|
2020-01-29 09:06:10 +08:00
|
|
|
|
using osu.Game.Users;
|
2020-02-04 06:02:58 +08:00
|
|
|
|
using osuTK.Input;
|
2020-01-29 09:06:10 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.Visual.Online
|
|
|
|
|
{
|
2021-05-27 07:47:00 +08:00
|
|
|
|
public class TestSceneMessageNotifier : OsuManualInputManagerTestScene
|
2020-01-29 09:06:10 +08:00
|
|
|
|
{
|
|
|
|
|
private User friend;
|
|
|
|
|
private Channel publicChannel;
|
2020-02-04 05:08:01 +08:00
|
|
|
|
private Channel privateMessageChannel;
|
2020-01-29 09:06:10 +08:00
|
|
|
|
private TestContainer testContainer;
|
|
|
|
|
|
|
|
|
|
private int messageIdCounter;
|
|
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void Setup()
|
|
|
|
|
{
|
2021-06-05 17:10:16 +08:00
|
|
|
|
// We blindly mark every request as success so that ChannelManager doesn't remove our channel again.
|
|
|
|
|
if (API is DummyAPIAccess daa)
|
|
|
|
|
{
|
|
|
|
|
daa.HandleRequest = (request) => {
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-30 10:41:21 +08:00
|
|
|
|
friend = new User { Id = 0, Username = "Friend" };
|
|
|
|
|
publicChannel = new Channel { Id = 1, Name = "osu" };
|
2020-02-04 05:19:55 +08:00
|
|
|
|
privateMessageChannel = new Channel(friend) { Id = 2, Name = friend.Username, Type = ChannelType.PM };
|
2020-01-29 09:06:10 +08:00
|
|
|
|
|
2020-01-30 12:24:26 +08:00
|
|
|
|
Schedule(() =>
|
2020-01-29 09:06:10 +08:00
|
|
|
|
{
|
2020-02-04 05:19:55 +08:00
|
|
|
|
Child = testContainer = new TestContainer(new[] { publicChannel, privateMessageChannel })
|
2020-01-30 12:24:26 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
};
|
2020-01-29 09:06:10 +08:00
|
|
|
|
|
2020-01-30 12:24:26 +08:00
|
|
|
|
testContainer.ChatOverlay.Show();
|
|
|
|
|
});
|
2020-01-29 09:06:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestPublicChannelMention()
|
|
|
|
|
{
|
2020-02-05 02:23:46 +08:00
|
|
|
|
AddStep("switch to PMs", () => testContainer.ChannelManager.CurrentChannel.Value = privateMessageChannel);
|
2020-01-29 09:06:10 +08:00
|
|
|
|
|
2020-02-05 02:23:46 +08:00
|
|
|
|
AddStep("receive public message", () => receiveMessage(friend, publicChannel, "Hello everyone"));
|
|
|
|
|
AddAssert("no notifications fired", () => testContainer.NotificationOverlay.UnreadCount.Value == 0);
|
2020-01-29 09:06:10 +08:00
|
|
|
|
|
2020-02-05 02:23:46 +08:00
|
|
|
|
AddStep("receive message containing mention", () => receiveMessage(friend, publicChannel, $"Hello {API.LocalUser.Value.Username.ToLowerInvariant()}!"));
|
|
|
|
|
AddAssert("1 notification fired", () => testContainer.NotificationOverlay.UnreadCount.Value == 1);
|
2020-02-04 06:02:58 +08:00
|
|
|
|
|
2020-02-05 02:23:46 +08:00
|
|
|
|
AddStep("open notification overlay", () => testContainer.NotificationOverlay.Show());
|
|
|
|
|
AddStep("click notification", clickNotification<MessageNotifier.MentionNotification>);
|
2020-02-04 06:02:58 +08:00
|
|
|
|
|
2020-02-05 02:23:46 +08:00
|
|
|
|
AddAssert("chat overlay is open", () => testContainer.ChatOverlay.State.Value == Visibility.Visible);
|
|
|
|
|
AddAssert("public channel is selected", () => testContainer.ChannelManager.CurrentChannel.Value == publicChannel);
|
2020-01-29 09:06:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestPrivateMessageNotification()
|
|
|
|
|
{
|
2020-02-05 02:23:46 +08:00
|
|
|
|
AddStep("switch to public channel", () => testContainer.ChannelManager.CurrentChannel.Value = publicChannel);
|
2020-02-04 06:02:58 +08:00
|
|
|
|
|
2020-02-05 02:23:46 +08:00
|
|
|
|
AddStep("receive PM", () => receiveMessage(friend, privateMessageChannel, $"Hello {API.LocalUser.Value.Username}"));
|
|
|
|
|
AddAssert("1 notification fired", () => testContainer.NotificationOverlay.UnreadCount.Value == 1);
|
2020-02-04 06:02:58 +08:00
|
|
|
|
|
2020-02-05 02:23:46 +08:00
|
|
|
|
AddStep("open notification overlay", () => testContainer.NotificationOverlay.Show());
|
|
|
|
|
AddStep("click notification", clickNotification<MessageNotifier.PrivateMessageNotification>);
|
2020-02-04 06:02:58 +08:00
|
|
|
|
|
2020-02-05 02:23:46 +08:00
|
|
|
|
AddAssert("chat overlay is open", () => testContainer.ChatOverlay.State.Value == Visibility.Visible);
|
|
|
|
|
AddAssert("PM channel is selected", () => testContainer.ChannelManager.CurrentChannel.Value == privateMessageChannel);
|
2020-02-04 06:02:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-05 02:23:46 +08:00
|
|
|
|
[Test]
|
|
|
|
|
public void TestNoNotificationWhenPMChannelOpen()
|
|
|
|
|
{
|
|
|
|
|
AddStep("switch to PMs", () => testContainer.ChannelManager.CurrentChannel.Value = privateMessageChannel);
|
|
|
|
|
|
|
|
|
|
AddStep("receive PM", () => receiveMessage(friend, privateMessageChannel, "you're reading this, right?"));
|
|
|
|
|
|
|
|
|
|
AddAssert("no notifications fired", () => testContainer.NotificationOverlay.UnreadCount.Value == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestNoNotificationWhenMentionedInOpenPublicChannel()
|
|
|
|
|
{
|
|
|
|
|
AddStep("switch to public channel", () => testContainer.ChannelManager.CurrentChannel.Value = publicChannel);
|
|
|
|
|
|
|
|
|
|
AddStep("receive mention", () => receiveMessage(friend, publicChannel, $"{API.LocalUser.Value.Username.ToUpperInvariant()} has been reading this"));
|
|
|
|
|
|
|
|
|
|
AddAssert("no notifications fired", () => testContainer.NotificationOverlay.UnreadCount.Value == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestNoNotificationOnSelfMention()
|
|
|
|
|
{
|
|
|
|
|
AddStep("switch to PM channel", () => testContainer.ChannelManager.CurrentChannel.Value = privateMessageChannel);
|
|
|
|
|
|
|
|
|
|
AddStep("receive self-mention", () => receiveMessage(API.LocalUser.Value, publicChannel, $"my name is {API.LocalUser.Value.Username}"));
|
|
|
|
|
|
|
|
|
|
AddAssert("no notifications fired", () => testContainer.NotificationOverlay.UnreadCount.Value == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestNoNotificationOnPMFromSelf()
|
|
|
|
|
{
|
|
|
|
|
AddStep("switch to public channel", () => testContainer.ChannelManager.CurrentChannel.Value = publicChannel);
|
|
|
|
|
|
|
|
|
|
AddStep("receive PM from self", () => receiveMessage(API.LocalUser.Value, privateMessageChannel, "hey hey"));
|
|
|
|
|
|
|
|
|
|
AddAssert("no notifications fired", () => testContainer.NotificationOverlay.UnreadCount.Value == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestNotificationsNotFiredTwice()
|
|
|
|
|
{
|
|
|
|
|
AddStep("switch to public channel", () => testContainer.ChannelManager.CurrentChannel.Value = publicChannel);
|
|
|
|
|
|
|
|
|
|
AddStep("receive same PM twice", () =>
|
|
|
|
|
{
|
|
|
|
|
var message = createMessage(friend, privateMessageChannel, "hey hey");
|
|
|
|
|
privateMessageChannel.AddNewMessages(message, message);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
AddStep("open notification overlay", () => testContainer.NotificationOverlay.Show());
|
|
|
|
|
AddAssert("1 notification fired", () => testContainer.NotificationOverlay.UnreadCount.Value == 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void receiveMessage(User sender, Channel channel, string content) => channel.AddNewMessages(createMessage(sender, channel, content));
|
|
|
|
|
|
|
|
|
|
private Message createMessage(User sender, Channel channel, string content) => new Message(messageIdCounter++)
|
|
|
|
|
{
|
|
|
|
|
Content = content,
|
|
|
|
|
Sender = sender,
|
|
|
|
|
ChannelId = channel.Id
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-04 06:02:58 +08:00
|
|
|
|
private void clickNotification<T>() where T : Notification
|
|
|
|
|
{
|
|
|
|
|
var notification = testContainer.NotificationOverlay.ChildrenOfType<T>().Single();
|
|
|
|
|
|
|
|
|
|
InputManager.MoveMouseTo(notification);
|
|
|
|
|
InputManager.Click(MouseButton.Left);
|
2020-01-29 09:06:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class TestContainer : Container
|
|
|
|
|
{
|
2020-01-30 10:41:21 +08:00
|
|
|
|
private readonly Channel[] channels;
|
2020-01-29 09:06:10 +08:00
|
|
|
|
|
|
|
|
|
public TestContainer(Channel[] channels) => this.channels = channels;
|
|
|
|
|
|
|
|
|
|
[Cached]
|
|
|
|
|
public ChannelManager ChannelManager { get; } = new ChannelManager();
|
|
|
|
|
|
|
|
|
|
[Cached]
|
|
|
|
|
public NotificationOverlay NotificationOverlay { get; } = new NotificationOverlay();
|
|
|
|
|
|
|
|
|
|
[Cached]
|
|
|
|
|
public MessageNotifier MessageNotifier { get; } = new MessageNotifier();
|
|
|
|
|
|
|
|
|
|
[Cached]
|
|
|
|
|
public ChatOverlay ChatOverlay { get; } = new ChatOverlay();
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load()
|
|
|
|
|
{
|
2020-02-04 06:00:29 +08:00
|
|
|
|
AddRange(new Drawable[] { ChannelManager, ChatOverlay, NotificationOverlay, MessageNotifier });
|
2020-01-29 09:06:10 +08:00
|
|
|
|
|
|
|
|
|
((BindableList<Channel>)ChannelManager.AvailableChannels).AddRange(channels);
|
2020-02-04 06:05:46 +08:00
|
|
|
|
|
|
|
|
|
foreach (var channel in channels)
|
|
|
|
|
ChannelManager.JoinChannel(channel);
|
2020-01-29 09:06:10 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|