update test sample

增加cache file system单元测试
This commit is contained in:
何冠峰
2025-07-17 21:26:13 +08:00
parent ac7ee16017
commit db159428c6
42 changed files with 890 additions and 269 deletions
@@ -3,18 +3,115 @@ using System.IO;
using System.Text;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.U2D;
using UnityEngine.TestTools;
using NUnit.Framework;
using YooAsset;
public class T3_TestCacheFileSystem : IPrebuildSetup, IPostBuildCleanup
{
public void Setup()
{
throw new NotImplementedException();
}
public void Cleanup()
{
throw new NotImplementedException();
}
[UnityTest]
public IEnumerator A_InitializePackage()
{
// 清空旧的缓存目录
string projectPath = Path.GetDirectoryName(Application.dataPath);
string cacheRoot = $"{projectPath}/yoo";
Directory.Delete(cacheRoot, true);
// 拷贝打包资源到本地服务器
{
string packageRoot = string.Empty;
#if UNITY_EDITOR
packageRoot = UnityEditor.EditorPrefs.GetString(T2_TestBuldinFileSystem.ASSET_BUNDLE_PACKAGE_ROOT_KEY);
#endif
if (Directory.Exists(packageRoot) == false)
throw new Exception($"Not found package root : {packageRoot}");
string testServerDirectory = "C://xampp/htdocs/CDN/Android/Test";
}
// 初始化资源包 ASSET_BUNDLE
{
var package = YooAssets.CreatePackage(TestDefine.AssetBundlePackageName);
// 初始化资源包
var initParams = new HostPlayModeParameters();
var fileDecryption = new TestFileStreamDecryption();
var manifestProcess = new TestProcessManifest();
string hostServerIP = "http://127.0.0.1/CDN/Android/Test/";
var remoteServices = new TestRemoteServices(hostServerIP);
initParams.BuildinFileSystemParameters = null;
initParams.CacheFileSystemParameters = FileSystemParameters.CreateDefaultCacheFileSystemParameters(remoteServices, fileDecryption);
initParams.CacheFileSystemParameters.AddParameter(FileSystemParametersDefine.MANIFEST_SERVICES, manifestProcess);
var initializeOp = package.InitializeAsync(initParams);
yield return initializeOp;
if (initializeOp.Status != EOperationStatus.Succeed)
Debug.LogError(initializeOp.Error);
Assert.AreEqual(EOperationStatus.Succeed, initializeOp.Status);
// 请求资源版本
var requetVersionOp = package.RequestPackageVersionAsync();
yield return requetVersionOp;
if (requetVersionOp.Status != EOperationStatus.Succeed)
Debug.LogError(requetVersionOp.Error);
Assert.AreEqual(EOperationStatus.Succeed, requetVersionOp.Status);
// 更新资源清单
var updateManifestOp = package.UpdatePackageManifestAsync(requetVersionOp.PackageVersion);
yield return updateManifestOp;
if (updateManifestOp.Status != EOperationStatus.Succeed)
Debug.LogError(updateManifestOp.Error);
Assert.AreEqual(EOperationStatus.Succeed, updateManifestOp.Status);
}
}
private class TestRemoteServices : IRemoteServices
{
private readonly string _localServerRoot;
public TestRemoteServices(string localServerRoot)
{
_localServerRoot = localServerRoot;
}
string IRemoteServices.GetRemoteMainURL(string fileName)
{
return $"{_localServerRoot}/{fileName}";
}
string IRemoteServices.GetRemoteFallbackURL(string fileName)
{
return $"{_localServerRoot}/{fileName}";
}
}
[UnityTest]
public IEnumerator B1_TestBundlePlaying()
{
var tester = new TestBundlePlaying();
yield return tester.RuntimeTester();
}
[UnityTest]
public IEnumerator B2_TestBundleImporter()
{
var tester = new TestBundleImporter();
yield return tester.RuntimeTester();
}
[UnityTest]
public IEnumerator B3_TestBundleDownloader()
{
var tester = new TestBundleDownloader();
yield return tester.RuntimeTester();
}
}
@@ -0,0 +1,47 @@
using System;
using System.IO;
using System.Text;
using System.Collections;
using UnityEngine;
using NUnit.Framework;
using YooAsset;
/// <summary>
/// 测试远端文件下载
/// </summary>
public class TestBundleDownloader
{
public IEnumerator RuntimeTester()
{
ResourcePackage package = YooAssets.GetPackage(TestDefine.AssetBundlePackageName);
Assert.IsNotNull(package);
var downloader = package.CreateResourceDownloader(10, 1);
if (downloader.TotalDownloadCount == 0)
{
Assert.Fail($"Test downloader not found any file !");
}
downloader.BeginDownload();
downloader.DownloadFinishCallback = (DownloaderFinishData data) =>
{
if (data.Succeed == false)
Assert.Fail($"Test downloader failed ! {downloader.Error}");
};
// 等待一秒
yield return new WaitForSeconds(1f);
}
}
/* 资源代码流程
* 远端文件下载(下载器)
CacheFileSystem::DownloadFileAsync()
{
//RemoteServices返回CDN文件路径
string mainURL = RemoteServices.GetRemoteMainURL(bundle.FileName);
string fallbackURL = RemoteServices.GetRemoteFallbackURL(bundle.FileName);
options.SetURL(mainURL, fallbackURL);
return new DownloadPackageBundleOperation(bundle, options);
}
*/
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6d1ca2e6dac603a459531fb09e61fd31
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,57 @@
using System;
using System.IO;
using System.Text;
using System.Collections;
using UnityEngine;
using NUnit.Framework;
using YooAsset;
/// <summary>
/// 测试本地文件导入
/// </summary>
public class TestBundleImporter
{
public IEnumerator RuntimeTester()
{
ResourcePackage package = YooAssets.GetPackage(TestDefine.AssetBundlePackageName);
Assert.IsNotNull(package);
string packageRoot = UnityEditor.EditorPrefs.GetString(T2_TestBuldinFileSystem.ASSET_BUNDLE_PACKAGE_ROOT_KEY);
DirectoryInfo packageDir = new DirectoryInfo(packageRoot);
string fileRoot = $"{packageDir.Parent.FullName}/OutputCache";
ImportFileInfo fileInfoA = new ImportFileInfo();
fileInfoA.FilePath = $"{fileRoot}/assets_samples_test_sample_testres3_import_prefab_importa.bundle.encrypt";
fileInfoA.BundleName = "assets_samples_test_sample_testres3_import_prefab_importa.bundle";
ImportFileInfo fileInfoB = new ImportFileInfo();
fileInfoB.FilePath = $"{fileRoot}/assets_samples_test_sample_testres3_import_prefab_importb.bundle.encrypt";
fileInfoB.BundleName = "assets_samples_test_sample_testres3_import_prefab_importb.bundle";
ImportFileInfo[] importInfos = { fileInfoA, fileInfoB };
var unpacker = package.CreateResourceImporter(importInfos, 10, 1);
Assert.AreEqual(unpacker.TotalDownloadCount, 2);
unpacker.BeginDownload();
yield return unpacker;
Assert.AreEqual(EOperationStatus.Succeed, unpacker.Status);
// 等待一秒
yield return new WaitForSeconds(1f);
}
}
/* 资源代码流程
* 本地文件导入(导入器)
BundleInfo::CreateDownloader()
{
options.ImportFilePath = _importFilePath;
return _cacheFileSystem.DownloadFileAsync(Bundle, options);
}
CacheFileSystem::DownloadFileAsync()
{
string mainURL = ConvertToWWWPath(options.ImportFilePath);
options.SetURL(mainURL, mainURL);
return new DownloadPackageBundleOperation(bundle, options);
}
*/
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 84a2bd73c962d0a4c9c3373ee8cd5380
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,71 @@
using System;
using System.IO;
using System.Text;
using System.Collections;
using UnityEngine;
using NUnit.Framework;
using YooAsset;
/// <summary>
/// 测试边玩边下
/// </summary>
public class TestBundlePlaying
{
public IEnumerator RuntimeTester()
{
ResourcePackage package = YooAssets.GetPackage(TestDefine.AssetBundlePackageName);
Assert.IsNotNull(package);
if (package.IsNeedDownloadFromRemote("panel_a") == false)
{
Assert.Fail("Load bundle is already existed !");
}
if (package.IsNeedDownloadFromRemote("panel_b") == false)
{
Assert.Fail("Load bundle is already existed !");
}
// 测试异步加载
{
var assetsHandle = package.LoadAssetAsync<GameObject>("panel_a");
yield return assetsHandle;
Assert.AreEqual(EOperationStatus.Succeed, assetsHandle.Status);
}
// 测试同步加载
{
var assetsHandle = package.LoadAssetSync<GameObject>("panel_b");
yield return assetsHandle;
Assert.AreEqual(EOperationStatus.Failed, assetsHandle.Status);
yield return new WaitForSeconds(1f);
assetsHandle = package.LoadAssetSync<GameObject>("panel_b");
yield return assetsHandle;
Assert.AreEqual(EOperationStatus.Succeed, assetsHandle.Status);
}
// 等待一秒
yield return new WaitForSeconds(1f);
}
}
/* 资源代码流程
* 远端文件下载(加载器)
CacheFileSystem::LoadBundleFile()
{
_cacheFileSystem.LoadBundleFile(bundle);
}
CacheFileSystem::LoadAssetBundleOperation()
{
DownloadFileOptions options = new DownloadFileOptions(int.MaxValue, 60);
_cacheFileSystem.DownloadFileAsync(_bundle, options);
}
CacheFileSystem::DownloadFileAsync()
{
//RemoteServices返回CDN文件路径
string mainURL = RemoteServices.GetRemoteMainURL(bundle.FileName);
string fallbackURL = RemoteServices.GetRemoteFallbackURL(bundle.FileName);
options.SetURL(mainURL, fallbackURL);
return new DownloadPackageBundleOperation(bundle, options);
}
*/
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6db248f26f2eaaa4282a7bbdf709c321
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: