[Settings Tests] Migrate General Settings tests (#5753)

* added MSTest project

* migrated general settings tests

* enabled settings tests run in the build pipeline

* added tests

* move relay command class to separate file

* added a foldername parameter for general settings view model
This commit is contained in:
Nkateko
2020-08-13 15:02:05 -07:00
committed by GitHub
parent e4ea8d2abd
commit 24d7232559
14 changed files with 725 additions and 83 deletions

View File

@@ -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;
using Microsoft.PowerToys.Settings.UI.Lib;
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]
[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);
}
}
}

View File

@@ -0,0 +1,17 @@
// 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.Lib;
namespace Microsoft.PowerToys.Settings.UnitTest
{
public class BasePTSettingsTest : BasePTModuleSettings
{
public BasePTSettingsTest()
{
Name = string.Empty;
Version = string.Empty;
}
}
}

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.Lib.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 Helper_CompareVersions_ShouldBeEqual_WhenSuccessful()
{
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 Helper_CompareVersions_ShouldBeSmaller_WhenSuccessful()
{
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 Helper_CompareVersions_ShouldThrowBadFormat_WhenNoVersionString()
{
Helper.CompareVersions("v0.0.1", string.Empty);
}
[TestMethod]
[ExpectedException(typeof(FormatException))]
public void Helper_CompareVersions_ShouldThrowBadFormat_WhenShortVersionString()
{
Helper.CompareVersions("v0.0.1", "v0.1");
}
[TestMethod]
[ExpectedException(typeof(FormatException))]
public void Helper_CompareVersions_ShouldThrowBadFormat_WhenLongVersionString()
{
Helper.CompareVersions("v0.0.1", "v0.0.0.1");
}
[TestMethod]
[ExpectedException(typeof(FormatException))]
public void Helper_CompareVersions_ShouldThrowBadFormat_WhenItIsNotAVersionString()
{
Helper.CompareVersions("v0.0.1", "HelloWorld");
}
}
}

View File

@@ -0,0 +1,123 @@
// 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;
using System.Linq;
using System.Text.Json;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UnitTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace CommonLibTest
{
[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);
BasePTSettingsTest actual_json = SettingsUtils.GetSettings<BasePTSettingsTest>(file_name);
// Assert
Assert.IsTrue(actual_json.Equals(actual_json));
}
[TestMethod]
public void SaveSettings_ShouldCreateFile_WhenFilePathIsNotFound()
{
// Arrange
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);
// 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);
bool pathFound = SettingsUtils.SettingsFolderExists(file_name_exists);
// Assert
Assert.IsFalse(pathNotFound);
Assert.IsTrue(pathFound);
}
[TestMethod]
public void CreateSettingsFolder_ShouldCreateFolder_WhenSuccessful()
{
// Arrange
string file_name = "test\\" + RandomString();
// Act
SettingsUtils.CreateSettingsFolder(file_name);
// 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());
}
}
}

View File

