mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 10:46:33 +02:00
FxCop for Preview handler (#6833)
This commit is contained in:
@@ -11,13 +11,13 @@ using Common.Utilities;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace UnitTests_PreviewHandlerCommon
|
||||
namespace PreviewHandlerCommonUnitTests
|
||||
{
|
||||
[TestClass]
|
||||
public class StreamWrapperTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldThrow_IfInitializeWithNullStream()
|
||||
public void StreamWrapperShouldThrowIfInitializeWithNullStream()
|
||||
{
|
||||
// Arrange
|
||||
IStream stream = null;
|
||||
@@ -26,7 +26,10 @@ namespace UnitTests_PreviewHandlerCommon
|
||||
// Act
|
||||
try
|
||||
{
|
||||
var streamWrapper = new ReadonlyStream(stream);
|
||||
using (var streamWrapper = new ReadonlyStream(stream))
|
||||
{
|
||||
// do work
|
||||
}
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
@@ -38,107 +41,113 @@ namespace UnitTests_PreviewHandlerCommon
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldReturnCanReadTrue()
|
||||
public void StreamWrapperShouldReturnCanReadTrue()
|
||||
{
|
||||
// Arrange
|
||||
var streamMock = new Mock<IStream>();
|
||||
|
||||
// Act
|
||||
var streamWrapper = new ReadonlyStream(streamMock.Object);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(streamWrapper.CanRead, true);
|
||||
using (var streamWrapper = new ReadonlyStream(streamMock.Object))
|
||||
{
|
||||
// Assert
|
||||
Assert.AreEqual(streamWrapper.CanRead, true);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldReturnCanSeekTrue()
|
||||
public void StreamWrapperShouldReturnCanSeekTrue()
|
||||
{
|
||||
// Arrange
|
||||
var streamMock = new Mock<IStream>();
|
||||
|
||||
// Act
|
||||
var streamWrapper = new ReadonlyStream(streamMock.Object);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(streamWrapper.CanSeek, true);
|
||||
using (var streamWrapper = new ReadonlyStream(streamMock.Object))
|
||||
{
|
||||
// Assert
|
||||
Assert.AreEqual(streamWrapper.CanSeek, true);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldReturnCanWriteFalse()
|
||||
public void StreamWrapperShouldReturnCanWriteFalse()
|
||||
{
|
||||
// Arrange
|
||||
var streamMock = new Mock<IStream>();
|
||||
|
||||
// Act
|
||||
var streamWrapper = new ReadonlyStream(streamMock.Object);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(streamWrapper.CanWrite, false);
|
||||
using (var streamWrapper = new ReadonlyStream(streamMock.Object))
|
||||
{
|
||||
// Assert
|
||||
Assert.AreEqual(streamWrapper.CanWrite, false);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldReturnValidLength()
|
||||
public void StreamWrapperShouldReturnValidLength()
|
||||
{
|
||||
// Arrange
|
||||
long streamLength = 5;
|
||||
var stremMock = new Mock<IStream>();
|
||||
var streamMock = new Mock<IStream>();
|
||||
var stat = new System.Runtime.InteropServices.ComTypes.STATSTG
|
||||
{
|
||||
cbSize = streamLength,
|
||||
};
|
||||
|
||||
stremMock
|
||||
.Setup(x => x.Stat(out stat, It.IsAny<int>()));
|
||||
var streamWrapper = new ReadonlyStream(stremMock.Object);
|
||||
streamMock.Setup(x => x.Stat(out stat, It.IsAny<int>()));
|
||||
|
||||
// Act
|
||||
var actualLength = streamWrapper.Length;
|
||||
using (var streamWrapper = new ReadonlyStream(streamMock.Object))
|
||||
{
|
||||
// Act
|
||||
var actualLength = streamWrapper.Length;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(actualLength, streamLength);
|
||||
// Assert
|
||||
Assert.AreEqual(actualLength, streamLength);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldReturnValidPosition()
|
||||
public void StreamWrapperShouldReturnValidPosition()
|
||||
{
|
||||
// Arrange
|
||||
int expectedDwOrigin = 1; // STREAM_SEEK_CUR
|
||||
long expectedOffset = 0;
|
||||
long currPosition = 5;
|
||||
var stremMock = new Mock<IStream>();
|
||||
var streamMock = new Mock<IStream>();
|
||||
|
||||
stremMock
|
||||
streamMock
|
||||
.Setup(x => x.Seek(It.IsAny<long>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
|
||||
.Callback<long, int, IntPtr>((dlibMove, dwOrigin, plibNewPosition) =>
|
||||
{
|
||||
Marshal.WriteInt64(plibNewPosition, currPosition);
|
||||
});
|
||||
var streamWrapper = new ReadonlyStream(stremMock.Object);
|
||||
|
||||
// Act
|
||||
var actualPosition = streamWrapper.Position;
|
||||
using (var streamWrapper = new ReadonlyStream(streamMock.Object))
|
||||
{
|
||||
// Act
|
||||
var actualPosition = streamWrapper.Position;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(actualPosition, currPosition);
|
||||
stremMock.Verify(_ => _.Seek(It.Is<long>(offset => offset == expectedOffset), It.Is<int>(dworigin => dworigin == expectedDwOrigin), It.IsAny<IntPtr>()), Times.Once);
|
||||
// Assert
|
||||
Assert.AreEqual(actualPosition, currPosition);
|
||||
streamMock.Verify(_ => _.Seek(It.Is<long>(offset => offset == expectedOffset), It.Is<int>(dworigin => dworigin == expectedDwOrigin), It.IsAny<IntPtr>()), Times.Once);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldCallIStreamSeek_WhenSetPosition()
|
||||
public void StreamWrapperShouldCallIStreamSeekWhenSetPosition()
|
||||
{
|
||||
// Arrange
|
||||
long positionToSet = 5;
|
||||
int expectedDwOrigin = 0; // STREAM_SEEK_SET
|
||||
var stremMock = new Mock<IStream>();
|
||||
var streamMock = new Mock<IStream>();
|
||||
|
||||
var streamWrapper = new ReadonlyStream(stremMock.Object)
|
||||
using (var streamWrapper = new ReadonlyStream(streamMock.Object))
|
||||
{
|
||||
// Act
|
||||
Position = positionToSet,
|
||||
};
|
||||
streamWrapper.Position = positionToSet;
|
||||
|
||||
// Assert
|
||||
stremMock.Verify(_ => _.Seek(It.Is<long>(offset => offset == positionToSet), It.Is<int>(dworigin => dworigin == expectedDwOrigin), It.IsAny<IntPtr>()), Times.Once);
|
||||
// Assert
|
||||
streamMock.Verify(_ => _.Seek(It.Is<long>(offset => offset == positionToSet), It.Is<int>(dworigin => dworigin == expectedDwOrigin), It.IsAny<IntPtr>()), Times.Once);
|
||||
}
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
@@ -148,7 +157,7 @@ namespace UnitTests_PreviewHandlerCommon
|
||||
[DataRow(5L, SeekOrigin.Current)]
|
||||
[DataRow(0L, SeekOrigin.End)]
|
||||
[DataRow(5L, SeekOrigin.End)]
|
||||
public void StreamWrapper_ShouldCallIStreamSeekWithValidArguments_WhenSeekCalled(long offset, SeekOrigin origin)
|
||||
public void StreamWrapperShouldCallIStreamSeekWithValidArgumentsWhenSeekCalled(long offset, SeekOrigin origin)
|
||||
{
|
||||
// Arrange
|
||||
int expectedDwOrigin = 0;
|
||||
@@ -167,37 +176,39 @@ namespace UnitTests_PreviewHandlerCommon
|
||||
break;
|
||||
}
|
||||
|
||||
var stremMock = new Mock<IStream>();
|
||||
var streamWrapper = new ReadonlyStream(stremMock.Object);
|
||||
var streamMock = new Mock<IStream>();
|
||||
using (var streamWrapper = new ReadonlyStream(streamMock.Object))
|
||||
{
|
||||
// Act
|
||||
streamWrapper.Seek(offset, origin);
|
||||
|
||||
// Act
|
||||
streamWrapper.Seek(offset, origin);
|
||||
|
||||
// Assert
|
||||
stremMock.Verify(_ => _.Seek(It.Is<long>(actualOffset => actualOffset == offset), It.Is<int>(actualDwOrigin => actualDwOrigin == expectedDwOrigin), It.IsAny<IntPtr>()), Times.Once);
|
||||
// Assert
|
||||
streamMock.Verify(_ => _.Seek(It.Is<long>(actualOffset => actualOffset == offset), It.Is<int>(actualDwOrigin => actualDwOrigin == expectedDwOrigin), It.IsAny<IntPtr>()), Times.Once);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldReturnValidPosition_WhenSeekCalled()
|
||||
public void StreamWrapperShouldReturnValidPositionWhenSeekCalled()
|
||||
{
|
||||
// Arrange
|
||||
long position = 5;
|
||||
var stremMock = new Mock<IStream>();
|
||||
var streamMock = new Mock<IStream>();
|
||||
|
||||
stremMock
|
||||
streamMock
|
||||
.Setup(x => x.Seek(It.IsAny<long>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
|
||||
.Callback<long, int, IntPtr>((dlibMove, dwOrigin, plibNewPosition) =>
|
||||
{
|
||||
Marshal.WriteInt64(plibNewPosition, position);
|
||||
});
|
||||
|
||||
var streamWrapper = new ReadonlyStream(stremMock.Object);
|
||||
using (var streamWrapper = new ReadonlyStream(streamMock.Object))
|
||||
{
|
||||
// Act
|
||||
var actualPosition = streamWrapper.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
// Act
|
||||
var actualPosition = streamWrapper.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(position, actualPosition);
|
||||
// Assert
|
||||
Assert.AreEqual(position, actualPosition);
|
||||
}
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
@@ -205,34 +216,35 @@ namespace UnitTests_PreviewHandlerCommon
|
||||
[DataRow(10, 0, -5)]
|
||||
[DataRow(10, 0, 11)]
|
||||
[DataRow(10, 5, 6)]
|
||||
public void StreamWrapper_ShouldThrow_WhenReadCalledWithOutOfRangeArguments(int bufferLength, int offSet, int bytesToRead)
|
||||
public void StreamWrapperShouldThrowWhenReadCalledWithOutOfRangeArguments(int bufferLength, int offSet, int bytesToRead)
|
||||
{
|
||||
// Arrange
|
||||
var buffer = new byte[bufferLength];
|
||||
var stremMock = new Mock<IStream>();
|
||||
var streamMock = new Mock<IStream>();
|
||||
ArgumentOutOfRangeException exception = null;
|
||||
|
||||
var streamWrapper = new ReadonlyStream(stremMock.Object);
|
||||
|
||||
// Act
|
||||
try
|
||||
using (var streamWrapper = new ReadonlyStream(streamMock.Object))
|
||||
{
|
||||
streamWrapper.Read(buffer, offSet, bytesToRead);
|
||||
}
|
||||
catch (ArgumentOutOfRangeException ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
// Act
|
||||
try
|
||||
{
|
||||
streamWrapper.Read(buffer, offSet, bytesToRead);
|
||||
}
|
||||
catch (ArgumentOutOfRangeException ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(exception);
|
||||
// Assert
|
||||
Assert.IsNotNull(exception);
|
||||
}
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow(5, 0)]
|
||||
[DataRow(5, 5)]
|
||||
[DataRow(0, 5)]
|
||||
public void StreamWrapper_ShouldSetValidBuffer_WhenReadCalled(int count, int offset)
|
||||
public void StreamWrapperShouldSetValidBufferWhenReadCalled(int count, int offset)
|
||||
{
|
||||
// Arrange
|
||||
var inputBuffer = new byte[1024];
|
||||
@@ -242,9 +254,9 @@ namespace UnitTests_PreviewHandlerCommon
|
||||
streamBytes[i] = (byte)i;
|
||||
}
|
||||
|
||||
var stremMock = new Mock<IStream>();
|
||||
var streamMock = new Mock<IStream>();
|
||||
|
||||
stremMock
|
||||
streamMock
|
||||
.Setup(x => x.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
|
||||
.Callback<byte[], int, IntPtr>((buffer, countToRead, bytesReadPtr) =>
|
||||
{
|
||||
@@ -252,80 +264,87 @@ namespace UnitTests_PreviewHandlerCommon
|
||||
Marshal.WriteInt32(bytesReadPtr, count);
|
||||
});
|
||||
|
||||
var streamWrapper = new ReadonlyStream(stremMock.Object);
|
||||
using (var streamWrapper = new ReadonlyStream(streamMock.Object))
|
||||
{
|
||||
// Act
|
||||
var bytesRead = streamWrapper.Read(inputBuffer, offset, count);
|
||||
|
||||
// Act
|
||||
var bytesRead = streamWrapper.Read(inputBuffer, offset, count);
|
||||
|
||||
// Assert
|
||||
CollectionAssert.AreEqual(streamBytes, inputBuffer.Skip(offset).Take(count).ToArray());
|
||||
Assert.AreEqual(count, bytesRead);
|
||||
// Assert
|
||||
CollectionAssert.AreEqual(streamBytes, inputBuffer.Skip(offset).Take(count).ToArray());
|
||||
Assert.AreEqual(count, bytesRead);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldThrowNotImplementedException_WhenFlushCalled()
|
||||
public void StreamWrapperShouldThrowNotImplementedExceptionWhenFlushCalled()
|
||||
{
|
||||
// Arrange
|
||||
var stremMock = new Mock<IStream>();
|
||||
var streamWrapper = new ReadonlyStream(stremMock.Object);
|
||||
NotImplementedException exception = null;
|
||||
|
||||
// Act
|
||||
try
|
||||
var streamMock = new Mock<IStream>();
|
||||
using (var streamWrapper = new ReadonlyStream(streamMock.Object))
|
||||
{
|
||||
streamWrapper.Flush();
|
||||
}
|
||||
catch (NotImplementedException ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
NotImplementedException exception = null;
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(exception);
|
||||
// Act
|
||||
try
|
||||
{
|
||||
streamWrapper.Flush();
|
||||
}
|
||||
catch (NotImplementedException ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(exception);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldThrowNotImplementedException_WhenSetLengthCalled()
|
||||
public void StreamWrapperShouldThrowNotImplementedExceptionWhenSetLengthCalled()
|
||||
{
|
||||
// Arrange
|
||||
var stremMock = new Mock<IStream>();
|
||||
var streamWrapper = new ReadonlyStream(stremMock.Object);
|
||||
NotImplementedException exception = null;
|
||||
|
||||
// Act
|
||||
try
|
||||
var streamMock = new Mock<IStream>();
|
||||
using (var streamWrapper = new ReadonlyStream(streamMock.Object))
|
||||
{
|
||||
streamWrapper.SetLength(5);
|
||||
}
|
||||
catch (NotImplementedException ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
NotImplementedException exception = null;
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(exception);
|
||||
// Act
|
||||
try
|
||||
{
|
||||
streamWrapper.SetLength(5);
|
||||
}
|
||||
catch (NotImplementedException ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(exception);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldThrowNotImplementedException_WhenWriteCalled()
|
||||
public void StreamWrapperShouldThrowNotImplementedExceptionWhenWriteCalled()
|
||||
{
|
||||
// Arrange
|
||||
var stremMock = new Mock<IStream>();
|
||||
var streamWrapper = new ReadonlyStream(stremMock.Object);
|
||||
NotImplementedException exception = null;
|
||||
|
||||
// Act
|
||||
try
|
||||
var streamMock = new Mock<IStream>();
|
||||
using (var streamWrapper = new ReadonlyStream(streamMock.Object))
|
||||
{
|
||||
streamWrapper.Write(new byte[5], 0, 0);
|
||||
}
|
||||
catch (NotImplementedException ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
NotImplementedException exception = null;
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(exception);
|
||||
// Act
|
||||
try
|
||||
{
|
||||
streamWrapper.Write(new byte[5], 0, 0);
|
||||
}
|
||||
catch (NotImplementedException ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user