yyl
2026-05-11 51b0f6ed9f4e1d3bb6f8144470b46908c7699a96
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// ============================================================================
// 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<InvalidOperationException>(() =>
            {
                _service.LoadAssetAsync<UnityEngine.Object>("test_location").GetAwaiter().GetResult();
            });
        }
 
        [Test]
        public void ThrowsIfNotInitialized_LoadAssetAsyncByType()
        {
            Assert.Throws<InvalidOperationException>(() =>
            {
                _service.LoadAssetAsync("test_location", typeof(UnityEngine.Object)).GetAwaiter().GetResult();
            });
        }
 
        [Test]
        public void ThrowsIfNotInitialized_LoadRawFileTextAsync()
        {
            Assert.Throws<InvalidOperationException>(() =>
            {
                _service.LoadRawFileTextAsync("test_location").GetAwaiter().GetResult();
            });
        }
 
        [Test]
        public void ThrowsIfNotInitialized_LoadRawFileBytesAsync()
        {
            Assert.Throws<InvalidOperationException>(() =>
            {
                _service.LoadRawFileBytesAsync("test_location").GetAwaiter().GetResult();
            });
        }
 
        [Test]
        public void ThrowsIfNotInitialized_CheckLocationValid()
        {
            Assert.Throws<InvalidOperationException>(() =>
            {
                _service.CheckLocationValid("test_location");
            });
        }
 
        [Test]
        public void ThrowsIfNotInitialized_GetAssetInfosByTag()
        {
            Assert.Throws<InvalidOperationException>(() =>
            {
                _service.GetAssetInfosByTag("test_tag");
            });
        }
 
        [Test]
        public void ThrowsIfNotInitialized_IsNeedDownloadFromRemote()
        {
            Assert.Throws<InvalidOperationException>(() =>
            {
                _service.IsNeedDownloadFromRemote("test_location");
            });
        }
 
        [Test]
        public void ThrowsIfNotInitialized_UnloadUnusedAssetsAsync()
        {
            Assert.Throws<InvalidOperationException>(() =>
            {
                _service.UnloadUnusedAssetsAsync().GetAwaiter().GetResult();
            });
        }
 
        [Test]
        public void ThrowsIfNotInitialized_UnloadAllAssetsAsync()
        {
            Assert.Throws<InvalidOperationException>(() =>
            {
                _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<UnityEngine.Object>("test_location");
            Assert.IsNull(result);
        }
 
        [Test]
        public void IYooAssetBridge_IsRegistered_ReturnsFalse_BeforeInit()
        {
            IYooAssetBridge bridge = _service;
            Assert.IsFalse(bridge.IsRegistered);
        }
    }
}