common: refactor common library pt2 (#8588)

- remove common lib
- split settings, remove common-md
- move ipc interop/kb_layout to interop
- rename core -> settings, settings -> old_settings
- os-detect header-only; interop -> PowerToysInterop
- split notifications, move single-use headers where they're used
- winstore lib
- rename com utils
- rename Updating and Telemetry projects
- rename core -> settings-ui and remove examples folder
- rename settings-ui folder + consisent common/version include
This commit is contained in:
Andrey Nekrasov
2020-12-15 15:16:09 +03:00
committed by GitHub
parent cddf48547d
commit 212ea2de30
588 changed files with 3304 additions and 3328 deletions

View File

@@ -0,0 +1,58 @@
// 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 System.IO.Abstractions.TestingHelpers;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UnitTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;
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]
[ObsoleteAttribute("This test method is obsolete.", true)]
public void ToJsonStringShouldReturnValidJSONOfModelWhenSuccessful()
{
// Mock Disk access
var mockFileSystem = new MockFileSystem();
var settingsUtils = new SettingsUtils(mockFileSystem);
// 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();
settingsUtils.SaveSettings(testSettingsConfigs, file_name);
JsonSchema expectedSchema = JsonSchema.Parse(expectedSchemaText);
// Act
JObject actualSchema = JObject.Parse(settingsUtils.GetSettings<BasePTSettingsTest>(file_name).ToJsonString());
bool valid = actualSchema.IsValid(expectedSchema);
// Assert
Assert.IsTrue(valid);
}
}
}

View File

@@ -0,0 +1,28 @@
// 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 Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
namespace Microsoft.PowerToys.Settings.UnitTest
{
public class BasePTSettingsTest : BasePTModuleSettings, ISettingsConfig
{
public BasePTSettingsTest()
{
Name = string.Empty;
Version = string.Empty;
}
public string GetModuleName()
{
return Name;
}
public bool UpgradeSettingsConfiguration()
{
return false;
}
}
}

View File

@@ -0,0 +1,76 @@
// 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.Library.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace CommonLibTest
{
[TestClass]
public class HelperTest
{
public static void TestStringIsSmaller(string v1, string v2)
{
var res = Helper.CompareVersions(v1, v2);
Assert.IsTrue(res < 0);
}
public static void TestStringsAreEqual(string v1, string v2)
{
var res = Helper.CompareVersions(v1, v2);
Assert.IsTrue(res == 0);
}
[TestMethod]
public void HelperCompareVersionsShouldBeEqualWhenSuccessful()
{
TestStringsAreEqual("v0.0.0", "v0.0.0");
TestStringsAreEqual("v0.1.1", "v0.1.1");
TestStringsAreEqual("v1.1.1", "v1.1.1");
TestStringsAreEqual("v1.999.99", "v1.999.99");
}
[TestMethod]
public void HelperCompareVersionsShouldBeSmallerWhenSuccessful()
{
TestStringIsSmaller("v0.0.0", "v0.0.1");
TestStringIsSmaller("v0.0.0", "v0.1.0");
TestStringIsSmaller("v0.0.0", "v1.0.0");
TestStringIsSmaller("v1.0.1", "v1.0.2");
TestStringIsSmaller("v1.1.1", "v1.1.2");
TestStringIsSmaller("v1.1.1", "v1.2.0");
TestStringIsSmaller("v1.999.99", "v2.0.0");
TestStringIsSmaller("v1.0.99", "v1.2.0");
}
[TestMethod]
[ExpectedException(typeof(FormatException))]
public void HelperCompareVersionsShouldThrowBadFormatWhenNoVersionString()
{
Helper.CompareVersions("v0.0.1", string.Empty);
}
[TestMethod]
[ExpectedException(typeof(FormatException))]
public void HelperCompareVersionsShouldThrowBadFormatWhenShortVersionString()
{
Helper.CompareVersions("v0.0.1", "v0.1");
}
[TestMethod]
[ExpectedException(typeof(FormatException))]
public void HelperCompareVersionsShouldThrowBadFormatWhenLongVersionString()
{
Helper.CompareVersions("v0.0.1", "v0.0.0.1");
}
[TestMethod]
[ExpectedException(typeof(FormatException))]
public void HelperCompareVersionsShouldThrowBadFormatWhenItIsNotAVersionString()
{
Helper.CompareVersions("v0.0.1", "HelloWorld");
}
}
}

View File

