[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

@@ -110,7 +110,6 @@
<Compile Include="Services\NavigationService.cs" />
<Compile Include="ViewModels\ColorPickerViewModel.cs" />
<Compile Include="ViewModels\Commands\ButtonClickCommand.cs" />
<Compile Include="ViewModels\GeneralViewModel.cs" />
<Compile Include="ViewModels\FancyZonesViewModel.cs" />
<Compile Include="ViewModels\ImageResizerViewModel.cs" />
<Compile Include="ViewModels\KeyboardManagerViewModel.cs" />

View File

@@ -1,372 +0,0 @@
// 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 Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
using Microsoft.PowerToys.Settings.UI.ViewModels.Commands;
using Microsoft.PowerToys.Settings.UI.Views;
using Windows.ApplicationModel.Resources;
using Windows.UI.Xaml;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public class GeneralViewModel : Observable
{
private GeneralSettings GeneralSettingsConfigs { get; set; }
public ButtonClickCommand CheckFoUpdatesEventHandler { get; set; }
public ButtonClickCommand RestartElevatedButtonEventHandler { get; set; }
private ResourceLoader loader = ResourceLoader.GetForViewIndependentUse();
public string RunningAsUserDefaultText { get; private set; }
public string RunningAsAdminDefaultText { get; private set; }
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;
public GeneralViewModel()
{
CheckFoUpdatesEventHandler = new ButtonClickCommand(CheckForUpdates_Click);
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);
}
switch (GeneralSettingsConfigs.Theme.ToLower())
{
case "light":
_isLightThemeRadioButtonChecked = true;
try
{
ShellPage.ShellHandler.RequestedTheme = ElementTheme.Light;
}
catch
{
}
break;
case "dark":
_isDarkThemeRadioButtonChecked = true;
try
{
ShellPage.ShellHandler.RequestedTheme = ElementTheme.Dark;
}
catch
{
}
break;
case "system":
_isSystemThemeRadioButtonChecked = true;
try
{
ShellPage.ShellHandler.RequestedTheme = ElementTheme.Default;
}
catch
{
}
break;
}
_startup = GeneralSettingsConfigs.Startup;
_autoDownloadUpdates = GeneralSettingsConfigs.AutoDownloadUpdates;
_isElevated = ShellPage.IsElevated;
_runElevated = GeneralSettingsConfigs.RunElevated;
RunningAsUserDefaultText = loader.GetString("GeneralSettings_RunningAsUserText");
RunningAsAdminDefaultText = loader.GetString("GeneralSettings_RunningAsAdminText");
_isAdmin = ShellPage.IsUserAnAdmin;
}
// 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
{
ShellPage.ShellHandler.RequestedTheme = ElementTheme.Dark;
}
catch
{
}
RaisePropertyChanged();
}
}
}
public bool IsLightThemeRadioButtonChecked
{
get
{
return _isLightThemeRadioButtonChecked;
}
set
{
if (value == true)
{
GeneralSettingsConfigs.Theme = "light";
_isLightThemeRadioButtonChecked = value;
try
{
ShellPage.ShellHandler.RequestedTheme = ElementTheme.Light;
}
catch
{
}
RaisePropertyChanged();
}
}
}
public bool IsSystemThemeRadioButtonChecked
{
get
{
return _isSystemThemeRadioButtonChecked;
}
set
{
if (value == true)
{
GeneralSettingsConfigs.Theme = "system";
_isSystemThemeRadioButtonChecked = value;
try
{
ShellPage.ShellHandler.RequestedTheme = ElementTheme.Default;
}
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);
ShellPage.DefaultSndMSGCallback(outsettings.ToString());
}
// callback function to launch the URL to check for updates.
private void CheckForUpdates_Click()
{
GeneralSettings settings = SettingsUtils.GetSettings<GeneralSettings>(string.Empty);
settings.CustomActionName = "check_for_updates";
OutGoingGeneralSettings outsettings = new OutGoingGeneralSettings(settings);
GeneralSettingsCustomAction customaction = new GeneralSettingsCustomAction(outsettings);
ShellPage.CheckForUpdatesMsgCallback?.Invoke(customaction.ToString());
}
public void Restart_Elevated()
{
GeneralSettings settings = SettingsUtils.GetSettings<GeneralSettings>(string.Empty);
settings.CustomActionName = "restart_elevation";
OutGoingGeneralSettings outsettings = new OutGoingGeneralSettings(settings);
GeneralSettingsCustomAction customaction = new GeneralSettingsCustomAction(outsettings);
ShellPage.SndRestartAsAdminMsgCallback?.Invoke(customaction.ToString());
}
}
}

View File

