diff --git a/Test/Realtime.Test/Message.cs b/Test/Realtime.Test/Message.cs index c721a41..47d2494 100644 --- a/Test/Realtime.Test/Message.cs +++ b/Test/Realtime.Test/Message.cs @@ -1,14 +1,12 @@ using NUnit.Framework; -using System; -using System.Threading; +using System.Collections.ObjectModel; using System.Threading.Tasks; -using System.Collections.Generic; using LeanCloud; using LeanCloud.Common; using LeanCloud.Storage; using LeanCloud.Realtime; -using static System.Console; +using static NUnit.Framework.TestContext; namespace Realtime.Test { public class Message { @@ -36,16 +34,25 @@ namespace Realtime.Test { } [Test] + [Order(0)] public async Task Send() { - AutoResetEvent are = new AutoResetEvent(false); + TaskCompletionSource tcs = new TaskCompletionSource(); + + int count = 0; m2.OnMessage = (conv, msg) => { WriteLine(msg.Id); if (msg is LCIMImageMessage imageMsg) { WriteLine($"-------- url: {imageMsg.Url}"); + count++; } else if (msg is LCIMFileMessage fileMsg) { WriteLine($"-------- name: {fileMsg.Format}"); + count++; } else if (msg is LCIMTextMessage textMsg) { WriteLine($"-------- text: {textMsg.Text}"); + count++; + } + if (count >= 3) { + tcs.SetResult(null); } }; @@ -64,6 +71,79 @@ namespace Realtime.Test { LCIMFileMessage fileMessage = new LCIMFileMessage(file); await conversation.Send(fileMessage); Assert.NotNull(fileMessage.Id); + + await tcs.Task; + } + + [Test] + [Order(1)] + public async Task AckAndRead() { + TaskCompletionSource tcs = new TaskCompletionSource(); + 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 tcs = new TaskCompletionSource(); + 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 tcs = new TaskCompletionSource(); + 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 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); + } } } }