@@ -0,0 +1,201 @@
// 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;
using System.Text.Json;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ViewModelTests
{
[TestClass]
public class General
{
public const string generalSettings_file_name = "Test\\GenealSettings";
[TestInitialize]
public void Setup()
{
// initialize creation of test settings file.
GeneralSettings generalSettings = new GeneralSettings();
SettingsUtils.SaveSettings(generalSettings.ToJsonString(), generalSettings_file_name);
}
[TestCleanup]
public void CleanUp()
{
// delete folder created.
if (SettingsUtils.SettingsFolderExists(generalSettings_file_name))
{
DeleteFolder(generalSettings_file_name);
}
}
public void DeleteFolder(string powertoy)
{
Directory.Delete(Path.Combine(SettingsUtils.LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"), true);
}
[TestMethod]
public void IsElevated_ShouldUpdateRunasAdminStatusAttrs_WhenSuccessful()
{
// Arrange
Func<string, int> SendMockIPCConfigMSG = msg => { return 0; };
Func<string, int> SendRestartAdminIPCMessage = msg => { return 0; };
Func<string, int> SendCheckForUpdatesIPCMessage = msg => { return 0; };
GeneralViewModel viewModel = new GeneralViewModel(
"GeneralSettings_RunningAsAdminText",
"GeneralSettings_RunningAsUserText",
false,
false,
UpdateUIThemeMethod,
SendMockIPCConfigMSG,
SendRestartAdminIPCMessage,
SendCheckForUpdatesIPCMessage,
generalSettings_file_name);
Assert.AreEqual(viewModel.RunningAsUserDefaultText, viewModel.RunningAsText);
Assert.IsFalse(viewModel.IsElevated);
// Act
viewModel.IsElevated = true;
// Assert
Assert.AreEqual(viewModel.RunningAsAdminDefaultText, viewModel.RunningAsText);
Assert.IsTrue(viewModel.IsElevated);
}
[TestMethod]
public void Startup_ShouldEnableRunOnStartUp_WhenSuccessful()
{
// Assert
Func<string, int> SendMockIPCConfigMSG = msg =>
{
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
Assert.IsTrue(snd.GeneralSettings.Startup);
return 0;
};
// Arrange
Func<string, int> SendRestartAdminIPCMessage = msg => { return 0; };
Func<string, int> SendCheckForUpdatesIPCMessage = msg => { return 0; };
GeneralViewModel viewModel = new GeneralViewModel(
"GeneralSettings_RunningAsAdminText",
"GeneralSettings_RunningAsUserText",
false,
false,
UpdateUIThemeMethod,
SendMockIPCConfigMSG,
SendRestartAdminIPCMessage,
SendCheckForUpdatesIPCMessage,
generalSettings_file_name);
Assert.IsFalse(viewModel.Startup);
// act
viewModel.Startup = true;
}
[TestMethod]
public void RunElevated_ShouldEnableAlwaysRunElevated_WhenSuccessful()
{
// Assert
Func<string, int> SendMockIPCConfigMSG = msg =>
{
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
Assert.IsTrue(snd.GeneralSettings.RunElevated);
return 0;
};
Func<string, int> SendRestartAdminIPCMessage = msg => { return 0; };
Func<string, int> SendCheckForUpdatesIPCMessage = msg => { return 0; };
// Arrange
GeneralViewModel viewModel = new GeneralViewModel(
"GeneralSettings_RunningAsAdminText",
"GeneralSettings_RunningAsUserText",
false,
false,
UpdateUIThemeMethod,
SendMockIPCConfigMSG,
SendRestartAdminIPCMessage,
SendCheckForUpdatesIPCMessage,
generalSettings_file_name);
Assert.IsFalse(viewModel.RunElevated);
// act
viewModel.RunElevated = true;
}
[TestMethod]
public void IsLightThemeRadioButtonChecked_ShouldThemeToLight_WhenSuccessful()
{
// Arrange
GeneralViewModel viewModel = null;
// Assert
Func<string, int> SendMockIPCConfigMSG = msg =>
{
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
Assert.AreEqual("light", snd.GeneralSettings.Theme);
return 0;
};
Func<string, int> SendRestartAdminIPCMessage = msg => { return 0; };
Func<string, int> SendCheckForUpdatesIPCMessage = msg => { return 0; };
viewModel = new GeneralViewModel(
"GeneralSettings_RunningAsAdminText",
"GeneralSettings_RunningAsUserText",
false,
false,
UpdateUIThemeMethod,
SendMockIPCConfigMSG,
SendRestartAdminIPCMessage,
SendCheckForUpdatesIPCMessage,
generalSettings_file_name);
Assert.IsFalse(viewModel.IsLightThemeRadioButtonChecked);
// act
viewModel.IsLightThemeRadioButtonChecked = true;
}
[TestMethod]
public void IsDarkThemeRadioButtonChecked_ShouldThemeToDark_WhenSuccessful()
{
// Arrange
// Assert
Func<string, int> SendMockIPCConfigMSG = msg =>
{
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
Assert.AreEqual("dark", snd.GeneralSettings.Theme);
return 0;
};
Func<string, int> SendRestartAdminIPCMessage = msg => { return 0; };
Func<string, int> SendCheckForUpdatesIPCMessage = msg => { return 0; };
GeneralViewModel viewModel = new GeneralViewModel(
"GeneralSettings_RunningAsAdminText",
"GeneralSettings_RunningAsUserText",
false,
false,
UpdateUIThemeMethod,
SendMockIPCConfigMSG,
SendRestartAdminIPCMessage,
SendCheckForUpdatesIPCMessage,
generalSettings_file_name);
Assert.IsFalse(viewModel.IsDarkThemeRadioButtonChecked);
// act
viewModel.IsDarkThemeRadioButtonChecked = true;
}
public int UpdateUIThemeMethod(string themeName)
{
return 0;
}
}
}