using System.Collections;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.TestTools;
using TMPro;
using BaseGames.UI;
namespace BaseGames.Tests.PlayMode
{
///
/// ToastManager 队列行为测试:
/// · Enqueue 一条后 Toast 激活并最终隐藏
/// · 连续 Enqueue 3 条按序串行播放
///
/// 不依赖事件频道(直接调用 Enqueue),不依赖本地化(标题/正文为常量字符串)。
///
public class ToastManagerTests
{
private GameObject _host;
private ToastManager _mgr;
private ToastNotification _toast;
[SetUp]
public void SetUp()
{
_host = new GameObject("ToastHost");
// Toast 预制:CanvasGroup + 子文本
var toastGO = new GameObject("Toast", typeof(CanvasGroup));
toastGO.transform.SetParent(_host.transform);
toastGO.SetActive(false);
_toast = toastGO.AddComponent();
// 反射注入 _displayDuration / _fadeDuration 减为短值,缩短测试时长
SetPrivate(_toast, "_displayDuration", 0.05f);
SetPrivate(_toast, "_fadeDuration", 0.02f);
_mgr = _host.AddComponent();
SetPrivate(_mgr, "_toast", _toast);
_host.SetActive(false);
_host.SetActive(true); // 触发 Awake/OnEnable
}
[TearDown]
public void TearDown() { Object.DestroyImmediate(_host); }
[UnityTest]
public IEnumerator Enqueue_ShowsThenHides()
{
_mgr.Enqueue("T", "B", null);
yield return null;
Assert.IsTrue(_toast.gameObject.activeSelf, "入队后 Toast 应当激活");
// 总时长 ~= 0.02+0.05+0.02 = 0.09s,再 + 队列等待 0.1s
yield return new WaitForSecondsRealtime(0.5f);
Assert.IsFalse(_toast.gameObject.activeSelf, "Toast 自动隐藏未生效");
}
[UnityTest]
public IEnumerator MultipleEnqueue_PlaysSerially()
{
_mgr.Enqueue("A", "1", null);
_mgr.Enqueue("B", "2", null);
yield return null;
Assert.IsTrue(_toast.gameObject.activeSelf, "第一条应当立即播放");
// 不严格验证内容(涉及私有字段),只验证活动状态推进。
yield return new WaitForSecondsRealtime(1.0f);
Assert.IsFalse(_toast.gameObject.activeSelf, "两条串行播放后应当全部结束");
}
// ── 反射工具 ──────────────────────────────────────────────────────
private static void SetPrivate(object target, string fieldName, object value)
{
var f = target.GetType().GetField(fieldName,
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
f?.SetValue(target, value);
}
}
}