|
| 1 | +using System.Linq; |
| 2 | +using NUnit.Framework; |
| 3 | +using Sentry.Unity.Integrations; |
| 4 | +using Sentry.Unity.Tests.Stubs; |
| 5 | + |
| 6 | +namespace Sentry.Unity.Tests; |
| 7 | + |
| 8 | +public class LowMemoryIntegrationTests |
| 9 | +{ |
| 10 | + private class Fixture |
| 11 | + { |
| 12 | + public TestApplication Application { get; set; } = new(); |
| 13 | + public TestHub Hub { get; set; } = new(); |
| 14 | + public SentryUnityOptions Options { get; set; } = new(); |
| 15 | + |
| 16 | + public LowMemoryIntegration GetSut() => new(Application); |
| 17 | + } |
| 18 | + |
| 19 | + private Fixture _fixture = null!; |
| 20 | + |
| 21 | + [SetUp] |
| 22 | + public void SetUp() |
| 23 | + { |
| 24 | + _fixture = new Fixture(); |
| 25 | + } |
| 26 | + |
| 27 | + [Test] |
| 28 | + public void LowMemory_DisabledHub_NoBreadcrumbAdded() |
| 29 | + { |
| 30 | + _fixture.Hub = new TestHub(false); |
| 31 | + var sut = _fixture.GetSut(); |
| 32 | + |
| 33 | + sut.Register(_fixture.Hub, _fixture.Options); |
| 34 | + _fixture.Application.OnLowMemory(); |
| 35 | + |
| 36 | + Assert.Zero(_fixture.Hub.ConfigureScopeCalls.Count); |
| 37 | + } |
| 38 | + |
| 39 | + [Test] |
| 40 | + public void LowMemory_EnabledHub_BreadcrumbAdded() |
| 41 | + { |
| 42 | + var sut = _fixture.GetSut(); |
| 43 | + |
| 44 | + sut.Register(_fixture.Hub, _fixture.Options); |
| 45 | + _fixture.Application.OnLowMemory(); |
| 46 | + |
| 47 | + var configureScope = _fixture.Hub.ConfigureScopeCalls.Single(); |
| 48 | + var scope = new Scope(_fixture.Options); |
| 49 | + configureScope(scope); |
| 50 | + var actualCrumb = scope.Breadcrumbs.Single(); |
| 51 | + |
| 52 | + Assert.AreEqual("Low memory", actualCrumb.Message); |
| 53 | + Assert.AreEqual("device.event", actualCrumb.Category); |
| 54 | + Assert.AreEqual("system", actualCrumb.Type); |
| 55 | + Assert.AreEqual(BreadcrumbLevel.Warning, actualCrumb.Level); |
| 56 | + Assert.NotNull(actualCrumb.Data); |
| 57 | + Assert.AreEqual("LOW_MEMORY", actualCrumb.Data?["action"]); |
| 58 | + } |
| 59 | + |
| 60 | + [Test] |
| 61 | + public void LowMemory_MultipleTriggers_MultipleBreadcrumbsAdded() |
| 62 | + { |
| 63 | + var sut = _fixture.GetSut(); |
| 64 | + |
| 65 | + sut.Register(_fixture.Hub, _fixture.Options); |
| 66 | + _fixture.Application.OnLowMemory(); |
| 67 | + _fixture.Application.OnLowMemory(); |
| 68 | + _fixture.Application.OnLowMemory(); |
| 69 | + |
| 70 | + Assert.AreEqual(3, _fixture.Hub.ConfigureScopeCalls.Count); |
| 71 | + } |
| 72 | +} |
0 commit comments