Files
AIC-Project/Client/Assets/Script/Tests/EditMode/WorldChatModuleTests.cs
T

89 lines
2.6 KiB
C#

using System;
using System.Reflection;
using NUnit.Framework;
using TMPro;
using UnityEditor;
using UnityEngine;
using XGame;
using XWorld.Framework.Protocol;
public sealed class WorldChatModuleTests
{
private GameObject view;
private GameObject host;
[TearDown]
public void TearDown()
{
if (view != null)
{
UnityEngine.Object.DestroyImmediate(view);
}
if (host != null)
{
UnityEngine.Object.DestroyImmediate(host);
}
}
[Test]
public void EndEditingInput_SubmitsTrimmedWorldChat()
{
WorldChatModule module = CreateBoundModule();
TMP_InputField input = view.transform.FindTransform("InputHitArea").GetComponent<TMP_InputField>();
string sent = null;
module.OnSendWorldChat = text => sent = text;
input.text = " hello world ";
input.onEndEdit.Invoke(input.text);
Assert.That(sent, Is.EqualTo("hello world"));
Assert.That(input.text, Is.Empty);
}
[Test]
public void ReceivedMessage_UpdatesCollapsedPreview()
{
WorldChatModule module = CreateBoundModule();
TMP_Text collapsedText = view.transform.FindTransform("Txt_CollapsedPlaceholder").GetComponent<TMP_Text>();
module.HandleWorldChatMessage(new WorldChatMessageMsg
{
PlayerId = 2,
PlayerName = "Bob",
Text = "hello world"
});
Assert.That(collapsedText.text, Is.EqualTo("Bob: hello world"));
}
private WorldChatModule CreateBoundModule()
{
GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Game/Art/UI/Prefab/UI_CommonChat.prefab");
Assert.That(prefab, Is.Not.Null);
view = UnityEngine.Object.Instantiate(prefab);
host = new GameObject("WorldChatModuleTestHost");
WorldChatModule module = host.AddComponent<WorldChatModule>();
module.Initialize(1, _ => { });
SetPrivateField(module, "view", view);
InvokePrivate(module, "CacheControls");
InvokePrivate(module, "BindEvents");
return module;
}
private static void SetPrivateField(object target, string name, object value)
{
FieldInfo field = target.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic);
Assert.That(field, Is.Not.Null);
field.SetValue(target, value);
}
private static void InvokePrivate(object target, string name)
{
MethodInfo method = target.GetType().GetMethod(name, BindingFlags.Instance | BindingFlags.NonPublic);
Assert.That(method, Is.Not.Null);
method.Invoke(target, Array.Empty<object>());
}
}