mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 10:46:33 +02:00
[Settings] Migrate PowerRename Tests (#5787)
* added MSTest project * migrated powerrename tests * updated test * reverted changes to xaml file
This commit is contained in:
@@ -2,33 +2,42 @@
|
||||
// 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 PowerRenameViewModel : Observable
|
||||
{
|
||||
private const string ModuleName = "PowerRename";
|
||||
|
||||
public string SettingsConfigFileFolder = string.Empty;
|
||||
|
||||
private PowerRenameSettings Settings { get; set; }
|
||||
|
||||
public PowerRenameViewModel()
|
||||
private Func<string, int> SendConfigMSG { get; }
|
||||
|
||||
public PowerRenameViewModel(Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "")
|
||||
{
|
||||
// Update Settings file folder:
|
||||
SettingsConfigFileFolder = configFileSubfolder;
|
||||
|
||||
try
|
||||
{
|
||||
PowerRenameLocalProperties localSettings = SettingsUtils.GetSettings<PowerRenameLocalProperties>(ModuleName, "power-rename-settings.json");
|
||||
PowerRenameLocalProperties localSettings = SettingsUtils.GetSettings<PowerRenameLocalProperties>(GetSettingsSubPath(), "power-rename-settings.json");
|
||||
Settings = new PowerRenameSettings(localSettings);
|
||||
}
|
||||
catch
|
||||
{
|
||||
PowerRenameLocalProperties localSettings = new PowerRenameLocalProperties();
|
||||
Settings = new PowerRenameSettings(localSettings);
|
||||
SettingsUtils.SaveSettings(localSettings.ToJsonString(), ModuleName, "power-rename-settings.json");
|
||||
SettingsUtils.SaveSettings(localSettings.ToJsonString(), GetSettingsSubPath(), "power-rename-settings.json");
|
||||
}
|
||||
|
||||
// set the callback functions value to hangle outgoing IPC message.
|
||||
SendConfigMSG = ipcMSGCallBackFunc;
|
||||
|
||||
_powerRenameEnabledOnContextMenu = Settings.Properties.ShowIcon.Value;
|
||||
_powerRenameEnabledOnContextExtendedMenu = Settings.Properties.ExtendedContextMenuOnly.Value;
|
||||
_powerRenameRestoreFlagsOnLaunch = Settings.Properties.PersistState.Value;
|
||||
@@ -67,17 +76,15 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
||||
{
|
||||
if (value != _powerRenameEnabled)
|
||||
{
|
||||
if (ShellPage.DefaultSndMSGCallback != null)
|
||||
{
|
||||
|
||||
GeneralSettings generalSettings = SettingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
generalSettings.Enabled.PowerRename = value;
|
||||
OutGoingGeneralSettings snd = new OutGoingGeneralSettings(generalSettings);
|
||||
ShellPage.DefaultSndMSGCallback(snd.ToString());
|
||||
SendConfigMSG(snd.ToString());
|
||||
|
||||
_powerRenameEnabled = value;
|
||||
OnPropertyChanged("IsEnabled");
|
||||
RaisePropertyChanged("GlobalAndMruEnabled");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -181,16 +188,21 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
||||
}
|
||||
}
|
||||
|
||||
public string GetSettingsSubPath()
|
||||
{
|
||||
return SettingsConfigFileFolder + "\\" + ModuleName;
|
||||
}
|
||||
|
||||
private void RaisePropertyChanged([CallerMemberName] string propertyName = null)
|
||||
{
|
||||
// Notify UI of property change
|
||||
OnPropertyChanged(propertyName);
|
||||
|
||||
if (ShellPage.DefaultSndMSGCallback != null)
|
||||
if (SendConfigMSG != null)
|
||||
{
|
||||
SndPowerRenameSettings snd = new SndPowerRenameSettings(Settings);
|
||||
SndModuleSettings<SndPowerRenameSettings> ipcMessage = new SndModuleSettings<SndPowerRenameSettings>(snd);
|
||||
ShellPage.DefaultSndMSGCallback(ipcMessage.ToJsonString());
|
||||
SendConfigMSG(ipcMessage.ToJsonString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 PowerRename
|
||||
{
|
||||
public const string ModuleName = "PowerRename";
|
||||
public const string generalSettings_file_name = "Test\\PowerRename";
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
// initialize creation of test settings file.
|
||||
GeneralSettings generalSettings = new GeneralSettings();
|
||||
PowerRenameSettings powerRename = new PowerRenameSettings();
|
||||
|
||||
SettingsUtils.SaveSettings(generalSettings.ToJsonString());
|
||||
SettingsUtils.SaveSettings(powerRename.ToJsonString(), generalSettings_file_name, "power-rename-settings.json");
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void CleanUp()
|
||||
{
|
||||
// delete folder created.
|
||||
if (SettingsUtils.SettingsFolderExists(generalSettings_file_name))
|
||||
{
|
||||
DeleteFolder(generalSettings_file_name);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsEnabled_ShouldEnableModule_WhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> SendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
|
||||
Assert.IsTrue(snd.GeneralSettings.Enabled.PowerRename);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
|
||||
// act
|
||||
viewModel.IsEnabled = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MRUEnabled_ShouldSetValue2True_WhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> SendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
PowerRenameSettingsIPCMessage snd = JsonSerializer.Deserialize<PowerRenameSettingsIPCMessage>(msg);
|
||||
Assert.IsTrue(snd.Powertoys.PowerRename.Properties.MRUEnabled.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
|
||||
// act
|
||||
viewModel.MRUEnabled = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void WhenIsEnabledIsOffAndMRUEnabledIsOffGlobalAndMruShouldBeOff()
|
||||
{
|
||||
Func<string, int> SendMockIPCConfigMSG = msg => { return 0; };
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
|
||||
viewModel.IsEnabled = false;
|
||||
viewModel.MRUEnabled = false;
|
||||
|
||||
Assert.IsFalse(viewModel.GlobalAndMruEnabled);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void WhenIsEnabledIsOffAndMRUEnabledIsOnGlobalAndMruShouldBeOff()
|
||||
{
|
||||
Func<string, int> SendMockIPCConfigMSG = msg => { return 0; };
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
|
||||
viewModel.IsEnabled = false;
|
||||
viewModel.MRUEnabled = true;
|
||||
|
||||
Assert.IsFalse(viewModel.GlobalAndMruEnabled);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void WhenIsEnabledIsOnAndMRUEnabledIsOffGlobalAndMruShouldBeOff()
|
||||
{
|
||||
Func<string, int> SendMockIPCConfigMSG = msg => { return 0; };
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
|
||||
viewModel.IsEnabled = true;
|
||||
viewModel.MRUEnabled = false;
|
||||
|
||||
Assert.IsFalse(viewModel.GlobalAndMruEnabled);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void WhenIsEnabledIsOnAndMRUEnabledIsOnGlobalAndMruShouldBeOn()
|
||||
{
|
||||
Func<string, int> SendMockIPCConfigMSG = msg => { return 0; };
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
|
||||
viewModel.IsEnabled = true;
|
||||
viewModel.MRUEnabled = true;
|
||||
|
||||
Assert.IsTrue(viewModel.GlobalAndMruEnabled);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnabledOnContextMenu_ShouldSetValue2True_WhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> SendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
PowerRenameSettingsIPCMessage snd = JsonSerializer.Deserialize<PowerRenameSettingsIPCMessage>(msg);
|
||||
Assert.IsTrue(snd.Powertoys.PowerRename.Properties.ShowIcon.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
|
||||
// act
|
||||
viewModel.EnabledOnContextMenu = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnabledOnContextExtendedMenu_ShouldSetValue2True_WhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> SendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
PowerRenameSettingsIPCMessage snd = JsonSerializer.Deserialize<PowerRenameSettingsIPCMessage>(msg);
|
||||
Assert.IsTrue(snd.Powertoys.PowerRename.Properties.ShowIcon.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
|
||||
// act
|
||||
viewModel.EnabledOnContextMenu = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void RestoreFlagsOnLaunch_ShouldSetValue2True_WhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> SendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
PowerRenameSettingsIPCMessage snd = JsonSerializer.Deserialize<PowerRenameSettingsIPCMessage>(msg);
|
||||
Assert.IsTrue(snd.Powertoys.PowerRename.Properties.PersistState.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
|
||||
// act
|
||||
viewModel.RestoreFlagsOnLaunch = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MaxDispListNum_ShouldSetMaxSuggListTo20_WhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> SendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
PowerRenameSettingsIPCMessage snd = JsonSerializer.Deserialize<PowerRenameSettingsIPCMessage>(msg);
|
||||
Assert.AreEqual(20, snd.Powertoys.PowerRename.Properties.MaxMRUSize.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
|
||||
// act
|
||||
viewModel.MaxDispListNum = 20;
|
||||
}
|
||||
|
||||
public void DeleteFolder(string powertoy)
|
||||
{
|
||||
Directory.Delete(Path.Combine(SettingsUtils.LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -113,7 +113,6 @@
|
||||
<Compile Include="ViewModels\ImageResizerViewModel.cs" />
|
||||
<Compile Include="ViewModels\KeyboardManagerViewModel.cs" />
|
||||
<Compile Include="ViewModels\PowerPreviewViewModel.cs" />
|
||||
<Compile Include="ViewModels\PowerRenameViewModel.cs" />
|
||||
<Compile Include="ViewModels\PowerLauncherViewModel.cs" />
|
||||
<Compile Include="ViewModels\ShellViewModel.cs" />
|
||||
<Compile Include="Views\ColorPickerPage.xaml.cs">
|
||||
|
||||
@@ -78,7 +78,7 @@
|
||||
<TextBlock x:Uid="PowerRename_Toggle_MaxDispListNum"
|
||||
Margin="{StaticResource SmallTopMargin}"
|
||||
Foreground="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabled, Converter={StaticResource ModuleEnabledToForegroundConverter}}"/>
|
||||
|
||||
|
||||
<muxc:NumberBox SpinButtonPlacementMode="Compact"
|
||||
HorizontalAlignment="Left"
|
||||
Margin="{StaticResource HeaderTextTopMargin}"
|
||||
|
||||
@@ -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
|
||||
@@ -13,8 +13,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
|
||||
public PowerRenamePage()
|
||||
{
|
||||
InitializeComponent();
|
||||
ViewModel = new PowerRenameViewModel();
|
||||
this.InitializeComponent();
|
||||
ViewModel = new PowerRenameViewModel(ShellPage.SendDefaultIPCMessage);
|
||||
|
||||
DataContext = ViewModel;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user