2020-08-13 15:02:05 -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;
|
2020-10-22 09:45:48 -07:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library;
|
|
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library.ViewModels;
|
2020-10-08 16:34:19 -07:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.UnitTests.BackwardsCompatibility;
|
2020-09-23 13:20:32 -07:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
|
2020-10-08 16:34:19 -07:00
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
2020-09-21 10:14:44 -07:00
|
|
|
|
using Moq;
|
2020-11-02 18:33:43 +01:00
|
|
|
|
using JsonSerializer = System.Text.Json.JsonSerializer;
|
2020-08-13 15:02:05 -07:00
|
|
|
|
|
|
|
|
|
|
namespace ViewModelTests
|
|
|
|
|
|
{
|
|
|
|
|
|
[TestClass]
|
|
|
|
|
|
public class General
|
|
|
|
|
|
{
|
2020-11-18 09:13:03 -08:00
|
|
|
|
public const string GeneralSettingsFileName = "Test\\GeneralSettings";
|
2020-08-13 15:02:05 -07:00
|
|
|
|
|
2020-09-23 13:20:32 -07:00
|
|
|
|
private Mock<ISettingsUtils> mockGeneralSettingsUtils;
|
|
|
|
|
|
|
|
|
|
|
|
[TestInitialize]
|
2020-10-06 15:00:25 -07:00
|
|
|
|
public void SetUpStubSettingUtils()
|
2020-09-23 13:20:32 -07:00
|
|
|
|
{
|
|
|
|
|
|
mockGeneralSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<GeneralSettings>();
|
|
|
|
|
|
}
|
2020-11-17 15:29:21 -08:00
|
|
|
|
|
2020-10-08 16:34:19 -07:00
|
|
|
|
[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)
|
|
|
|
|
|
{
|
2020-11-02 18:33:43 +01:00
|
|
|
|
var settingPathMock = new Mock<ISettingsPath>();
|
|
|
|
|
|
var fileMock = BackCompatTestProperties.GetGeneralSettingsIOProvider(version);
|
|
|
|
|
|
|
|
|
|
|
|
var mockGeneralSettingsUtils = new SettingsUtils(fileMock.Object, settingPathMock.Object);
|
2021-01-14 14:14:29 +02:00
|
|
|
|
GeneralSettings originalGeneralSettings = mockGeneralSettingsUtils.GetSettingsOrDefault<GeneralSettings>();
|
2020-11-02 18:33:43 +01:00
|
|
|
|
|
2020-10-08 16:34:19 -07:00
|
|
|
|
var generalSettingsRepository = new BackCompatTestProperties.MockSettingsRepository<GeneralSettings>(mockGeneralSettingsUtils);
|
|
|
|
|
|
|
|
|
|
|
|
// Initialise View Model with test Config files
|
|
|
|
|
|
// Arrange
|
2020-11-17 15:29:21 -08:00
|
|
|
|
Func<string, int> sendMockIPCConfigMSG = msg => 0;
|
|
|
|
|
|
Func<string, int> sendRestartAdminIPCMessage = msg => 0;
|
|
|
|
|
|
Func<string, int> sendCheckForUpdatesIPCMessage = msg => 0;
|
2020-10-08 16:34:19 -07:00
|
|
|
|
var viewModel = new GeneralViewModel(
|
|
|
|
|
|
settingsRepository: generalSettingsRepository,
|
|
|
|
|
|
runAsAdminText: "GeneralSettings_RunningAsAdminText",
|
|
|
|
|
|
runAsUserText: "GeneralSettings_RunningAsUserText",
|
|
|
|
|
|
isElevated: false,
|
|
|
|
|
|
isAdmin: false,
|
|
|
|
|
|
updateTheme: UpdateUIThemeMethod,
|
2020-11-17 15:29:21 -08:00
|
|
|
|
ipcMSGCallBackFunc: sendMockIPCConfigMSG,
|
|
|
|
|
|
ipcMSGRestartAsAdminMSGCallBackFunc: sendRestartAdminIPCMessage,
|
|
|
|
|
|
ipcMSGCheckForUpdatesCallBackFunc: sendCheckForUpdatesIPCMessage,
|
2020-10-08 16:34:19 -07:00
|
|
|
|
configFileSubfolder: string.Empty);
|
|
|
|
|
|
|
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.AutoDownloadUpdates, viewModel.AutoDownloadUpdates);
|
|
|
|
|
|
Assert.AreEqual(originalGeneralSettings.PowertoysVersion, viewModel.PowerToysVersion);
|
|
|
|
|
|
Assert.AreEqual(originalGeneralSettings.RunElevated, viewModel.RunElevated);
|
|
|
|
|
|
Assert.AreEqual(originalGeneralSettings.Startup, viewModel.Startup);
|
|
|
|
|
|
|
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-11-02 18:33:43 +01:00
|
|
|
|
BackCompatTestProperties.VerifyGeneralSettingsIOProviderWasRead(fileMock, expectedCallCount);
|
2020-10-08 16:34:19 -07:00
|
|
|
|
}
|
2020-09-23 13:20:32 -07:00
|
|
|
|
|
2020-08-13 15:02:05 -07:00
|
|
|
|
[TestMethod]
|
2020-10-06 15:00:25 -07:00
|
|
|
|
public void IsElevatedShouldUpdateRunasAdminStatusAttrsWhenSuccessful()
|
2020-08-13 15:02:05 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
2020-11-17 15:29:21 -08:00
|
|
|
|
Func<string, int> sendMockIPCConfigMSG = msg => { return 0; };
|
|
|
|
|
|
Func<string, int> sendRestartAdminIPCMessage = msg => { return 0; };
|
|
|
|
|
|
Func<string, int> sendCheckForUpdatesIPCMessage = msg => { return 0; };
|
2020-08-13 15:02:05 -07:00
|
|
|
|
GeneralViewModel viewModel = new GeneralViewModel(
|
2020-11-02 18:33:43 +01:00
|
|
|
|
settingsRepository: SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object),
|
2020-08-13 15:02:05 -07:00
|
|
|
|
"GeneralSettings_RunningAsAdminText",
|
|
|
|
|
|
"GeneralSettings_RunningAsUserText",
|
|
|
|
|
|
false,
|
|
|
|
|
|
false,
|
|
|
|
|
|
UpdateUIThemeMethod,
|
2020-11-17 15:29:21 -08:00
|
|
|
|
sendMockIPCConfigMSG,
|
|
|
|
|
|
sendRestartAdminIPCMessage,
|
|
|
|
|
|
sendCheckForUpdatesIPCMessage,
|
|
|
|
|
|
GeneralSettingsFileName);
|
2020-08-13 15:02:05 -07:00
|
|
|
|
|
|
|
|
|
|
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]
|
2020-10-06 15:00:25 -07:00
|
|
|
|
public void StartupShouldEnableRunOnStartUpWhenSuccessful()
|
2020-08-13 15:02:05 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Assert
|
2020-11-17 15:29:21 -08:00
|
|
|
|
Func<string, int> sendMockIPCConfigMSG = msg =>
|
2020-08-13 15:02:05 -07:00
|
|
|
|
{
|
|
|
|
|
|
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
|
|
|
|
|
|
Assert.IsTrue(snd.GeneralSettings.Startup);
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Arrange
|
2020-11-17 15:29:21 -08:00
|
|
|
|
Func<string, int> sendRestartAdminIPCMessage = msg => { return 0; };
|
|
|
|
|
|
Func<string, int> sendCheckForUpdatesIPCMessage = msg => { return 0; };
|
2020-08-13 15:02:05 -07:00
|
|
|
|
GeneralViewModel viewModel = new GeneralViewModel(
|
2020-11-02 18:33:43 +01:00
|
|
|
|
settingsRepository: SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object),
|
2020-08-13 15:02:05 -07:00
|
|
|
|
"GeneralSettings_RunningAsAdminText",
|
|
|
|
|
|
"GeneralSettings_RunningAsUserText",
|
|
|
|
|
|
false,
|
|
|
|
|
|
false,
|
|
|
|
|
|
UpdateUIThemeMethod,
|
2020-11-17 15:29:21 -08:00
|
|
|
|
sendMockIPCConfigMSG,
|
|
|
|
|
|
sendRestartAdminIPCMessage,
|
|
|
|
|
|
sendCheckForUpdatesIPCMessage,
|
|
|
|
|
|
GeneralSettingsFileName);
|
2020-08-13 15:02:05 -07:00
|
|
|
|
Assert.IsFalse(viewModel.Startup);
|
|
|
|
|
|
|
|
|
|
|
|
// act
|
|
|
|
|
|
viewModel.Startup = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2020-10-06 15:00:25 -07:00
|
|
|
|
public void RunElevatedShouldEnableAlwaysRunElevatedWhenSuccessful()
|
2020-08-13 15:02:05 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Assert
|
2020-11-17 15:29:21 -08:00
|
|
|
|
Func<string, int> sendMockIPCConfigMSG = msg =>
|
2020-08-13 15:02:05 -07:00
|
|
|
|
{
|
|
|
|
|
|
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
|
|
|
|
|
|
Assert.IsTrue(snd.GeneralSettings.RunElevated);
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2020-11-17 15:29:21 -08:00
|
|
|
|
Func<string, int> sendRestartAdminIPCMessage = msg => { return 0; };
|
|
|
|
|
|
Func<string, int> sendCheckForUpdatesIPCMessage = msg => { return 0; };
|
2020-08-13 15:02:05 -07:00
|
|
|
|
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
GeneralViewModel viewModel = new GeneralViewModel(
|
2020-11-02 18:33:43 +01:00
|
|
|
|
settingsRepository: SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object),
|
2020-08-13 15:02:05 -07:00
|
|
|
|
"GeneralSettings_RunningAsAdminText",
|
|
|
|
|
|
"GeneralSettings_RunningAsUserText",
|
|
|
|
|
|
false,
|
|
|
|
|
|
false,
|
|
|
|
|
|
UpdateUIThemeMethod,
|
2020-11-17 15:29:21 -08:00
|
|
|
|
sendMockIPCConfigMSG,
|
|
|
|
|
|
sendRestartAdminIPCMessage,
|
|
|
|
|
|
sendCheckForUpdatesIPCMessage,
|
|
|
|
|
|
GeneralSettingsFileName);
|
2020-08-13 15:02:05 -07:00
|
|
|
|
|
|
|
|
|
|
Assert.IsFalse(viewModel.RunElevated);
|
|
|
|
|
|
|
|
|
|
|
|
// act
|
|
|
|
|
|
viewModel.RunElevated = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2020-10-06 15:00:25 -07:00
|
|
|
|
public void IsLightThemeRadioButtonCheckedShouldThemeToLightWhenSuccessful()
|
2020-08-13 15:02:05 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
GeneralViewModel viewModel = null;
|
2020-11-17 15:29:21 -08:00
|
|
|
|
|
2020-08-13 15:02:05 -07:00
|
|
|
|
// Assert
|
2020-11-17 15:29:21 -08:00
|
|
|
|
Func<string, int> sendMockIPCConfigMSG = msg =>
|
2020-08-13 15:02:05 -07:00
|
|
|
|
{
|
|
|
|
|
|
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
|
|
|
|
|
|
Assert.AreEqual("light", snd.GeneralSettings.Theme);
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2020-11-17 15:29:21 -08:00
|
|
|
|
Func<string, int> sendRestartAdminIPCMessage = msg => { return 0; };
|
|
|
|
|
|
Func<string, int> sendCheckForUpdatesIPCMessage = msg => { return 0; };
|
2020-08-13 15:02:05 -07:00
|
|
|
|
viewModel = new GeneralViewModel(
|
2020-11-02 18:33:43 +01:00
|
|
|
|
settingsRepository: SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object),
|
2020-08-13 15:02:05 -07:00
|
|
|
|
"GeneralSettings_RunningAsAdminText",
|
|
|
|
|
|
"GeneralSettings_RunningAsUserText",
|
|
|
|
|
|
false,
|
|
|
|
|
|
false,
|
|
|
|
|
|
UpdateUIThemeMethod,
|
2020-11-17 15:29:21 -08:00
|
|
|
|
sendMockIPCConfigMSG,
|
|
|
|
|
|
sendRestartAdminIPCMessage,
|
|
|
|
|
|
sendCheckForUpdatesIPCMessage,
|
|
|
|
|
|
GeneralSettingsFileName);
|
2021-08-23 19:48:52 +02:00
|
|
|
|
Assert.AreNotEqual(1, viewModel.ThemeIndex);
|
2020-08-13 15:02:05 -07:00
|
|
|
|
|
|
|
|
|
|
// act
|
2021-08-23 19:48:52 +02:00
|
|
|
|
viewModel.ThemeIndex = 1;
|
2020-08-13 15:02:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2020-10-06 15:00:25 -07:00
|
|
|
|
public void IsDarkThemeRadioButtonCheckedShouldThemeToDarkWhenSuccessful()
|
2020-08-13 15:02:05 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
// Assert
|
2020-11-17 15:29:21 -08:00
|
|
|
|
Func<string, int> sendMockIPCConfigMSG = msg =>
|
2020-08-13 15:02:05 -07:00
|
|
|
|
{
|
|
|
|
|
|
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
|
|
|
|
|
|
Assert.AreEqual("dark", snd.GeneralSettings.Theme);
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2020-11-17 15:29:21 -08:00
|
|
|
|
Func<string, int> sendRestartAdminIPCMessage = msg => { return 0; };
|
|
|
|
|
|
Func<string, int> sendCheckForUpdatesIPCMessage = msg => { return 0; };
|
2020-08-13 15:02:05 -07:00
|
|
|
|
GeneralViewModel viewModel = new GeneralViewModel(
|
2020-11-02 18:33:43 +01:00
|
|
|
|
settingsRepository: SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object),
|
2020-08-13 15:02:05 -07:00
|
|
|
|
"GeneralSettings_RunningAsAdminText",
|
|
|
|
|
|
"GeneralSettings_RunningAsUserText",
|
|
|
|
|
|
false,
|
|
|
|
|
|
false,
|
|
|
|
|
|
UpdateUIThemeMethod,
|
2020-11-17 15:29:21 -08:00
|
|
|
|
sendMockIPCConfigMSG,
|
|
|
|
|
|
sendRestartAdminIPCMessage,
|
|
|
|
|
|
sendCheckForUpdatesIPCMessage,
|
|
|
|
|
|
GeneralSettingsFileName);
|
2021-08-23 19:48:52 +02:00
|
|
|
|
Assert.AreNotEqual(0, viewModel.ThemeIndex);
|
2020-08-13 15:02:05 -07:00
|
|
|
|
|
|
|
|
|
|
// act
|
2021-08-23 19:48:52 +02:00
|
|
|
|
viewModel.ThemeIndex = 0;
|
2020-08-13 15:02:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-21 10:14:44 -07:00
|
|
|
|
[TestMethod]
|
|
|
|
|
|
public void AllModulesAreEnabledByDefault()
|
|
|
|
|
|
{
|
2020-11-17 15:29:21 -08:00
|
|
|
|
// arrange
|
2020-09-21 10:14:44 -07:00
|
|
|
|
EnabledModules modules = new EnabledModules();
|
|
|
|
|
|
|
2020-11-17 15:29:21 -08:00
|
|
|
|
// Assert
|
2020-09-21 10:14:44 -07:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-06 15:00:25 -07:00
|
|
|
|
public static int UpdateUIThemeMethod(string themeName)
|
2020-08-13 15:02:05 -07:00
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|