[Settings] Migrated Image Resizer Tests (#5765)

* added MSTest project

* Migrated Image Resizer Tests

* fixed links

* removed exception
This commit is contained in:
Nkateko
2020-08-18 13:43:58 -07:00
committed by GitHub
parent 8c98c7df29
commit 5e13152c73
5 changed files with 318 additions and 82 deletions

View File

@@ -110,7 +110,6 @@
<Compile Include="Services\NavigationService.cs" />
<Compile Include="ViewModels\Commands\ButtonClickCommand.cs" />
<Compile Include="ViewModels\FancyZonesViewModel.cs" />
<Compile Include="ViewModels\ImageResizerViewModel.cs" />
<Compile Include="ViewModels\PowerLauncherViewModel.cs" />
<Compile Include="ViewModels\ShellViewModel.cs" />
<Compile Include="Views\ColorPickerPage.xaml.cs">

View File

@@ -1,368 +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.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Input;
using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.Views;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public class ImageResizerViewModel : Observable
{
private ImageResizerSettings Settings { get; set; }
private const string ModuleName = "ImageResizer";
public ImageResizerViewModel()
{
try
{
Settings = SettingsUtils.GetSettings<ImageResizerSettings>(ModuleName);
}
catch
{
Settings = new ImageResizerSettings();
SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
}
GeneralSettings generalSettings;
try
{
generalSettings = SettingsUtils.GetSettings<GeneralSettings>(string.Empty);
}
catch
{
generalSettings = new GeneralSettings();
SettingsUtils.SaveSettings(generalSettings.ToJsonString(), string.Empty);
}
_isEnabled = generalSettings.Enabled.ImageResizer;
_advancedSizes = Settings.Properties.ImageresizerSizes.Value;
_jpegQualityLevel = Settings.Properties.ImageresizerJpegQualityLevel.Value;
_pngInterlaceOption = Settings.Properties.ImageresizerPngInterlaceOption.Value;
_tiffCompressOption = Settings.Properties.ImageresizerTiffCompressOption.Value;
_fileName = Settings.Properties.ImageresizerFileName.Value;
_keepDateModified = Settings.Properties.ImageresizerKeepDateModified.Value;
_encoderGuidId = GetEncoderIndex(Settings.Properties.ImageresizerFallbackEncoder.Value);
int i = 0;
foreach (ImageSize size in _advancedSizes)
{
size.Id = i;
i++;
size.PropertyChanged += Size_PropertyChanged;
}
}
private bool _isEnabled = false;
private ObservableCollection<ImageSize> _advancedSizes = new ObservableCollection<ImageSize>();
private int _jpegQualityLevel = 0;
private int _pngInterlaceOption;
private int _tiffCompressOption;
private string _fileName;
private bool _keepDateModified;
private int _encoderGuidId = 0;
public bool IsEnabled
{
get
{
return _isEnabled;
}
set
{
if (value != _isEnabled)
{
_isEnabled = value;
GeneralSettings generalSettings = SettingsUtils.GetSettings<GeneralSettings>(string.Empty);
generalSettings.Enabled.ImageResizer = value;
OutGoingGeneralSettings snd = new OutGoingGeneralSettings(generalSettings);
ShellPage.DefaultSndMSGCallback(snd.ToString());
OnPropertyChanged("IsEnabled");
}
}
}
public ObservableCollection<ImageSize> Sizes
{
get
{
return _advancedSizes;
}
set
{
SettingsUtils.SaveSettings(Settings.Properties.ImageresizerSizes.ToJsonString(), ModuleName, "sizes.json");
_advancedSizes = value;
Settings.Properties.ImageresizerSizes = new ImageResizerSizes(value);
SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
OnPropertyChanged("Sizes");
}
}
public int JPEGQualityLevel
{
get
{
return _jpegQualityLevel;
}
set
{
if (_jpegQualityLevel != value)
{
_jpegQualityLevel = value;
Settings.Properties.ImageresizerJpegQualityLevel.Value = value;
SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
OnPropertyChanged("JPEGQualityLevel");
}
}
}
public int PngInterlaceOption
{
get
{
return _pngInterlaceOption;
}
set
{
if (_pngInterlaceOption != value)
{
_pngInterlaceOption = value;
Settings.Properties.ImageresizerPngInterlaceOption.Value = value;
SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
OnPropertyChanged("PngInterlaceOption");
}
}
}
public int TiffCompressOption
{
get
{
return _tiffCompressOption;
}
set
{
if (_tiffCompressOption != value)
{
_tiffCompressOption = value;
Settings.Properties.ImageresizerTiffCompressOption.Value = value;
SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
OnPropertyChanged("TiffCompressOption");
}
}
}
public string FileName
{
get
{
return _fileName;
}
set
{
if (!string.IsNullOrWhiteSpace(value))
{
_fileName = value;
Settings.Properties.ImageresizerFileName.Value = value;
SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
OnPropertyChanged("FileName");
}
}
}
public bool KeepDateModified
{
get
{
return _keepDateModified;
}
set
{
_keepDateModified = value;
Settings.Properties.ImageresizerKeepDateModified.Value = value;
SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
OnPropertyChanged("KeepDateModified");
}
}
public int Encoder
{
get
{
return _encoderGuidId;
}
set
{
if (_encoderGuidId != value)
{
_encoderGuidId = value;
SettingsUtils.SaveSettings(Settings.Properties.ImageresizerSizes.ToJsonString(), ModuleName, "sizes.json");
Settings.Properties.ImageresizerFallbackEncoder.Value = GetEncoderGuid(value);
SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
OnPropertyChanged("Encoder");
}
}
}
public ICommand SaveSizesEventHandler
{
get
{
return new RelayCommand(SavesImageSizes);
}
}
public ICommand DeleteImageSizeEventHandler
{
get
{
return new RelayCommand<int>(DeleteImageSize);
}
}
public ICommand AddImageSizeEventHandler
{
get
{
return new RelayCommand(AddRow);
}
}
public void AddRow()
{
ObservableCollection<ImageSize> imageSizes = Sizes;
int maxId = imageSizes.Count > 0 ? imageSizes.OrderBy(x => x.Id).Last().Id : -1;
ImageSize newSize = new ImageSize(maxId + 1);
newSize.PropertyChanged += Size_PropertyChanged;
imageSizes.Add(newSize);
Sizes = imageSizes;
OnPropertyChanged("Sizes");
}
public void DeleteImageSize(int id)
{
try
{
ImageSize size = Sizes.Where<ImageSize>(x => x.Id == id).First();
ObservableCollection<ImageSize> imageSizes = Sizes;
imageSizes.Remove(size);
Sizes = imageSizes;
OnPropertyChanged("Sizes");
}
catch
{
}
}
public void SavesImageSizes()
{
SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
}
public string GetEncoderGuid(int value)
{
// PNG Encoder guid
if (value == 0)
{
return "1b7cfaf4-713f-473c-bbcd-6137425faeaf";
}
// Bitmap Encoder guid
else if (value == 1)
{
return "0af1d87e-fcfe-4188-bdeb-a7906471cbe3";
}
// JPEG Encoder guid
else if (value == 2)
{
return "19e4a5aa-5662-4fc5-a0c0-1758028e1057";
}
// Tiff encoder guid.
else if (value == 3)
{
return "163bcc30-e2e9-4f0b-961d-a3e9fdb788a3";
}
// Tiff encoder guid.
else if (value == 4)
{
return "57a37caa-367a-4540-916b-f183c5093a4b";
}
// Gif encoder guid.
else if (value == 5)
{
return "1f8a5601-7d4d-4cbd-9c82-1bc8d4eeb9a5";
}
return null;
}
public int GetEncoderIndex(string guid)
{
// PNG Encoder guid
if (guid == "1b7cfaf4-713f-473c-bbcd-6137425faeaf")
{
return 0;
}
// Bitmap Encoder guid
else if (guid == "0af1d87e-fcfe-4188-bdeb-a7906471cbe3")
{
return 1;
}
// JPEG Encoder guid
else if (guid == "19e4a5aa-5662-4fc5-a0c0-1758028e1057")
{
return 2;
}
// Tiff encoder guid.
else if (guid == "163bcc30-e2e9-4f0b-961d-a3e9fdb788a3")
{
return 3;
}
// Tiff encoder guid.
else if (guid == "57a37caa-367a-4540-916b-f183c5093a4b")
{
return 4;
}
// Gif encoder guid.
else if (guid == "1f8a5601-7d4d-4cbd-9c82-1bc8d4eeb9a5")
{
return 5;
}
return -1;
}
public void Size_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
ImageSize modifiedSize = (ImageSize)sender;
ObservableCollection<ImageSize> imageSizes = Sizes;
imageSizes.Where<ImageSize>(x => x.Id == modifiedSize.Id).First().Update(modifiedSize);
Sizes = imageSizes;
OnPropertyChanged("Sizes");
}
}
}

