Skip to content

Commit 180bd4e

Browse files
authored
Merge pull request #270 from nowsprinting/chore/screenshot_async
Change screenshot method to TakeScreenshotAsync
2 parents c9411e1 + 4b7915c commit 180bd4e

File tree

7 files changed

+35
-65
lines changed

7 files changed

+35
-65
lines changed

Runtime/Monkey.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ namespace TestHelper.UI
3030
/// </summary>
3131
public static class Monkey
3232
{
33-
private static MonoBehaviour s_coroutineRunner; // for take screenshots
34-
3533
/// <summary>
3634
/// Run monkey testing by repeating to call <c cref="RunStep">RunStep</c> and wait.
3735
/// </summary>
@@ -288,14 +286,19 @@ private static async UniTask TakeScreenshotAsync(ScreenshotOptions screenshotOpt
288286
return;
289287
}
290288

289+
var filename = screenshotOptions.FilenameStrategy.GetFilename();
290+
message.Append($", screenshot={filename}");
291+
292+
#if UNITY_2023_1_OR_NEWER
293+
await ScreenshotHelper.TakeScreenshotAsync(
294+
directory: screenshotOptions.Directory,
295+
filename: filename);
296+
#else
291297
if (!s_coroutineRunner)
292298
{
293299
s_coroutineRunner = new GameObject("CoroutineRunner").AddComponent<CoroutineRunner>();
294300
}
295301

296-
var filename = screenshotOptions.FilenameStrategy.GetFilename();
297-
message.Append($", screenshot={filename}");
298-
299302
await ScreenshotHelper.TakeScreenshot(
300303
directory: screenshotOptions.Directory,
301304
filename: filename,
@@ -304,10 +307,15 @@ await ScreenshotHelper.TakeScreenshot(
304307
logFilepath: false
305308
)
306309
.ToUniTask(s_coroutineRunner);
310+
#endif
307311
}
308312

313+
#if !UNITY_2023_1_OR_NEWER
314+
private static MonoBehaviour s_coroutineRunner; // for take screenshots
315+
309316
private class CoroutineRunner : MonoBehaviour
310317
{
311318
}
319+
#endif
312320
}
313321
}

Runtime/MonkeyConfig.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public class MonkeyConfig
5757
/// <summary>
5858
/// Show Gizmos on <c>GameView</c> during running monkey test if true
5959
/// </summary>
60+
[Obsolete("Use GizmosShowOnGameViewAttribute or GameViewControlHelper.SetGizmos instead.")]
6061
public bool Gizmos { get; set; }
6162

6263
/// <summary>
@@ -94,7 +95,9 @@ public class MonkeyConfig
9495

