Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress log option for screenshot #80

Merged
merged 3 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions RuntimeInternals/ScreenshotHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -57,11 +57,13 @@ private static string DefaultFilename(string callerMemberName)
/// Using caller method name when run in runtime context.</param>
/// <param name="superSize">The factor to increase resolution with.</param>
/// <param name="stereoCaptureMode">The eye texture to capture when stereo rendering is enabled.</param>
/// <param name="logFilepath">Output filename to Debug.Log</param>
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)
{
Expand Down Expand Up @@ -101,7 +103,11 @@ public static IEnumerator TakeScreenshot(
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}");
}
}
}
}
37 changes: 29 additions & 8 deletions Tests/RuntimeInternals/ScreenshotHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -37,6 +33,7 @@ public IEnumerator SetUp()
}

[UnityTest]
[LoadScene(TestScene)]
public IEnumerator TakeScreenshot_SaveToDefaultPath()
{
var path = Path.Combine(_defaultOutputDirectory, $"{nameof(TakeScreenshot_SaveToDefaultPath)}.png");
Expand All @@ -53,6 +50,7 @@ public IEnumerator TakeScreenshot_SaveToDefaultPath()
}

[UnityTest]
[LoadScene(TestScene)]
public IEnumerator TakeScreenshot_Multiple_SaveToEachSpecifyPaths()
{
const string Filename1 = "TakeScreenshot_Multiple_SpecifyFilename1.png";
Expand Down Expand Up @@ -85,6 +83,7 @@ public IEnumerator TakeScreenshot_Multiple_SaveToEachSpecifyPaths()
}

[UnityTest]
[LoadScene(TestScene)]
public IEnumerator TakeScreenshot_SpecifySuperSizeAndStereoCaptureMode_NotWork()
{
yield return ScreenshotHelper.TakeScreenshot(
Expand All @@ -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");
Expand All @@ -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_"));
}
}
}
}
45 changes: 0 additions & 45 deletions Tests/RuntimeInternals/ScreenshotHelperTestSceneBuilder.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"TestHelper.RuntimeInternals",
"TestHelper",
"UniTask"
],
"includePlatforms": [],
Expand Down