From 3059f4f025cf16022a7dfd17c154bcc0fbfafe74 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Fri, 3 Nov 2023 14:39:13 +0900 Subject: [PATCH 1/3] Rename TakeScreenshot tests --- .../Attributes/TakeScreenshotAttributeTest.cs | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Tests/Runtime/Attributes/TakeScreenshotAttributeTest.cs b/Tests/Runtime/Attributes/TakeScreenshotAttributeTest.cs index f4cede8..e8ff775 100644 --- a/Tests/Runtime/Attributes/TakeScreenshotAttributeTest.cs +++ b/Tests/Runtime/Attributes/TakeScreenshotAttributeTest.cs @@ -35,11 +35,11 @@ public void SetUp() [Test, Order(0)] [LoadScene(TestScene)] [TakeScreenshot] - public void Attach_TakeScreenshotAndSaveToDefaultPath() + public void Attach_SaveScreenshotToDefaultPath() { var path = Path.Combine( _defaultOutputDirectory, - $"{nameof(Attach_TakeScreenshotAndSaveToDefaultPath)}.png"); + $"{nameof(Attach_SaveScreenshotToDefaultPath)}.png"); if (File.Exists(path)) { File.Delete(path); @@ -51,22 +51,22 @@ public void Attach_TakeScreenshotAndSaveToDefaultPath() } [Test, Order(1)] - public void Attach_TakeScreenshotAndSaveToDefaultPath_AfterRunningTest_ExistFile() + public void Attach_SaveScreenshotToDefaultPath_ExistFile() { var path = Path.Combine( _defaultOutputDirectory, - $"{nameof(Attach_TakeScreenshotAndSaveToDefaultPath)}.png"); + $"{nameof(Attach_SaveScreenshotToDefaultPath)}.png"); Assert.That(path, Does.Exist); } [Test, Order(0)] [LoadScene(TestScene)] [TakeScreenshot] - public async Task AttachToAsyncTest_TakeScreenshotAndSaveToDefaultPath() + public async Task AttachToAsyncTest_SaveScreenshotToDefaultPath() { var path = Path.Combine( _defaultOutputDirectory, - $"{nameof(AttachToAsyncTest_TakeScreenshotAndSaveToDefaultPath)}.png"); + $"{nameof(AttachToAsyncTest_SaveScreenshotToDefaultPath)}.png"); if (File.Exists(path)) { File.Delete(path); @@ -79,22 +79,22 @@ public async Task AttachToAsyncTest_TakeScreenshotAndSaveToDefaultPath() } [Test, Order(1)] - public void AttachToAsyncTest_TakeScreenshotAndSaveToDefaultPath_AfterRunningTest_ExistFile() + public void AttachToAsyncTest_SaveScreenshotToDefaultPath_ExistFile() { var path = Path.Combine( _defaultOutputDirectory, - $"{nameof(AttachToAsyncTest_TakeScreenshotAndSaveToDefaultPath)}.png"); + $"{nameof(AttachToAsyncTest_SaveScreenshotToDefaultPath)}.png"); Assert.That(path, Does.Exist); } [UnityTest, Order(0)] [LoadScene(TestScene)] [TakeScreenshot] - public IEnumerator AttachToUnityTest_TakeScreenshotAndSaveToDefaultPath() + public IEnumerator AttachToUnityTest_SaveScreenshotToDefaultPath() { var path = Path.Combine( _defaultOutputDirectory, - $"{nameof(AttachToUnityTest_TakeScreenshotAndSaveToDefaultPath)}.png"); + $"{nameof(AttachToUnityTest_SaveScreenshotToDefaultPath)}.png"); if (File.Exists(path)) { File.Delete(path); @@ -107,21 +107,21 @@ public IEnumerator AttachToUnityTest_TakeScreenshotAndSaveToDefaultPath() } [Test, Order(1)] - public void AttachToUnityTest_TakeScreenshotAndSaveToDefaultPath_AfterRunningTest_ExistFile() + public void AttachToUnityTest_SaveScreenshotToDefaultPath_ExistFile() { var path = Path.Combine( _defaultOutputDirectory, - $"{nameof(AttachToUnityTest_TakeScreenshotAndSaveToDefaultPath)}.png"); + $"{nameof(AttachToUnityTest_SaveScreenshotToDefaultPath)}.png"); Assert.That(path, Does.Exist); } private const string SpecifyFilename = - nameof(AttachWithFilename_TakeScreenshotAndSaveToSpecifyPath) + "_Specified.png"; + nameof(AttachWithFilename_SaveScreenshotToSpecifyPath) + "_Specified.png"; [Test, Order(0)] [LoadScene(TestScene)] [TakeScreenshot(filename: SpecifyFilename)] - public void AttachWithFilename_TakeScreenshotAndSaveToSpecifyPath() + public void AttachWithFilename_SaveScreenshotToSpecifyPath() { var path = Path.Combine( _defaultOutputDirectory, @@ -137,7 +137,7 @@ public void AttachWithFilename_TakeScreenshotAndSaveToSpecifyPath() } [Test, Order(1)] - public void AttachWithFilename_TakeScreenshotAndSaveToSpecifyPath_AfterRunningTest_ExistFile() + public void AttachWithFilename_SaveScreenshotToSpecifyPath_ExistFile() { var path = Path.Combine( _defaultOutputDirectory, @@ -168,7 +168,7 @@ public void AttachWithGizmos_TakeScreenshotWithGizmos() [Test, Order(1)] [UnityPlatform(RuntimePlatform.OSXEditor, RuntimePlatform.WindowsEditor, RuntimePlatform.LinuxEditor)] - public void AttachWithGizmos_TakeScreenshotWithGizmos_AfterRunningTest_ExistFile() + public void AttachWithGizmos_TakeScreenshotWithGizmos_ExistFile() { var path = Path.Combine( _defaultOutputDirectory, From 84b5e585289d5b41fb6ffdc922f6d811339f18d1 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Fri, 3 Nov 2023 15:19:57 +0900 Subject: [PATCH 2/3] Refactor TakeScreenshot tests --- .../Attributes/TakeScreenshotAttributeTest.cs | 124 ++++++++++++++++++ .../RuntimeInternals/ScreenshotHelperTest.cs | 77 +---------- 2 files changed, 127 insertions(+), 74 deletions(-) diff --git a/Tests/Runtime/Attributes/TakeScreenshotAttributeTest.cs b/Tests/Runtime/Attributes/TakeScreenshotAttributeTest.cs index e8ff775..44b959e 100644 --- a/Tests/Runtime/Attributes/TakeScreenshotAttributeTest.cs +++ b/Tests/Runtime/Attributes/TakeScreenshotAttributeTest.cs @@ -16,6 +16,8 @@ namespace TestHelper.Attributes public class TakeScreenshotAttributeTest { 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 const int FileSizeThreshold2X = 100 * 1024; // Normal size is 80 to 90KB private readonly string _defaultOutputDirectory = Path.Combine(Application.persistentDataPath, "TestHelper", "Screenshots"); @@ -57,6 +59,7 @@ public void Attach_SaveScreenshotToDefaultPath_ExistFile() _defaultOutputDirectory, $"{nameof(Attach_SaveScreenshotToDefaultPath)}.png"); Assert.That(path, Does.Exist); + Assert.That(File.ReadAllBytes(path), Has.Length.GreaterThan(FileSizeThreshold)); } [Test, Order(0)] @@ -85,6 +88,7 @@ public void AttachToAsyncTest_SaveScreenshotToDefaultPath_ExistFile() _defaultOutputDirectory, $"{nameof(AttachToAsyncTest_SaveScreenshotToDefaultPath)}.png"); Assert.That(path, Does.Exist); + Assert.That(File.ReadAllBytes(path), Has.Length.GreaterThan(FileSizeThreshold)); } [UnityTest, Order(0)] @@ -113,6 +117,67 @@ public void AttachToUnityTest_SaveScreenshotToDefaultPath_ExistFile() _defaultOutputDirectory, $"{nameof(AttachToUnityTest_SaveScreenshotToDefaultPath)}.png"); Assert.That(path, Does.Exist); + Assert.That(File.ReadAllBytes(path), Has.Length.GreaterThan(FileSizeThreshold)); + } + + [Test, Order(0)] + [LoadScene(TestScene)] + [TakeScreenshot] + public void AttachToParameterizedTest_SaveAllScreenshots( + [Values(0, 1)] int i, + [Values(2, 3)] int j) + { + var path = Path.Combine( + _defaultOutputDirectory, + $"{nameof(AttachToParameterizedTest_SaveAllScreenshots)}_{i}-{j}_.png"); + if (File.Exists(path)) + { + File.Delete(path); + } + + Assume.That(path, Does.Not.Exist); + + // Take screenshot after running the test. + } + + [Test, Order(1)] + public void AttachToParameterizedTest_SaveAllScreenshots_ExistFile( + [Values(0, 1)] int i, + [Values(2, 3)] int j) + { + var path = Path.Combine( + _defaultOutputDirectory, + $"{nameof(AttachToParameterizedTest_SaveAllScreenshots)}_{i}-{j}_.png"); + Assert.That(path, Does.Exist); + } + + private const string SpecifyRelativeDirectory = "Logs/TestHelper/Screenshots"; + + [Test, Order(0)] + [LoadScene(TestScene)] + [TakeScreenshot(directory: SpecifyRelativeDirectory)] + public void AttachWithDirectory_SaveScreenshotToSpecifyPath() + { + var path = Path.Combine( + Path.GetFullPath(SpecifyRelativeDirectory), + $"{nameof(AttachWithDirectory_SaveScreenshotToSpecifyPath)}.png"); + if (File.Exists(path)) + { + File.Delete(path); + } + + Assume.That(path, Does.Not.Exist); + + // Take screenshot after running the test. + } + + [Test, Order(1)] + public void AttachWithDirectory_SaveScreenshotToSpecifyPath_ExistFile() + { + var path = Path.Combine( + Path.GetFullPath(SpecifyRelativeDirectory), + $"{nameof(AttachWithDirectory_SaveScreenshotToSpecifyPath)}.png"); + Assert.That(path, Does.Exist); } private const string SpecifyFilename = @@ -145,6 +210,64 @@ public void AttachWithFilename_SaveScreenshotToSpecifyPath_ExistFile() Assert.That(path, Does.Exist); } + [Test, Order(0)] + [LoadScene(TestScene)] + [TakeScreenshot(superSize: 2)] + public void AttachWithSuperSize_SaveSuperSizeScreenshot() + { + var path = Path.Combine( + _defaultOutputDirectory, + $"{nameof(AttachWithSuperSize_SaveSuperSizeScreenshot)}.png"); + if (File.Exists(path)) + { + File.Delete(path); + } + + Assume.That(path, Does.Not.Exist); + + // Take screenshot after running the test. + } + + [Test, Order(1)] + public void AttachWithSuperSize_SaveSuperSizeScreenshot_ExistFile() + { + var path = Path.Combine( + _defaultOutputDirectory, + $"{nameof(AttachWithSuperSize_SaveSuperSizeScreenshot)}.png"); + Assert.That(path, Does.Exist); + Assert.That(File.ReadAllBytes(path), Has.Length.GreaterThan(FileSizeThreshold2X)); + } + + [Test, Order(0)] + [LoadScene(TestScene)] + [TakeScreenshot(stereoCaptureMode: ScreenCapture.StereoScreenCaptureMode.BothEyes)] + public void AttachWithStereoCaptureMode_SaveStereoScreenshot() + { + var path = Path.Combine( + _defaultOutputDirectory, + $"{nameof(AttachWithStereoCaptureMode_SaveStereoScreenshot)}.png"); + if (File.Exists(path)) + { + File.Delete(path); + } + + Assume.That(path, Does.Not.Exist); + + // Take screenshot after running the test. + } + + [Test, Order(1)] + public void AttachWithStereoCaptureMode_SaveStereoScreenshot_ExistFile() + { + var path = Path.Combine( + _defaultOutputDirectory, + $"{nameof(AttachWithStereoCaptureMode_SaveStereoScreenshot)}.png"); + Assert.That(path, Does.Exist); + // Is it a stereo screenshot? See for yourself! Be a witness!! + // Note: Require stereo rendering settings. + // See: https://docs.unity3d.com/Manual/SinglePassStereoRendering.html + } + private class GizmoDemo : MonoBehaviour { private void OnDrawGizmos() @@ -174,6 +297,7 @@ public void AttachWithGizmos_TakeScreenshotWithGizmos_ExistFile() _defaultOutputDirectory, $"{nameof(AttachWithGizmos_TakeScreenshotWithGizmos)}.png"); Assert.That(path, Does.Exist); + // Is Gizmos shown in the screenshot? See for yourself! Be a witness!! } } } diff --git a/Tests/RuntimeInternals/ScreenshotHelperTest.cs b/Tests/RuntimeInternals/ScreenshotHelperTest.cs index cdb192e..b530873 100644 --- a/Tests/RuntimeInternals/ScreenshotHelperTest.cs +++ b/Tests/RuntimeInternals/ScreenshotHelperTest.cs @@ -56,27 +56,10 @@ public IEnumerator TakeScreenshot_SaveToDefaultPath() } [UnityTest] - public IEnumerator TakeScreenshot_SpecifyRelativeDirectory_SaveToSpecifyPath() + public IEnumerator TakeScreenshot_Multiple_SaveToEachSpecifyPaths() { - const string RelativePath = "Logs/TestHelper/Screenshots"; - var path = Path.Combine(Path.GetFullPath(RelativePath), - $"{nameof(TakeScreenshot_SpecifyRelativeDirectory_SaveToSpecifyPath)}.png"); - if (File.Exists(path)) - { - File.Delete(path); - } - - Assume.That(path, Does.Not.Exist); - - yield return ScreenshotHelper.TakeScreenshot(directory: RelativePath); - Assert.That(path, Does.Exist); - } - - [UnityTest] - public IEnumerator TakeScreenshot_SpecifyFilename_SaveToSpecifyPath() - { - const string Filename1 = "TakeScreenshot_SpecifyFilename1.png"; - const string Filename2 = "TakeScreenshot_SpecifyFilename2.png"; + const string Filename1 = "TakeScreenshot_Multiple_SpecifyFilename1.png"; + const string Filename2 = "TakeScreenshot_Multiple_SpecifyFilename2.png"; var path1 = Path.Combine(_defaultOutputDirectory, Filename1); if (File.Exists(path1)) @@ -104,42 +87,6 @@ public IEnumerator TakeScreenshot_SpecifyFilename_SaveToSpecifyPath() Assert.That(path2, Does.Exist); } - [UnityTest] - public IEnumerator TakeScreenshot_SpecifySuperSize_SaveSuperSizeFile() - { - var path = Path.Combine(_defaultOutputDirectory, - $"{nameof(TakeScreenshot_SpecifySuperSize_SaveSuperSizeFile)}.png"); - if (File.Exists(path)) - { - File.Delete(path); - } - - Assume.That(path, Does.Not.Exist); - - yield return ScreenshotHelper.TakeScreenshot(superSize: 2); - Assert.That(path, Does.Exist); - // Please visually check the file. - } - - [UnityTest] - public IEnumerator TakeScreenshot_SpecifyStereoCaptureMode_SaveStereoFile() - { - var path = Path.Combine(_defaultOutputDirectory, - $"{nameof(TakeScreenshot_SpecifyStereoCaptureMode_SaveStereoFile)}.png"); - if (File.Exists(path)) - { - File.Delete(path); - } - - Assume.That(path, Does.Not.Exist); - - yield return ScreenshotHelper.TakeScreenshot( - stereoCaptureMode: ScreenCapture.StereoScreenCaptureMode.BothEyes); - Assert.That(path, Does.Exist); - // Require stereo rendering settings. - // See: https://docs.unity3d.com/Manual/SinglePassStereoRendering.html - } - [UnityTest] public IEnumerator TakeScreenshot_SpecifySuperSizeAndStereoCaptureMode_NotWork() { @@ -149,24 +96,6 @@ public IEnumerator TakeScreenshot_SpecifySuperSizeAndStereoCaptureMode_NotWork() LogAssert.Expect(LogType.Error, "superSize and stereoCaptureMode cannot be specified at the same time."); } - [UnityTest] - public IEnumerator TakeScreenshot_Parameterized_SaveAllFiles( - [Values(0, 1)] int i, - [Values(2, 3)] int j) - { - var path = Path.Combine(_defaultOutputDirectory, - $"{nameof(TakeScreenshot_Parameterized_SaveAllFiles)}_{i}-{j}_.png"); - if (File.Exists(path)) - { - File.Delete(path); - } - - Assume.That(path, Does.Not.Exist); - - yield return ScreenshotHelper.TakeScreenshot(); - Assert.That(path, Does.Exist); - } - [Test] public async Task TakeScreenshot_FromAsyncTest() { From a76b98a9987fd6ff4760fde3a186ff4b90741a76 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Fri, 3 Nov 2023 15:28:09 +0900 Subject: [PATCH 3/3] Fix comments --- .../Attributes/GizmosShowOnGameViewAttributeTest.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Tests/Runtime/Attributes/GizmosShowOnGameViewAttributeTest.cs b/Tests/Runtime/Attributes/GizmosShowOnGameViewAttributeTest.cs index b8ae775..dbc53b3 100644 --- a/Tests/Runtime/Attributes/GizmosShowOnGameViewAttributeTest.cs +++ b/Tests/Runtime/Attributes/GizmosShowOnGameViewAttributeTest.cs @@ -34,7 +34,7 @@ public IEnumerator UnitySetUp() [UnityTearDown] public IEnumerator UnityTearDown() { - yield return ScreenshotHelper.TakeScreenshot(); // Take screenshot before hide Gizmos. + yield return ScreenshotHelper.TakeScreenshot(); // Take screenshot before revert Gizmos. } [Test] @@ -42,7 +42,7 @@ public IEnumerator UnityTearDown() [GizmosShowOnGameView] public void Attach_True_ShowGizmos() { - // verify screenshot. + // See the screenshot yourself! Be a witness!! } [Test] @@ -50,7 +50,7 @@ public void Attach_True_ShowGizmos() [GizmosShowOnGameView(false)] public void Attach_False_HideGizmos() { - // verify screenshot. + // See the screenshot yourself! Be a witness!! } [Test] @@ -59,7 +59,7 @@ public void Attach_False_HideGizmos() public async Task AttachToAsyncTest_ShowGizmos() { await Task.Yield(); - // verify screenshot. + // See the screenshot yourself! Be a witness!! } [UnityTest] @@ -68,7 +68,7 @@ public async Task AttachToAsyncTest_ShowGizmos() public IEnumerator AttachToUnityTest_ShowGizmos() { yield return null; - // verify screenshot. + // See the screenshot yourself! Be a witness!! } } }