mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
* 1) Making Directory Methods private. 2) Removing the CreateDirectory / DeleteDirectory functionality from all Settings Unit Tests. * Abstracting disk access via IIOProvider to be able to provide mocks for unit tests instead of writing to disk. This also prevents developers who are running unit tests from interfering with the PowerToys settings on their local dev box. * Dependency Injecting stub SettingsUtils for all tests * Removing ISettingsUtils from constructors of objects that need to be deserialized (ColorPickerSettings/PowerLauncherSettings) as this breaks System.Text.Json * Removing unused namespace reference * Removing redifined mock * As per PR feedback. Stub Settings utils should work with any settings type if the intent is to compile / avoid null ref exceptions. Strangely when implementing this fix it became apparent that a stub settings isn't enough, and disk access needed to be mocked. I can't explain why the tests were passing previously. * Leveraging GetMockIOProviderForSaveLoadExists
130 lines
4.2 KiB
C#
130 lines
4.2 KiB
C#
// 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 Microsoft.PowerToys.Settings.UI.Lib;
|
|
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
using Moq;
|
|
|
|
namespace ViewModelTests
|
|
{
|
|
[TestClass]
|
|
public class PowerLauncherViewModelTest
|
|
{
|
|
private class SendCallbackMock
|
|
{
|
|
public int TimesSent { get; set; }
|
|
|
|
public void OnSend(PowerLauncherSettings settings)
|
|
{
|
|
TimesSent++;
|
|
}
|
|
}
|
|
|
|
private PowerLauncherViewModel viewModel;
|
|
private PowerLauncherSettings mockSettings;
|
|
private SendCallbackMock sendCallbackMock;
|
|
[TestInitialize]
|
|
public void Initialize()
|
|
{
|
|
mockSettings = new PowerLauncherSettings();
|
|
sendCallbackMock = new SendCallbackMock();
|
|
viewModel = new PowerLauncherViewModel(
|
|
mockSettings,
|
|
new PowerLauncherViewModel.SendCallback(sendCallbackMock.OnSend));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void SearchPreference_ShouldUpdatePreferences()
|
|
{
|
|
viewModel.SearchResultPreference = "SearchOptionsAreNotValidated";
|
|
viewModel.SearchTypePreference = "SearchOptionsAreNotValidated";
|
|
|
|
Assert.AreEqual(sendCallbackMock.TimesSent, 2);
|
|
Assert.IsTrue(mockSettings.Properties.SearchResultPreference == "SearchOptionsAreNotValidated");
|
|
Assert.IsTrue(mockSettings.Properties.SearchTypePreference == "SearchOptionsAreNotValidated");
|
|
}
|
|
|
|
public void AssertHotkeySettings(HotkeySettings setting, bool win, bool ctrl, bool alt, bool shift, int code)
|
|
{
|
|
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);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Hotkeys_ShouldUpdateHotkeys()
|
|
{
|
|
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]
|
|
public void Override_ShouldUpdateOverrides()
|
|
{
|
|
viewModel.OverrideWinRKey = true;
|
|
viewModel.OverrideWinSKey = false;
|
|
|
|
Assert.AreEqual(1, sendCallbackMock.TimesSent);
|
|
|
|
Assert.IsTrue(mockSettings.Properties.OverrideWinkeyR);
|
|
Assert.IsFalse(mockSettings.Properties.OverrideWinkeyS);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void DriveDetectionViewModel_WhenSet_MustUpdateOverrides()
|
|
{
|
|
// Act
|
|
viewModel.DisableDriveDetectionWarning = true;
|
|
|
|
// Assert
|
|
Assert.AreEqual(1, sendCallbackMock.TimesSent);
|
|
Assert.IsTrue(mockSettings.Properties.DisableDriveDetectionWarning);
|
|
}
|
|
}
|
|
}
|