Skip to content

Commit

Permalink
Add cleanup output file before take screenshot test
Browse files Browse the repository at this point in the history
  • Loading branch information
nowsprinting committed Oct 30, 2023
1 parent 26cf1da commit 3666ef6
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 17 deletions.
40 changes: 40 additions & 0 deletions Tests/Runtime/Attributes/TakeScreenshotAttributeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

Expand All @@ -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.
}
Expand All @@ -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.
}
Expand All @@ -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.
}

Expand Down
82 changes: 67 additions & 15 deletions Tests/Runtime/Utils/ScreenshotHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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.
}
Expand All @@ -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
Expand All @@ -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);
}
}
Expand Down
11 changes: 9 additions & 2 deletions Tests/RuntimeInternals/ScreenshotHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down

0 comments on commit 3666ef6

Please sign in to comment.