mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 19:57:07 +02:00
[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:
@@ -2,12 +2,11 @@
|
|||||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||||
// See the LICENSE file in the project root for more information.
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using Microsoft.PowerToys.Settings.UI.Helpers;
|
using Microsoft.PowerToys.Settings.UI.Lib.Helpers;
|
||||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
|
||||||
using Microsoft.PowerToys.Settings.UI.Views;
|
|
||||||
|
|
||||||
namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||||
{
|
{
|
||||||
public class ShortcutGuideViewModel : Observable
|
public class ShortcutGuideViewModel : Observable
|
||||||
{
|
{
|
||||||
@@ -15,16 +14,23 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
|||||||
|
|
||||||
private const string ModuleName = "Shortcut Guide";
|
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
|
try
|
||||||
{
|
{
|
||||||
Settings = SettingsUtils.GetSettings<ShortcutGuideSettings>(ModuleName);
|
Settings = SettingsUtils.GetSettings<ShortcutGuideSettings>(GetSettingsSubPath());
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
Settings = new ShortcutGuideSettings();
|
Settings = new ShortcutGuideSettings();
|
||||||
SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
|
SettingsUtils.SaveSettings(Settings.ToJsonString(), GetSettingsSubPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
GeneralSettings generalSettings;
|
GeneralSettings generalSettings;
|
||||||
@@ -39,9 +45,12 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
|||||||
SettingsUtils.SaveSettings(generalSettings.ToJsonString(), string.Empty);
|
SettingsUtils.SaveSettings(generalSettings.ToJsonString(), string.Empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
_isEnabled = generalSettings.Enabled.ShortcutGuide;
|
// set the callback functions value to hangle outgoing IPC message.
|
||||||
_pressTime = Settings.Properties.PressTime.Value;
|
SendConfigMSG = ipcMSGCallBackFunc;
|
||||||
_opacity = Settings.Properties.OverlayOpacity.Value;
|
|
||||||
|
this._isEnabled = generalSettings.Enabled.ShortcutGuide;
|
||||||
|
this._pressTime = Settings.Properties.PressTime.Value;
|
||||||
|
this._opacity = Settings.Properties.OverlayOpacity.Value;
|
||||||
|
|
||||||
string theme = Settings.Properties.Theme.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 generalSettings = SettingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||||
generalSettings.Enabled.ShortcutGuide = value;
|
generalSettings.Enabled.ShortcutGuide = value;
|
||||||
OutGoingGeneralSettings snd = new OutGoingGeneralSettings(generalSettings);
|
OutGoingGeneralSettings snd = new OutGoingGeneralSettings(generalSettings);
|
||||||
ShellPage.DefaultSndMSGCallback(snd.ToString());
|
SendConfigMSG(snd.ToString());
|
||||||
OnPropertyChanged("IsEnabled");
|
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)
|
public void RaisePropertyChanged([CallerMemberName] string propertyName = null)
|
||||||
{
|
{
|
||||||
OnPropertyChanged(propertyName);
|
OnPropertyChanged(propertyName);
|
||||||
SndShortcutGuideSettings outsettings = new SndShortcutGuideSettings(Settings);
|
SndShortcutGuideSettings outsettings = new SndShortcutGuideSettings(Settings);
|
||||||
SndModuleSettings<SndShortcutGuideSettings> ipcMessage = new SndModuleSettings<SndShortcutGuideSettings>(outsettings);
|
SndModuleSettings<SndShortcutGuideSettings> ipcMessage = new SndModuleSettings<SndShortcutGuideSettings>(outsettings);
|
||||||
ShellPage.DefaultSndMSGCallback(ipcMessage.ToJsonString());
|
SendConfigMSG(ipcMessage.ToJsonString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -116,7 +116,6 @@
|
|||||||
<Compile Include="ViewModels\PowerRenameViewModel.cs" />
|
<Compile Include="ViewModels\PowerRenameViewModel.cs" />
|
||||||
<Compile Include="ViewModels\PowerLauncherViewModel.cs" />
|
<Compile Include="ViewModels\PowerLauncherViewModel.cs" />
|
||||||
<Compile Include="ViewModels\ShellViewModel.cs" />
|
<Compile Include="ViewModels\ShellViewModel.cs" />
|
||||||
<Compile Include="ViewModels\ShortcutGuideViewModel.cs" />
|
|
||||||
<Compile Include="Views\ColorPickerPage.xaml.cs">
|
<Compile Include="Views\ColorPickerPage.xaml.cs">
|
||||||
<DependentUpon>ColorPickerPage.xaml</DependentUpon>
|
<DependentUpon>ColorPickerPage.xaml</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
|||||||
@@ -92,7 +92,7 @@
|
|||||||
<TextBlock x:Uid="ShortcutGuide_Theme"
|
<TextBlock x:Uid="ShortcutGuide_Theme"
|
||||||
Margin="{StaticResource SmallTopMargin}"
|
Margin="{StaticResource SmallTopMargin}"
|
||||||
Foreground="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabled, Converter={StaticResource ModuleEnabledToForegroundConverter}}"/>
|
Foreground="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabled, Converter={StaticResource ModuleEnabledToForegroundConverter}}"/>
|
||||||
|
|
||||||
<muxc:RadioButtons IsEnabled="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabled}"
|
<muxc:RadioButtons IsEnabled="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabled}"
|
||||||
SelectedIndex="{x:Bind Mode=TwoWay, Path=ViewModel.ThemeIndex}"
|
SelectedIndex="{x:Bind Mode=TwoWay, Path=ViewModel.ThemeIndex}"
|
||||||
Margin="{StaticResource HeaderTextTopMargin}">
|
Margin="{StaticResource HeaderTextTopMargin}">
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||||
// See the LICENSE file in the project root for more information.
|
// 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;
|
using Windows.UI.Xaml.Controls;
|
||||||
|
|
||||||
namespace Microsoft.PowerToys.Settings.UI.Views
|
namespace Microsoft.PowerToys.Settings.UI.Views
|
||||||
@@ -15,7 +15,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
|||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
|
||||||
ViewModel = new ShortcutGuideViewModel();
|
ViewModel = new ShortcutGuideViewModel(ShellPage.SendDefaultIPCMessage);
|
||||||
DataContext = ViewModel;
|
DataContext = ViewModel;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user