[Settings] Migrate Shortcut Guide Settings Tests (#5789)

* added MSTest project

* migrated shortcut guide tests

* updated tests

* updated tests

* reverted changes to xaml file
This commit is contained in:
Nkateko
2020-08-14 13:58:48 -07:00
committed by GitHub
parent 70d93a0bfc
commit 1683f191a9
5 changed files with 165 additions and 16 deletions

View File

@@ -2,12 +2,11 @@
// 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.Runtime.CompilerServices;
using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.Views;
using Microsoft.PowerToys.Settings.UI.Lib.Helpers;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
{
public class ShortcutGuideViewModel : Observable
{
@@ -15,16 +14,23 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
private const string ModuleName = "Shortcut Guide";
public ShortcutGuideViewModel()
private Func<string, int> SendConfigMSG { get; }
public string SettingsConfigFileFolder = string.Empty;
public ShortcutGuideViewModel(Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "")
{
// Update Settings file folder:
SettingsConfigFileFolder = configFileSubfolder;
try
{
Settings = SettingsUtils.GetSettings<ShortcutGuideSettings>(ModuleName);
Settings = SettingsUtils.GetSettings<ShortcutGuideSettings>(GetSettingsSubPath());
}
catch
{
Settings = new ShortcutGuideSettings();
SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
SettingsUtils.SaveSettings(Settings.ToJsonString(), GetSettingsSubPath());
}
GeneralSettings generalSettings;
@@ -39,9 +45,12 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
SettingsUtils.SaveSettings(generalSettings.ToJsonString(), string.Empty);
}
_isEnabled = generalSettings.Enabled.ShortcutGuide;
_pressTime = Settings.Properties.PressTime.Value;
_opacity = Settings.Properties.OverlayOpacity.Value;
// set the callback functions value to hangle outgoing IPC message.
SendConfigMSG = ipcMSGCallBackFunc;
this._isEnabled = generalSettings.Enabled.ShortcutGuide;
this._pressTime = Settings.Properties.PressTime.Value;
this._opacity = Settings.Properties.OverlayOpacity.Value;
string theme = Settings.Properties.Theme.Value;
@@ -81,7 +90,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
GeneralSettings generalSettings = SettingsUtils.GetSettings<GeneralSettings>(string.Empty);
generalSettings.Enabled.ShortcutGuide = value;
OutGoingGeneralSettings snd = new OutGoingGeneralSettings(generalSettings);
ShellPage.DefaultSndMSGCallback(snd.ToString());
SendConfigMSG(snd.ToString());
OnPropertyChanged("IsEnabled");
}
}
@@ -161,12 +170,17 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
public string GetSettingsSubPath()
{
return SettingsConfigFileFolder + "\\" + ModuleName;
}
public void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
OnPropertyChanged(propertyName);
SndShortcutGuideSettings outsettings = new SndShortcutGuideSettings(Settings);
SndModuleSettings<SndShortcutGuideSettings> ipcMessage = new SndModuleSettings<SndShortcutGuideSettings>(outsettings);
ShellPage.DefaultSndMSGCallback(ipcMessage.ToJsonString());
SendConfigMSG(ipcMessage.ToJsonString());
}
}
}

View File