View File

@@ -2,17 +2,17 @@
x:Class="Microsoft.PowerToys.Settings.UI.Views.ImageResizerPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewModel="using:Microsoft.PowerToys.Settings.UI.ViewModels"
xmlns:CustomControls="using:Microsoft.PowerToys.Settings.UI.Controls"
xmlns:models="using:Microsoft.PowerToys.Settings.UI.Lib"
xmlns:viewModels="using:Microsoft.PowerToys.Settings.UI.Lib.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
<viewModel:ImageResizerViewModel x:Key="ViewModel"/>
<!--<viewModel:ImageResizerViewModel x:Key="ViewModel"/>-->
</Page.Resources>
<Grid RowSpacing="{StaticResource DefaultRowSpacing}">
@@ -58,7 +58,7 @@
Foreground="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabled, Converter={StaticResource ModuleEnabledToForegroundConverter}}"/>
<ListView x:Name="ImagesSizesListView"
ItemsSource="{Binding Sizes, Mode=TwoWay, Source={StaticResource ViewModel}}"
ItemsSource="{x:Bind ViewModel.Sizes, Mode=TwoWay}"
Padding="0"
IsEnabled="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabled}"
SelectionMode="None">
@@ -146,16 +146,17 @@
<ComboBoxItem x:Uid="ImageResizer_Sizes_Units_Percent" />
<ComboBoxItem x:Uid="ImageResizer_Sizes_Units_Pixels" />
</ComboBox>
<Button x:Name="RemoveButton"
Background="Transparent"
Command = "{Binding DeleteImageSizeEventHandler, Source={StaticResource ViewModel}}"
CommandParameter="{Binding Id}"
FontFamily="Segoe MDL2 Assets"
Height="34"
Width="34"
Content="&#xE74D;"
VerticalAlignment="Center"
Margin="{StaticResource SmallTopMargin}" UseLayoutRounding="False"/>
<Button x:Name="RemoveButton"
Background="Transparent"
FontFamily="Segoe MDL2 Assets"
Height="34"
Width="34"
Content="&#xE74D;"
VerticalAlignment="Center"
Margin="{StaticResource SmallTopMargin}"
UseLayoutRounding="False"
Click="DeleteCustomSize"
CommandParameter="{Binding Id}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
@@ -169,8 +170,9 @@
Style="{StaticResource AddItemAppBarButtonStyle}"
IsEnabled="{x:Bind Mode=TwoWay, Path=ViewModel.IsEnabled}"
x:Uid="ImageResizer_AddSizeButton"
Click="AddSizeButton_Click"
Margin="{StaticResource AddItemButtonMargin}"
Command = "{Binding AddImageSizeEventHandler, Source={StaticResource ViewModel}}"
/>
</StackPanel>
@@ -179,7 +181,7 @@
Foreground="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabled, Converter={StaticResource ModuleEnabledToForegroundConverter}}"/>
<ComboBox x:Uid="ImageResizer_FallBackEncoderText"
SelectedIndex="{Binding Path=Encoder, Mode=TwoWay, Source={StaticResource ViewModel}}"
SelectedIndex="{x:Bind Path=ViewModel.Encoder, Mode=TwoWay}"
Width="240"
Margin="{StaticResource SmallTopMargin}"
IsEnabled="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabled}">
@@ -194,10 +196,10 @@
<TextBlock x:Uid="ImageResizer_Encoding"
Margin="{StaticResource SmallTopMargin}"
Foreground="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabled, Converter={StaticResource ModuleEnabledToForegroundConverter}}"/>
<muxc:NumberBox Minimum="0"
Maximum="100"
Value="{ Binding Mode=TwoWay, Path=JPEGQualityLevel, Source={StaticResource ViewModel}}"
Value="{x:Bind Mode=TwoWay, Path=ViewModel.JPEGQualityLevel}"
Width="240"
Margin="{StaticResource HeaderTextTopMargin}"
SpinButtonPlacementMode="Compact"
@@ -206,7 +208,7 @@
/>
<ComboBox x:Uid="ImageResizer_PNGInterlacing"
SelectedIndex="{ Binding Mode=TwoWay, Path=PngInterlaceOption, Source={StaticResource ViewModel}}"
SelectedIndex="{x:Bind Mode=TwoWay, Path=ViewModel.PngInterlaceOption}"
Width="240"
Margin="{StaticResource SmallTopMargin}"
IsEnabled="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabled}">
@@ -216,7 +218,7 @@
</ComboBox>
<ComboBox x:Uid="ImageResizer_TIFFCompression"
SelectedIndex="{ Binding Mode=TwoWay, Path=TiffCompressOption, Source={StaticResource ViewModel}}"
SelectedIndex="{x:Bind Mode=TwoWay, Path=ViewModel.TiffCompressOption}"
Width="240"
Margin="{StaticResource SmallTopMargin}"
IsEnabled="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabled}">
@@ -293,7 +295,7 @@
<CheckBox x:Uid="ImageResizer_UseOriginalDate"
IsEnabled="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabled}"
Margin="{StaticResource SmallTopMargin}"
IsChecked="{Binding Mode=TwoWay, Path=KeepDateModified, Source={StaticResource ViewModel}}"/>
IsChecked="{x:Bind Mode=TwoWay, Path=ViewModel.KeepDateModified}"/>
</StackPanel>
<RelativePanel x:Name="SidePanel"
@@ -318,7 +320,7 @@
RelativePanel.Below="DescriptionPanel">
<Image Source="ms-appx:///Assets/Modules/ImageResizer.png" />
</Border>
<StackPanel x:Name="LinksPanel"
Margin="0,1,0,0"
RelativePanel.Below="AboutImage"

View File

@@ -2,21 +2,48 @@
// 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 System;
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace Microsoft.PowerToys.Settings.UI.Views
{
public sealed partial class ImageResizerPage : Page
{
private ImageResizerViewModel ViewModel { get; set; }
public ImageResizerViewModel ViewModel { get; set; }
public ImageResizerPage()
{
InitializeComponent();
ViewModel = new ImageResizerViewModel();
ViewModel = new ImageResizerViewModel(ShellPage.SendDefaultIPCMessage);
DataContext = ViewModel;
}
public void DeleteCustomSize(object sender, RoutedEventArgs e)
{
try
{
Button deleteRowButton = (Button)sender;
int rowNum = int.Parse(deleteRowButton.CommandParameter.ToString());
ViewModel.DeleteImageSize(rowNum);
}
catch (Exception exp)
{
}
}
private void AddSizeButton_Click(object sender, RoutedEventArgs e)
{
try
{
ViewModel.AddRow();
}
catch (Exception exp)
{
}
}
}
}