Skip to content

Commit

Permalink
Merge pull request #26 from nowsprinting/fix/revert_gameviewresolutio…
Browse files Browse the repository at this point in the history
…nattribute

Revert "Refactor GameViewResolutionAttribute using IOuterUnityTestAction"
  • Loading branch information
nowsprinting committed Oct 27, 2023
2 parents 6b1cff9 + d5f1ee3 commit 40ef007
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 39 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
32 changes: 12 additions & 20 deletions Runtime/Attributes/GameViewResolutionAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,7 +17,7 @@ namespace TestHelper.Attributes
/// Set <c>GameView</c> resolution before SetUp test.
/// </summary>
[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;
Expand Down Expand Up @@ -46,6 +45,16 @@ public GameViewResolutionAttribute(GameViewResolution resolution)
(_width, _height, _name) = resolution.GetParameter();
}

/// <inheritdoc />
public void ApplyToContext(ITestExecutionContext context)
{
#if UNITY_2022_2_OR_NEWER
SetResolutionUsingPlayModeWindow();
#else
SetResolution();
#endif
}

// ReSharper disable once UnusedMember.Local
private void SetResolutionUsingPlayModeWindow()
{
Expand Down Expand Up @@ -97,22 +106,5 @@ private void SetResolution()
gameView.SelectedSizeIndex(index);
#endif
}

/// <inheritdoc />
public IEnumerator BeforeTest(ITest test)
{
#if UNITY_2022_2_OR_NEWER
SetResolutionUsingPlayModeWindow();
#else
SetResolution();
#endif
yield return null;
}

/// <inheritdoc />
public IEnumerator AfterTest(ITest test)
{
yield return null;
}
}
}
22 changes: 7 additions & 15 deletions Tests/Runtime/Attributes/GameViewResolutionAttributeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

0 comments on commit 40ef007

Please sign in to comment.