// 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.Drawing.Imaging; using System.Runtime.InteropServices; using System.Text; using Microsoft.PowerToys.STATestExtension; using Microsoft.PowerToys.ThumbnailHandler.Svg; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace SvgThumbnailProviderUnitTests { [STATestClass] public class SvgThumbnailProviderTests { [TestMethod] public void LoadSimpleSVGShouldReturnNonNullBitmap() { var svgBuilder = new StringBuilder(); svgBuilder.AppendLine(""); svgBuilder.AppendLine("\t"); svgBuilder.AppendLine("\t"); svgBuilder.AppendLine(""); SvgThumbnailProvider svgThumbnailProvider = new SvgThumbnailProvider(null); svgThumbnailProvider.SvgContents = svgBuilder.ToString(); svgThumbnailProvider.SvgContentsReady.Set(); Bitmap thumbnail = svgThumbnailProvider.GetThumbnail(256); Assert.IsNotNull(thumbnail); Assert.IsTrue(thumbnail.Width > 0); Assert.IsTrue(thumbnail.Height > 0); } [TestMethod] public void CheckBlockedElementsShouldReturnNonNullBitmapIfBlockedElementsIsPresentInNestedLevel() { var svgBuilder = new StringBuilder(); svgBuilder.AppendLine(""); svgBuilder.AppendLine("\t"); svgBuilder.AppendLine("\t\t"); svgBuilder.AppendLine("\t"); svgBuilder.AppendLine(""); SvgThumbnailProvider svgThumbnailProvider = new SvgThumbnailProvider(null); svgThumbnailProvider.SvgContents = svgBuilder.ToString(); svgThumbnailProvider.SvgContentsReady.Set(); Bitmap thumbnail = svgThumbnailProvider.GetThumbnail(256); Assert.IsTrue(thumbnail != null); } [TestMethod] public void CheckThatWidthAndHeightAreParsedCorrectly1() { SvgThumbnailProvider svgThumbnailProvider = new SvgThumbnailProvider(null); svgThumbnailProvider.SvgContents = @" "; svgThumbnailProvider.SvgContentsReady.Set(); Bitmap thumbnail = svgThumbnailProvider.GetThumbnail(256); Assert.IsTrue(thumbnail != null); } [TestMethod] public void CheckThatWidthAndHeightAreParsedCorrectly2() { SvgThumbnailProvider svgThumbnailProvider = new SvgThumbnailProvider(null); svgThumbnailProvider.SvgContents = @" "; svgThumbnailProvider.SvgContentsReady.Set(); Bitmap thumbnail = svgThumbnailProvider.GetThumbnail(256); Assert.IsTrue(thumbnail != null); } [TestMethod] public void CheckThatWidthAndHeightAreParsedCorrectly3() { SvgThumbnailProvider svgThumbnailProvider = new SvgThumbnailProvider(null); svgThumbnailProvider.SvgContents = @" "; svgThumbnailProvider.SvgContentsReady.Set(); Bitmap thumbnail = svgThumbnailProvider.GetThumbnail(256); Assert.IsTrue(thumbnail != null); } [TestMethod] public void CheckNoSvgShouldReturnNullBitmap() { var svgBuilder = new StringBuilder(); svgBuilder.AppendLine("

foo

"); SvgThumbnailProvider svgThumbnailProvider = new SvgThumbnailProvider(null); svgThumbnailProvider.SvgContents = svgBuilder.ToString(); svgThumbnailProvider.SvgContentsReady.Set(); Bitmap thumbnail = svgThumbnailProvider.GetThumbnail(256); Assert.IsTrue(thumbnail == null); } [TestMethod] public void CheckNoSvgEmptyStringShouldReturnNullBitmap() { SvgThumbnailProvider svgThumbnailProvider = new SvgThumbnailProvider(null); svgThumbnailProvider.SvgContents = string.Empty; svgThumbnailProvider.SvgContentsReady.Set(); Bitmap thumbnail = svgThumbnailProvider.GetThumbnail(256); Assert.IsTrue(thumbnail == null); } [TestMethod] public void CheckNoSvgNullStringShouldReturnNullBitmap() { SvgThumbnailProvider svgThumbnailProvider = new SvgThumbnailProvider(null); svgThumbnailProvider.SvgContents = string.Empty; svgThumbnailProvider.SvgContentsReady.Set(); Bitmap thumbnail = svgThumbnailProvider.GetThumbnail(256); Assert.IsTrue(thumbnail == null); } [TestMethod] public void CheckZeroSizedThumbnailShouldReturnNullBitmap() { string content = ""; SvgThumbnailProvider svgThumbnailProvider = new SvgThumbnailProvider(null); svgThumbnailProvider.SvgContents = content; svgThumbnailProvider.SvgContentsReady.Set(); Bitmap thumbnail = svgThumbnailProvider.GetThumbnail(0); Assert.IsTrue(thumbnail == null); } [TestMethod] public void CheckBlockedElementsShouldReturnBitmapHTMLWrapped() { var svgBuilder = new StringBuilder(); svgBuilder.AppendLine(""); svgBuilder.AppendLine(""); svgBuilder.AppendLine(""); svgBuilder.AppendLine(""); svgBuilder.AppendLine(""); svgBuilder.AppendLine(""); svgBuilder.AppendLine(""); svgBuilder.AppendLine(""); svgBuilder.AppendLine(""); svgBuilder.AppendLine(""); svgBuilder.AppendLine(""); svgBuilder.AppendLine(""); SvgThumbnailProvider svgThumbnailProvider = new SvgThumbnailProvider(null); svgThumbnailProvider.SvgContents = svgBuilder.ToString(); svgThumbnailProvider.SvgContentsReady.Set(); Bitmap thumbnail = svgThumbnailProvider.GetThumbnail(256); Assert.IsTrue(thumbnail != null); } [TestMethod] public void GetThumbnailValidStreamSVG() { var filePath = "HelperFiles/file1.svg"; SvgThumbnailProvider svgThumbnailProvider = new SvgThumbnailProvider(filePath); Bitmap bitmap = svgThumbnailProvider.GetThumbnail(256); Assert.IsTrue(bitmap != null); } [TestMethod] public void GetThumbnailValidStreamHTML() { var filePath = "HelperFiles/file2.svg"; SvgThumbnailProvider svgThumbnailProvider = new SvgThumbnailProvider(filePath); Bitmap bitmap = svgThumbnailProvider.GetThumbnail(256); Assert.IsTrue(bitmap != null); } [TestMethod] public void SvgCommentsAreHandledCorrectly() { var filePath = "HelperFiles/WithComments.svg"; SvgThumbnailProvider svgThumbnailProvider = new SvgThumbnailProvider(filePath); Bitmap bitmap = svgThumbnailProvider.GetThumbnail(8); Assert.IsTrue(bitmap != null); } } }