mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 03:36:44 +02:00
Add info bar if blocked elements present in Svg (#1620)
* Added logic to check for blocked elements * Added unit tests * Fix warnings from msi solution
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
using System;
|
||||
// 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.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
@@ -157,6 +161,68 @@ namespace UnitTests_SvgPreviewHandler
|
||||
Assert.AreEqual(finalParentWidth, textBox.Width);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_ShouldAddTextBox_IfBlockedElementsArePresent()
|
||||
{
|
||||
// Arrange
|
||||
var svgPreviewControl = new SvgPreviewControl();
|
||||
var svgBuilder = new StringBuilder();
|
||||
svgBuilder.AppendLine("<svg width =\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">");
|
||||
svgBuilder.AppendLine("\t<script>alert(\"hello\")</script>");
|
||||
svgBuilder.AppendLine("</svg>");
|
||||
|
||||
// Act
|
||||
svgPreviewControl.DoPreview(GetMockStream(svgBuilder.ToString()));
|
||||
|
||||
// Assert
|
||||
Assert.IsInstanceOfType(svgPreviewControl.Controls[0], typeof(RichTextBox));
|
||||
Assert.IsInstanceOfType(svgPreviewControl.Controls[1], typeof(WebBrowserExt));
|
||||
Assert.AreEqual(svgPreviewControl.Controls.Count, 2);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_ShouldNotAddTextBox_IfNoBlockedElementsArePresent()
|
||||
{
|
||||
// Arrange
|
||||
var svgPreviewControl = new SvgPreviewControl();
|
||||
var svgBuilder = new StringBuilder();
|
||||
svgBuilder.AppendLine("<svg viewBox=\"0 0 100 100\" xmlns=\"http://www.w3.org/2000/svg\">");
|
||||
svgBuilder.AppendLine("\t<circle cx=\"50\" cy=\"50\" r=\"50\">");
|
||||
svgBuilder.AppendLine("\t</circle>");
|
||||
svgBuilder.AppendLine("</svg>");
|
||||
|
||||
// Act
|
||||
svgPreviewControl.DoPreview(GetMockStream(svgBuilder.ToString()));
|
||||
|
||||
// Assert
|
||||
Assert.IsInstanceOfType(svgPreviewControl.Controls[0], typeof(WebBrowserExt));
|
||||
Assert.AreEqual(svgPreviewControl.Controls.Count, 1);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_InfoBarWidthShouldAdjustWithParentControlWidthChanges_IfBlockedElementsArePresent()
|
||||
{
|
||||
// Arrange
|
||||
var svgPreviewControl = new SvgPreviewControl();
|
||||
var svgBuilder = new StringBuilder();
|
||||
svgBuilder.AppendLine("<svg width =\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">");
|
||||
svgBuilder.AppendLine("\t<script>alert(\"hello\")</script>");
|
||||
svgBuilder.AppendLine("</svg>");
|
||||
svgPreviewControl.DoPreview(GetMockStream(svgBuilder.ToString()));
|
||||
var textBox = svgPreviewControl.Controls[0] as RichTextBox;
|
||||
var incrementParentControlWidth = 5;
|
||||
var intialParentWidth = svgPreviewControl.Width;
|
||||
var intitialTextBoxWidth = textBox.Width;
|
||||
var finalParentWidth = intialParentWidth + incrementParentControlWidth;
|
||||
|
||||
// Act
|
||||
svgPreviewControl.Width += incrementParentControlWidth;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(intialParentWidth, intitialTextBoxWidth);
|
||||
Assert.AreEqual(finalParentWidth, textBox.Width);
|
||||
}
|
||||
|
||||
private IStream GetMockStream(string streamData)
|
||||
{
|
||||
var mockStream = new Mock<IStream>();
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
// 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 Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using SvgPreviewHandler.Utilities;
|
||||
using System.Text;
|
||||
|
||||
namespace UnitTests_SvgPreviewHandler
|
||||
{
|
||||
[TestClass]
|
||||
public class SvgPreviewHandlerHelperTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void CheckBlockedElements_ShoudReturnTrue_IfABlockedElementIsPresent()
|
||||
{
|
||||
// Arrange
|
||||
var svgBuilder = new StringBuilder();
|
||||
svgBuilder.AppendLine("<svg width =\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">");
|
||||
svgBuilder.AppendLine("\t<script>alert(\"hello\")</script>");
|
||||
svgBuilder.AppendLine("</svg>");
|
||||
bool foundFilteredElement;
|
||||
|
||||
// Act
|
||||
foundFilteredElement = SvgPreviewHandlerHelper.CheckBlockedElements(svgBuilder.ToString());
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(foundFilteredElement);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CheckBlockedElements_ShoudReturnTrue_IfBlockedElementsIsPresentInNestedLevel()
|
||||
{
|
||||
// Arrange
|
||||
var svgBuilder = new StringBuilder();
|
||||
svgBuilder.AppendLine("<svg viewBox=\"0 0 100 100\" xmlns=\"http://www.w3.org/2000/svg\">");
|
||||
svgBuilder.AppendLine("\t<circle cx=\"50\" cy=\"50\" r=\"50\">");
|
||||
svgBuilder.AppendLine("\t\t<script>alert(\"valid-message\")</script>");
|
||||
svgBuilder.AppendLine("\t</circle>");
|
||||
svgBuilder.AppendLine("</svg>");
|
||||
bool foundFilteredElement;
|
||||
|
||||
// Act
|
||||
foundFilteredElement = SvgPreviewHandlerHelper.CheckBlockedElements(svgBuilder.ToString());
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(foundFilteredElement);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CheckBlockedElements_ShoudReturnTrue_IfMultipleBlockedElementsArePresent()
|
||||
{
|
||||
// Arrange
|
||||
var svgBuilder = new StringBuilder();
|
||||
svgBuilder.AppendLine("<svg width =\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">");
|
||||
svgBuilder.AppendLine("\t<script>alert(\"valid-message\")</script>");
|
||||
svgBuilder.AppendLine("\t<image href=\"valid-url\" height=\"200\" width=\"200\"/>");
|
||||
svgBuilder.AppendLine("</svg>");
|
||||
bool foundFilteredElement;
|
||||
|
||||
// Act
|
||||
foundFilteredElement = SvgPreviewHandlerHelper.CheckBlockedElements(svgBuilder.ToString());
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(foundFilteredElement);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CheckBlockedElements_ShoudReturnFalse_IfNoBlockedElementsArePresent()
|
||||
{
|
||||
// Arrange
|
||||
var svgBuilder = new StringBuilder();
|
||||
svgBuilder.AppendLine("<svg viewBox=\"0 0 100 100\" xmlns=\"http://www.w3.org/2000/svg\">");
|
||||
svgBuilder.AppendLine("\t<circle cx=\"50\" cy=\"50\" r=\"50\">");
|
||||
svgBuilder.AppendLine("\t</circle>");
|
||||
svgBuilder.AppendLine("</svg>");
|
||||
bool foundFilteredElement;
|
||||
|
||||
// Act
|
||||
foundFilteredElement = SvgPreviewHandlerHelper.CheckBlockedElements(svgBuilder.ToString());
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(foundFilteredElement);
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow("")]
|
||||
[DataRow(" ")]
|
||||
[DataRow(null)]
|
||||
public void CheckBlockedElements_ShoudReturnFalse_IfSvgDataIsNullOrWhiteSpaces(string svgData)
|
||||
{
|
||||
// Arrange
|
||||
bool foundFilteredElement;
|
||||
|
||||
// Act
|
||||
foundFilteredElement = SvgPreviewHandlerHelper.CheckBlockedElements(svgData);
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(foundFilteredElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,6 +89,7 @@
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="SvgPreviewControlTests.cs" />
|
||||
<Compile Include="SvgPreviewHandlerHelperTests.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Moq">
|
||||
|
||||
Reference in New Issue
Block a user