// 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 Common.ComInterlop; using Microsoft.PowerToys.STATestExtension; using Microsoft.PowerToys.ThumbnailHandler.Svg; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; namespace SvgThumbnailProviderUnitTests { [STATestClass] public class SvgThumbnailProviderTests { [TestMethod] public void LoadSimpleSVGShouldReturnNonNullBitmap() { var svgBuilder = new StringBuilder(); svgBuilder.AppendLine(""); svgBuilder.AppendLine("\t"); svgBuilder.AppendLine("\t"); svgBuilder.AppendLine(""); Bitmap thumbnail = SvgThumbnailProvider.GetThumbnail(svgBuilder.ToString(), 256); Assert.IsTrue(thumbnail != null); } [TestMethod] public void CheckBlockedElementsShouldReturnNonNullBitmapIfBlockedElementsIsPresentInNestedLevel() { var svgBuilder = new StringBuilder(); svgBuilder.AppendLine(""); svgBuilder.AppendLine("\t"); svgBuilder.AppendLine("\t\t"); svgBuilder.AppendLine("\t"); svgBuilder.AppendLine(""); Bitmap thumbnail = SvgThumbnailProvider.GetThumbnail(svgBuilder.ToString(), 256); Assert.IsTrue(thumbnail != null); } [TestMethod] public void CheckNoSvgShouldReturnNullBitmap() { var svgBuilder = new StringBuilder(); svgBuilder.AppendLine("

foo

"); Bitmap thumbnail = SvgThumbnailProvider.GetThumbnail(svgBuilder.ToString(), 256); Assert.IsTrue(thumbnail == null); } [TestMethod] public void CheckNoSvgEmptyStringShouldReturnNullBitmap() { Bitmap thumbnail = SvgThumbnailProvider.GetThumbnail(string.Empty, 256); Assert.IsTrue(thumbnail == null); } [TestMethod] public void CheckNoSvgNullStringShouldReturnNullBitmap() { Bitmap thumbnail = SvgThumbnailProvider.GetThumbnail(null, 256); Assert.IsTrue(thumbnail == null); } [TestMethod] public void CheckZeroSizedThumbnailShouldReturnNullBitmap() { string content = ""; Bitmap thumbnail = SvgThumbnailProvider.GetThumbnail(content, 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(""); Bitmap thumbnail = SvgThumbnailProvider.GetThumbnail(svgBuilder.ToString(), 256); Assert.IsTrue(thumbnail != null); } [TestMethod] public void GetThumbnailValidStreamSVG() { var svgBuilder = new StringBuilder(); svgBuilder.AppendLine(""); svgBuilder.AppendLine(""); svgBuilder.AppendLine(""); svgBuilder.AppendLine(""); SvgThumbnailProvider provider = new SvgThumbnailProvider(); provider.Initialize(GetMockStream(svgBuilder.ToString()), 0); IntPtr bitmap; WTS_ALPHATYPE alphaType; provider.GetThumbnail(256, out bitmap, out alphaType); Assert.IsTrue(bitmap != IntPtr.Zero); Assert.IsTrue(alphaType == WTS_ALPHATYPE.WTSAT_RGB); } [TestMethod] public void GetThumbnailValidStreamHTML() { 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 provider = new SvgThumbnailProvider(); provider.Initialize(GetMockStream(svgBuilder.ToString()), 0); IntPtr bitmap; WTS_ALPHATYPE alphaType; provider.GetThumbnail(256, out bitmap, out alphaType); Assert.IsTrue(bitmap != IntPtr.Zero); Assert.IsTrue(alphaType == WTS_ALPHATYPE.WTSAT_RGB); } 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; } } }