From 2bfb2af333e48d8169445599e2704c1d82df16f0 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Tue, 29 Jun 2021 14:28:51 +0200 Subject: [PATCH 1/5] added SentryMonoBehaviourTests --- src/Sentry.Unity/Integrations/IApplication.cs | 3 + .../Integrations/SessionIntegration.cs | 2 +- ...auseListener.cs => SentryMonoBehaviour.cs} | 22 ++- .../SentryMonoBehaviourTests.cs | 131 ++++++++++++++++++ .../Stubs/TestApplication.cs | 3 +- 5 files changed, 153 insertions(+), 8 deletions(-) rename src/Sentry.Unity/{ApplicationPauseListener.cs => SentryMonoBehaviour.cs} (82%) create mode 100644 test/Sentry.Unity.Tests/SentryMonoBehaviourTests.cs diff --git a/src/Sentry.Unity/Integrations/IApplication.cs b/src/Sentry.Unity/Integrations/IApplication.cs index b172a96eb..8d75ca67b 100644 --- a/src/Sentry.Unity/Integrations/IApplication.cs +++ b/src/Sentry.Unity/Integrations/IApplication.cs @@ -15,6 +15,7 @@ internal interface IApplication string Version { get; } string PersistentDataPath { get; } bool IsMainThread { get; } + RuntimePlatform Platform { get; } } internal sealed class ApplicationAdapter : IApplication @@ -43,6 +44,8 @@ private ApplicationAdapter() public bool IsMainThread => Thread.CurrentThread.ManagedThreadId == 1; + public RuntimePlatform Platform => Application.platform; + private void OnLogMessageReceived(string condition, string stackTrace, LogType type) => LogMessageReceived?.Invoke(condition, stackTrace, type); diff --git a/src/Sentry.Unity/Integrations/SessionIntegration.cs b/src/Sentry.Unity/Integrations/SessionIntegration.cs index 010349a17..7410da93a 100644 --- a/src/Sentry.Unity/Integrations/SessionIntegration.cs +++ b/src/Sentry.Unity/Integrations/SessionIntegration.cs @@ -17,7 +17,7 @@ public void Register(IHub hub, SentryOptions options) // HideFlags.HideAndDontSave hides the GameObject in the hierarchy and prevents changing of scenes from destroying it var gameListenerObject = new GameObject("SentryListener") {hideFlags = HideFlags.HideAndDontSave}; - var gameListener = gameListenerObject.AddComponent(); + var gameListener = gameListenerObject.AddComponent(); gameListener.ApplicationResuming += () => { options.DiagnosticLogger?.LogDebug("Resuming session."); diff --git a/src/Sentry.Unity/ApplicationPauseListener.cs b/src/Sentry.Unity/SentryMonoBehaviour.cs similarity index 82% rename from src/Sentry.Unity/ApplicationPauseListener.cs rename to src/Sentry.Unity/SentryMonoBehaviour.cs index 668463018..d34e94df1 100644 --- a/src/Sentry.Unity/ApplicationPauseListener.cs +++ b/src/Sentry.Unity/SentryMonoBehaviour.cs @@ -1,4 +1,5 @@ using System; +using Sentry.Unity.Integrations; using UnityEngine; namespace Sentry.Unity @@ -7,7 +8,7 @@ namespace Sentry.Unity /// A MonoBehavior used to forward application focus events to subscribers. /// [DefaultExecutionOrder(-900)] - internal class ApplicationPauseListener : MonoBehaviour + internal class SentryMonoBehaviour : MonoBehaviour { /// /// Hook to receive an event when the application gains focus. @@ -28,6 +29,17 @@ internal class ApplicationPauseListener : MonoBehaviour // Keeping internal track of running state because OnApplicationPause and OnApplicationFocus get called during startup and would fire false resume events private bool _isRunning = true; + private IApplication? _application; + internal IApplication Application + { + get + { + _application ??= ApplicationAdapter.Instance; + return _application; + } + set => _application = value; + } + /// /// To receive Leaving/Resuming events on Android. /// @@ -37,9 +49,9 @@ internal class ApplicationPauseListener : MonoBehaviour /// /// /// - private void OnApplicationPause(bool pauseStatus) + internal void OnApplicationPause(bool pauseStatus) { - if (Application.platform != RuntimePlatform.Android) + if (Application.Platform != RuntimePlatform.Android) { return; } @@ -60,10 +72,10 @@ private void OnApplicationPause(bool pauseStatus) /// To receive Leaving/Resuming events on all platforms except Android. /// /// - private void OnApplicationFocus(bool hasFocus) + internal void OnApplicationFocus(bool hasFocus) { // To avoid event duplication on Android since the pause event will be handled via OnApplicationPause - if (Application.platform == RuntimePlatform.Android) + if (Application.Platform == RuntimePlatform.Android) { return; } diff --git a/test/Sentry.Unity.Tests/SentryMonoBehaviourTests.cs b/test/Sentry.Unity.Tests/SentryMonoBehaviourTests.cs new file mode 100644 index 000000000..c53a01a72 --- /dev/null +++ b/test/Sentry.Unity.Tests/SentryMonoBehaviourTests.cs @@ -0,0 +1,131 @@ +using NUnit.Framework; +using Sentry.Unity.Tests.Stubs; +using UnityEngine; + +namespace Sentry.Unity.Tests +{ + public class SentryMonoBehaviourTests + { + [Test] + public void OnApplicationPause_OnAndroid_ApplicationPausingTriggered() + { + var wasPausingCalled = false; + + var gameObject = new GameObject("PauseTest"); + var listener = gameObject.AddComponent(); + listener.Application = new TestApplication {Platform = RuntimePlatform.Android}; + listener.ApplicationPausing += () => wasPausingCalled = true; + + listener.OnApplicationPause(true); + + Assert.IsTrue(wasPausingCalled); + } + + [Test] + public void OnApplicationPause_NotOnAndroid_ApplicationPausingNotTriggered() + { + var wasPausingCalled = false; + + var gameObject = new GameObject("PauseTest"); + var listener = gameObject.AddComponent(); + listener.Application = new TestApplication {Platform = RuntimePlatform.IPhonePlayer}; + listener.ApplicationPausing += () => wasPausingCalled = true; + + listener.OnApplicationPause(true); + + Assert.IsFalse(wasPausingCalled); + } + + [Test] + public void OnApplicationPause_OnAndroid_ApplicationPausingTriggeredOnlyOnce() + { + var pauseEventTriggerCounter = 0; + + var gameObject = new GameObject("PauseTest"); + var listener = gameObject.AddComponent(); + listener.Application = new TestApplication {Platform = RuntimePlatform.Android}; + listener.ApplicationPausing += () => pauseEventTriggerCounter++; + + listener.OnApplicationPause(true); + listener.OnApplicationPause(true); + + Assert.AreEqual(1, pauseEventTriggerCounter); + } + + [Test] + public void OnApplicationPause_OnAndroid_PausingIsRequiredBeforeApplicationResumingTrigger() + { + var wasPausingCalled = false; + var wasResumingCalled = false; + + var gameObject = new GameObject("PauseTest"); + var listener = gameObject.AddComponent(); + listener.Application = new TestApplication {Platform = RuntimePlatform.Android}; + listener.ApplicationPausing += () => wasPausingCalled = true; + listener.ApplicationResuming += () => wasResumingCalled = true; + + listener.OnApplicationPause(false); + + Assert.IsFalse(wasResumingCalled); + + listener.OnApplicationPause(true); + listener.OnApplicationPause(false); + + Assert.IsTrue(wasPausingCalled); + Assert.IsTrue(wasResumingCalled); + } + + [Test] + public void OnApplicationFocus_OnAndroid_ApplicationPausingNotTriggered() + { + var wasPausingCalled = false; + + var gameObject = new GameObject("PauseTest"); + var listener = gameObject.AddComponent(); + listener.Application = new TestApplication {Platform = RuntimePlatform.Android}; + listener.ApplicationPausing += () => wasPausingCalled = true; + + listener.OnApplicationFocus(false); + + Assert.IsFalse(wasPausingCalled); + } + + [Test] + public void OnApplicationFocus_NotOnAndroid_ApplicationPausingTriggered() + { + var wasPausingCalled = false; + + var gameObject = new GameObject("PauseTest"); + var listener = gameObject.AddComponent(); + listener.Application = new TestApplication {Platform = RuntimePlatform.IPhonePlayer}; + listener.ApplicationPausing += () => wasPausingCalled = true; + + listener.OnApplicationFocus(false); + + Assert.IsTrue(wasPausingCalled); + } + + [Test] + public void OnApplicationFocus_NotOnAndroid_PausingIsRequiredBeforeApplicationResumingTrigger() + { + var wasPausingCalled = false; + var wasResumingCalled = false; + + var gameObject = new GameObject("PauseTest"); + var listener = gameObject.AddComponent(); + listener.Application = new TestApplication {Platform = RuntimePlatform.IPhonePlayer}; + listener.ApplicationPausing += () => wasPausingCalled = true; + listener.ApplicationResuming += () => wasResumingCalled = true; + + listener.OnApplicationFocus(true); + + Assert.IsFalse(wasResumingCalled); + + listener.OnApplicationFocus(false); + listener.OnApplicationFocus(true); + + Assert.IsTrue(wasPausingCalled); + Assert.IsTrue(wasResumingCalled); + } + } +} diff --git a/test/Sentry.Unity.Tests/Stubs/TestApplication.cs b/test/Sentry.Unity.Tests/Stubs/TestApplication.cs index b7195cc80..867c49bab 100644 --- a/test/Sentry.Unity.Tests/Stubs/TestApplication.cs +++ b/test/Sentry.Unity.Tests/Stubs/TestApplication.cs @@ -1,7 +1,6 @@ using System; using Sentry.Unity.Integrations; using UnityEngine; -using UnityEngine.TestTools.Constraints; namespace Sentry.Unity.Tests.Stubs { @@ -29,7 +28,7 @@ public TestApplication( public string Version { get; } public string PersistentDataPath { get; } public bool IsMainThread { get; } - + public RuntimePlatform Platform { get; set; } private void OnQuitting() => Quitting?.Invoke(); private void OnLogMessageReceived(string condition, string stacktrace, LogType type) From 35a64206352424b8143d2beca5367155478e467e Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Tue, 29 Jun 2021 15:00:11 +0200 Subject: [PATCH 2/5] started on session integration test --- test/Sentry.Unity.Tests/IntegrationTests.cs | 4 +-- .../SessionIntegrationTests.cs | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 test/Sentry.Unity.Tests/SessionIntegrationTests.cs diff --git a/test/Sentry.Unity.Tests/IntegrationTests.cs b/test/Sentry.Unity.Tests/IntegrationTests.cs index c9628411c..830d34bd1 100644 --- a/test/Sentry.Unity.Tests/IntegrationTests.cs +++ b/test/Sentry.Unity.Tests/IntegrationTests.cs @@ -232,7 +232,7 @@ public IEnumerator Init_OptionsAreDefaulted() UnitySentryOptionsTests.AssertOptions(expectedOptions, actualOptions!); } - private static IEnumerator SetupSceneCoroutine(string sceneName) + internal static IEnumerator SetupSceneCoroutine(string sceneName) { // load scene with initialized Sentry, SceneManager.LoadSceneAsync(sceneName); SceneManager.LoadScene(sceneName); @@ -244,7 +244,7 @@ private static IEnumerator SetupSceneCoroutine(string sceneName) LogAssert.ignoreFailingMessages = true; } - private static IDisposable InitSentrySdk(Action configure) + internal static IDisposable InitSentrySdk(Action configure) { SentryUnity.Init(options => { diff --git a/test/Sentry.Unity.Tests/SessionIntegrationTests.cs b/test/Sentry.Unity.Tests/SessionIntegrationTests.cs new file mode 100644 index 000000000..9c77b6950 --- /dev/null +++ b/test/Sentry.Unity.Tests/SessionIntegrationTests.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections; +using System.Linq; +using NUnit.Framework; +using Sentry.Unity.Integrations; +using Sentry.Unity.Tests.TestBehaviours; +using UnityEngine; +using UnityEngine.TestTools; + +namespace Sentry.Unity.Tests +{ + public class SessionIntegrationTests + { + [UnityTest] + public IEnumerator SessionIntegration_SentryMonoBehaviourCreated() + { + yield return null; + + using var _ = IntegrationTests.InitSentrySdk(o => + { + o.AutoSessionTracking = true; + o.AutoSessionTrackingInterval = TimeSpan.FromMilliseconds(10); + }); + var testBehaviour = GameObject.FindObjectOfType(); + + Assert.IsNotNull(testBehaviour); + } + } +} From 9309f8dee1728c682309b97a223375cef6815bbb Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Tue, 29 Jun 2021 19:51:52 +0200 Subject: [PATCH 3/5] cleanup --- .../Integrations/SessionIntegration.cs | 2 +- src/Sentry.Unity/SentryOptionsUtility.cs | 1 - test/Sentry.Unity.Tests/IntegrationTests.cs | 1 + .../SentryMonoBehaviourTests.cs | 30 ++++++++++++++----- .../SessionIntegrationTests.cs | 28 ++++++++--------- .../Stubs/TestApplication.cs | 7 +++-- 6 files changed, 43 insertions(+), 26 deletions(-) diff --git a/src/Sentry.Unity/Integrations/SessionIntegration.cs b/src/Sentry.Unity/Integrations/SessionIntegration.cs index 7410da93a..0ec43c5ae 100644 --- a/src/Sentry.Unity/Integrations/SessionIntegration.cs +++ b/src/Sentry.Unity/Integrations/SessionIntegration.cs @@ -16,7 +16,7 @@ public void Register(IHub hub, SentryOptions options) options.DiagnosticLogger?.LogDebug("Registering Session integration."); // HideFlags.HideAndDontSave hides the GameObject in the hierarchy and prevents changing of scenes from destroying it - var gameListenerObject = new GameObject("SentryListener") {hideFlags = HideFlags.HideAndDontSave}; + var gameListenerObject = new GameObject("SentryMonoBehaviour") {hideFlags = HideFlags.HideAndDontSave}; var gameListener = gameListenerObject.AddComponent(); gameListener.ApplicationResuming += () => { diff --git a/src/Sentry.Unity/SentryOptionsUtility.cs b/src/Sentry.Unity/SentryOptionsUtility.cs index a3ee3d168..60c2f1375 100644 --- a/src/Sentry.Unity/SentryOptionsUtility.cs +++ b/src/Sentry.Unity/SentryOptionsUtility.cs @@ -1,4 +1,3 @@ -using System; using Sentry.Unity.Integrations; namespace Sentry.Unity diff --git a/test/Sentry.Unity.Tests/IntegrationTests.cs b/test/Sentry.Unity.Tests/IntegrationTests.cs index 830d34bd1..c47fbf0fe 100644 --- a/test/Sentry.Unity.Tests/IntegrationTests.cs +++ b/test/Sentry.Unity.Tests/IntegrationTests.cs @@ -249,6 +249,7 @@ internal static IDisposable InitSentrySdk(Action configure) SentryUnity.Init(options => { options.Dsn = "https://94677106febe46b88b9b9ae5efd18a00@o447951.ingest.sentry.io/5439417"; + configure.Invoke(options); }); return new SentryDisposable(); diff --git a/test/Sentry.Unity.Tests/SentryMonoBehaviourTests.cs b/test/Sentry.Unity.Tests/SentryMonoBehaviourTests.cs index c53a01a72..020f8a656 100644 --- a/test/Sentry.Unity.Tests/SentryMonoBehaviourTests.cs +++ b/test/Sentry.Unity.Tests/SentryMonoBehaviourTests.cs @@ -13,7 +13,7 @@ public void OnApplicationPause_OnAndroid_ApplicationPausingTriggered() var gameObject = new GameObject("PauseTest"); var listener = gameObject.AddComponent(); - listener.Application = new TestApplication {Platform = RuntimePlatform.Android}; + listener.Application = new TestApplication (platform: RuntimePlatform.Android); listener.ApplicationPausing += () => wasPausingCalled = true; listener.OnApplicationPause(true); @@ -28,7 +28,7 @@ public void OnApplicationPause_NotOnAndroid_ApplicationPausingNotTriggered() var gameObject = new GameObject("PauseTest"); var listener = gameObject.AddComponent(); - listener.Application = new TestApplication {Platform = RuntimePlatform.IPhonePlayer}; + listener.Application = new TestApplication (platform: RuntimePlatform.IPhonePlayer); listener.ApplicationPausing += () => wasPausingCalled = true; listener.OnApplicationPause(true); @@ -43,7 +43,7 @@ public void OnApplicationPause_OnAndroid_ApplicationPausingTriggeredOnlyOnce() var gameObject = new GameObject("PauseTest"); var listener = gameObject.AddComponent(); - listener.Application = new TestApplication {Platform = RuntimePlatform.Android}; + listener.Application = new TestApplication (platform: RuntimePlatform.Android); listener.ApplicationPausing += () => pauseEventTriggerCounter++; listener.OnApplicationPause(true); @@ -60,7 +60,7 @@ public void OnApplicationPause_OnAndroid_PausingIsRequiredBeforeApplicationResum var gameObject = new GameObject("PauseTest"); var listener = gameObject.AddComponent(); - listener.Application = new TestApplication {Platform = RuntimePlatform.Android}; + listener.Application = new TestApplication (platform: RuntimePlatform.Android); listener.ApplicationPausing += () => wasPausingCalled = true; listener.ApplicationResuming += () => wasResumingCalled = true; @@ -82,7 +82,7 @@ public void OnApplicationFocus_OnAndroid_ApplicationPausingNotTriggered() var gameObject = new GameObject("PauseTest"); var listener = gameObject.AddComponent(); - listener.Application = new TestApplication {Platform = RuntimePlatform.Android}; + listener.Application = new TestApplication (platform: RuntimePlatform.Android); listener.ApplicationPausing += () => wasPausingCalled = true; listener.OnApplicationFocus(false); @@ -97,7 +97,7 @@ public void OnApplicationFocus_NotOnAndroid_ApplicationPausingTriggered() var gameObject = new GameObject("PauseTest"); var listener = gameObject.AddComponent(); - listener.Application = new TestApplication {Platform = RuntimePlatform.IPhonePlayer}; + listener.Application = new TestApplication (platform: RuntimePlatform.IPhonePlayer); listener.ApplicationPausing += () => wasPausingCalled = true; listener.OnApplicationFocus(false); @@ -105,6 +105,22 @@ public void OnApplicationFocus_NotOnAndroid_ApplicationPausingTriggered() Assert.IsTrue(wasPausingCalled); } + [Test] + public void OnApplicationFocus_NotOnAndroid_ApplicationPausingTriggeredOnlyOnce() + { + var pauseEventTriggerCounter = 0; + + var gameObject = new GameObject("PauseTest"); + var listener = gameObject.AddComponent(); + listener.Application = new TestApplication (platform: RuntimePlatform.IPhonePlayer); + listener.ApplicationPausing += () => pauseEventTriggerCounter++; + + listener.OnApplicationFocus(false); + listener.OnApplicationFocus(false); + + Assert.AreEqual(1, pauseEventTriggerCounter); + } + [Test] public void OnApplicationFocus_NotOnAndroid_PausingIsRequiredBeforeApplicationResumingTrigger() { @@ -113,7 +129,7 @@ public void OnApplicationFocus_NotOnAndroid_PausingIsRequiredBeforeApplicationRe var gameObject = new GameObject("PauseTest"); var listener = gameObject.AddComponent(); - listener.Application = new TestApplication {Platform = RuntimePlatform.IPhonePlayer}; + listener.Application = new TestApplication (platform: RuntimePlatform.IPhonePlayer); listener.ApplicationPausing += () => wasPausingCalled = true; listener.ApplicationResuming += () => wasResumingCalled = true; diff --git a/test/Sentry.Unity.Tests/SessionIntegrationTests.cs b/test/Sentry.Unity.Tests/SessionIntegrationTests.cs index 9c77b6950..9090056b8 100644 --- a/test/Sentry.Unity.Tests/SessionIntegrationTests.cs +++ b/test/Sentry.Unity.Tests/SessionIntegrationTests.cs @@ -1,9 +1,5 @@ -using System; using System.Collections; -using System.Linq; using NUnit.Framework; -using Sentry.Unity.Integrations; -using Sentry.Unity.Tests.TestBehaviours; using UnityEngine; using UnityEngine.TestTools; @@ -11,19 +7,21 @@ namespace Sentry.Unity.Tests { public class SessionIntegrationTests { - [UnityTest] - public IEnumerator SessionIntegration_SentryMonoBehaviourCreated() + [UnityTest] + public IEnumerator SessionIntegration_Init_SentryMonoBehaviourCreated() + { + yield return null; + + using var _ = IntegrationTests.InitSentrySdk(o => { - yield return null; + // o.AutoSessionTracking = true; We expect this to be true by default + }); - using var _ = IntegrationTests.InitSentrySdk(o => - { - o.AutoSessionTracking = true; - o.AutoSessionTrackingInterval = TimeSpan.FromMilliseconds(10); - }); - var testBehaviour = GameObject.FindObjectOfType(); + var sentryGameObject = GameObject.Find("SentryMonoBehaviour"); + var sentryMonoBehaviour = sentryGameObject.GetComponent(); - Assert.IsNotNull(testBehaviour); - } + Assert.IsNotNull(sentryGameObject); + Assert.IsNotNull(sentryMonoBehaviour); + } } } diff --git a/test/Sentry.Unity.Tests/Stubs/TestApplication.cs b/test/Sentry.Unity.Tests/Stubs/TestApplication.cs index 867c49bab..d16e33388 100644 --- a/test/Sentry.Unity.Tests/Stubs/TestApplication.cs +++ b/test/Sentry.Unity.Tests/Stubs/TestApplication.cs @@ -11,13 +11,16 @@ public TestApplication( string productName = "", string version = "", string persistentDataPath = "", - bool isMainThread = true) + bool isMainThread = true, + RuntimePlatform platform = RuntimePlatform.WindowsEditor + ) { IsEditor = isEditor; ProductName = productName; Version = version; PersistentDataPath = persistentDataPath; IsMainThread = isMainThread; + Platform = platform; } public event Application.LogCallback? LogMessageReceived; @@ -28,7 +31,7 @@ public TestApplication( public string Version { get; } public string PersistentDataPath { get; } public bool IsMainThread { get; } - public RuntimePlatform Platform { get; set; } + public RuntimePlatform Platform { get; } private void OnQuitting() => Quitting?.Invoke(); private void OnLogMessageReceived(string condition, string stacktrace, LogType type) From 7620188258ea1a2c5aebb28012c1a53b6984618c Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Wed, 30 Jun 2021 11:43:59 +0200 Subject: [PATCH 4/5] moved setup to fixture --- .../SentryMonoBehaviourTests.cs | 103 ++++++++++-------- 1 file changed, 55 insertions(+), 48 deletions(-) diff --git a/test/Sentry.Unity.Tests/SentryMonoBehaviourTests.cs b/test/Sentry.Unity.Tests/SentryMonoBehaviourTests.cs index 020f8a656..e34c97b8a 100644 --- a/test/Sentry.Unity.Tests/SentryMonoBehaviourTests.cs +++ b/test/Sentry.Unity.Tests/SentryMonoBehaviourTests.cs @@ -6,17 +6,38 @@ namespace Sentry.Unity.Tests { public class SentryMonoBehaviourTests { + private class Fixture + { + public SentryMonoBehaviour GetSut(RuntimePlatform platform) + { + var gameObject = new GameObject("PauseTest"); + var sentryMonoBehaviour = gameObject.AddComponent(); + sentryMonoBehaviour.Application = new TestApplication(platform: platform); + + return sentryMonoBehaviour; + } + } + + private Fixture _fixture = null!; + + + + + [SetUp] + public void SetUp() + { + _fixture = new Fixture(); + } + [Test] public void OnApplicationPause_OnAndroid_ApplicationPausingTriggered() { var wasPausingCalled = false; - var gameObject = new GameObject("PauseTest"); - var listener = gameObject.AddComponent(); - listener.Application = new TestApplication (platform: RuntimePlatform.Android); - listener.ApplicationPausing += () => wasPausingCalled = true; + var sut = _fixture.GetSut(RuntimePlatform.Android); + sut.ApplicationPausing += () => wasPausingCalled = true; - listener.OnApplicationPause(true); + sut.OnApplicationPause(true); Assert.IsTrue(wasPausingCalled); } @@ -26,12 +47,10 @@ public void OnApplicationPause_NotOnAndroid_ApplicationPausingNotTriggered() { var wasPausingCalled = false; - var gameObject = new GameObject("PauseTest"); - var listener = gameObject.AddComponent(); - listener.Application = new TestApplication (platform: RuntimePlatform.IPhonePlayer); - listener.ApplicationPausing += () => wasPausingCalled = true; + var sut = _fixture.GetSut(RuntimePlatform.IPhonePlayer); + sut.ApplicationPausing += () => wasPausingCalled = true; - listener.OnApplicationPause(true); + sut.OnApplicationPause(true); Assert.IsFalse(wasPausingCalled); } @@ -41,13 +60,11 @@ public void OnApplicationPause_OnAndroid_ApplicationPausingTriggeredOnlyOnce() { var pauseEventTriggerCounter = 0; - var gameObject = new GameObject("PauseTest"); - var listener = gameObject.AddComponent(); - listener.Application = new TestApplication (platform: RuntimePlatform.Android); - listener.ApplicationPausing += () => pauseEventTriggerCounter++; + var sut = _fixture.GetSut(RuntimePlatform.Android); + sut.ApplicationPausing += () => pauseEventTriggerCounter++; - listener.OnApplicationPause(true); - listener.OnApplicationPause(true); + sut.OnApplicationPause(true); + sut.OnApplicationPause(true); Assert.AreEqual(1, pauseEventTriggerCounter); } @@ -58,18 +75,16 @@ public void OnApplicationPause_OnAndroid_PausingIsRequiredBeforeApplicationResum var wasPausingCalled = false; var wasResumingCalled = false; - var gameObject = new GameObject("PauseTest"); - var listener = gameObject.AddComponent(); - listener.Application = new TestApplication (platform: RuntimePlatform.Android); - listener.ApplicationPausing += () => wasPausingCalled = true; - listener.ApplicationResuming += () => wasResumingCalled = true; + var sut = _fixture.GetSut(RuntimePlatform.Android); + sut.ApplicationPausing += () => wasPausingCalled = true; + sut.ApplicationResuming += () => wasResumingCalled = true; - listener.OnApplicationPause(false); + sut.OnApplicationPause(false); Assert.IsFalse(wasResumingCalled); - listener.OnApplicationPause(true); - listener.OnApplicationPause(false); + sut.OnApplicationPause(true); + sut.OnApplicationPause(false); Assert.IsTrue(wasPausingCalled); Assert.IsTrue(wasResumingCalled); @@ -80,12 +95,10 @@ public void OnApplicationFocus_OnAndroid_ApplicationPausingNotTriggered() { var wasPausingCalled = false; - var gameObject = new GameObject("PauseTest"); - var listener = gameObject.AddComponent(); - listener.Application = new TestApplication (platform: RuntimePlatform.Android); - listener.ApplicationPausing += () => wasPausingCalled = true; + var sut = _fixture.GetSut(RuntimePlatform.Android); + sut.ApplicationPausing += () => wasPausingCalled = true; - listener.OnApplicationFocus(false); + sut.OnApplicationFocus(false); Assert.IsFalse(wasPausingCalled); } @@ -95,12 +108,10 @@ public void OnApplicationFocus_NotOnAndroid_ApplicationPausingTriggered() { var wasPausingCalled = false; - var gameObject = new GameObject("PauseTest"); - var listener = gameObject.AddComponent(); - listener.Application = new TestApplication (platform: RuntimePlatform.IPhonePlayer); - listener.ApplicationPausing += () => wasPausingCalled = true; + var sut = _fixture.GetSut(RuntimePlatform.IPhonePlayer); + sut.ApplicationPausing += () => wasPausingCalled = true; - listener.OnApplicationFocus(false); + sut.OnApplicationFocus(false); Assert.IsTrue(wasPausingCalled); } @@ -110,13 +121,11 @@ public void OnApplicationFocus_NotOnAndroid_ApplicationPausingTriggeredOnlyOnce( { var pauseEventTriggerCounter = 0; - var gameObject = new GameObject("PauseTest"); - var listener = gameObject.AddComponent(); - listener.Application = new TestApplication (platform: RuntimePlatform.IPhonePlayer); - listener.ApplicationPausing += () => pauseEventTriggerCounter++; + var sut = _fixture.GetSut(RuntimePlatform.IPhonePlayer); + sut.ApplicationPausing += () => pauseEventTriggerCounter++; - listener.OnApplicationFocus(false); - listener.OnApplicationFocus(false); + sut.OnApplicationFocus(false); + sut.OnApplicationFocus(false); Assert.AreEqual(1, pauseEventTriggerCounter); } @@ -127,18 +136,16 @@ public void OnApplicationFocus_NotOnAndroid_PausingIsRequiredBeforeApplicationRe var wasPausingCalled = false; var wasResumingCalled = false; - var gameObject = new GameObject("PauseTest"); - var listener = gameObject.AddComponent(); - listener.Application = new TestApplication (platform: RuntimePlatform.IPhonePlayer); - listener.ApplicationPausing += () => wasPausingCalled = true; - listener.ApplicationResuming += () => wasResumingCalled = true; + var sut = _fixture.GetSut(RuntimePlatform.IPhonePlayer); + sut.ApplicationPausing += () => wasPausingCalled = true; + sut.ApplicationResuming += () => wasResumingCalled = true; - listener.OnApplicationFocus(true); + sut.OnApplicationFocus(true); Assert.IsFalse(wasResumingCalled); - listener.OnApplicationFocus(false); - listener.OnApplicationFocus(true); + sut.OnApplicationFocus(false); + sut.OnApplicationFocus(true); Assert.IsTrue(wasPausingCalled); Assert.IsTrue(wasResumingCalled); From 42f7d912b4bdc78d65cef63220aa9951685e8b4e Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Wed, 30 Jun 2021 13:23:48 +0200 Subject: [PATCH 5/5] removed empty lines --- test/Sentry.Unity.Tests/SentryMonoBehaviourTests.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/Sentry.Unity.Tests/SentryMonoBehaviourTests.cs b/test/Sentry.Unity.Tests/SentryMonoBehaviourTests.cs index e34c97b8a..315db298c 100644 --- a/test/Sentry.Unity.Tests/SentryMonoBehaviourTests.cs +++ b/test/Sentry.Unity.Tests/SentryMonoBehaviourTests.cs @@ -20,9 +20,6 @@ public SentryMonoBehaviour GetSut(RuntimePlatform platform) private Fixture _fixture = null!; - - - [SetUp] public void SetUp() {