@@ -0,0 +1,136 @@
// 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 ShortcutGuide
{
public const string ShortCutGuideTestFolderName = "Test\\ShortCutGuide";
[TestInitialize]
public void Setup()
{
// initialize creation of test settings file.
// Test base path:
// C:\Users\<user name>\AppData\Local\Packages\08e1807b-8b6d-4bfa-adc4-79c64aae8e78_9abkseg265h2m\LocalState\Microsoft\PowerToys\
GeneralSettings generalSettings = new GeneralSettings();
ShortcutGuideSettings shortcutGuide = new ShortcutGuideSettings();
SettingsUtils.SaveSettings(generalSettings.ToJsonString());
SettingsUtils.SaveSettings(shortcutGuide.ToJsonString(), ShortCutGuideTestFolderName);
}
[TestCleanup]
public void CleanUp()
{
// delete folder created.
// delete general settings folder.
string ShortCutGuideTestFolderName = string.Empty;
if (SettingsUtils.SettingsFolderExists(string.Empty))
{
DeleteFolder(string.Empty);
}
// delete power rename folder.
if (SettingsUtils.SettingsFolderExists(ShortCutGuideTestFolderName))
{
DeleteFolder(ShortCutGuideTestFolderName);
}
}
public void DeleteFolder(string powertoy)
{
Directory.Delete(Path.Combine(SettingsUtils.LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"), true);
}
[TestMethod]
public void IsEnabled_ShouldEnableModule_WhenSuccessful()
{
// Assert
// Initialize mock function of sending IPC message.
Func<string, int> SendMockIPCConfigMSG = msg =>
{
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
Assert.IsTrue(snd.GeneralSettings.Enabled.ShortcutGuide);
return 0;
};
// Arrange
ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(SendMockIPCConfigMSG, ShortCutGuideTestFolderName);
// Act
viewModel.IsEnabled = true;
}
[TestMethod]
public void ThemeIndex_ShouldSetThemeToDark_WhenSuccessful()
{
// Assert
// Initialize mock function of sending IPC message.
Func<string, int> SendMockIPCConfigMSG = msg =>
{
ShortcutGuideSettingsIPCMessage snd = JsonSerializer.Deserialize<ShortcutGuideSettingsIPCMessage>(msg);
Assert.AreEqual("dark", snd.Powertoys.ShortcutGuide.Properties.Theme.Value);
return 0;
};
// Arrange
ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(SendMockIPCConfigMSG, ShortCutGuideTestFolderName);
Assert.AreEqual(1, viewModel.ThemeIndex);
// Act
viewModel.ThemeIndex = 0;
}
[TestMethod]
public void PressTime_ShouldSetPressTimeToOneHundred_WhenSuccessful()
{
// Assert
// Initialize mock function of sending IPC message.
Func<string, int> SendMockIPCConfigMSG = msg =>
{
ShortcutGuideSettingsIPCMessage snd = JsonSerializer.Deserialize<ShortcutGuideSettingsIPCMessage>(msg);
Assert.AreEqual(100, snd.Powertoys.ShortcutGuide.Properties.PressTime.Value);
return 0;
};
// Arrange
ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(SendMockIPCConfigMSG, ShortCutGuideTestFolderName);
Assert.AreEqual(900, viewModel.PressTime);
// Act
viewModel.PressTime = 100;
}
[TestMethod]
public void OverlayOpacity_ShouldSeOverlayOpacityToOneHundred_WhenSuccessful()
{
// Assert
// Initialize mock function of sending IPC message.
Func<string, int> SendMockIPCConfigMSG = msg =>
{
ShortcutGuideSettingsIPCMessage snd = JsonSerializer.Deserialize<ShortcutGuideSettingsIPCMessage>(msg);
// Serialisation not working as expected in the test project:
Assert.AreEqual(100, snd.Powertoys.ShortcutGuide.Properties.OverlayOpacity.Value);
return 0;
};
// Arrange
ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(SendMockIPCConfigMSG, ShortCutGuideTestFolderName);
Assert.AreEqual(90, viewModel.OverlayOpacity);
// Act
viewModel.OverlayOpacity = 100;
}
}
}

View File

@@ -116,7 +116,6 @@
<Compile Include="ViewModels\PowerRenameViewModel.cs" />
<Compile Include="ViewModels\PowerLauncherViewModel.cs" />
<Compile Include="ViewModels\ShellViewModel.cs" />
<Compile Include="ViewModels\ShortcutGuideViewModel.cs" />
<Compile Include="Views\ColorPickerPage.xaml.cs">
<DependentUpon>ColorPickerPage.xaml</DependentUpon>
</Compile>

View File

@@ -92,7 +92,7 @@
<TextBlock x:Uid="ShortcutGuide_Theme"
Margin="{StaticResource SmallTopMargin}"
Foreground="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabled, Converter={StaticResource ModuleEnabledToForegroundConverter}}"/>
<muxc:RadioButtons IsEnabled="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabled}"
SelectedIndex="{x:Bind Mode=TwoWay, Path=ViewModel.ThemeIndex}"
Margin="{StaticResource HeaderTextTopMargin}">

View File

@@ -2,7 +2,7 @@
// 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.ViewModels;
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
using Windows.UI.Xaml.Controls;
namespace Microsoft.PowerToys.Settings.UI.Views
@@ -15,7 +15,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
{
InitializeComponent();
ViewModel = new ShortcutGuideViewModel();
ViewModel = new ShortcutGuideViewModel(ShellPage.SendDefaultIPCMessage);
DataContext = ViewModel;
}
}