[Settings Tests] Migrate General Settings tests (#5753)

* added MSTest project

* migrated general settings tests

* enabled settings tests run in the build pipeline

* added tests

* move relay command class to separate file

* added a foldername parameter for general settings view model
This commit is contained in:
Nkateko
2020-08-13 15:02:05 -07:00
committed by GitHub
parent e4ea8d2abd
commit 24d7232559
14 changed files with 725 additions and 83 deletions

View File

@@ -0,0 +1,27 @@
// 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.ComponentModel;
using System.Runtime.CompilerServices;
namespace Microsoft.PowerToys.Settings.UI.Lib.Helpers
{
public class Observable : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Equals(storage, value))
{
return;
}
storage = value;
OnPropertyChanged(propertyName);
}
protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

View File

@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Windows.Input;
namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels.Commands
{
public class ButtonClickCommand : ICommand
{
private readonly Action _execute;
public ButtonClickCommand(Action execute)
{
this._execute = execute;
}
// Occurs when changes occur that affect whether or not the command should execute.
public event EventHandler CanExecuteChanged;
// Defines the method that determines whether the command can execute in its current state.
public bool CanExecute(object parameter)
{
return true;
}
// Defines the method to be called when the command is invoked.
public void Execute(object parameter)
{
_execute();
}
}
}

View File

