From 6ab1c753dfbfda3b67ddcbc04ad528a8d4c3d435 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Mon, 30 Oct 2023 21:58:49 +0900 Subject: [PATCH 1/4] Refactor --- RuntimeInternals/GameViewControlHelper.cs | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/RuntimeInternals/GameViewControlHelper.cs b/RuntimeInternals/GameViewControlHelper.cs index bd7c10d..20021eb 100644 --- a/RuntimeInternals/GameViewControlHelper.cs +++ b/RuntimeInternals/GameViewControlHelper.cs @@ -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; @@ -15,24 +13,13 @@ namespace TestHelper.RuntimeInternals /// public static class GameViewControlHelper { - private static Type s_gameView; - /// /// Focus GameView or SimulatorWindow. /// 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 - - s_gameView = assembly.GetType(viewClass); - } - - EditorWindow.GetWindow(s_gameView, false, null, true); + GameViewWrapper.GetWindow(); #endif } From 88f1e63e93ec0067bb4ebd46180218870c636961 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Mon, 30 Oct 2023 22:46:44 +0900 Subject: [PATCH 2/4] Add GizmosShowOnGameViewAttribute --- README.md | 26 +++++++ .../GizmosShowOnGameViewAttribute.cs | 46 ++++++++++++ .../GizmosShowOnGameViewAttribute.cs.meta | 3 + RuntimeInternals/GameViewControlHelper.cs | 32 +++++++++ .../Wrappers/UnityEditor/GameViewWrapper.cs | 23 +++++- .../GizmosShowOnGameViewAttributeTest.cs | 71 +++++++++++++++++++ .../GizmosShowOnGameViewAttributeTest.cs.meta | 3 + 7 files changed, 202 insertions(+), 2 deletions(-) create mode 100644 Runtime/Attributes/GizmosShowOnGameViewAttribute.cs create mode 100644 Runtime/Attributes/GizmosShowOnGameViewAttribute.cs.meta create mode 100644 Tests/Runtime/Attributes/GizmosShowOnGameViewAttributeTest.cs create mode 100644 Tests/Runtime/Attributes/GizmosShowOnGameViewAttributeTest.cs.meta diff --git a/README.md b/README.md index 4b8a6ce..bae6ef7 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,32 @@ 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. + } +} +``` + + #### IgnoreBatchMode `IgnoreBatchModeAttribute` is an NUnit test attribute class to skip test execution when run tests with `-batchmode` from the commandline. diff --git a/Runtime/Attributes/GizmosShowOnGameViewAttribute.cs b/Runtime/Attributes/GizmosShowOnGameViewAttribute.cs new file mode 100644 index 0000000..2a0ddf9 --- /dev/null +++ b/Runtime/Attributes/GizmosShowOnGameViewAttribute.cs @@ -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 +{ + /// + /// Show/ hide Gizmos on GameView during the test running. + /// + [AttributeUsage(AttributeTargets.Method)] + public class GizmosShowOnGameViewAttribute : NUnitAttribute, IOuterUnityTestAction + { + private readonly bool _show; + private readonly bool _beforeShow; + + /// + /// Show/ hide Gizmos on GameView during the test running. + /// + /// True: show Gizmos, False: hide Gizmos. + public GizmosShowOnGameViewAttribute(bool show = true) + { + _show = show; + _beforeShow = GameViewControlHelper.GetGizmos(); + } + + /// + public IEnumerator BeforeTest(ITest test) + { + GameViewControlHelper.SetGizmos(_show); + yield return null; + } + + /// + public IEnumerator AfterTest(ITest test) + { + GameViewControlHelper.SetGizmos(_beforeShow); + yield return null; + } + } +} diff --git a/Runtime/Attributes/GizmosShowOnGameViewAttribute.cs.meta b/Runtime/Attributes/GizmosShowOnGameViewAttribute.cs.meta new file mode 100644 index 0000000..4ffe685 --- /dev/null +++ b/Runtime/Attributes/GizmosShowOnGameViewAttribute.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 3a637ba305864a10abff5c260c115410 +timeCreated: 1698672259 \ No newline at end of file diff --git a/RuntimeInternals/GameViewControlHelper.cs b/RuntimeInternals/GameViewControlHelper.cs index 20021eb..72411cd 100644 --- a/RuntimeInternals/GameViewControlHelper.cs +++ b/RuntimeInternals/GameViewControlHelper.cs @@ -23,6 +23,38 @@ public static void Focus() #endif } + /// + /// Get Gizmos show/ hide status on GameView. + /// + /// True: show Gizmos, False: hide Gizmos. + public static bool GetGizmos() + { + var gizmos = false; +#if UNITY_EDITOR + var gameViewWrapper = GameViewWrapper.GetWindow(false); + if (gameViewWrapper != null) + { + gizmos = gameViewWrapper.GetGizmos(); + } +#endif + return gizmos; + } + + /// + /// Show/ hide Gizmos on GameView. + /// + /// True: show Gizmos, False: hide Gizmos. + public static void SetGizmos(bool show) + { +#if UNITY_EDITOR + var gameViewWrapper = GameViewWrapper.GetWindow(false); + if (gameViewWrapper != null) + { + gameViewWrapper.SetGizmos(show); + } +#endif + } + /// /// Set GameView resolution. /// diff --git a/RuntimeInternals/Wrappers/UnityEditor/GameViewWrapper.cs b/RuntimeInternals/Wrappers/UnityEditor/GameViewWrapper.cs index cca6004..ddb485e 100644 --- a/RuntimeInternals/Wrappers/UnityEditor/GameViewWrapper.cs +++ b/RuntimeInternals/Wrappers/UnityEditor/GameViewWrapper.cs @@ -21,6 +21,9 @@ 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) @@ -28,7 +31,7 @@ private GameViewWrapper(EditorWindow instance) _instance = instance; } - public static GameViewWrapper GetWindow() + public static GameViewWrapper GetWindow(bool focus = true) { if (s_gameView == null) { @@ -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."); @@ -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 diff --git a/Tests/Runtime/Attributes/GizmosShowOnGameViewAttributeTest.cs b/Tests/Runtime/Attributes/GizmosShowOnGameViewAttributeTest.cs new file mode 100644 index 0000000..0589fe1 --- /dev/null +++ b/Tests/Runtime/Attributes/GizmosShowOnGameViewAttributeTest.cs @@ -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(); + 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; + } + } +} diff --git a/Tests/Runtime/Attributes/GizmosShowOnGameViewAttributeTest.cs.meta b/Tests/Runtime/Attributes/GizmosShowOnGameViewAttributeTest.cs.meta new file mode 100644 index 0000000..3ad44aa --- /dev/null +++ b/Tests/Runtime/Attributes/GizmosShowOnGameViewAttributeTest.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 73971ef78bfc46199676e6a57b297512 +timeCreated: 1698673251 \ No newline at end of file From 5bc1f8c81b3bdcf1260737c92dc8d2125b969f38 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Mon, 30 Oct 2023 23:12:34 +0900 Subject: [PATCH 3/4] Fix README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index bae6ef7..75046d4 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,9 @@ public class MyTestClass } ``` +> **Note** +> In batchmode, open `GameView` window. + #### IgnoreBatchMode From b6dd4fddf9c7a158cd3414aafce330cfedbf708a Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Mon, 30 Oct 2023 23:35:42 +0900 Subject: [PATCH 4/4] Add show Gizmos argument to TakeScreenshotAttribute --- Runtime/Attributes/TakeScreenshotAttribute.cs | 22 ++++++++++++++++--- .../Attributes/TakeScreenshotAttributeTest.cs | 20 +++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/Runtime/Attributes/TakeScreenshotAttribute.cs b/Runtime/Attributes/TakeScreenshotAttribute.cs index 09f4785..3e036ca 100644 --- a/Runtime/Attributes/TakeScreenshotAttribute.cs +++ b/Runtime/Attributes/TakeScreenshotAttribute.cs @@ -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 { @@ -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; /// /// Take a screenshot and save it to file after running the test. @@ -38,17 +40,19 @@ public class TakeScreenshotAttribute : NUnitAttribute, IOuterUnityTestAction /// Filename to store screenshot. /// The factor to increase resolution with. /// The eye texture to capture when stereo rendering is enabled. + /// True: show Gizmos on GameView 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; } /// @@ -60,7 +64,19 @@ public IEnumerator BeforeTest(ITest test) /// 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); + } } } } diff --git a/Tests/Runtime/Attributes/TakeScreenshotAttributeTest.cs b/Tests/Runtime/Attributes/TakeScreenshotAttributeTest.cs index 664ad2c..efa8e0d 100644 --- a/Tests/Runtime/Attributes/TakeScreenshotAttributeTest.cs +++ b/Tests/Runtime/Attributes/TakeScreenshotAttributeTest.cs @@ -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(); + + // Take screenshot after running the test. + } } }