|
| 1 | +namespace Sentry.Tests.Protocol; |
| 2 | + |
| 3 | +public class DeviceTests |
| 4 | +{ |
| 5 | + [Fact] |
| 6 | + public void Clone_CopyValues() |
| 7 | + { |
| 8 | + // Arrange |
| 9 | + var sut = TestDevice(); |
| 10 | + |
| 11 | + // Act |
| 12 | + var result = sut.Clone(); |
| 13 | + |
| 14 | + // Assert |
| 15 | + AssertAreEqual(result, sut); |
| 16 | + } |
| 17 | + |
| 18 | + [Fact] |
| 19 | + public void WriteTo_FromJson_Symmetric() |
| 20 | + { |
| 21 | + // Arrange |
| 22 | + var sut = TestDevice(); |
| 23 | + |
| 24 | + var json = sut.ToJsonString(); |
| 25 | + using var document = JsonDocument.Parse(json); |
| 26 | + var jsonElement = document.RootElement; |
| 27 | + |
| 28 | + // Act |
| 29 | + var result = Device.FromJson(jsonElement); |
| 30 | + |
| 31 | + // Assert |
| 32 | + AssertAreEqual(result, sut); |
| 33 | + } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public void FromJson_JavaTypes_CastCorrectly() |
| 37 | + { |
| 38 | + // Arrange |
| 39 | + var sut = TestDevice(); |
| 40 | + |
| 41 | + var json = sut.ToJsonString() |
| 42 | + // In the Java SDK, the Processor Frequency is stored as a Double |
| 43 | + .Replace(@"""processor_frequency"": 12", @"""processor_frequency"": 12.0"); |
| 44 | + |
| 45 | + using var document = JsonDocument.Parse(json); |
| 46 | + var jsonElement = document.RootElement; |
| 47 | + |
| 48 | + // Act |
| 49 | + var result = Device.FromJson(jsonElement); |
| 50 | + |
| 51 | + // Assert |
| 52 | + AssertAreEqual(result, sut); |
| 53 | + } |
| 54 | + |
| 55 | + private static Device TestDevice() |
| 56 | + { |
| 57 | + return new Device |
| 58 | + { |
| 59 | + Name = "TestName", |
| 60 | + Manufacturer = "TestManufacturer", |
| 61 | + Brand = "TestBrand", |
| 62 | + Architecture = "TestArchitecture", |
| 63 | + BatteryLevel = 1, |
| 64 | + IsCharging = true, |
| 65 | + IsOnline = true, |
| 66 | + BootTime = new DateTimeOffset(2001, 06, 15, 12, 30, 0, TimeSpan.Zero), |
| 67 | + ExternalFreeStorage = 2, |
| 68 | + ExternalStorageSize = 3, |
| 69 | + ScreenResolution = "800x600", |
| 70 | + ScreenDensity = 1.2f, |
| 71 | + ScreenDpi = 4, |
| 72 | + Family = "TestFamily", |
| 73 | + FreeMemory = 5, |
| 74 | + FreeStorage = 6, |
| 75 | + MemorySize = 7, |
| 76 | + Model = "TestModel", |
| 77 | + ModelId = "TestModelId", |
| 78 | + Orientation = DeviceOrientation.Landscape, |
| 79 | + Simulator = true, |
| 80 | + StorageSize = 8, |
| 81 | + Timezone = TimeZoneInfo.Utc, |
| 82 | + UsableMemory = 9, |
| 83 | + LowMemory = true, |
| 84 | + ProcessorCount = 11, |
| 85 | + CpuDescription = "TestCpuDescription", |
| 86 | + ProcessorFrequency = 12, |
| 87 | + SupportsVibration = true, |
| 88 | + DeviceType = "TestDeviceType", |
| 89 | + BatteryStatus = "TestBatteryStatus", |
| 90 | + DeviceUniqueIdentifier = "TestDeviceUniqueIdentifier", |
| 91 | + SupportsAccelerometer = true, |
| 92 | + SupportsGyroscope = true, |
| 93 | + SupportsAudio = true, |
| 94 | + SupportsLocationService = true |
| 95 | + }; |
| 96 | + } |
| 97 | + |
| 98 | + private static void AssertAreEqual(Device actual, Device expected) |
| 99 | + { |
| 100 | + using (new AssertionScope()) |
| 101 | + { |
| 102 | + actual.Name.Should().Be(expected.Name); |
| 103 | + actual.Manufacturer.Should().Be(expected.Manufacturer); |
| 104 | + actual.Brand.Should().Be(expected.Brand); |
| 105 | + actual.Architecture.Should().Be(expected.Architecture); |
| 106 | + actual.BatteryLevel.Should().Be(expected.BatteryLevel); |
| 107 | + actual.IsCharging.Should().Be(expected.IsCharging); |
| 108 | + actual.IsOnline.Should().Be(expected.IsOnline); |
| 109 | + actual.BootTime.Should().Be(expected.BootTime); |
| 110 | + actual.ExternalFreeStorage.Should().Be(expected.ExternalFreeStorage); |
| 111 | + actual.ExternalStorageSize.Should().Be(expected.ExternalStorageSize); |
| 112 | + actual.ScreenResolution.Should().Be(expected.ScreenResolution); |
| 113 | + actual.ScreenDensity.Should().Be(expected.ScreenDensity); |
| 114 | + actual.ScreenDpi.Should().Be(expected.ScreenDpi); |
| 115 | + actual.Family.Should().Be(expected.Family); |
| 116 | + actual.FreeMemory.Should().Be(expected.FreeMemory); |
| 117 | + actual.FreeStorage.Should().Be(expected.FreeStorage); |
| 118 | + actual.MemorySize.Should().Be(expected.MemorySize); |
| 119 | + actual.Model.Should().Be(expected.Model); |
| 120 | + actual.ModelId.Should().Be(expected.ModelId); |
| 121 | + actual.Orientation.Should().Be(expected.Orientation); |
| 122 | + actual.Simulator.Should().Be(expected.Simulator); |
| 123 | + actual.StorageSize.Should().Be(expected.StorageSize); |
| 124 | + actual.Timezone.Should().Be(expected.Timezone); |
| 125 | + actual.UsableMemory.Should().Be(expected.UsableMemory); |
| 126 | + actual.LowMemory.Should().Be(expected.LowMemory); |
| 127 | + actual.ProcessorCount.Should().Be(expected.ProcessorCount); |
| 128 | + actual.CpuDescription.Should().Be(expected.CpuDescription); |
| 129 | + actual.ProcessorFrequency.Should().Be(expected.ProcessorFrequency); |
| 130 | + actual.SupportsVibration.Should().Be(expected.SupportsVibration); |
| 131 | + actual.DeviceType.Should().Be(expected.DeviceType); |
| 132 | + actual.BatteryStatus.Should().Be(expected.BatteryStatus); |
| 133 | + actual.DeviceUniqueIdentifier.Should().Be(expected.DeviceUniqueIdentifier); |
| 134 | + actual.SupportsAccelerometer.Should().Be(expected.SupportsAccelerometer); |
| 135 | + actual.SupportsGyroscope.Should().Be(expected.SupportsGyroscope); |
| 136 | + actual.SupportsAudio.Should().Be(expected.SupportsAudio); |
| 137 | + actual.SupportsLocationService.Should().Be(expected.SupportsLocationService); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments