diff --git a/Tests/Runtime/AgentDispatcherTest.cs b/Tests/Runtime/AgentDispatcherTest.cs index 0c39324..cb5f19b 100644 --- a/Tests/Runtime/AgentDispatcherTest.cs +++ b/Tests/Runtime/AgentDispatcherTest.cs @@ -9,6 +9,7 @@ using Cysharp.Threading.Tasks.Triggers; using DeNA.Anjin.Agents; using DeNA.Anjin.Settings; +using DeNA.Anjin.TestDoubles; using DeNA.Anjin.Utilities; using NUnit.Framework; using UnityEngine; @@ -50,11 +51,11 @@ private static AutopilotSettings CreateAutopilotSettings() return testSettings; } - private static DoNothingAgent CreateDoNothingAgent(string name = nameof(DoNothingAgent)) + private static SpyAliveCountAgent CreateSpyAliveCountAgent(string name = nameof(DoNothingAgent)) { - var doNothingAgent = ScriptableObject.CreateInstance(); - doNothingAgent.name = name; - return doNothingAgent; + var agent = ScriptableObject.CreateInstance(); + agent.name = name; + return agent; } private void SetUpDispatcher(AutopilotSettings settings) @@ -81,12 +82,6 @@ private static async UniTask LoadTestSceneAsync(string path, LoadSceneMod return scene; } - private static IEnumerable FindAliveAgentNames() - { - return Object.FindObjectsOfType().Select(x => x.name); - // Note: AsyncDestroyTrigger is a component attached to the GameObject when binding agent by GetCancellationTokenOnDestroy. - } - [Test] public async Task DispatchByScene_DispatchAgentBySceneAgentMaps() { @@ -94,14 +89,15 @@ public async Task DispatchByScene_DispatchAgentBySceneAgentMaps() var settings = CreateAutopilotSettings(); settings.sceneAgentMaps.Add(new SceneAgentMap { - scenePath = TestScenePath, agent = CreateDoNothingAgent(AgentName) + scenePath = TestScenePath, agent = CreateSpyAliveCountAgent(AgentName) }); SetUpDispatcher(settings); await LoadTestSceneAsync(TestScenePath); - var actual = FindAliveAgentNames(); - Assert.That(actual, Is.EquivalentTo(new[] { AgentName })); + var gameObject = GameObject.Find(AgentName); + Assert.That(gameObject, Is.Not.Null); + Assert.That(SpyAliveCountAgent.AliveInstances, Is.EqualTo(1)); } [Test] @@ -109,13 +105,14 @@ public async Task DispatchByScene_DispatchFallbackAgent() { const string AgentName = "Fallback Agent"; var settings = CreateAutopilotSettings(); - settings.fallbackAgent = CreateDoNothingAgent(AgentName); + settings.fallbackAgent = CreateSpyAliveCountAgent(AgentName); SetUpDispatcher(settings); await LoadTestSceneAsync(TestScenePath); - var actual = FindAliveAgentNames(); - Assert.That(actual, Is.EquivalentTo(new[] { AgentName })); + var gameObject = GameObject.Find(AgentName); + Assert.That(gameObject, Is.Not.Null); + Assert.That(SpyAliveCountAgent.AliveInstances, Is.EqualTo(1)); } [Test] @@ -126,9 +123,8 @@ public async Task DispatchByScene_NoSceneAgentMapsAndFallbackAgent_AgentIsNotDis await LoadTestSceneAsync(TestScenePath); - var actual = FindAliveAgentNames(); - Assert.That(actual, Is.Empty); LogAssert.Expect(LogType.Warning, "Agent not found by scene: Buttons"); + Assert.That(SpyAliveCountAgent.AliveInstances, Is.EqualTo(0)); } [Test] @@ -136,13 +132,14 @@ public async Task DispatchByScene_DispatchObserverAgent() { const string AgentName = "Observer Agent"; var settings = CreateAutopilotSettings(); - settings.observerAgent = CreateDoNothingAgent(AgentName); + settings.observerAgent = CreateSpyAliveCountAgent(AgentName); SetUpDispatcher(settings); await LoadTestSceneAsync(TestScenePath); - var actual = FindAliveAgentNames(); - Assert.That(actual, Is.EquivalentTo(new[] { AgentName })); + var gameObject = GameObject.Find(AgentName); + Assert.That(gameObject, Is.Not.Null); + Assert.That(SpyAliveCountAgent.AliveInstances, Is.EqualTo(1)); } [Test] @@ -152,22 +149,19 @@ public async Task DispatchByScene_ReActivateScene_NotCreateDuplicateAgents() var settings = CreateAutopilotSettings(); settings.sceneAgentMaps.Add(new SceneAgentMap { - scenePath = TestScenePath, agent = CreateDoNothingAgent(AgentName) + scenePath = TestScenePath, agent = CreateSpyAliveCountAgent(AgentName) }); SetUpDispatcher(settings); var scene = await LoadTestSceneAsync(TestScenePath); - - var agents = FindAliveAgentNames(); - Assume.That(agents, Is.EquivalentTo(new[] { AgentName })); + Assume.That(SpyAliveCountAgent.AliveInstances, Is.EqualTo(1)); var additiveScene = await LoadTestSceneAsync(TestScenePath2, LoadSceneMode.Additive); SceneManager.SetActiveScene(additiveScene); SceneManager.SetActiveScene(scene); // Re-activate - var actual = FindAliveAgentNames(); - Assert.That(actual, Is.EquivalentTo(new[] { AgentName })); + Assert.That(SpyAliveCountAgent.AliveInstances, Is.EqualTo(1)); // Not create duplicate agents } } } diff --git a/Tests/Runtime/TestDoubles/SpyAliveCountAgent.cs b/Tests/Runtime/TestDoubles/SpyAliveCountAgent.cs new file mode 100644 index 0000000..1f449f0 --- /dev/null +++ b/Tests/Runtime/TestDoubles/SpyAliveCountAgent.cs @@ -0,0 +1,33 @@ +// Copyright (c) 2023-2024 DeNA Co., Ltd. +// This software is released under the MIT License. + +using System.Threading; +using Cysharp.Threading.Tasks; +using DeNA.Anjin.Agents; +using UnityEngine; + +namespace DeNA.Anjin.TestDoubles +{ + /// + /// Spy agent that count the number of alive instances. + /// + [AddComponentMenu("")] + // ReSharper disable once RequiredBaseTypesIsNotInherited + public class SpyAliveCountAgent : AbstractAgent + { + public static int AliveInstances { get; private set; } + + public override async UniTask Run(CancellationToken token) + { + AliveInstances++; + try + { + await UniTask.WaitWhile(() => true, cancellationToken: token); // Wait indefinitely + } + finally + { + AliveInstances--; + } + } + } +} diff --git a/Tests/Runtime/TestDoubles/SpyAliveCountAgent.cs.meta b/Tests/Runtime/TestDoubles/SpyAliveCountAgent.cs.meta new file mode 100644 index 0000000..d169139 --- /dev/null +++ b/Tests/Runtime/TestDoubles/SpyAliveCountAgent.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a45a8161cc09438b9b33c7a670e39335 +timeCreated: 1713821110 \ No newline at end of file