@@ -0,0 +1,63 @@
// 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.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace CommonLibTest
{
[TestClass]
public class SettingsRepositoryTest
{
private static Task<SettingsRepository<GeneralSettings>> GetSettingsRepository(ISettingsUtils settingsUtils)
{
return Task.Run(() =>
{
return SettingsRepository<GeneralSettings>.GetInstance(settingsUtils);
});
}
[TestMethod]
public void SettingsRepositoryInstanceWhenCalledMustReturnSameObject()
{
// The singleton class Settings Repository must always have a single instance
var mockSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<GeneralSettings>();
// Arrange and Act
SettingsRepository<GeneralSettings> firstInstance = SettingsRepository<GeneralSettings>.GetInstance(mockSettingsUtils.Object);
SettingsRepository<GeneralSettings> secondInstance = SettingsRepository<GeneralSettings>.GetInstance(mockSettingsUtils.Object);
// Assert
Assert.IsTrue(object.ReferenceEquals(firstInstance, secondInstance));
}
[TestMethod]
public void SettingsRepositoryInstanceMustBeTheSameAcrossThreads()
{
// Multiple tasks try to access and initialize the settings repository class, however they must all access the same settings Repository object.
// Arrange
var mockSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<GeneralSettings>();
List<Task<SettingsRepository<GeneralSettings>>> settingsRepoTasks = new List<Task<SettingsRepository<GeneralSettings>>>();
int numberOfTasks = 100;
for (int i = 0; i < numberOfTasks; i++)
{
settingsRepoTasks.Add(GetSettingsRepository(mockSettingsUtils.Object));
}
// Act
Task.WaitAll(settingsRepoTasks.ToArray());
// Assert
for (int i = 0; i < numberOfTasks - 1; i++)
{
Assert.IsTrue(object.ReferenceEquals(settingsRepoTasks[i].Result, settingsRepoTasks[i + 1].Result));
}
}
}
}

View File

@@ -0,0 +1,125 @@
// 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 System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Text.Json;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
using Microsoft.PowerToys.Settings.UnitTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace CommonLibTest
{
[TestClass]
public class SettingsUtilsTests
{
[TestMethod]
public void SaveSettingsSaveSettingsToFileWhenFilePathExists()
{
// Arrange
var mockFileSystem = new MockFileSystem();
var settingsUtils = new SettingsUtils(mockFileSystem);
string file_name = "\\test";
string file_contents_correct_json_content = "{\"name\":\"powertoy module name\",\"version\":\"powertoy version\"}";
BasePTSettingsTest expected_json = JsonSerializer.Deserialize<BasePTSettingsTest>(file_contents_correct_json_content);
// Act
settingsUtils.SaveSettings(file_contents_correct_json_content, file_name);
BasePTSettingsTest actual_json = settingsUtils.GetSettings<BasePTSettingsTest>(file_name);
// Assert
Assert.AreEqual(expected_json.ToJsonString(), actual_json.ToJsonString());
}
[TestMethod]
public void SaveSettingsShouldCreateFileWhenFilePathIsNotFound()
{
// Arrange
var mockFileSystem = new MockFileSystem();
var settingsUtils = new SettingsUtils(mockFileSystem);
string file_name = "test\\Test Folder";
string file_contents_correct_json_content = "{\"name\":\"powertoy module name\",\"version\":\"powertoy version\"}";
BasePTSettingsTest expected_json = JsonSerializer.Deserialize<BasePTSettingsTest>(file_contents_correct_json_content);
settingsUtils.SaveSettings(file_contents_correct_json_content, file_name);
BasePTSettingsTest actual_json = settingsUtils.GetSettings<BasePTSettingsTest>(file_name);
// Assert
Assert.AreEqual(expected_json.ToJsonString(), actual_json.ToJsonString());
}
[TestMethod]
public void SettingsFolderExistsShouldReturnFalseWhenFilePathIsNotFound()
{
// Arrange
var mockFileSystem = new MockFileSystem();
var settingsUtils = new SettingsUtils(mockFileSystem);
string file_name_random = "test\\" + RandomString();
string file_name_exists = "test\\exists";
string file_contents_correct_json_content = "{\"name\":\"powertoy module name\",\"version\":\"powertoy version\"}";
// Act
bool pathNotFound = settingsUtils.SettingsExists(file_name_random);
settingsUtils.SaveSettings(file_contents_correct_json_content, file_name_exists);
bool pathFound = settingsUtils.SettingsExists(file_name_exists);
// Assert
Assert.IsFalse(pathNotFound);
Assert.IsTrue(pathFound);
}
[TestMethod]
public void SettingsUtilsMustReturnDefaultItemWhenFileIsCorrupt()
{
// Arrange
var mockFileSystem = new MockFileSystem();
var mockSettingsUtils = new SettingsUtils(mockFileSystem);
// Act
TestClass settings = mockSettingsUtils.GetSettings<TestClass>(string.Empty);
// Assert
Assert.AreEqual(settings.TestInt, 100);
Assert.AreEqual(settings.TestString, "test");
}
public static string RandomString()
{
Random random = new Random();
int length = 20;
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
private partial class TestClass : ISettingsConfig
{
public int TestInt { get; set; } = 100;
public string TestString { get; set; } = "test";
public string GetModuleName()
{
throw new NotImplementedException();
}
public string ToJsonString()
{
return JsonSerializer.Serialize(this);
}
public bool UpgradeSettingsConfiguration()
{
throw new NotImplementedException();
}
}
}
}