2020-08-13 15:02:05 -07:00
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Lib;
|
2020-09-21 10:14:44 -07:00
|
|
|
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
|
|
|
|
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
|
2020-08-13 15:02:05 -07:00
|
|
|
using Microsoft.PowerToys.Settings.UnitTest;
|
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
2020-09-21 10:14:44 -07:00
|
|
|
using Moq;
|
2020-08-13 15:02:05 -07:00
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using Newtonsoft.Json.Schema;
|
|
|
|
|
|
|
|
|
|
namespace CommonLibTest
|
|
|
|
|
{
|
|
|
|
|
[TestClass]
|
|
|
|
|
public class BasePTModuleSettingsTest
|
|
|
|
|
{
|
|
|
|
|
// Work around for System.JSON required properties:
|
|
|
|
|
// https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to.
|
|
|
|
|
// Test also fails when the attributes are not initialized i.e they have null values.
|
|
|
|
|
[TestMethod]
|
|
|
|
|
[Obsolete]
|
|
|
|
|
public void ToJsonString_ShouldReturnValidJSONOfModel_WhenSuccessful()
|
|
|
|
|
{
|
2020-09-21 10:14:44 -07:00
|
|
|
//Mock Disk access
|
|
|
|
|
string saveContent = string.Empty;
|
|
|
|
|
string savePath = string.Empty;
|
|
|
|
|
var mockIOProvider = IIOProviderMocks.GetMockIOProviderForSaveLoadExists();
|
|
|
|
|
|
|
|
|
|
var settingsUtils = new SettingsUtils(mockIOProvider.Object);
|
|
|
|
|
|
2020-08-13 15:02:05 -07:00
|
|
|
// Arrange
|
|
|
|
|
string file_name = "test\\BasePTModuleSettingsTest";
|
|
|
|
|
string expectedSchemaText = @"
|
|
|
|
|
{
|
|
|
|
|
'$schema': 'http://json-schema.org/draft-04/schema#',
|
|
|
|
|
'type': 'object',
|
|
|
|
|
'properties': {
|
|
|
|
|
'name': {
|
|
|
|
|
'type': 'string'
|
|
|
|
|
},
|
|
|
|
|
'version': {
|
|
|
|
|
'type': 'string'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
'additionalProperties': false
|
|
|
|
|
}";
|
|
|
|
|
|
|
|
|
|
string testSettingsConfigs = new BasePTSettingsTest().ToJsonString();
|
2020-09-21 10:14:44 -07:00
|
|
|
settingsUtils.SaveSettings(testSettingsConfigs, file_name);
|
2020-08-13 15:02:05 -07:00
|
|
|
JsonSchema expectedSchema = JsonSchema.Parse(expectedSchemaText);
|
|
|
|
|
|
|
|
|
|
// Act
|
2020-09-21 10:14:44 -07:00
|
|
|
JObject actualSchema = JObject.Parse(settingsUtils.GetSettings<BasePTSettingsTest>(file_name).ToJsonString());
|
2020-08-13 15:02:05 -07:00
|
|
|
bool valid = actualSchema.IsValid(expectedSchema);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.IsTrue(valid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|