diff --git a/README.md b/README.md index 7122b0c..d9b6d71 100644 --- a/README.md +++ b/README.md @@ -534,8 +534,12 @@ private static async UniTask InitializeOnLaunchAutopilotMethodAsync() } ``` -Note that the autopilot launch process is performed with `RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)` (default for `RuntimeInitializeOnLoadMethod`). -Also, `RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)` implements the initialization process for Configurable Enter Play Mode. +> [!NOTE] +> You can specify callback order with argument. Callbacks with lower values are called before ones with higher values. + +> [!NOTE] +> The autopilot launch process is performed with `RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)` (default for `RuntimeInitializeOnLoadMethod`). +> Also, `RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)` implements the initialization process for Configurable Enter Play Mode. diff --git a/README_ja.md b/README_ja.md index ec63b37..3057db7 100644 --- a/README_ja.md +++ b/README_ja.md @@ -539,8 +539,12 @@ private static async UniTask InitializeOnLaunchAutopilotMethodAsync() } ``` -なお、オートパイロットの起動処理は、`RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)`(`RuntimeInitializeOnLoadMethod`のデフォルト)で実行しています。 -また`RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)`で、Configurable Enter Play Modeのための初期化処理を実装しています。 +> [!NOTE] +> 引数に呼び出し順序を指定できます。値の低いメソッドから順に呼び出されます。 + +> [!NOTE] +> オートパイロットの起動処理は、`RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)`(`RuntimeInitializeOnLoadMethod`のデフォルト)で実行しています。 +> また`RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)`で、Configurable Enter Play Modeのための初期化処理を実装しています。 diff --git a/Runtime/Agents/OneTimeAgent.cs b/Runtime/Agents/OneTimeAgent.cs index 174ad7f..d0d35f5 100644 --- a/Runtime/Agents/OneTimeAgent.cs +++ b/Runtime/Agents/OneTimeAgent.cs @@ -5,9 +5,6 @@ using Cysharp.Threading.Tasks; using DeNA.Anjin.Attributes; using UnityEngine; -#if UNITY_EDITOR -using UnityEditor; -#endif namespace DeNA.Anjin.Agents { @@ -23,7 +20,7 @@ public class OneTimeAgent : AbstractAgent /// public AbstractAgent agent; - [SerializeField] [HideInInspector] internal bool wasExecuted; + public bool WasExecuted { get; internal set; } [InitializeOnLaunchAutopilot] public static void ResetExecutedFlag() @@ -32,28 +29,20 @@ public static void ResetExecutedFlag() var oneTimeAgents = FindObjectsOfType(); foreach (var current in oneTimeAgents) { - current.wasExecuted = false; + current.WasExecuted = false; } -#if UNITY_EDITOR - // Reset asset files (in Editor only) - foreach (var guid in AssetDatabase.FindAssets("t:OneTimeAgent")) - { - var so = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid)); - so.wasExecuted = false; - } -#endif } /// public override async UniTask Run(CancellationToken token) { - if (wasExecuted) + if (WasExecuted) { Logger.Log($"Skip {this.name} since it has already been executed"); return; } - wasExecuted = true; + WasExecuted = true; Logger.Log($"Enter {this.name}.Run()"); diff --git a/Runtime/Attributes/InitializeOnLaunchAutopilotAttribute.cs b/Runtime/Attributes/InitializeOnLaunchAutopilotAttribute.cs index 838fdb5..c09008f 100644 --- a/Runtime/Attributes/InitializeOnLaunchAutopilotAttribute.cs +++ b/Runtime/Attributes/InitializeOnLaunchAutopilotAttribute.cs @@ -1,4 +1,4 @@ -// Copyright (c) 2023 DeNA Co., Ltd. +// Copyright (c) 2023-2024 DeNA Co., Ltd. // This software is released under the MIT License. using System; @@ -8,10 +8,28 @@ namespace DeNA.Anjin.Attributes { /// /// Attach to a static method that performs game title-specific initialization at autopilot startup. - /// Initialization is performed at the timing of `RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)`. + /// Called when `RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)`. /// [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] public class InitializeOnLaunchAutopilotAttribute : PreserveAttribute { + internal const int InitializeLoggerOrder = -20000; + internal const int CovertObsoleteOrder = -10000; + internal const int DefaultOrder = 0; + + /// + /// Relative callback order for callbacks. Callbacks with lower values are called before ones with higher values. + /// + public int CallbackOrder { get; private set; } + + /// + /// Attach to a static method that performs game title-specific initialization at autopilot startup. + /// Called when `RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)`. + /// + /// Relative callback order. Callbacks with lower values are called before ones with higher values. + public InitializeOnLaunchAutopilotAttribute(int callbackOrder = DefaultOrder) + { + this.CallbackOrder = callbackOrder; + } } } diff --git a/Runtime/Autopilot.cs b/Runtime/Autopilot.cs index 53d3645..e41bfc7 100644 --- a/Runtime/Autopilot.cs +++ b/Runtime/Autopilot.cs @@ -12,13 +12,6 @@ using UnityEngine; using UnityEngine.SceneManagement; using Assert = UnityEngine.Assertions.Assert; -#if UNITY_INCLUDE_TESTS -using NUnit.Framework; -using AssertionException = NUnit.Framework.AssertionException; -#endif -#if UNITY_EDITOR -using UnityEditor; -#endif namespace DeNA.Anjin { @@ -163,37 +156,10 @@ public async UniTask TerminateAsync(ExitCode exitCode, string logString = null, JUnitReporter.Output(_state.settings.junitReportPath, (int)exitCode, logString, stackTrace, time); } - Destroy(this.gameObject); + DestroyImmediate(this.gameObject); - _logger.Log("Terminate autopilot"); - _state.settings = null; - _state.exitCode = exitCode; - - if (_state.launchFrom == LaunchType.PlayMode) // Note: Editor play mode, Play mode tests, and Player build - { -#if UNITY_INCLUDE_TESTS - // Play mode tests - if (TestContext.CurrentContext != null && exitCode != ExitCode.Normally) - { - throw new AssertionException($"Autopilot failed with exit code {exitCode}"); - } -#endif - return; // Only terminate autopilot run if starting from play mode. - } - -#if UNITY_EDITOR - // Terminate when launch from edit mode (including launch from commandline) - _logger.Log("Stop playing by autopilot"); - // XXX: Avoid a problem that Editor stay playing despite isPlaying get assigned false. - // SEE: https://github.com/DeNA/Anjin/issues/20 - await UniTask.NextFrame(token); - EditorApplication.isPlaying = false; - // Note: If launched from the command line, `DeNA.Anjin.Editor.Commandline.OnChangePlayModeState()` will be called, and the Unity editor will be terminated. -#else - // Player build launch from commandline - _logger.Log($"Exit Unity-player by autopilot, exit code={exitCode}"); - Application.Quit((int)exitCode); -#endif + _logger.Log("Terminate Autopilot"); + await Launcher.TeardownLaunchAutopilotAsync(_state, _logger, exitCode, "Autopilot", token); } /// diff --git a/Runtime/ExitCode.cs b/Runtime/ExitCode.cs index e9ea4a8..dc4b3f6 100644 --- a/Runtime/ExitCode.cs +++ b/Runtime/ExitCode.cs @@ -4,23 +4,28 @@ namespace DeNA.Anjin { /// - /// Exit code for autopilot running + /// Autopilot exit code. /// public enum ExitCode { /// - /// Normally exit + /// Normally exit. /// Normally = 0, /// - /// Exit by un catch Exceptions + /// Uncaught exceptions. /// UnCatchExceptions = 1, /// - /// Exit by fault in log message + /// Autopilot running failed. /// - AutopilotFailed = 2 + AutopilotFailed = 2, + + /// + /// Autopilot launching failed. + /// + AutopilotLaunchingFailed } } diff --git a/Runtime/Launcher.cs b/Runtime/Launcher.cs index 39b24f2..88eee4a 100644 --- a/Runtime/Launcher.cs +++ b/Runtime/Launcher.cs @@ -2,15 +2,22 @@ // This software is released under the MIT License. using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; +using System.Threading; using System.Threading.Tasks; using Cysharp.Threading.Tasks; using DeNA.Anjin.Attributes; using DeNA.Anjin.Settings; using DeNA.Anjin.Utilities; +using NUnit.Framework; using UnityEngine; using Object = UnityEngine.Object; +#if UNITY_INCLUDE_TESTS +using NUnit.Framework; +using AssertionException = NUnit.Framework.AssertionException; +#endif #if UNITY_EDITOR using UnityEditor; #endif @@ -23,11 +30,11 @@ namespace DeNA.Anjin public static class Launcher { /// - /// Run autopilot from Play Mode test. - /// If an error is detected in running, it will be output to `LogError` and the test will fail. + /// Launch autopilot from Play Mode tests or runtime (e.g., debug menu). /// /// Autopilot settings - public static async UniTask LaunchAutopilotAsync(AutopilotSettings settings) + /// Task cancellation token + public static async UniTask LaunchAutopilotAsync(AutopilotSettings settings, CancellationToken token = default) { #if UNITY_EDITOR if (!EditorApplication.isPlaying) @@ -45,22 +52,24 @@ public static async UniTask LaunchAutopilotAsync(AutopilotSettings settings) state.settings = settings; LaunchAutopilot().Forget(); - await UniTask.WaitUntil(() => !state.IsRunning); + await UniTask.WaitUntil(() => !state.IsRunning, cancellationToken: token); } /// - /// Run autopilot from Play Mode test. - /// If an error is detected in running, it will be output to `LogError` and the test will fail. + /// Launch autopilot from Play Mode tests or runtime (e.g., debug menu). /// - /// Asset file path for autopilot settings. When running the player, it reads from Resources - public static async UniTask LaunchAutopilotAsync(string autopilotSettingsPath) + /// Asset file path for autopilot settings. When running the player, it reads from Resources + /// Task cancellation token + public static async UniTask LaunchAutopilotAsync(string settingsPath, CancellationToken token = default) { #if UNITY_EDITOR - var settings = AssetDatabase.LoadAssetAtPath(autopilotSettingsPath); + var settings = AssetDatabase.LoadAssetAtPath(settingsPath); #else - var settings = Resources.Load(autopilotSettingsPath); + var settings = Resources.Load(settingsPath); #endif - await LaunchAutopilotAsync(settings); + Assert.IsNotNull(settings, $"Autopilot settings not found: {settingsPath}"); + + await LaunchAutopilotAsync(settings, token); } /// @@ -69,7 +78,11 @@ public static async UniTask LaunchAutopilotAsync(string autopilotSettingsPath) [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] private static void LaunchAutopilotOnPlayerFromCommandline() { -#if !UNITY_EDITOR + if (Application.isEditor) + { + return; + } + var args = new Arguments(); if (!args.LaunchAutopilotSettings.IsCaptured()) { @@ -85,11 +98,10 @@ private static void LaunchAutopilotOnPlayerFromCommandline() state.launchFrom = LaunchType.Commandline; state.settings = settings; -#endif } /// - /// Run autopilot + /// Launch autopilot (internal). /// [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)] // ReSharper disable once Unity.IncorrectMethodSignature @@ -101,34 +113,146 @@ internal static async UniTaskVoid LaunchAutopilot() return; // Normally play mode (not run autopilot) } - ScreenshotStore.CleanDirectories(); + ScreenshotStore.CleanDirectories(); // Note: Scheduled to change soon. - await CallAttachedInitializeOnLaunchAutopilotAttributeMethods(); + try + { + await CallInitializeOnLaunchAutopilotMethods(); + } + catch (Exception e) + { + Debug.LogException(e); + + var logger = Debug.unityLogger; // Note: Logger is not initialized yet. + const ExitCode ExitCode = ExitCode.AutopilotLaunchingFailed; + const string Caller = "Autopilot launcher"; + Debug.Log("Cancel launching Autopilot"); + TeardownLaunchAutopilotAsync(state, logger, ExitCode, Caller).Forget(); + return; + } var autopilot = new GameObject(nameof(Autopilot)).AddComponent(); Object.DontDestroyOnLoad(autopilot); } - private static async UniTask CallAttachedInitializeOnLaunchAutopilotAttributeMethods() + private static async UniTask CallInitializeOnLaunchAutopilotMethods() + { + var orderedMethodMap = GetOrderedInitializeOnLaunchAutopilotMethodsMap(); + // key: order, value: methods attaching InitializeOnLaunchAutopilotAttribute. + + var tasks = new List(); + var uniTasks = new List(); + + foreach (var order in orderedMethodMap.Keys.OrderBy(x => x)) + { + tasks.Clear(); + uniTasks.Clear(); + + foreach (var method in orderedMethodMap[order]) + { + try + { + switch (method.ReturnType.Name) + { + case nameof(Task): + tasks.Add((Task)method.Invoke(null, null)); + break; + case nameof(UniTask): + uniTasks.Add((UniTask)method.Invoke(null, null)); + break; + default: + method.Invoke(null, null); // static method only + break; + } + } + catch (Exception) + { + Debug.LogError($"Invoke method failed: {method.Name}"); // Note: Logger is not initialized yet. + throw; + } + } + + await Task.WhenAll(tasks); + await UniTask.WhenAll(uniTasks); + } + } + + private static Dictionary> GetOrderedInitializeOnLaunchAutopilotMethodsMap() + { + var orderedMethodMap = new Dictionary>(); + foreach (var (order, method) in GetInitializeOnLaunchAutopilotMethods()) + { + if (!orderedMethodMap.TryGetValue(order, out var methods)) + { + methods = new List(); + orderedMethodMap[order] = methods; + } + + methods.Add(method); + } + + return orderedMethodMap; + } + + private static IEnumerable<(int, MethodInfo)> GetInitializeOnLaunchAutopilotMethods() { +#if UNITY_EDITOR && UNITY_2021_3_OR_NEWER + return TypeCache.GetMethodsWithAttribute() + .Select(x => (x.GetCustomAttribute().CallbackOrder, x)) + .OrderBy(x => x.Item1) + .Select(x => (x.Item1, x.Item2)); +#else const BindingFlags MethodBindingFlags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; - foreach (var methodInfo in AppDomain.CurrentDomain.GetAssemblies() + foreach (var method in AppDomain.CurrentDomain.GetAssemblies() .SelectMany(x => x.GetTypes()) - .SelectMany(x => x.GetMethods(MethodBindingFlags)) - .Where(x => x.GetCustomAttributes(typeof(InitializeOnLaunchAutopilotAttribute), false).Any())) + .SelectMany(x => x.GetMethods(MethodBindingFlags))) + { + var attribute = method.GetCustomAttributes(false) + .FirstOrDefault(); + if (attribute != null) + { + yield return (attribute.CallbackOrder, method); + } + } +#endif + } + + internal static async UniTask TeardownLaunchAutopilotAsync(AutopilotState state, ILogger logger, + ExitCode exitCode, string caller, CancellationToken token = default) + { + state.settings = null; + state.exitCode = exitCode; + + if (state.launchFrom == LaunchType.PlayMode) // Note: Editor play mode, Play mode tests, and Player build { - switch (methodInfo.ReturnType.Name) +#if UNITY_INCLUDE_TESTS + // Play mode tests + if (TestContext.CurrentContext != null && exitCode != ExitCode.Normally) { - case nameof(Task): - await (Task)methodInfo.Invoke(null, null); - break; - case nameof(UniTask): - await (UniTask)methodInfo.Invoke(null, null); - break; - default: - methodInfo.Invoke(null, null); // static method only - break; + throw new AssertionException($"{caller} failed with exit code {exitCode}"); } +#endif + return; // Only terminate autopilot run if starting from play mode. + } + + if (Application.isEditor) + { + // Terminate when launch from edit mode (including launch from commandline) + logger.Log($"Stop playing by {caller}"); + + // XXX: Avoid a problem that Editor stay playing despite isPlaying get assigned false. + // SEE: https://github.com/DeNA/Anjin/issues/20 + await UniTask.NextFrame(token); +#if UNITY_EDITOR + EditorApplication.isPlaying = false; + // Note: If launched from the command line, `DeNA.Anjin.Editor.Commandline.OnChangePlayModeState()` will be called, and the Unity editor will be terminated. +#endif + } + else + { + // Player build launch from commandline + logger.Log($"Exit Unity-player by {caller}, exit code={exitCode}"); + Application.Quit((int)exitCode); } } } diff --git a/Runtime/Loggers/ConsoleLoggerAsset.cs b/Runtime/Loggers/ConsoleLoggerAsset.cs index f4a824e..f9ec956 100644 --- a/Runtime/Loggers/ConsoleLoggerAsset.cs +++ b/Runtime/Loggers/ConsoleLoggerAsset.cs @@ -3,9 +3,6 @@ using DeNA.Anjin.Attributes; using UnityEngine; -#if UNITY_EDITOR -using UnityEditor; -#endif namespace DeNA.Anjin.Loggers { @@ -44,7 +41,7 @@ public override void Dispose() // Nothing to dispose. } - [InitializeOnLaunchAutopilot] + [InitializeOnLaunchAutopilot(InitializeOnLaunchAutopilotAttribute.InitializeLoggerOrder)] public static void ResetLoggers() { // Reset runtime instances @@ -53,14 +50,6 @@ public static void ResetLoggers() { current._logger = null; } -#if UNITY_EDITOR - // Reset asset files (in Editor only) - foreach (var guid in AssetDatabase.FindAssets($"t:{nameof(ConsoleLoggerAsset)}")) - { - var so = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid)); - so._logger = null; - } -#endif } } } diff --git a/Runtime/Loggers/FileLoggerAsset.cs b/Runtime/Loggers/FileLoggerAsset.cs index 7675865..219a40a 100644 --- a/Runtime/Loggers/FileLoggerAsset.cs +++ b/Runtime/Loggers/FileLoggerAsset.cs @@ -8,9 +8,6 @@ using DeNA.Anjin.Utilities; using UnityEngine; using Object = UnityEngine.Object; -#if UNITY_EDITOR -using UnityEditor; -#endif namespace DeNA.Anjin.Loggers { @@ -159,7 +156,7 @@ public void Dispose() } } - [InitializeOnLaunchAutopilot] + [InitializeOnLaunchAutopilot(InitializeOnLaunchAutopilotAttribute.InitializeLoggerOrder)] public static void ResetLoggers() { // Reset runtime instances @@ -170,16 +167,6 @@ public static void ResetLoggers() current._handler = null; current._logger = null; } -#if UNITY_EDITOR - // Reset asset files (in Editor only) - foreach (var guid in AssetDatabase.FindAssets($"t:{nameof(FileLoggerAsset)}")) - { - var so = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid)); - so._handler?.Dispose(); - so._handler = null; - so._logger = null; - } -#endif } } } diff --git a/Tests/Runtime/Agents/OneTimeAgentTest.cs b/Tests/Runtime/Agents/OneTimeAgentTest.cs index c83338e..7bb83d7 100644 --- a/Tests/Runtime/Agents/OneTimeAgentTest.cs +++ b/Tests/Runtime/Agents/OneTimeAgentTest.cs @@ -70,7 +70,7 @@ public async Task Run_markWasExecuted() await UniTask.Delay(500); // Consider overhead Assert.That(task.Status, Is.EqualTo(UniTaskStatus.Succeeded)); - Assert.That(agent.wasExecuted, Is.True); + Assert.That(agent.WasExecuted, Is.True); } LogAssert.Expect(LogType.Log, $"Enter {agent.name}.Run()"); @@ -87,7 +87,7 @@ public async Task Run_wasExecuted_notExecuteChildAgent() agent.Random = new RandomFactory(0).CreateRandom(); agent.name = nameof(Run_cancelTask_stopAgent); agent.agent = childAgent; - agent.wasExecuted = true; + agent.WasExecuted = true; using (var cancellationTokenSource = new CancellationTokenSource()) { @@ -132,14 +132,14 @@ public async Task Run_setLoggerAndRandomInstanceToChildAgent() public async Task ResetExecutedFlagWhenLaunchAutopilot() { var sut = ScriptableObject.CreateInstance(); - sut.wasExecuted = true; + sut.WasExecuted = true; var settings = ScriptableObject.CreateInstance(); settings.lifespanSec = 1; await Launcher.LaunchAutopilotAsync(settings); sut = ScriptableObject.CreateInstance(); // Reload because domain reloaded - Assert.That(sut.wasExecuted, Is.False); // was reset + Assert.That(sut.WasExecuted, Is.False); // was reset } } } diff --git a/Tests/Runtime/LauncherTest.cs b/Tests/Runtime/LauncherTest.cs index ac7858c..0bdfdd3 100644 --- a/Tests/Runtime/LauncherTest.cs +++ b/Tests/Runtime/LauncherTest.cs @@ -76,7 +76,7 @@ public async Task LaunchAutopilotAsync_WithAssetFile_RunAutopilot() } [Test] - public async Task LaunchAutopilotAsync_InitializeOnLaunchAutopilotAttribute_Called() + public async Task LaunchAutopilotAsync_InitializeOnLaunchAutopilotMethodWasCalled() { SpyInitializeOnLaunchAutopilot.Reset(); @@ -86,11 +86,11 @@ public async Task LaunchAutopilotAsync_InitializeOnLaunchAutopilotAttribute_Call settings.lifespanSec = 1; await Launcher.LaunchAutopilotAsync(settings); - Assert.That(SpyInitializeOnLaunchAutopilot.IsCallInitializeOnLaunchAutopilotMethod, Is.True); + Assert.That(SpyInitializeOnLaunchAutopilot.WasCalled, Is.True); } [Test] - public async Task LaunchAutopilotAsync_InitializeOnLaunchAutopilotAttributeAttachToNonPublicMethod_Called() + public async Task LaunchAutopilotAsync_InitializeOnLaunchAutopilotNonPublicMethodWasCalled() { SpyInitializeOnLaunchAutopilot.Reset(); @@ -99,11 +99,11 @@ public async Task LaunchAutopilotAsync_InitializeOnLaunchAutopilotAttributeAttac settings.lifespanSec = 1; await Launcher.LaunchAutopilotAsync(settings); - Assert.That(SpyInitializeOnLaunchAutopilot.IsCallInitializeOnLaunchAutopilotMethodNonPublic, Is.True); + Assert.That(SpyInitializeOnLaunchAutopilot.WasCalledNonPublicMethod, Is.True); } [Test] - public async Task LaunchAutopilotAsync_InitializeOnLaunchAutopilotAttributeAttachToTaskAsyncMethod_Called() + public async Task LaunchAutopilotAsync_InitializeOnLaunchAutopilotTaskAsyncWasCalled() { SpyInitializeOnLaunchAutopilot.Reset(); @@ -113,11 +113,11 @@ public async Task LaunchAutopilotAsync_InitializeOnLaunchAutopilotAttributeAttac settings.lifespanSec = 1; await Launcher.LaunchAutopilotAsync(settings); - Assert.That(SpyInitializeOnLaunchAutopilot.IsCallInitializeOnLaunchAutopilotMethodTaskAsync, Is.True); + Assert.That(SpyInitializeOnLaunchAutopilot.WasCalledTaskAsync, Is.True); } [Test] - public async Task LaunchAutopilotAsync_InitializeOnLaunchAutopilotAttributeAttachToUniTaskAsyncMethod_Called() + public async Task LaunchAutopilotAsync_InitializeOnLaunchAutopilotUniTaskAsyncWasCalled() { SpyInitializeOnLaunchAutopilot.Reset(); @@ -127,7 +127,7 @@ public async Task LaunchAutopilotAsync_InitializeOnLaunchAutopilotAttributeAttac settings.lifespanSec = 1; await Launcher.LaunchAutopilotAsync(settings); - Assert.That(SpyInitializeOnLaunchAutopilot.IsCallInitializeOnLaunchAutopilotMethodUniTaskAsync, Is.True); + Assert.That(SpyInitializeOnLaunchAutopilot.WasCalledUniTaskAsync, Is.True); } } } diff --git a/Tests/Runtime/Loggers/ConsoleLoggerAssetTest.cs b/Tests/Runtime/Loggers/ConsoleLoggerAssetTest.cs index 008771c..d582892 100644 --- a/Tests/Runtime/Loggers/ConsoleLoggerAssetTest.cs +++ b/Tests/Runtime/Loggers/ConsoleLoggerAssetTest.cs @@ -42,7 +42,7 @@ public void FilterLogTypeSpecified( } [Test] - public void ResetLoggers_InstanceOnly_ResetLoggerAsset() + public void ResetLoggers_ResetLoggerAsset() { var sut = ScriptableObject.CreateInstance(); sut.filterLogType = LogType.Warning; @@ -57,25 +57,5 @@ public void ResetLoggers_InstanceOnly_ResetLoggerAsset() sut.Dispose(); Assert.That(sut.Logger.filterLogType, Is.EqualTo(LogType.Error)); } - - [Test] - [UnityPlatform(RuntimePlatform.OSXEditor, RuntimePlatform.WindowsEditor, RuntimePlatform.LinuxEditor)] - public void ResetLoggers_FromAssetFile_ResetLoggerAsset() - { -#if UNITY_EDITOR - var sut = AssetDatabase.LoadAssetAtPath( - "Packages/com.dena.anjin/Tests/TestAssets/ConsoleLogger.asset"); - sut.Logger.Log("Before reset"); - sut.Dispose(); - Assume.That(sut.Logger.filterLogType, Is.EqualTo(LogType.Warning)); - - ConsoleLoggerAsset.ResetLoggers(); // Called when on launch autopilot - - sut.filterLogType = LogType.Error; - sut.Logger.Log("After reset"); - sut.Dispose(); - Assert.That(sut.Logger.filterLogType, Is.EqualTo(LogType.Error)); -#endif - } } } diff --git a/Tests/Runtime/Loggers/FileLoggerAssetTest.cs b/Tests/Runtime/Loggers/FileLoggerAssetTest.cs index 01540e9..f6dcc2c 100644 --- a/Tests/Runtime/Loggers/FileLoggerAssetTest.cs +++ b/Tests/Runtime/Loggers/FileLoggerAssetTest.cs @@ -244,7 +244,7 @@ public async Task LogException_WithTimestamp_WriteTimestamp() } [Test] - public async Task ResetLoggers_InstanceOnly_ResetLoggerAsset() + public async Task ResetLoggers_ResetLoggerAsset() { var path = GetOutputPath(); var sut = ScriptableObject.CreateInstance(); @@ -262,29 +262,5 @@ public async Task ResetLoggers_InstanceOnly_ResetLoggerAsset() await Task.Yield(); Assert.That(path, Does.Exist); } - - [Test] - [UnityPlatform(RuntimePlatform.OSXEditor, RuntimePlatform.WindowsEditor, RuntimePlatform.LinuxEditor)] - public async Task ResetLoggers_FromAssetFile_ResetLoggerAsset() - { - var path = GetOutputPath(); -#if UNITY_EDITOR - var sut = AssetDatabase.LoadAssetAtPath( - "Packages/com.dena.anjin/Tests/TestAssets/FileLogger.asset"); - sut.outputPath = path; - sut.Logger.Log("Before reset"); - sut.Dispose(); - await Task.Yield(); - Assume.That(path, Does.Exist); - - File.Delete(path); - FileLoggerAsset.ResetLoggers(); // Called when on launch autopilot - - sut.Logger.Log("After reset"); - sut.Dispose(); - await Task.Yield(); - Assert.That(path, Does.Exist); -#endif - } } } diff --git a/Tests/Runtime/TestDoubles/SpyInitializeOnLaunchAutopilot.cs b/Tests/Runtime/TestDoubles/SpyInitializeOnLaunchAutopilot.cs index f107839..5cbfeef 100644 --- a/Tests/Runtime/TestDoubles/SpyInitializeOnLaunchAutopilot.cs +++ b/Tests/Runtime/TestDoubles/SpyInitializeOnLaunchAutopilot.cs @@ -9,43 +9,43 @@ namespace DeNA.Anjin.TestDoubles { public static class SpyInitializeOnLaunchAutopilot { - public static bool IsCallInitializeOnLaunchAutopilotMethod { get; private set; } - public static bool IsCallInitializeOnLaunchAutopilotMethodNonPublic { get; private set; } - public static bool IsCallInitializeOnLaunchAutopilotMethodTaskAsync { get; private set; } - public static bool IsCallInitializeOnLaunchAutopilotMethodUniTaskAsync { get; private set; } + public static bool WasCalled { get; private set; } + public static bool WasCalledNonPublicMethod { get; private set; } + public static bool WasCalledTaskAsync { get; private set; } + public static bool WasCalledUniTaskAsync { get; private set; } public static void Reset() { - IsCallInitializeOnLaunchAutopilotMethod = false; - IsCallInitializeOnLaunchAutopilotMethodNonPublic = false; - IsCallInitializeOnLaunchAutopilotMethodTaskAsync = false; - IsCallInitializeOnLaunchAutopilotMethodUniTaskAsync = false; + WasCalled = false; + WasCalledNonPublicMethod = false; + WasCalledTaskAsync = false; + WasCalledUniTaskAsync = false; } [InitializeOnLaunchAutopilot] public static void InitializeOnLaunchAutopilotMethod() { - IsCallInitializeOnLaunchAutopilotMethod = true; + WasCalled = true; } [InitializeOnLaunchAutopilot] - private static void InitializeOnLaunchAutopilotMethodNonPublic() + private static void InitializeOnLaunchAutopilotNonPublicMethod() { - IsCallInitializeOnLaunchAutopilotMethodNonPublic = true; + WasCalledNonPublicMethod = true; } [InitializeOnLaunchAutopilot] - public static async Task InitializeOnLaunchAutopilotMethodTaskAsync() + public static async Task InitializeOnLaunchAutopilotTaskAsync() { await Task.Delay(0); - IsCallInitializeOnLaunchAutopilotMethodTaskAsync = true; + WasCalledTaskAsync = true; } [InitializeOnLaunchAutopilot] - public static async UniTask InitializeOnLaunchAutopilotMethodUniTaskAsync() + public static async UniTask InitializeOnLaunchAutopilotUniTaskAsync() { await UniTask.Delay(0); - IsCallInitializeOnLaunchAutopilotMethodUniTaskAsync = true; + WasCalledUniTaskAsync = true; } } }