Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
- 2022.3.17f1
- 2023.2.5f1
include:
- unityVersion: 2019.4.40f1
- unityVersion: 2022.3.17f1
octocov: true

steps:
Expand Down
37 changes: 37 additions & 0 deletions Runtime/Attributes/SimulatorViewAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2023-2024 Koji Hasegawa.
// This software is released under the MIT License.

using System;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using TestHelper.RuntimeInternals;
using UnityEngine;

namespace TestHelper.Attributes
{
/// <summary>
/// Set <c>SimulatorView</c> orientation before SetUp test.
/// This attribute works only with Unity 2022.2 or newer. (not support device simulator package)
/// </summary>
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
public class SimulatorViewAttribute : NUnitAttribute, IApplyToContext
{
private readonly ScreenOrientation _orientation;

/// <summary>
/// Set <c>SimulatorView</c> orientation before SetUp test.
/// This attribute works only with Unity 2022.2 or newer. (not support device simulator package)
/// </summary>
/// <param name="orientation">Set screen orientation</param>
public SimulatorViewAttribute(ScreenOrientation orientation = ScreenOrientation.Portrait)
{
_orientation = orientation;
}

public void ApplyToContext(ITestExecutionContext context)
{
SimulatorViewControlHelper.SetScreenOrientation(_orientation);
}
}
}
2 changes: 2 additions & 0 deletions Runtime/Attributes/SimulatorViewAttribute.cs.meta

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

2 changes: 1 addition & 1 deletion RuntimeInternals/GameViewControlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace TestHelper.RuntimeInternals
public static class GameViewControlHelper
{
/// <summary>
/// Focus <c>GameView</c> or <c>SimulatorWindow</c>.
/// Focus <c>GameView</c>.
/// </summary>
public static void Focus()
{
Expand Down
45 changes: 45 additions & 0 deletions RuntimeInternals/SimulatorViewControlHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2023-2024 Koji Hasegawa.
// This software is released under the MIT License.

using UnityEngine;
#if UNITY_EDITOR && UNITY_2022_2_OR_NEWER
using UnityEditor;
using Screen = UnityEngine.Device.Screen; // defined in UnityEditor.DeviceSimulatorModule module
#endif

namespace TestHelper.RuntimeInternals
{
/// <summary>
/// <c>SimulatorView</c> control helper.
/// This class can be used from the runtime code because it does not depend on test-framework.
/// This class works only with Unity 2022.2 or newer. (not support device simulator package)
/// </summary>
public static class SimulatorViewControlHelper
{
/// <summary>
/// Focus <c>SimulatorView</c>.
/// </summary>
public static void Focus()
{
#if UNITY_EDITOR && UNITY_2022_2_OR_NEWER
PlayModeWindow.SetViewType(PlayModeWindow.PlayModeViewTypes.SimulatorView);
#else
Debug.LogError("SimulatorView is not supported.");
#endif
}

/// <summary>
/// Set <c>ScreenOrientation</c> on <c>SimulatorView</c>.
/// </summary>
/// <param name="orientation">Screen orientation. However, <c>AutoRotation</c> is ignored</param>
public static void SetScreenOrientation(ScreenOrientation orientation)
{
#if UNITY_EDITOR && UNITY_2022_2_OR_NEWER
PlayModeWindow.SetViewType(PlayModeWindow.PlayModeViewTypes.SimulatorView);
Screen.orientation = orientation; // UnityEngine.Device.Screen
#else
Debug.LogError("SimulatorView is not supported.");
#endif
}
}
}
11 changes: 11 additions & 0 deletions RuntimeInternals/SimulatorViewControlHelper.cs.meta

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

42 changes: 42 additions & 0 deletions Tests/Runtime/Attributes/SimulatorViewAttributeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2023-2024 Koji Hasegawa.
// This software is released under the MIT License.

using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using NUnit.Framework;
using TestHelper.RuntimeInternals;
using UnityEngine;
using UnityEngine.TestTools;

namespace TestHelper.Attributes
{
[TestFixture]
[UnityPlatform(RuntimePlatform.OSXEditor, RuntimePlatform.WindowsEditor, RuntimePlatform.LinuxEditor)]
[UnityVersion("2022.2")]
public class SimulatorViewAttributeTest
{
[OneTimeTearDown]
public void OneTimeTearDown()
{
SimulatorViewControlHelper.SetScreenOrientation(ScreenOrientation.Portrait);
}

[Test]
[SimulatorView(ScreenOrientation.PortraitUpsideDown)]
public async Task Attach_ScreenOrientation_PortraitUpsideDown()
{
await UniTask.NextFrame(); // Wait to apply change SimulatorView

Assert.That(Screen.orientation, Is.EqualTo(ScreenOrientation.PortraitUpsideDown));
}

[Test]
[SimulatorView(ScreenOrientation.LandscapeLeft)]
public async Task Attach_ScreenOrientation_LandscapeLeft()
{
await UniTask.NextFrame(); // Wait to apply change SimulatorView

Assert.That(Screen.orientation, Is.EqualTo(ScreenOrientation.LandscapeLeft));
}
}
}
2 changes: 2 additions & 0 deletions Tests/Runtime/Attributes/SimulatorViewAttributeTest.cs.meta

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

30 changes: 30 additions & 0 deletions Tests/RuntimeInternals/SimulatorViewControlHelperTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2023-2024 Koji Hasegawa.
// This software is released under the MIT License.

using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using NUnit.Framework;
using TestHelper.Attributes;
using UnityEngine;
using UnityEngine.TestTools;

namespace TestHelper.RuntimeInternals
{
[TestFixture]
[UnityPlatform(RuntimePlatform.OSXEditor, RuntimePlatform.WindowsEditor, RuntimePlatform.LinuxEditor)]
[UnityVersion("2022.2")]
public class SimulatorViewControlHelperTest
{
[TestCase(ScreenOrientation.Portrait)]
[TestCase(ScreenOrientation.PortraitUpsideDown)]
[TestCase(ScreenOrientation.LandscapeLeft)]
[TestCase(ScreenOrientation.LandscapeRight)]
public async Task SetScreenOrientation_RotateScreen(ScreenOrientation orientation)
{
SimulatorViewControlHelper.SetScreenOrientation(orientation);
await UniTask.NextFrame();

Assert.That(Screen.orientation, Is.EqualTo(orientation));
}
}
}
11 changes: 11 additions & 0 deletions Tests/RuntimeInternals/SimulatorViewControlHelperTest.cs.meta

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

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"TestHelper.RuntimeInternals",
"TestHelper",
"UniTask"
],
"includePlatforms": [],
Expand Down