完成 P2 前架构整改
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FlowScope.UI;
|
||||
using NUnit.Framework;
|
||||
using UnityEngine;
|
||||
@@ -58,6 +59,27 @@ namespace FlowScope.Tests.PlayMode.UI
|
||||
Assert.That(resources.Handles, Has.Count.EqualTo(1));
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator PreloadAsync_WhenSamePanelLoadsConcurrently_UsesOneResourceLoad()
|
||||
{
|
||||
var prefab = CreatePrefab<ExplicitPathPanel>("ExplicitPathPrefab");
|
||||
var resources = new GatedUITestResources("Custom/Explicit", prefab);
|
||||
var preload = new UIPreloadService(resources);
|
||||
|
||||
var first = preload.PreloadAsync<ExplicitPathPanel>(CancellationToken.None);
|
||||
var second = preload.PreloadAsync<ExplicitPathPanel>(CancellationToken.None);
|
||||
|
||||
Assert.That(resources.LoadCalls, Is.EqualTo(1));
|
||||
|
||||
resources.Complete();
|
||||
yield return UITestAsync.Await(first);
|
||||
yield return UITestAsync.Await(second);
|
||||
|
||||
Assert.That(resources.LoadCalls, Is.EqualTo(1));
|
||||
Assert.That(resources.Handle.IsDisposed, Is.False);
|
||||
Assert.That(preload.IsPreloaded<ExplicitPathPanel>(), Is.True);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator OpenAsync_WhenPanelIsPreloaded_ReusesPreloadedHandle()
|
||||
{
|
||||
@@ -111,5 +133,52 @@ namespace FlowScope.Tests.PlayMode.UI
|
||||
gameObject.AddComponent<TPanel>();
|
||||
return gameObject;
|
||||
}
|
||||
|
||||
private sealed class GatedUITestResources : FlowScope.Resources.IResourceService
|
||||
{
|
||||
private readonly string _key;
|
||||
private readonly GameObject _asset;
|
||||
private readonly TaskCompletionSource<FlowScope.Resources.IResourceHandle<GameObject>> _gate =
|
||||
new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
|
||||
public GatedUITestResources(string key, GameObject asset)
|
||||
{
|
||||
_key = key;
|
||||
_asset = asset;
|
||||
Handle = new UITestResourceHandle<GameObject>(key, asset);
|
||||
}
|
||||
|
||||
public int LoadCalls { get; private set; }
|
||||
public UITestResourceHandle<GameObject> Handle { get; }
|
||||
|
||||
public Task<FlowScope.Resources.IResourceHandle<T>> LoadAsync<T>(
|
||||
string key,
|
||||
CancellationToken cancellationToken)
|
||||
where T : class
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
LoadCalls++;
|
||||
Assert.That(key, Is.EqualTo(_key));
|
||||
return AwaitHandle<T>();
|
||||
}
|
||||
|
||||
public FlowScope.Resources.IResourceGroup CreateGroup()
|
||||
{
|
||||
throw new System.NotSupportedException();
|
||||
}
|
||||
|
||||
public void Complete()
|
||||
{
|
||||
_gate.SetResult(Handle);
|
||||
}
|
||||
|
||||
private async Task<FlowScope.Resources.IResourceHandle<T>> AwaitHandle<T>()
|
||||
where T : class
|
||||
{
|
||||
var handle = await _gate.Task;
|
||||
Assert.That(handle.Asset, Is.SameAs(_asset));
|
||||
return (FlowScope.Resources.IResourceHandle<T>)(object)handle;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user