From 46ae78990f3c73a4598fcfcc959742a529384fc5 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Tue, 30 Apr 2024 17:40:00 +0900 Subject: [PATCH 1/3] Add SimulatorViewAttribute. supports set ScreenOrientation --- Runtime/Attributes/SimulatorViewAttribute.cs | 35 +++++++++++++++ .../Attributes/SimulatorViewAttribute.cs.meta | 2 + RuntimeInternals/GameViewControlHelper.cs | 2 +- .../SimulatorViewControlHelper.cs | 44 +++++++++++++++++++ .../SimulatorViewControlHelper.cs.meta | 11 +++++ .../Attributes/SimulatorViewAttributeTest.cs | 42 ++++++++++++++++++ .../SimulatorViewAttributeTest.cs.meta | 2 + .../SimulatorViewControlHelperTest.cs | 30 +++++++++++++ .../SimulatorViewControlHelperTest.cs.meta | 11 +++++ .../TestHelper.RuntimeInternals.Tests.asmdef | 1 + 10 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 Runtime/Attributes/SimulatorViewAttribute.cs create mode 100644 Runtime/Attributes/SimulatorViewAttribute.cs.meta create mode 100644 RuntimeInternals/SimulatorViewControlHelper.cs create mode 100644 RuntimeInternals/SimulatorViewControlHelper.cs.meta create mode 100644 Tests/Runtime/Attributes/SimulatorViewAttributeTest.cs create mode 100644 Tests/Runtime/Attributes/SimulatorViewAttributeTest.cs.meta create mode 100644 Tests/RuntimeInternals/SimulatorViewControlHelperTest.cs create mode 100644 Tests/RuntimeInternals/SimulatorViewControlHelperTest.cs.meta diff --git a/Runtime/Attributes/SimulatorViewAttribute.cs b/Runtime/Attributes/SimulatorViewAttribute.cs new file mode 100644 index 0000000..43a0c1f --- /dev/null +++ b/Runtime/Attributes/SimulatorViewAttribute.cs @@ -0,0 +1,35 @@ +// 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 +{ + /// + /// Set SimulatorView orientation before SetUp test. + /// + [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)] + public class SimulatorViewAttribute : NUnitAttribute, IApplyToContext + { + private readonly ScreenOrientation _orientation; + + /// + /// Set SimulatorView orientation before SetUp test. + /// + /// Set screen orientation + public SimulatorViewAttribute(ScreenOrientation orientation = ScreenOrientation.Portrait) + { + _orientation = orientation; + } + + public void ApplyToContext(ITestExecutionContext context) + { + SimulatorViewControlHelper.SetScreenOrientation(_orientation); + } + } +} diff --git a/Runtime/Attributes/SimulatorViewAttribute.cs.meta b/Runtime/Attributes/SimulatorViewAttribute.cs.meta new file mode 100644 index 0000000..f0705a6 --- /dev/null +++ b/Runtime/Attributes/SimulatorViewAttribute.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: b03db1ab479ec4082aa9a1a5399a2383 \ No newline at end of file diff --git a/RuntimeInternals/GameViewControlHelper.cs b/RuntimeInternals/GameViewControlHelper.cs index ed5d44b..891cda3 100644 --- a/RuntimeInternals/GameViewControlHelper.cs +++ b/RuntimeInternals/GameViewControlHelper.cs @@ -16,7 +16,7 @@ namespace TestHelper.RuntimeInternals public static class GameViewControlHelper { /// - /// Focus GameView or SimulatorWindow. + /// Focus GameView. /// public static void Focus() { diff --git a/RuntimeInternals/SimulatorViewControlHelper.cs b/RuntimeInternals/SimulatorViewControlHelper.cs new file mode 100644 index 0000000..0232d22 --- /dev/null +++ b/RuntimeInternals/SimulatorViewControlHelper.cs @@ -0,0 +1,44 @@ +// 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 +{ + /// + /// SimulatorView control helper. + /// This class can be used from the runtime code because it does not depend on test-framework. + /// + public static class SimulatorViewControlHelper + { + /// + /// Focus SimulatorView. + /// + 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 + } + + /// + /// Set ScreenOrientation on SimulatorView. + /// + /// Screen orientation. However, AutoRotation is ignored + 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 + } + } +} diff --git a/RuntimeInternals/SimulatorViewControlHelper.cs.meta b/RuntimeInternals/SimulatorViewControlHelper.cs.meta new file mode 100644 index 0000000..a9e4b85 --- /dev/null +++ b/RuntimeInternals/SimulatorViewControlHelper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3566c79b2819f40a1981063caa66a263 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Tests/Runtime/Attributes/SimulatorViewAttributeTest.cs b/Tests/Runtime/Attributes/SimulatorViewAttributeTest.cs new file mode 100644 index 0000000..17d5340 --- /dev/null +++ b/Tests/Runtime/Attributes/SimulatorViewAttributeTest.cs @@ -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)); + } + } +} diff --git a/Tests/Runtime/Attributes/SimulatorViewAttributeTest.cs.meta b/Tests/Runtime/Attributes/SimulatorViewAttributeTest.cs.meta new file mode 100644 index 0000000..2a82dff --- /dev/null +++ b/Tests/Runtime/Attributes/SimulatorViewAttributeTest.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 76dda6dad8f774ff28128e74f98c79a9 \ No newline at end of file diff --git a/Tests/RuntimeInternals/SimulatorViewControlHelperTest.cs b/Tests/RuntimeInternals/SimulatorViewControlHelperTest.cs new file mode 100644 index 0000000..14ed426 --- /dev/null +++ b/Tests/RuntimeInternals/SimulatorViewControlHelperTest.cs @@ -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)); + } + } +} diff --git a/Tests/RuntimeInternals/SimulatorViewControlHelperTest.cs.meta b/Tests/RuntimeInternals/SimulatorViewControlHelperTest.cs.meta new file mode 100644 index 0000000..fad8002 --- /dev/null +++ b/Tests/RuntimeInternals/SimulatorViewControlHelperTest.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ec16ac3a4551242ffb23cea2374bfe53 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Tests/RuntimeInternals/TestHelper.RuntimeInternals.Tests.asmdef b/Tests/RuntimeInternals/TestHelper.RuntimeInternals.Tests.asmdef index c095b95..b5c6fda 100644 --- a/Tests/RuntimeInternals/TestHelper.RuntimeInternals.Tests.asmdef +++ b/Tests/RuntimeInternals/TestHelper.RuntimeInternals.Tests.asmdef @@ -5,6 +5,7 @@ "UnityEngine.TestRunner", "UnityEditor.TestRunner", "TestHelper.RuntimeInternals", + "TestHelper", "UniTask" ], "includePlatforms": [], From 077cb205b38c678ac5a55a77e5bbdf990bc968d3 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Tue, 30 Apr 2024 18:26:09 +0900 Subject: [PATCH 2/3] Add comments --- Runtime/Attributes/SimulatorViewAttribute.cs | 2 ++ RuntimeInternals/SimulatorViewControlHelper.cs | 1 + 2 files changed, 3 insertions(+) diff --git a/Runtime/Attributes/SimulatorViewAttribute.cs b/Runtime/Attributes/SimulatorViewAttribute.cs index 43a0c1f..479ab9d 100644 --- a/Runtime/Attributes/SimulatorViewAttribute.cs +++ b/Runtime/Attributes/SimulatorViewAttribute.cs @@ -12,6 +12,7 @@ namespace TestHelper.Attributes { /// /// Set SimulatorView orientation before SetUp test. + /// This attribute works only with Unity 2022.2 or newer. (not support device simulator package) /// [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)] public class SimulatorViewAttribute : NUnitAttribute, IApplyToContext @@ -20,6 +21,7 @@ public class SimulatorViewAttribute : NUnitAttribute, IApplyToContext /// /// Set SimulatorView orientation before SetUp test. + /// This attribute works only with Unity 2022.2 or newer. (not support device simulator package) /// /// Set screen orientation public SimulatorViewAttribute(ScreenOrientation orientation = ScreenOrientation.Portrait) diff --git a/RuntimeInternals/SimulatorViewControlHelper.cs b/RuntimeInternals/SimulatorViewControlHelper.cs index 0232d22..b23b8db 100644 --- a/RuntimeInternals/SimulatorViewControlHelper.cs +++ b/RuntimeInternals/SimulatorViewControlHelper.cs @@ -12,6 +12,7 @@ namespace TestHelper.RuntimeInternals /// /// SimulatorView 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) /// public static class SimulatorViewControlHelper { From 3101a32c5b16893c0e8a22b8ff945b8e3cabb787 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Tue, 30 Apr 2024 18:31:35 +0900 Subject: [PATCH 3/3] Change coverage Unity version to Unity 2020 LTS --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1f8894e..803bbca 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -41,7 +41,7 @@ jobs: - 2022.3.17f1 - 2023.2.5f1 include: - - unityVersion: 2019.4.40f1 + - unityVersion: 2022.3.17f1 octocov: true steps: