2021-06-29 07:30:46 +00: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.
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2021-08-15 13:44:23 +00:00
|
|
|
using System.Collections.Generic;
|
2021-06-29 07:30:46 +00:00
|
|
|
using System.Linq;
|
|
|
|
using NUnit.Framework;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Testing;
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Online.API.Requests;
|
2021-11-04 09:02:44 +00:00
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2021-06-29 07:30:46 +00:00
|
|
|
using osu.Game.Online.Chat;
|
|
|
|
using osu.Game.Tests.Visual;
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.Chat
|
|
|
|
{
|
|
|
|
[HeadlessTest]
|
|
|
|
public partial class TestSceneChannelManager : OsuTestScene
|
|
|
|
{
|
|
|
|
private ChannelManager channelManager;
|
|
|
|
private int currentMessageId;
|
2021-08-15 14:02:25 +00:00
|
|
|
private List<Message> sentMessages;
|
2022-11-02 08:13:14 +00:00
|
|
|
private List<int> silencedUserIds;
|
2021-06-29 07:30:46 +00:00
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void Setup() => Schedule(() =>
|
|
|
|
{
|
2022-06-08 09:54:23 +00:00
|
|
|
var container = new ChannelManagerContainer(API);
|
2021-06-29 07:30:46 +00:00
|
|
|
Child = container;
|
|
|
|
channelManager = container.ChannelManager;
|
|
|
|
});
|
|
|
|
|
|
|
|
[SetUpSteps]
|
|
|
|
public void SetUpSteps()
|
|
|
|
{
|
|
|
|
AddStep("register request handling", () =>
|
|
|
|
{
|
|
|
|
currentMessageId = 0;
|
2021-08-15 13:44:23 +00:00
|
|
|
sentMessages = new List<Message>();
|
2022-11-02 08:13:14 +00:00
|
|
|
silencedUserIds = new List<int>();
|
2021-06-29 07:30:46 +00:00
|
|
|
|
|
|
|
((DummyAPIAccess)API).HandleRequest = req =>
|
|
|
|
{
|
|
|
|
switch (req)
|
|
|
|
{
|
2021-06-29 11:13:39 +00:00
|
|
|
case JoinChannelRequest joinChannel:
|
|
|
|
joinChannel.TriggerSuccess();
|
2021-06-29 07:30:46 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case PostMessageRequest postMessage:
|
2021-08-15 18:35:52 +00:00
|
|
|
handlePostMessageRequest(postMessage);
|
|
|
|
return true;
|
2021-06-29 07:30:46 +00:00
|
|
|
|
2021-08-15 18:35:52 +00:00
|
|
|
case MarkChannelAsReadRequest markRead:
|
|
|
|
handleMarkChannelAsReadRequest(markRead);
|
2021-06-29 07:30:46 +00:00
|
|
|
return true;
|
2022-11-02 08:13:14 +00:00
|
|
|
|
|
|
|
case ChatAckRequest ack:
|
|
|
|
ack.TriggerSuccess(new ChatAckResponse { Silences = silencedUserIds.Select(u => new ChatSilence { UserId = u }).ToList() });
|
|
|
|
silencedUserIds.Clear();
|
|
|
|
return true;
|
2022-11-12 12:00:56 +00:00
|
|
|
|
2022-11-11 05:55:41 +00:00
|
|
|
case GetUpdatesRequest updatesRequest:
|
|
|
|
updatesRequest.TriggerSuccess(new GetUpdatesResponse
|
|
|
|
{
|
|
|
|
Messages = sentMessages.ToList(),
|
|
|
|
Presence = new List<Channel>()
|
|
|
|
});
|
|
|
|
return true;
|
2021-06-29 07:30:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestCommandsPostedToCorrectChannelWhenNotCurrent()
|
|
|
|
{
|
|
|
|
Channel channel1 = null;
|
|
|
|
Channel channel2 = null;
|
|
|
|
|
|
|
|
AddStep("join 2 rooms", () =>
|
|
|
|
{
|
|
|
|
channelManager.JoinChannel(channel1 = createChannel(1, ChannelType.Public));
|
|
|
|
channelManager.JoinChannel(channel2 = createChannel(2, ChannelType.Public));
|
|
|
|
});
|
|
|
|
|
|
|
|
AddStep("select channel 1", () => channelManager.CurrentChannel.Value = channel1);
|
|
|
|
|
|
|
|
AddStep("post /me command to channel 2", () => channelManager.PostCommand("me dances", channel2));
|
|
|
|
AddAssert("/me command received by channel 2", () => channel2.Messages.Last().Content == "dances");
|
|
|
|
|
|
|
|
AddStep("post /np command to channel 2", () => channelManager.PostCommand("np", channel2));
|
|
|
|
AddAssert("/np command received by channel 2", () => channel2.Messages.Last().Content.Contains("is listening to"));
|
|
|
|
}
|
|
|
|
|
2021-08-15 13:44:23 +00:00
|
|
|
[Test]
|
2021-08-15 14:02:25 +00:00
|
|
|
public void TestMarkAsReadIgnoringLocalMessages()
|
|
|
|
{
|
2021-08-15 13:44:23 +00:00
|
|
|
Channel channel = null;
|
|
|
|
|
|
|
|
AddStep("join channel and select it", () =>
|
|
|
|
{
|
|
|
|
channelManager.JoinChannel(channel = createChannel(1, ChannelType.Public));
|
|
|
|
channelManager.CurrentChannel.Value = channel;
|
|
|
|
});
|
|
|
|
|
|
|
|
AddStep("post message", () => channelManager.PostMessage("Something interesting"));
|
2022-11-11 05:55:41 +00:00
|
|
|
AddUntilStep("message postesd", () => !channel.Messages.Any(m => m is LocalMessage));
|
2021-08-15 18:35:52 +00:00
|
|
|
|
2021-08-15 13:44:23 +00:00
|
|
|
AddStep("post /help command", () => channelManager.PostCommand("help", channel));
|
|
|
|
AddStep("post /me command with no action", () => channelManager.PostCommand("me", channel));
|
|
|
|
AddStep("post /join command with no channel", () => channelManager.PostCommand("join", channel));
|
|
|
|
AddStep("post /join command with non-existent channel", () => channelManager.PostCommand("join i-dont-exist", channel));
|
|
|
|
AddStep("post non-existent command", () => channelManager.PostCommand("non-existent-cmd arg", channel));
|
|
|
|
|
2021-08-15 18:35:52 +00:00
|
|
|
AddStep("mark channel as read", () => channelManager.MarkChannelAsRead(channel));
|
|
|
|
AddAssert("channel's last read ID is set to the latest message", () => channel.LastReadId == sentMessages.Last().Id);
|
|
|
|
}
|
2021-08-15 13:44:23 +00:00
|
|
|
|
2022-11-02 08:13:14 +00:00
|
|
|
[Test]
|
|
|
|
public void TestSilencedUsersAreRemoved()
|
|
|
|
{
|
|
|
|
Channel channel = null;
|
|
|
|
|
|
|
|
AddStep("join channel and select it", () =>
|
|
|
|
{
|
|
|
|
channelManager.JoinChannel(channel = createChannel(1, ChannelType.Public));
|
|
|
|
channelManager.CurrentChannel.Value = channel;
|
|
|
|
});
|
|
|
|
|
|
|
|
AddStep("post message", () => channelManager.PostMessage("Definitely something bad"));
|
|
|
|
|
|
|
|
AddStep("mark user as silenced and send ack request", () =>
|
|
|
|
{
|
|
|
|
silencedUserIds.Add(API.LocalUser.Value.OnlineID);
|
|
|
|
channelManager.SendAck();
|
|
|
|
});
|
|
|
|
|
|
|
|
AddAssert("channel has no more messages", () => channel.Messages, () => Is.Empty);
|
|
|
|
}
|
|
|
|
|
2021-08-15 18:35:52 +00:00
|
|
|
private void handlePostMessageRequest(PostMessageRequest request)
|
|
|
|
{
|
|
|
|
var message = new Message(++currentMessageId)
|
|
|
|
{
|
|
|
|
IsAction = request.Message.IsAction,
|
|
|
|
ChannelId = request.Message.ChannelId,
|
|
|
|
Content = request.Message.Content,
|
|
|
|
Links = request.Message.Links,
|
|
|
|
Timestamp = request.Message.Timestamp,
|
2022-11-11 05:55:41 +00:00
|
|
|
Sender = request.Message.Sender,
|
|
|
|
Uuid = request.Message.Uuid
|
2021-08-15 18:35:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
sentMessages.Add(message);
|
|
|
|
request.TriggerSuccess(message);
|
|
|
|
}
|
2021-08-15 13:44:23 +00:00
|
|
|
|
2021-08-15 18:35:52 +00:00
|
|
|
private void handleMarkChannelAsReadRequest(MarkChannelAsReadRequest request)
|
|
|
|
{
|
|
|
|
// only accept messages that were sent through the API
|
|
|
|
if (sentMessages.Contains(request.Message))
|
|
|
|
{
|
|
|
|
request.TriggerSuccess();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
request.TriggerFailure(new APIException("unknown message!", null));
|
|
|
|
}
|
2021-08-15 13:44:23 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 09:02:44 +00:00
|
|
|
private Channel createChannel(int id, ChannelType type) => new Channel(new APIUser())
|
2021-06-29 07:30:46 +00:00
|
|
|
{
|
|
|
|
Id = id,
|
|
|
|
Name = $"Channel {id}",
|
|
|
|
Topic = $"Topic of channel {id} with type {type}",
|
|
|
|
Type = type,
|
2021-08-15 13:44:23 +00:00
|
|
|
LastMessageId = 0,
|
2021-06-29 07:30:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private partial class ChannelManagerContainer : CompositeDrawable
|
|
|
|
{
|
|
|
|
[Cached]
|
2022-06-08 09:54:23 +00:00
|
|
|
public ChannelManager ChannelManager { get; }
|
2021-06-29 07:30:46 +00:00
|
|
|
|
2022-06-08 09:54:23 +00:00
|
|
|
public ChannelManagerContainer(IAPIProvider apiProvider)
|
2021-06-29 07:30:46 +00:00
|
|
|
{
|
2022-11-04 09:51:00 +00:00
|
|
|
InternalChild = ChannelManager = new ChannelManager(apiProvider);
|
2021-06-29 07:30:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|