1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 15:32:55 +08:00
osu-lazer/osu.Game.Tests/Visual/Online/TestSceneMessageNotifier.cs

241 lines
9.6 KiB
C#
Raw Normal View History

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.
using System.Collections.Generic;
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;
using osu.Framework.Testing;
2021-06-05 17:10:16 +08:00
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
2020-01-29 09:06:10 +08:00
using osu.Game.Online.Chat;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
using osuTK.Input;
using APIUser = osu.Game.Online.API.Requests.Responses.APIUser;
2020-01-29 09:06:10 +08:00
namespace osu.Game.Tests.Visual.Online
{
public class TestSceneMessageNotifier : OsuManualInputManagerTestScene
2020-01-29 09:06:10 +08:00
{
private APIUser friend;
2020-01-29 09:06:10 +08:00
private Channel publicChannel;
private Channel privateMessageChannel;
2020-01-29 09:06:10 +08:00
private TestContainer testContainer;
private int messageIdCounter;
[SetUp]
public void Setup() => Schedule(() =>
2020-01-29 09:06:10 +08:00
{
2021-06-05 17:10:16 +08:00
if (API is DummyAPIAccess daa)
{
daa.HandleRequest = dummyAPIHandleRequest;
2021-06-05 17:10:16 +08:00
}
friend = new APIUser { Id = 0, Username = "Friend" };
2020-01-30 10:41:21 +08:00
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
private bool dummyAPIHandleRequest(APIRequest request)
{
switch (request)
{
case GetMessagesRequest messagesRequest:
messagesRequest.TriggerSuccess(new List<Message>(0));
return true;
case CreateChannelRequest createChannelRequest:
var apiChatChannel = new APIChatChannel
{
RecentMessages = new List<Message>(0),
ChannelID = (int)createChannelRequest.Channel.Id
};
createChannelRequest.TriggerSuccess(apiChatChannel);
return true;
case ListChannelsRequest listChannelsRequest:
listChannelsRequest.TriggerSuccess(new List<Channel>(1) { publicChannel });
return true;
2021-06-05 21:55:58 +08:00
case GetUpdatesRequest updatesRequest:
updatesRequest.TriggerSuccess(new GetUpdatesResponse
{
Messages = new List<Message>(0),
Presence = new List<Channel>(0)
});
return true;
case JoinChannelRequest joinChannelRequest:
joinChannelRequest.TriggerSuccess();
return true;
default:
return false;
}
}
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-05 02:23:46 +08:00
AddStep("open notification overlay", () => testContainer.NotificationOverlay.Show());
AddStep("click notification", clickNotification<MessageNotifier.MentionNotification>);
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-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-05 02:23:46 +08:00
AddStep("open notification overlay", () => testContainer.NotificationOverlay.Show());
AddStep("click notification", clickNotification<MessageNotifier.PrivateMessageNotification>);
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-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(APIUser sender, Channel channel, string content) => channel.AddNewMessages(createMessage(sender, channel, content));
2020-02-05 02:23:46 +08:00
private Message createMessage(APIUser sender, Channel channel, string content) => new Message(messageIdCounter++)
2020-02-05 02:23:46 +08:00
{
Content = content,
Sender = sender,
ChannelId = channel.Id
};
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
{
[Cached]
public ChannelManager ChannelManager { get; } = new ChannelManager();
[Cached(typeof(INotificationOverlay))]
public NotificationOverlay NotificationOverlay { get; } = new NotificationOverlay
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
};
2020-01-29 09:06:10 +08:00
[Cached]
public ChatOverlay ChatOverlay { get; } = new ChatOverlay();
2021-06-11 15:17:42 +08:00
private readonly MessageNotifier messageNotifier = new MessageNotifier();
private readonly Channel[] channels;
public TestContainer(Channel[] channels)
{
this.channels = channels;
}
2020-01-29 09:06:10 +08:00
[BackgroundDependencyLoader]
private void load()
{
2021-06-11 15:17:42 +08:00
Children = 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
}
}
}
}