From d5f1ee32ae14a2c900cf950ca8f5541c15803594 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Fri, 27 Oct 2023 22:52:17 +0900 Subject: [PATCH] Revert "Refactor GameViewResolutionAttribute using IOuterUnityTestAction" This reverts commit 2c0e34e8390c4fb03f66f6eda064cca10b0da496. --- README.md | 15 ++++++--- .../Attributes/GameViewResolutionAttribute.cs | 32 +++++++------------ .../GameViewResolutionAttributeTest.cs | 22 ++++--------- 3 files changed, 30 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 2b7fc95..4be8a55 100644 --- a/README.md +++ b/README.md @@ -47,26 +47,33 @@ public class MyTestClass `GameViewResolutionAttribute` is an NUnit test attribute class to set custom resolution to `GameView` before run test. This attribute can attached to test method, test class (`TestFixture`) and test assembly. -Can be used with sync Test, async Test and UnityTest. +Can be used with async Test and UnityTest. Usage: ```csharp +using System.Collections; using NUnit.Framework; using TestHelper.Attributes; +using UnityEngine.TestTools; [TestFixture] public class MyTestClass { - [Test] + [UnityTest] [GameViewResolution(640, 480, "VGA")] - public void MyTestMethod() + public IEnumerator MyTestMethod() { - // e.g., test using GraphicRaycaster, Graphics Tests Framework, etc... + yield return null; // wait for one frame to apply resolution. + + // e.g., test using GraphicRaycaster. } } ``` +> **Warning** +> Wait for one frame to apply resolution. + > **Note** > In batchmode, open `GameView` window. diff --git a/Runtime/Attributes/GameViewResolutionAttribute.cs b/Runtime/Attributes/GameViewResolutionAttribute.cs index 0c7f1fa..e840979 100644 --- a/Runtime/Attributes/GameViewResolutionAttribute.cs +++ b/Runtime/Attributes/GameViewResolutionAttribute.cs @@ -2,11 +2,10 @@ // This software is released under the MIT License. using System; -using System.Collections; using NUnit.Framework; using NUnit.Framework.Interfaces; +using NUnit.Framework.Internal; using UnityEngine; -using UnityEngine.TestTools; #if UNITY_EDITOR using TestHelper.Wrappers.UnityEditor; using UnityEditor; @@ -18,7 +17,7 @@ namespace TestHelper.Attributes /// Set GameView resolution before SetUp test. /// [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)] - public class GameViewResolutionAttribute : NUnitAttribute, IOuterUnityTestAction + public class GameViewResolutionAttribute : NUnitAttribute, IApplyToContext { private readonly uint _width; private readonly uint _height; @@ -46,6 +45,16 @@ public GameViewResolutionAttribute(GameViewResolution resolution) (_width, _height, _name) = resolution.GetParameter(); } + /// + public void ApplyToContext(ITestExecutionContext context) + { +#if UNITY_2022_2_OR_NEWER + SetResolutionUsingPlayModeWindow(); +#else + SetResolution(); +#endif + } + // ReSharper disable once UnusedMember.Local private void SetResolutionUsingPlayModeWindow() { @@ -97,22 +106,5 @@ private void SetResolution() gameView.SelectedSizeIndex(index); #endif } - - /// - public IEnumerator BeforeTest(ITest test) - { -#if UNITY_2022_2_OR_NEWER - SetResolutionUsingPlayModeWindow(); -#else - SetResolution(); -#endif - yield return null; - } - - /// - public IEnumerator AfterTest(ITest test) - { - yield return null; - } } } diff --git a/Tests/Runtime/Attributes/GameViewResolutionAttributeTest.cs b/Tests/Runtime/Attributes/GameViewResolutionAttributeTest.cs index 2c5732c..02ecaa6 100644 --- a/Tests/Runtime/Attributes/GameViewResolutionAttributeTest.cs +++ b/Tests/Runtime/Attributes/GameViewResolutionAttributeTest.cs @@ -13,32 +13,24 @@ namespace TestHelper.Attributes [UnityPlatform(RuntimePlatform.OSXEditor, RuntimePlatform.WindowsEditor, RuntimePlatform.LinuxEditor)] public class GameViewResolutionAttributeTest { - [Test, Order(0)] + [Test] [GameViewResolution(1920, 1080, "Full HD")] - public void Attach_SetScreenSizeToFullHD() - { - Assert.That(Screen.width, Is.EqualTo(1920)); - Assert.That(Screen.height, Is.EqualTo(1080)); - } - - [Test, Order(0)] - [GameViewResolution(GameViewResolution.XGA)] public async Task AttachToAsyncTest_SetScreenSizeToFullHD() { - Assert.That(Screen.width, Is.EqualTo(1024)); - Assert.That(Screen.height, Is.EqualTo(768)); + await Task.Yield(); // Wait to apply change GameView resolution - await Task.Yield(); // Not require awaiting before test + Assert.That(Screen.width, Is.EqualTo(1920)); + Assert.That(Screen.height, Is.EqualTo(1080)); } - [UnityTest] // Run last + [UnityTest] [GameViewResolution(GameViewResolution.VGA)] public IEnumerator AttachToUnityTest_SetScreenSizeToVGA() { + yield return null; // Wait to apply change GameView resolution + Assert.That(Screen.width, Is.EqualTo(640)); Assert.That(Screen.height, Is.EqualTo(480)); - - yield return null; // Not require awaiting before test } } }