@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Windows.Input;
namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels.Commands
{
public class RelayCommand : ICommand
{
private readonly Action _execute;
private readonly Func<bool> _canExecute;
public event EventHandler CanExecuteChanged;
public RelayCommand(Action execute)
: this(execute, null)
{
}
public RelayCommand(Action execute, Func<bool> canExecute)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
public bool CanExecute(object parameter) => _canExecute == null || _canExecute();
public void Execute(object parameter) => _execute();
public void OnCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
}

View File

@@ -0,0 +1,36 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Windows.Input;
namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels.Commands
{
public class RelayCommand<T> : ICommand
{
private readonly Action<T> execute;
private readonly Func<T, bool> canExecute;
public event EventHandler CanExecuteChanged;
public RelayCommand(Action<T> execute)
: this(execute, null)
{
}
public RelayCommand(Action<T> execute, Func<T, bool> canExecute)
{
this.execute = execute ?? throw new ArgumentNullException(nameof(execute));
this.canExecute = canExecute;
}
public bool CanExecute(object parameter) => canExecute == null || canExecute((T)parameter);
public void Execute(object parameter) => execute((T)parameter);
public void OnCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
}

View File

@@ -0,0 +1,366 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text.Json;
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 GeneralViewModel : Observable
{
private GeneralSettings GeneralSettingsConfigs { get; set; }
public ButtonClickCommand CheckFoUpdatesEventHandler { get; set; }
public ButtonClickCommand RestartElevatedButtonEventHandler { get; set; }
public Func<string, int> UpdateUIThemeCallBack { get; }
public Func<string, int> SendConfigMSG { get; }
public Func<string, int> SendRestartAsAdminConfigMSG { get; }
public Func<string, int> SendCheckForUpdatesConfigMSG { get; }
public readonly string RunningAsUserDefaultText;
public readonly string RunningAsAdminDefaultText;
public 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 = "")
{
this.CheckFoUpdatesEventHandler = new ButtonClickCommand(CheckForUpdates_Click);
this.RestartElevatedButtonEventHandler = new ButtonClickCommand(Restart_Elevated);
try
{
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);
}
}
catch (FormatException e)
{
// If there is an issue with the version number format, don't migrate settings.
Debug.WriteLine(e.Message);
}
catch
{
GeneralSettingsConfigs = new GeneralSettings();
SettingsUtils.SaveSettings(GeneralSettingsConfigs.ToJsonString(), string.Empty);
}
// set the callback functions value to hangle outgoing IPC message.
SendConfigMSG = ipcMSGCallBackFunc;
SendCheckForUpdatesConfigMSG = ipcMSGCheckForUpdatesCallBackFunc;
SendRestartAsAdminConfigMSG = ipcMSGRestartAsAdminMSGCallBackFunc;
// set the callback function value to update the UI theme.
UpdateUIThemeCallBack = updateTheme;
UpdateUIThemeCallBack(GeneralSettingsConfigs.Theme.ToLower());
// Update Settings file folder:
SettingsConfigFileFolder = configFileSubfolder;
switch (GeneralSettingsConfigs.Theme.ToLower())
{
case "light":
_isLightThemeRadioButtonChecked = true;
break;
case "dark":
_isDarkThemeRadioButtonChecked = true;
break;
case "system":
_isSystemThemeRadioButtonChecked = true;
break;
}
_startup = GeneralSettingsConfigs.Startup;
_autoDownloadUpdates = GeneralSettingsConfigs.AutoDownloadUpdates;
_isElevated = isElevated;
_runElevated = GeneralSettingsConfigs.RunElevated;
RunningAsUserDefaultText = runAsUserText;
RunningAsAdminDefaultText = runAsAdminText;
_isAdmin = isAdmin;
}
private bool _packaged = false;
private bool _startup = false;
private bool _isElevated = false;
private bool _runElevated = false;
private bool _isAdmin = false;
private bool _isDarkThemeRadioButtonChecked = false;
private bool _isLightThemeRadioButtonChecked = false;
private bool _isSystemThemeRadioButtonChecked = false;
private bool _autoDownloadUpdates = false;
// Gets or sets a value indicating whether packaged.
public bool Packaged
{
get
{
return _packaged;
}
set
{
if (_packaged != value)
{
_packaged = value;
RaisePropertyChanged();
}
}
}
// Gets or sets a value indicating whether run powertoys on start-up.
public bool Startup
{
get
{
return _startup;
}
set
{
if (_startup != value)
{
_startup = value;
GeneralSettingsConfigs.Startup = value;
RaisePropertyChanged();
}
}
}
public string RunningAsText
{
get
{
if (!IsElevated)
{
return RunningAsUserDefaultText;
}
else
{
return RunningAsAdminDefaultText;
}
}
set
{
OnPropertyChanged("RunningAsAdminText");
}
}
// Gets or sets a value indicating whether the powertoy elevated.
public bool IsElevated
{
get
{
return _isElevated;
}
set
{
if (_isElevated != value)
{
_isElevated = value;
OnPropertyChanged("IsElevated");
OnPropertyChanged("IsAdminButtonEnabled");
OnPropertyChanged("RunningAsAdminText");
}
}
}
public bool IsAdminButtonEnabled
{
get
{
return !IsElevated;
}
set
{
OnPropertyChanged("IsAdminButtonEnabled");
}
}
// Gets or sets a value indicating whether powertoys should run elevated.
public bool RunElevated
{
get
{
return _runElevated;
}
set
{
if (_runElevated != value)
{
_runElevated = value;
GeneralSettingsConfigs.RunElevated = value;
RaisePropertyChanged();
}
}
}
// Gets a value indicating whether the user is part of administrators group.
public bool IsAdmin
{
get
{
return _isAdmin;
}
}
public bool AutoDownloadUpdates
{
get
{
return _autoDownloadUpdates;
}
set
{
if (_autoDownloadUpdates != value)
{
_autoDownloadUpdates = value;
GeneralSettingsConfigs.AutoDownloadUpdates = value;
RaisePropertyChanged();
}
}
}
public bool IsDarkThemeRadioButtonChecked
{
get
{
return _isDarkThemeRadioButtonChecked;
}
set
{
if (value == true)
{
GeneralSettingsConfigs.Theme = "dark";
_isDarkThemeRadioButtonChecked = value;
try
{
UpdateUIThemeCallBack(GeneralSettingsConfigs.Theme);
}
catch
{
}
RaisePropertyChanged();
}
}
}
public bool IsLightThemeRadioButtonChecked
{
get
{
return _isLightThemeRadioButtonChecked;
}
set
{
if (value == true)
{
GeneralSettingsConfigs.Theme = "light";
_isLightThemeRadioButtonChecked = value;
try
{
UpdateUIThemeCallBack(GeneralSettingsConfigs.Theme);
}
catch
{
}
RaisePropertyChanged();
}
}
}
public bool IsSystemThemeRadioButtonChecked
{
get
{
return _isSystemThemeRadioButtonChecked;
}
set
{
if (value == true)
{
GeneralSettingsConfigs.Theme = "system";
_isSystemThemeRadioButtonChecked = value;
try
{
UpdateUIThemeCallBack(GeneralSettingsConfigs.Theme);
}
catch
{
}
RaisePropertyChanged();
}
}
}
public string PowerToysVersion
{
get
{
return Helper.GetProductVersion();
}
}
public void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
// Notify UI of property change
OnPropertyChanged(propertyName);
OutGoingGeneralSettings outsettings = new OutGoingGeneralSettings(GeneralSettingsConfigs);
SendConfigMSG(outsettings.ToString());
}
// callback function to launch the URL to check for updates.
private async void CheckForUpdates_Click()
{
GeneralSettings settings = SettingsUtils.GetSettings<GeneralSettings>(SettingsConfigFileFolder);
settings.CustomActionName = "check_for_updates";
OutGoingGeneralSettings outsettings = new OutGoingGeneralSettings(settings);
GeneralSettingsCustomAction customaction = new GeneralSettingsCustomAction(outsettings);
SendCheckForUpdatesConfigMSG(customaction.ToString());
}
public void Restart_Elevated()
{
GeneralSettings settings = SettingsUtils.GetSettings<GeneralSettings>(SettingsConfigFileFolder);
settings.CustomActionName = "restart_elevation";
OutGoingGeneralSettings outsettings = new OutGoingGeneralSettings(settings);
GeneralSettingsCustomAction customaction = new GeneralSettingsCustomAction(outsettings);
SendRestartAsAdminConfigMSG(customaction.ToString());
}
}
}