mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-15 11:17:53 +01:00
User/ryanbod/mock settings disk access (#6188)
* 1) Making Directory Methods private. 2) Removing the CreateDirectory / DeleteDirectory functionality from all Settings Unit Tests. * Abstracting disk access via IIOProvider to be able to provide mocks for unit tests instead of writing to disk. This also prevents developers who are running unit tests from interfering with the PowerToys settings on their local dev box. * Dependency Injecting stub SettingsUtils for all tests * Removing ISettingsUtils from constructors of objects that need to be deserialized (ColorPickerSettings/PowerLauncherSettings) as this breaks System.Text.Json * Removing unused namespace reference * Removing redifined mock * As per PR feedback. Stub Settings utils should work with any settings type if the intent is to compile / avoid null ref exceptions. Strangely when implementing this fix it became apparent that a stub settings isn't enough, and disk access needed to be mocked. I can't explain why the tests were passing previously. * Leveraging GetMockIOProviderForSaveLoadExists
This commit is contained in:
committed by
GitHub
parent
6e89ef62e4
commit
0f6428eed0
@@ -2,8 +2,10 @@
|
||||
// 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 System.Text.Json.Serialization;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Lib
|
||||
{
|
||||
@@ -21,7 +23,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
|
||||
Name = ModuleName;
|
||||
}
|
||||
|
||||
public virtual void Save()
|
||||
public virtual void Save(ISettingsUtils settingsUtils)
|
||||
{
|
||||
// Save settings to file
|
||||
var options = new JsonSerializerOptions
|
||||
@@ -29,7 +31,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
|
||||
WriteIndented = true,
|
||||
};
|
||||
|
||||
SettingsUtils.SaveSettings(JsonSerializer.Serialize(this, options), ModuleName);
|
||||
settingsUtils.SaveSettings(JsonSerializer.Serialize(this, options), ModuleName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
// 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.
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Lib
|
||||
{
|
||||
public interface ISettingsUtils
|
||||
{
|
||||
T GetSettings<T>(string powertoy = "", string fileName = "settings.json");
|
||||
|
||||
void SaveSettings(string jsonSettings, string powertoy = "", string fileName = "settings.json");
|
||||
|
||||
bool SettingsExists(string powertoy = "", string fileName = "settings.json");
|
||||
|
||||
void DeleteSettings(string powertoy = "");
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,10 @@
|
||||
// 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 System.Text.Json.Serialization;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Lib
|
||||
{
|
||||
@@ -21,7 +23,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
|
||||
Name = ModuleName;
|
||||
}
|
||||
|
||||
public virtual void Save()
|
||||
public virtual void Save(ISettingsUtils settingsUtils)
|
||||
{
|
||||
// Save settings to file
|
||||
var options = new JsonSerializerOptions
|
||||
@@ -29,7 +31,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
|
||||
WriteIndented = true,
|
||||
};
|
||||
|
||||
SettingsUtils.SaveSettings(JsonSerializer.Serialize(this, options), ModuleName);
|
||||
settingsUtils.SaveSettings(JsonSerializer.Serialize(this, options), ModuleName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,30 +3,35 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text.Json;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Lib
|
||||
{
|
||||
public static class SettingsUtils
|
||||
public class SettingsUtils : ISettingsUtils
|
||||
{
|
||||
private const string DefaultFileName = "settings.json";
|
||||
private const string DefaultModuleName = "";
|
||||
private IIOProvider _ioProvider;
|
||||
|
||||
public static void DeleteSettings(string powertoy, string fileName = DefaultFileName)
|
||||
public SettingsUtils(IIOProvider ioProvider)
|
||||
{
|
||||
File.Delete(GetSettingsPath(powertoy, fileName));
|
||||
_ioProvider = ioProvider ?? throw new ArgumentNullException(nameof(ioProvider));
|
||||
}
|
||||
|
||||
public static bool SettingsFolderExists(string powertoy)
|
||||
private bool SettingsFolderExists(string powertoy)
|
||||
{
|
||||
return Directory.Exists(Path.Combine(LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"));
|
||||
return _ioProvider.DirectoryExists(System.IO.Path.Combine(LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"));
|
||||
}
|
||||
|
||||
public static void CreateSettingsFolder(string powertoy)
|
||||
private void CreateSettingsFolder(string powertoy)
|
||||
{
|
||||
Directory.CreateDirectory(Path.Combine(LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"));
|
||||
_ioProvider.CreateDirectory(System.IO.Path.Combine(LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"));
|
||||
}
|
||||
|
||||
public void DeleteSettings(string powertoy = "")
|
||||
{
|
||||
_ioProvider.DeleteDirectory(System.IO.Path.Combine(LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -37,38 +42,38 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(powertoy))
|
||||
{
|
||||
return Path.Combine(
|
||||
return System.IO.Path.Combine(
|
||||
LocalApplicationDataFolder(),
|
||||
$"Microsoft\\PowerToys\\{fileName}");
|
||||
}
|
||||
|
||||
return Path.Combine(
|
||||
return System.IO.Path.Combine(
|
||||
LocalApplicationDataFolder(),
|
||||
$"Microsoft\\PowerToys\\{powertoy}\\{fileName}");
|
||||
}
|
||||
|
||||
public static bool SettingsExists(string powertoy = DefaultModuleName, string fileName = DefaultFileName)
|
||||
public bool SettingsExists(string powertoy = DefaultModuleName, string fileName = DefaultFileName)
|
||||
{
|
||||
return File.Exists(GetSettingsPath(powertoy, fileName));
|
||||
return _ioProvider.FileExists(GetSettingsPath(powertoy, fileName));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a Deserialized object of the json settings string.
|
||||
/// </summary>
|
||||
/// <returns>Deserialized json settings object.</returns>
|
||||
public static T GetSettings<T>(string powertoy = DefaultModuleName, string fileName = DefaultFileName)
|
||||
public T GetSettings<T>(string powertoy = DefaultModuleName, string fileName = DefaultFileName)
|
||||
{
|
||||
// Adding Trim('\0') to overcome possible NTFS file corruption.
|
||||
// Look at issue https://github.com/microsoft/PowerToys/issues/6413 you'll see the file has a large sum of \0 to fill up a 4096 byte buffer for writing to disk
|
||||
// This, while not totally ideal, does work around the problem by trimming the end.
|
||||
// The file itself did write the content correctly but something is off with the actual end of the file, hence the 0x00 bug
|
||||
var jsonSettingsString = File.ReadAllText(GetSettingsPath(powertoy, fileName)).Trim('\0');
|
||||
var jsonSettingsString = _ioProvider.ReadAllText(GetSettingsPath(powertoy, fileName)).Trim('\0');
|
||||
|
||||
return JsonSerializer.Deserialize<T>(jsonSettingsString);
|
||||
}
|
||||
|
||||
// Save settings to a json file.
|
||||
public static void SaveSettings(string jsonSettings, string powertoy = DefaultModuleName, string fileName = DefaultFileName)
|
||||
public void SaveSettings(string jsonSettings, string powertoy = DefaultModuleName, string fileName = DefaultFileName)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -79,7 +84,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
|
||||
CreateSettingsFolder(powertoy);
|
||||
}
|
||||
|
||||
File.WriteAllText(GetSettingsPath(powertoy, fileName), jsonSettings);
|
||||
_ioProvider.WriteAllText(GetSettingsPath(powertoy, fileName), jsonSettings);
|
||||
}
|
||||
}
|
||||
catch
|
||||
@@ -87,7 +92,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
|
||||
}
|
||||
}
|
||||
|
||||
public static string LocalApplicationDataFolder()
|
||||
private static string LocalApplicationDataFolder()
|
||||
{
|
||||
return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
// 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.
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Lib.Utilities
|
||||
{
|
||||
public interface IIOProvider
|
||||
{
|
||||
bool FileExists(string path);
|
||||
|
||||
bool DirectoryExists(string path);
|
||||
|
||||
bool CreateDirectory(string path);
|
||||
|
||||
void DeleteDirectory(string path);
|
||||
|
||||
void WriteAllText(string path, string content);
|
||||
|
||||
string ReadAllText(string path);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// 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.IO;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Lib.Utilities
|
||||
{
|
||||
public class SystemIOProvider : IIOProvider
|
||||
{
|
||||
public bool CreateDirectory(string path)
|
||||
{
|
||||
var directioryInfo = Directory.CreateDirectory(path);
|
||||
return directioryInfo != null;
|
||||
}
|
||||
|
||||
public void DeleteDirectory(string path)
|
||||
{
|
||||
Directory.Delete(path);
|
||||
}
|
||||
|
||||
public bool DirectoryExists(string path)
|
||||
{
|
||||
return Directory.Exists(path);
|
||||
}
|
||||
|
||||
public bool FileExists(string path)
|
||||
{
|
||||
return File.Exists(path);
|
||||
}
|
||||
|
||||
public string ReadAllText(string path)
|
||||
{
|
||||
return File.ReadAllText(path);
|
||||
}
|
||||
|
||||
public void WriteAllText(string path, string content)
|
||||
{
|
||||
File.WriteAllText(path, content);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,30 +5,33 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Helpers;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
{
|
||||
public class ColorPickerViewModel : Observable
|
||||
{
|
||||
private readonly ISettingsUtils _settingsUtils;
|
||||
private ColorPickerSettings _colorPickerSettings;
|
||||
private bool _isEnabled;
|
||||
|
||||
private Func<string, int> SendConfigMSG { get; }
|
||||
|
||||
public ColorPickerViewModel(Func<string, int> ipcMSGCallBackFunc)
|
||||
public ColorPickerViewModel(ISettingsUtils settingsUtils, Func<string, int> ipcMSGCallBackFunc)
|
||||
{
|
||||
if (SettingsUtils.SettingsExists(ColorPickerSettings.ModuleName))
|
||||
_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
|
||||
if (_settingsUtils.SettingsExists(ColorPickerSettings.ModuleName))
|
||||
{
|
||||
_colorPickerSettings = SettingsUtils.GetSettings<ColorPickerSettings>(ColorPickerSettings.ModuleName);
|
||||
_colorPickerSettings = _settingsUtils.GetSettings<ColorPickerSettings>(ColorPickerSettings.ModuleName);
|
||||
}
|
||||
else
|
||||
{
|
||||
_colorPickerSettings = new ColorPickerSettings();
|
||||
}
|
||||
|
||||
if (SettingsUtils.SettingsExists())
|
||||
if (_settingsUtils.SettingsExists())
|
||||
{
|
||||
var generalSettings = SettingsUtils.GetSettings<GeneralSettings>();
|
||||
var generalSettings = _settingsUtils.GetSettings<GeneralSettings>();
|
||||
_isEnabled = generalSettings.Enabled.ColorPicker;
|
||||
}
|
||||
|
||||
@@ -51,7 +54,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
OnPropertyChanged(nameof(IsEnabled));
|
||||
|
||||
// grab the latest version of settings
|
||||
var generalSettings = SettingsUtils.GetSettings<GeneralSettings>();
|
||||
var generalSettings = _settingsUtils.GetSettings<GeneralSettings>();
|
||||
generalSettings.Enabled.ColorPicker = value;
|
||||
OutGoingGeneralSettings outgoing = new OutGoingGeneralSettings(generalSettings);
|
||||
SendConfigMSG(outgoing.ToString());
|
||||
|
||||
@@ -6,12 +6,15 @@ using System;
|
||||
using System.Drawing;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Helpers;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels.Commands;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
{
|
||||
public class FancyZonesViewModel : Observable
|
||||
{
|
||||
private readonly ISettingsUtils _settingsUtils;
|
||||
|
||||
private const string ModuleName = "FancyZones";
|
||||
|
||||
public ButtonClickCommand LaunchEditorEventHandler { get; set; }
|
||||
@@ -22,18 +25,19 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
|
||||
private string settingsConfigFileFolder = string.Empty;
|
||||
|
||||
public FancyZonesViewModel(Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "")
|
||||
public FancyZonesViewModel(ISettingsUtils settingsUtils, Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "")
|
||||
{
|
||||
settingsConfigFileFolder = configFileSubfolder;
|
||||
_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
|
||||
|
||||
try
|
||||
{
|
||||
Settings = SettingsUtils.GetSettings<FancyZonesSettings>(GetSettingsSubPath());
|
||||
Settings = _settingsUtils.GetSettings<FancyZonesSettings>(GetSettingsSubPath());
|
||||
}
|
||||
catch
|
||||
{
|
||||
Settings = new FancyZonesSettings();
|
||||
SettingsUtils.SaveSettings(Settings.ToJsonString(), GetSettingsSubPath());
|
||||
_settingsUtils.SaveSettings(Settings.ToJsonString(), GetSettingsSubPath());
|
||||
}
|
||||
|
||||
LaunchEditorEventHandler = new ButtonClickCommand(LaunchEditor);
|
||||
@@ -71,12 +75,12 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
GeneralSettings generalSettings;
|
||||
try
|
||||
{
|
||||
generalSettings = SettingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
generalSettings = _settingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
}
|
||||
catch
|
||||
{
|
||||
generalSettings = new GeneralSettings();
|
||||
SettingsUtils.SaveSettings(generalSettings.ToJsonString(), string.Empty);
|
||||
_settingsUtils.SaveSettings(generalSettings.ToJsonString(), string.Empty);
|
||||
}
|
||||
|
||||
_isEnabled = generalSettings.Enabled.FancyZones;
|
||||
@@ -117,7 +121,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
if (value != _isEnabled)
|
||||
{
|
||||
_isEnabled = value;
|
||||
GeneralSettings generalSettings = SettingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
GeneralSettings generalSettings = _settingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
generalSettings.Enabled.FancyZones = value;
|
||||
OutGoingGeneralSettings snd = new OutGoingGeneralSettings(generalSettings);
|
||||
|
||||
|
||||
@@ -13,6 +13,8 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
{
|
||||
public class GeneralViewModel : Observable
|
||||
{
|
||||
private readonly ISettingsUtils _settingsUtils;
|
||||
|
||||
private GeneralSettings GeneralSettingsConfigs { get; set; }
|
||||
|
||||
public ButtonClickCommand CheckFoUpdatesEventHandler { get; set; }
|
||||
@@ -33,20 +35,21 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
|
||||
private string _settingsConfigFileFolder = string.Empty;
|
||||
|
||||
public GeneralViewModel(string runAsAdminText, string runAsUserText, bool isElevated, bool isAdmin, Func<string, int> updateTheme, Func<string, int> ipcMSGCallBackFunc, Func<string, int> ipcMSGRestartAsAdminMSGCallBackFunc, Func<string, int> ipcMSGCheckForUpdatesCallBackFunc, string configFileSubfolder = "")
|
||||
public GeneralViewModel(ISettingsUtils settingsUtils, string runAsAdminText, string runAsUserText, bool isElevated, bool isAdmin, Func<string, int> updateTheme, Func<string, int> ipcMSGCallBackFunc, Func<string, int> ipcMSGRestartAsAdminMSGCallBackFunc, Func<string, int> ipcMSGCheckForUpdatesCallBackFunc, string configFileSubfolder = "")
|
||||
{
|
||||
CheckFoUpdatesEventHandler = new ButtonClickCommand(CheckForUpdates_Click);
|
||||
RestartElevatedButtonEventHandler = new ButtonClickCommand(Restart_Elevated);
|
||||
_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
|
||||
|
||||
try
|
||||
{
|
||||
GeneralSettingsConfigs = SettingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
GeneralSettingsConfigs = _settingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
|
||||
if (Helper.CompareVersions(GeneralSettingsConfigs.PowertoysVersion, Helper.GetProductVersion()) < 0)
|
||||
{
|
||||
// Update settings
|
||||
GeneralSettingsConfigs.PowertoysVersion = Helper.GetProductVersion();
|
||||
SettingsUtils.SaveSettings(GeneralSettingsConfigs.ToJsonString(), string.Empty);
|
||||
_settingsUtils.SaveSettings(GeneralSettingsConfigs.ToJsonString(), string.Empty);
|
||||
}
|
||||
}
|
||||
catch (FormatException e)
|
||||
@@ -57,7 +60,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
catch
|
||||
{
|
||||
GeneralSettingsConfigs = new GeneralSettings();
|
||||
SettingsUtils.SaveSettings(GeneralSettingsConfigs.ToJsonString(), string.Empty);
|
||||
_settingsUtils.SaveSettings(GeneralSettingsConfigs.ToJsonString(), string.Empty);
|
||||
}
|
||||
|
||||
// set the callback functions value to hangle outgoing IPC message.
|
||||
@@ -360,7 +363,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
// callback function to launch the URL to check for updates.
|
||||
private void CheckForUpdates_Click()
|
||||
{
|
||||
GeneralSettings settings = SettingsUtils.GetSettings<GeneralSettings>(_settingsConfigFileFolder);
|
||||
GeneralSettings settings = _settingsUtils.GetSettings<GeneralSettings>(_settingsConfigFileFolder);
|
||||
settings.CustomActionName = "check_for_updates";
|
||||
|
||||
OutGoingGeneralSettings outsettings = new OutGoingGeneralSettings(settings);
|
||||
@@ -371,7 +374,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
|
||||
public void Restart_Elevated()
|
||||
{
|
||||
GeneralSettings settings = SettingsUtils.GetSettings<GeneralSettings>(_settingsConfigFileFolder);
|
||||
GeneralSettings settings = _settingsUtils.GetSettings<GeneralSettings>(_settingsConfigFileFolder);
|
||||
settings.CustomActionName = "restart_elevation";
|
||||
|
||||
OutGoingGeneralSettings outsettings = new OutGoingGeneralSettings(settings);
|
||||
|
||||
@@ -7,39 +7,44 @@ using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Helpers;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
{
|
||||
public class ImageResizerViewModel : Observable
|
||||
{
|
||||
private readonly ISettingsUtils _settingsUtils;
|
||||
|
||||
private ImageResizerSettings Settings { get; set; }
|
||||
|
||||
private const string ModuleName = "ImageResizer";
|
||||
|
||||
private Func<string, int> SendConfigMSG { get; }
|
||||
|
||||
public ImageResizerViewModel(Func<string, int> ipcMSGCallBackFunc)
|
||||
public ImageResizerViewModel(ISettingsUtils settingsUtils, Func<string, int> ipcMSGCallBackFunc)
|
||||
{
|
||||
_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
|
||||
|
||||
try
|
||||
{
|
||||
Settings = SettingsUtils.GetSettings<ImageResizerSettings>(ModuleName);
|
||||
Settings = _settingsUtils.GetSettings<ImageResizerSettings>(ModuleName);
|
||||
}
|
||||
catch
|
||||
{
|
||||
Settings = new ImageResizerSettings();
|
||||
SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
|
||||
_settingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
|
||||
}
|
||||
|
||||
GeneralSettings generalSettings;
|
||||
|
||||
try
|
||||
{
|
||||
generalSettings = SettingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
generalSettings = _settingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
}
|
||||
catch
|
||||
{
|
||||
generalSettings = new GeneralSettings();
|
||||
SettingsUtils.SaveSettings(generalSettings.ToJsonString(), string.Empty);
|
||||
_settingsUtils.SaveSettings(generalSettings.ToJsonString(), string.Empty);
|
||||
}
|
||||
|
||||
// set the callback functions value to hangle outgoing IPC message.
|
||||
@@ -84,7 +89,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
if (value != _isEnabled)
|
||||
{
|
||||
_isEnabled = value;
|
||||
GeneralSettings generalSettings = SettingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
GeneralSettings generalSettings = _settingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
generalSettings.Enabled.ImageResizer = value;
|
||||
OutGoingGeneralSettings snd = new OutGoingGeneralSettings(generalSettings);
|
||||
SendConfigMSG(snd.ToString());
|
||||
@@ -121,7 +126,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
{
|
||||
_jpegQualityLevel = value;
|
||||
Settings.Properties.ImageresizerJpegQualityLevel.Value = value;
|
||||
SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
|
||||
_settingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
|
||||
OnPropertyChanged("JPEGQualityLevel");
|
||||
}
|
||||
}
|
||||
@@ -140,7 +145,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
{
|
||||
_pngInterlaceOption = value;
|
||||
Settings.Properties.ImageresizerPngInterlaceOption.Value = value;
|
||||
SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
|
||||
_settingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
|
||||
OnPropertyChanged("PngInterlaceOption");
|
||||
}
|
||||
}
|
||||
@@ -159,7 +164,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
{
|
||||
_tiffCompressOption = value;
|
||||
Settings.Properties.ImageresizerTiffCompressOption.Value = value;
|
||||
SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
|
||||
_settingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
|
||||
OnPropertyChanged("TiffCompressOption");
|
||||
}
|
||||
}
|
||||
@@ -178,7 +183,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
{
|
||||
_fileName = value;
|
||||
Settings.Properties.ImageresizerFileName.Value = value;
|
||||
SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
|
||||
_settingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
|
||||
OnPropertyChanged("FileName");
|
||||
}
|
||||
}
|
||||
@@ -195,7 +200,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
{
|
||||
_keepDateModified = value;
|
||||
Settings.Properties.ImageresizerKeepDateModified.Value = value;
|
||||
SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
|
||||
_settingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
|
||||
OnPropertyChanged("KeepDateModified");
|
||||
}
|
||||
}
|
||||
@@ -212,9 +217,9 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
if (_encoderGuidId != value)
|
||||
{
|
||||
_encoderGuidId = value;
|
||||
SettingsUtils.SaveSettings(Settings.Properties.ImageresizerSizes.ToJsonString(), ModuleName, "sizes.json");
|
||||
_settingsUtils.SaveSettings(Settings.Properties.ImageresizerSizes.ToJsonString(), ModuleName, "sizes.json");
|
||||
Settings.Properties.ImageresizerFallbackEncoder.Value = GetEncoderGuid(value);
|
||||
SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
|
||||
_settingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
|
||||
OnPropertyChanged("Encoder");
|
||||
}
|
||||
}
|
||||
@@ -243,9 +248,9 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
|
||||
public void SavesImageSizes(ObservableCollection<ImageSize> imageSizes)
|
||||
{
|
||||
SettingsUtils.SaveSettings(Settings.Properties.ImageresizerSizes.ToJsonString(), ModuleName, "sizes.json");
|
||||
_settingsUtils.SaveSettings(Settings.Properties.ImageresizerSizes.ToJsonString(), ModuleName, "sizes.json");
|
||||
Settings.Properties.ImageresizerSizes = new ImageResizerSizes(imageSizes);
|
||||
SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
|
||||
_settingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
|
||||
}
|
||||
|
||||
public string GetEncoderGuid(int value)
|
||||
|
||||
@@ -16,6 +16,8 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
{
|
||||
public class KeyboardManagerViewModel : Observable
|
||||
{
|
||||
private readonly ISettingsUtils _settingsUtils;
|
||||
|
||||
private const string PowerToyName = "Keyboard Manager";
|
||||
private const string RemapKeyboardActionName = "RemapKeyboard";
|
||||
private const string RemapKeyboardActionValue = "Open Remap Keyboard Window";
|
||||
@@ -36,16 +38,18 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
|
||||
private Func<List<KeysDataModel>, int> FilterRemapKeysList { get; }
|
||||
|
||||
public KeyboardManagerViewModel(Func<string, int> ipcMSGCallBackFunc, Func<List<KeysDataModel>, int> filterRemapKeysList)
|
||||
public KeyboardManagerViewModel(ISettingsUtils settingsUtils, Func<string, int> ipcMSGCallBackFunc, Func<List<KeysDataModel>, int> filterRemapKeysList)
|
||||
{
|
||||
// set the callback functions value to hangle outgoing IPC message.
|
||||
SendConfigMSG = ipcMSGCallBackFunc;
|
||||
FilterRemapKeysList = filterRemapKeysList;
|
||||
|
||||
if (SettingsUtils.SettingsExists(PowerToyName))
|
||||
_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
|
||||
|
||||
if (_settingsUtils.SettingsExists(PowerToyName))
|
||||
{
|
||||
// Todo: Be more resilient while reading and saving settings.
|
||||
Settings = SettingsUtils.GetSettings<KeyboardManagerSettings>(PowerToyName);
|
||||
Settings = _settingsUtils.GetSettings<KeyboardManagerSettings>(PowerToyName);
|
||||
|
||||
// Load profile.
|
||||
if (!LoadProfile())
|
||||
@@ -56,17 +60,17 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
else
|
||||
{
|
||||
Settings = new KeyboardManagerSettings(PowerToyName);
|
||||
SettingsUtils.SaveSettings(Settings.ToJsonString(), PowerToyName);
|
||||
_settingsUtils.SaveSettings(Settings.ToJsonString(), PowerToyName);
|
||||
}
|
||||
|
||||
if (SettingsUtils.SettingsExists())
|
||||
if (_settingsUtils.SettingsExists())
|
||||
{
|
||||
_generalSettings = SettingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
_generalSettings = _settingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
}
|
||||
else
|
||||
{
|
||||
_generalSettings = new GeneralSettings();
|
||||
SettingsUtils.SaveSettings(_generalSettings.ToJsonString(), string.Empty);
|
||||
_settingsUtils.SaveSettings(_generalSettings.ToJsonString(), string.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,7 +177,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
// update the UI element here.
|
||||
try
|
||||
{
|
||||
_profile = SettingsUtils.GetSettings<KeyboardManagerProfile>(PowerToyName, Settings.Properties.ActiveConfiguration.Value + JsonFileType);
|
||||
_profile = _settingsUtils.GetSettings<KeyboardManagerProfile>(PowerToyName, Settings.Properties.ActiveConfiguration.Value + JsonFileType);
|
||||
FilterRemapKeysList(_profile.RemapKeys.InProcessRemapKeys);
|
||||
}
|
||||
finally
|
||||
|
||||
@@ -6,11 +6,14 @@ using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text.Json;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Helpers;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
{
|
||||
public class PowerLauncherViewModel : Observable
|
||||
{
|
||||
private readonly ISettingsUtils _settingsUtils;
|
||||
|
||||
private PowerLauncherSettings settings;
|
||||
private GeneralSettings generalSettings;
|
||||
|
||||
@@ -20,21 +23,21 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
|
||||
private Func<string, int> SendConfigMSG { get; }
|
||||
|
||||
public PowerLauncherViewModel(Func<string, int> ipcMSGCallBackFunc, int defaultKeyCode)
|
||||
public PowerLauncherViewModel(ISettingsUtils settingsUtils, Func<string, int> ipcMSGCallBackFunc, int defaultKeyCode)
|
||||
{
|
||||
_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
|
||||
|
||||
// set the callback functions value to hangle outgoing IPC message.
|
||||
SendConfigMSG = ipcMSGCallBackFunc;
|
||||
|
||||
callback = (PowerLauncherSettings settings) =>
|
||||
{
|
||||
// Propagate changes to Power Launcher through IPC
|
||||
SendConfigMSG(
|
||||
string.Format("{{ \"powertoys\": {{ \"{0}\": {1} }} }}", PowerLauncherSettings.ModuleName, JsonSerializer.Serialize(settings)));
|
||||
};
|
||||
|
||||
if (SettingsUtils.SettingsExists(PowerLauncherSettings.ModuleName))
|
||||
if (_settingsUtils.SettingsExists(PowerLauncherSettings.ModuleName))
|
||||
{
|
||||
settings = SettingsUtils.GetSettings<PowerLauncherSettings>(PowerLauncherSettings.ModuleName);
|
||||
settings = _settingsUtils.GetSettings<PowerLauncherSettings>(PowerLauncherSettings.ModuleName);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -45,9 +48,9 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
callback(settings);
|
||||
}
|
||||
|
||||
if (SettingsUtils.SettingsExists())
|
||||
if (_settingsUtils.SettingsExists())
|
||||
{
|
||||
generalSettings = SettingsUtils.GetSettings<GeneralSettings>();
|
||||
generalSettings = _settingsUtils.GetSettings<GeneralSettings>();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -5,11 +5,14 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Helpers;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
{
|
||||
public class PowerPreviewViewModel : Observable
|
||||
{
|
||||
private readonly ISettingsUtils _settingsUtils;
|
||||
|
||||
private const string ModuleName = "File Explorer";
|
||||
|
||||
private PowerPreviewSettings Settings { get; set; }
|
||||
@@ -18,19 +21,20 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
|
||||
private string _settingsConfigFileFolder = string.Empty;
|
||||
|
||||
public PowerPreviewViewModel(Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "")
|
||||
public PowerPreviewViewModel(ISettingsUtils settingsUtils, Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "")
|
||||
{
|
||||
// Update Settings file folder:
|
||||
_settingsConfigFileFolder = configFileSubfolder;
|
||||
_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
|
||||
|
||||
try
|
||||
{
|
||||
Settings = SettingsUtils.GetSettings<PowerPreviewSettings>(GetSettingsSubPath());
|
||||
Settings = _settingsUtils.GetSettings<PowerPreviewSettings>(GetSettingsSubPath());
|
||||
}
|
||||
catch
|
||||
{
|
||||
Settings = new PowerPreviewSettings();
|
||||
SettingsUtils.SaveSettings(Settings.ToJsonString(), GetSettingsSubPath());
|
||||
_settingsUtils.SaveSettings(Settings.ToJsonString(), GetSettingsSubPath());
|
||||
}
|
||||
|
||||
// set the callback functions value to hangle outgoing IPC message.
|
||||
|
||||
@@ -5,11 +5,14 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Helpers;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
{
|
||||
public class PowerRenameViewModel : Observable
|
||||
{
|
||||
private readonly ISettingsUtils _settingsUtils;
|
||||
|
||||
private const string ModuleName = "PowerRename";
|
||||
|
||||
private string _settingsConfigFileFolder = string.Empty;
|
||||
@@ -18,21 +21,22 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
|
||||
private Func<string, int> SendConfigMSG { get; }
|
||||
|
||||
public PowerRenameViewModel(Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "")
|
||||
public PowerRenameViewModel(ISettingsUtils settingsUtils, Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "")
|
||||
{
|
||||
// Update Settings file folder:
|
||||
_settingsConfigFileFolder = configFileSubfolder;
|
||||
_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
|
||||
|
||||
try
|
||||
{
|
||||
PowerRenameLocalProperties localSettings = SettingsUtils.GetSettings<PowerRenameLocalProperties>(GetSettingsSubPath(), "power-rename-settings.json");
|
||||
PowerRenameLocalProperties localSettings = _settingsUtils.GetSettings<PowerRenameLocalProperties>(GetSettingsSubPath(), "power-rename-settings.json");
|
||||
Settings = new PowerRenameSettings(localSettings);
|
||||
}
|
||||
catch
|
||||
{
|
||||
PowerRenameLocalProperties localSettings = new PowerRenameLocalProperties();
|
||||
Settings = new PowerRenameSettings(localSettings);
|
||||
SettingsUtils.SaveSettings(localSettings.ToJsonString(), GetSettingsSubPath(), "power-rename-settings.json");
|
||||
_settingsUtils.SaveSettings(localSettings.ToJsonString(), GetSettingsSubPath(), "power-rename-settings.json");
|
||||
}
|
||||
|
||||
// set the callback functions value to hangle outgoing IPC message.
|
||||
@@ -47,12 +51,12 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
GeneralSettings generalSettings;
|
||||
try
|
||||
{
|
||||
generalSettings = SettingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
generalSettings = _settingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
}
|
||||
catch
|
||||
{
|
||||
generalSettings = new GeneralSettings();
|
||||
SettingsUtils.SaveSettings(generalSettings.ToJsonString(), string.Empty);
|
||||
_settingsUtils.SaveSettings(generalSettings.ToJsonString(), string.Empty);
|
||||
}
|
||||
|
||||
_powerRenameEnabled = generalSettings.Enabled.PowerRename;
|
||||
@@ -76,7 +80,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
{
|
||||
if (value != _powerRenameEnabled)
|
||||
{
|
||||
GeneralSettings generalSettings = SettingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
GeneralSettings generalSettings = _settingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
generalSettings.Enabled.PowerRename = value;
|
||||
OutGoingGeneralSettings snd = new OutGoingGeneralSettings(generalSettings);
|
||||
SendConfigMSG(snd.ToString());
|
||||
|
||||
@@ -5,11 +5,14 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Helpers;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
{
|
||||
public class ShortcutGuideViewModel : Observable
|
||||
{
|
||||
private readonly ISettingsUtils _settingsUtils;
|
||||
|
||||
private ShortcutGuideSettings Settings { get; set; }
|
||||
|
||||
private const string ModuleName = "Shortcut Guide";
|
||||
@@ -18,31 +21,32 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
|
||||
private string _settingsConfigFileFolder = string.Empty;
|
||||
|
||||
public ShortcutGuideViewModel(Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "")
|
||||
public ShortcutGuideViewModel(ISettingsUtils settingsUtils, Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "")
|
||||
{
|
||||
// Update Settings file folder:
|
||||
_settingsConfigFileFolder = configFileSubfolder;
|
||||
_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
|
||||
|
||||
try
|
||||
{
|
||||
Settings = SettingsUtils.GetSettings<ShortcutGuideSettings>(GetSettingsSubPath());
|
||||
Settings = _settingsUtils.GetSettings<ShortcutGuideSettings>(GetSettingsSubPath());
|
||||
}
|
||||
catch
|
||||
{
|
||||
Settings = new ShortcutGuideSettings();
|
||||
SettingsUtils.SaveSettings(Settings.ToJsonString(), GetSettingsSubPath());
|
||||
_settingsUtils.SaveSettings(Settings.ToJsonString(), GetSettingsSubPath());
|
||||
}
|
||||
|
||||
GeneralSettings generalSettings;
|
||||
|
||||
try
|
||||
{
|
||||
generalSettings = SettingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
generalSettings = _settingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
}
|
||||
catch
|
||||
{
|
||||
generalSettings = new GeneralSettings();
|
||||
SettingsUtils.SaveSettings(generalSettings.ToJsonString(), string.Empty);
|
||||
_settingsUtils.SaveSettings(generalSettings.ToJsonString(), string.Empty);
|
||||
}
|
||||
|
||||
// set the callback functions value to hangle outgoing IPC message.
|
||||
@@ -87,7 +91,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
||||
if (value != _isEnabled)
|
||||
{
|
||||
_isEnabled = value;
|
||||
GeneralSettings generalSettings = SettingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
GeneralSettings generalSettings = _settingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
generalSettings.Enabled.ShortcutGuide = value;
|
||||
OutGoingGeneralSettings snd = new OutGoingGeneralSettings(generalSettings);
|
||||
SendConfigMSG(snd.ToString());
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
|
||||
<PackageReference Include="Moq" Version="4.14.5" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
|
||||
<PackageReference Include="coverlet.collector" Version="1.3.0">
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.UnitTests.Mocks
|
||||
{
|
||||
internal static class IIOProviderMocks
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// This method mocks an IO provider to validate tests wich required saving to a file, and then reading the contents of that file, or verifying it exists
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
internal static Mock<IIOProvider> GetMockIOProviderForSaveLoadExists()
|
||||
{
|
||||
string savePath = string.Empty;
|
||||
string saveContent = string.Empty;
|
||||
var mockIOProvider = new Mock<IIOProvider>();
|
||||
mockIOProvider.Setup(x => x.WriteAllText(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Callback<string, string>((path, content) =>
|
||||
{
|
||||
savePath = path;
|
||||
saveContent = content;
|
||||
});
|
||||
mockIOProvider.Setup(x => x.ReadAllText(It.Is<string>(x => x.Equals(savePath, StringComparison.Ordinal))))
|
||||
.Returns(() => saveContent);
|
||||
|
||||
mockIOProvider.Setup(x => x.FileExists(It.Is<string>(x => x.Equals(savePath, StringComparison.Ordinal))))
|
||||
.Returns(true);
|
||||
mockIOProvider.Setup(x => x.FileExists(It.Is<string>(x => !x.Equals(savePath, StringComparison.Ordinal))))
|
||||
.Returns(false);
|
||||
|
||||
return mockIOProvider;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.UnitTests.Mocks
|
||||
{
|
||||
internal static class ISettingsUtilsMocks
|
||||
{
|
||||
//Stubs out empty values for imageresizersettings and general settings as needed by the imageresizer viewmodel
|
||||
internal static Mock<ISettingsUtils> GetStubSettingsUtils()
|
||||
{
|
||||
var settingsUtils = new Mock<ISettingsUtils>();
|
||||
settingsUtils.Setup(x => x.GetSettings<It.IsAnyType>(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(new InvocationFunc(invocation =>
|
||||
{
|
||||
var typeArgument = invocation.Method.GetGenericArguments()[0];
|
||||
return Activator.CreateInstance(typeArgument);
|
||||
}));
|
||||
|
||||
return settingsUtils;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,11 @@
|
||||
|
||||
using System;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
|
||||
using Microsoft.PowerToys.Settings.UnitTest;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Schema;
|
||||
|
||||
@@ -21,6 +24,13 @@ namespace CommonLibTest
|
||||
[Obsolete]
|
||||
public void ToJsonString_ShouldReturnValidJSONOfModel_WhenSuccessful()
|
||||
{
|
||||
//Mock Disk access
|
||||
string saveContent = string.Empty;
|
||||
string savePath = string.Empty;
|
||||
var mockIOProvider = IIOProviderMocks.GetMockIOProviderForSaveLoadExists();
|
||||
|
||||
var settingsUtils = new SettingsUtils(mockIOProvider.Object);
|
||||
|
||||
// Arrange
|
||||
string file_name = "test\\BasePTModuleSettingsTest";
|
||||
string expectedSchemaText = @"
|
||||
@@ -39,11 +49,11 @@ namespace CommonLibTest
|
||||
}";
|
||||
|
||||
string testSettingsConfigs = new BasePTSettingsTest().ToJsonString();
|
||||
SettingsUtils.SaveSettings(testSettingsConfigs, file_name);
|
||||
settingsUtils.SaveSettings(testSettingsConfigs, file_name);
|
||||
JsonSchema expectedSchema = JsonSchema.Parse(expectedSchemaText);
|
||||
|
||||
// Act
|
||||
JObject actualSchema = JObject.Parse(SettingsUtils.GetSettings<BasePTSettingsTest>(file_name).ToJsonString());
|
||||
JObject actualSchema = JObject.Parse(settingsUtils.GetSettings<BasePTSettingsTest>(file_name).ToJsonString());
|
||||
bool valid = actualSchema.IsValid(expectedSchema);
|
||||
|
||||
// Assert
|
||||
|
||||
@@ -7,45 +7,34 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
|
||||
using Microsoft.PowerToys.Settings.UnitTest;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace CommonLibTest
|
||||
{
|
||||
[TestClass]
|
||||
public class SettingsUtilsTests
|
||||
{
|
||||
public SettingsUtilsTests()
|
||||
{
|
||||
string file_name = "\\test";
|
||||
if (SettingsUtils.SettingsFolderExists(file_name))
|
||||
{
|
||||
DeleteFolder(file_name);
|
||||
}
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void Cleanup()
|
||||
{
|
||||
string file_name = "\\test";
|
||||
if (SettingsUtils.SettingsFolderExists(file_name))
|
||||
{
|
||||
DeleteFolder(file_name);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SaveSettings_SaveSettingsToFile_WhenFilePathExists()
|
||||
{
|
||||
// Arrange
|
||||
var mockIOProvider = IIOProviderMocks.GetMockIOProviderForSaveLoadExists();
|
||||
var settingsUtils = new SettingsUtils(mockIOProvider.Object);
|
||||
|
||||
string file_name = "\\test";
|
||||
string file_contents_correct_json_content = "{\"name\":\"powertoy module name\",\"version\":\"powertoy version\"}";
|
||||
|
||||
BasePTSettingsTest expected_json = JsonSerializer.Deserialize<BasePTSettingsTest>(file_contents_correct_json_content);
|
||||
|
||||
// Act
|
||||
SettingsUtils.SaveSettings(file_contents_correct_json_content, file_name);
|
||||
BasePTSettingsTest actual_json = SettingsUtils.GetSettings<BasePTSettingsTest>(file_name);
|
||||
settingsUtils.SaveSettings(file_contents_correct_json_content, file_name);
|
||||
BasePTSettingsTest actual_json = settingsUtils.GetSettings<BasePTSettingsTest>(file_name);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(expected_json.ToJsonString(), actual_json.ToJsonString());
|
||||
@@ -55,19 +44,15 @@ namespace CommonLibTest
|
||||
public void SaveSettings_ShouldCreateFile_WhenFilePathIsNotFound()
|
||||
{
|
||||
// Arrange
|
||||
var mockIOProvider = IIOProviderMocks.GetMockIOProviderForSaveLoadExists();
|
||||
var settingsUtils = new SettingsUtils(mockIOProvider.Object);
|
||||
string file_name = "test\\Test Folder";
|
||||
string file_contents_correct_json_content = "{\"name\":\"powertoy module name\",\"version\":\"powertoy version\"}";
|
||||
|
||||
BasePTSettingsTest expected_json = JsonSerializer.Deserialize<BasePTSettingsTest>(file_contents_correct_json_content);
|
||||
|
||||
// Act
|
||||
if (SettingsUtils.SettingsFolderExists(file_name))
|
||||
{
|
||||
DeleteFolder(file_name);
|
||||
}
|
||||
|
||||
SettingsUtils.SaveSettings(file_contents_correct_json_content, file_name);
|
||||
BasePTSettingsTest actual_json = SettingsUtils.GetSettings<BasePTSettingsTest>(file_name);
|
||||
settingsUtils.SaveSettings(file_contents_correct_json_content, file_name);
|
||||
BasePTSettingsTest actual_json = settingsUtils.GetSettings<BasePTSettingsTest>(file_name);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(expected_json.ToJsonString(), actual_json.ToJsonString());
|
||||
@@ -77,39 +62,23 @@ namespace CommonLibTest
|
||||
public void SettingsFolderExists_ShouldReturnFalse_WhenFilePathIsNotFound()
|
||||
{
|
||||
// Arrange
|
||||
var mockIOProvider = IIOProviderMocks.GetMockIOProviderForSaveLoadExists();
|
||||
var settingsUtils = new SettingsUtils(mockIOProvider.Object);
|
||||
string file_name_random = "test\\" + RandomString();
|
||||
string file_name_exists = "test\\exists";
|
||||
string file_contents_correct_json_content = "{\"name\":\"powertoy module name\",\"version\":\"powertoy version\"}";
|
||||
|
||||
// Act
|
||||
bool pathNotFound = SettingsUtils.SettingsFolderExists(file_name_random);
|
||||
bool pathNotFound = settingsUtils.SettingsExists(file_name_random);
|
||||
|
||||
SettingsUtils.SaveSettings(file_contents_correct_json_content, file_name_exists);
|
||||
bool pathFound = SettingsUtils.SettingsFolderExists(file_name_exists);
|
||||
settingsUtils.SaveSettings(file_contents_correct_json_content, file_name_exists);
|
||||
bool pathFound = settingsUtils.SettingsExists(file_name_exists);
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(pathNotFound);
|
||||
Assert.IsTrue(pathFound);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CreateSettingsFolder_ShouldCreateFolder_WhenSuccessful()
|
||||
{
|
||||
// Arrange
|
||||
string file_name = "test\\" + RandomString();
|
||||
|
||||
// Act
|
||||
SettingsUtils.CreateSettingsFolder(file_name);
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(SettingsUtils.SettingsFolderExists(file_name));
|
||||
}
|
||||
|
||||
public void DeleteFolder(string powertoy)
|
||||
{
|
||||
Directory.Delete(Path.Combine(SettingsUtils.LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"), true);
|
||||
}
|
||||
|
||||
public static string RandomString()
|
||||
{
|
||||
Random random = new Random();
|
||||
|
||||
@@ -6,58 +6,15 @@ using System.IO;
|
||||
using System.Text.Json;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace ViewModelTests
|
||||
{
|
||||
[TestClass]
|
||||
public class ColorPicker
|
||||
{
|
||||
private const string ModuleName = "ColorPicker";
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
var generalSettings = new GeneralSettings();
|
||||
var colorPickerSettings = new ColorPickerSettings();
|
||||
|
||||
SettingsUtils.SaveSettings(generalSettings.ToJsonString());
|
||||
SettingsUtils.SaveSettings(colorPickerSettings.ToJsonString(), colorPickerSettings.Name, ModuleName + ".json");
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void CleanUp()
|
||||
{
|
||||
string generalSettings_file_name = string.Empty;
|
||||
if (SettingsUtils.SettingsFolderExists(generalSettings_file_name))
|
||||
{
|
||||
DeleteFolder(generalSettings_file_name);
|
||||
}
|
||||
|
||||
if (SettingsUtils.SettingsFolderExists(ModuleName))
|
||||
{
|
||||
DeleteFolder(ModuleName);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ColorPickerIsEnabledByDefault()
|
||||
{
|
||||
var viewModel = new ColorPickerViewModel(ColorPickerIsEnabledByDefault_IPC);
|
||||
|
||||
Assert.IsTrue(viewModel.IsEnabled);
|
||||
}
|
||||
|
||||
public int ColorPickerIsEnabledByDefault_IPC(string msg)
|
||||
{
|
||||
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
|
||||
Assert.IsTrue(snd.GeneralSettings.Enabled.ColorPicker);
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static void DeleteFolder(string powertoy)
|
||||
{
|
||||
Directory.Delete(Path.Combine(SettingsUtils.LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"), true);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,9 @@ using System.Text.Json;
|
||||
using CommonLibTest;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace ViewModelTests
|
||||
{
|
||||
@@ -18,39 +20,6 @@ namespace ViewModelTests
|
||||
{
|
||||
public const string FancyZonesTestFolderName = "Test\\FancyZones";
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
// initialize creation of test settings file.
|
||||
GeneralSettings generalSettings = new GeneralSettings();
|
||||
FZConfigProperties fZConfigProperties = new FZConfigProperties();
|
||||
|
||||
SettingsUtils.SaveSettings(generalSettings.ToJsonString());
|
||||
SettingsUtils.SaveSettings(fZConfigProperties.ToJsonString(), FancyZonesTestFolderName);
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void CleanUp()
|
||||
{
|
||||
// delete general settings folder created.
|
||||
string generalSettings_file_name = string.Empty;
|
||||
if (SettingsUtils.SettingsFolderExists(string.Empty))
|
||||
{
|
||||
DeleteFolder(string.Empty);
|
||||
}
|
||||
|
||||
// delete fancy zones folder created.
|
||||
if (SettingsUtils.SettingsFolderExists(FancyZonesTestFolderName))
|
||||
{
|
||||
DeleteFolder(FancyZonesTestFolderName);
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteFolder(string powertoy)
|
||||
{
|
||||
Directory.Delete(Path.Combine(SettingsUtils.LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"), true);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsEnabled_ShouldDisableModule_WhenSuccessful()
|
||||
{
|
||||
@@ -62,7 +31,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsTrue(viewModel.IsEnabled); // check if the module is enabled.
|
||||
|
||||
// act
|
||||
@@ -81,7 +50,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsTrue(viewModel.ShiftDrag); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
@@ -100,7 +69,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsFalse(viewModel.OverrideSnapHotkeys); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
@@ -119,7 +88,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsFalse(viewModel.MoveWindowsBasedOnPosition); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
@@ -138,7 +107,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsFalse(viewModel.MakeDraggedWindowsTransparent); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
@@ -157,7 +126,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsFalse(viewModel.MouseSwitch); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
@@ -176,7 +145,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsFalse(viewModel.DisplayChangeMoveWindows); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
@@ -195,7 +164,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsFalse(viewModel.ZoneSetChangeMoveWindows); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
@@ -214,7 +183,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsFalse(viewModel.AppLastZoneMoveWindows); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
@@ -232,7 +201,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsFalse(viewModel.OpenWindowOnActiveMonitor); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
@@ -251,7 +220,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsFalse(viewModel.RestoreSize); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
@@ -270,7 +239,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsTrue(viewModel.UseCursorPosEditorStartupScreen); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
@@ -289,7 +258,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.IsFalse(viewModel.ShowOnAllMonitors); // check if value was initialized to false.
|
||||
|
||||
// act
|
||||
@@ -308,7 +277,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.AreEqual(ConfigDefaults.DefaultFancyZonesZoneHighlightColor, viewModel.ZoneHighlightColor);
|
||||
|
||||
// act
|
||||
@@ -327,7 +296,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.AreEqual(ConfigDefaults.DefaultFancyzonesBorderColor, viewModel.ZoneBorderColor);
|
||||
|
||||
// act
|
||||
@@ -346,7 +315,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.AreEqual(ConfigDefaults.DefaultFancyZonesInActiveColor, viewModel.ZoneInActiveColor);
|
||||
|
||||
// act
|
||||
@@ -365,7 +334,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.AreEqual(string.Empty, viewModel.ExcludedApps);
|
||||
|
||||
// act
|
||||
@@ -384,7 +353,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName);
|
||||
Assert.AreEqual(50, viewModel.HighlightOpacity);
|
||||
|
||||
// act
|
||||
|
||||
@@ -8,6 +8,8 @@ using System.Text.Json;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using NuGet.Frameworks;
|
||||
|
||||
namespace ViewModelTests
|
||||
{
|
||||
@@ -16,29 +18,6 @@ namespace ViewModelTests
|
||||
{
|
||||
public const string generalSettings_file_name = "Test\\GenealSettings";
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
// initialize creation of test settings file.
|
||||
GeneralSettings generalSettings = new GeneralSettings();
|
||||
SettingsUtils.SaveSettings(generalSettings.ToJsonString(), generalSettings_file_name);
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void CleanUp()
|
||||
{
|
||||
// delete folder created.
|
||||
if (SettingsUtils.SettingsFolderExists(generalSettings_file_name))
|
||||
{
|
||||
DeleteFolder(generalSettings_file_name);
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteFolder(string powertoy)
|
||||
{
|
||||
Directory.Delete(Path.Combine(SettingsUtils.LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"), true);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsElevated_ShouldUpdateRunasAdminStatusAttrs_WhenSuccessful()
|
||||
{
|
||||
@@ -47,6 +26,7 @@ namespace ViewModelTests
|
||||
Func<string, int> SendRestartAdminIPCMessage = msg => { return 0; };
|
||||
Func<string, int> SendCheckForUpdatesIPCMessage = msg => { return 0; };
|
||||
GeneralViewModel viewModel = new GeneralViewModel(
|
||||
new Mock<ISettingsUtils>().Object,
|
||||
"GeneralSettings_RunningAsAdminText",
|
||||
"GeneralSettings_RunningAsUserText",
|
||||
false,
|
||||
@@ -83,6 +63,7 @@ namespace ViewModelTests
|
||||
Func<string, int> SendRestartAdminIPCMessage = msg => { return 0; };
|
||||
Func<string, int> SendCheckForUpdatesIPCMessage = msg => { return 0; };
|
||||
GeneralViewModel viewModel = new GeneralViewModel(
|
||||
new Mock<ISettingsUtils>().Object,
|
||||
"GeneralSettings_RunningAsAdminText",
|
||||
"GeneralSettings_RunningAsUserText",
|
||||
false,
|
||||
@@ -114,6 +95,7 @@ namespace ViewModelTests
|
||||
|
||||
// Arrange
|
||||
GeneralViewModel viewModel = new GeneralViewModel(
|
||||
new Mock<ISettingsUtils>().Object,
|
||||
"GeneralSettings_RunningAsAdminText",
|
||||
"GeneralSettings_RunningAsUserText",
|
||||
false,
|
||||
@@ -146,6 +128,7 @@ namespace ViewModelTests
|
||||
Func<string, int> SendRestartAdminIPCMessage = msg => { return 0; };
|
||||
Func<string, int> SendCheckForUpdatesIPCMessage = msg => { return 0; };
|
||||
viewModel = new GeneralViewModel(
|
||||
new Mock<ISettingsUtils>().Object,
|
||||
"GeneralSettings_RunningAsAdminText",
|
||||
"GeneralSettings_RunningAsUserText",
|
||||
false,
|
||||
@@ -176,6 +159,7 @@ namespace ViewModelTests
|
||||
Func<string, int> SendRestartAdminIPCMessage = msg => { return 0; };
|
||||
Func<string, int> SendCheckForUpdatesIPCMessage = msg => { return 0; };
|
||||
GeneralViewModel viewModel = new GeneralViewModel(
|
||||
new Mock<ISettingsUtils>().Object,
|
||||
"GeneralSettings_RunningAsAdminText",
|
||||
"GeneralSettings_RunningAsUserText",
|
||||
false,
|
||||
@@ -193,6 +177,24 @@ namespace ViewModelTests
|
||||
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 int UpdateUIThemeMethod(string themeName)
|
||||
{
|
||||
return 0;
|
||||
|
||||
@@ -7,8 +7,11 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace ViewModelTests
|
||||
{
|
||||
@@ -17,43 +20,11 @@ namespace ViewModelTests
|
||||
{
|
||||
public const string Module = "ImageResizer";
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
// initialize creation of test settings file.
|
||||
// Test base path:
|
||||
// C:\Users\<user name>\AppData\Local\Packages\08e1807b-8b6d-4bfa-adc4-79c64aae8e78_9abkseg265h2m\LocalState\Microsoft\PowerToys\
|
||||
GeneralSettings generalSettings = new GeneralSettings();
|
||||
ImageResizerSettings imageResizer = new ImageResizerSettings();
|
||||
|
||||
SettingsUtils.SaveSettings(generalSettings.ToJsonString());
|
||||
SettingsUtils.SaveSettings(imageResizer.ToJsonString(), imageResizer.Name);
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void CleanUp()
|
||||
{
|
||||
// delete folder created.
|
||||
string generalSettings_file_name = string.Empty;
|
||||
if (SettingsUtils.SettingsFolderExists(generalSettings_file_name))
|
||||
{
|
||||
DeleteFolder(generalSettings_file_name);
|
||||
}
|
||||
|
||||
if (SettingsUtils.SettingsFolderExists(Module))
|
||||
{
|
||||
DeleteFolder(Module);
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteFolder(string powertoy)
|
||||
{
|
||||
Directory.Delete(Path.Combine(SettingsUtils.LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"), true);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsEnabled_ShouldEnableModule_WhenSuccessful()
|
||||
{
|
||||
var mockSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils();
|
||||
|
||||
// Assert
|
||||
Func<string, int> SendMockIPCConfigMSG = msg =>
|
||||
{
|
||||
@@ -63,7 +34,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(SendMockIPCConfigMSG);
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils.Object, SendMockIPCConfigMSG);
|
||||
|
||||
// act
|
||||
viewModel.IsEnabled = true;
|
||||
@@ -73,14 +44,16 @@ namespace ViewModelTests
|
||||
public void JPEGQualityLevel_ShouldSetValueToTen_WhenSuccessful()
|
||||
{
|
||||
// arrange
|
||||
var mockIOProvider = IIOProviderMocks.GetMockIOProviderForSaveLoadExists();
|
||||
var mockSettingsUtils = new SettingsUtils(mockIOProvider.Object);
|
||||
Func<string, int> SendMockIPCConfigMSG = msg => { return 0; };
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(SendMockIPCConfigMSG);
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils, SendMockIPCConfigMSG);
|
||||
|
||||
// act
|
||||
viewModel.JPEGQualityLevel = 10;
|
||||
|
||||
// Assert
|
||||
viewModel = new ImageResizerViewModel(SendMockIPCConfigMSG);
|
||||
viewModel = new ImageResizerViewModel(mockSettingsUtils, SendMockIPCConfigMSG);
|
||||
Assert.AreEqual(10, viewModel.JPEGQualityLevel);
|
||||
}
|
||||
|
||||
@@ -88,14 +61,16 @@ namespace ViewModelTests
|
||||
public void PngInterlaceOption_ShouldSetValueToTen_WhenSuccessful()
|
||||
{
|
||||
// arrange
|
||||
var mockIOProvider = IIOProviderMocks.GetMockIOProviderForSaveLoadExists();
|
||||
var mockSettingsUtils = new SettingsUtils(mockIOProvider.Object);
|
||||
Func<string, int> SendMockIPCConfigMSG = msg => { return 0; };
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(SendMockIPCConfigMSG);
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils, SendMockIPCConfigMSG);
|
||||
|
||||
// act
|
||||
viewModel.PngInterlaceOption = 10;
|
||||
|
||||
// Assert
|
||||
viewModel = new ImageResizerViewModel(SendMockIPCConfigMSG);
|
||||
viewModel = new ImageResizerViewModel(mockSettingsUtils, SendMockIPCConfigMSG);
|
||||
Assert.AreEqual(10, viewModel.PngInterlaceOption);
|
||||
}
|
||||
|
||||
@@ -103,14 +78,16 @@ namespace ViewModelTests
|
||||
public void TiffCompressOption_ShouldSetValueToTen_WhenSuccessful()
|
||||
{
|
||||
// arrange
|
||||
var mockIOProvider = IIOProviderMocks.GetMockIOProviderForSaveLoadExists();
|
||||
var mockSettingsUtils = new SettingsUtils(mockIOProvider.Object);
|
||||
Func<string, int> SendMockIPCConfigMSG = msg => { return 0; };
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(SendMockIPCConfigMSG);
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils, SendMockIPCConfigMSG);
|
||||
|
||||
// act
|
||||
viewModel.TiffCompressOption = 10;
|
||||
|
||||
// Assert
|
||||
viewModel = new ImageResizerViewModel(SendMockIPCConfigMSG);
|
||||
viewModel = new ImageResizerViewModel(mockSettingsUtils, SendMockIPCConfigMSG);
|
||||
Assert.AreEqual(10, viewModel.TiffCompressOption);
|
||||
}
|
||||
|
||||
@@ -118,15 +95,17 @@ namespace ViewModelTests
|
||||
public void FileName_ShouldUpdateValue_WhenSuccessful()
|
||||
{
|
||||
// arrange
|
||||
var mockIOProvider = IIOProviderMocks.GetMockIOProviderForSaveLoadExists();
|
||||
var mockSettingsUtils = new SettingsUtils(mockIOProvider.Object);
|
||||
Func<string, int> SendMockIPCConfigMSG = msg => { return 0; };
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(SendMockIPCConfigMSG);
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils, SendMockIPCConfigMSG);
|
||||
string expectedValue = "%1 (%3)";
|
||||
|
||||
// act
|
||||
viewModel.FileName = expectedValue;
|
||||
|
||||
// Assert
|
||||
viewModel = new ImageResizerViewModel(SendMockIPCConfigMSG);
|
||||
viewModel = new ImageResizerViewModel(mockSettingsUtils, SendMockIPCConfigMSG);
|
||||
Assert.AreEqual(expectedValue, viewModel.FileName);
|
||||
}
|
||||
|
||||
@@ -134,29 +113,39 @@ namespace ViewModelTests
|
||||
public void KeepDateModified_ShouldUpdateValue_WhenSuccessful()
|
||||
{
|
||||
// arrange
|
||||
var settingUtils = ISettingsUtilsMocks.GetStubSettingsUtils();
|
||||
|
||||
var expectedSettingsString = new ImageResizerSettings() { Properties = new ImageResizerProperties() { ImageresizerKeepDateModified = new BoolProperty() { Value = true } } }.ToJsonString();
|
||||
settingUtils.Setup(x => x.SaveSettings(
|
||||
It.Is<string>(content => content.Equals(expectedSettingsString, StringComparison.Ordinal)),
|
||||
It.Is<string>(module => module.Equals(Module, StringComparison.Ordinal)),
|
||||
It.IsAny<string>()))
|
||||
.Verifiable();
|
||||
|
||||
Func<string, int> SendMockIPCConfigMSG = msg => { return 0; };
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(SendMockIPCConfigMSG);
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(settingUtils.Object, SendMockIPCConfigMSG);
|
||||
|
||||
// act
|
||||
viewModel.KeepDateModified = true;
|
||||
|
||||
// Assert
|
||||
ImageResizerSettings settings = SettingsUtils.GetSettings<ImageResizerSettings>(Module);
|
||||
Assert.AreEqual(true, settings.Properties.ImageresizerKeepDateModified.Value);
|
||||
settingUtils.Verify();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Encoder_ShouldUpdateValue_WhenSuccessful()
|
||||
{
|
||||
// arrange
|
||||
var mockIOProvider = IIOProviderMocks.GetMockIOProviderForSaveLoadExists();
|
||||
var mockSettingsUtils = new SettingsUtils(mockIOProvider.Object);
|
||||
Func<string, int> SendMockIPCConfigMSG = msg => { return 0; };
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(SendMockIPCConfigMSG);
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils, SendMockIPCConfigMSG);
|
||||
|
||||
// act
|
||||
viewModel.Encoder = 3;
|
||||
|
||||
// Assert
|
||||
viewModel = new ImageResizerViewModel(SendMockIPCConfigMSG);
|
||||
viewModel = new ImageResizerViewModel(mockSettingsUtils, SendMockIPCConfigMSG);
|
||||
Assert.AreEqual("163bcc30-e2e9-4f0b-961d-a3e9fdb788a3", viewModel.GetEncoderGuid(viewModel.Encoder));
|
||||
Assert.AreEqual(3, viewModel.Encoder);
|
||||
}
|
||||
@@ -165,8 +154,9 @@ namespace ViewModelTests
|
||||
public void AddRow_ShouldAddEmptyImageSize_WhenSuccessful()
|
||||
{
|
||||
// arrange
|
||||
var mockSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils();
|
||||
Func<string, int> SendMockIPCConfigMSG = msg => { return 0; };
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(SendMockIPCConfigMSG);
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils.Object, SendMockIPCConfigMSG);
|
||||
int sizeOfOriginalArray = viewModel.Sizes.Count;
|
||||
|
||||
// act
|
||||
@@ -180,8 +170,9 @@ namespace ViewModelTests
|
||||
public void DeleteImageSize_ShouldDeleteImageSize_WhenSuccessful()
|
||||
{
|
||||
// arrange
|
||||
var mockSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils();
|
||||
Func<string, int> SendMockIPCConfigMSG = msg => { return 0; };
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(SendMockIPCConfigMSG);
|
||||
ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils.Object, SendMockIPCConfigMSG);
|
||||
int sizeOfOriginalArray = viewModel.Sizes.Count;
|
||||
ImageSize deleteCandidate = viewModel.Sizes.Where<ImageSize>(x => x.Id == 0).First();
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace ViewModelTests
|
||||
{
|
||||
@@ -24,13 +25,11 @@ namespace ViewModelTests
|
||||
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));
|
||||
|
||||
@@ -7,7 +7,9 @@ using System.IO;
|
||||
using System.Text.Json;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace ViewModelTests
|
||||
{
|
||||
@@ -16,38 +18,6 @@ namespace ViewModelTests
|
||||
{
|
||||
public const string Module = "Test\\File Explorer";
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
// initialize creation of test settings file.
|
||||
GeneralSettings generalSettings = new GeneralSettings();
|
||||
PowerPreviewSettings powerpreview = new PowerPreviewSettings();
|
||||
|
||||
SettingsUtils.SaveSettings(generalSettings.ToJsonString());
|
||||
SettingsUtils.SaveSettings(powerpreview.ToJsonString(), powerpreview.Name);
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void CleanUp()
|
||||
{
|
||||
// delete folder created.
|
||||
string generalSettings_file_name = string.Empty;
|
||||
if (SettingsUtils.SettingsFolderExists(generalSettings_file_name))
|
||||
{
|
||||
DeleteFolder(generalSettings_file_name);
|
||||
}
|
||||
|
||||
if (SettingsUtils.SettingsFolderExists(Module))
|
||||
{
|
||||
DeleteFolder(Module);
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteFolder(string powertoy)
|
||||
{
|
||||
Directory.Delete(Path.Combine(SettingsUtils.LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"), true);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SVGRenderIsEnabled_ShouldPrevHandler_WhenSuccessful()
|
||||
{
|
||||
@@ -60,7 +30,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
PowerPreviewViewModel viewModel = new PowerPreviewViewModel(SendMockIPCConfigMSG, Module);
|
||||
PowerPreviewViewModel viewModel = new PowerPreviewViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, Module);
|
||||
|
||||
// act
|
||||
viewModel.SVGRenderIsEnabled = true;
|
||||
@@ -78,7 +48,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
PowerPreviewViewModel viewModel = new PowerPreviewViewModel(SendMockIPCConfigMSG, Module);
|
||||
PowerPreviewViewModel viewModel = new PowerPreviewViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, Module);
|
||||
|
||||
// act
|
||||
viewModel.SVGThumbnailIsEnabled = true;
|
||||
@@ -96,7 +66,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
PowerPreviewViewModel viewModel = new PowerPreviewViewModel(SendMockIPCConfigMSG, Module);;
|
||||
PowerPreviewViewModel viewModel = new PowerPreviewViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, Module);;
|
||||
|
||||
// act
|
||||
viewModel.MDRenderIsEnabled = true;
|
||||
|
||||
@@ -7,7 +7,9 @@ using System.IO;
|
||||
using System.Text.Json;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace ViewModelTests
|
||||
{
|
||||
@@ -16,26 +18,6 @@ namespace ViewModelTests
|
||||
{
|
||||
public const string ModuleName = "PowerRename";
|
||||
public const string generalSettings_file_name = "Test\\PowerRename";
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
// initialize creation of test settings file.
|
||||
GeneralSettings generalSettings = new GeneralSettings();
|
||||
PowerRenameSettings powerRename = new PowerRenameSettings();
|
||||
|
||||
SettingsUtils.SaveSettings(generalSettings.ToJsonString());
|
||||
SettingsUtils.SaveSettings(powerRename.ToJsonString(), generalSettings_file_name, "power-rename-settings.json");
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void CleanUp()
|
||||
{
|
||||
// delete folder created.
|
||||
if (SettingsUtils.SettingsFolderExists(generalSettings_file_name))
|
||||
{
|
||||
DeleteFolder(generalSettings_file_name);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsEnabled_ShouldEnableModule_WhenSuccessful()
|
||||
@@ -49,7 +31,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
|
||||
// act
|
||||
viewModel.IsEnabled = true;
|
||||
@@ -67,7 +49,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
|
||||
// act
|
||||
viewModel.MRUEnabled = true;
|
||||
@@ -77,7 +59,7 @@ namespace ViewModelTests
|
||||
public void WhenIsEnabledIsOffAndMRUEnabledIsOffGlobalAndMruShouldBeOff()
|
||||
{
|
||||
Func<string, int> SendMockIPCConfigMSG = msg => { return 0; };
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
|
||||
viewModel.IsEnabled = false;
|
||||
viewModel.MRUEnabled = false;
|
||||
@@ -89,7 +71,7 @@ namespace ViewModelTests
|
||||
public void WhenIsEnabledIsOffAndMRUEnabledIsOnGlobalAndMruShouldBeOff()
|
||||
{
|
||||
Func<string, int> SendMockIPCConfigMSG = msg => { return 0; };
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
|
||||
viewModel.IsEnabled = false;
|
||||
viewModel.MRUEnabled = true;
|
||||
@@ -101,7 +83,7 @@ namespace ViewModelTests
|
||||
public void WhenIsEnabledIsOnAndMRUEnabledIsOffGlobalAndMruShouldBeOff()
|
||||
{
|
||||
Func<string, int> SendMockIPCConfigMSG = msg => { return 0; };
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
|
||||
viewModel.IsEnabled = true;
|
||||
viewModel.MRUEnabled = false;
|
||||
@@ -113,7 +95,7 @@ namespace ViewModelTests
|
||||
public void WhenIsEnabledIsOnAndMRUEnabledIsOnGlobalAndMruShouldBeOn()
|
||||
{
|
||||
Func<string, int> SendMockIPCConfigMSG = msg => { return 0; };
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
|
||||
viewModel.IsEnabled = true;
|
||||
viewModel.MRUEnabled = true;
|
||||
@@ -133,7 +115,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
|
||||
// act
|
||||
viewModel.EnabledOnContextMenu = true;
|
||||
@@ -151,7 +133,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
|
||||
// act
|
||||
viewModel.EnabledOnContextMenu = true;
|
||||
@@ -169,7 +151,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
|
||||
// act
|
||||
viewModel.RestoreFlagsOnLaunch = true;
|
||||
@@ -187,15 +169,10 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// arrange
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
PowerRenameViewModel viewModel = new PowerRenameViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, generalSettings_file_name);
|
||||
|
||||
// act
|
||||
viewModel.MaxDispListNum = 20;
|
||||
}
|
||||
|
||||
public void DeleteFolder(string powertoy)
|
||||
{
|
||||
Directory.Delete(Path.Combine(SettingsUtils.LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,9 @@ using System.IO;
|
||||
using System.Text.Json;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
|
||||
using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace ViewModelTests
|
||||
{
|
||||
@@ -16,42 +18,6 @@ namespace ViewModelTests
|
||||
{
|
||||
public const string ShortCutGuideTestFolderName = "Test\\ShortCutGuide";
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
// initialize creation of test settings file.
|
||||
// Test base path:
|
||||
// C:\Users\<user name>\AppData\Local\Packages\08e1807b-8b6d-4bfa-adc4-79c64aae8e78_9abkseg265h2m\LocalState\Microsoft\PowerToys\
|
||||
GeneralSettings generalSettings = new GeneralSettings();
|
||||
ShortcutGuideSettings shortcutGuide = new ShortcutGuideSettings();
|
||||
|
||||
SettingsUtils.SaveSettings(generalSettings.ToJsonString());
|
||||
SettingsUtils.SaveSettings(shortcutGuide.ToJsonString(), ShortCutGuideTestFolderName);
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void CleanUp()
|
||||
{
|
||||
// delete folder created.
|
||||
// delete general settings folder.
|
||||
string ShortCutGuideTestFolderName = string.Empty;
|
||||
if (SettingsUtils.SettingsFolderExists(string.Empty))
|
||||
{
|
||||
DeleteFolder(string.Empty);
|
||||
}
|
||||
|
||||
// delete power rename folder.
|
||||
if (SettingsUtils.SettingsFolderExists(ShortCutGuideTestFolderName))
|
||||
{
|
||||
DeleteFolder(ShortCutGuideTestFolderName);
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteFolder(string powertoy)
|
||||
{
|
||||
Directory.Delete(Path.Combine(SettingsUtils.LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"), true);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsEnabled_ShouldEnableModule_WhenSuccessful()
|
||||
{
|
||||
@@ -65,7 +31,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// Arrange
|
||||
ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(SendMockIPCConfigMSG, ShortCutGuideTestFolderName);
|
||||
ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, ShortCutGuideTestFolderName);
|
||||
|
||||
// Act
|
||||
viewModel.IsEnabled = true;
|
||||
@@ -84,7 +50,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// Arrange
|
||||
ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(SendMockIPCConfigMSG, ShortCutGuideTestFolderName);
|
||||
ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, ShortCutGuideTestFolderName);
|
||||
Assert.AreEqual(1, viewModel.ThemeIndex);
|
||||
|
||||
// Act
|
||||
@@ -104,7 +70,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// Arrange
|
||||
ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(SendMockIPCConfigMSG, ShortCutGuideTestFolderName);
|
||||
ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, ShortCutGuideTestFolderName);
|
||||
Assert.AreEqual(900, viewModel.PressTime);
|
||||
|
||||
// Act
|
||||
@@ -126,7 +92,7 @@ namespace ViewModelTests
|
||||
};
|
||||
|
||||
// Arrange
|
||||
ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(SendMockIPCConfigMSG, ShortCutGuideTestFolderName);
|
||||
ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, ShortCutGuideTestFolderName);
|
||||
Assert.AreEqual(90, viewModel.OverlayOpacity);
|
||||
|
||||
// Act
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
using System;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Data;
|
||||
using Windows.UI.Xaml.Media;
|
||||
@@ -12,10 +13,12 @@ namespace Microsoft.PowerToys.Settings.UI.Converters
|
||||
{
|
||||
public sealed class ModuleEnabledToForegroundConverter : IValueConverter
|
||||
{
|
||||
private readonly ISettingsUtils settingsUtils = new SettingsUtils(new SystemIOProvider());
|
||||
|
||||
public object Convert(object value, Type targetType, object parameter, string language)
|
||||
{
|
||||
bool isEnabled = (bool)value;
|
||||
GeneralSettings generalSettings = SettingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
GeneralSettings generalSettings = settingsUtils.GetSettings<GeneralSettings>(string.Empty);
|
||||
|
||||
var defaultTheme = new Windows.UI.ViewManagement.UISettings();
|
||||
var uiTheme = defaultTheme.GetColorValue(Windows.UI.ViewManagement.UIColorType.Background).ToString();
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
|
||||
@@ -13,7 +15,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
|
||||
public ColorPickerPage()
|
||||
{
|
||||
ViewModel = new ColorPickerViewModel(ShellPage.SendDefaultIPCMessage);
|
||||
var settingsUtils = new SettingsUtils(new SystemIOProvider());
|
||||
ViewModel = new ColorPickerViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage);
|
||||
DataContext = ViewModel;
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
|
||||
@@ -14,7 +16,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
public FancyZonesPage()
|
||||
{
|
||||
InitializeComponent();
|
||||
ViewModel = new FancyZonesViewModel(ShellPage.SendDefaultIPCMessage);
|
||||
var settingsUtils = new SettingsUtils(new SystemIOProvider());
|
||||
ViewModel = new FancyZonesViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage);
|
||||
DataContext = ViewModel;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
|
||||
using Windows.ApplicationModel.Resources;
|
||||
using Windows.Data.Json;
|
||||
@@ -31,8 +33,10 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
|
||||
// Load string resources
|
||||
ResourceLoader loader = ResourceLoader.GetForViewIndependentUse();
|
||||
var settingsUtils = new SettingsUtils(new SystemIOProvider());
|
||||
|
||||
ViewModel = new GeneralViewModel(
|
||||
settingsUtils,
|
||||
loader.GetString("GeneralSettings_RunningAsAdminText"),
|
||||
loader.GetString("GeneralSettings_RunningAsUserText"),
|
||||
ShellPage.IsElevated,
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
@@ -16,7 +18,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
ViewModel = new ImageResizerViewModel(ShellPage.SendDefaultIPCMessage);
|
||||
var settingsUtils = new SettingsUtils(new SystemIOProvider());
|
||||
ViewModel = new ImageResizerViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage);
|
||||
DataContext = ViewModel;
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
{
|
||||
dispatcher = Window.Current.Dispatcher;
|
||||
|
||||
ViewModel = new KeyboardManagerViewModel(ShellPage.SendDefaultIPCMessage, FilterRemapKeysList);
|
||||
var settingsUtils = new SettingsUtils(new SystemIOProvider());
|
||||
ViewModel = new KeyboardManagerViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage, FilterRemapKeysList);
|
||||
|
||||
watcher = Helper.GetFileWatcher(
|
||||
PowerToyName,
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
|
||||
@@ -19,7 +21,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
public PowerLauncherPage()
|
||||
{
|
||||
InitializeComponent();
|
||||
ViewModel = new PowerLauncherViewModel(ShellPage.SendDefaultIPCMessage, (int)Windows.System.VirtualKey.Space);
|
||||
var settingsUtils = new SettingsUtils(new SystemIOProvider());
|
||||
ViewModel = new PowerLauncherViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage, (int)Windows.System.VirtualKey.Space);
|
||||
DataContext = ViewModel;
|
||||
|
||||
var loader = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView();
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
|
||||
@@ -17,7 +19,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
public PowerPreviewPage()
|
||||
{
|
||||
InitializeComponent();
|
||||
ViewModel = new PowerPreviewViewModel(ShellPage.SendDefaultIPCMessage);
|
||||
var settingsUtils = new SettingsUtils(new SystemIOProvider());
|
||||
ViewModel = new PowerPreviewViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage);
|
||||
DataContext = ViewModel;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
|
||||
@@ -14,7 +16,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
public PowerRenamePage()
|
||||
{
|
||||
InitializeComponent();
|
||||
ViewModel = new PowerRenameViewModel(ShellPage.SendDefaultIPCMessage);
|
||||
var settingsUtils = new SettingsUtils(new SystemIOProvider());
|
||||
ViewModel = new PowerRenameViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage);
|
||||
|
||||
DataContext = ViewModel;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
|
||||
@@ -14,8 +16,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
public ShortcutGuidePage()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
ViewModel = new ShortcutGuideViewModel(ShellPage.SendDefaultIPCMessage);
|
||||
var settingsUtils = new SettingsUtils(new SystemIOProvider());
|
||||
ViewModel = new ShortcutGuideViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage);
|
||||
DataContext = ViewModel;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ namespace ColorPicker.Settings
|
||||
[Export(typeof(IUserSettings))]
|
||||
public class UserSettings : IUserSettings
|
||||
{
|
||||
private readonly ISettingsUtils _settingsUtils;
|
||||
private const string ColorPickerModuleName = "ColorPicker";
|
||||
private const string DefaultActivationShortcut = "Ctrl + Break";
|
||||
private const int MaxNumberOfRetry = 5;
|
||||
@@ -27,6 +28,7 @@ namespace ColorPicker.Settings
|
||||
[ImportingConstructor]
|
||||
public UserSettings()
|
||||
{
|
||||
_settingsUtils = new SettingsUtils(new SystemIOProvider());
|
||||
ChangeCursor = new SettingItem<bool>(true);
|
||||
ActivationShortcut = new SettingItem<string>(DefaultActivationShortcut);
|
||||
CopiedColorRepresentation = new SettingItem<ColorRepresentationType>(ColorRepresentationType.HEX);
|
||||
@@ -56,14 +58,14 @@ namespace ColorPicker.Settings
|
||||
{
|
||||
retryCount++;
|
||||
|
||||
if (!SettingsUtils.SettingsExists(ColorPickerModuleName))
|
||||
if (!_settingsUtils.SettingsExists(ColorPickerModuleName))
|
||||
{
|
||||
Logger.LogInfo("ColorPicker settings.json was missing, creating a new one");
|
||||
var defaultColorPickerSettings = new ColorPickerSettings();
|
||||
defaultColorPickerSettings.Save();
|
||||
defaultColorPickerSettings.Save(_settingsUtils);
|
||||
}
|
||||
|
||||
var settings = SettingsUtils.GetSettings<ColorPickerSettings>(ColorPickerModuleName);
|
||||
var settings = _settingsUtils.GetSettings<ColorPickerSettings>(ColorPickerModuleName);
|
||||
if (settings != null)
|
||||
{
|
||||
ChangeCursor.Value = settings.Properties.ChangeCursor;
|
||||
|
||||
@@ -7,6 +7,7 @@ using System.IO;
|
||||
using System.Threading;
|
||||
using System.Windows.Input;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib;
|
||||
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
||||
using PowerLauncher.Helper;
|
||||
using Wox.Core.Plugin;
|
||||
using Wox.Infrastructure.Hotkey;
|
||||
@@ -20,6 +21,8 @@ namespace PowerLauncher
|
||||
// Watch for /Local/Microsoft/PowerToys/Launcher/Settings.json changes
|
||||
public class SettingsWatcher : BaseModel
|
||||
{
|
||||
private readonly ISettingsUtils _settingsUtils;
|
||||
|
||||
private const int MaxRetries = 10;
|
||||
private static readonly object _watcherSyncObject = new object();
|
||||
private readonly FileSystemWatcher _watcher;
|
||||
@@ -27,6 +30,7 @@ namespace PowerLauncher
|
||||
|
||||
public SettingsWatcher(Settings settings)
|
||||
{
|
||||
_settingsUtils = new SettingsUtils(new SystemIOProvider());
|
||||
_settings = settings;
|
||||
|
||||
// Set up watcher
|
||||
@@ -36,13 +40,14 @@ namespace PowerLauncher
|
||||
OverloadSettings();
|
||||
}
|
||||
|
||||
public static void CreateSettingsIfNotExists()
|
||||
public void CreateSettingsIfNotExists()
|
||||
{
|
||||
if (!SettingsUtils.SettingsExists(PowerLauncherSettings.ModuleName))
|
||||
if (!_settingsUtils.SettingsExists(PowerLauncherSettings.ModuleName))
|
||||
{
|
||||
Log.Info("|SettingsWatcher.OverloadSettings|PT Run settings.json was missing, creating a new one");
|
||||
|
||||
var defaultSettings = new PowerLauncherSettings();
|
||||
defaultSettings.Save();
|
||||
defaultSettings.Save(_settingsUtils);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +63,7 @@ namespace PowerLauncher
|
||||
retryCount++;
|
||||
CreateSettingsIfNotExists();
|
||||
|
||||
var overloadSettings = SettingsUtils.GetSettings<PowerLauncherSettings>(PowerLauncherSettings.ModuleName);
|
||||
var overloadSettings = _settingsUtils.GetSettings<PowerLauncherSettings>(PowerLauncherSettings.ModuleName);
|
||||
|
||||
var openPowerlauncher = ConvertHotkey(overloadSettings.Properties.OpenPowerLauncher);
|
||||
if (_settings.Hotkey != openPowerlauncher)
|
||||
@@ -121,7 +126,7 @@ namespace PowerLauncher
|
||||
|
||||
// Settings.json could possibly be corrupted. To mitigate this we delete the
|
||||
// current file and replace it with a correct json value.
|
||||
SettingsUtils.DeleteSettings(PowerLauncherSettings.ModuleName);
|
||||
_settingsUtils.DeleteSettings(PowerLauncherSettings.ModuleName);
|
||||
CreateSettingsIfNotExists();
|
||||
ErrorReporting.ShowMessageBox(Properties.Resources.deseralization_error_title, Properties.Resources.deseralization_error_message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user