Skip to content

Commit

Permalink
update a few tests, optimize code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoborks committed Dec 29, 2024
1 parent b5cbfa6 commit be61365
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -286,21 +286,27 @@ async ValueTask<SceneResult> TransitionWithIntermediateAsync(SceneParameters sce
async ValueTask<SceneResult> TransitionWithIntermediateLoadingAsync(SceneParameters sceneParameters, ILoadSceneInfo intermediateSceneInfo, LoadingBehavior loadingBehavior, CancellationToken token)
{
LoadingProgress progress = loadingBehavior.Progress;
while (progress.State != LoadingState.Loading && !token.IsCancellationRequested)
await Awaitable.NextFrameAsync(token);
await WaitForLoadingStateAsync(progress, LoadingState.Loading, token);

await UnloadSourceSceneAsync(token);

Scene[] loadedScenes = await LoadAsync(sceneParameters, progress, token);
progress.SetState(LoadingState.TargetSceneLoaded);

while (progress.State != LoadingState.TransitionComplete && !token.IsCancellationRequested)
await Awaitable.NextFrameAsync(token);
await WaitForLoadingStateAsync(progress, LoadingState.TransitionComplete, token);

await UnloadAsync(new SceneParameters(intermediateSceneInfo), token);
return new SceneResult(loadedScenes);
}

async ValueTask WaitForLoadingStateAsync(LoadingProgress progress, LoadingState targetState, CancellationToken token = default)
{
while (progress.State != targetState && !token.IsCancellationRequested)
{
await Awaitable.NextFrameAsync(token);
}
}

async ValueTask<SceneResult> TransitionWithIntermediateNoLoadingAsync(SceneParameters sceneParameters, ILoadSceneInfo intermediateSceneInfo, CancellationToken token)
{
await UnloadSourceSceneAsync(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public readonly ILoadSceneInfo[] GetLoadSceneInfos()

public readonly bool ShouldSetActive()
{
return _setIndexActive == 0;
return _setIndexActive >= 0;
}

public readonly int GetIndexToActivate()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Text;
using System.Linq;
using UnityEngine.SceneManagement;

namespace MyGameDevTools.SceneLoading
Expand All @@ -19,7 +19,7 @@ public SceneResult(Scene[] sceneArray)

public SceneResult(Scene scene)
{
_sceneArray = null;
_sceneArray = new[] { scene };
_singleScene = scene;
}

Expand All @@ -41,27 +41,7 @@ public override string ToString()
if (!_singleScene.IsValid())
return "Empty SceneResult";

int sceneCount = (_sceneArray == null || _sceneArray.Length == 0) ? 1 : _sceneArray.Length;

StringBuilder builder = new("{ ");
if (sceneCount == 1)
{
builder.Append(_singleScene.name);
builder.Append(" }");
return builder.ToString();
}

for (int i = 0; i < sceneCount; i++)
{
if (i > 0)
{
builder.Append(", ");
}
builder.Append(_sceneArray[i].name);
}

builder.Append(" }");
return builder.ToString();
return $"{{ {string.Join(", ", _sceneArray.Select(scene => scene.name))} }}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ public IEnumerator LoadScenes([ValueSource(typeof(SceneTestEnvironment), nameof(
manager.SceneLoaded += reportSceneLoaded;

var progress = new SimpleProgress();
var loadTask = manager.LoadAsync(sceneParameters, progress).AsTask();
var loadTask = manager.LoadAsync(sceneParameters, progress);

Assert.AreEqual(0, progress.Value);

yield return new WaitTask<SceneResult>(loadTask);
yield return new WaitValueTask<SceneResult>(loadTask);

manager.SceneLoaded -= reportSceneLoaded;
Scene[] loadedScenes = loadTask.Result;
Expand All @@ -202,12 +202,12 @@ public IEnumerator LoadScene_Progress([ValueSource(typeof(SceneTestEnvironment),
{
var progress = new SimpleProgress();
Assert.AreEqual(0, progress.Value);
yield return new WaitTask<SceneResult>(manager.LoadAsync(sceneParameters, progress).AsTask());
yield return new WaitValueTask<SceneResult>(manager.LoadAsync(sceneParameters, progress));
Assert.AreEqual(1, progress.Value);
}

[UnityTest]
public IEnumerator LoadScene_Multiple([ValueSource(typeof(SceneTestEnvironment), nameof(SceneTestEnvironment.SceneManagers))] ISceneManager manager, [ValueSource(typeof(SceneTestEnvironment), nameof(SceneTestEnvironment.SceneParametersList))] SceneParameters sceneParameters)
public IEnumerator LoadScene_Stress([ValueSource(typeof(SceneTestEnvironment), nameof(SceneTestEnvironment.SceneManagers))] ISceneManager manager, [ValueSource(typeof(SceneTestEnvironment), nameof(SceneTestEnvironment.SceneParametersList))] SceneParameters sceneParameters)
{
ILoadSceneInfo[] sceneInfos = sceneParameters.GetLoadSceneInfos();
int length = sceneInfos.Length;
Expand All @@ -217,8 +217,8 @@ public IEnumerator LoadScene_Multiple([ValueSource(typeof(SceneTestEnvironment),

for (int i = 0; i < length; i++)
{
var loadTask = manager.LoadAsync(new SceneParameters(sceneInfos[i], setActive)).AsTask();
yield return new WaitTask<SceneResult>(loadTask);
var loadTask = manager.LoadAsync(new SceneParameters(sceneInfos[i], setActive));
yield return new WaitValueTask<SceneResult>(loadTask);
loadedScenes[i] = loadTask.Result;
}

Expand All @@ -240,7 +240,7 @@ public void LoadScene_NotInBuild([ValueSource(typeof(SceneTestEnvironment), name
var sceneName = "not-a-real-scene";
if (manager is SceneManager || manager is AdvancedSceneManager)
LogAssert.Expect(LogType.Error, new Regex("'not-a-real-scene' couldn't be loaded"));
var wait = new WaitTask<SceneResult>(manager.LoadAsync(new SceneParameters(new LoadSceneInfoName(sceneName), false)).AsTask());
var wait = new WaitTask<SceneResult>(manager.LoadAsync(sceneName).AsTask());
Assert.Throws<AggregateException>(() => wait.MoveNext());
}

Expand Down Expand Up @@ -290,39 +290,31 @@ public void UnloadScene_NotLoaded([ValueSource(typeof(SceneTestEnvironment), nam
var sceneName = "not-a-real-scene";
if (manager is not AdvancedSceneManager)
LogAssert.Expect(LogType.Warning, new Regex("Some of the scenes could not be found loaded"));
var wait = new WaitTask<SceneResult>(manager.UnloadAsync(new SceneParameters(new LoadSceneInfoName(sceneName))).AsTask());
var wait = new WaitTask<SceneResult>(manager.UnloadAsync(sceneName).AsTask());
Assert.Throws<AggregateException>(() => wait.MoveNext());
}

[UnityTest]
public IEnumerator Transition([ValueSource(typeof(SceneTestEnvironment), nameof(SceneTestEnvironment.SceneManagers))] ISceneManager manager, [ValueSource(typeof(SceneTestEnvironment), nameof(SceneTestEnvironment.SceneParametersList))] SceneParameters sceneParameters, [ValueSource(nameof(LoadingSceneInfos))] ILoadSceneInfo loadingScene)
{
yield return LoadFirstScene(manager);

int sceneCount = sceneParameters.Length;
yield return Transition_Template(manager, () => manager.TransitionAsync(sceneParameters, loadingScene).AsTask(), sceneParameters.Length, sceneParameters.GetIndexToActivate());
}

var task = manager.TransitionAsync(sceneParameters, loadingScene).AsTask();
public IEnumerator Transition_Template(ISceneManager manager, Func<Task<SceneResult>> transitionTask, int sceneCount, int setIndexActive)
{
yield return LoadFirstScene(manager);

var task = transitionTask();
yield return new WaitTask<SceneResult>(task);

Scene[] loadedScenes = task.Result;
Assert.AreEqual(sceneCount, loadedScenes.Length);
if (setIndexActive >= 0)
Assert.AreEqual(loadedScenes[setIndexActive], manager.GetActiveScene());

yield return new WaitUntil(() => manager.TotalSceneCount == sceneCount);
}

public IEnumerator Transition_Template(ISceneManager manager, ValueTask<SceneResult> transitionTask)
{
yield return LoadFirstScene(manager);

yield return new WaitValueTask<SceneResult>(transitionTask);

Scene loadedScene = transitionTask.Result;
Assert.AreEqual(loadedScene, manager.GetActiveScene());

yield return new WaitUntil(() => manager.TotalSceneCount == 1);
}

[UnityTest]
public IEnumerator Transition_NoSourceScene([ValueSource(typeof(SceneTestEnvironment), nameof(SceneTestEnvironment.SceneManagers))] ISceneManager manager, [ValueSource(typeof(SceneTestEnvironment), nameof(SceneTestEnvironment.SingleLoadSceneInfoList))] ILoadSceneInfo targetScene, [ValueSource(nameof(LoadingSceneInfos))] ILoadSceneInfo loadingScene)
{
Expand Down Expand Up @@ -359,15 +351,15 @@ void sceneUnloaded(Scene scene)
[UnityTest]
public IEnumerator LoadByInfo_UnloadByScene([ValueSource(typeof(SceneTestEnvironment), nameof(SceneTestEnvironment.SceneManagers))] ISceneManager manager, [ValueSource(typeof(SceneTestEnvironment), nameof(SceneTestEnvironment.SingleLoadSceneInfoList))] ILoadSceneInfo sceneInfo)
{
var task = manager.LoadAsync(new SceneParameters(sceneInfo)).AsTask();
var task = manager.LoadAsync(new SceneParameters(sceneInfo));

yield return new WaitTask<SceneResult>(task);
yield return new WaitValueTask<SceneResult>(task);

Scene scene = task.Result;

task = manager.UnloadAsync(new SceneParameters(new LoadSceneInfoScene(scene))).AsTask();
task = manager.UnloadAsync(scene);

yield return new WaitTask<SceneResult>(task);
yield return new WaitValueTask<SceneResult>(task);

Assert.Zero(manager.LoadedSceneCount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static IEnumerator UnloadManagerScenes(ISceneManager sceneManager)
var lastScene = sceneManager.GetLastLoadedScene();
while (sceneManager.LoadedSceneCount > 0 && lastScene.IsValid())
{
yield return new WaitTask<SceneResult>(sceneManager.UnloadAsync(new SceneParameters(new LoadSceneInfoScene(lastScene))).AsTask());
yield return new WaitTask<SceneResult>(sceneManager.UnloadAsync(lastScene).AsTask());
lastScene = sceneManager.GetLastLoadedScene();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
"type": "System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
"key": "AutoGenerateReport",
"value": "{\"m_Value\":false}"
},
{
"type": "System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
"key": "GenerateAdditionalMetrics",
"value": "{\"m_Value\":true}"
},
{
"type": "System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
"key": "GenerateTestReferences",
"value": "{\"m_Value\":true}"
}
]
}
Expand Down

0 comments on commit be61365

Please sign in to comment.