Files
zeling_v2/Assets/Tests/PlayMode/UITweenTests.cs
2026-05-25 13:21:41 +08:00

61 lines
2.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.TestTools;
using BaseGames.UI;
namespace BaseGames.Tests.PlayMode
{
/// <summary>
/// UITween 静态库测试验证补间在终态吸附、零时长立即返回、null 安全。
/// </summary>
public class UITweenTests
{
private GameObject _go;
[SetUp] public void SetUp() { _go = new GameObject("TweenHost", typeof(RectTransform), typeof(CanvasGroup)); }
[TearDown] public void TearDown() { Object.DestroyImmediate(_go); }
[UnityTest]
public IEnumerator MoveAnchored_ReachesTarget()
{
var rect = _go.GetComponent<RectTransform>();
rect.anchoredPosition = Vector2.zero;
yield return _go.AddComponent<TestRunner>().Run(UITween.MoveAnchored(rect, new Vector2(100, 50), 0.05f));
Assert.AreEqual(new Vector2(100, 50), rect.anchoredPosition);
}
[UnityTest]
public IEnumerator FadeCanvasGroup_ReachesTarget()
{
var cg = _go.GetComponent<CanvasGroup>();
cg.alpha = 0f;
yield return _go.AddComponent<TestRunner>().Run(UITween.FadeCanvasGroup(cg, 1f, 0.05f));
Assert.AreEqual(1f, cg.alpha, 0.001f);
}
[UnityTest]
public IEnumerator ZeroDuration_SnapsImmediately()
{
var rect = _go.GetComponent<RectTransform>();
yield return _go.AddComponent<TestRunner>().Run(UITween.MoveAnchored(rect, new Vector2(7, 7), 0f));
Assert.AreEqual(new Vector2(7, 7), rect.anchoredPosition);
}
[UnityTest]
public IEnumerator NullTarget_NoException()
{
yield return _go.AddComponent<TestRunner>().Run(UITween.MoveAnchored(null, Vector2.one, 0.05f));
yield return _go.AddComponent<TestRunner>().Run(UITween.FadeCanvasGroup(null, 1f, 0.05f));
Assert.Pass();
}
/// <summary>挂宿主跑协程的辅助 MonoBehaviour测试场景无 EventSystem。</summary>
private class TestRunner : MonoBehaviour
{
public IEnumerator Run(IEnumerator inner) { yield return StartCoroutine(inner); }
}
}
}