FxCop for Preview handler (#6833)

This commit is contained in:
Clint Rutkas 2020-09-29 10:43:18 -07:00 committed by GitHub
parent 240f67f15c
commit fb8e80c09c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 233 additions and 199 deletions

View File

@ -7,12 +7,12 @@ using Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace UnitTests_PreviewHandlerCommon
namespace PreviewHandlerCommonUnitTests
{
[TestClass]
public class FileBasedPreviewHandlerTests
{
public class TestFileBasedPreviewHandler : FileBasedPreviewHandler
internal class TestFileBasedPreviewHandler : FileBasedPreviewHandler
{
public override void DoPreview()
{
@ -28,7 +28,7 @@ namespace UnitTests_PreviewHandlerCommon
[DataTestMethod]
[DataRow(0U)]
[DataRow(1U)]
public void FileBasedPreviewHandler_ShouldSetFilePath_WhenInitializeCalled(uint grfMode)
public void FileBasedPreviewHandlerShouldSetFilePathWhenInitializeCalled(uint grfMode)
{
// Arrange
var fileBasedPreviewHandler = new TestFileBasedPreviewHandler();

View File

@ -4,12 +4,12 @@
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTests_PreviewHandlerCommon
namespace PreviewHandlerCommonUnitTests
{
[TestClass]
public class FormHandlerControlTests
@ -19,7 +19,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void FormHandlerControl_ShouldCreateHandle_OnInitialization()
public void FormHandlerControlShouldCreateHandleOnInitialization()
{
// Arrange and act
using (var testFormHandlerControl = new TestFormControl())
@ -30,7 +30,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void FormHandlerControl_ShouldSetVisibleFalse_OnInitialization()
public void FormHandlerControlShouldSetVisibleFalseOnInitialization()
{
// Arrange and act
using (var testFormHandlerControl = new TestFormControl())
@ -41,7 +41,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void FormHandlerControl_ShouldSetFormBorderStyle_OnInitialization()
public void FormHandlerControlShouldSetFormBorderStyleOnInitialization()
{
// Arrange and act
using (var testFormHandlerControl = new TestFormControl())
@ -52,7 +52,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void FormHandlerControl_ShouldReturnValidHandle_WhenGetHandleCalled()
public void FormHandlerControlShouldReturnValidHandleWhenGetHandleCalled()
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
@ -66,7 +66,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void FormHandlerControl_ShouldSetBackgroundColor_WhenSetBackgroundColorCalled()
public void FormHandlerControlShouldSetBackgroundColorWhenSetBackgroundColorCalled()
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
@ -82,23 +82,24 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void FormHandlerControl_ShouldSetFont_WhenSetFontCalled()
public void FormHandlerControlShouldSetFontWhenSetFontCalled()
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
{
var font = new Font("Arial", 20);
using (var font = new Font("Arial", 20))
{
// Act
testFormHandlerControl.SetFont(font);
// Act
testFormHandlerControl.SetFont(font);
// Assert
Assert.AreEqual(font, testFormHandlerControl.Font);
// Assert
Assert.AreEqual(font, testFormHandlerControl.Font);
}
}
}
[TestMethod]
public void FormHandlerControl_ShouldUpdateBounds_WhenSetRectCalled()
public void FormHandlerControlShouldUpdateBoundsWhenSetRectCalled()
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
@ -114,7 +115,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void FormHandlerControl_ShouldSetTextColor_WhenSetTextColorCalled()
public void FormHandlerControlShouldSetTextColorWhenSetTextColorCalled()
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
@ -130,7 +131,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void FormHandlerControl_ShouldClearAllControls_WhenUnloadCalled()
public void FormHandlerControlShouldClearAllControlsWhenUnloadCalled()
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
@ -147,7 +148,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void FormHandlerControl_ShouldSetVisibleFalse_WhenUnloadCalled()
public void FormHandlerControlShouldSetVisibleFalseWhenUnloadCalled()
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
@ -161,7 +162,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void FormHandlerControl_ShouldSetVisibletrue_WhenDoPreviewCalled()
public void FormHandlerControlShouldSetVisibletrueWhenDoPreviewCalled()
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
@ -175,26 +176,24 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void FormHandlerControl_ShouldSetParentHandle_WhenSetWindowCalled()
public void FormHandlerControlShouldSetParentHandleWhenSetWindowCalled()
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
{
var parentFormWindow = new UserControl();
var parentHwnd = parentFormWindow.Handle;
var rect = new Rectangle(2, 2, 4, 4);
using (var parentFormWindow = new UserControl())
{
var parentHwnd = parentFormWindow.Handle;
var rect = new Rectangle(2, 2, 4, 4);
// Act
testFormHandlerControl.SetWindow(parentHwnd, rect);
var actualParentHwnd = GetAncestor(testFormHandlerControl.Handle, 1); // GA_PARENT 1
// Act
testFormHandlerControl.SetWindow(parentHwnd, rect);
var actualParentHwnd = NativeMethods.GetAncestor(testFormHandlerControl.Handle, 1); // GA_PARENT 1
// Assert
Assert.AreEqual(parentHwnd, actualParentHwnd);
// Assert
Assert.AreEqual(parentHwnd, actualParentHwnd);
}
}
}
// Gets the ancestor window: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getancestor
[DllImport("user32.dll")]
private static extern IntPtr GetAncestor(IntPtr hWnd, uint gaFlags);
}
}

View File

@ -0,0 +1,16 @@
// 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;
using System.Runtime.InteropServices;
namespace PreviewHandlerCommonUnitTests
{
public static class NativeMethods
{
// Gets the ancestor window: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getancestor
[DllImport("user32.dll")]
internal static extern IntPtr GetAncestor(IntPtr hWnd, uint gaFlags);
}
}

View File

@ -10,14 +10,14 @@ using Common.ComInterlop;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace UnitTests_PreviewHandlerCommon
namespace PreviewHandlerCommonUnitTests
{
[TestClass]
public class PreviewHandlerBaseTests
{
private static IPreviewHandlerControl previewHandlerControl;
public class TestPreviewHandler : PreviewHandlerBase
internal class TestPreviewHandler : PreviewHandlerBase
{
public TestPreviewHandler()
{
@ -36,7 +36,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void PreviewHandlerBase_ShouldCallPreviewControlSetWindow_WhenSetWindowCalled()
public void PreviewHandlerBaseShouldCallPreviewControlSetWindowWhenSetWindowCalled()
{
// Arrange
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
@ -66,7 +66,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void PreviewHandlerBase_ShouldCallPreviewControlSetrect_WhenSetRectCalled()
public void PreviewHandlerBaseShouldCallPreviewControlSetrectWhenSetRectCalled()
{
// Arrange
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
@ -92,7 +92,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void PreviewHandlerBase_ShouldCallPreviewControlUnload_WhenUnloadCalled()
public void PreviewHandlerBaseShouldCallPreviewControlUnloadWhenUnloadCalled()
{
// Arrange
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
@ -108,7 +108,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void PreviewHandlerBase_ShouldCallPreviewControlSetBackgroundColor_WhenSetBackgroundColorCalled()
public void PreviewHandlerBaseShouldCallPreviewControlSetBackgroundColorWhenSetBackgroundColorCalled()
{
// Arrange
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
@ -125,7 +125,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void PreviewHandlerBase_ShouldCallPreviewControlSetTextColor_WhenSetTextColorCalled()
public void PreviewHandlerBaseShouldCallPreviewControlSetTextColorWhenSetTextColorCalled()
{
// Arrange
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
@ -142,7 +142,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void PreviewHandlerBase_ShouldCallPreviewControlSetFont_WhenSetFontCalled()
public void PreviewHandlerBaseShouldCallPreviewControlSetFontWhenSetFontCalled()
{
// Arrange
Font actualFont = null;
@ -166,7 +166,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void PreviewHandlerBase_ShouldCallPreviewControlSetFocus_WhenSetFocusCalled()
public void PreviewHandlerBaseShouldCallPreviewControlSetFocusWhenSetFocusCalled()
{
// Arrange
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
@ -182,7 +182,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void PreviewHandlerBase_ShouldSetHandleOnQueryFocus_WhenPreviewControlsReturnValidHandle()
public void PreviewHandlerBaseShouldSetHandleOnQueryFocusWhenPreviewControlsReturnValidHandle()
{
// Arrange
var hwnd = new IntPtr(5);
@ -202,7 +202,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void PreviewHandlerBase_ShouldThrowOnQueryFocus_WhenPreviewControlsReturnNotValidHandle()
public void PreviewHandlerBaseShouldThrowOnQueryFocusWhenPreviewControlsReturnNotValidHandle()
{
// Arrange
var hwnd = IntPtr.Zero;
@ -230,7 +230,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void PreviewHandlerBase_ShouldDirectKeyStrokesToIPreviewHandlerFrame_IfIPreviewHandlerFrameIsSet()
public void PreviewHandlerBaseShouldDirectKeyStrokesToIPreviewHandlerFrameIfIPreviewHandlerFrameIsSet()
{
// Arrange
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
@ -251,7 +251,7 @@ namespace UnitTests_PreviewHandlerCommon
[DataTestMethod]
[DataRow(0U)]
[DataRow(1U)]
public void PreviewHandlerBase_ShouldReturnIPreviewHandlerFrameResponse_IfIPreviewHandlerFrameIsSet(uint resultCode)
public void PreviewHandlerBaseShouldReturnIPreviewHandlerFrameResponseIfIPreviewHandlerFrameIsSet(uint resultCode)
{
// Arrange
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
@ -273,7 +273,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void PreviewHandlerBase_ShouldReturnS_FALSE_IfIPreviewHandlerFrameIsNotSet()
public void PreviewHandlerBaseShouldReturnUintFalseIfIPreviewHandlerFrameIsNotSet()
{
// Arrange
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
@ -291,7 +291,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void PreviewHandlerBase_ShouldReturnPreviewControlHandle_IfGetWindowCalled()
public void PreviewHandlerBaseShouldReturnPreviewControlHandleIfGetWindowCalled()
{
// Arrange
var previewControlHandle = new IntPtr(5);
@ -313,7 +313,7 @@ namespace UnitTests_PreviewHandlerCommon
[DataTestMethod]
[DataRow(true)]
[DataRow(false)]
public void PreviewHandlerBase_ShouldThrowNotImplementedException_IfContextSensitiveHelpCalled(bool fEnterMode)
public void PreviewHandlerBaseShouldThrowNotImplementedExceptionIfContextSensitiveHelpCalled(bool fEnterMode)
{
// Arrange
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
@ -337,7 +337,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void PreviewHandlerBase_ShouldReturnSite_WhenGetSiteCalled()
public void PreviewHandlerBaseShouldReturnSiteWhenGetSiteCalled()
{
// Arrange
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
@ -346,19 +346,18 @@ namespace UnitTests_PreviewHandlerCommon
var testPreviewHandler = new TestPreviewHandler();
var site = new Mock<IPreviewHandlerFrame>().Object;
testPreviewHandler.SetSite(site);
object actualSite = null;
var riid = Guid.Empty;
// Act
testPreviewHandler.GetSite(ref riid, out actualSite);
testPreviewHandler.GetSite(ref riid, out object actualSite);
// Assert
Assert.AreEqual(actualSite, site);
}
private LOGFONT GetLogFont()
private static LOGFONT GetLogFont()
{
var logFont = new LOGFONT
return new LOGFONT
{
LfHeight = 12,
LfWidth = 0,
@ -374,21 +373,17 @@ namespace UnitTests_PreviewHandlerCommon
LfPitchAndFamily = Convert.ToByte(0),
LfFaceName = "valid-font",
};
return logFont;
}
private RECT GetRectangle(int left, int top, int right, int bottom)
private static RECT GetRectangle(int left, int top, int right, int bottom)
{
var rect = new RECT
return new RECT
{
Left = left,
Top = top,
Right = right,
Bottom = bottom,
};
return rect;
}
private static IPreviewHandlerControl GetMockPreviewHandlerControl()

View File

@ -8,12 +8,12 @@ using Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace UnitTests_PreviewHandlerCommon
namespace PreviewHandlerCommonUnitTests
{
[TestClass]
public class StreamBasedPreviewHandlerTests
{
public class TestStreamBasedPreviewHandler : StreamBasedPreviewHandler
internal class TestStreamBasedPreviewHandler : StreamBasedPreviewHandler
{
public override void DoPreview()
{
@ -29,7 +29,7 @@ namespace UnitTests_PreviewHandlerCommon
[DataTestMethod]
[DataRow(0U)]
[DataRow(1U)]
public void StreamBasedPreviewHandler_ShouldSetStream_WhenInitializeCalled(uint grfMode)
public void StreamBasedPreviewHandlerShouldSetStreamWhenInitializeCalled(uint grfMode)
{
// Arrange
var streamBasedPreviewHandler = new TestStreamBasedPreviewHandler();

View File

@ -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);
}
}
}
}

View File

@ -44,7 +44,7 @@
<ProjectGuid>{748417CA-F17E-487F-9411-CAFB6D3F4877}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>UnitTests_PreviewHandlerCommon</RootNamespace>
<RootNamespace>PreviewHandlerCommonUnitTests</RootNamespace>
<AssemblyName>UnitTests-PreviewHandlerCommon</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
@ -82,7 +82,6 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
@ -98,6 +97,7 @@
</Compile>
<Compile Include="FileBasedPreviewHandlerTests.cs" />
<Compile Include="FormHandlerControlTests.cs" />
<Compile Include="NativeMethods.cs" />
<Compile Include="PreviewHandlerBaseTests.cs" />
<Compile Include="StreamBasedPreviewHandlerTests.cs" />
<Compile Include="StreamWrapperTests.cs" />
@ -114,6 +114,11 @@
</AdditionalFiles>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers">
<Version>3.3.0</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers">
<Version>1.1.118</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@ -6,7 +6,7 @@ using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PreviewHandlerCommon;
namespace UnitTests_PreviewHandlerCommon
namespace PreviewHandlerCommonUnitTests
{
[TestClass]
public class WebBrowserExtUnitTests : WebBrowserExt
@ -14,7 +14,7 @@ namespace UnitTests_PreviewHandlerCommon
private const string DISPIDAMBIENTDLCONTROL = "[DISPID=-5512]";
[TestMethod]
public void InvokeMember_ShouldSetValidFlags_WhenCalledWithValidDispId()
public void InvokeMemberShouldSetValidFlagsWhenCalledWithValidDispId()
{
// Arrange
var extendedSite = CreateWebBrowserSiteBase() as WebBrowserSiteExt;
@ -37,7 +37,7 @@ namespace UnitTests_PreviewHandlerCommon
}
[TestMethod]
public void InvokeMember_ShouldOnlySetValidFlags_WhenCalledWithValidDispId()
public void InvokeMemberShouldOnlySetValidFlagsWhenCalledWithValidDispId()
{
// Arrange
var extendedSite = CreateWebBrowserSiteBase() as WebBrowserSiteExt;