2020-08-19 08:12:07 -07:00
// 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.
2020-11-17 15:29:21 -08:00
using System ;
2020-10-22 09:45:48 -07:00
using Microsoft.PowerToys.Settings.UI.Library ;
using Microsoft.PowerToys.Settings.UI.Library.ViewModels ;
2020-11-17 15:29:21 -08:00
using Microsoft.PowerToys.Settings.UI.UnitTests.BackwardsCompatibility ;
2020-08-19 08:12:07 -07:00
using Microsoft.VisualStudio.TestTools.UnitTesting ;
2020-09-21 10:14:44 -07:00
using Moq ;
2020-08-19 08:12:07 -07:00
namespace ViewModelTests
{
[TestClass]
public class PowerLauncherViewModelTest
{
private class SendCallbackMock
{
public int TimesSent { get ; set ; }
2020-10-06 15:00:25 -07:00
// PowerLauncherSettings is unused, but required according to SendCallback's signature.
// Naming parameter with discard symbol to suppress FxCop warnings.
2020-11-17 15:29:21 -08:00
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1313:Parameter names should begin with lower-case letter", Justification = "We actually don't validate setting, just calculate it was sent")]
2020-10-06 15:00:25 -07:00
public void OnSend ( PowerLauncherSettings _ )
2020-08-19 08:12:07 -07:00
{
TimesSent + + ;
}
}
private PowerLauncherViewModel viewModel ;
private PowerLauncherSettings mockSettings ;
private SendCallbackMock sendCallbackMock ;
2020-11-17 15:29:21 -08:00
2020-08-19 08:12:07 -07:00
[TestInitialize]
public void Initialize ( )
{
mockSettings = new PowerLauncherSettings ( ) ;
sendCallbackMock = new SendCallbackMock ( ) ;
viewModel = new PowerLauncherViewModel (
mockSettings ,
new PowerLauncherViewModel . SendCallback ( sendCallbackMock . OnSend ) ) ;
}
2020-10-08 16:34:19 -07:00
/// <summary>
/// Test if the original settings files were modified.
/// </summary>
[TestMethod]
[DataRow("v0.18.2", "settings.json")]
[DataRow("v0.19.2", "settings.json")]
[DataRow("v0.20.1", "settings.json")]
[DataRow("v0.21.1", "settings.json")]
[DataRow("v0.22.0", "settings.json")]
public void OriginalFilesModificationTest ( string version , string fileName )
{
2020-11-02 18:33:43 +01:00
var settingPathMock = new Mock < ISettingsPath > ( ) ;
2020-10-08 16:34:19 -07:00
var mockIOProvider = BackCompatTestProperties . GetModuleIOProvider ( version , PowerLauncherSettings . ModuleName , fileName ) ;
2020-11-02 18:33:43 +01:00
var mockSettingsUtils = new SettingsUtils ( mockIOProvider . Object , settingPathMock . Object ) ;
2021-01-14 14:14:29 +02:00
PowerLauncherSettings originalSettings = mockSettingsUtils . GetSettingsOrDefault < PowerLauncherSettings > ( PowerLauncherSettings . ModuleName ) ;
2020-10-08 16:34:19 -07:00
var mockGeneralIOProvider = BackCompatTestProperties . GetGeneralSettingsIOProvider ( version ) ;
2020-11-02 18:33:43 +01:00
var mockGeneralSettingsUtils = new SettingsUtils ( mockGeneralIOProvider . Object , settingPathMock . Object ) ;
2021-01-14 14:14:29 +02:00
GeneralSettings originalGeneralSettings = mockGeneralSettingsUtils . GetSettingsOrDefault < GeneralSettings > ( ) ;
2020-11-02 18:33:43 +01:00
2020-10-08 16:34:19 -07:00
var generalSettingsRepository = new BackCompatTestProperties . MockSettingsRepository < GeneralSettings > ( mockGeneralSettingsUtils ) ;
// Initialise View Model with test Config files
2020-11-17 15:29:21 -08:00
Func < string , int > sendMockIPCConfigMSG = msg = > { return 0 ; } ;
2021-03-15 14:52:03 +02:00
PowerLauncherViewModel viewModel = new PowerLauncherViewModel ( originalSettings , generalSettingsRepository , sendMockIPCConfigMSG , ( ) = > true ) ;
2020-10-08 16:34:19 -07:00
2020-10-30 14:42:49 -04:00
// Verify that the old settings persisted
2020-10-08 16:34:19 -07:00
Assert . AreEqual ( originalGeneralSettings . Enabled . PowerLauncher , viewModel . EnablePowerLauncher ) ;
Assert . AreEqual ( originalSettings . Properties . ClearInputOnLaunch , viewModel . ClearInputOnLaunch ) ;
Assert . AreEqual ( originalSettings . Properties . CopyPathLocation . ToString ( ) , viewModel . CopyPathLocation . ToString ( ) ) ;
Assert . AreEqual ( originalSettings . Properties . IgnoreHotkeysInFullscreen , viewModel . IgnoreHotkeysInFullScreen ) ;
Assert . AreEqual ( originalSettings . Properties . MaximumNumberOfResults , viewModel . MaximumNumberOfResults ) ;
Assert . AreEqual ( originalSettings . Properties . OpenPowerLauncher . ToString ( ) , viewModel . OpenPowerLauncher . ToString ( ) ) ;
Assert . AreEqual ( originalSettings . Properties . OverrideWinkeyR , viewModel . OverrideWinRKey ) ;
Assert . AreEqual ( originalSettings . Properties . OverrideWinkeyS , viewModel . OverrideWinSKey ) ;
Assert . AreEqual ( originalSettings . Properties . SearchResultPreference , viewModel . SearchResultPreference ) ;
Assert . AreEqual ( originalSettings . Properties . SearchTypePreference , viewModel . SearchTypePreference ) ;
2020-11-17 15:29:21 -08:00
// Verify that the stub file was used
var expectedCallCount = 2 ; // once via the view model, and once by the test (GetSettings<T>)
2020-10-08 16:34:19 -07:00
BackCompatTestProperties . VerifyGeneralSettingsIOProviderWasRead ( mockGeneralIOProvider , expectedCallCount ) ;
}
2020-08-19 08:12:07 -07:00
[TestMethod]
2020-10-06 15:00:25 -07:00
public void SearchPreferenceShouldUpdatePreferences ( )
2020-08-19 08:12:07 -07:00
{
viewModel . SearchResultPreference = "SearchOptionsAreNotValidated" ;
viewModel . SearchTypePreference = "SearchOptionsAreNotValidated" ;
Assert . AreEqual ( sendCallbackMock . TimesSent , 2 ) ;
Assert . IsTrue ( mockSettings . Properties . SearchResultPreference = = "SearchOptionsAreNotValidated" ) ;
Assert . IsTrue ( mockSettings . Properties . SearchTypePreference = = "SearchOptionsAreNotValidated" ) ;
}
2020-10-06 15:00:25 -07:00
public static void AssertHotkeySettings ( HotkeySettings setting , bool win , bool ctrl , bool alt , bool shift , int code )
2020-08-19 08:12:07 -07:00
{
2020-10-06 15:00:25 -07:00
if ( setting ! = null )
{
Assert . AreEqual ( win , setting . Win ) ;
Assert . AreEqual ( ctrl , setting . Ctrl ) ;
Assert . AreEqual ( alt , setting . Alt ) ;
Assert . AreEqual ( shift , setting . Shift ) ;
Assert . AreEqual ( code , setting . Code ) ;
2020-11-17 15:29:21 -08:00
}
else
2020-10-06 15:00:25 -07:00
{
Assert . Fail ( "setting parameter is null" ) ;
}
2020-08-19 08:12:07 -07:00
}
[TestMethod]
2020-10-06 15:00:25 -07:00
public void HotkeysShouldUpdateHotkeys ( )
2020-08-19 08:12:07 -07:00
{
var openPowerLauncher = new HotkeySettings ( ) ;
openPowerLauncher . Win = true ;
openPowerLauncher . Code = 83 ;
var openFileLocation = new HotkeySettings ( ) ;
openFileLocation . Ctrl = true ;
openFileLocation . Code = 65 ;
var openConsole = new HotkeySettings ( ) ;
openConsole . Alt = true ;
openConsole . Code = 68 ;
var copyFileLocation = new HotkeySettings ( ) ;
copyFileLocation . Shift = true ;
copyFileLocation . Code = 70 ;
viewModel . OpenPowerLauncher = openPowerLauncher ;
viewModel . OpenFileLocation = openFileLocation ;
viewModel . CopyPathLocation = copyFileLocation ;
Assert . AreEqual ( 3 , sendCallbackMock . TimesSent ) ;
AssertHotkeySettings (
mockSettings . Properties . OpenPowerLauncher ,
true ,
false ,
false ,
false ,
83 ) ;
AssertHotkeySettings (
mockSettings . Properties . OpenFileLocation ,
false ,
true ,
false ,
false ,
65 ) ;
AssertHotkeySettings (
mockSettings . Properties . CopyPathLocation ,
false ,
false ,
false ,
true ,
70 ) ;
}
[TestMethod]
2020-10-06 15:00:25 -07:00
public void OverrideShouldUpdateOverrides ( )
2020-08-19 08:12:07 -07:00
{
viewModel . OverrideWinRKey = true ;
viewModel . OverrideWinSKey = false ;
Assert . AreEqual ( 1 , sendCallbackMock . TimesSent ) ;
Assert . IsTrue ( mockSettings . Properties . OverrideWinkeyR ) ;
Assert . IsFalse ( mockSettings . Properties . OverrideWinkeyS ) ;
}
}
}