From 1683f191a955fb6d1064ac7d5e5f4bb464a6ef5d Mon Sep 17 00:00:00 2001 From: Nkateko <58791731+laviusmotileng-ms@users.noreply.github.com> Date: Fri, 14 Aug 2020 13:58:48 -0700 Subject: [PATCH] [Settings] Migrate Shortcut Guide Settings Tests (#5789) * added MSTest project * migrated shortcut guide tests * updated tests * updated tests * reverted changes to xaml file --- .../ViewModels/ShortcutGuideViewModel.cs | 38 +++-- .../ViewModelTests/ShortcutGuide.cs | 136 ++++++++++++++++++ .../Microsoft.PowerToys.Settings.UI.csproj | 1 - .../Views/ShortcutGuidePage.xaml | 2 +- .../Views/ShortcutGuidePage.xaml.cs | 4 +- 5 files changed, 165 insertions(+), 16 deletions(-) rename src/core/{Microsoft.PowerToys.Settings.UI => Microsoft.PowerToys.Settings.UI.Lib}/ViewModels/ShortcutGuideViewModel.cs (80%) create mode 100644 src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/ShortcutGuide.cs diff --git a/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/ShortcutGuideViewModel.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/ShortcutGuideViewModel.cs similarity index 80% rename from src/core/Microsoft.PowerToys.Settings.UI/ViewModels/ShortcutGuideViewModel.cs rename to src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/ShortcutGuideViewModel.cs index cc61c3748d..f196155572 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/ShortcutGuideViewModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/ShortcutGuideViewModel.cs @@ -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 SendConfigMSG { get; } + + public string SettingsConfigFileFolder = string.Empty; + + public ShortcutGuideViewModel(Func ipcMSGCallBackFunc, string configFileSubfolder = "") { + // Update Settings file folder: + SettingsConfigFileFolder = configFileSubfolder; + try { - Settings = SettingsUtils.GetSettings(ModuleName); + Settings = SettingsUtils.GetSettings(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(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 ipcMessage = new SndModuleSettings(outsettings); - ShellPage.DefaultSndMSGCallback(ipcMessage.ToJsonString()); + SendConfigMSG(ipcMessage.ToJsonString()); } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/ShortcutGuide.cs b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/ShortcutGuide.cs new file mode 100644 index 0000000000..7401f6d319 --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/ShortcutGuide.cs @@ -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\\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 SendMockIPCConfigMSG = msg => + { + OutGoingGeneralSettings snd = JsonSerializer.Deserialize(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 SendMockIPCConfigMSG = msg => + { + ShortcutGuideSettingsIPCMessage snd = JsonSerializer.Deserialize(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 SendMockIPCConfigMSG = msg => + { + ShortcutGuideSettingsIPCMessage snd = JsonSerializer.Deserialize(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 SendMockIPCConfigMSG = msg => + { + ShortcutGuideSettingsIPCMessage snd = JsonSerializer.Deserialize(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; + } + } +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Microsoft.PowerToys.Settings.UI.csproj b/src/core/Microsoft.PowerToys.Settings.UI/Microsoft.PowerToys.Settings.UI.csproj index 883095b0d1..8526dd23c8 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Microsoft.PowerToys.Settings.UI.csproj +++ b/src/core/Microsoft.PowerToys.Settings.UI/Microsoft.PowerToys.Settings.UI.csproj @@ -116,7 +116,6 @@ - ColorPickerPage.xaml diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Views/ShortcutGuidePage.xaml b/src/core/Microsoft.PowerToys.Settings.UI/Views/ShortcutGuidePage.xaml index b6029015ea..6df946513a 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Views/ShortcutGuidePage.xaml +++ b/src/core/Microsoft.PowerToys.Settings.UI/Views/ShortcutGuidePage.xaml @@ -92,7 +92,7 @@ - + diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Views/ShortcutGuidePage.xaml.cs b/src/core/Microsoft.PowerToys.Settings.UI/Views/ShortcutGuidePage.xaml.cs index af254364c5..0bc20f05fc 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Views/ShortcutGuidePage.xaml.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI/Views/ShortcutGuidePage.xaml.cs @@ -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; } }