diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerLauncherSettings.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerLauncherSettings.cs index b41061bde9..2ce14acefa 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerLauncherSettings.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerLauncherSettings.cs @@ -2,10 +2,14 @@ // 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; + namespace Microsoft.PowerToys.Settings.UI.Lib { public class PowerLauncherSettings : BasePTModuleSettings { + public const string POWERTOYNAME = "PowerLauncher"; + public PowerLauncherProperties properties { get; set; } public PowerLauncherSettings() @@ -14,5 +18,16 @@ namespace Microsoft.PowerToys.Settings.UI.Lib version = "1"; name = "_unset_"; } + + public virtual void Save() + { + // Save settings to file + var options = new JsonSerializerOptions + { + WriteIndented = true, + }; + + SettingsUtils.SaveSettings(JsonSerializer.Serialize(this, options), POWERTOYNAME); + } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/PowerLauncherViewModel.cs b/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/PowerLauncherViewModel.cs index 20bae40952..1c2fbd0889 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/PowerLauncherViewModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/PowerLauncherViewModel.cs @@ -12,19 +12,36 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels { public class PowerLauncherViewModel : Observable { - private const string POWERTOYNAME = "PowerLauncher"; private PowerLauncherSettings settings; + public delegate void SendCallback(PowerLauncherSettings settings); + + private readonly SendCallback callback; + public PowerLauncherViewModel() { - if (SettingsUtils.SettingsExists(POWERTOYNAME)) + if (SettingsUtils.SettingsExists(PowerLauncherSettings.POWERTOYNAME)) { - settings = SettingsUtils.GetSettings(POWERTOYNAME); + settings = SettingsUtils.GetSettings(PowerLauncherSettings.POWERTOYNAME); } else { settings = new PowerLauncherSettings(); } + + callback = (PowerLauncherSettings settings) => + { + // Propagate changes to Power Launcher through IPC + var propertiesJson = JsonSerializer.Serialize(settings.properties); + ShellPage.DefaultSndMSGCallback( + string.Format("{{ \"{0}\": {1} }}", PowerLauncherSettings.POWERTOYNAME, JsonSerializer.Serialize(settings.properties))); + }; + } + + public PowerLauncherViewModel(PowerLauncherSettings settings, SendCallback callback) + { + this.settings = settings; + this.callback = callback; } private void UpdateSettings([CallerMemberName] string propertyName = null) @@ -32,17 +49,8 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels // Notify UI of property change OnPropertyChanged(propertyName); - // Save settings to file - var options = new JsonSerializerOptions - { - WriteIndented = true, - }; - SettingsUtils.SaveSettings(JsonSerializer.Serialize(settings, options), POWERTOYNAME); - - // Propagate changes to Power Launcher through IPC - var propertiesJson = JsonSerializer.Serialize(settings.properties); - ShellPage.DefaultSndMSGCallback( - string.Format("{{ \"{0}\": {1} }}", POWERTOYNAME, JsonSerializer.Serialize(settings.properties))); + settings.Save(); + callback(settings); } public bool EnablePowerLauncher diff --git a/src/core/Microsoft.PowerToys.Settings.UnitTest/Microsoft.PowerToys.Settings.UnitTest.csproj b/src/core/Microsoft.PowerToys.Settings.UnitTest/Microsoft.PowerToys.Settings.UnitTest.csproj index c6a9a2e9af..afead19b32 100644 --- a/src/core/Microsoft.PowerToys.Settings.UnitTest/Microsoft.PowerToys.Settings.UnitTest.csproj +++ b/src/core/Microsoft.PowerToys.Settings.UnitTest/Microsoft.PowerToys.Settings.UnitTest.csproj @@ -1,184 +1,185 @@ - - - - - Debug - x86 - {A80355C2-780D-4245-BD80-25B8DE698EE3} - AppContainerExe - Properties - Microsoft.PowerToys.Settings.UnitTest - Microsoft.PowerToys.Settings.UnitTest - en-US - UAP - 10.0.18362.0 - 10.0.18362.0 - 14 - 512 - {A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - $(VisualStudioVersion) - false - - - true - bin\x86\Debug\ - DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP - ;2008 - full - x86 - false - prompt - true - - - bin\x86\Release\ - TRACE;NETFX_CORE;WINDOWS_UWP - true - ;2008 - pdbonly - x86 - false - prompt - true - true - - - true - bin\ARM\Debug\ - DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP - ;2008 - full - ARM - false - prompt - true - - - bin\ARM\Release\ - TRACE;NETFX_CORE;WINDOWS_UWP - true - ;2008 - pdbonly - ARM - false - prompt - true - true - - - true - bin\ARM64\Debug\ - DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP - ;2008 - full - ARM64 - false - prompt - true - true - - - bin\ARM64\Release\ - TRACE;NETFX_CORE;WINDOWS_UWP - true - ;2008 - pdbonly - ARM64 - false - prompt - true - true - - - true - bin\x64\Debug\Test\ - DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP - ;2008 - full - x64 - false - prompt - true - - - bin\x64\Release\ - TRACE;NETFX_CORE;WINDOWS_UWP - true - ;2008 - pdbonly - x64 - false - prompt - true - false - - - PackageReference - - - - - - - - - - - UnitTestApp.xaml - - - - - - MSBuild:Compile - Designer - - - - - Designer - - - - - - - - - - - - - - 6.2.9 - - - 2.1.1 - - - 2.1.1 - - - - - {b1bcc8c6-46b5-4bfa-8f22-20f32d99ec6a} - Microsoft.PowerToys.Settings.UI.Lib - - - {a7d5099e-f0fd-4bf3-8522-5a682759f915} - Microsoft.PowerToys.Settings.UI - - - - - 14.0 - - - + + + + + Debug + x86 + {A80355C2-780D-4245-BD80-25B8DE698EE3} + AppContainerExe + Properties + Microsoft.PowerToys.Settings.UnitTest + Microsoft.PowerToys.Settings.UnitTest + en-US + UAP + 10.0.18362.0 + 10.0.18362.0 + 14 + 512 + {A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(VisualStudioVersion) + false + + + true + bin\x86\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + x86 + false + prompt + true + + + bin\x86\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + x86 + false + prompt + true + true + + + true + bin\ARM\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + ARM + false + prompt + true + + + bin\ARM\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + ARM + false + prompt + true + true + + + true + bin\ARM64\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + ARM64 + false + prompt + true + true + + + bin\ARM64\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + ARM64 + false + prompt + true + true + + + true + bin\x64\Debug\Test\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + x64 + false + prompt + true + + + bin\x64\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + x64 + false + prompt + true + false + + + PackageReference + + + + + + + + + + + UnitTestApp.xaml + + + + + + + MSBuild:Compile + Designer + + + + + Designer + + + + + + + + + + + + + + 6.2.9 + + + 2.1.1 + + + 2.1.1 + + + + + {b1bcc8c6-46b5-4bfa-8f22-20f32d99ec6a} + Microsoft.PowerToys.Settings.UI.Lib + + + {a7d5099e-f0fd-4bf3-8522-5a682759f915} + Microsoft.PowerToys.Settings.UI + + + + + 14.0 + + + \ No newline at end of file diff --git a/src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/PowerLauncherViewModelTest.cs b/src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/PowerLauncherViewModelTest.cs new file mode 100644 index 0000000000..de57d00dd8 --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/PowerLauncherViewModelTest.cs @@ -0,0 +1,159 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.PowerToys.Settings.UI.ViewModels; +using Microsoft.PowerToys.Settings.UI.Lib; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Text.Json; + +namespace Microsoft.PowerToys.Settings.UnitTest.ViewModelTests +{ + [TestClass] + public class PowerLauncherViewModelTest + { + class PowerLauncherSettingsMock : PowerLauncherSettings + { + public int TimesSaved { get; set; } + public override void Save() + { + TimesSaved++; + } + } + + class SendCallbackMock + { + public int TimesSent { get; set; } + public void OnSend(PowerLauncherSettings settings) + { + TimesSent++; + } + } + private PowerLauncherViewModel viewModel; + private PowerLauncherSettingsMock mockSettings; + private SendCallbackMock sendCallbackMock; + + + [TestInitialize] + public void Initialize() + { + mockSettings = new PowerLauncherSettingsMock(); + sendCallbackMock = new SendCallbackMock(); + + viewModel = new PowerLauncherViewModel( + mockSettings, + new PowerLauncherViewModel.SendCallback(sendCallbackMock.OnSend) + ); + } + + [TestMethod] + public void IsEnabled_ShouldEnableModule() + { + viewModel.EnablePowerLauncher = true; + + Assert.AreEqual(sendCallbackMock.TimesSent, 1); + Assert.AreEqual(mockSettings.TimesSaved, 1); + + Assert.IsTrue(mockSettings.properties.enable_powerlauncher == true); + } + + [TestMethod] + public void SearchPreference_ShouldUpdatePreferences() + { + viewModel.SearchResultPreference = "SearchOptionsAreNotValidated"; + viewModel.SearchTypePreference = "SearchOptionsAreNotValidated"; + + Assert.AreEqual(sendCallbackMock.TimesSent, 2); + Assert.AreEqual(mockSettings.TimesSaved, 2); + + Assert.IsTrue(mockSettings.properties.search_result_preference == "SearchOptionsAreNotValidated"); + Assert.IsTrue(mockSettings.properties.search_type_preference == "SearchOptionsAreNotValidated"); + } + + public void AssertHotkeySettings(HotkeySettings setting, bool win, bool ctrl, bool alt, bool shift, int code) + { + Assert.AreEqual(setting.Win, win); + Assert.AreEqual(setting.Ctrl, ctrl); + Assert.AreEqual(setting.Alt, alt); + Assert.AreEqual(setting.Shift, shift); + Assert.AreEqual(setting.Code, code); + } + + [TestMethod] + public void Hotkeys_ShouldUpdateHotkeys() + { + var openPowerLauncher = new HotkeySettings(); + openPowerLauncher.Win = true; + openPowerLauncher.Code = (int)Windows.System.VirtualKey.S; + + + var openFileLocation = new HotkeySettings(); + openFileLocation.Ctrl = true; + openFileLocation.Code = (int)Windows.System.VirtualKey.A; + + var openConsole = new HotkeySettings(); + openConsole.Alt = true; + openConsole.Code = (int)Windows.System.VirtualKey.D; + + var copyFileLocation = new HotkeySettings(); + copyFileLocation.Shift = true; + copyFileLocation.Code = (int)Windows.System.VirtualKey.F; + + viewModel.OpenPowerLauncher = openPowerLauncher; + viewModel.OpenFileLocation = openFileLocation; + viewModel.OpenConsole = openConsole; + viewModel.CopyPathLocation = copyFileLocation; + + Assert.AreEqual(mockSettings.TimesSaved, 4); + Assert.AreEqual(sendCallbackMock.TimesSent, 4); + + AssertHotkeySettings( + mockSettings.properties.open_powerlauncher, + true, + false, + false, + false, + (int)Windows.System.VirtualKey.S + ); + AssertHotkeySettings( + mockSettings.properties.open_file_location, + false, + true, + false, + false, + (int)Windows.System.VirtualKey.A + ); + AssertHotkeySettings( + mockSettings.properties.open_console, + false, + false, + true, + false, + (int)Windows.System.VirtualKey.D + ); + AssertHotkeySettings( + mockSettings.properties.copy_path_location, + false, + false, + false, + true, + (int)Windows.System.VirtualKey.F + ); + } + + [TestMethod] + public void Override_ShouldUpdateOverrides() + { + viewModel.OverrideWinRKey = true; + viewModel.OverrideWinSKey = false; + + + Assert.AreEqual(sendCallbackMock.TimesSent, 1); + Assert.AreEqual(mockSettings.TimesSaved, 1); + + Assert.IsTrue(mockSettings.properties.override_win_r_key); + Assert.IsFalse(mockSettings.properties.override_win_s_key); + } + } +} diff --git a/src/modules/fancyzones/tests/UnitTests/UnitTests.vcxproj.filters b/src/modules/fancyzones/tests/UnitTests/UnitTests.vcxproj.filters index 71de598915..a6affca694 100644 --- a/src/modules/fancyzones/tests/UnitTests/UnitTests.vcxproj.filters +++ b/src/modules/fancyzones/tests/UnitTests/UnitTests.vcxproj.filters @@ -52,6 +52,8 @@ - + + Source Files + \ No newline at end of file