csharp-sdk-upm/Realtime/Realtime.Test/Message.cs

282 lines
9.8 KiB
C#
Raw Normal View History

using NUnit.Framework;
2020-04-27 11:54:01 +08:00
using System;
2020-04-26 16:15:53 +08:00
using System.Collections.ObjectModel;
using System.Threading.Tasks;
2020-04-28 17:04:46 +08:00
using System.Collections.Generic;
using LeanCloud;
using LeanCloud.Storage;
using LeanCloud.Realtime;
2020-04-26 16:15:53 +08:00
using static NUnit.Framework.TestContext;
2020-04-27 17:44:53 +08:00
/// <summary>
2020-04-28 11:20:51 +08:00
/// Emoji 消息
2020-04-27 17:44:53 +08:00
/// </summary>
2020-04-28 11:20:51 +08:00
class EmojiMessage : LCIMTypedMessage {
public const int EmojiMessageType = 1;
2020-04-27 17:44:53 +08:00
2020-04-28 11:20:51 +08:00
public override int MessageType => EmojiMessageType;
2020-04-27 17:44:53 +08:00
public string Ecode {
get {
return data["ecode"] as string;
} set {
data["ecode"] = value;
}
}
}
namespace Realtime.Test {
public class Message {
private LCIMClient m1;
private LCIMClient m2;
private LCIMConversation conversation;
[SetUp]
public async Task SetUp() {
LCLogger.LogDelegate += Utils.Print;
LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com");
m1 = new LCIMClient("m1");
m2 = new LCIMClient("m2");
await m1.Open();
await m2.Open();
conversation = await m1.CreateConversation(new string[] { "m2" });
}
[TearDown]
public async Task TearDown() {
await m1.Close();
await m2.Close();
LCLogger.LogDelegate -= Utils.Print;
}
[Test]
2020-04-26 16:15:53 +08:00
[Order(0)]
public async Task Send() {
2020-04-26 16:15:53 +08:00
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
int count = 0;
m2.OnMessage = (conv, msg) => {
WriteLine(msg.Id);
if (msg is LCIMImageMessage imageMsg) {
WriteLine($"-------- url: {imageMsg.Url}");
2020-04-26 16:15:53 +08:00
count++;
} else if (msg is LCIMFileMessage fileMsg) {
WriteLine($"-------- name: {fileMsg.Format}");
2020-04-26 16:15:53 +08:00
count++;
} else if (msg is LCIMTextMessage textMsg) {
WriteLine($"-------- text: {textMsg.Text}");
2020-04-26 16:15:53 +08:00
count++;
}
if (count >= 3) {
tcs.SetResult(null);
}
};
LCIMTextMessage textMessage = new LCIMTextMessage("hello, world");
await conversation.Send(textMessage);
Assert.NotNull(textMessage.Id);
2020-04-30 15:36:06 +08:00
LCFile image = new LCFile("hello", "../../../../../assets/hello.png");
await image.Save();
LCIMImageMessage imageMessage = new LCIMImageMessage(image);
await conversation.Send(imageMessage);
Assert.NotNull(imageMessage.Id);
2020-04-30 15:36:06 +08:00
LCFile file = new LCFile("apk", "../../../../../assets/test.apk");
await file.Save();
LCIMFileMessage fileMessage = new LCIMFileMessage(file);
await conversation.Send(fileMessage);
Assert.NotNull(fileMessage.Id);
2020-04-26 16:15:53 +08:00
await tcs.Task;
}
[Test]
[Order(1)]
public async Task AckAndRead() {
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
m2.OnMessage = async (conv, msg) => {
await conv.Read();
};
m1.OnMessageDelivered = (conv, msgId) => {
WriteLine($"{msgId} is delivered.");
};
m1.OnMessageRead = (conv, msgId) => {
WriteLine($"{msgId} is read.");
tcs.SetResult(null);
};
LCIMTextMessage textMessage = new LCIMTextMessage("hello");
LCIMMessageSendOptions options = new LCIMMessageSendOptions {
Receipt = true
};
await conversation.Send(textMessage, options);
await tcs.Task;
}
[Test]
[Order(2)]
public async Task Recall() {
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
m2.OnMessageRecalled = (conv, msgId) => {
WriteLine($"{msgId} is recalled.");
tcs.SetResult(null);
};
LCIMTextMessage textMessage = new LCIMTextMessage("I will be recalled.");
await conversation.Send(textMessage);
await Task.Delay(1000);
await conversation.RecallMessage(textMessage);
await tcs.Task;
}
[Test]
[Order(3)]
public async Task Update() {
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
m2.OnMessageUpdated = (conv, msg) => {
Assert.True(msg is LCIMTextMessage);
LCIMTextMessage textMessage = msg as LCIMTextMessage;
Assert.AreEqual(textMessage.Text, "world");
WriteLine($"{msg.Id} is updated");
tcs.SetResult(null);
};
LCIMTextMessage oldMessage = new LCIMTextMessage("hello");
await conversation.Send(oldMessage);
await Task.Delay(1000);
LCIMTextMessage newMessage = new LCIMTextMessage("world");
await conversation.UpdateMessage(oldMessage, newMessage);
await tcs.Task;
}
[Test]
[Order(4)]
public async Task Query() {
ReadOnlyCollection<LCIMMessage> messages = await conversation.QueryMessages();
Assert.Greater(messages.Count, 0);
foreach (LCIMMessage message in messages) {
Assert.AreEqual(message.ConversationId, conversation.Id);
Assert.NotNull(message.Id);
WriteLine(message.Id);
}
}
2020-04-27 11:54:01 +08:00
[Test]
[Order(5)]
public async Task Unread() {
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
string clientId = Guid.NewGuid().ToString();
LCIMClient client = new LCIMClient(clientId);
LCIMConversation conversation = await m1.CreateConversation(new string[] { clientId });
2020-04-28 17:04:46 +08:00
2020-04-27 11:54:01 +08:00
LCIMTextMessage textMessage = new LCIMTextMessage("hello");
await conversation.Send(textMessage);
2020-04-28 17:04:46 +08:00
2020-04-27 11:54:01 +08:00
client.OnUnreadMessagesCountUpdated = (convs) => {
foreach (LCIMConversation conv in convs) {
WriteLine($"unread count: {conv.Unread}");
2020-04-30 11:53:15 +08:00
//Assert.AreEqual(conv.Unread, 1);
2020-04-29 16:39:02 +08:00
//Assert.True(conv.LastMessage is LCIMTextMessage);
//LCIMTextMessage textMsg = conv.LastMessage as LCIMTextMessage;
//Assert.AreEqual(textMsg.Text, "hello");
2020-04-27 11:54:01 +08:00
}
};
await client.Open();
2020-04-28 17:04:46 +08:00
client.OnMessage = (conv, msg) => {
WriteLine($"unread count: {conv.Unread}");
Assert.AreEqual(conv.Unread, 2);
Assert.True(conv.LastMessage is LCIMTextMessage);
LCIMTextMessage textMsg = conv.LastMessage as LCIMTextMessage;
Assert.AreEqual(textMsg.Text, "world");
tcs.SetResult(true);
};
textMessage = new LCIMTextMessage("world");
await conversation.Send(textMessage);
2020-04-27 11:54:01 +08:00
await tcs.Task;
}
2020-04-27 14:47:04 +08:00
[Test]
[Order(6)]
public async Task Attributes() {
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
m2.OnMessage = (conv, msg) => {
Assert.True(msg is LCIMTypedMessage);
LCIMTypedMessage typedMsg = msg as LCIMTypedMessage;
Assert.AreEqual(typedMsg["k1"], 123);
Assert.True((bool)typedMsg["k2"]);
Assert.AreEqual(typedMsg["k3"], "code");
tcs.SetResult(null);
};
LCIMTextMessage textMsg = new LCIMTextMessage("hi");
textMsg["k1"] = 123;
textMsg["k2"] = true;
textMsg["k3"] = "code";
await conversation.Send(textMsg);
await tcs.Task;
}
2020-04-27 17:44:53 +08:00
[Test]
[Order(7)]
public async Task Custom() {
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
// 注册自定义类型消息
2020-04-28 11:20:51 +08:00
LCIMTypedMessage.Register(EmojiMessage.EmojiMessageType,
() => new EmojiMessage());
2020-04-27 17:44:53 +08:00
m2.OnMessage = (conv, msg) => {
2020-04-28 11:20:51 +08:00
Assert.True(msg is EmojiMessage);
EmojiMessage emojiMsg = msg as EmojiMessage;
Assert.AreEqual(emojiMsg.Ecode, "#0123");
2020-04-27 17:44:53 +08:00
tcs.SetResult(null);
};
2020-04-28 11:20:51 +08:00
EmojiMessage emojiMessage = new EmojiMessage {
2020-04-27 17:44:53 +08:00
Ecode = "#0123"
};
2020-04-28 11:20:51 +08:00
await conversation.Send(emojiMessage);
2020-04-27 17:44:53 +08:00
await tcs.Task;
}
2020-04-28 17:04:46 +08:00
[Test]
[Order(8)]
public async Task MentionList() {
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
m2.OnMessage = (conv, msg) => {
Assert.True(msg.Mentioned);
Assert.True(msg.MentionIdList.Contains(m2.Id));
tcs.SetResult(null);
};
LCIMTextMessage textMessage = new LCIMTextMessage("hello") {
MentionIdList = new List<string> { m2.Id }
};
await conversation.Send(textMessage);
await tcs.Task;
}
[Test]
[Order(9)]
public async Task MentionAll() {
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
m2.OnMessage = (conv, msg) => {
Assert.True(msg.Mentioned);
Assert.True(msg.MentionAll);
tcs.SetResult(null);
};
LCIMTextMessage textMessage = new LCIMTextMessage("world") {
MentionAll = true
};
await conversation.Send(textMessage);
await tcs.Task;
}
}
}