2020-08-14 13:58:48 -07:00
|
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Text.Json;
|
2024-10-17 05:14:57 -04:00
|
|
|
|
|
2020-10-22 09:45:48 -07:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library;
|
2020-10-08 16:34:19 -07:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.UnitTests.BackwardsCompatibility;
|
2020-09-21 10:14:44 -07:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
|
2022-10-26 14:02:31 +01:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.ViewModels;
|
2020-08-14 13:58:48 -07:00
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
2020-09-21 10:14:44 -07:00
|
|
|
|
using Moq;
|
2020-08-14 13:58:48 -07:00
|
|
|
|
|
|
|
|
|
|
namespace ViewModelTests
|
|
|
|
|
|
{
|
|
|
|
|
|
[TestClass]
|
|
|
|
|
|
public class ShortcutGuide
|
|
|
|
|
|
{
|
|
|
|
|
|
public const string ShortCutGuideTestFolderName = "Test\\ShortCutGuide";
|
|
|
|
|
|
|
2020-10-08 16:34:19 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Test if the original settings files were modified.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
|
[DataRow("v0.18.2", "settings.json")]
|
|
|
|
|
|
[DataRow("v0.19.2", "settings.json")]
|
|
|
|
|
|
[DataRow("v0.20.1", "settings.json")]
|
|
|
|
|
|
[DataRow("v0.21.1", "settings.json")]
|
|
|
|
|
|
[DataRow("v0.22.0", "settings.json")]
|
|
|
|
|
|
public void OriginalFilesModificationTest(string version, string fileName)
|
|
|
|
|
|
{
|
2020-11-02 18:33:43 +01:00
|
|
|
|
var settingPathMock = new Mock<ISettingsPath>();
|
2020-10-08 16:34:19 -07:00
|
|
|
|
var mockIOProvider = BackCompatTestProperties.GetModuleIOProvider(version, ShortcutGuideSettings.ModuleName, fileName);
|
2020-11-02 18:33:43 +01:00
|
|
|
|
var mockSettingsUtils = new SettingsUtils(mockIOProvider.Object, settingPathMock.Object);
|
2021-01-14 14:14:29 +02:00
|
|
|
|
ShortcutGuideSettings originalSettings = mockSettingsUtils.GetSettingsOrDefault<ShortcutGuideSettings>(ShortcutGuideSettings.ModuleName);
|
2020-10-08 16:34:19 -07:00
|
|
|
|
|
|
|
|
|
|
var mockGeneralIOProvider = BackCompatTestProperties.GetGeneralSettingsIOProvider(version);
|
2020-11-02 18:33:43 +01:00
|
|
|
|
var mockGeneralSettingsUtils = new SettingsUtils(mockGeneralIOProvider.Object, settingPathMock.Object);
|
2021-01-14 14:14:29 +02:00
|
|
|
|
GeneralSettings originalGeneralSettings = mockGeneralSettingsUtils.GetSettingsOrDefault<GeneralSettings>();
|
2020-10-08 16:34:19 -07:00
|
|
|
|
var generalSettingsRepository = new BackCompatTestProperties.MockSettingsRepository<GeneralSettings>(mockGeneralSettingsUtils);
|
|
|
|
|
|
var shortcutSettingsRepository = new BackCompatTestProperties.MockSettingsRepository<ShortcutGuideSettings>(mockSettingsUtils);
|
|
|
|
|
|
|
|
|
|
|
|
// Initialise View Model with test Config files
|
2020-11-17 15:29:21 -08:00
|
|
|
|
Func<string, int> sendMockIPCConfigMSG = msg => { return 0; };
|
2021-05-20 15:07:34 +03:00
|
|
|
|
ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(mockSettingsUtils, generalSettingsRepository, shortcutSettingsRepository, sendMockIPCConfigMSG);
|
2020-10-08 16:34:19 -07:00
|
|
|
|
|
2020-10-30 14:42:49 -04:00
|
|
|
|
// Verify that the old settings persisted
|
2020-10-08 16:34:19 -07:00
|
|
|
|
Assert.AreEqual(originalGeneralSettings.Enabled.ShortcutGuide, viewModel.IsEnabled);
|
|
|
|
|
|
Assert.AreEqual(originalSettings.Properties.OverlayOpacity.Value, viewModel.OverlayOpacity);
|
|
|
|
|
|
|
2020-11-17 15:29:21 -08:00
|
|
|
|
// Verify that the stub file was used
|
|
|
|
|
|
var expectedCallCount = 2; // once via the view model, and once by the test (GetSettings<T>)
|
2020-10-08 16:34:19 -07:00
|
|
|
|
BackCompatTestProperties.VerifyModuleIOProviderWasRead(mockIOProvider, ShortcutGuideSettings.ModuleName, expectedCallCount);
|
|
|
|
|
|
BackCompatTestProperties.VerifyGeneralSettingsIOProviderWasRead(mockGeneralIOProvider, expectedCallCount);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-23 13:20:32 -07:00
|
|
|
|
private Mock<ISettingsUtils> mockGeneralSettingsUtils;
|
|
|
|
|
|
|
|
|
|
|
|
private Mock<ISettingsUtils> mockShortcutGuideSettingsUtils;
|
|
|
|
|
|
|
|
|
|
|
|
[TestInitialize]
|
2020-10-06 15:00:25 -07:00
|
|
|
|
public void SetUpStubSettingUtils()
|
2020-09-23 13:20:32 -07:00
|
|
|
|
{
|
|
|
|
|
|
mockGeneralSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<GeneralSettings>();
|
|
|
|
|
|
mockShortcutGuideSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<ShortcutGuideSettings>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-14 13:58:48 -07:00
|
|
|
|
[TestMethod]
|
2020-10-06 15:00:25 -07:00
|
|
|
|
public void IsEnabledShouldEnableModuleWhenSuccessful()
|
2020-08-14 13:58:48 -07:00
|
|
|
|
{
|
2021-05-20 15:07:34 +03:00
|
|
|
|
var settingsUtilsMock = new Mock<SettingsUtils>();
|
|
|
|
|
|
|
2020-08-14 13:58:48 -07:00
|
|
|
|
// Assert
|
|
|
|
|
|
// Initialize mock function of sending IPC message.
|
2020-11-17 15:29:21 -08:00
|
|
|
|
Func<string, int> sendMockIPCConfigMSG = msg =>
|
2020-08-14 13:58:48 -07:00
|
|
|
|
{
|
|
|
|
|
|
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
|
|
|
|
|
|
Assert.IsTrue(snd.GeneralSettings.Enabled.ShortcutGuide);
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Arrange
|
2021-05-20 15:07:34 +03:00
|
|
|
|
ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(settingsUtilsMock.Object, SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<ShortcutGuideSettings>.GetInstance(mockShortcutGuideSettingsUtils.Object), sendMockIPCConfigMSG, ShortCutGuideTestFolderName);
|
2020-08-14 13:58:48 -07:00
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
|
viewModel.IsEnabled = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2020-10-06 15:00:25 -07:00
|
|
|
|
public void ThemeIndexShouldSetThemeToDarkWhenSuccessful()
|
2020-08-14 13:58:48 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
2021-05-20 15:07:34 +03:00
|
|
|
|
var settingsUtilsMock = new Mock<ISettingsUtils>();
|
|
|
|
|
|
ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(settingsUtilsMock.Object, SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<ShortcutGuideSettings>.GetInstance(mockShortcutGuideSettingsUtils.Object), msg => { return 0; }, ShortCutGuideTestFolderName);
|
2020-11-17 15:29:21 -08:00
|
|
|
|
|
2020-09-28 12:44:18 -07:00
|
|
|
|
// Initialize shortcut guide settings theme to 'system' to be in sync with shortcut_guide.h.
|
|
|
|
|
|
Assert.AreEqual(2, viewModel.ThemeIndex);
|
2020-08-14 13:58:48 -07:00
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
|
viewModel.ThemeIndex = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// Assert
|
2021-05-20 15:07:34 +03:00
|
|
|
|
Func<string, bool> isDark = s => JsonSerializer.Deserialize<ShortcutGuideSettings>(s).Properties.Theme.Value == "dark";
|
|
|
|
|
|
settingsUtilsMock.Verify(x => x.SaveSettings(It.Is<string>(y => isDark(y)), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
2020-08-14 13:58:48 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2020-10-06 15:00:25 -07:00
|
|
|
|
public void OverlayOpacityShouldSeOverlayOpacityToOneHundredWhenSuccessful()
|
2020-08-14 13:58:48 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
2021-05-20 15:07:34 +03:00
|
|
|
|
var settingsUtilsMock = new Mock<ISettingsUtils>();
|
|
|
|
|
|
ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(settingsUtilsMock.Object, SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<ShortcutGuideSettings>.GetInstance(mockShortcutGuideSettingsUtils.Object), msg => { return 0; }, ShortCutGuideTestFolderName);
|
2020-08-14 13:58:48 -07:00
|
|
|
|
Assert.AreEqual(90, viewModel.OverlayOpacity);
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
|
viewModel.OverlayOpacity = 100;
|
2021-05-20 15:07:34 +03:00
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
|
Func<string, bool> equal100 = s => JsonSerializer.Deserialize<ShortcutGuideSettings>(s).Properties.OverlayOpacity.Value == 100;
|
|
|
|
|
|
settingsUtilsMock.Verify(x => x.SaveSettings(It.Is<string>(y => equal100(y)), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
2020-08-14 13:58:48 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|