@@ -3,7 +3,6 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Microsoft.PowerToys.Settings.UI.Views"
xmlns:viewModel="using:Microsoft.PowerToys.Settings.UI.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:converters="using:Microsoft.Toolkit.Uwp.UI.Converters"
@@ -14,7 +13,6 @@
<Page.Resources>
<converters:BoolToObjectConverter x:Key="BoolToVisibilityConverter" TrueValue="Collapsed" FalseValue="Visible"/>
<converters:BoolToVisibilityConverter x:Key="VisibleIfTrueConverter"/>
<viewModel:GeneralViewModel x:Key="eventViewModel"/>
</Page.Resources>
<Grid RowSpacing="{StaticResource DefaultRowSpacing}">
@@ -56,25 +54,25 @@
<TextBlock x:Uid="Admin_Mode" FontWeight="SemiBold"
Style="{StaticResource SubtitleTextBlockStyle}"/>
<TextBlock Text="{Binding Mode=TwoWay, Path=RunningAsText, Source={StaticResource eventViewModel}}"
<TextBlock Text="{x:Bind Mode=TwoWay, Path=ViewModel.RunningAsText}"
Margin="{StaticResource SmallTopMargin}"/>
<Button x:Uid="GeneralPage_RestartAsAdmin_Button"
Margin="{StaticResource SmallTopMargin}"
Style="{StaticResource AccentButtonStyle}"
Command = "{Binding RestartElevatedButtonEventHandler, Source={StaticResource eventViewModel}}"
IsEnabled="{Binding Mode=TwoWay, Path=IsAdminButtonEnabled, Source={StaticResource eventViewModel}}"
Command = "{Binding RestartElevatedButtonEventHandler}"
IsEnabled="{Binding Mode=TwoWay, Path=IsAdminButtonEnabled}"
/>
<TextBlock x:Uid="General_RunAsAdminRequired"
Foreground="{ThemeResource SystemControlErrorTextForegroundBrush}"
Visibility="{Binding Mode=TwoWay, Path=IsElevated, Source={StaticResource eventViewModel}, Converter={StaticResource BoolToVisibilityConverter}}"
Visibility="{Binding Mode=TwoWay, Path=IsElevated, Converter={StaticResource BoolToVisibilityConverter}}"
Margin="0,24,0,-8" />
<ToggleSwitch Margin="{StaticResource SmallTopMargin}"
x:Uid="GeneralSettings_AlwaysRunAsAdminText"
IsEnabled="{Binding Mode=TwoWay, Path=IsElevated, Source={StaticResource eventViewModel}}"
IsOn="{Binding Mode=TwoWay, Path=RunElevated, Source={StaticResource eventViewModel}}"/>
IsEnabled="{Binding Mode=TwoWay, Path=IsElevated}"
IsOn="{Binding Mode=TwoWay, Path=RunElevated}"/>
<HyperlinkButton NavigateUri="https://aka.ms/powertoysDetectedElevatedHelp">
<TextBlock x:Uid="GeneralPage_ToggleSwitch_AlwaysRunElevated_Link" />
@@ -88,20 +86,20 @@
Margin="{StaticResource SmallTopMargin}">
<RadioButton x:Uid="GeneralPage_Radio_Theme_Dark"
Content="Dark"
IsChecked="{ Binding Mode=TwoWay, Path=IsDarkThemeRadioButtonChecked, Source={StaticResource eventViewModel}}"/>
IsChecked="{ Binding Mode=TwoWay, Path=IsDarkThemeRadioButtonChecked}"/>
<RadioButton x:Uid="GeneralPage_Radio_Theme_Light"
Content="Light"
IsChecked="{ Binding Mode=TwoWay, Path=IsLightThemeRadioButtonChecked, Source={StaticResource eventViewModel}}"/>
IsChecked="{ Binding Mode=TwoWay, Path=IsLightThemeRadioButtonChecked}"/>
<RadioButton x:Uid="GeneralPage_Radio_Theme_Default"
Content="System default"
IsChecked="{ Binding Mode=TwoWay, Path=IsSystemThemeRadioButtonChecked, Source={StaticResource eventViewModel}}"/>
IsChecked="{ Binding Mode=TwoWay, Path=IsSystemThemeRadioButtonChecked}"/>
</muxc:RadioButtons>
<ToggleSwitch x:Uid="GeneralPage_ToggleSwitch_RunAtStartUp"
Margin="{StaticResource SmallTopMargin}"
IsOn="{Binding Mode=TwoWay, Path=Startup, Source={StaticResource eventViewModel}}"/>
IsOn="{Binding Mode=TwoWay, Path=Startup}"/>
@@ -109,7 +107,7 @@
Style="{StaticResource SettingsGroupTitleStyle}"/>
<StackPanel Orientation="Horizontal" Margin="{StaticResource SmallTopMargin}">
<TextBlock Text="Version: " x:Uid="General_Version" />
<HyperlinkButton NavigateUri="https://aka.ms/installpowertoys" Margin="4,-6,0,0">
<HyperlinkButton NavigateUri="https://github.com/microsoft/PowerToys/releases" Margin="4,-6,0,0">
<TextBlock Text="{x:Bind ViewModel.PowerToysVersion }" />
</HyperlinkButton>
</StackPanel>
@@ -118,13 +116,13 @@
<Button x:Uid="GeneralPage_CheckForUpdates"
Style="{StaticResource AccentButtonStyle}"
Foreground="White"
Command="{Binding CheckFoUpdatesEventHandler, Source={StaticResource eventViewModel}}"
Command="{Binding CheckFoUpdatesEventHandler}"
/>
<ToggleSwitch x:Uid="GeneralPage_ToggleSwitch_AutoDownloadUpdates"
Margin="{StaticResource MediumTopMargin}"
Visibility="{Binding Mode=TwoWay, Path=IsAdmin, Source={StaticResource eventViewModel}, Converter={StaticResource VisibleIfTrueConverter}}"
IsOn="{Binding Mode=TwoWay, Path=AutoDownloadUpdates, Source={StaticResource eventViewModel}}"/>
Visibility="{Binding Mode=TwoWay, Path=IsAdmin, Converter={StaticResource VisibleIfTrueConverter}}"
IsOn="{Binding Mode=TwoWay, Path=AutoDownloadUpdates}"/>
</StackPanel>
@@ -159,15 +157,15 @@
<TextBlock x:Uid="General_Repository"/>
</HyperlinkButton>
<HyperlinkButton NavigateUri="https://aka.ms/powerToysReportBug">
<HyperlinkButton NavigateUri="https://github.com/microsoft/PowerToys/issues">
<TextBlock x:Uid="GeneralPage_ReportAbug"/>
</HyperlinkButton>
<HyperlinkButton NavigateUri="https://aka.ms/powerToysRequestFeature">
<HyperlinkButton NavigateUri="https://github.com/microsoft/PowerToys/issues">
<TextBlock x:Uid="GeneralPage_RequestAFeature_URL"/>
</HyperlinkButton>
<HyperlinkButton NavigateUri=" https://go.microsoft.com/fwlink/?LinkId=521839">
<HyperlinkButton NavigateUri=" http://go.microsoft.com/fwlink/?LinkId=521839">
<TextBlock x:Uid="GeneralPage_PrivacyStatement_URL"/>
</HyperlinkButton>

