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-08 16:34:19 -07:00
|
|
|
|
using System.Globalization;
|
2020-08-13 15:02:05 -07:00
|
|
|
|
using System.Text.Json;
|
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-10-06 15:00:25 -07:00
|
|
|
|
public const string generalSettingsFileName = "Test\\GenealSettings";
|
2020-08-13 15:02:05 -07:00
|
|
|
|
|
2020-09-23 13:20:32 -07:00
|
|
|
|
private Mock<ISettingsUtils> mockGeneralSettingsUtils;
|
|
|
|
|
|
|
2020-10-08 16:34:19 -07:00
|
|
|
|
|
|
|
|
|
|
|
2020-09-23 13:20:32 -07:00
|
|
|
|
[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-10-08 16:34:19 -07:00
|
|
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[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);
|
2020-10-08 16:34:19 -07:00
|
|
|
|
GeneralSettings originalGeneralSettings = mockGeneralSettingsUtils.GetSettings<GeneralSettings>();
|
2020-11-02 18:33:43 +01:00
|
|
|
|
|
2020-10-08 16:34:19 -07:00
|
|
|
|
var generalSettingsRepository = new BackCompatTestProperties.MockSettingsRepository<GeneralSettings>(mockGeneralSettingsUtils);
|
|
|
|
|
|
|
2020-11-02 18:33:43 +01:00
|
|
|
|
|
2020-10-08 16:34:19 -07:00
|
|
|
|
// Initialise View Model with test Config files
|
|
|
|
|
|
// Arrange
|
2020-11-02 18:33:43 +01: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,
|
|
|
|
|
|
ipcMSGCallBackFunc: SendMockIPCConfigMSG,
|
|
|
|
|
|
ipcMSGRestartAsAdminMSGCallBackFunc: SendRestartAdminIPCMessage,
|
|
|
|
|
|
ipcMSGCheckForUpdatesCallBackFunc: SendCheckForUpdatesIPCMessage,
|
|
|
|
|
|
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.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>)
|
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
|
|
|
|
|
|
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(
|
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,
|
|
|
|
|
|
SendMockIPCConfigMSG,
|
|
|
|
|
|
SendRestartAdminIPCMessage,
|
|
|
|
|
|
SendCheckForUpdatesIPCMessage,
|
2020-10-06 15:00:25 -07:00
|
|
|
|
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
|
|
|
|
|
|
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(
|
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,
|
|
|
|
|
|
SendMockIPCConfigMSG,
|
|
|
|
|
|
SendRestartAdminIPCMessage,
|
|
|
|
|
|
SendCheckForUpdatesIPCMessage,
|
2020-10-06 15:00:25 -07:00
|
|
|
|
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
|
|
|
|
|
|
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(
|
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,
|
|
|
|
|
|
SendMockIPCConfigMSG,
|
|
|
|
|
|
SendRestartAdminIPCMessage,
|
|
|
|
|
|
SendCheckForUpdatesIPCMessage,
|
2020-10-06 15:00:25 -07:00
|
|
|
|
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;
|
|
|
|
|
|
// 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(
|
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,
|
|
|
|
|
|
SendMockIPCConfigMSG,
|
|
|
|
|
|
SendRestartAdminIPCMessage,
|
|
|
|
|
|
SendCheckForUpdatesIPCMessage,
|
2020-10-06 15:00:25 -07:00
|
|
|
|
generalSettingsFileName);
|
2020-08-13 15:02:05 -07:00
|
|
|
|
Assert.IsFalse(viewModel.IsLightThemeRadioButtonChecked);
|
|
|
|
|
|
|
|
|
|
|
|
// act
|
|
|
|
|
|
viewModel.IsLightThemeRadioButtonChecked = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2020-10-06 15:00:25 -07:00
|
|
|
|
public void IsDarkThemeRadioButtonCheckedShouldThemeToDarkWhenSuccessful()
|
2020-08-13 15:02:05 -07:00
|
|
|
|
{
|
|
|
|
|
|
// 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(
|
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,
|
|
|
|
|
|
SendMockIPCConfigMSG,
|
|
|
|
|
|
SendRestartAdminIPCMessage,
|
|
|
|
|
|
SendCheckForUpdatesIPCMessage,
|
2020-10-06 15:00:25 -07:00
|
|
|
|
generalSettingsFileName);
|
2020-08-13 15:02:05 -07:00
|
|
|
|
Assert.IsFalse(viewModel.IsDarkThemeRadioButtonChecked);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// act
|
|
|
|
|
|
viewModel.IsDarkThemeRadioButtonChecked = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-21 10:14:44 -07:00
|
|
|
|
[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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-06 15:00:25 -07:00
|
|
|
|
public static int UpdateUIThemeMethod(string themeName)
|
2020-08-13 15:02:05 -07:00
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|