9596
/// <summary>
9697
/// Operators that the monkey invokes.
97-
/// <c>logger</c> and <c>screenshotOptions</c> will be overridden at runtime by the same name properties in this <c>MonkeyConfig</c> instance.
98+
/// <p/>
99+
/// <c>logger</c>, <c>screenshotOptions</c>, <c>GetScreenPoint</c>, and <c>ReachableStrategy</c> will be overridden at runtime by the same name properties in this <c>MonkeyConfig</c> instance.
100+
/// And <c>Random</c> will also be overridden at runtime by a forked instance from the <c>Random</c> property in this <c>MonkeyConfig</c> instance.
98101
/// </summary>
99102
public IEnumerable<IOperator> Operators { get; set; } = new IOperator[]
100103
{

Runtime/Operators/Utils/OperationLogger.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public async UniTask Log()
6161
if (_screenshotOptions != null)
6262
{
6363
_lastScreenshotPath = _screenshotOptions.FilenameStrategy.GetFilename();
64+
#if UNITY_2023_1_OR_NEWER
65+
await ScreenshotHelper.TakeScreenshotAsync(
66+
directory: _screenshotOptions.Directory,
67+
filename: _lastScreenshotPath);
68+
#else
6469
await ScreenshotHelper.TakeScreenshot(
6570
directory: _screenshotOptions.Directory,
6671
filename: _lastScreenshotPath,
@@ -69,6 +74,7 @@ await ScreenshotHelper.TakeScreenshot(
6974
logFilepath: false
7075
)
7176
.ToUniTask(_gameObject.GetComponent<MonoBehaviour>());
77+
#endif
7278
}
7379

7480
_logger.Log(BuildMessage());

Runtime/Operators/Utils/PointerClickEventSimulator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace TestHelper.UI.Operators.Utils
2727
/// </summary>
2828
/// <param name="gameObject">Click target <c>GameObject</c></param>
2929
/// <param name="raycastResult"><c>RaycastResult</c> includes the screen position of the starting operation. Passing <c>default</c> may be OK, depending on the game-title implementation.</param>
30-
/// <param name="logger">Verbose logger set if you need</param>
30+
/// <param name="logger">Logger that outputs messages when something is irregular.</param>
3131
public PointerClickEventSimulator(GameObject gameObject, RaycastResult raycastResult, ILogger logger)
3232
{
3333
_gameObject = gameObject;

Runtime/Operators/Utils/PointerDragEventSimulator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public struct PointerDragEventSimulator : IDisposable
3131
/// </summary>
3232
/// <param name="gameObject">Drag target <c>GameObject</c></param>
3333
/// <param name="raycastResult"><c>RaycastResult</c> includes the screen position of the starting operation. Passing <c>default</c> may be OK, depending on the game-title implementation.</param>
34-
/// <param name="logger">Verbose logger set if you need</param>
34+
/// <param name="logger">Logger that outputs messages when something is irregular.</param>
3535
public PointerDragEventSimulator(GameObject gameObject, RaycastResult raycastResult, ILogger logger)
3636
{
3737
_gameObject = gameObject;

Runtime/ScreenshotOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) 2023-2024 Koji Hasegawa.
22
// This software is released under the MIT License.
33

4+
using System;
45
using TestHelper.UI.ScreenshotFilenameStrategies;
56
using UnityEngine;
67

@@ -27,6 +28,7 @@ public class ScreenshotOptions
2728
/// The factor to increase resolution with.
2829
/// SuperSize and StereoCaptureMode cannot be specified at the same time.
2930
/// </summary>
31+
[Obsolete("The way screenshots are taken has changed, so this property is no longer used.")]
3032
public int SuperSize { get; set; } = 1;
3133

3234
/// <summary>
@@ -37,6 +39,7 @@ public class ScreenshotOptions
3739
/// Require stereo rendering settings.
3840
/// See: https://docs.unity3d.com/Manual/SinglePassStereoRendering.html
3941
/// </remarks>
42+
[Obsolete("The way screenshots are taken has changed, so this property is no longer used.")]
4043
public ScreenCapture.StereoScreenCaptureMode StereoCaptureMode { get; set; } =
4144
ScreenCapture.StereoScreenCaptureMode.LeftEye;
4245
}

Tests/Runtime/MonkeyTest.cs

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,6 @@ public void LotteryOperator_BingoReachableComponent_ReturnOperator()
344344
public class Screenshots
345345
{
346346
private const int FileSizeThreshold = 5441; // VGA size solid color file size
347-
private const int FileSizeThreshold2X = 100 * 1024; // Normal size is 80 to 90KB
348347
private readonly string _defaultOutputDirectory = CommandLineArgs.GetScreenshotDirectory();
349348
private string _filename;
350349
private string _path;
@@ -361,7 +360,7 @@ public void SetUp()
361360
}
362361
}
363362

364-
private MonkeyConfig CreateMonkeyConfig(ScreenshotOptions screenshotOptions)
363+
private static MonkeyConfig CreateMonkeyConfig(ScreenshotOptions screenshotOptions)
365364
{
366365
var config = new MonkeyConfig
367366
{
@@ -377,7 +376,7 @@ private MonkeyConfig CreateMonkeyConfig(ScreenshotOptions screenshotOptions)
377376
return config;
378377
}
379378

380-
private InteractableComponentsFinder CreateInteractableComponentsFinder(MonkeyConfig config)
379+
private static InteractableComponentsFinder CreateInteractableComponentsFinder(MonkeyConfig config)
381380
{
382381
return new InteractableComponentsFinder(operators: config.Operators);
383382
}
@@ -419,7 +418,6 @@ public async Task RunStep_withScreenshots_specifyPath_takeScreenshotsAndSaveToSp
419418
{
420419
Directory = relativeDirectory,
421420
FilenameStrategy = new StubScreenshotFilenameStrategy(filename),
422-
SuperSize = 2,
423421
});
424422
var interactableComponentsFinder = CreateInteractableComponentsFinder(config);
425423

@@ -434,53 +432,6 @@ await Monkey.RunStep(
434432
Assert.That(new FileInfo(path), Has.Length.GreaterThan(FileSizeThreshold));
435433
}
436434

437-
[Test]
438-
[Description("This test fails with stereo rendering settings.")]
439-
[LoadScene(TestScene)]
440-
public async Task RunStep_withScreenshots_superSize_takeScreenshotsSuperSize()
441-
{
442-
var config = CreateMonkeyConfig(new ScreenshotOptions
443-
{
444-
SuperSize = 2, // 2x size
445-
});
446-
var interactableComponentsFinder = CreateInteractableComponentsFinder(config);
447-
448-
await Monkey.RunStep(
449-
config.Random,
450-
config.Logger,
451-
interactableComponentsFinder,
452-
config.IgnoreStrategy,
453-
config.ReachableStrategy);
454-
455-
Assert.That(_path, Does.Exist);
456-
Assert.That(new FileInfo(_path), Has.Length.GreaterThan(FileSizeThreshold2X));
457-
// Note: This test fails with stereo rendering settings.
458-
// See: https://docs.unity3d.com/Manual/SinglePassStereoRendering.html
459-
}
460-
461-
[Test]
462-
[LoadScene(TestScene)]
463-
[Description("Is it a stereo screenshot? See for yourself! Be a witness!!")]
464-
public async Task RunStep_withScreenshots_stereo_takeScreenshotsStereo()
465-
{
466-
var config = CreateMonkeyConfig(new ScreenshotOptions
467-
{
468-
StereoCaptureMode = ScreenCapture.StereoScreenCaptureMode.BothEyes,
469-
});
470-
var interactableComponentsFinder = CreateInteractableComponentsFinder(config);
471-
472-
await Monkey.RunStep(
473-
config.Random,
474-
config.Logger,
475-
interactableComponentsFinder,
476-
config.IgnoreStrategy,
477-
config.ReachableStrategy);
478-
479-
Assert.That(_path, Does.Exist);
480-
// Note: Require stereo rendering settings.
481-
// See: https://docs.unity3d.com/Manual/SinglePassStereoRendering.html
482-
}
483-
484435
[Test]
485436
[LoadScene(TestScene)]
486437
public async Task Run_withScreenshots_noInteractiveComponent_takeScreenshot()
@@ -511,12 +462,11 @@ public async Task Run_withScreenshots_noInteractiveComponent_takeScreenshot()
511462
}
512463