View File

@@ -2,7 +2,9 @@
// 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.ViewModels;
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
using Windows.ApplicationModel.Resources;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace Microsoft.PowerToys.Settings.UI.Views
@@ -23,10 +25,40 @@ namespace Microsoft.PowerToys.Settings.UI.Views
/// </summary>
public GeneralPage()
{
InitializeComponent();
this.InitializeComponent();
ViewModel = new GeneralViewModel();
GeneralView.DataContext = ViewModel;
// Load string resources
ResourceLoader loader = ResourceLoader.GetForViewIndependentUse();
this.ViewModel = new GeneralViewModel(
loader.GetString("GeneralSettings_RunningAsAdminText"),
loader.GetString("GeneralSettings_RunningAsUserText"),
ShellPage.IsElevated,
ShellPage.IsUserAnAdmin,
UpdateUIThemeMethod,
ShellPage.SendDefaultIPCMessage,
ShellPage.SendRestartAdminIPCMessage,
ShellPage.SendCheckForUpdatesIPCMessage);
DataContext = ViewModel;
}
public int UpdateUIThemeMethod(string themeName)
{
switch (themeName)
{
case "light":
ShellPage.ShellHandler.RequestedTheme = ElementTheme.Light;
break;
case "dark":
ShellPage.ShellHandler.RequestedTheme = ElementTheme.Dark;
break;
case "system":
ShellPage.ShellHandler.RequestedTheme = ElementTheme.Default;
break;
}
return 0;
}
}
}

View File

@@ -61,6 +61,24 @@ namespace Microsoft.PowerToys.Settings.UI.Views
shellFrame.Navigate(typeof(GeneralPage));
}
public static int SendDefaultIPCMessage(string msg)
{
DefaultSndMSGCallback(msg);
return 0;
}
public static int SendCheckForUpdatesIPCMessage(string msg)
{
CheckForUpdatesMsgCallback(msg);
return 0;
}
public static int SendRestartAdminIPCMessage(string msg)
{
SndRestartAsAdminMsgCallback(msg);
return 0;
}
/// <summary>
/// Set Default IPC Message callback function.
/// </summary>