From a935d694081259ce9f31ff46ca24f6f4abf1e779 Mon Sep 17 00:00:00 2001 From: Nkateko <58791731+laviusmotileng-ms@users.noreply.github.com> Date: Fri, 3 Jul 2020 15:40:53 -0700 Subject: [PATCH] added null value handle for image resizer size input (#4721) * addle null value handle for image resizer size input * check for negative withd and height * check for negative withd and height * Update ImageSize.cs Co-authored-by: Lavius Motileng --- .../ImageSize.cs | 20 +++++++++-- .../ViewModelTests/ImageResizer.cs | 34 +++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageSize.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageSize.cs index f2646afe40..0f12980fd5 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageSize.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageSize.cs @@ -144,9 +144,17 @@ namespace Microsoft.PowerToys.Settings.UI.Lib set { + int newWidth = -1; + int.TryParse(value + string.Empty, out newWidth); + + if (newWidth < 0) + { + newWidth = 0; + } + if (_width != value) { - _width = value; + _width = newWidth; OnPropertyChanged(); } } @@ -162,9 +170,17 @@ namespace Microsoft.PowerToys.Settings.UI.Lib set { + int newHeight = -1; + int.TryParse(value + string.Empty, out newHeight); + + if (newHeight < 0) + { + newHeight = 0; + } + if (_height != value) { - _height = value; + _height = newHeight; OnPropertyChanged(); } } diff --git a/src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/ImageResizer.cs b/src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/ImageResizer.cs index fd3e1f3409..109e9227ea 100644 --- a/src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/ImageResizer.cs +++ b/src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/ImageResizer.cs @@ -183,5 +183,39 @@ namespace ViewModelTests Assert.AreEqual(viewModel.Sizes.Count, sizeOfOriginalArray - 1); Assert.IsFalse(viewModel.Sizes.Contains(deleteCandidate)); } + + [TestMethod] + public void UpdateWidthAndHeight_ShouldUpateSize_WhenCorrectValuesAreProvided() + { + // arrange + ImageSize imageSize = new ImageSize() + { + Id = 0, + Name = "Test", + Fit = (int)ResizeFit.Fit, + Width = 30, + Height = 30, + Unit = (int)ResizeUnit.Pixel, + }; + + double negativeWidth = -2.0; + double negativeHeight = -2.0; + + // Act + imageSize.Width = negativeWidth; + imageSize.Height = negativeHeight; + + // Assert + Assert.AreEqual(0, imageSize.Width); + Assert.AreEqual(0, imageSize.Height); + + // Act + imageSize.Width = 50; + imageSize.Height = 50; + + // Assert + Assert.AreEqual(50, imageSize.Width); + Assert.AreEqual(50, imageSize.Height); + } } }