Skip to content

Commit 2086d1b

Browse files
Add initial compatibility and serializer tests for the updates to BaseObjectStorage
1 parent b664b23 commit 2086d1b

File tree

5 files changed

+130
-33
lines changed

5 files changed

+130
-33
lines changed

GazeInputTest/GazeInputTest.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<DefaultLanguage>en-US</DefaultLanguage>
1313
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
1414
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.19041.0</TargetPlatformVersion>
15-
<TargetPlatformMinVersion>10.0.17134.0</TargetPlatformMinVersion>
15+
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
1616
<MinimumVisualStudioVersion>14</MinimumVisualStudioVersion>
1717
<FileAlignment>512</FileAlignment>
1818
<ProjectTypeGuids>{A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 System.Reflection;
7+
using Microsoft.Toolkit.Uwp.Helpers;
8+
using Newtonsoft.Json;
9+
10+
namespace UnitTests.UWP.Helpers
11+
{
12+
/// <summary>
13+
/// This is a Serializer which should mimic the previous functionality of 6.1.1 release of the Toolkit with Newtonsoft.Json.
14+
/// </summary>
15+
internal class JsonObjectSerializer : IObjectSerializer
16+
{
17+
public T Deserialize<T>(object value)
18+
{
19+
var type = typeof(T);
20+
var typeInfo = type.GetTypeInfo();
21+
22+
// Note: If you're creating a new app, you could just use the serializer directly.
23+
// This if/return combo is to maintain compatibility with 6.1.1
24+
if (typeInfo.IsPrimitive || type == typeof(string))
25+
{
26+
return (T)Convert.ChangeType(value, type);
27+
}
28+
29+
return JsonConvert.DeserializeObject<T>((string)value);
30+
}
31+
32+
public object Serialize<T>(T value)
33+
{
34+
var type = typeof(T);
35+
var typeInfo = type.GetTypeInfo();
36+
37+
// Note: If you're creating a new app, you could just use the serializer directly.
38+
// This if/return combo is to maintain compatibility with 6.1.1
39+
if (typeInfo.IsPrimitive || type == typeof(string))
40+
{
41+
return value;
42+
}
43+
44+
return JsonConvert.SerializeObject(value);
45+
}
46+
}
47+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 System.Text.Json;
7+
using Microsoft.Toolkit.Uwp.Helpers;
8+
9+
namespace UnitTests.UWP.Helpers
10+
{
11+
/// <summary>
12+
/// Example class of writing a new <see cref="IObjectSerializer"/> that uses System.Text.Json.
13+
/// </summary>
14+
internal class SystemTextJsonSerializer : IObjectSerializer
15+
{
16+
public T Deserialize<T>(object value) => JsonSerializer.Deserialize<T>(value as string);
17+
18+
public object Serialize<T>(T value) => JsonSerializer.Serialize(value);
19+
}
20+
}

UnitTests/UnitTests.UWP/Helpers/Test_StorageHelper.cs

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,32 @@
55
using Microsoft.Toolkit.Uwp.Helpers;
66
using Microsoft.VisualStudio.TestTools.UnitTesting;
77
using System;
8-
using System.Text.Json;
9-
using UnitTests.UI;
8+
using Newtonsoft.Json;
9+
using UnitTests.UWP.Helpers;
10+
using System.Diagnostics.Contracts;
1011

1112
namespace UnitTests.Helpers
1213
{
1314
[TestClass]
1415
public class Test_StorageHelper
1516
{
16-
private LocalObjectStorageHelper storageHelper = new LocalObjectStorageHelper();
17+
private LocalObjectStorageHelper _localStorageHelperSystem = new LocalObjectStorageHelper(new SystemSerializer());
18+
private LocalObjectStorageHelper _localStorageHelperJsonCompat = new LocalObjectStorageHelper(new JsonObjectSerializer());
19+
20+
private LocalObjectStorageHelper _localStorageHelperJsonNew = new LocalObjectStorageHelper(new SystemTextJsonSerializer());
21+
22+
/// <summary>
23+
/// Checks that we're running 10.0.3 version of Newtonsoft.Json package which we used in 6.1.1.
24+
/// </summary>
25+
[TestCategory("Helpers")]
26+
[TestMethod]
27+
public void Test_StorageHelper_CheckNewtonsoftVersion()
28+
{
29+
var version = typeof(JsonSerializer).Assembly.GetName().Version;
30+
Assert.AreEqual(10, version.Major);
31+
Assert.AreEqual(0, version.Minor);
32+
Assert.AreEqual(0, version.Revision); // Apparently the file revision was not updated for the updated package
33+
}
1734

1835
[TestCategory("Helpers")]
1936
[TestMethod]
@@ -23,38 +40,45 @@ public void Test_StorageHelper_LegacyIntTest()
2340

2441
int input = 42;
2542

26-
// simulate previous version by generating json and manually inserting it as string
27-
string jsonInput = JsonSerializer.Serialize(input);
28-
29-
storageHelper.Save<string>(key, jsonInput);
43+
// Use our previous Json layer to store value
44+
_localStorageHelperJsonCompat.Save(key, input);
3045

31-
// now read it as int to valid that the change works
32-
int output = storageHelper.Read<int>(key, 0);
46+
// But try and read from our new system to see if it works
47+
int output = _localStorageHelperSystem.Read(key, 0);
3348

3449
Assert.AreEqual(input, output);
3550
}
3651

37-
[Ignore]
52+
/// <summary>
53+
/// If we try and deserialize a complex type with the <see cref="SystemSerializer"/>, we do a check ourselves and will throw our own exception.
54+
/// </summary>
3855
[TestCategory("Helpers")]
3956
[TestMethod]
40-
public void Test_StorageHelper_LegacyDateTest()
57+
[ExpectedException(typeof(NotSupportedException))]
58+
public void Test_StorageHelper_LegacyDateTestFailure()
4159
{
4260
string key = "ChristmasDay";
4361

4462
DateTime input = new DateTime(2017, 12, 25);
4563

46-
// simulate previous version by generating json and manually inserting it as string
47-
string jsonInput = JsonSerializer.Serialize(input);
48-
49-
storageHelper.Save<string>(key, jsonInput);
64+
_localStorageHelperJsonCompat.Save(key, input);
5065

5166
// now read it as int to valid that the change works
52-
DateTime output = storageHelper.Read<DateTime>(key, DateTime.Today);
67+
DateTime output = _localStorageHelperSystem.Read(key, DateTime.Today);
68+
}
5369

54-
Assert.AreEqual(input, output);
70+
/// <summary>
71+
/// The <see cref="SystemSerializer"/> doesn't support complex types, since it just passes through directly.
72+
/// We'll get the argument exception from the <see cref="ApplicationDataContainer"/> API.
73+
/// </summary>
74+
[TestCategory("Helpers")]
75+
[TestMethod]
76+
[ExpectedException(typeof(ArgumentException))]
77+
public void Test_StorageHelper_DateTestFailure()
78+
{
79+
_localStorageHelperSystem.Save("Today", DateTime.Today);
5580
}
5681

57-
[Ignore]
5882
[TestCategory("Helpers")]
5983
[TestMethod]
6084
public void Test_StorageHelper_LegacyInternalClassTest()
@@ -64,12 +88,10 @@ public void Test_StorageHelper_LegacyInternalClassTest()
6488
UI.Person input = new UI.Person() { Name = "Joe Bloggs", Age = 42 };
6589

6690
// simulate previous version by generating json and manually inserting it as string
67-
string jsonInput = JsonSerializer.Serialize(input);
68-
69-
storageHelper.Save<string>(key, jsonInput);
91+
_localStorageHelperJsonCompat.Save(key, input);
7092

7193
// now read it as int to valid that the change works
72-
UI.Person output = storageHelper.Read<UI.Person>(key, null);
94+
UI.Person output = _localStorageHelperJsonCompat.Read<UI.Person>(key, null);
7395

7496
Assert.IsNotNull(output);
7597
Assert.AreEqual(input.Name, output.Name);
@@ -82,15 +104,14 @@ public void Test_StorageHelper_LegacyPublicClassTest()
82104
{
83105
string key = "Contact";
84106

107+
// Here's we're serializing a different class which has the same properties as our other class below.
85108
UI.Person input = new UI.Person() { Name = "Joe Bloggs", Age = 42 };
86109

87110
// simulate previous version by generating json and manually inserting it as string
88-
string jsonInput = JsonSerializer.Serialize(input);
89-
90-
storageHelper.Save(key, jsonInput);
111+
_localStorageHelperJsonCompat.Save(key, input);
91112

92113
// now read it as int to valid that the change works
93-
Person output = storageHelper.Read<Person>(key, null);
114+
Person output = _localStorageHelperJsonCompat.Read<Person>(key, null);
94115

95116
Assert.IsNotNull(output);
96117
Assert.AreEqual(input.Name, output.Name);
@@ -105,10 +126,10 @@ public void Test_StorageHelper_IntTest()
105126

106127
int input = 42;
107128

108-
storageHelper.Save<int>(key, input);
129+
_localStorageHelperSystem.Save<int>(key, input);
109130

110131
// now read it as int to valid that the change works
111-
int output = storageHelper.Read<int>(key, 0);
132+
int output = _localStorageHelperSystem.Read<int>(key, 0);
112133

113134
Assert.AreEqual(input, output);
114135
}
@@ -121,10 +142,10 @@ public void Test_StorageHelper_NewDateTest()
121142

122143
DateTime input = new DateTime(2017, 12, 25);
123144

124-
storageHelper.Save<DateTime>(key, input);
145+
_localStorageHelperJsonNew.Save(key, input);
125146

126147
// now read it as int to valid that the change works
127-
DateTime output = storageHelper.Read<DateTime>(key, DateTime.Today);
148+
DateTime output = _localStorageHelperJsonNew.Read(key, DateTime.Today);
128149

129150
Assert.AreEqual(input, output);
130151
}
@@ -137,10 +158,10 @@ public void Test_StorageHelper_NewPersonTest()
137158

138159
Person input = new Person() { Name = "Joe Bloggs", Age = 42 };
139160

140-
storageHelper.Save<Person>(key, input);
161+
_localStorageHelperJsonNew.Save(key, input);
141162

142163
// now read it as int to valid that the change works
143-
Person output = storageHelper.Read<Person>(key, null);
164+
Person output = _localStorageHelperJsonNew.Read<Person>(key, null);
144165

145166
Assert.IsNotNull(output);
146167
Assert.AreEqual(input.Name, output.Name);

UnitTests/UnitTests.UWP/UnitTests.UWP.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@
119119
<PackageReference Include="MSTest.TestFramework">
120120
<Version>2.1.0</Version>
121121
</PackageReference>
122+
<PackageReference Include="Newtonsoft.Json">
123+
<Version>10.0.3</Version>
124+
<!-- DO NOT UPGRADE THIS PACKAGE FROM 10.0.3, this is used for upgrade testing between 6.1.1 and 7.0.0 for the Object Serializers -->
125+
</PackageReference>
126+
<PackageReference Include="System.Text.Json">
127+
<Version>4.7.2</Version>
128+
</PackageReference>
122129
<PackageReference Include="System.Xml.XPath.XmlDocument">
123130
<Version>4.3.0</Version>
124131
</PackageReference>
@@ -141,6 +148,8 @@
141148
<Compile Include="Extensions\Test_EnumValuesExtension.cs" />
142149
<Compile Include="Extensions\Test_NullableBoolMarkupExtension.cs" />
143150
<Compile Include="GlobalSuppressions.cs" />
151+
<Compile Include="Helpers\JsonObjectSerializer.cs" />
152+
<Compile Include="Helpers\SystemTextJsonSerializer.cs" />
144153
<Compile Include="Helpers\TestCollectionCapableDeepLinkParser.cs" />
145154
<Compile Include="Helpers\TestDeepLinkParser.cs" />
146155
<Compile Include="Extensions\Test_DispatcherQueueExtensions.cs" />

0 commit comments

Comments
 (0)