mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-04 18:26:39 +02:00
Test frameworks consolidated (#12672)
This commit is contained in:
committed by
GitHub
parent
c3a51f9227
commit
e96c0da265
@@ -1,46 +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;
|
||||
|
||||
[module: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1636:FileHeaderCopyrightTextMustMatch", Justification = "File created under PowerToys.")]
|
||||
|
||||
namespace ImageResizer.Properties
|
||||
{
|
||||
public class AppFixture : IDisposable
|
||||
{
|
||||
public AppFixture()
|
||||
{
|
||||
// new App() needs to be created since Settings.Reload() uses App.Current to update properties on the UI thread. App() can be created only once otherwise it results in System.InvalidOperationException : Cannot create more than one System.Windows.Application instance in the same AppDomain.
|
||||
_imageResizerApp = new App();
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("CodeQuality", "IDE0052:Remove unread private members", Justification = "new App() needs to be created since Settings.Reload() uses App.Current to update properties on the UI thread. App() can be created only once otherwise it results in System.InvalidOperationException : Cannot create more than one System.Windows.Application instance in the same AppDomain")]
|
||||
private App _imageResizerApp;
|
||||
private bool _disposedValue;
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!_disposedValue)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_imageResizerApp.Dispose();
|
||||
_imageResizerApp = null;
|
||||
}
|
||||
|
||||
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
|
||||
// TODO: set large fields to null
|
||||
_disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
||||
Dispose(disposing: true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,21 @@
|
||||
// Copyright (c) Brice Lambson
|
||||
// Copyright (c) Brice Lambson
|
||||
// The Brice Lambson licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/
|
||||
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Specialized;
|
||||
using System.ComponentModel;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using ImageResizer.Models;
|
||||
using ImageResizer.Test;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
using Xunit.Extensions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace ImageResizer.Properties
|
||||
{
|
||||
public class SettingsTests : IClassFixture<AppFixture>, IDisposable
|
||||
[TestClass]
|
||||
public class SettingsTests
|
||||
{
|
||||
private bool disposedValue;
|
||||
private static App _imageResizerApp;
|
||||
|
||||
public SettingsTests()
|
||||
{
|
||||
@@ -25,7 +23,16 @@ namespace ImageResizer.Properties
|
||||
Settings.SettingsPath = ".\\test_settings.json";
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[ClassInitialize]
|
||||
#pragma warning disable CA1801 // Review unused parameters
|
||||
public static void ClassInitialize(TestContext context)
|
||||
#pragma warning restore CA1801 // Review unused parameters
|
||||
{
|
||||
// new App() needs to be created since Settings.Reload() uses App.Current to update properties on the UI thread. App() can be created only once otherwise it results in System.InvalidOperationException : Cannot create more than one System.Windows.Application instance in the same AppDomain.
|
||||
_imageResizerApp = new App();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AllSizesPropagatesSizesCollectionEvents()
|
||||
{
|
||||
var settings = new Settings
|
||||
@@ -41,10 +48,10 @@ namespace ImageResizer.Properties
|
||||
h => ncc.CollectionChanged -= h,
|
||||
() => settings.Sizes.Add(new ResizeSize()));
|
||||
|
||||
Assert.Equal(NotifyCollectionChangedAction.Add, result.Arguments.Action);
|
||||
Assert.AreEqual(NotifyCollectionChangedAction.Add, result.Arguments.Action);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[TestMethod]
|
||||
public void AllSizesPropagatesSizesPropertyEvents()
|
||||
{
|
||||
var settings = new Settings
|
||||
@@ -53,13 +60,22 @@ namespace ImageResizer.Properties
|
||||
};
|
||||
|
||||
settings.Sizes.Clear();
|
||||
Assert.PropertyChanged(
|
||||
(INotifyPropertyChanged)settings.AllSizes,
|
||||
"Item[]",
|
||||
() => settings.Sizes.Add(new ResizeSize()));
|
||||
|
||||
var result = false;
|
||||
((INotifyPropertyChanged)settings.AllSizes).PropertyChanged += (sender, e) =>
|
||||
{
|
||||
if (e.PropertyName == "Item[]")
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
};
|
||||
|
||||
settings.Sizes.Add(new ResizeSize());
|
||||
|
||||
Assert.IsTrue(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[TestMethod]
|
||||
public void AllSizesContainsSizes()
|
||||
{
|
||||
var settings = new Settings
|
||||
@@ -68,10 +84,10 @@ namespace ImageResizer.Properties
|
||||
};
|
||||
|
||||
settings.Sizes.Add(new ResizeSize());
|
||||
Assert.Contains(settings.Sizes[0], settings.AllSizes);
|
||||
CollectionAssert.Contains(settings.AllSizes.ToList(), settings.Sizes[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[TestMethod]
|
||||
public void AllSizesContainsCustomSize()
|
||||
{
|
||||
var settings = new Settings
|
||||
@@ -80,10 +96,10 @@ namespace ImageResizer.Properties
|
||||
};
|
||||
settings.Sizes.Clear();
|
||||
|
||||
Assert.Contains(settings.CustomSize, settings.AllSizes);
|
||||
CollectionAssert.Contains(settings.AllSizes.ToList(), settings.CustomSize);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[TestMethod]
|
||||
public void AllSizesHandlesPropertyEventsForCustomSize()
|
||||
{
|
||||
var originalCustomSize = new CustomSize();
|
||||
@@ -100,29 +116,29 @@ namespace ImageResizer.Properties
|
||||
h => ncc.CollectionChanged -= h,
|
||||
() => settings.CustomSize = new CustomSize());
|
||||
|
||||
Assert.Equal(NotifyCollectionChangedAction.Replace, result.Arguments.Action);
|
||||
Assert.Equal(1, result.Arguments.NewItems.Count);
|
||||
Assert.Equal(settings.CustomSize, result.Arguments.NewItems[0]);
|
||||
Assert.Equal(0, result.Arguments.NewStartingIndex);
|
||||
Assert.Equal(1, result.Arguments.OldItems.Count);
|
||||
Assert.Equal(originalCustomSize, result.Arguments.OldItems[0]);
|
||||
Assert.Equal(0, result.Arguments.OldStartingIndex);
|
||||
Assert.AreEqual(NotifyCollectionChangedAction.Replace, result.Arguments.Action);
|
||||
Assert.AreEqual(1, result.Arguments.NewItems.Count);
|
||||
Assert.AreEqual(settings.CustomSize, result.Arguments.NewItems[0]);
|
||||
Assert.AreEqual(0, result.Arguments.NewStartingIndex);
|
||||
Assert.AreEqual(1, result.Arguments.OldItems.Count);
|
||||
Assert.AreEqual(originalCustomSize, result.Arguments.OldItems[0]);
|
||||
Assert.AreEqual(0, result.Arguments.OldStartingIndex);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[TestMethod]
|
||||
public void FileNameFormatWorks()
|
||||
{
|
||||
var settings = new Settings { FileName = "{T}%1e%2s%3t%4%5%6%7" };
|
||||
|
||||
var result = settings.FileNameFormat;
|
||||
|
||||
Assert.Equal("{{T}}{0}e{1}s{2}t{3}{4}{5}%7", result);
|
||||
Assert.AreEqual("{{T}}{0}e{1}s{2}t{3}{4}{5}%7", result);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)]
|
||||
[InlineData(1)]
|
||||
[InlineData(2)]
|
||||
[DataTestMethod]
|
||||
[DataRow(0)]
|
||||
[DataRow(1)]
|
||||
[DataRow(2)]
|
||||
public void SelectedSizeReturnsCustomSizeWhenOutOfRange(int index)
|
||||
{
|
||||
var settings = new Settings
|
||||
@@ -134,10 +150,10 @@ namespace ImageResizer.Properties
|
||||
|
||||
var result = settings.SelectedSize;
|
||||
|
||||
Assert.Same(settings.CustomSize, result);
|
||||
Assert.AreEqual(settings.CustomSize, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[TestMethod]
|
||||
public void SelectedSizeReturnsSizeWhenInRange()
|
||||
{
|
||||
var settings = new Settings
|
||||
@@ -148,22 +164,22 @@ namespace ImageResizer.Properties
|
||||
settings.Sizes.Add(new ResizeSize());
|
||||
var result = settings.SelectedSize;
|
||||
|
||||
Assert.Same(settings.Sizes[0], result);
|
||||
Assert.AreEqual(settings.Sizes[0], result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[TestMethod]
|
||||
public void IDataErrorInfoErrorReturnsEmpty()
|
||||
{
|
||||
var settings = new Settings();
|
||||
|
||||
var result = ((IDataErrorInfo)settings).Error;
|
||||
|
||||
Assert.Empty(result);
|
||||
Assert.AreEqual(result, string.Empty);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)]
|
||||
[InlineData(101)]
|
||||
[DataTestMethod]
|
||||
[DataRow(0)]
|
||||
[DataRow(101)]
|
||||
public void IDataErrorInfoItemJpegQualityLevelReturnsErrorWhenOutOfRange(int value)
|
||||
{
|
||||
var settings = new Settings { JpegQualityLevel = value };
|
||||
@@ -171,129 +187,180 @@ namespace ImageResizer.Properties
|
||||
var result = ((IDataErrorInfo)settings)["JpegQualityLevel"];
|
||||
|
||||
// Using InvariantCulture since this is used internally
|
||||
Assert.Equal(
|
||||
Assert.AreEqual(
|
||||
string.Format(CultureInfo.InvariantCulture, Resources.ValueMustBeBetween, 1, 100),
|
||||
result);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1)]
|
||||
[InlineData(100)]
|
||||
[DataTestMethod]
|
||||
[DataRow(1)]
|
||||
[DataRow(100)]
|
||||
public void IDataErrorInfoItemJpegQualityLevelReturnsEmptyWhenInRange(int value)
|
||||
{
|
||||
var settings = new Settings { JpegQualityLevel = value };
|
||||
|
||||
var result = ((IDataErrorInfo)settings)["JpegQualityLevel"];
|
||||
|
||||
Assert.Empty(result);
|
||||
Assert.AreEqual(result, string.Empty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[TestMethod]
|
||||
public void IDataErrorInfoItemReturnsEmptyWhenNotJpegQualityLevel()
|
||||
{
|
||||
var settings = new Settings();
|
||||
|
||||
var result = ((IDataErrorInfo)settings)["Unknown"];
|
||||
|
||||
Assert.Empty(result);
|
||||
Assert.AreEqual(result, string.Empty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[TestMethod]
|
||||
public void ReloadCreatesFileWhenFileNotFound()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new Settings();
|
||||
|
||||
// Assert
|
||||
Assert.False(System.IO.File.Exists(Settings.SettingsPath));
|
||||
Assert.IsFalse(System.IO.File.Exists(Settings.SettingsPath));
|
||||
|
||||
// Act
|
||||
settings.Reload();
|
||||
|
||||
// Assert
|
||||
Assert.True(System.IO.File.Exists(Settings.SettingsPath));
|
||||
Assert.IsTrue(System.IO.File.Exists(Settings.SettingsPath));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[TestMethod]
|
||||
public void SaveCreatesFile()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new Settings();
|
||||
|
||||
// Assert
|
||||
Assert.False(System.IO.File.Exists(Settings.SettingsPath));
|
||||
Assert.IsFalse(System.IO.File.Exists(Settings.SettingsPath));
|
||||
|
||||
// Act
|
||||
settings.Save();
|
||||
|
||||
// Assert
|
||||
Assert.True(System.IO.File.Exists(Settings.SettingsPath));
|
||||
Assert.IsTrue(System.IO.File.Exists(Settings.SettingsPath));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[TestMethod]
|
||||
public void SaveJsonIsReadableByReload()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new Settings();
|
||||
|
||||
// Assert
|
||||
Assert.False(System.IO.File.Exists(Settings.SettingsPath));
|
||||
Assert.IsFalse(System.IO.File.Exists(Settings.SettingsPath));
|
||||
|
||||
// Act
|
||||
settings.Save();
|
||||
settings.Reload(); // If the JSON file created by Save() is not readable this function will throw an error
|
||||
|
||||
// Assert
|
||||
Assert.True(System.IO.File.Exists(Settings.SettingsPath));
|
||||
Assert.IsTrue(System.IO.File.Exists(Settings.SettingsPath));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[TestMethod]
|
||||
public void ReloadRaisesPropertyChanged()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new Settings();
|
||||
settings.Save(); // To create the settings file
|
||||
|
||||
var shrinkOnlyChanged = false;
|
||||
var replaceChanged = false;
|
||||
var ignoreOrientationChanged = false;
|
||||
var jpegQualityLevelChanged = false;
|
||||
var pngInterlaceOptionChanged = false;
|
||||
var tiffCompressOptionChanged = false;
|
||||
var fileNameChanged = false;
|
||||
var keepDateModifiedChanged = false;
|
||||
var fallbackEncoderChanged = false;
|
||||
var customSizeChanged = false;
|
||||
var selectedSizeIndexChanged = false;
|
||||
|
||||
settings.PropertyChanged += (sender, e) =>
|
||||
{
|
||||
if (e.PropertyName == "ShrinkOnly")
|
||||
{
|
||||
shrinkOnlyChanged = true;
|
||||
}
|
||||
else if (e.PropertyName == "Replace")
|
||||
{
|
||||
replaceChanged = true;
|
||||
}
|
||||
else if (e.PropertyName == "IgnoreOrientation")
|
||||
{
|
||||
ignoreOrientationChanged = true;
|
||||
}
|
||||
else if (e.PropertyName == "JpegQualityLevel")
|
||||
{
|
||||
jpegQualityLevelChanged = true;
|
||||
}
|
||||
else if (e.PropertyName == "PngInterlaceOption")
|
||||
{
|
||||
pngInterlaceOptionChanged = true;
|
||||
}
|
||||
else if (e.PropertyName == "TiffCompressOption")
|
||||
{
|
||||
tiffCompressOptionChanged = true;
|
||||
}
|
||||
else if (e.PropertyName == "FileName")
|
||||
{
|
||||
fileNameChanged = true;
|
||||
}
|
||||
else if (e.PropertyName == "KeepDateModified")
|
||||
{
|
||||
keepDateModifiedChanged = true;
|
||||
}
|
||||
else if (e.PropertyName == "FallbackEncoder")
|
||||
{
|
||||
fallbackEncoderChanged = true;
|
||||
}
|
||||
else if (e.PropertyName == "CustomSize")
|
||||
{
|
||||
customSizeChanged = true;
|
||||
}
|
||||
else if (e.PropertyName == "SelectedSizeIndex")
|
||||
{
|
||||
selectedSizeIndexChanged = true;
|
||||
}
|
||||
};
|
||||
|
||||
// Act
|
||||
var action = new System.Action(settings.Reload);
|
||||
settings.Reload();
|
||||
|
||||
// Assert
|
||||
Assert.PropertyChanged(settings, "ShrinkOnly", action);
|
||||
Assert.PropertyChanged(settings, "Replace", action);
|
||||
Assert.PropertyChanged(settings, "IgnoreOrientation", action);
|
||||
Assert.PropertyChanged(settings, "JpegQualityLevel", action);
|
||||
Assert.PropertyChanged(settings, "PngInterlaceOption", action);
|
||||
Assert.PropertyChanged(settings, "TiffCompressOption", action);
|
||||
Assert.PropertyChanged(settings, "FileName", action);
|
||||
Assert.PropertyChanged(settings, "KeepDateModified", action);
|
||||
Assert.PropertyChanged(settings, "FallbackEncoder", action);
|
||||
Assert.PropertyChanged(settings, "CustomSize", action);
|
||||
Assert.PropertyChanged(settings, "SelectedSizeIndex", action);
|
||||
Assert.IsTrue(shrinkOnlyChanged);
|
||||
Assert.IsTrue(replaceChanged);
|
||||
Assert.IsTrue(ignoreOrientationChanged);
|
||||
Assert.IsTrue(jpegQualityLevelChanged);
|
||||
Assert.IsTrue(pngInterlaceOptionChanged);
|
||||
Assert.IsTrue(tiffCompressOptionChanged);
|
||||
Assert.IsTrue(fileNameChanged);
|
||||
Assert.IsTrue(keepDateModifiedChanged);
|
||||
Assert.IsTrue(fallbackEncoderChanged);
|
||||
Assert.IsTrue(customSizeChanged);
|
||||
Assert.IsTrue(selectedSizeIndexChanged);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
[ClassCleanup]
|
||||
public static void ClassCleanup()
|
||||
{
|
||||
if (!disposedValue)
|
||||
_imageResizerApp.Dispose();
|
||||
_imageResizerApp = null;
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void TestCleanUp()
|
||||
{
|
||||
if (System.IO.File.Exists(Settings.SettingsPath))
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
if (System.IO.File.Exists(Settings.SettingsPath))
|
||||
{
|
||||
System.IO.File.Delete(Settings.SettingsPath);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
|
||||
// TODO: set large fields to null
|
||||
disposedValue = true;
|
||||
System.IO.File.Delete(Settings.SettingsPath);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
||||
Dispose(disposing: true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user