|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using NUnit.Framework; |
| 4 | +using Sentry.Unity.Integrations; |
| 5 | +using Sentry.Unity.Tests.SharedClasses; |
| 6 | +using Sentry.Unity.Tests.Stubs; |
| 7 | +using static Sentry.Unity.Tests.SceneManagerIntegrationTests; |
| 8 | + |
| 9 | +namespace Sentry.Unity.Tests; |
| 10 | + |
| 11 | +public class TraceGenerationIntegrationTests |
| 12 | +{ |
| 13 | + private class Fixture |
| 14 | + { |
| 15 | + public FakeSceneManager SceneManager { get; set; } = new(); |
| 16 | + public TestSentryMonoBehaviour SentryMonoBehaviour { get; set; } = new(); |
| 17 | + public TestHub TestHub { get; set; } = new(); |
| 18 | + public TestLogger Logger { get; set; } = new(); |
| 19 | + public SentryOptions SentryOptions { get; set; } |
| 20 | + |
| 21 | + public Fixture() => SentryOptions = new SentryOptions { DiagnosticLogger = Logger }; |
| 22 | + |
| 23 | + public TraceGenerationIntegration GetSut() => new(SentryMonoBehaviour, SceneManager); |
| 24 | + } |
| 25 | + |
| 26 | + private readonly Fixture _fixture = new(); |
| 27 | + |
| 28 | + [SetUp] |
| 29 | + public void SetUp() |
| 30 | + { |
| 31 | + _fixture.TestHub = new TestHub(); |
| 32 | + SentrySdk.UseHub(_fixture.TestHub); |
| 33 | + } |
| 34 | + |
| 35 | + [Test] |
| 36 | + public void TraceGeneration_OnRegister_GeneratesInitialTrace() |
| 37 | + { |
| 38 | + // Arrange |
| 39 | + var sut = _fixture.GetSut(); |
| 40 | + |
| 41 | + // Act |
| 42 | + sut.Register(_fixture.TestHub, _fixture.SentryOptions); |
| 43 | + |
| 44 | + // Assert |
| 45 | + var configureScope = _fixture.TestHub.ConfigureScopeCalls.Single(); |
| 46 | + var scope = new Scope(_fixture.SentryOptions); |
| 47 | + var initialPropagationContext = scope.PropagationContext; |
| 48 | + configureScope(scope); |
| 49 | + |
| 50 | + Assert.AreNotEqual(initialPropagationContext, scope.PropagationContext); |
| 51 | + } |
| 52 | + |
| 53 | + [Test] |
| 54 | + public void TraceGeneration_OnApplicationResume_GeneratesNewTrace() |
| 55 | + { |
| 56 | + // Arrange |
| 57 | + var sut = _fixture.GetSut(); |
| 58 | + sut.Register(_fixture.TestHub, _fixture.SentryOptions); |
| 59 | + var initialCallsCount = _fixture.TestHub.ConfigureScopeCalls.Count; |
| 60 | + |
| 61 | + // Act |
| 62 | + _fixture.SentryMonoBehaviour.ResumeApplication(); |
| 63 | + |
| 64 | + // Assert |
| 65 | + // Calling 'Register' already generated a trace, so we expect 1+1 calls to ConfigureScope |
| 66 | + Assert.AreEqual(initialCallsCount + 1, _fixture.TestHub.ConfigureScopeCalls.Count); |
| 67 | + var configureScope = _fixture.TestHub.ConfigureScopeCalls.Last(); |
| 68 | + var scope = new Scope(_fixture.SentryOptions); |
| 69 | + var initialPropagationContext = scope.PropagationContext; |
| 70 | + configureScope(scope); |
| 71 | + |
| 72 | + Assert.AreNotEqual(initialPropagationContext, scope.PropagationContext); |
| 73 | + } |
| 74 | + |
| 75 | + [Test] |
| 76 | + public void TraceGeneration_OnActiveSceneChange_GeneratesNewTrace() |
| 77 | + { |
| 78 | + // Arrange |
| 79 | + var sut = _fixture.GetSut(); |
| 80 | + sut.Register(_fixture.TestHub, _fixture.SentryOptions); |
| 81 | + var initialCallsCount = _fixture.TestHub.ConfigureScopeCalls.Count; |
| 82 | + |
| 83 | + // Act |
| 84 | + _fixture.SceneManager.OnActiveSceneChanged(new SceneAdapter("from scene name"), new SceneAdapter("to scene name")); |
| 85 | + |
| 86 | + // Assert |
| 87 | + // Calling 'Register' already generated a trace, so we expect 1+1 calls to ConfigureScope |
| 88 | + Assert.AreEqual(initialCallsCount + 1, _fixture.TestHub.ConfigureScopeCalls.Count); |
| 89 | + var configureScope = _fixture.TestHub.ConfigureScopeCalls.Last(); |
| 90 | + var scope = new Scope(_fixture.SentryOptions); |
| 91 | + var initialPropagationContext = scope.PropagationContext; |
| 92 | + configureScope(scope); |
| 93 | + |
| 94 | + Assert.AreNotEqual(initialPropagationContext, scope.PropagationContext); |
| 95 | + } |
| 96 | + |
| 97 | + internal class TestSentryMonoBehaviour : ISentryMonoBehaviour |
| 98 | + { |
| 99 | + public event Action? ApplicationResuming; |
| 100 | + |
| 101 | + public void ResumeApplication() => ApplicationResuming?.Invoke(); |
| 102 | + } |
| 103 | +} |
0 commit comments