diff --git a/RuntimeInternals/ScreenshotHelper.cs b/RuntimeInternals/ScreenshotHelper.cs index e6a2459..7f18772 100644 --- a/RuntimeInternals/ScreenshotHelper.cs +++ b/RuntimeInternals/ScreenshotHelper.cs @@ -19,7 +19,7 @@ public static class ScreenshotHelper { private static string DefaultDirectoryPath => CommandLineArgs.GetScreenshotDirectory(); - private static string DefaultFilename(string callerMemberName) + internal static string DefaultFilename(string callerMemberName) { #if UNITY_INCLUDE_TESTS if (TestContext.CurrentTestExecutionContext != null) @@ -57,11 +57,13 @@ private static string DefaultFilename(string callerMemberName) /// Using caller method name when run in runtime context. /// The factor to increase resolution with. /// The eye texture to capture when stereo rendering is enabled. + /// Output filename to Debug.Log public static IEnumerator TakeScreenshot( string directory = null, string filename = null, int superSize = 1, ScreenCapture.StereoScreenCaptureMode stereoCaptureMode = ScreenCapture.StereoScreenCaptureMode.LeftEye, + bool logFilepath = true, // ReSharper disable once InvalidXmlDocComment [CallerMemberName] string callerMemberName = null) { @@ -101,7 +103,11 @@ private static string DefaultFilename(string callerMemberName) var path = Path.Combine(directory, filename); var bytes = texture.EncodeToPNG(); File.WriteAllBytes(path, bytes); - Debug.Log($"Save screenshot to {path}"); + + if (logFilepath) + { + Debug.Log($"Save screenshot to {path}"); + } } } } diff --git a/Tests/RuntimeInternals/ScreenshotHelperTest.cs b/Tests/RuntimeInternals/ScreenshotHelperTest.cs index 0797384..a00762c 100644 --- a/Tests/RuntimeInternals/ScreenshotHelperTest.cs +++ b/Tests/RuntimeInternals/ScreenshotHelperTest.cs @@ -6,29 +6,25 @@ using System.Threading.Tasks; using Cysharp.Threading.Tasks; using NUnit.Framework; +using TestHelper.Attributes; using UnityEngine; -using UnityEngine.SceneManagement; using UnityEngine.TestTools; using UnityEngine.UI; namespace TestHelper.RuntimeInternals { [TestFixture] - [PrebuildSetup(typeof(ScreenshotHelperTestSceneBuilder))] - [PostBuildCleanup(typeof(ScreenshotHelperTestSceneBuilder))] public class ScreenshotHelperTest { - internal const string TestScene = "Packages/com.nowsprinting.test-helper/Tests/Scenes/ScreenshotTest.unity"; + private const string TestScene = "Packages/com.nowsprinting.test-helper/Tests/Scenes/ScreenshotTest.unity"; private const int FileSizeThreshold = 5441; // VGA size solid color file size private readonly string _defaultOutputDirectory = CommandLineArgs.GetScreenshotDirectory(); private Text _text; - [UnitySetUp] - public IEnumerator SetUp() + [SetUp] + public void SetUp() { - yield return SceneManager.LoadSceneAsync(TestScene); - var textObject = GameObject.Find("Text"); Assume.That(textObject, Is.Not.Null); @@ -37,6 +33,7 @@ public IEnumerator SetUp() } [UnityTest] + [LoadScene(TestScene)] public IEnumerator TakeScreenshot_SaveToDefaultPath() { var path = Path.Combine(_defaultOutputDirectory, $"{nameof(TakeScreenshot_SaveToDefaultPath)}.png"); @@ -53,6 +50,7 @@ public IEnumerator TakeScreenshot_SaveToDefaultPath() } [UnityTest] + [LoadScene(TestScene)] public IEnumerator TakeScreenshot_Multiple_SaveToEachSpecifyPaths() { const string Filename1 = "TakeScreenshot_Multiple_SpecifyFilename1.png"; @@ -85,6 +83,7 @@ public IEnumerator TakeScreenshot_Multiple_SaveToEachSpecifyPaths() } [UnityTest] + [LoadScene(TestScene)] public IEnumerator TakeScreenshot_SpecifySuperSizeAndStereoCaptureMode_NotWork() { yield return ScreenshotHelper.TakeScreenshot( @@ -94,6 +93,7 @@ public IEnumerator TakeScreenshot_SpecifySuperSizeAndStereoCaptureMode_NotWork() } [Test] + [LoadScene(TestScene)] public async Task TakeScreenshot_FromAsyncTest() { var path = Path.Combine(_defaultOutputDirectory, $"{nameof(TakeScreenshot_FromAsyncTest)}.png"); @@ -116,5 +116,26 @@ public async Task TakeScreenshot_FromAsyncTest() private class CoroutineRunner : MonoBehaviour { } + + [UnityTest] + [LoadScene(TestScene)] + public IEnumerator TakeScreenshot_WithoutLogFilepath_SuppressLogging() + { + yield return ScreenshotHelper.TakeScreenshot(logFilepath: false); + + LogAssert.NoUnexpectedReceived(); // No output to Debug.Log + } + + [TestFixture] + public class Internal + { + [TestCase(0, "s")] + public void DefaultFilename_Parameterized(int i, string s) + { + var actual = ScreenshotHelper.DefaultFilename(null); + + Assert.That(actual, Is.EqualTo("DefaultFilename_Parameterized_0-s_")); + } + } } } diff --git a/Tests/RuntimeInternals/ScreenshotHelperTestSceneBuilder.cs b/Tests/RuntimeInternals/ScreenshotHelperTestSceneBuilder.cs deleted file mode 100644 index 522775a..0000000 --- a/Tests/RuntimeInternals/ScreenshotHelperTestSceneBuilder.cs +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) 2023 Koji Hasegawa. -// This software is released under the MIT License. - -using System.Linq; -using UnityEngine.TestTools; -#if UNITY_EDITOR -using UnityEditor; -#endif - -namespace TestHelper.RuntimeInternals -{ - /// - /// Temporary add test scene to "Scenes in Build" during the build for tests. - /// What not use LoadSceneAttribute? because not depend on the TestHelper assembly. - /// - internal class ScreenshotHelperTestSceneBuilder : IPrebuildSetup, IPostBuildCleanup - { -#if UNITY_EDITOR - private EditorBuildSettingsScene[] _scenesInBuild; -#endif - - /// - public void Setup() - { -#if UNITY_EDITOR - _scenesInBuild = EditorBuildSettings.scenes; - - var scenesList = _scenesInBuild.ToList(); - scenesList.Add(new EditorBuildSettingsScene( - ScreenshotHelperTest.TestScene, - true)); - - EditorBuildSettings.scenes = scenesList.ToArray(); -#endif - } - - /// - public void Cleanup() - { -#if UNITY_EDITOR - EditorBuildSettings.scenes = _scenesInBuild; -#endif - } - } -} diff --git a/Tests/RuntimeInternals/ScreenshotHelperTestSceneBuilder.cs.meta b/Tests/RuntimeInternals/ScreenshotHelperTestSceneBuilder.cs.meta deleted file mode 100644 index f8fb804..0000000 --- a/Tests/RuntimeInternals/ScreenshotHelperTestSceneBuilder.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 9fdff0e620164da4921dd1775a2b8b21 -timeCreated: 1698852891 \ No newline at end of file diff --git a/Tests/RuntimeInternals/TestHelper.RuntimeInternals.Tests.asmdef b/Tests/RuntimeInternals/TestHelper.RuntimeInternals.Tests.asmdef index c095b95..b5c6fda 100644 --- a/Tests/RuntimeInternals/TestHelper.RuntimeInternals.Tests.asmdef +++ b/Tests/RuntimeInternals/TestHelper.RuntimeInternals.Tests.asmdef @@ -5,6 +5,7 @@ "UnityEngine.TestRunner", "UnityEditor.TestRunner", "TestHelper.RuntimeInternals", + "TestHelper", "UniTask" ], "includePlatforms": [],