Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Tests and Refactored code #2129

Merged
21 commits merged into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions PowerToys.sln
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,6 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "powerpreviewTest", "src\mod
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "core", "core", "{C3081D9A-1586-441A-B5F4-ED815B3719C1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.PowerToys.Settings.Test", "src\core\Microsoft.PowerToys.Settings.Test\Microsoft.PowerToys.Settings.Test.csproj", "{6F2B5799-36AE-4D10-9DCF-B19CD26A5DBC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.PowerToys.Settings.UI.Runner", "src\core\Microsoft.PowerToys.Settings.UI.Runner\Microsoft.PowerToys.Settings.UI.Runner.csproj", "{E4E0D2AE-B17D-4BD4-8BEE-AFC8CC464C5F}"
ProjectSection(ProjectDependencies) = postProject
{F88B6FD1-14BD-48DB-85C2-6C51B8045121} = {F88B6FD1-14BD-48DB-85C2-6C51B8045121}
Expand All @@ -209,6 +207,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.PowerToys.Settings.UI.Lib", "src\core\Microsoft.PowerToys.Settings.UI.Lib\Microsoft.PowerToys.Settings.UI.Lib.csproj", "{B1BCC8C6-46B5-4BFA-8F22-20F32D99EC6A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.PowerToys.Settings.Test", "src\core\Microsoft.PowerToys.Settings.Test\Microsoft.PowerToys.Settings.Test.csproj", "{C908B41B-6D64-4F7B-99F0-B98C4C7FDFCB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand Down Expand Up @@ -367,10 +367,6 @@ Global
{47310AB4-9034-4BD1-8D8B-E88AD21A171B}.Debug|x64.Build.0 = Debug|x64
{47310AB4-9034-4BD1-8D8B-E88AD21A171B}.Release|x64.ActiveCfg = Release|x64
{47310AB4-9034-4BD1-8D8B-E88AD21A171B}.Release|x64.Build.0 = Release|x64
{6F2B5799-36AE-4D10-9DCF-B19CD26A5DBC}.Debug|x64.ActiveCfg = Debug|x64
{6F2B5799-36AE-4D10-9DCF-B19CD26A5DBC}.Debug|x64.Build.0 = Debug|x64
{6F2B5799-36AE-4D10-9DCF-B19CD26A5DBC}.Release|x64.ActiveCfg = Release|x64
{6F2B5799-36AE-4D10-9DCF-B19CD26A5DBC}.Release|x64.Build.0 = Release|x64
{E4E0D2AE-B17D-4BD4-8BEE-AFC8CC464C5F}.Debug|x64.ActiveCfg = Debug|x64
{E4E0D2AE-B17D-4BD4-8BEE-AFC8CC464C5F}.Debug|x64.Build.0 = Debug|x64
{E4E0D2AE-B17D-4BD4-8BEE-AFC8CC464C5F}.Release|x64.ActiveCfg = Release|x64
Expand All @@ -391,6 +387,10 @@ Global
{B1BCC8C6-46B5-4BFA-8F22-20F32D99EC6A}.Debug|x64.Build.0 = Debug|x64
{B1BCC8C6-46B5-4BFA-8F22-20F32D99EC6A}.Release|x64.ActiveCfg = Release|Any CPU
{B1BCC8C6-46B5-4BFA-8F22-20F32D99EC6A}.Release|x64.Build.0 = Release|Any CPU
{C908B41B-6D64-4F7B-99F0-B98C4C7FDFCB}.Debug|x64.ActiveCfg = Debug|x64
{C908B41B-6D64-4F7B-99F0-B98C4C7FDFCB}.Debug|x64.Build.0 = Debug|x64
{C908B41B-6D64-4F7B-99F0-B98C4C7FDFCB}.Release|x64.ActiveCfg = Release|x64
{C908B41B-6D64-4F7B-99F0-B98C4C7FDFCB}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -438,12 +438,12 @@ Global
{748417CA-F17E-487F-9411-CAFB6D3F4877} = {2F305555-C296-497E-AC20-5FA1B237996A}
{217DF501-135C-4E38-BFC8-99D4821032EA} = {2F305555-C296-497E-AC20-5FA1B237996A}
{47310AB4-9034-4BD1-8D8B-E88AD21A171B} = {2F305555-C296-497E-AC20-5FA1B237996A}
{6F2B5799-36AE-4D10-9DCF-B19CD26A5DBC} = {C3081D9A-1586-441A-B5F4-ED815B3719C1}
{E4E0D2AE-B17D-4BD4-8BEE-AFC8CC464C5F} = {C3081D9A-1586-441A-B5F4-ED815B3719C1}
{F88B6FD1-14BD-48DB-85C2-6C51B8045121} = {C3081D9A-1586-441A-B5F4-ED815B3719C1}
{A7D5099E-F0FD-4BF3-8522-5A682759F915} = {C3081D9A-1586-441A-B5F4-ED815B3719C1}
{C073B057-B157-40F0-8678-1DCD119D841C} = {C3081D9A-1586-441A-B5F4-ED815B3719C1}
{B1BCC8C6-46B5-4BFA-8F22-20F32D99EC6A} = {C3081D9A-1586-441A-B5F4-ED815B3719C1}
{C908B41B-6D64-4F7B-99F0-B98C4C7FDFCB} = {C3081D9A-1586-441A-B5F4-ED815B3719C1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C3A2F9D1-7930-4EF4-A6FC-7EE0A99821D0}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using System;

namespace Microsoft.PowerToys.Settings.UnitTest
{
[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 failes when the attributes are not initiliazed i.e they have null values.
[TestMethod]
[Obsolete]
public void ToJsonString_ShouldReturnValidJSONOfModel_WhenSuccessful()
{
// 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);
}
}
}
13 changes: 13 additions & 0 deletions src/core/Microsoft.PowerToys.Settings.Test/BasePTSettingsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.PowerToys.Settings.UI.Lib;

namespace Microsoft.PowerToys.Settings.UnitTest
{
public class BasePTSettingsTest : BasePTModuleSettings
{
public BasePTSettingsTest()
{
this.name = string.Empty;
this.version = string.Empty;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.PowerToys.Settings.UI.Lib\Microsoft.PowerToys.Settings.UI.Lib.csproj" />
</ItemGroup>

</Project>
108 changes: 108 additions & 0 deletions src/core/Microsoft.PowerToys.Settings.Test/SettingsUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;

namespace Microsoft.PowerToys.Settings.UnitTest
{
[TestClass]
public class SettingsUtilsTests
{
public SettingsUtilsTests()
{
string file_name = "\\test";
if (SettingsUtils.SettingsFolderExists(file_name))
{
DeleteFolder(file_name);
}
}
[TestMethod]
public void SaveSettings_SaveSettingsToFile_WhenFilePathExists()
{
// Arrange
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);
This conversation was marked as resolved.
Show resolved Hide resolved
BasePTSettingsTest actual_json = SettingsUtils.GetSettings<BasePTSettingsTest>(file_name);

// Assert
Assert.IsTrue(actual_json.Equals(actual_json));
}

[TestMethod]
public async Task SaveSettings_ShouldCreateFile_WhenFilePathIsNotFoundAsync()
{
// Arrange
string file_name = "test\\Test Folder";
This conversation was marked as resolved.
Show resolved Hide resolved
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
if(SettingsUtils.SettingsFolderExists(file_name))
{
DeleteFolder(file_name);
}

SettingsUtils.SaveSettings(file_contents_correct_json_content, file_name);
BasePTSettingsTest actual_json = SettingsUtils.GetSettings<BasePTSettingsTest>(file_name);

// Assert
Assert.IsTrue(actual_json.Equals(actual_json));
}

[TestMethod]
public void SettingsFolderExists_ShouldReturnFalse_WhenFilePathIsNotFound()
{
// Arrange
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.SettingsFolderExists(file_name_random);

SettingsUtils.SaveSettings(file_contents_correct_json_content, file_name_exists);
This conversation was marked as resolved.
Show resolved Hide resolved
bool pathFound = SettingsUtils.SettingsFolderExists(file_name_exists);

// Assert
Assert.IsFalse(pathNotFound);
Assert.IsTrue(pathFound);
}

[TestMethod]
public void CreateSettingsFolder_ShouldCreateFolder_WhenSuccessfull()
{
// Arrange
string file_name = "test\\" + RandomString();

// Act
SettingsUtils.CreateSettingsFolder(file_name);
This conversation was marked as resolved.
Show resolved Hide resolved

// Assert
Assert.IsTrue(SettingsUtils.SettingsFolderExists(file_name));
}

public void DeleteFolder(string powertoy)
{
Directory.Delete(Path.Combine(SettingsUtils.LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"), true);
}

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());
}
}
}
13 changes: 0 additions & 13 deletions src/core/Microsoft.PowerToys.Settings.Test/UnitTest1.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static void SaveSettings(string jsonSettings, string powertoy)
}
}

private static string LocalApplicationDataFolder()
public static string LocalApplicationDataFolder()
This conversation was marked as resolved.
Show resolved Hide resolved
{
return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\ActivationService.cs" />
<Compile Include="Services\NavigationService.cs" />
<Compile Include="ViewModels\Commands\ButtonClickCommand.cs" />
<Compile Include="ViewModels\GeneralViewModel.cs" />
<Compile Include="ViewModels\FancyZonesViewModel.cs" />
<Compile Include="ViewModels\ImageResizerViewModel.cs" />
<Compile Include="ViewModels\KeyboardManagerViewModel.cs" />
<Compile Include="ViewModels\Keys.cs" />
<Compile Include="ViewModels\PowerPreviewViewModel.cs" />
<Compile Include="ViewModels\PowerRenameViewModel.cs" />
<Compile Include="ViewModels\PowerLauncherViewModel.cs" />
<Compile Include="ViewModels\RemapKeysModel.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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.Windows.Input;

namespace Microsoft.PowerToys.Settings.UI.ViewModels.Commands
{
public class ButtonClickCommand : ICommand
{
private readonly Action _execute;

public ButtonClickCommand(Action execute)
{
this._execute = execute;
}

// Occurs when changes occur that affect whether or not the command should execute.
public event EventHandler CanExecuteChanged;

// Defines the method that determines whether the command can execute in its current state.
public bool CanExecute(object parameter)
{
return true;
}

// Defines the method to be called when the command is invoked.
public void Execute(object parameter)
{
_execute();
}
}
}
Loading