From 3666ef6716b24c70c2c028489dd0dc93f04a1cbb Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Mon, 30 Oct 2023 21:38:35 +0900 Subject: [PATCH] Add cleanup output file before take screenshot test --- .../Attributes/TakeScreenshotAttributeTest.cs | 40 +++++++++ Tests/Runtime/Utils/ScreenshotHelperTest.cs | 82 +++++++++++++++---- .../RuntimeInternals/ScreenshotHelperTest.cs | 11 ++- 3 files changed, 116 insertions(+), 17 deletions(-) diff --git a/Tests/Runtime/Attributes/TakeScreenshotAttributeTest.cs b/Tests/Runtime/Attributes/TakeScreenshotAttributeTest.cs index 77f2f98..664ad2c 100644 --- a/Tests/Runtime/Attributes/TakeScreenshotAttributeTest.cs +++ b/Tests/Runtime/Attributes/TakeScreenshotAttributeTest.cs @@ -37,6 +37,16 @@ public void SetUp() [TakeScreenshot] public void Attach_TakeScreenshotAndSaveToDefaultPath() { + var path = Path.Combine( + _defaultOutputDirectory, + $"{nameof(Attach_TakeScreenshotAndSaveToDefaultPath)}.png"); + if (File.Exists(path)) + { + File.Delete(path); + } + + Assume.That(path, Does.Not.Exist); + // Take screenshot after running the test. } @@ -54,6 +64,16 @@ public void Attach_TakeScreenshotAndSaveToDefaultPath_AfterRunningTest_ExistFile [TakeScreenshot] public async Task AttachToAsyncTest_TakeScreenshotAndSaveToDefaultPath() { + var path = Path.Combine( + _defaultOutputDirectory, + $"{nameof(AttachToAsyncTest_TakeScreenshotAndSaveToDefaultPath)}.png"); + if (File.Exists(path)) + { + File.Delete(path); + } + + Assume.That(path, Does.Not.Exist); + await Task.Yield(); // Take screenshot after running the test. } @@ -72,6 +92,16 @@ public void AttachToAsyncTest_TakeScreenshotAndSaveToDefaultPath_AfterRunningTes [TakeScreenshot] public IEnumerator AttachToUnityTest_TakeScreenshotAndSaveToDefaultPath() { + var path = Path.Combine( + _defaultOutputDirectory, + $"{nameof(AttachToUnityTest_TakeScreenshotAndSaveToDefaultPath)}.png"); + if (File.Exists(path)) + { + File.Delete(path); + } + + Assume.That(path, Does.Not.Exist); + yield return null; // Take screenshot after running the test. } @@ -90,6 +120,16 @@ public void AttachToUnityTest_TakeScreenshotAndSaveToDefaultPath_AfterRunningTes [TakeScreenshot(filename: nameof(AttachWithFilename_TakeScreenshotAndSaveToSpecifyPath) + ".png")] public void AttachWithFilename_TakeScreenshotAndSaveToSpecifyPath() { + var path = Path.Combine( + _defaultOutputDirectory, + $"{nameof(AttachWithFilename_TakeScreenshotAndSaveToSpecifyPath)}.png"); + if (File.Exists(path)) + { + File.Delete(path); + } + + Assume.That(path, Does.Not.Exist); + // Take screenshot after running the test. } diff --git a/Tests/Runtime/Utils/ScreenshotHelperTest.cs b/Tests/Runtime/Utils/ScreenshotHelperTest.cs index 625108b..ce4de37 100644 --- a/Tests/Runtime/Utils/ScreenshotHelperTest.cs +++ b/Tests/Runtime/Utils/ScreenshotHelperTest.cs @@ -37,9 +37,15 @@ public void SetUp() [LoadScene(TestScene)] public IEnumerator TakeScreenshot_SaveToDefaultPath() { - yield return ScreenshotHelper.TakeScreenshot(); - var path = Path.Combine(_defaultOutputDirectory, $"{nameof(TakeScreenshot_SaveToDefaultPath)}.png"); + if (File.Exists(path)) + { + File.Delete(path); + } + + Assume.That(path, Does.Not.Exist); + + yield return ScreenshotHelper.TakeScreenshot(); Assert.That(path, Does.Exist); Assert.That(File.ReadAllBytes(path), Has.Length.GreaterThan(FileSizeThreshold)); } @@ -50,10 +56,16 @@ public IEnumerator TakeScreenshot_SaveToDefaultPath() public IEnumerator TakeScreenshot_SpecifyDirectoryInEditor_SaveToSpecifyPath() { const string RelativePath = "Logs/Screenshots"; - yield return ScreenshotHelper.TakeScreenshot(directory: RelativePath); - var path = Path.Combine(Path.GetFullPath(RelativePath), $"{nameof(TakeScreenshot_SpecifyDirectoryInEditor_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); } @@ -64,10 +76,16 @@ public IEnumerator TakeScreenshot_SpecifyDirectoryInEditor_SaveToSpecifyPath() public IEnumerator TakeScreenshot_SpecifyDirectoryOnPlayer_SaveToDefaultPath() { const string RelativePath = "Logs/Screenshots"; - yield return ScreenshotHelper.TakeScreenshot(directory: RelativePath); - var path = Path.Combine(_defaultOutputDirectory, $"{nameof(TakeScreenshot_SpecifyDirectoryOnPlayer_SaveToDefaultPath)}.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); } @@ -78,24 +96,46 @@ public IEnumerator TakeScreenshot_SpecifyFilename_SaveToSpecifyPath() const string Filename1 = "SpecifyFilename1.png"; const string Filename2 = "SpecifyFilename2.png"; + var path1 = Path.Combine(_defaultOutputDirectory, Filename1); + if (File.Exists(path1)) + { + File.Delete(path1); + } + + var path2 = Path.Combine(_defaultOutputDirectory, Filename2); + if (File.Exists(path2)) + { + File.Delete(path2); + } + + Assume.That(path1, Does.Not.Exist); + Assume.That(path2, Does.Not.Exist); + _text.text = Filename1; yield return ScreenshotHelper.TakeScreenshot(filename: Filename1); _text.text = Filename2; yield return ScreenshotHelper.TakeScreenshot(filename: Filename2); - Assert.That(Path.Combine(_defaultOutputDirectory, Filename1), Does.Exist); - Assert.That(Path.Combine(_defaultOutputDirectory, Filename2), Does.Exist); + // Verify after calling twice + Assert.That(path1, Does.Exist); + Assert.That(path2, Does.Exist); } [UnityTest] [LoadScene(TestScene)] public IEnumerator TakeScreenshot_SpecifySuperSize_SaveSuperSizeFile() { - yield return ScreenshotHelper.TakeScreenshot(superSize: 2); - 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. } @@ -104,11 +144,17 @@ public IEnumerator TakeScreenshot_SpecifySuperSize_SaveSuperSizeFile() [LoadScene(TestScene)] public IEnumerator TakeScreenshot_SpecifyStereoCaptureMode_SaveStereoFile() { - yield return ScreenshotHelper.TakeScreenshot( - stereoCaptureMode: ScreenCapture.StereoScreenCaptureMode.BothEyes); - 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 @@ -129,10 +175,16 @@ public IEnumerator TakeScreenshot_Parameterized_SaveAllFiles( [Values(0, 1)] int i, [Values(2, 3)] int j) { - yield return ScreenshotHelper.TakeScreenshot(); - 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); } } diff --git a/Tests/RuntimeInternals/ScreenshotHelperTest.cs b/Tests/RuntimeInternals/ScreenshotHelperTest.cs index 393a4c4..d7c7657 100644 --- a/Tests/RuntimeInternals/ScreenshotHelperTest.cs +++ b/Tests/RuntimeInternals/ScreenshotHelperTest.cs @@ -37,9 +37,16 @@ public void SetUp() [LoadScene(TestScene)] public IEnumerator TakeScreenshot_SaveToDefaultPath() { - yield return ScreenshotHelper.TakeScreenshot(); // internal, default filename is member name - var path = Path.Combine(_defaultOutputDirectory, $"{nameof(TakeScreenshot_SaveToDefaultPath)}.png"); + if (File.Exists(path)) + { + File.Delete(path); + } + + Assume.That(path, Does.Not.Exist); + + yield return ScreenshotHelper.TakeScreenshot(); // default filename is member name when internal + Assert.That(path, Does.Exist); Assert.That(File.ReadAllBytes(path), Has.Length.GreaterThan(FileSizeThreshold)); }