Skip to content

Commit

Permalink
Merge pull request #37 from nowsprinting/feature/gizmos
Browse files Browse the repository at this point in the history
Add GizmosShowOnGameViewAttribute
  • Loading branch information
nowsprinting committed Oct 30, 2023
2 parents d30a554 + b6dd4fd commit c6993b6
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 16 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,35 @@ public class MyTestClass
> In batchmode, open `GameView` window.

#### GizmosShowOnGameView

`GizmosShowOnGameViewAttribute` is an NUnit test attribute class to show/hide Gizmos on `GameView` during the test running.

This attribute can attached to test method only.
Can be used with sync Test, async Test, and UnityTest.

Usage:

```csharp
using NUnit.Framework;
using TestHelper.Attributes;

[TestFixture]
public class MyTestClass
{
[Test]
[GizmosShowOnGameView(true)]
public void MyTestMethod()
{
// Show Gizmos on GameView.
}
}
```

> **Note**
> In batchmode, open `GameView` window.

#### IgnoreBatchMode

`IgnoreBatchModeAttribute` is an NUnit test attribute class to skip test execution when run tests with `-batchmode` from the commandline.
Expand Down
46 changes: 46 additions & 0 deletions Runtime/Attributes/GizmosShowOnGameViewAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2023 Koji Hasegawa.
// This software is released under the MIT License.

using System;
using System.Collections;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using TestHelper.RuntimeInternals;
using UnityEngine.TestTools;

namespace TestHelper.Attributes
{
/// <summary>
/// Show/ hide Gizmos on <c>GameView</c> during the test running.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class GizmosShowOnGameViewAttribute : NUnitAttribute, IOuterUnityTestAction
{
private readonly bool _show;
private readonly bool _beforeShow;

/// <summary>
/// Show/ hide Gizmos on <c>GameView</c> during the test running.
/// </summary>
/// <param name="show">True: show Gizmos, False: hide Gizmos.</param>
public GizmosShowOnGameViewAttribute(bool show = true)
{
_show = show;
_beforeShow = GameViewControlHelper.GetGizmos();
}

/// <inheritdoc />
public IEnumerator BeforeTest(ITest test)
{
GameViewControlHelper.SetGizmos(_show);
yield return null;
}

/// <inheritdoc />
public IEnumerator AfterTest(ITest test)
{
GameViewControlHelper.SetGizmos(_beforeShow);
yield return null;
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Attributes/GizmosShowOnGameViewAttribute.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 19 additions & 3 deletions Runtime/Attributes/TakeScreenshotAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
using System.Collections;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using TestHelper.Utils;
using TestHelper.RuntimeInternals;
using UnityEngine;
using UnityEngine.TestTools;
using ScreenshotHelper = TestHelper.Utils.ScreenshotHelper;

namespace TestHelper.Attributes
{
Expand All @@ -21,6 +22,7 @@ public class TakeScreenshotAttribute : NUnitAttribute, IOuterUnityTestAction
private readonly string _filename;
private readonly int _superSize;
private readonly ScreenCapture.StereoScreenCaptureMode _stereoCaptureMode;
private readonly bool _gizmos;

/// <summary>
/// Take a screenshot and save it to file after running the test.
Expand All @@ -38,17 +40,19 @@ public class TakeScreenshotAttribute : NUnitAttribute, IOuterUnityTestAction
/// <param name="filename">Filename to store screenshot.</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="gizmos">True: show Gizmos on GameView</param>
public TakeScreenshotAttribute(
string directory = null,
string filename = null,
int superSize = 1,
ScreenCapture.StereoScreenCaptureMode stereoCaptureMode = ScreenCapture.StereoScreenCaptureMode.LeftEye
)
ScreenCapture.StereoScreenCaptureMode stereoCaptureMode = ScreenCapture.StereoScreenCaptureMode.LeftEye,
bool gizmos = false)
{
_directory = directory;
_filename = filename;
_superSize = superSize;
_stereoCaptureMode = stereoCaptureMode;
_gizmos = gizmos;
}

/// <inheritdoc />
Expand All @@ -60,7 +64,19 @@ public IEnumerator BeforeTest(ITest test)
/// <inheritdoc />
public IEnumerator AfterTest(ITest test)
{
var beforeGizmos = false;
if (_gizmos)
{
beforeGizmos = GameViewControlHelper.GetGizmos();
GameViewControlHelper.SetGizmos(true);
}

yield return ScreenshotHelper.TakeScreenshot(_directory, _filename, _superSize, _stereoCaptureMode);

if (_gizmos)
{
GameViewControlHelper.SetGizmos(beforeGizmos);
}
}
}
}
41 changes: 30 additions & 11 deletions RuntimeInternals/GameViewControlHelper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) 2023 Koji Hasegawa.
// This software is released under the MIT License.

