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 18 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
18 changes: 9 additions & 9 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 @@ -389,8 +385,12 @@ Global
{C073B057-B157-40F0-8678-1DCD119D841C}.Release|x64.ActiveCfg = Release|x64
{B1BCC8C6-46B5-4BFA-8F22-20F32D99EC6A}.Debug|x64.ActiveCfg = Debug|x64
{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
{B1BCC8C6-46B5-4BFA-8F22-20F32D99EC6A}.Release|x64.ActiveCfg = Release|x64
{B1BCC8C6-46B5-4BFA-8F22-20F32D99EC6A}.Release|x64.Build.0 = Release|x64
{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>
119 changes: 119 additions & 0 deletions src/core/Microsoft.PowerToys.Settings.Test/SettingsUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
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);
}
}

[TestCleanup()]
public void Cleanup()
{
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 @@ -22,6 +22,8 @@ public FZConfigProperties()
this.FancyzonesHighlightOpacity = new IntProperty();
this.FancyzonesEditorHotkey = new KeyBoardKeysProperty();
this.FancyzonesExcludedApps = new StringProperty();
this.FancyzonesInActiveColor = new StringProperty();
this.FancyzonesBorderColor = new StringProperty();
}

[JsonPropertyName("fancyzones_shiftDrag")]
Expand Down Expand Up @@ -62,5 +64,11 @@ public FZConfigProperties()

[JsonPropertyName("fancyzones_excluded_apps")]
public StringProperty FancyzonesExcludedApps { get; set; }

[JsonPropertyName("fancyzones_zoneBorderColor")]
public StringProperty FancyzonesBorderColor { get; set; }

[JsonPropertyName("fancyzones_zoneColor")]
public StringProperty FancyzonesInActiveColor { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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.Text.Json;
using System.Text.Json.Serialization;

namespace Microsoft.PowerToys.Settings.UI.Lib
{

public class PowerRenameLocalProperties
{
public PowerRenameLocalProperties()
{
PersistState = false;
MRUEnabled = false;
MaxMRUSize = 0;
ShowIcon = false;
ExtendedContextMenuOnly = false;
}

private int _maxSize;

public bool PersistState { get; set; }

public bool MRUEnabled { get; set; }

public int MaxMRUSize
{
get
{
return _maxSize;
}

set
{
if (value < 0)
{
_maxSize = 0;
}
}
}

public bool ShowIcon { get; set; }

public bool ExtendedContextMenuOnly { get; set; }

public string ToJsonString()
{
return JsonSerializer.Serialize(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ public class PowerRenameProperties
{
public PowerRenameProperties()
{
PersistInput = new BoolProperty();
MruEnabled = new BoolProperty();
MaxMruSize = new IntProperty();
ShowIconInMenu = new BoolProperty();
ShowExtendedMenu = new BoolProperty();
PersistState = new BoolProperty();
MRUEnabled = new BoolProperty();
MaxMRUSize = new IntProperty();
ShowIcon = new BoolProperty();
ExtendedContextMenuOnly = new BoolProperty();
}

[JsonPropertyName("bool_persist_input")]
public BoolProperty PersistInput { get; set; }
public BoolProperty PersistState { get; set; }

[JsonPropertyName("bool_mru_enabled")]
public BoolProperty MruEnabled { get; set; }
public BoolProperty MRUEnabled { get; set; }

[JsonPropertyName("int_max_mru_size")]
public IntProperty MaxMruSize { get; set; }
public IntProperty MaxMRUSize { get; set; }

[JsonPropertyName("bool_show_icon_on_menu")]
public BoolProperty ShowIconInMenu { get; set; }
public BoolProperty ShowIcon { get; set; }

[JsonPropertyName("bool_show_extended_menu")]
public BoolProperty ShowExtendedMenu { get; set; }
public BoolProperty ExtendedContextMenuOnly { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ public PowerRenameSettings()
name = "_unset_";
}

public PowerRenameSettings(PowerRenameLocalProperties localProperties)
{
properties = new PowerRenameProperties();
properties.PersistState.Value = localProperties.PersistState;
properties.MRUEnabled.Value = localProperties.MRUEnabled;
properties.MaxMRUSize.Value = localProperties.MaxMRUSize;
properties.ShowIcon.Value = localProperties.ShowIcon;
properties.ExtendedContextMenuOnly.Value = localProperties.ExtendedContextMenuOnly;

version = "1";
name = "_unset_";
This conversation was marked as resolved.
Show resolved Hide resolved
}

public PowerRenameSettings(string ptName)
{
properties = new PowerRenameProperties();
Expand Down
Loading