// 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.Drawing;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Windows.Forms;
using Microsoft.PowerToys.PreviewHandler.Svg;
using Microsoft.PowerToys.STATestExtension;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using PreviewHandlerCommon;
namespace SvgPreviewHandlerUnitTests
{
[STATestClass]
public class SvgPreviewControlTests
{
[TestMethod]
public void SvgPreviewControlShouldAddExtendedBrowserControlWhenDoPreviewCalled()
{
// Arrange
using (var svgPreviewControl = new SvgPreviewControl())
{
// Act
svgPreviewControl.DoPreview(GetMockStream(""));
// Assert
Assert.AreEqual(1, svgPreviewControl.Controls.Count);
Assert.IsInstanceOfType(svgPreviewControl.Controls[0], typeof(WebBrowserExt));
}
}
[TestMethod]
public void SvgPreviewControlShouldSetDocumentStreamWhenDoPreviewCalled()
{
// Arrange
using (var svgPreviewControl = new SvgPreviewControl())
{
// Act
svgPreviewControl.DoPreview(GetMockStream(""));
// Assert
Assert.IsNotNull(((WebBrowser)svgPreviewControl.Controls[0]).DocumentStream);
}
}
[TestMethod]
public void SvgPreviewControlShouldDisableWebBrowserContextMenuWhenDoPreviewCalled()
{
// Arrange
using (var svgPreviewControl = new SvgPreviewControl())
{
// Act
svgPreviewControl.DoPreview(GetMockStream(""));
// Assert
Assert.AreEqual(false, ((WebBrowser)svgPreviewControl.Controls[0]).IsWebBrowserContextMenuEnabled);
}
}
[TestMethod]
public void SvgPreviewControlShouldFillDockForWebBrowserWhenDoPreviewCalled()
{
// Arrange
using (var svgPreviewControl = new SvgPreviewControl())
{
// Act
svgPreviewControl.DoPreview(GetMockStream(""));
// Assert
Assert.AreEqual(DockStyle.Fill, ((WebBrowser)svgPreviewControl.Controls[0]).Dock);
}
}
[TestMethod]
public void SvgPreviewControlShouldSetScriptErrorsSuppressedPropertyWhenDoPreviewCalled()
{
// Arrange
using (var svgPreviewControl = new SvgPreviewControl())
{
// Act
svgPreviewControl.DoPreview(GetMockStream(""));
// Assert
Assert.AreEqual(true, ((WebBrowser)svgPreviewControl.Controls[0]).ScriptErrorsSuppressed);
}
}
[TestMethod]
public void SvgPreviewControlShouldSetScrollBarsEnabledPropertyWhenDoPreviewCalled()
{
// Arrange
using (var svgPreviewControl = new SvgPreviewControl())
{
// Act
svgPreviewControl.DoPreview(GetMockStream(""));
// Assert
Assert.AreEqual(true, ((WebBrowser)svgPreviewControl.Controls[0]).ScrollBarsEnabled);
}
}
[TestMethod]
public void SvgPreviewControlShouldDisableAllowNavigationWhenDoPreviewCalled()
{
// Arrange
using (var svgPreviewControl = new SvgPreviewControl())
{
// Act
svgPreviewControl.DoPreview(GetMockStream(""));
// Assert
Assert.AreEqual(false, ((WebBrowser)svgPreviewControl.Controls[0]).AllowNavigation);
}
}
[TestMethod]
public void SvgPreviewControlShouldAddValidInfoBarIfSvgPreviewThrows()
{
// Arrange
using (var svgPreviewControl = new SvgPreviewControl())
{
var mockStream = new Mock();
mockStream
.Setup(x => x.Read(It.IsAny(), It.IsAny(), It.IsAny()))
.Throws(new Exception());
// Act
svgPreviewControl.DoPreview(mockStream.Object);
var textBox = svgPreviewControl.Controls[0] as RichTextBox;
// Assert
Assert.IsFalse(string.IsNullOrWhiteSpace(textBox.Text));
Assert.AreEqual(1, svgPreviewControl.Controls.Count);
Assert.AreEqual(DockStyle.Top, textBox.Dock);
Assert.AreEqual(Color.LightYellow, textBox.BackColor);
Assert.IsTrue(textBox.Multiline);
Assert.IsTrue(textBox.ReadOnly);
Assert.AreEqual(RichTextBoxScrollBars.None, textBox.ScrollBars);
Assert.AreEqual(BorderStyle.None, textBox.BorderStyle);
}
}
[TestMethod]
public void SvgPreviewControlInfoBarWidthShouldAdjustWithParentControlWidthChangesIfSvgPreviewThrows()
{
// Arrange
using (var svgPreviewControl = new SvgPreviewControl())
{
var mockStream = new Mock();
mockStream
.Setup(x => x.Read(It.IsAny(), It.IsAny(), It.IsAny()))
.Throws(new Exception());
svgPreviewControl.DoPreview(mockStream.Object);
var textBox = svgPreviewControl.Controls[0] as RichTextBox;
var incrementParentControlWidth = 5;
var initialParentWidth = svgPreviewControl.Width;
var initialTextBoxWidth = textBox.Width;
var finalParentWidth = initialParentWidth + incrementParentControlWidth;
// Act
svgPreviewControl.Width += incrementParentControlWidth;
// Assert
Assert.AreEqual(initialTextBoxWidth, initialParentWidth);
Assert.AreEqual(textBox.Width, finalParentWidth);
}
}
[TestMethod]
public void SvgPreviewControlShouldAddTextBoxIfBlockedElementsArePresent()
{
// Arrange
using (var svgPreviewControl = new SvgPreviewControl())
{
var svgBuilder = new StringBuilder();
svgBuilder.AppendLine("");
// Act
svgPreviewControl.DoPreview(GetMockStream(svgBuilder.ToString()));
// Assert
Assert.IsInstanceOfType(svgPreviewControl.Controls[0], typeof(RichTextBox));
Assert.IsInstanceOfType(svgPreviewControl.Controls[1], typeof(WebBrowserExt));
Assert.AreEqual(2, svgPreviewControl.Controls.Count);
}
}
[TestMethod]
public void SvgPreviewControlShouldNotAddTextBoxIfNoBlockedElementsArePresent()
{
// Arrange
using (var svgPreviewControl = new SvgPreviewControl())
{
var svgBuilder = new StringBuilder();
svgBuilder.AppendLine("");
// Act
svgPreviewControl.DoPreview(GetMockStream(svgBuilder.ToString()));
// Assert
Assert.IsInstanceOfType(svgPreviewControl.Controls[0], typeof(WebBrowserExt));
Assert.AreEqual(1, svgPreviewControl.Controls.Count);
}
}
[TestMethod]
public void SvgPreviewControlInfoBarWidthShouldAdjustWithParentControlWidthChangesIfBlockedElementsArePresent()
{
// Arrange
using (var svgPreviewControl = new SvgPreviewControl())
{
var svgBuilder = new StringBuilder();
svgBuilder.AppendLine("");
svgPreviewControl.DoPreview(GetMockStream(svgBuilder.ToString()));
var textBox = svgPreviewControl.Controls[0] as RichTextBox;
var incrementParentControlWidth = 5;
var initialParentWidth = svgPreviewControl.Width;
var initialTextBoxWidth = textBox.Width;
var finalParentWidth = initialParentWidth + incrementParentControlWidth;
// Act
svgPreviewControl.Width += incrementParentControlWidth;
// Assert
Assert.AreEqual(initialTextBoxWidth, initialParentWidth);
Assert.AreEqual(textBox.Width, finalParentWidth);
}
}
private static IStream GetMockStream(string streamData)
{
var mockStream = new Mock();
var streamBytes = Encoding.UTF8.GetBytes(streamData);
var streamMock = new Mock();
var firstCall = true;
streamMock
.Setup(x => x.Read(It.IsAny(), It.IsAny(), It.IsAny()))
.Callback((buffer, countToRead, bytesReadPtr) =>
{
if (firstCall)
{
Array.Copy(streamBytes, 0, buffer, 0, streamBytes.Length);
Marshal.WriteInt32(bytesReadPtr, streamBytes.Length);
firstCall = false;
}
else
{
Marshal.WriteInt32(bytesReadPtr, 0);
}
});
return streamMock.Object;
}
}
}