// ============================================================================ // YooAssetServiceTests.cs — YooAssetService 核心逻辑单元测试 // 覆盖:初始化状态机、LoadAssetAsync null/invalid 处理、ReleaseHandle 幂等性 // ============================================================================ using System; using NUnit.Framework; using ProjSG.Resource; namespace ProjSG.Resource.Tests { [TestFixture] public class YooAssetServiceTests { private YooAssetService _service; [SetUp] public void SetUp() { // 确保每个测试开始时销毁旧实例 if (YooAssetService.IsValid()) { YooAssetService.Destroy(); } _service = YooAssetService.Instance; } [TearDown] public void TearDown() { if (YooAssetService.IsValid()) { YooAssetService.Destroy(); } } // ==================================================================== // 初始化状态机测试 // ==================================================================== [Test] public void IsInitialized_BeforeInit_ReturnsFalse() { Assert.IsFalse(_service.IsInitialized); } [Test] public void ThrowsIfNotInitialized_LoadAssetAsync() { // 未初始化时调用加载方法应抛出 InvalidOperationException // ThrowIfNotInitialized 在异步方法同步部分抛出,GetAwaiter().GetResult() 会立即重新抛出 Assert.Throws(() => { _service.LoadAssetAsync("test_location").GetAwaiter().GetResult(); }); } [Test] public void ThrowsIfNotInitialized_LoadAssetAsyncByType() { Assert.Throws(() => { _service.LoadAssetAsync("test_location", typeof(UnityEngine.Object)).GetAwaiter().GetResult(); }); } [Test] public void ThrowsIfNotInitialized_LoadRawFileTextAsync() { Assert.Throws(() => { _service.LoadRawFileTextAsync("test_location").GetAwaiter().GetResult(); }); } [Test] public void ThrowsIfNotInitialized_LoadRawFileBytesAsync() { Assert.Throws(() => { _service.LoadRawFileBytesAsync("test_location").GetAwaiter().GetResult(); }); } [Test] public void ThrowsIfNotInitialized_CheckLocationValid() { Assert.Throws(() => { _service.CheckLocationValid("test_location"); }); } [Test] public void ThrowsIfNotInitialized_GetAssetInfosByTag() { Assert.Throws(() => { _service.GetAssetInfosByTag("test_tag"); }); } [Test] public void ThrowsIfNotInitialized_IsNeedDownloadFromRemote() { Assert.Throws(() => { _service.IsNeedDownloadFromRemote("test_location"); }); } [Test] public void ThrowsIfNotInitialized_UnloadUnusedAssetsAsync() { Assert.Throws(() => { _service.UnloadUnusedAssetsAsync().GetAwaiter().GetResult(); }); } [Test] public void ThrowsIfNotInitialized_UnloadAllAssetsAsync() { Assert.Throws(() => { _service.UnloadAllAssetsAsync().GetAwaiter().GetResult(); }); } // ==================================================================== // ReleaseHandle 幂等性测试 // ==================================================================== [Test] public void ReleaseHandle_WithNull_DoesNotThrow() { // ReleaseHandle(null) 应安全返回,不抛异常 Assert.DoesNotThrow(() => { _service.ReleaseHandle(null); }); } [Test] public void ReleaseHandle_CalledTwiceWithNull_StillDoesNotThrow() { // 多次调用 null 仍安全 Assert.DoesNotThrow(() => { _service.ReleaseHandle(null); _service.ReleaseHandle(null); }); } // ==================================================================== // Singleton 行为测试 // ==================================================================== [Test] public void Singleton_Instance_ReturnsSameInstance() { var instance1 = YooAssetService.Instance; var instance2 = YooAssetService.Instance; Assert.AreSame(instance1, instance2); } [Test] public void Singleton_IsValid_ReturnsTrue() { _ = YooAssetService.Instance; Assert.IsTrue(YooAssetService.IsValid()); } [Test] public void Singleton_AfterDestroy_IsValidReturnsFalse() { _ = YooAssetService.Instance; YooAssetService.Destroy(); Assert.IsFalse(YooAssetService.IsValid()); } [Test] public void Singleton_AfterDestroy_NewInstanceCreated() { var instance1 = YooAssetService.Instance; YooAssetService.Destroy(); var instance2 = YooAssetService.Instance; Assert.AreNotSame(instance1, instance2); } // ==================================================================== // IYooAssetBridge 接口行为测试 // ==================================================================== [Test] public void IYooAssetBridge_GetCached_ReturnsNull_BeforeUS4() { // GetCached 在 US4 集成前返回 null IYooAssetBridge bridge = _service; var result = bridge.GetCached("test_location"); Assert.IsNull(result); } [Test] public void IYooAssetBridge_IsRegistered_ReturnsFalse_BeforeInit() { IYooAssetBridge bridge = _service; Assert.IsFalse(bridge.IsRegistered); } } }