mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 10:46:33 +02:00
common: refactor common library pt2 (#8588)
- remove common lib - split settings, remove common-md - move ipc interop/kb_layout to interop - rename core -> settings, settings -> old_settings - os-detect header-only; interop -> PowerToysInterop - split notifications, move single-use headers where they're used - winstore lib - rename com utils - rename Updating and Telemetry projects - rename core -> settings-ui and remove examples folder - rename settings-ui folder + consisent common/version include
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
// 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.Text.Json;
|
||||
using Microsoft.PowerToys.Settings.UI.Library;
|
||||
using Microsoft.PowerToys.Settings.UI.Library.ViewModels;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.BackwardsCompatibility;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace ViewModelTests
|
||||
{
|
||||
[TestClass]
|
||||
public class ColorPicker
|
||||
{
|
||||
/// <summary>
|
||||
/// Test if the original settings files were modified.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
[DataRow("v0.20.1", "settings.json")] // Color picker was introduced in .20
|
||||
[DataRow("v0.21.1", "settings.json")]
|
||||
[DataRow("v0.22.0", "settings.json")]
|
||||
public void OriginalFilesModificationTest(string version, string fileName)
|
||||
{
|
||||
// Arrange
|
||||
var mockIOProvider = BackCompatTestProperties.GetModuleIOProvider(version, ColorPickerSettings.ModuleName, fileName);
|
||||
var settingPathMock = new Mock<ISettingsPath>();
|
||||
|
||||
var mockSettingsUtils = new SettingsUtils(mockIOProvider.Object, settingPathMock.Object);
|
||||
ColorPickerSettings originalSettings = mockSettingsUtils.GetSettings<ColorPickerSettings>(ColorPickerSettings.ModuleName);
|
||||
|
||||
var mockGeneralIOProvider = BackCompatTestProperties.GetGeneralSettingsIOProvider(version);
|
||||
|
||||
var mockGeneralSettingsUtils = new SettingsUtils(mockGeneralIOProvider.Object, settingPathMock.Object);
|
||||
GeneralSettings originalGeneralSettings = mockGeneralSettingsUtils.GetSettings<GeneralSettings>();
|
||||
var generalSettingsRepository = new BackCompatTestProperties.MockSettingsRepository<GeneralSettings>(mockGeneralSettingsUtils);
|
||||
|
||||
// Act
|
||||
// Initialise View Model with test Config files
|
||||
using (var viewModel = new ColorPickerViewModel(mockSettingsUtils, generalSettingsRepository, ColorPickerIsEnabledByDefaultIPC))
|
||||
{
|
||||
// Assert
|
||||
// Verify that the old settings persisted
|
||||
Assert.AreEqual(originalGeneralSettings.Enabled.ColorPicker, viewModel.IsEnabled);
|
||||
Assert.AreEqual(originalSettings.Properties.ActivationShortcut.ToString(), viewModel.ActivationShortcut.ToString());
|
||||
Assert.AreEqual(originalSettings.Properties.ChangeCursor, viewModel.ChangeCursor);
|
||||
|
||||
// Verify that the stub file was used
|
||||
var expectedCallCount = 2; // once via the view model, and once by the test (GetSettings<T>)
|
||||
BackCompatTestProperties.VerifyModuleIOProviderWasRead(mockIOProvider, ColorPickerSettings.ModuleName, expectedCallCount);
|
||||
BackCompatTestProperties.VerifyGeneralSettingsIOProviderWasRead(mockGeneralIOProvider, expectedCallCount);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ColorPickerIsEnabledByDefault()
|
||||
{
|
||||
var mockSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<ColorPickerSettings>();
|
||||
using (var viewModel = new ColorPickerViewModel(ISettingsUtilsMocks.GetStubSettingsUtils<ColorPickerSettings>().Object, SettingsRepository<GeneralSettings>.GetInstance(ISettingsUtilsMocks.GetStubSettingsUtils<GeneralSettings>().Object), ColorPickerIsEnabledByDefaultIPC))
|
||||
{
|
||||
Assert.IsTrue(viewModel.IsEnabled);
|
||||
}
|
||||
}
|
||||
|
||||
private static int ColorPickerIsEnabledByDefaultIPC(string msg)
|
||||
{
|
||||
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
|
||||
Assert.IsTrue(snd.GeneralSettings.Enabled.ColorPicker);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,435 @@
|
||||
// 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.Text.Json;
|
||||
using Microsoft.PowerToys.Settings.UI.Library;
|
||||
using Microsoft.PowerToys.Settings.UI.Library.ViewModels;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.BackwardsCompatibility;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace ViewModelTests
|
||||
{
|
||||
[TestClass]
|
||||
public class FancyZones
|
||||
{
|
||||
public const string FancyZonesTestFolderName = "Test\\FancyZones";
|
||||
|
||||
/// <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)
|
||||
{
|
||||
var settingPathMock = new Mock<ISettingsPath>();
|
||||
|
||||
var fileMock = BackCompatTestProperties.GetModuleIOProvider(version, FancyZonesSettings.ModuleName, fileName);
|
||||
var mockSettingsUtils = new SettingsUtils(fileMock.Object, settingPathMock.Object);
|
||||
FancyZonesSettings originalSettings = mockSettingsUtils.GetSettings<FancyZonesSettings>(FancyZonesSettings.ModuleName);
|
||||
|
||||
var mockGeneralIOProvider = BackCompatTestProperties.GetGeneralSettingsIOProvider(version);
|
||||
var mockGeneralSettingsUtils = new SettingsUtils(mockGeneralIOProvider.Object, settingPathMock.Object);
|
||||
GeneralSettings originalGeneralSettings = mockGeneralSettingsUtils.GetSettings<GeneralSettings>();
|
||||
|
||||
var generalSettingsRepository = new BackCompatTestProperties.MockSettingsRepository<GeneralSettings>(mockGeneralSettingsUtils);
|
||||
var fancyZonesRepository = new BackCompatTestProperties.MockSettingsRepository<FancyZonesSettings>(mockSettingsUtils);
|
||||
|
||||
// Initialise View Model with test Config files
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(generalSettingsRepository, fancyZonesRepository, ColorPickerIsEnabledByDefault_IPC);
|
||||
|
||||
// Verify that the old settings persisted
|
||||
Assert.AreEqual(originalGeneralSettings.Enabled.FancyZones, viewModel.IsEnabled);
|
||||
Assert.AreEqual(originalSettings.Properties.FancyzonesAppLastZoneMoveWindows.Value, viewModel.AppLastZoneMoveWindows);
|
||||
Assert.AreEqual(originalSettings.Properties.FancyzonesBorderColor.Value, viewModel.ZoneBorderColor);
|
||||
Assert.AreEqual(originalSettings.Properties.FancyzonesDisplayChangeMoveWindows.Value, viewModel.DisplayChangeMoveWindows);
|
||||
Assert.AreEqual(originalSettings.Properties.FancyzonesEditorHotkey.Value.ToString(), viewModel.EditorHotkey.ToString());
|
||||
Assert.AreEqual(originalSettings.Properties.FancyzonesExcludedApps.Value, viewModel.ExcludedApps);
|
||||
Assert.AreEqual(originalSettings.Properties.FancyzonesHighlightOpacity.Value, viewModel.HighlightOpacity);
|
||||
Assert.AreEqual(originalSettings.Properties.FancyzonesInActiveColor.Value, viewModel.ZoneInActiveColor);
|
||||
Assert.AreEqual(originalSettings.Properties.FancyzonesMakeDraggedWindowTransparent.Value, viewModel.MakeDraggedWindowsTransparent);
|
||||
Assert.AreEqual(originalSettings.Properties.FancyzonesMouseSwitch.Value, viewModel.MouseSwitch);
|
||||
Assert.AreEqual(originalSettings.Properties.FancyzonesMoveWindowsAcrossMonitors.Value, viewModel.MoveWindowsAcrossMonitors);
|
||||
Assert.AreEqual(originalSettings.Properties.FancyzonesMoveWindowsBasedOnPosition.Value, viewModel.MoveWindowsBasedOnPosition);
|
||||
Assert.AreEqual(originalSettings.Properties.FancyzonesOpenWindowOnActiveMonitor.Value, viewModel.OpenWindowOnActiveMonitor);
|
||||
Assert.AreEqual(originalSettings.Properties.FancyzonesOverrideSnapHotkeys.Value, viewModel.OverrideSnapHotkeys);
|
||||
Assert.AreEqual(originalSettings.Properties.FancyzonesRestoreSize.Value, viewModel.RestoreSize);
|
||||
Assert.AreEqual(originalSettings.Properties.FancyzonesShiftDrag.Value, viewModel.ShiftDrag);
|
||||
Assert.AreEqual(originalSettings.Properties.FancyzonesShowOnAllMonitors.Value, viewModel.ShowOnAllMonitors);
|
||||
Assert.AreEqual(originalSettings.Properties.FancyzonesSpanZonesAcrossMonitors.Value, viewModel.SpanZonesAcrossMonitors);
|
||||
Assert.AreEqual(originalSettings.Properties.FancyzonesZoneHighlightColor.Value, viewModel.ZoneHighlightColor);
|
||||
Assert.AreEqual(originalSettings.Properties.FancyzonesZoneSetChangeMoveWindows.Value, viewModel.ZoneSetChangeMoveWindows);
|
||||
Assert.AreEqual(originalSettings.Properties.UseCursorposEditorStartupscreen.Value, viewModel.UseCursorPosEditorStartupScreen);
|
||||
|
||||
// Verify that the stub file was used
|
||||
var expectedCallCount = 2; // once via the view model, and once by the test (GetSettings<T>)
|
||||
BackCompatTestProperties.VerifyModuleIOProviderWasRead(fileMock, FancyZonesSettings.ModuleName, expectedCallCount);
|
||||
BackCompatTestProperties.VerifyGeneralSettingsIOProviderWasRead(mockGeneralIOProvider, expectedCallCount);
|
||||
}
|
||||
|
||||
private int ColorPickerIsEnabledByDefault_IPC(string msg)
|
||||
{
|
||||
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
|
||||
Assert.IsTrue(snd.GeneralSettings.Enabled.ColorPicker);
|
||||
return 0;
|
||||
}
|
||||
|
||||
private Mock<ISettingsUtils> mockGeneralSettingsUtils;
|
||||
|
||||
private Mock<ISettingsUtils> mockFancyZonesSettingsUtils;
|
||||
|
||||
[TestInitialize]
|
||||
public void SetUpStubSettingUtils()
|
||||
{
|
||||
mockGeneralSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<GeneralSettings>();
|
||||
mockFancyZonesSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<FancyZonesSettings>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsEnabledShouldDisableModuleWhenSuccessful()
|
||||
{
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
|
||||
Assert.IsFalse(snd.GeneralSettings.Enabled.FancyZones);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<FancyZonesSettings>.GetInstance(mockFancyZonesSettingsUtils.Object), sendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsTrue(viewModel.IsEnabled); // check if the module is enabled.
|
||||
|
||||
// act
|
||||
viewModel.IsEnabled = false;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ShiftDragShouldSetValue2FalseWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
FancyZonesSettingsIPCMessage snd = JsonSerializer.Deserialize<FancyZonesSettingsIPCMessage>(msg);
|
||||
Assert.IsFalse(snd.Powertoys.FancyZones.Properties.FancyzonesShiftDrag.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<FancyZonesSettings>.GetInstance(mockFancyZonesSettingsUtils.Object), sendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsTrue(viewModel.ShiftDrag); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
viewModel.ShiftDrag = false;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void OverrideSnapHotkeysShouldSetValue2TrueWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
FancyZonesSettingsIPCMessage snd = JsonSerializer.Deserialize<FancyZonesSettingsIPCMessage>(msg);
|
||||
Assert.IsTrue(snd.Powertoys.FancyZones.Properties.FancyzonesOverrideSnapHotkeys.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<FancyZonesSettings>.GetInstance(mockFancyZonesSettingsUtils.Object), sendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsFalse(viewModel.OverrideSnapHotkeys); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
viewModel.OverrideSnapHotkeys = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MoveWindowsBasedOnPositionShouldSetValue2TrueWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
FancyZonesSettingsIPCMessage snd = JsonSerializer.Deserialize<FancyZonesSettingsIPCMessage>(msg);
|
||||
Assert.IsTrue(snd.Powertoys.FancyZones.Properties.FancyzonesMoveWindowsBasedOnPosition.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<FancyZonesSettings>.GetInstance(mockFancyZonesSettingsUtils.Object), sendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsFalse(viewModel.MoveWindowsBasedOnPosition); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
viewModel.MoveWindowsBasedOnPosition = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ZoneSetChangeFlashZonesShouldSetValue2FalseWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
FancyZonesSettingsIPCMessage snd = JsonSerializer.Deserialize<FancyZonesSettingsIPCMessage>(msg);
|
||||
Assert.IsTrue(snd.Powertoys.FancyZones.Properties.FancyzonesMakeDraggedWindowTransparent.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<FancyZonesSettings>.GetInstance(mockFancyZonesSettingsUtils.Object), sendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsFalse(viewModel.MakeDraggedWindowsTransparent); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
viewModel.MakeDraggedWindowsTransparent = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MouseSwitchShouldSetValue2TrueWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
FancyZonesSettingsIPCMessage snd = JsonSerializer.Deserialize<FancyZonesSettingsIPCMessage>(msg);
|
||||
Assert.IsTrue(snd.Powertoys.FancyZones.Properties.FancyzonesMouseSwitch.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<FancyZonesSettings>.GetInstance(mockFancyZonesSettingsUtils.Object), sendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsFalse(viewModel.MouseSwitch); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
viewModel.MouseSwitch = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DisplayChangeMoveWindowsShouldSetValue2TrueWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
FancyZonesSettingsIPCMessage snd = JsonSerializer.Deserialize<FancyZonesSettingsIPCMessage>(msg);
|
||||
Assert.IsTrue(snd.Powertoys.FancyZones.Properties.FancyzonesDisplayChangeMoveWindows.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<FancyZonesSettings>.GetInstance(mockFancyZonesSettingsUtils.Object), sendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsFalse(viewModel.DisplayChangeMoveWindows); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
viewModel.DisplayChangeMoveWindows = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ZoneSetChangeMoveWindowsShouldSetValue2TrueWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
FancyZonesSettingsIPCMessage snd = JsonSerializer.Deserialize<FancyZonesSettingsIPCMessage>(msg);
|
||||
Assert.IsTrue(snd.Powertoys.FancyZones.Properties.FancyzonesZoneSetChangeMoveWindows.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<FancyZonesSettings>.GetInstance(mockFancyZonesSettingsUtils.Object), sendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsFalse(viewModel.ZoneSetChangeMoveWindows); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
viewModel.ZoneSetChangeMoveWindows = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AppLastZoneMoveWindowsShouldSetValue2TrueWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
FancyZonesSettingsIPCMessage snd = JsonSerializer.Deserialize<FancyZonesSettingsIPCMessage>(msg);
|
||||
Assert.IsTrue(snd.Powertoys.FancyZones.Properties.FancyzonesAppLastZoneMoveWindows.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<FancyZonesSettings>.GetInstance(mockFancyZonesSettingsUtils.Object), sendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsFalse(viewModel.AppLastZoneMoveWindows); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
viewModel.AppLastZoneMoveWindows = true;
|
||||
}
|
||||
|
||||
public void OpenWindowOnActiveMonitorShouldSetValue2TrueWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
FancyZonesSettingsIPCMessage snd = JsonSerializer.Deserialize<FancyZonesSettingsIPCMessage>(msg);
|
||||
Assert.IsTrue(snd.Powertoys.FancyZones.Properties.FancyzonesOpenWindowOnActiveMonitor.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<FancyZonesSettings>.GetInstance(mockFancyZonesSettingsUtils.Object), sendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsFalse(viewModel.OpenWindowOnActiveMonitor); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
viewModel.OpenWindowOnActiveMonitor = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void RestoreSizeShouldSetValue2TrueWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
FancyZonesSettingsIPCMessage snd = JsonSerializer.Deserialize<FancyZonesSettingsIPCMessage>(msg);
|
||||
Assert.IsTrue(snd.Powertoys.FancyZones.Properties.FancyzonesRestoreSize.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<FancyZonesSettings>.GetInstance(mockFancyZonesSettingsUtils.Object), sendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsFalse(viewModel.RestoreSize); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
viewModel.RestoreSize = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void UseCursorPosEditorStartupScreenShouldSetValue2FalseWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
FancyZonesSettingsIPCMessage snd = JsonSerializer.Deserialize<FancyZonesSettingsIPCMessage>(msg);
|
||||
Assert.IsTrue(snd.Powertoys.FancyZones.Properties.UseCursorposEditorStartupscreen.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<FancyZonesSettings>.GetInstance(mockFancyZonesSettingsUtils.Object), sendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsTrue(viewModel.UseCursorPosEditorStartupScreen); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
viewModel.UseCursorPosEditorStartupScreen = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ShowOnAllMonitorsShouldSetValue2TrueWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
FancyZonesSettingsIPCMessage snd = JsonSerializer.Deserialize<FancyZonesSettingsIPCMessage>(msg);
|
||||
Assert.IsTrue(snd.Powertoys.FancyZones.Properties.FancyzonesShowOnAllMonitors.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<FancyZonesSettings>.GetInstance(mockFancyZonesSettingsUtils.Object), sendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsFalse(viewModel.ShowOnAllMonitors); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
viewModel.ShowOnAllMonitors = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ZoneHighlightColorShouldSetColorValue2WhiteWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
FancyZonesSettingsIPCMessage snd = JsonSerializer.Deserialize<FancyZonesSettingsIPCMessage>(msg);
|
||||
Assert.AreEqual("#FFFFFF", snd.Powertoys.FancyZones.Properties.FancyzonesZoneHighlightColor.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<FancyZonesSettings>.GetInstance(mockFancyZonesSettingsUtils.Object), sendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.AreEqual(ConfigDefaults.DefaultFancyZonesZoneHighlightColor, viewModel.ZoneHighlightColor);
|
||||
|
||||
// act
|
||||
viewModel.ZoneHighlightColor = "#FFFFFF";
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ZoneBorderColorShouldSetColorValue2WhiteWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
FancyZonesSettingsIPCMessage snd = JsonSerializer.Deserialize<FancyZonesSettingsIPCMessage>(msg);
|
||||
Assert.AreEqual("#FFFFFF", snd.Powertoys.FancyZones.Properties.FancyzonesBorderColor.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<FancyZonesSettings>.GetInstance(mockFancyZonesSettingsUtils.Object), sendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.AreEqual(ConfigDefaults.DefaultFancyzonesBorderColor, viewModel.ZoneBorderColor);
|
||||
|
||||
// act
|
||||
viewModel.ZoneBorderColor = "#FFFFFF";
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ZoneInActiveColorShouldSetColorValue2WhiteWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
FancyZonesSettingsIPCMessage snd = JsonSerializer.Deserialize<FancyZonesSettingsIPCMessage>(msg);
|
||||
Assert.AreEqual("#FFFFFF", snd.Powertoys.FancyZones.Properties.FancyzonesInActiveColor.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<FancyZonesSettings>.GetInstance(mockFancyZonesSettingsUtils.Object), sendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.AreEqual(ConfigDefaults.DefaultFancyZonesInActiveColor, viewModel.ZoneInActiveColor);
|
||||
|
||||
// act
|
||||
viewModel.ZoneInActiveColor = "#FFFFFF";
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ExcludedAppsShouldSetColorValue2WhiteWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
FancyZonesSettingsIPCMessage snd = JsonSerializer.Deserialize<FancyZonesSettingsIPCMessage>(msg);
|
||||
Assert.AreEqual("Sample", snd.Powertoys.FancyZones.Properties.FancyzonesExcludedApps.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<FancyZonesSettings>.GetInstance(mockFancyZonesSettingsUtils.Object), sendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.AreEqual(string.Empty, viewModel.ExcludedApps);
|
||||
|
||||
// act
|
||||
viewModel.ExcludedApps = "Sample";
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void HighlightOpacityShouldSetOpacityValueTo60WhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
FancyZonesSettingsIPCMessage snd = JsonSerializer.Deserialize<FancyZonesSettingsIPCMessage>(msg);
|
||||
Assert.AreEqual(60, snd.Powertoys.FancyZones.Properties.FancyzonesHighlightOpacity.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<FancyZonesSettings>.GetInstance(mockFancyZonesSettingsUtils.Object), sendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.AreEqual(50, viewModel.HighlightOpacity);
|
||||
|
||||
// act
|
||||
viewModel.HighlightOpacity = 60;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,254 @@
|
||||
// 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 Microsoft.PowerToys.Settings.UI.Library;
|
||||
using Microsoft.PowerToys.Settings.UI.Library.ViewModels;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.BackwardsCompatibility;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using JsonSerializer = System.Text.Json.JsonSerializer;
|
||||
|
||||
namespace ViewModelTests
|
||||
{
|
||||
[TestClass]
|
||||
public class General
|
||||
{
|
||||
public const string GeneralSettingsFileName = "Test\\GeneralSettings";
|
||||
|
||||
private Mock<ISettingsUtils> mockGeneralSettingsUtils;
|
||||
|
||||
[TestInitialize]
|
||||
public void SetUpStubSettingUtils()
|
||||
{
|
||||
mockGeneralSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<GeneralSettings>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[DataRow("v0.18.2")]
|
||||
[DataRow("v0.19.2")]
|
||||
[DataRow("v0.20.1")]
|
||||
[DataRow("v0.21.1")]
|
||||
[DataRow("v0.22.0")]
|
||||
public void OriginalFilesModificationTest(string version)
|
||||
{
|
||||
var settingPathMock = new Mock<ISettingsPath>();
|
||||
var fileMock = BackCompatTestProperties.GetGeneralSettingsIOProvider(version);
|
||||
|
||||
var mockGeneralSettingsUtils = new SettingsUtils(fileMock.Object, settingPathMock.Object);
|
||||
GeneralSettings originalGeneralSettings = mockGeneralSettingsUtils.GetSettings<GeneralSettings>();
|
||||
|
||||
var generalSettingsRepository = new BackCompatTestProperties.MockSettingsRepository<GeneralSettings>(mockGeneralSettingsUtils);
|
||||
|
||||
// Initialise View Model with test Config files
|
||||
// Arrange
|
||||
Func<string, int> sendMockIPCConfigMSG = msg => 0;
|
||||
Func<string, int> sendRestartAdminIPCMessage = msg => 0;
|
||||
Func<string, int> sendCheckForUpdatesIPCMessage = msg => 0;
|
||||
var viewModel = new GeneralViewModel(
|
||||
settingsRepository: generalSettingsRepository,
|
||||
runAsAdminText: "GeneralSettings_RunningAsAdminText",
|
||||
runAsUserText: "GeneralSettings_RunningAsUserText",
|
||||
isElevated: false,
|
||||
isAdmin: false,
|
||||
updateTheme: UpdateUIThemeMethod,
|
||||
ipcMSGCallBackFunc: sendMockIPCConfigMSG,
|
||||
ipcMSGRestartAsAdminMSGCallBackFunc: sendRestartAdminIPCMessage,
|
||||
ipcMSGCheckForUpdatesCallBackFunc: sendCheckForUpdatesIPCMessage,
|
||||
configFileSubfolder: string.Empty);
|
||||
|
||||
// Verify that the old settings persisted
|
||||
Assert.AreEqual(originalGeneralSettings.AutoDownloadUpdates, viewModel.AutoDownloadUpdates);
|
||||
Assert.AreEqual(originalGeneralSettings.Packaged, viewModel.Packaged);
|
||||
Assert.AreEqual(originalGeneralSettings.PowertoysVersion, viewModel.PowerToysVersion);
|
||||
Assert.AreEqual(originalGeneralSettings.RunElevated, viewModel.RunElevated);
|
||||
Assert.AreEqual(originalGeneralSettings.Startup, viewModel.Startup);
|
||||
|
||||
// Verify that the stub file was used
|
||||
var expectedCallCount = 2; // once via the view model, and once by the test (GetSettings<T>)
|
||||
BackCompatTestProperties.VerifyGeneralSettingsIOProviderWasRead(fileMock, expectedCallCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsElevatedShouldUpdateRunasAdminStatusAttrsWhenSuccessful()
|
||||
{
|
||||
// Arrange
|
||||
Func<string, int> sendMockIPCConfigMSG = msg => { return 0; };
|
||||
Func<string, int> sendRestartAdminIPCMessage = msg => { return 0; };
|
||||
Func<string, int> sendCheckForUpdatesIPCMessage = msg => { return 0; };
|
||||
GeneralViewModel viewModel = new GeneralViewModel(
|
||||
settingsRepository: SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object),
|
||||
"GeneralSettings_RunningAsAdminText",
|
||||
"GeneralSettings_RunningAsUserText",
|
||||
false,
|
||||
false,
|
||||
UpdateUIThemeMethod,
|
||||
sendMockIPCConfigMSG,
|
||||
sendRestartAdminIPCMessage,
|
||||
sendCheckForUpdatesIPCMessage,
|
||||
GeneralSettingsFileName);
|
||||
|
||||
Assert.AreEqual(viewModel.RunningAsUserDefaultText, viewModel.RunningAsText);
|
||||
Assert.IsFalse(viewModel.IsElevated);
|
||||
|
||||
// Act
|
||||
viewModel.IsElevated = true;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(viewModel.RunningAsAdminDefaultText, viewModel.RunningAsText);
|
||||
Assert.IsTrue(viewModel.IsElevated);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StartupShouldEnableRunOnStartUpWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
|
||||
Assert.IsTrue(snd.GeneralSettings.Startup);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// Arrange
|
||||
Func<string, int> sendRestartAdminIPCMessage = msg => { return 0; };
|
||||
Func<string, int> sendCheckForUpdatesIPCMessage = msg => { return 0; };
|
||||
GeneralViewModel viewModel = new GeneralViewModel(
|
||||
settingsRepository: SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object),
|
||||
"GeneralSettings_RunningAsAdminText",
|
||||
"GeneralSettings_RunningAsUserText",
|
||||
false,
|
||||
false,
|
||||
UpdateUIThemeMethod,
|
||||
sendMockIPCConfigMSG,
|
||||
sendRestartAdminIPCMessage,
|
||||
sendCheckForUpdatesIPCMessage,
|
||||
GeneralSettingsFileName);
|
||||
Assert.IsFalse(viewModel.Startup);
|
||||
|
||||
// act
|
||||
viewModel.Startup = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void RunElevatedShouldEnableAlwaysRunElevatedWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
|
||||
Assert.IsTrue(snd.GeneralSettings.RunElevated);
|
||||
return 0;
|
||||
};
|
||||
|
||||
Func<string, int> sendRestartAdminIPCMessage = msg => { return 0; };
|
||||
Func<string, int> sendCheckForUpdatesIPCMessage = msg => { return 0; };
|
||||
|
||||
// Arrange
|
||||
GeneralViewModel viewModel = new GeneralViewModel(
|
||||
settingsRepository: SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object),
|
||||
"GeneralSettings_RunningAsAdminText",
|
||||
"GeneralSettings_RunningAsUserText",
|
||||
false,
|
||||
false,
|
||||
UpdateUIThemeMethod,
|
||||
sendMockIPCConfigMSG,
|
||||
sendRestartAdminIPCMessage,
|
||||
sendCheckForUpdatesIPCMessage,
|
||||
GeneralSettingsFileName);
|
||||
|
||||
Assert.IsFalse(viewModel.RunElevated);
|
||||
|
||||
// act
|
||||
viewModel.RunElevated = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsLightThemeRadioButtonCheckedShouldThemeToLightWhenSuccessful()
|
||||
{
|
||||
// Arrange
|
||||
GeneralViewModel viewModel = null;
|
||||
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
|
||||
Assert.AreEqual("light", snd.GeneralSettings.Theme);
|
||||
return 0;
|
||||
};
|
||||
|
||||
Func<string, int> sendRestartAdminIPCMessage = msg => { return 0; };
|
||||
Func<string, int> sendCheckForUpdatesIPCMessage = msg => { return 0; };
|
||||
viewModel = new GeneralViewModel(
|
||||
settingsRepository: SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object),
|
||||
"GeneralSettings_RunningAsAdminText",
|
||||
"GeneralSettings_RunningAsUserText",
|
||||
false,
|
||||
false,
|
||||
UpdateUIThemeMethod,
|
||||
sendMockIPCConfigMSG,
|
||||
sendRestartAdminIPCMessage,
|
||||
sendCheckForUpdatesIPCMessage,
|
||||
GeneralSettingsFileName);
|
||||
Assert.IsFalse(viewModel.IsLightThemeRadioButtonChecked);
|
||||
|
||||
// act
|
||||
viewModel.IsLightThemeRadioButtonChecked = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsDarkThemeRadioButtonCheckedShouldThemeToDarkWhenSuccessful()
|
||||
{
|
||||
// Arrange
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
|
||||
Assert.AreEqual("dark", snd.GeneralSettings.Theme);
|
||||
return 0;
|
||||
};
|
||||
|
||||
Func<string, int> sendRestartAdminIPCMessage = msg => { return 0; };
|
||||
Func<string, int> sendCheckForUpdatesIPCMessage = msg => { return 0; };
|
||||
GeneralViewModel viewModel = new GeneralViewModel(
|
||||
settingsRepository: SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object),
|
||||
"GeneralSettings_RunningAsAdminText",
|
||||
"GeneralSettings_RunningAsUserText",
|
||||
false,
|
||||
false,
|
||||
UpdateUIThemeMethod,
|
||||
sendMockIPCConfigMSG,
|
||||
sendRestartAdminIPCMessage,
|
||||
sendCheckForUpdatesIPCMessage,
|
||||
GeneralSettingsFileName);
|
||||
Assert.IsFalse(viewModel.IsDarkThemeRadioButtonChecked);
|
||||
|
||||
// act
|
||||
viewModel.IsDarkThemeRadioButtonChecked = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AllModulesAreEnabledByDefault()
|
||||
{
|
||||
// arrange
|
||||
EnabledModules modules = new EnabledModules();
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(modules.FancyZones);
|
||||
Assert.IsTrue(modules.ImageResizer);
|
||||
Assert.IsTrue(modules.FileExplorerPreview);
|
||||
Assert.IsTrue(modules.ShortcutGuide);
|
||||
Assert.IsTrue(modules.PowerRename);
|
||||
Assert.IsTrue(modules.KeyboardManager);
|
||||
Assert.IsTrue(modules.PowerLauncher);
|
||||
Assert.IsTrue(modules.ColorPicker);
|
||||
}
|
||||
|
||||
public static int UpdateUIThemeMethod(string themeName)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,274 @@
|
||||
// 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.Abstractions.TestingHelpers;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using Microsoft.PowerToys.Settings.UI.Library;
|
||||
using Microsoft.PowerToys.Settings.UI.Library.ViewModels;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.BackwardsCompatibility;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace ViewModelTests
|
||||
{
|
||||
[TestClass]
|
||||
public class ImageResizer
|
||||
{
|
||||
private Mock<ISettingsUtils> _mockGeneralSettingsUtils;
|
||||
|
||||
private Mock<ISettingsUtils> _mockImgResizerSettingsUtils;
|
||||
|
||||
[TestInitialize]
|
||||
public void SetUpStubSettingUtils()
|
||||
{
|
||||
_mockGeneralSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<GeneralSettings>();
|
||||
_mockImgResizerSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<ImageResizerSettings>();
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
var settingPathMock = new Mock<ISettingsPath>();
|
||||
|
||||
var fileMock = BackCompatTestProperties.GetModuleIOProvider(version, ImageResizerSettings.ModuleName, fileName);
|
||||
var mockSettingsUtils = new SettingsUtils(fileMock.Object, settingPathMock.Object);
|
||||
|
||||
ImageResizerSettings originalSettings = mockSettingsUtils.GetSettings<ImageResizerSettings>(ImageResizerSettings.ModuleName);
|
||||
|
||||
var mockGeneralFileMock = BackCompatTestProperties.GetGeneralSettingsIOProvider(version);
|
||||
var mockGeneralSettingsUtils = new SettingsUtils(mockGeneralFileMock.Object, settingPathMock.Object);
|
||||
GeneralSettings originalGeneralSettings = mockGeneralSettingsUtils.GetSettings<GeneralSettings>();
|
||||
|
||||
var generalSettingsRepository = new BackCompatTestProperties.MockSettingsRepository<GeneralSettings>(mockGeneralSettingsUtils);
|
||||
|
||||
// Initialise View Model with test Config files
|
||||
Func<string, int> sendMockIPCConfigMSG = msg => { return 0; };
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils, generalSettingsRepository, sendMockIPCConfigMSG);
|
||||
|
||||
// Verify that the old settings persisted
|
||||
Assert.AreEqual(originalGeneralSettings.Enabled.ImageResizer, viewModel.IsEnabled);
|
||||
Assert.AreEqual(ImageResizerViewModel.GetEncoderIndex(originalSettings.Properties.ImageresizerFallbackEncoder.Value), viewModel.Encoder);
|
||||
Assert.AreEqual(originalSettings.Properties.ImageresizerFileName.Value, viewModel.FileName);
|
||||
Assert.AreEqual(originalSettings.Properties.ImageresizerJpegQualityLevel.Value, viewModel.JPEGQualityLevel);
|
||||
Assert.AreEqual(originalSettings.Properties.ImageresizerKeepDateModified.Value, viewModel.KeepDateModified);
|
||||
Assert.AreEqual(originalSettings.Properties.ImageresizerPngInterlaceOption.Value, viewModel.PngInterlaceOption);
|
||||
Assert.AreEqual(originalSettings.Properties.ImageresizerSizes.Value.Count, viewModel.Sizes.Count);
|
||||
Assert.AreEqual(originalSettings.Properties.ImageresizerTiffCompressOption.Value, viewModel.TiffCompressOption);
|
||||
|
||||
// Verify that the stub file was used
|
||||
var expectedCallCount = 2; // once via the view model, and once by the test (GetSettings<T>)
|
||||
BackCompatTestProperties.VerifyModuleIOProviderWasRead(fileMock, ImageResizerSettings.ModuleName, expectedCallCount);
|
||||
BackCompatTestProperties.VerifyGeneralSettingsIOProviderWasRead(mockGeneralFileMock, expectedCallCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsEnabledShouldEnableModuleWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
|
||||
Assert.IsTrue(snd.GeneralSettings.Enabled.ImageResizer);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(_mockImgResizerSettingsUtils.Object, SettingsRepository<GeneralSettings>.GetInstance(_mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG);
|
||||
|
||||
// act
|
||||
viewModel.IsEnabled = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void JPEGQualityLevelShouldSetValueToTenWhenSuccessful()
|
||||
{
|
||||
// arrange
|
||||
var fileSystemMock = new MockFileSystem();
|
||||
var mockSettingsUtils = new SettingsUtils(fileSystemMock);
|
||||
Func<string, int> sendMockIPCConfigMSG = msg => { return 0; };
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils, SettingsRepository<GeneralSettings>.GetInstance(_mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG);
|
||||
|
||||
// act
|
||||
viewModel.JPEGQualityLevel = 10;
|
||||
|
||||
// Assert
|
||||
viewModel = new ImageResizerViewModel(mockSettingsUtils, SettingsRepository<GeneralSettings>.GetInstance(_mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG);
|
||||
Assert.AreEqual(10, viewModel.JPEGQualityLevel);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PngInterlaceOptionShouldSetValueToTenWhenSuccessful()
|
||||
{
|
||||
// arrange
|
||||
var fileSystemMock = new MockFileSystem();
|
||||
var mockSettingsUtils = new SettingsUtils(fileSystemMock);
|
||||
Func<string, int> sendMockIPCConfigMSG = msg => { return 0; };
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils, SettingsRepository<GeneralSettings>.GetInstance(_mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG);
|
||||
|
||||
// act
|
||||
viewModel.PngInterlaceOption = 10;
|
||||
|
||||
// Assert
|
||||
viewModel = new ImageResizerViewModel(mockSettingsUtils, SettingsRepository<GeneralSettings>.GetInstance(_mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG);
|
||||
Assert.AreEqual(10, viewModel.PngInterlaceOption);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TiffCompressOptionShouldSetValueToTenWhenSuccessful()
|
||||
{
|
||||
// arrange
|
||||
var fileSystemMock = new MockFileSystem();
|
||||
var mockSettingsUtils = new SettingsUtils(fileSystemMock);
|
||||
Func<string, int> sendMockIPCConfigMSG = msg => { return 0; };
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils, SettingsRepository<GeneralSettings>.GetInstance(_mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG);
|
||||
|
||||
// act
|
||||
viewModel.TiffCompressOption = 10;
|
||||
|
||||
// Assert
|
||||
viewModel = new ImageResizerViewModel(mockSettingsUtils, SettingsRepository<GeneralSettings>.GetInstance(_mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG);
|
||||
Assert.AreEqual(10, viewModel.TiffCompressOption);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FileNameShouldUpdateValueWhenSuccessful()
|
||||
{
|
||||
// arrange
|
||||
var fileSystemMock = new MockFileSystem();
|
||||
var mockSettingsUtils = new SettingsUtils(fileSystemMock);
|
||||
Func<string, int> sendMockIPCConfigMSG = msg => { return 0; };
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils, SettingsRepository<GeneralSettings>.GetInstance(_mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG);
|
||||
string expectedValue = "%1 (%3)";
|
||||
|
||||
// act
|
||||
viewModel.FileName = expectedValue;
|
||||
|
||||
// Assert
|
||||
viewModel = new ImageResizerViewModel(mockSettingsUtils, SettingsRepository<GeneralSettings>.GetInstance(_mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG);
|
||||
Assert.AreEqual(expectedValue, viewModel.FileName);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void KeepDateModifiedShouldUpdateValueWhenSuccessful()
|
||||
{
|
||||
// arrange
|
||||
var settingUtils = ISettingsUtilsMocks.GetStubSettingsUtils<ImageResizerSettings>();
|
||||
|
||||
var expectedSettingsString = new ImageResizerSettings() { Properties = new ImageResizerProperties() { ImageresizerKeepDateModified = new BoolProperty() { Value = true } } }.ToJsonString();
|
||||
|
||||
// Using Ordinal since this is used internally
|
||||
settingUtils.Setup(x => x.SaveSettings(
|
||||
It.Is<string>(content => content.Equals(expectedSettingsString, StringComparison.Ordinal)),
|
||||
It.Is<string>(module => module.Equals(ImageResizerSettings.ModuleName, StringComparison.Ordinal)),
|
||||
It.IsAny<string>()))
|
||||
.Verifiable();
|
||||
|
||||
Func<string, int> sendMockIPCConfigMSG = msg => { return 0; };
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(settingUtils.Object, SettingsRepository<GeneralSettings>.GetInstance(_mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG);
|
||||
|
||||
// act
|
||||
viewModel.KeepDateModified = true;
|
||||
|
||||
// Assert
|
||||
settingUtils.Verify();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EncoderShouldUpdateValueWhenSuccessful()
|
||||
{
|
||||
// arrange
|
||||
var fileSystemMock = new MockFileSystem();
|
||||
var mockSettingsUtils = new SettingsUtils(fileSystemMock);
|
||||
Func<string, int> sendMockIPCConfigMSG = msg => { return 0; };
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils, SettingsRepository<GeneralSettings>.GetInstance(_mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG);
|
||||
|
||||
// act
|
||||
viewModel.Encoder = 3;
|
||||
|
||||
// Assert
|
||||
viewModel = new ImageResizerViewModel(mockSettingsUtils, SettingsRepository<GeneralSettings>.GetInstance(_mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG);
|
||||
Assert.AreEqual("163bcc30-e2e9-4f0b-961d-a3e9fdb788a3", viewModel.EncoderGuid);
|
||||
Assert.AreEqual(3, viewModel.Encoder);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AddRowShouldAddEmptyImageSizeWhenSuccessful()
|
||||
{
|
||||
// arrange
|
||||
var mockSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<ImageResizerSettings>();
|
||||
Func<string, int> sendMockIPCConfigMSG = msg => { return 0; };
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils.Object, SettingsRepository<GeneralSettings>.GetInstance(_mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG);
|
||||
int sizeOfOriginalArray = viewModel.Sizes.Count;
|
||||
|
||||
// act
|
||||
viewModel.AddRow();
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(sizeOfOriginalArray + 1, viewModel.Sizes.Count);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DeleteImageSizeShouldDeleteImageSizeWhenSuccessful()
|
||||
{
|
||||
// arrange
|
||||
var mockSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<ImageResizerSettings>();
|
||||
Func<string, int> sendMockIPCConfigMSG = msg => { return 0; };
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils.Object, SettingsRepository<GeneralSettings>.GetInstance(_mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG);
|
||||
int sizeOfOriginalArray = viewModel.Sizes.Count;
|
||||
ImageSize deleteCandidate = viewModel.Sizes.Where<ImageSize>(x => x.Id == 0).First();
|
||||
|
||||
// act
|
||||
viewModel.DeleteImageSize(0);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(sizeOfOriginalArray - 1, viewModel.Sizes.Count);
|
||||
Assert.IsFalse(viewModel.Sizes.Contains(deleteCandidate));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void UpdateWidthAndHeightShouldUpdateSizeWhenCorrectValuesAreProvided()
|
||||
{
|
||||
// arrange
|
||||
ImageSize imageSize = new ImageSize()
|
||||
{
|
||||
Id = 0,
|
||||
Name = "Test",
|
||||
Fit = (int)ResizeFit.Fit,
|
||||
Width = 30,
|
||||
Height = 30,
|
||||
Unit = (int)ResizeUnit.Pixel,
|
||||
};
|
||||
|
||||
double negativeWidth = -2.0;
|
||||
double negativeHeight = -2.0;
|
||||
|
||||
// Act
|
||||
imageSize.Width = negativeWidth;
|
||||
imageSize.Height = negativeHeight;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(0, imageSize.Width);
|
||||
Assert.AreEqual(0, imageSize.Height);
|
||||
|
||||
// Act
|
||||
imageSize.Width = 50;
|
||||
imageSize.Height = 50;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(50, imageSize.Width);
|
||||
Assert.AreEqual(50, imageSize.Height);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
// 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.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.PowerToys.Settings.UI.Library;
|
||||
using Microsoft.PowerToys.Settings.UI.Library.ViewModels;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace ViewModelTests
|
||||
{
|
||||
[TestClass]
|
||||
public class KeyboardManager
|
||||
{
|
||||
public const string Module = KeyboardManagerSettings.ModuleName;
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void CleanUp()
|
||||
{
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CombineShortcutListsShouldReturnEmptyListWhenBothArgumentsAreEmptyLists()
|
||||
{
|
||||
// arrange
|
||||
var firstList = new List<KeysDataModel>();
|
||||
var secondList = new List<AppSpecificKeysDataModel>();
|
||||
|
||||
// act
|
||||
var result = KeyboardManagerViewModel.CombineShortcutLists(firstList, secondList);
|
||||
|
||||
// Assert
|
||||
var expectedResult = new List<AppSpecificKeysDataModel>();
|
||||
|
||||
Assert.AreEqual(expectedResult.Count, result.Count);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CombineShortcutListsShouldReturnEmptyListWhenBothArgumentsAreNull()
|
||||
{
|
||||
// act
|
||||
var result = KeyboardManagerViewModel.CombineShortcutLists(null, null);
|
||||
|
||||
// Assert
|
||||
var expectedResult = new List<AppSpecificKeysDataModel>();
|
||||
|
||||
Assert.AreEqual(expectedResult.Count, result.Count);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CombineShortcutListsShouldReturnListWithOneAppSpecificEntryWhenFirstArgumentIsNullAndSecondArgumentHasOneEntry()
|
||||
{
|
||||
// arrange
|
||||
var secondList = new List<AppSpecificKeysDataModel>();
|
||||
var entry = new AppSpecificKeysDataModel();
|
||||
entry.OriginalKeys = "17;65";
|
||||
entry.NewRemapKeys = "17;86";
|
||||
entry.TargetApp = "msedge";
|
||||
secondList.Add(entry);
|
||||
|
||||
// act
|
||||
var result = KeyboardManagerViewModel.CombineShortcutLists(null, secondList);
|
||||
|
||||
// Assert
|
||||
var expectedResult = new List<AppSpecificKeysDataModel>();
|
||||
var expectedEntry = new AppSpecificKeysDataModel();
|
||||
expectedEntry.OriginalKeys = entry.OriginalKeys;
|
||||
expectedEntry.NewRemapKeys = entry.NewRemapKeys;
|
||||
expectedEntry.TargetApp = entry.TargetApp;
|
||||
expectedResult.Add(expectedEntry);
|
||||
|
||||
Assert.AreEqual(expectedResult.Count, result.Count);
|
||||
Assert.IsTrue(expectedResult[0].Compare(result[0]));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CombineShortcutListsShouldReturnListWithOneAllAppsEntryWhenFirstArgumentHasOneEntryAndSecondArgumentIsNull()
|
||||
{
|
||||
// arrange
|
||||
var firstList = new List<KeysDataModel>();
|
||||
var entry = new KeysDataModel();
|
||||
entry.OriginalKeys = "17;65";
|
||||
entry.NewRemapKeys = "17;86";
|
||||
firstList.Add(entry);
|
||||
|
||||
// act
|
||||
var result = KeyboardManagerViewModel.CombineShortcutLists(firstList, null);
|
||||
|
||||
// Assert
|
||||
var expectedResult = new List<AppSpecificKeysDataModel>();
|
||||
var expectedEntry = new AppSpecificKeysDataModel();
|
||||
expectedEntry.OriginalKeys = entry.OriginalKeys;
|
||||
expectedEntry.NewRemapKeys = entry.NewRemapKeys;
|
||||
expectedEntry.TargetApp = "All Apps";
|
||||
expectedResult.Add(expectedEntry);
|
||||
|
||||
Assert.AreEqual(expectedResult.Count, result.Count);
|
||||
Assert.IsTrue(expectedResult[0].Compare(result[0]));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CombineShortcutListsShouldReturnListWithOneAllAppsEntryWhenFirstArgumentHasOneEntryAndSecondArgumentIsEmpty()
|
||||
{
|
||||
// arrange
|
||||
var firstList = new List<KeysDataModel>();
|
||||
var entry = new KeysDataModel();
|
||||
entry.OriginalKeys = "17;65";
|
||||
entry.NewRemapKeys = "17;86";
|
||||
firstList.Add(entry);
|
||||
var secondList = new List<AppSpecificKeysDataModel>();
|
||||
|
||||
// act
|
||||
var result = KeyboardManagerViewModel.CombineShortcutLists(firstList, secondList);
|
||||
|
||||
// Assert
|
||||
var expectedResult = new List<AppSpecificKeysDataModel>();
|
||||
var expectedEntry = new AppSpecificKeysDataModel();
|
||||
expectedEntry.OriginalKeys = entry.OriginalKeys;
|
||||
expectedEntry.NewRemapKeys = entry.NewRemapKeys;
|
||||
expectedEntry.TargetApp = "All Apps";
|
||||
expectedResult.Add(expectedEntry);
|
||||
var x = expectedResult[0].Equals(result[0]);
|
||||
Assert.AreEqual(expectedResult.Count, result.Count);
|
||||
Assert.IsTrue(expectedResult[0].Compare(result[0]));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CombineShortcutListsShouldReturnListWithOneAppSpecificEntryWhenFirstArgumentIsEmptyAndSecondArgumentHasOneEntry()
|
||||
{
|
||||
// arrange
|
||||
var firstList = new List<KeysDataModel>();
|
||||
var secondList = new List<AppSpecificKeysDataModel>();
|
||||
var entry = new AppSpecificKeysDataModel();
|
||||
entry.OriginalKeys = "17;65";
|
||||
entry.NewRemapKeys = "17;86";
|
||||
entry.TargetApp = "msedge";
|
||||
secondList.Add(entry);
|
||||
|
||||
// act
|
||||
var result = KeyboardManagerViewModel.CombineShortcutLists(firstList, secondList);
|
||||
|
||||
// Assert
|
||||
var expectedResult = new List<AppSpecificKeysDataModel>();
|
||||
var expectedEntry = new AppSpecificKeysDataModel();
|
||||
expectedEntry.OriginalKeys = entry.OriginalKeys;
|
||||
expectedEntry.NewRemapKeys = entry.NewRemapKeys;
|
||||
expectedEntry.TargetApp = entry.TargetApp;
|
||||
expectedResult.Add(expectedEntry);
|
||||
|
||||
Assert.AreEqual(expectedResult.Count, result.Count);
|
||||
Assert.IsTrue(expectedResult[0].Compare(result[0]));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CombineShortcutListsShouldReturnListWithOneAllAppsEntryAndOneAppSpecificEntryWhenFirstArgumentHasOneEntryAndSecondArgumentHasOneEntry()
|
||||
{
|
||||
// arrange
|
||||
var firstList = new List<KeysDataModel>();
|
||||
var firstListEntry = new KeysDataModel();
|
||||
firstListEntry.OriginalKeys = "17;65";
|
||||
firstListEntry.NewRemapKeys = "17;86";
|
||||
firstList.Add(firstListEntry);
|
||||
var secondList = new List<AppSpecificKeysDataModel>();
|
||||
var secondListEntry = new AppSpecificKeysDataModel();
|
||||
secondListEntry.OriginalKeys = "17;66";
|
||||
secondListEntry.NewRemapKeys = "17;87";
|
||||
secondListEntry.TargetApp = "msedge";
|
||||
secondList.Add(secondListEntry);
|
||||
|
||||
// act
|
||||
var result = KeyboardManagerViewModel.CombineShortcutLists(firstList, secondList);
|
||||
|
||||
// Assert
|
||||
var expectedResult = new List<AppSpecificKeysDataModel>();
|
||||
var expectedFirstEntry = new AppSpecificKeysDataModel();
|
||||
expectedFirstEntry.OriginalKeys = firstListEntry.OriginalKeys;
|
||||
expectedFirstEntry.NewRemapKeys = firstListEntry.NewRemapKeys;
|
||||
expectedFirstEntry.TargetApp = "All Apps";
|
||||
expectedResult.Add(expectedFirstEntry);
|
||||
var expectedSecondEntry = new AppSpecificKeysDataModel();
|
||||
expectedSecondEntry.OriginalKeys = secondListEntry.OriginalKeys;
|
||||
expectedSecondEntry.NewRemapKeys = secondListEntry.NewRemapKeys;
|
||||
expectedSecondEntry.TargetApp = secondListEntry.TargetApp;
|
||||
expectedResult.Add(expectedSecondEntry);
|
||||
|
||||
Assert.AreEqual(expectedResult.Count, result.Count);
|
||||
Assert.IsTrue(expectedResult[0].Compare(result[0]));
|
||||
Assert.IsTrue(expectedResult[1].Compare(result[1]));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
// 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 Microsoft.PowerToys.Settings.UI.Library;
|
||||
using Microsoft.PowerToys.Settings.UI.Library.ViewModels;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.BackwardsCompatibility;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace ViewModelTests
|
||||
{
|
||||
[TestClass]
|
||||
public class PowerLauncherViewModelTest
|
||||
{
|
||||
private class SendCallbackMock
|
||||
{
|
||||
public int TimesSent { get; set; }
|
||||
|
||||
// PowerLauncherSettings is unused, but required according to SendCallback's signature.
|
||||
// Naming parameter with discard symbol to suppress FxCop warnings.
|
||||
[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")]
|
||||
public void OnSend(PowerLauncherSettings _)
|
||||
{
|
||||
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));
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
var settingPathMock = new Mock<ISettingsPath>();
|
||||
|
||||
var mockIOProvider = BackCompatTestProperties.GetModuleIOProvider(version, PowerLauncherSettings.ModuleName, fileName);
|
||||
var mockSettingsUtils = new SettingsUtils(mockIOProvider.Object, settingPathMock.Object);
|
||||
PowerLauncherSettings originalSettings = mockSettingsUtils.GetSettings<PowerLauncherSettings>(PowerLauncherSettings.ModuleName);
|
||||
|
||||
var mockGeneralIOProvider = BackCompatTestProperties.GetGeneralSettingsIOProvider(version);
|
||||
var mockGeneralSettingsUtils = new SettingsUtils(mockGeneralIOProvider.Object, settingPathMock.Object);
|
||||
GeneralSettings originalGeneralSettings = mockGeneralSettingsUtils.GetSettings<GeneralSettings>();
|
||||
|
||||
var generalSettingsRepository = new BackCompatTestProperties.MockSettingsRepository<GeneralSettings>(mockGeneralSettingsUtils);
|
||||
|
||||
// Initialise View Model with test Config files
|
||||
Func<string, int> sendMockIPCConfigMSG = msg => { return 0; };
|
||||
PowerLauncherViewModel viewModel = new PowerLauncherViewModel(mockSettingsUtils, generalSettingsRepository, sendMockIPCConfigMSG, 32);
|
||||
|
||||
// Verify that the old settings persisted
|
||||
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.DisableDriveDetectionWarning, viewModel.DisableDriveDetectionWarning);
|
||||
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);
|
||||
|
||||
// Verify that the stub file was used
|
||||
var expectedCallCount = 2; // once via the view model, and once by the test (GetSettings<T>)
|
||||
BackCompatTestProperties.VerifyModuleIOProviderWasRead(mockIOProvider, PowerLauncherSettings.ModuleName, expectedCallCount);
|
||||
BackCompatTestProperties.VerifyGeneralSettingsIOProviderWasRead(mockGeneralIOProvider, expectedCallCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SearchPreferenceShouldUpdatePreferences()
|
||||
{
|
||||
viewModel.SearchResultPreference = "SearchOptionsAreNotValidated";
|
||||
viewModel.SearchTypePreference = "SearchOptionsAreNotValidated";
|
||||
|
||||
Assert.AreEqual(sendCallbackMock.TimesSent, 2);
|
||||
Assert.IsTrue(mockSettings.Properties.SearchResultPreference == "SearchOptionsAreNotValidated");
|
||||
Assert.IsTrue(mockSettings.Properties.SearchTypePreference == "SearchOptionsAreNotValidated");
|
||||
}
|
||||
|
||||
public static void AssertHotkeySettings(HotkeySettings setting, bool win, bool ctrl, bool alt, bool shift, int code)
|
||||
{
|
||||
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);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail("setting parameter is null");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void HotkeysShouldUpdateHotkeys()
|
||||
{
|
||||
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 OverrideShouldUpdateOverrides()
|
||||
{
|
||||
viewModel.OverrideWinRKey = true;
|
||||
viewModel.OverrideWinSKey = false;
|
||||
|
||||
Assert.AreEqual(1, sendCallbackMock.TimesSent);
|
||||
|
||||
Assert.IsTrue(mockSettings.Properties.OverrideWinkeyR);
|
||||
Assert.IsFalse(mockSettings.Properties.OverrideWinkeyS);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DriveDetectionViewModelWhenSetMustUpdateOverrides()
|
||||
{
|
||||
// Act
|
||||
viewModel.DisableDriveDetectionWarning = true;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(1, sendCallbackMock.TimesSent);
|
||||
Assert.IsTrue(mockSettings.Properties.DisableDriveDetectionWarning);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
// 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.Text.Json;
|
||||
using Microsoft.PowerToys.Settings.UI.Library;
|
||||
using Microsoft.PowerToys.Settings.UI.Library.ViewModels;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.BackwardsCompatibility;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace ViewModelTests
|
||||
{
|
||||
[TestClass]
|
||||
public class PowerPreview
|
||||
{
|
||||
private Mock<ISettingsUtils> mockPowerPreviewSettingsUtils;
|
||||
|
||||
private Mock<ISettingsUtils> mockGeneralSettingsUtils;
|
||||
|
||||
[TestInitialize]
|
||||
public void SetUpStubSettingUtils()
|
||||
{
|
||||
mockPowerPreviewSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<PowerPreviewSettings>();
|
||||
mockGeneralSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<GeneralSettings>();
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
var settingPathMock = new Mock<ISettingsPath>();
|
||||
var fileMock = BackCompatTestProperties.GetModuleIOProvider(version, PowerPreviewSettings.ModuleName, fileName);
|
||||
|
||||
var mockSettingsUtils = new SettingsUtils(fileMock.Object, settingPathMock.Object);
|
||||
PowerPreviewSettings originalSettings = mockSettingsUtils.GetSettings<PowerPreviewSettings>(PowerPreviewSettings.ModuleName);
|
||||
var repository = new BackCompatTestProperties.MockSettingsRepository<PowerPreviewSettings>(mockSettingsUtils);
|
||||
|
||||
var mockGeneralIOProvider = BackCompatTestProperties.GetGeneralSettingsIOProvider(version);
|
||||
var mockGeneralSettingsUtils = new SettingsUtils(mockGeneralIOProvider.Object, settingPathMock.Object);
|
||||
GeneralSettings originalGeneralSettings = mockGeneralSettingsUtils.GetSettings<GeneralSettings>();
|
||||
var generalSettingsRepository = new BackCompatTestProperties.MockSettingsRepository<GeneralSettings>(mockGeneralSettingsUtils);
|
||||
|
||||
// Initialise View Model with test Config files
|
||||
Func<string, int> sendMockIPCConfigMSG = msg => { return 0; };
|
||||
PowerPreviewViewModel viewModel = new PowerPreviewViewModel(repository, generalSettingsRepository, sendMockIPCConfigMSG);
|
||||
|
||||
// Verify that the old settings persisted
|
||||
Assert.AreEqual(originalGeneralSettings.IsElevated, viewModel.IsElevated);
|
||||
Assert.AreEqual(originalSettings.Properties.EnableMdPreview, viewModel.MDRenderIsEnabled);
|
||||
Assert.AreEqual(originalSettings.Properties.EnableSvgPreview, viewModel.SVGRenderIsEnabled);
|
||||
Assert.AreEqual(originalSettings.Properties.EnableSvgThumbnail, viewModel.SVGThumbnailIsEnabled);
|
||||
|
||||
// Verify that the stub file was used
|
||||
var expectedCallCount = 2; // once via the view model, and once by the test (GetSettings<T>)
|
||||
BackCompatTestProperties.VerifyModuleIOProviderWasRead(fileMock, PowerPreviewSettings.ModuleName, expectedCallCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SVGRenderIsEnabledShouldPrevHandlerWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
SndModuleSettings<SndPowerPreviewSettings> snd = JsonSerializer.Deserialize<SndModuleSettings<SndPowerPreviewSettings>>(msg);
|
||||
Assert.IsTrue(snd.PowertoysSetting.FileExplorerPreviewSettings.Properties.EnableSvgPreview);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
PowerPreviewViewModel viewModel = new PowerPreviewViewModel(SettingsRepository<PowerPreviewSettings>.GetInstance(mockPowerPreviewSettingsUtils.Object), SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG, PowerPreviewSettings.ModuleName);
|
||||
|
||||
// act
|
||||
viewModel.SVGRenderIsEnabled = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SVGThumbnailIsEnabledShouldPrevHandlerWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
SndModuleSettings<SndPowerPreviewSettings> snd = JsonSerializer.Deserialize<SndModuleSettings<SndPowerPreviewSettings>>(msg);
|
||||
Assert.IsTrue(snd.PowertoysSetting.FileExplorerPreviewSettings.Properties.EnableSvgThumbnail);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
PowerPreviewViewModel viewModel = new PowerPreviewViewModel(SettingsRepository<PowerPreviewSettings>.GetInstance(mockPowerPreviewSettingsUtils.Object), SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG, PowerPreviewSettings.ModuleName);
|
||||
|
||||
// act
|
||||
viewModel.SVGThumbnailIsEnabled = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MDRenderIsEnabledShouldPrevHandlerWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
SndModuleSettings<SndPowerPreviewSettings> snd = JsonSerializer.Deserialize<SndModuleSettings<SndPowerPreviewSettings>>(msg);
|
||||
Assert.IsTrue(snd.PowertoysSetting.FileExplorerPreviewSettings.Properties.EnableMdPreview);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// arrange
|
||||
PowerPreviewViewModel viewModel = new PowerPreviewViewModel(SettingsRepository<PowerPreviewSettings>.GetInstance(mockPowerPreviewSettingsUtils.Object), SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG, PowerPreviewSettings.ModuleName);
|
||||
|
||||
// act
|
||||
viewModel.MDRenderIsEnabled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
// 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.Text.Json;
|
||||
using Microsoft.PowerToys.Settings.UI.Library;
|
||||
using Microsoft.PowerToys.Settings.UI.Library.ViewModels;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.BackwardsCompatibility;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace ViewModelTests
|
||||
{
|
||||
[TestClass]
|
||||
public class PowerRename
|
||||
{
|
||||
public const string GeneralSettingsFileName = "Test\\PowerRename";
|
||||
|
||||
private Mock<ISettingsUtils> mockGeneralSettingsUtils;
|
||||
|
||||
private Mock<ISettingsUtils> mockPowerRenamePropertiesUtils;
|
||||
|
||||
[TestInitialize]
|
||||
public void SetUpStubSettingUtils()
|
||||
{
|
||||
mockGeneralSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<GeneralSettings>();
|
||||
mockPowerRenamePropertiesUtils = ISettingsUtilsMocks.GetStubSettingsUtils<PowerRenameLocalProperties>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test if the original settings files were modified.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
[DataRow("v0.18.2", "power-rename-settings.json")]
|
||||
[DataRow("v0.19.2", "power-rename-settings.json")]
|
||||
[DataRow("v0.20.1", "power-rename-settings.json")]
|
||||
[DataRow("v0.22.0", "power-rename-settings.json")]
|
||||
public void OriginalFilesModificationTest(string version, string fileName)
|
||||
{
|
||||
var settingPathMock = new Mock<ISettingsPath>();
|
||||
var mockIOProvider = BackCompatTestProperties.GetModuleIOProvider(version, PowerRenameSettings.ModuleName, fileName);
|
||||
|
||||
var mockSettingsUtils = new SettingsUtils(mockIOProvider.Object, settingPathMock.Object);
|
||||
PowerRenameLocalProperties originalSettings = mockSettingsUtils.GetSettings<PowerRenameLocalProperties>(PowerRenameSettings.ModuleName);
|
||||
|
||||
var mockGeneralIOProvider = BackCompatTestProperties.GetGeneralSettingsIOProvider(version);
|
||||
|
||||
var mockGeneralSettingsUtils = new SettingsUtils(mockGeneralIOProvider.Object, settingPathMock.Object);
|
||||
GeneralSettings originalGeneralSettings = mockGeneralSettingsUtils.GetSettings<GeneralSettings>();
|
||||
var generalSettingsRepository = new BackCompatTestProperties.MockSettingsRepository<GeneralSettings>(mockGeneralSettingsUtils);
|
||||
|
||||
// Initialise View Model with test Config files
|
||||
Func<string, int> sendMockIPCConfigMSG = msg => { return 0; };
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(mockSettingsUtils, generalSettingsRepository, sendMockIPCConfigMSG);
|
||||
|
||||
// Verify that the old settings persisted
|
||||
Assert.AreEqual(originalGeneralSettings.Enabled.PowerRename, viewModel.IsEnabled);
|
||||
Assert.AreEqual(originalSettings.ExtendedContextMenuOnly, viewModel.EnabledOnContextExtendedMenu);
|
||||
Assert.AreEqual(originalSettings.MRUEnabled, viewModel.MRUEnabled);
|
||||
Assert.AreEqual(originalSettings.ShowIcon, viewModel.EnabledOnContextMenu);
|
||||
|
||||
// Verify that the stub file was used
|
||||
var expectedCallCount = 2; // once via the view model, and once by the test (GetSettings<T>)
|
||||
BackCompatTestProperties.VerifyModuleIOProviderWasRead(mockIOProvider, PowerRenameSettings.ModuleName, expectedCallCount);
|
||||
BackCompatTestProperties.VerifyGeneralSettingsIOProviderWasRead(mockGeneralIOProvider, expectedCallCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsEnabledShouldEnableModuleWhenSuccessful()
|
||||
{
|
||||
// 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(mockPowerRenamePropertiesUtils.Object, SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG, GeneralSettingsFileName);
|
||||
|
||||
// act
|
||||
viewModel.IsEnabled = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MRUEnabledShouldSetValue2TrueWhenSuccessful()
|
||||
{
|
||||
// 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(mockPowerRenamePropertiesUtils.Object, SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG, GeneralSettingsFileName);
|
||||
|
||||
// act
|
||||
viewModel.MRUEnabled = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void WhenIsEnabledIsOffAndMRUEnabledIsOffGlobalAndMruShouldBeOff()
|
||||
{
|
||||
Func<string, int> sendMockIPCConfigMSG = msg => { return 0; };
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(mockPowerRenamePropertiesUtils.Object, SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG, GeneralSettingsFileName);
|
||||
|
||||
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(mockPowerRenamePropertiesUtils.Object, SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG, GeneralSettingsFileName);
|
||||
|
||||
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(mockPowerRenamePropertiesUtils.Object, SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG, GeneralSettingsFileName);
|
||||
|
||||
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(mockPowerRenamePropertiesUtils.Object, SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG, GeneralSettingsFileName);
|
||||
|
||||
viewModel.IsEnabled = true;
|
||||
viewModel.MRUEnabled = true;
|
||||
|
||||
Assert.IsTrue(viewModel.GlobalAndMruEnabled);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnabledOnContextMenuShouldSetValue2TrueWhenSuccessful()
|
||||
{
|
||||
// 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(mockPowerRenamePropertiesUtils.Object, SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG, GeneralSettingsFileName);
|
||||
|
||||
// act
|
||||
viewModel.EnabledOnContextMenu = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void EnabledOnContextExtendedMenuShouldSetValue2TrueWhenSuccessful()
|
||||
{
|
||||
// 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(mockPowerRenamePropertiesUtils.Object, SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG, GeneralSettingsFileName);
|
||||
|
||||
// act
|
||||
viewModel.EnabledOnContextMenu = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void RestoreFlagsOnLaunchShouldSetValue2TrueWhenSuccessful()
|
||||
{
|
||||
// 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(mockPowerRenamePropertiesUtils.Object, SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG, GeneralSettingsFileName);
|
||||
|
||||
// act
|
||||
viewModel.RestoreFlagsOnLaunch = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MaxDispListNumShouldSetMaxSuggListTo20WhenSuccessful()
|
||||
{
|
||||
// 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(mockPowerRenamePropertiesUtils.Object, SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG, GeneralSettingsFileName);
|
||||
|
||||
// act
|
||||
viewModel.MaxDispListNum = 20;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
// 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.Text.Json;
|
||||
using Microsoft.PowerToys.Settings.UI.Library;
|
||||
using Microsoft.PowerToys.Settings.UI.Library.ViewModels;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.BackwardsCompatibility;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace ViewModelTests
|
||||
{
|
||||
[TestClass]
|
||||
public class ShortcutGuide
|
||||
{
|
||||
public const string ShortCutGuideTestFolderName = "Test\\ShortCutGuide";
|
||||
|
||||
/// <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)
|
||||
{
|
||||
var settingPathMock = new Mock<ISettingsPath>();
|
||||
var mockIOProvider = BackCompatTestProperties.GetModuleIOProvider(version, ShortcutGuideSettings.ModuleName, fileName);
|
||||
var mockSettingsUtils = new SettingsUtils(mockIOProvider.Object, settingPathMock.Object);
|
||||
ShortcutGuideSettings originalSettings = mockSettingsUtils.GetSettings<ShortcutGuideSettings>(ShortcutGuideSettings.ModuleName);
|
||||
|
||||
var mockGeneralIOProvider = BackCompatTestProperties.GetGeneralSettingsIOProvider(version);
|
||||
var mockGeneralSettingsUtils = new SettingsUtils(mockGeneralIOProvider.Object, settingPathMock.Object);
|
||||
GeneralSettings originalGeneralSettings = mockGeneralSettingsUtils.GetSettings<GeneralSettings>();
|
||||
var generalSettingsRepository = new BackCompatTestProperties.MockSettingsRepository<GeneralSettings>(mockGeneralSettingsUtils);
|
||||
var shortcutSettingsRepository = new BackCompatTestProperties.MockSettingsRepository<ShortcutGuideSettings>(mockSettingsUtils);
|
||||
|
||||
// Initialise View Model with test Config files
|
||||
Func<string, int> sendMockIPCConfigMSG = msg => { return 0; };
|
||||
ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(generalSettingsRepository, shortcutSettingsRepository, sendMockIPCConfigMSG);
|
||||
|
||||
// Verify that the old settings persisted
|
||||
Assert.AreEqual(originalGeneralSettings.Enabled.ShortcutGuide, viewModel.IsEnabled);
|
||||
Assert.AreEqual(originalSettings.Properties.OverlayOpacity.Value, viewModel.OverlayOpacity);
|
||||
Assert.AreEqual(originalSettings.Properties.PressTime.Value, viewModel.PressTime);
|
||||
|
||||
// Verify that the stub file was used
|
||||
var expectedCallCount = 2; // once via the view model, and once by the test (GetSettings<T>)
|
||||
BackCompatTestProperties.VerifyModuleIOProviderWasRead(mockIOProvider, ShortcutGuideSettings.ModuleName, expectedCallCount);
|
||||
BackCompatTestProperties.VerifyGeneralSettingsIOProviderWasRead(mockGeneralIOProvider, expectedCallCount);
|
||||
}
|
||||
|
||||
private Mock<ISettingsUtils> mockGeneralSettingsUtils;
|
||||
|
||||
private Mock<ISettingsUtils> mockShortcutGuideSettingsUtils;
|
||||
|
||||
[TestInitialize]
|
||||
public void SetUpStubSettingUtils()
|
||||
{
|
||||
mockGeneralSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<GeneralSettings>();
|
||||
mockShortcutGuideSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<ShortcutGuideSettings>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsEnabledShouldEnableModuleWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
// Initialize mock function of sending IPC message.
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
|
||||
Assert.IsTrue(snd.GeneralSettings.Enabled.ShortcutGuide);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// Arrange
|
||||
ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<ShortcutGuideSettings>.GetInstance(mockShortcutGuideSettingsUtils.Object), sendMockIPCConfigMSG, ShortCutGuideTestFolderName);
|
||||
|
||||
// Act
|
||||
viewModel.IsEnabled = true;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ThemeIndexShouldSetThemeToDarkWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
// Initialize mock function of sending IPC message.
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
ShortcutGuideSettingsIPCMessage snd = JsonSerializer.Deserialize<ShortcutGuideSettingsIPCMessage>(msg);
|
||||
Assert.AreEqual("dark", snd.Powertoys.ShortcutGuide.Properties.Theme.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// Arrange
|
||||
ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<ShortcutGuideSettings>.GetInstance(mockShortcutGuideSettingsUtils.Object), sendMockIPCConfigMSG, ShortCutGuideTestFolderName);
|
||||
|
||||
// Initialize shortcut guide settings theme to 'system' to be in sync with shortcut_guide.h.
|
||||
Assert.AreEqual(2, viewModel.ThemeIndex);
|
||||
|
||||
// Act
|
||||
viewModel.ThemeIndex = 0;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PressTimeShouldSetPressTimeToOneHundredWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
// Initialize mock function of sending IPC message.
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
ShortcutGuideSettingsIPCMessage snd = JsonSerializer.Deserialize<ShortcutGuideSettingsIPCMessage>(msg);
|
||||
Assert.AreEqual(100, snd.Powertoys.ShortcutGuide.Properties.PressTime.Value);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// Arrange
|
||||
ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<ShortcutGuideSettings>.GetInstance(mockShortcutGuideSettingsUtils.Object), sendMockIPCConfigMSG, ShortCutGuideTestFolderName);
|
||||
Assert.AreEqual(900, viewModel.PressTime);
|
||||
|
||||
// Act
|
||||
viewModel.PressTime = 100;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void OverlayOpacityShouldSeOverlayOpacityToOneHundredWhenSuccessful()
|
||||
{
|
||||
// Assert
|
||||
// Initialize mock function of sending IPC message.
|
||||
Func<string, int> sendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
ShortcutGuideSettingsIPCMessage snd = JsonSerializer.Deserialize<ShortcutGuideSettingsIPCMessage>(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(SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<ShortcutGuideSettings>.GetInstance(mockShortcutGuideSettingsUtils.Object), sendMockIPCConfigMSG, ShortCutGuideTestFolderName);
|
||||
Assert.AreEqual(90, viewModel.OverlayOpacity);
|
||||
|
||||
// Act
|
||||
viewModel.OverlayOpacity = 100;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user