Skip to content

Commit

Permalink
Merge pull request #25 from nowsprinting/feature/refactor_gameviewres…
Browse files Browse the repository at this point in the history
…olutionattribute

Refactor GameViewResolutionAttribute using IOuterUnityTestAction
  • Loading branch information
nowsprinting committed Oct 26, 2023
2 parents b980305 + 2c0e34e commit 6b1cff9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
15 changes: 4 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,26 @@ 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 async Test and UnityTest.
Can be used with sync Test, async Test and UnityTest.

Usage:

```csharp
using System.Collections;
using NUnit.Framework;
using TestHelper.Attributes;
using UnityEngine.TestTools;

[TestFixture]
public class MyTestClass
{
[UnityTest]
[Test]
[GameViewResolution(640, 480, "VGA")]
public IEnumerator MyTestMethod()
public void MyTestMethod()
{
yield return null; // wait for one frame to apply resolution.
// e.g., test using GraphicRaycaster.
// e.g., test using GraphicRaycaster, Graphics Tests Framework, etc...
}
}
```

> **Warning**
> Wait for one frame to apply resolution.
> **Note**
> In batchmode, open `GameView` window.
Expand Down
32 changes: 20 additions & 12 deletions Runtime/Attributes/GameViewResolutionAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// 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 @@ -17,7 +18,7 @@ namespace TestHelper.Attributes
/// Set <c>GameView</c> resolution before SetUp test.
/// </summary>
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
public class GameViewResolutionAttribute : NUnitAttribute, IApplyToContext
public class GameViewResolutionAttribute : NUnitAttribute, IOuterUnityTestAction
{
private readonly uint _width;
private readonly uint _height;
Expand Down Expand Up @@ -45,16 +46,6 @@ 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 @@ -106,5 +97,22 @@ 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: 15 additions & 7 deletions Tests/Runtime/Attributes/GameViewResolutionAttributeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,32 @@ namespace TestHelper.Attributes
[UnityPlatform(RuntimePlatform.OSXEditor, RuntimePlatform.WindowsEditor, RuntimePlatform.LinuxEditor)]
public class GameViewResolutionAttributeTest
{
[Test]
[Test, Order(0)]
[GameViewResolution(1920, 1080, "Full HD")]
public async Task AttachToAsyncTest_SetScreenSizeToFullHD()
public void Attach_SetScreenSizeToFullHD()
{
await Task.Yield(); // Wait to apply change GameView resolution

Assert.That(Screen.width, Is.EqualTo(1920));
Assert.That(Screen.height, Is.EqualTo(1080));
}

[UnityTest]
[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(); // Not require awaiting before test
}

[UnityTest] // Run last
[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 6b1cff9

Please sign in to comment.