using System;
using System.Reflection;
using TestHelper.RuntimeInternals.Wrappers.UnityEditor;
using UnityEditor;
using UnityEngine;
Expand All @@ -15,24 +13,45 @@ namespace TestHelper.RuntimeInternals
/// </summary>
public static class GameViewControlHelper
{
private static Type s_gameView;

/// <summary>
/// Focus <c>GameView</c> or <c>SimulatorWindow</c>.
/// </summary>
public static void Focus()
{
#if UNITY_EDITOR
if (s_gameView == null)
{
var assembly = Assembly.Load("UnityEditor.dll");
var viewClass = Application.isBatchMode ? "UnityEditor.GameView" : "UnityEditor.PlayModeView";
// Note: Freezes when getting SimulatorWindow in batchmode
GameViewWrapper.GetWindow();
#endif
}

s_gameView = assembly.GetType(viewClass);
/// <summary>
/// Get Gizmos show/ hide status on <c>GameView</c>.
/// </summary>
/// <returns>True: show Gizmos, False: hide Gizmos.</returns>
public static bool GetGizmos()
{
var gizmos = false;
#if UNITY_EDITOR
var gameViewWrapper = GameViewWrapper.GetWindow(false);
if (gameViewWrapper != null)
{
gizmos = gameViewWrapper.GetGizmos();
}
#endif
return gizmos;
}

EditorWindow.GetWindow(s_gameView, false, null, true);
/// <summary>
/// Show/ hide Gizmos on <c>GameView</c>.
/// </summary>
/// <param name="show">True: show Gizmos, False: hide Gizmos.</param>
public static void SetGizmos(bool show)
{
#if UNITY_EDITOR
var gameViewWrapper = GameViewWrapper.GetWindow(false);
if (gameViewWrapper != null)
{
gameViewWrapper.SetGizmos(show);
}
#endif
}

Expand Down
23 changes: 21 additions & 2 deletions RuntimeInternals/Wrappers/UnityEditor/GameViewWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ public class GameViewWrapper
private static readonly PropertyInfo s_selectedSizeIndex = s_gameView
.GetProperty("selectedSizeIndex", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

private static readonly FieldInfo s_gizmos = s_gameView
.GetField("m_Gizmos", BindingFlags.Instance | BindingFlags.NonPublic);

private readonly EditorWindow _instance;

private GameViewWrapper(EditorWindow instance)
{
_instance = instance;
}

public static GameViewWrapper GetWindow()
public static GameViewWrapper GetWindow(bool focus = true)
{
if (s_gameView == null)
{
Expand All @@ -43,7 +46,13 @@ public static GameViewWrapper GetWindow()
return null;
}

var gameView = EditorWindow.GetWindow(s_gameView, false, null, true);
if (s_gizmos == null)
{
Debug.LogError("GameView.m_Gizmos field not found.");
return null;
}

var gameView = EditorWindow.GetWindow(s_gameView, false, null, focus);
if (gameView == null)
{
Debug.LogError("EditorWindow.GetWindow(GameView) failed.");
Expand All @@ -62,6 +71,16 @@ public void SelectedSizeIndex(int index)
{
s_selectedSizeIndex.SetValue(_instance, index);
}

public bool GetGizmos()
{
return (bool)s_gizmos.GetValue(_instance);
}

public void SetGizmos(bool show)
{
s_gizmos.SetValue(_instance, show);
}
}
}
#endif
71 changes: 71 additions & 0 deletions Tests/Runtime/Attributes/GizmosShowOnGameViewAttributeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) 2023 Koji Hasegawa.
// This software is released under the MIT License.

using System.Collections;
using System.Threading.Tasks;
using NUnit.Framework;
using TestHelper.Utils;
using UnityEngine;
using UnityEngine.TestTools;

namespace TestHelper.Attributes
{
[TestFixture]
public class GizmosShowOnGameViewAttributeTest
{
private class GizmoDemo : MonoBehaviour
{
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawSphere(transform.position, 0.2f);
}
}

[UnitySetUp]
public IEnumerator UnitySetUp()
{
var gameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
gameObject.AddComponent<GizmoDemo>();
yield return null;
}

[UnityTearDown]
public IEnumerator UnityTearDown()
{
yield return ScreenshotHelper.TakeScreenshot(); // Take screenshot before hide Gizmos.
}

[Test]
[CreateScene(camera: true, light: true)]
[GizmosShowOnGameView]
public void Attach_True_ShowGizmos()
{
// verify screenshot.
}

[Test]
[CreateScene(camera: true, light: true)]
[GizmosShowOnGameView(false)]
public void Attach_False_HideGizmos()
{
// verify screenshot.
}

[Test]
[CreateScene(camera: true, light: true)]
[GizmosShowOnGameView()]
public async Task AttachToAsyncTest_ShowGizmos()
{
await Task.Yield();
}

[UnityTest]
[CreateScene(camera: true, light: true)]
[GizmosShowOnGameView()]
public IEnumerator AttachToUnityTest_ShowGizmos()
{
yield return null;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions Tests/Runtime/Attributes/TakeScreenshotAttributeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,25 @@ public void AttachWithFilename_TakeScreenshotAndSaveToSpecifyPath_AfterRunningTe
$"{nameof(AttachWithFilename_TakeScreenshotAndSaveToSpecifyPath)}.png");
Assert.That(path, Does.Exist);
}

private class GizmoDemo : MonoBehaviour
{
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawSphere(transform.position, 0.2f);
}
}

[Test]
[LoadScene(TestScene)]
[TakeScreenshot(gizmos: true)]
public void AttachWithGizmos_TakeScreenshotWithGizmos()
{
var gameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
gameObject.AddComponent<GizmoDemo>();

// Take screenshot after running the test.
}
}
}

0 comments on commit c6993b6

Please sign in to comment.