Skip to content

Commit 0d36444

Browse files
committed
Added unit tests for new SystemInformation API
1 parent 76cf988 commit 0d36444

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

Microsoft.Toolkit.Uwp/Helpers/SystemInformation.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,29 @@ private SystemInformation()
5050
}
5151

5252
DeviceFamily = AnalyticsInfo.VersionInfo.DeviceFamily;
53+
5354
ulong version = ulong.Parse(AnalyticsInfo.VersionInfo.DeviceFamilyVersion);
55+
5456
OperatingSystemVersion = new OSVersion
5557
{
5658
Major = (ushort)((version & 0xFFFF000000000000L) >> 48),
5759
Minor = (ushort)((version & 0x0000FFFF00000000L) >> 32),
5860
Build = (ushort)((version & 0x00000000FFFF0000L) >> 16),
5961
Revision = (ushort)(version & 0x000000000000FFFFL)
6062
};
63+
6164
OperatingSystemArchitecture = Package.Current.Id.Architecture;
62-
EasClientDeviceInformation deviceInfo = new EasClientDeviceInformation();
65+
66+
EasClientDeviceInformation deviceInfo = new();
67+
6368
OperatingSystem = deviceInfo.OperatingSystem;
6469
DeviceManufacturer = deviceInfo.SystemManufacturer;
6570
DeviceModel = deviceInfo.SystemProductName;
6671
IsFirstRun = DetectIfFirstUse();
6772
(IsAppUpdated, PreviousVersionInstalled) = DetectIfAppUpdated();
6873
FirstUseTime = DetectFirstUseTime();
6974
FirstVersionInstalled = DetectFirstVersionInstalled();
75+
7076
InitializeValuesSetWithTrackAppUse();
7177
}
7278

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using Microsoft.Toolkit.Uwp.Helpers;
7+
using Microsoft.VisualStudio.TestTools.UnitTesting;
8+
using Windows.ApplicationModel;
9+
using Windows.Storage;
10+
11+
namespace UnitTests.Helpers
12+
{
13+
[TestClass]
14+
public class Test_SystemInformation
15+
{
16+
[TestCategory("Helpers")]
17+
[TestMethod]
18+
public void Test_SystemInformation_ConsistentInfoForFirstStartup()
19+
{
20+
ApplicationData.Current.LocalSettings.Values.Clear();
21+
22+
var systemInformation = (SystemInformation)Activator.CreateInstance(typeof(SystemInformation), nonPublic: true);
23+
var currentAppVersion = Package.Current.Id.Version;
24+
25+
Assert.IsTrue(systemInformation.IsFirstRun);
26+
Assert.IsFalse(systemInformation.IsAppUpdated);
27+
Assert.AreEqual(systemInformation.ApplicationVersion, currentAppVersion);
28+
Assert.AreEqual(systemInformation.PreviousVersionInstalled, currentAppVersion);
29+
Assert.AreEqual(systemInformation.FirstVersionInstalled, currentAppVersion);
30+
}
31+
32+
[TestCategory("Helpers")]
33+
[TestMethod]
34+
public void Test_SystemInformation_ConsistentInfoForStartupWithNoUpdate()
35+
{
36+
ApplicationData.Current.LocalSettings.Values.Clear();
37+
38+
// Simulate a first app startup
39+
_ = (SystemInformation)Activator.CreateInstance(typeof(SystemInformation), nonPublic: true);
40+
41+
var systemInformation = (SystemInformation)Activator.CreateInstance(typeof(SystemInformation), nonPublic: true);
42+
var currentAppVersion = Package.Current.Id.Version;
43+
44+
Assert.IsFalse(systemInformation.IsFirstRun);
45+
Assert.IsFalse(systemInformation.IsAppUpdated);
46+
Assert.AreEqual(systemInformation.ApplicationVersion, currentAppVersion);
47+
Assert.AreEqual(systemInformation.PreviousVersionInstalled, currentAppVersion);
48+
Assert.AreEqual(systemInformation.FirstVersionInstalled, currentAppVersion);
49+
}
50+
51+
[TestCategory("Helpers")]
52+
[TestMethod]
53+
public void Test_SystemInformation_ConsistentInfoForStartupWithUpdate()
54+
{
55+
ApplicationData.Current.LocalSettings.Values.Clear();
56+
57+
// Simulate a first app startup
58+
_ = (SystemInformation)Activator.CreateInstance(typeof(SystemInformation), nonPublic: true);
59+
60+
LocalObjectStorageHelper localObjectStorageHelper = new(new SystemSerializer());
61+
PackageVersion previousVersion = new() { Build = 42, Major = 1111, Minor = 2222, Revision = 12345 };
62+
63+
localObjectStorageHelper.Save("currentVersion", previousVersion.ToFormattedString());
64+
65+
var systemInformation = (SystemInformation)Activator.CreateInstance(typeof(SystemInformation), nonPublic: true);
66+
var currentAppVersion = Package.Current.Id.Version;
67+
68+
Assert.IsFalse(systemInformation.IsFirstRun);
69+
Assert.IsTrue(systemInformation.IsAppUpdated);
70+
Assert.AreEqual(systemInformation.ApplicationVersion, currentAppVersion);
71+
Assert.AreEqual(systemInformation.PreviousVersionInstalled, previousVersion);
72+
Assert.AreEqual(systemInformation.FirstVersionInstalled, currentAppVersion);
73+
}
74+
}
75+
}

UnitTests/UnitTests.UWP/UnitTests.UWP.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@
169169
<Compile Include="Helpers\Test_ConnectionHelper.cs" />
170170
<Compile Include="Helpers\Test_ScreenUnitHelper.cs" />
171171
<Compile Include="Helpers\Test_StorageFileHelper.cs" />
172+
<Compile Include="Helpers\Test_SystemInformation.cs" />
172173
<Compile Include="Helpers\Test_StorageHelper.cs" />
173174
<Compile Include="Helpers\Test_StreamHelper.cs" />
174175
<Compile Include="Helpers\Test_DeepLinkParser.cs" />

0 commit comments

Comments
 (0)