Added Image Resizer Settings (#2324)

* added image resizer settings

* updated string resource and binding

* added tests and removed sett advanced settings from image resizer

* fixed string resource spacing

* moved conbo box strings to string resource

* updated name of contributor

* Capitalized size names

* updated fallback encoder and sizers configs

* removed interence between settings | used static resource binding

* fixed build error
This commit is contained in:
Lavius Motileng
2020-04-26 17:34:03 -07:00
committed by GitHub
parent 4946daeea4
commit 8f2a33dcaa
38 changed files with 1431 additions and 110 deletions

View File

@@ -119,6 +119,7 @@
<SDKReference Include="TestPlatform.Universal, Version=$(UnitTestPlatformVersion)" />
</ItemGroup>
<ItemGroup>
<Compile Include="ViewModelTests\ImageResizer.cs" />
<Compile Include="ModelsTests\BasePTModuleSettingsTest.cs" />
<Compile Include="ModelsTests\BasePTSettingsTest.cs" />
<Compile Include="ModelsTests\SettingsUtilsTests.cs" />
@@ -126,8 +127,8 @@
<Compile Include="UnitTestApp.xaml.cs">
<DependentUpon>UnitTestApp.xaml</DependentUpon>
</Compile>
<Compile Include="ViewModelTests\ShortcutGuide.cs" />
<Compile Include="ViewModelTests\PowerLauncherViewModelTest.cs" />
<Compile Include="ViewModelTests\ShortcutGuideViewModelTest.cs" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="UnitTestApp.xaml">

View File

@@ -1,10 +1,11 @@
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UnitTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using System;
namespace Microsoft.PowerToys.Settings.UnitTest
namespace CommonLibTest
{
[TestClass]
public class BasePTModuleSettingsTest

View File

@@ -1,4 +1,5 @@
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UnitTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Diagnostics;
@@ -7,7 +8,7 @@ using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
namespace Microsoft.PowerToys.Settings.UnitTest
namespace CommonLibTest
{
[TestClass]
public class SettingsUtilsTests

View File

@@ -0,0 +1,214 @@
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.ViewModels;
using Microsoft.PowerToys.Settings.UI.Views;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace ViewModelTests
{
[TestClass]
public class ImageResizer
{
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);
}
}
public void DeleteFolder(string powertoy)
{
Directory.Delete(Path.Combine(SettingsUtils.LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"), true);
}
[TestMethod]
public void IsEnabled_ShouldEnableModule_WhenSuccessful()
{
// arrange
ImageResizerViewModel viewModel = new ImageResizerViewModel();
// Assert
ShellPage.DefaultSndMSGCallback = msg =>
{
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
Assert.IsTrue(snd.GeneralSettings.Enabled.ImageResizer);
};
// act
viewModel.IsEnabled = true;
}
[TestMethod]
public void JPEGQualityLevel_ShouldSetValueToTen_WhenSuccefull()
{
// arrange
ImageResizerViewModel viewModel = new ImageResizerViewModel();
// act
viewModel.JPEGQualityLevel = 10;
// Assert
viewModel = new ImageResizerViewModel();
Assert.AreEqual(10, viewModel.JPEGQualityLevel);
}
[TestMethod]
public void PngInterlaceOption_ShouldSetValueToTen_WhenSuccefull()
{
// arrange
ImageResizerViewModel viewModel = new ImageResizerViewModel();
// act
viewModel.PngInterlaceOption = 10;
// Assert
viewModel = new ImageResizerViewModel();
Assert.AreEqual(10, viewModel.PngInterlaceOption);
}
[TestMethod]
public void TiffCompressOption_ShouldSetValueToTen_WhenSuccefull()
{
// arrange
ImageResizerViewModel viewModel = new ImageResizerViewModel();
// act
viewModel.TiffCompressOption = 10;
// Assert
viewModel = new ImageResizerViewModel();
Assert.AreEqual(10, viewModel.TiffCompressOption);
}
[TestMethod]
public void FileName_ShouldUpdateValue_WhenSuccefull()
{
// arrange
ImageResizerViewModel viewModel = new ImageResizerViewModel();
string exptectedValue = "%1 (%3)";
// act
viewModel.FileName = exptectedValue;
// Assert
viewModel = new ImageResizerViewModel();
Assert.AreEqual(exptectedValue, viewModel.FileName);
}
[TestMethod]
public void FileName_ShouldNOTUpdateValue_WhenNameIsInValid ()
{
// arrange
ImageResizerViewModel viewModel = new ImageResizerViewModel();
string[] invalidNames =
{
string.Empty,
" ", // no name.
"%1", // single name value.
"%7 (%5)", // name max index exceeded.
"%8 (%8)", // name max index exceeded.
"%5 (%3 )", // name contains extra spaces.
"%5 (%3)", // name contains extra spaces.
"%5 ( %3)", // name contains extra spaces.
"% 5 ( %3)", // name contains extra spaces.
"%5 (% 3)", // name contains extra spaces.
"%5 ( %3 )", // name contains extra spaces.
};
// act and Assert
foreach (string invalidName in invalidNames)
{
viewModel = new ImageResizerViewModel();
viewModel.FileName = invalidName;
Assert.AreNotEqual(invalidName, viewModel.FileName);
ImageResizerSettings settings = SettingsUtils.GetSettings<ImageResizerSettings>(Module);
Assert.AreNotEqual(invalidName, settings.Properties.ImageresizerFileName.Value);
}
}
[TestMethod]
public void KeepDateModified_ShouldUpdateValue_WhenSuccefull()
{
// arrange
ImageResizerViewModel viewModel = new ImageResizerViewModel();
// act
viewModel.KeepDateModified = true;
// Assert
ImageResizerSettings settings = SettingsUtils.GetSettings<ImageResizerSettings>(Module);
Assert.AreEqual(true, settings.Properties.ImageresizerKeepDateModified.Value);
}
[TestMethod]
public void Encoder_ShouldUpdateValue_WhenSuccefull()
{
// arrange
ImageResizerViewModel viewModel = new ImageResizerViewModel();
// act
viewModel.Encoder = 3;
// Assert
viewModel = new ImageResizerViewModel();
Assert.AreEqual("163bcc30-e2e9-4f0b-961d-a3e9fdb788a3", viewModel.GetEncoderGuid(viewModel.Encoder));
Assert.AreEqual(3, viewModel.Encoder);
}
[TestMethod]
public void AddRow_ShouldAddEmptyImageSize_WhenSuccefull()
{
// arrange
ImageResizerViewModel viewModel = new ImageResizerViewModel();
int sizeOfOriginalArray = viewModel.Sizes.Count;
// act
viewModel.AddRow();
// Assert
Assert.AreEqual(viewModel.Sizes.Count, sizeOfOriginalArray + 1);
}
[TestMethod]
public void DeleteImageSize_ShouldDeleteImageSize_WhenSuccefull()
{
// arrange
ImageResizerViewModel viewModel = new ImageResizerViewModel();
int sizeOfOriginalArray = viewModel.Sizes.Count;
ImageSize deleteCandidate = viewModel.Sizes.Where<ImageSize>(x => x.Id == 0).First();
// act
viewModel.DeleteImageSize(0);
// Assert
Assert.AreEqual(viewModel.Sizes.Count, sizeOfOriginalArray - 1);
Assert.IsFalse(viewModel.Sizes.Contains(deleteCandidate));
}
}
}

View File

@@ -8,10 +8,10 @@ using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
namespace Microsoft.PowerToys.Settings.UnitTest.ViewModelTests
namespace ViewModelTests
{
[TestClass]
public class PowerLauncherViewModelTest
public class PowerLauncher
{
class PowerLauncherSettingsMock : PowerLauncherSettings
{

View File

@@ -10,10 +10,10 @@ using System.IO;
using System.Text.Json;
using Windows.UI.Popups;
namespace Microsoft.PowerToys.Settings.UnitTest.ViewModelTests
namespace ViewModelTests
{
[TestClass]
public class ShortcutGuideViewModelTest
public class ShortcutGuide
{
[TestInitialize]
public void Setup()
@@ -28,6 +28,22 @@ namespace Microsoft.PowerToys.Settings.UnitTest.ViewModelTests
SettingsUtils.SaveSettings(shortcutGuide.ToJsonString(), shortcutGuide.Name);
}
[TestCleanup]
public void CleanUp()
{
// delete folder created.
string file_name = "\\test";
if (SettingsUtils.SettingsFolderExists(file_name))
{
DeleteFolder(file_name);
}
}
public void DeleteFolder(string powertoy)
{
Directory.Delete(Path.Combine(SettingsUtils.LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"), true);
}
[TestMethod]
public void IsEnabled_ShouldEnableModule_WhenSuccessful()
{
@@ -39,7 +55,7 @@ namespace Microsoft.PowerToys.Settings.UnitTest.ViewModelTests
ShellPage.DefaultSndMSGCallback = msg =>
{
OutGoingGeneralSettings snd = JsonSerializer.Deserialize<OutGoingGeneralSettings>(msg);
Assert.IsTrue(snd.general.Enabled.ShortcutGuide);
Assert.IsTrue(snd.GeneralSettings.Enabled.ShortcutGuide);
};
// Act
@@ -56,9 +72,8 @@ namespace Microsoft.PowerToys.Settings.UnitTest.ViewModelTests
// Initilize mock function of sending IPC message.
ShellPage.DefaultSndMSGCallback = msg =>
{
SndModuleSettings<ShortcutGuideSettings> snd = JsonSerializer.Deserialize<SndModuleSettings<ShortcutGuideSettings>>(msg);
Assert.AreEqual("dark", snd.powertoys.Properties.Theme.Value);
Assert.AreEqual("hey", msg);
ShortcutGuideSettingsIPCMessage snd = JsonSerializer.Deserialize<ShortcutGuideSettingsIPCMessage>(msg);
Assert.AreEqual("dark", snd.Powertoys.ShortcutGuide.Properties.Theme.Value);
};
// Act
@@ -75,9 +90,8 @@ namespace Microsoft.PowerToys.Settings.UnitTest.ViewModelTests
// Initilize mock function of sending IPC message.
ShellPage.DefaultSndMSGCallback = msg =>
{
SndModuleSettings<ShortcutGuideSettings> snd = JsonSerializer.Deserialize<SndModuleSettings<ShortcutGuideSettings>>(msg);
// https://stackoverflow.com/questions/59198417/deserialization-of-reference-types-without-parameterless-constructor-is-not-supp
Assert.AreEqual(100, snd.powertoys.Properties.PressTime.Value);
ShortcutGuideSettingsIPCMessage snd = JsonSerializer.Deserialize<ShortcutGuideSettingsIPCMessage>(msg);
Assert.AreEqual(100, snd.Powertoys.ShortcutGuide.Properties.PressTime.Value);
};
// Act
@@ -94,14 +108,13 @@ namespace Microsoft.PowerToys.Settings.UnitTest.ViewModelTests
// Initilize mock function of sending IPC message.
ShellPage.DefaultSndMSGCallback = msg =>
{
SndModuleSettings<ShortcutGuideSettings> snd = JsonSerializer.Deserialize<SndModuleSettings<ShortcutGuideSettings>>(msg);
ShortcutGuideSettingsIPCMessage snd = JsonSerializer.Deserialize<ShortcutGuideSettingsIPCMessage>(msg);
// Serialisation not working as expected in the test project:
// https://stackoverflow.com/questions/59198417/deserialization-of-reference-types-without-parameterless-constructor-is-not-supp
Assert.AreEqual(100, snd.powertoys.Properties.OverlayOpacity.Value);
Assert.AreEqual(100, snd.Powertoys.ShortcutGuide.Properties.OverlayOpacity.Value);
};
// Act
viewModel.OverlayOpacity = 100;
}
}
}
}