513464
[Test]
514-
[GameViewResolution(GameViewResolution.VGA)]
515465
[LoadScene("../Scenes/InfiniteLoop.unity")]
516466
public async Task Run_withScreenshots_InfiniteLoop_takeScreenshot()
517467
{
518-
_filename = $"{TestContext.CurrentContext.Test.Name}_0011.png"; // 10 steps + 1
519-
_path = Path.Combine(_defaultOutputDirectory, _filename);
468+
var filename = $"{TestContext.CurrentContext.Test.Name}_0011.png"; // 10 steps + 1
469+
var path = Path.Combine(_defaultOutputDirectory, filename);
520470

521471
var config = CreateMonkeyConfig(new ScreenshotOptions());
522472
config.Lifetime = TimeSpan.FromSeconds(2); // 2sec
@@ -530,10 +480,10 @@ public async Task Run_withScreenshots_InfiniteLoop_takeScreenshot()
530480
}
531481
catch (InfiniteLoopException e)
532482
{
533-
Assert.That(e.Message, Does.Contain(_filename));
483+
Assert.That(e.Message, Does.Contain(filename));
534484
}
535485

536-
Assert.That(_path, Does.Exist);
486+
Assert.That(path, Does.Exist);
537487
}
538488
}
539489

@@ -661,10 +611,10 @@ public void LotteryOperator_NotReachableObjectOnly_LogNotLottery()
661611
}
662612

663613
[TestFixture]
614+
[GameViewResolution(GameViewResolution.VGA)]
664615
public class DetectingInfiniteLoop
665616
{
666617
[Test]
667-
[GameViewResolution(GameViewResolution.VGA)]
668618
[LoadScene("../Scenes/InfiniteLoop.unity")]
669619
public async Task Run_InfiniteLoop_throwsInfiniteLoopException()
670620
{

0 commit comments

Comments
 (0)