Skip to content

Commit

Permalink
Add Svg preview handler (microsoft#1129)
Browse files Browse the repository at this point in the history
* Added a new project for Svg preview handler

* Added initial implementation of Svg Preview Handler

* Fixed output path

* Added Unit Test Project

* Added StreamWrapper and Update Svg Control

* Updated Svg Handler Guid

* Removed migration backup folder

* Removed Fluent Assertions NuGet

* Added Comments for StreamWrapper

* Removed the manual GC collect

* Added unit tests for Svg preview Handler

* Updated the xml doc for stream wrapper
  • Loading branch information
udit3333 committed Feb 19, 2020
1 parent 45e50cf commit 782c97a
Show file tree
Hide file tree
Showing 12 changed files with 662 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SvgPreviewHandler")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SvgPreviewHandler")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("da425894-6e13-404f-8dcb-78584ec0557a")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
55 changes: 55 additions & 0 deletions src/modules/previewpane/SvgPreviewHandler/SvgPreviewControl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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.IO;
using System.Runtime.InteropServices.ComTypes;
using System.Windows.Forms;
using Common;
using Common.Utilities;

namespace SvgPreviewHandler
{
/// <summary>
/// Implementation of Control for Svg Preview Handler.
/// </summary>
public class SvgPreviewControl : FormHandlerControl
{
private Stream dataSourceStream;

/// <summary>
/// Start the preview on the Control.
/// </summary>
/// <param name="dataSource">Stream reference to access source file.</param>
public override void DoPreview<T>(T dataSource)
{
this.InvokeOnControlThread(() =>
{
WebBrowser browser = new WebBrowser();
this.dataSourceStream = new StreamWrapper(dataSource as IStream);
browser.DocumentStream = this.dataSourceStream;
browser.Dock = DockStyle.Fill;
browser.IsWebBrowserContextMenuEnabled = false;
browser.ScriptErrorsSuppressed = true;
browser.ScrollBarsEnabled = true;
this.Controls.Add(browser);
base.DoPreview(dataSource);
});
}

/// <summary>
/// Free resources on the unload of Preview.
/// </summary>
public override void Unload()
{
base.Unload();
if (this.dataSourceStream != null)
{
this.dataSourceStream.Dispose();
this.dataSourceStream = null;
}
}
}
}
36 changes: 36 additions & 0 deletions src/modules/previewpane/SvgPreviewHandler/SvgPreviewHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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;
using Common;

namespace SvgPreviewHandler
{
/// <summary>
/// Extends <see cref="StreamBasedPreviewHandler"/> for Svg Preview Handler.
/// </summary>
[PreviewHandler("SvgPreviewHandler", ".svg", "{88235ab2-bfce-4be8-9ed0-0408cd8da792}")]
[ProgId("SvgPreviewHandler")]
[Guid("ddee2b8a-6807-48a6-bb20-2338174ff779")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
public class SvgPreviewHandler : StreamBasedPreviewHandler
{
private SvgPreviewControl svgPreviewControl;

/// <inheritdoc/>
public override void DoPreview()
{
this.svgPreviewControl.DoPreview(this.Stream);
}

/// <inheritdoc/>
protected override IPreviewHandlerControl CreatePreviewHandlerControl()
{
this.svgPreviewControl = new SvgPreviewControl();
return this.svgPreviewControl;
}
}
}
85 changes: 85 additions & 0 deletions src/modules/previewpane/SvgPreviewHandler/SvgPreviewHandler.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{DA425894-6E13-404F-8DCB-78584EC0557A}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SvgPreviewHandler</RootNamespace>
<AssemblyName>SvgPreviewHandler</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>SvgPreviewHandler.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DocumentationFile>bin\Debug\SvgPreviewHandler.xml</DocumentationFile>
<WarningLevel>2</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<DocumentationFile>bin\Release\SvgPreviewHandler.xml</DocumentationFile>
<Optimize>true</Optimize>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="SvgPreviewControl.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="SvgPreviewHandler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\common\Common.csproj">
<Project>{af2349b8-e5b6-4004-9502-687c1c7730b1}</Project>
<Name>Common</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="..\..\..\codeAnalysis\StyleCop.json">
<Link>StyleCop.json</Link>
</AdditionalFiles>
<None Include="SvgPreviewHandler.snk" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="StyleCop.Analyzers">
<Version>1.1.118</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("UnitTests-SvgPreviewHandler")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("UnitTests-SvgPreviewHandler")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

[assembly: ComVisible(false)]

[assembly: Guid("060d75da-2d1c-48e6-a4a1-6f0718b64661")]

// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SvgPreviewHandler;

namespace UnitTests_SvgPreviewHandler
{
[TestClass]
public class SvgPreviewControlTests
{
[TestMethod]
public void SvgPreviewControl_ShouldAddBrowserControl_WhenDoPreviewCalled()
{
// Arrange
var svgPreviewControl = new SvgPreviewControl();
var mockStream = new Mock<IStream>();

// Act
svgPreviewControl.DoPreview(mockStream.Object);

// Assert
Assert.AreEqual(svgPreviewControl.Controls.Count, 1);
Assert.IsInstanceOfType(svgPreviewControl.Controls[0], typeof(WebBrowser));
}

[TestMethod]
public void SvgPreviewControl_ShouldSetDocumentStream_WhenDoPreviewCalled()
{
// Arrange
var svgPreviewControl = new SvgPreviewControl();
var mockStream = new Mock<IStream>();

// Act
svgPreviewControl.DoPreview(mockStream.Object);

// Assert
Assert.IsNotNull(((WebBrowser)svgPreviewControl.Controls[0]).DocumentStream);
}

[TestMethod]
public void SvgPreviewControl_ShouldDisableWebBrowserContextMenu_WhenDoPreviewCalled()
{
// Arrange
var svgPreviewControl = new SvgPreviewControl();
var mockStream = new Mock<IStream>();

// Act
svgPreviewControl.DoPreview(mockStream.Object);

// Assert
Assert.AreEqual(((WebBrowser)svgPreviewControl.Controls[0]).IsWebBrowserContextMenuEnabled, false);
}

[TestMethod]
public void SvgPreviewControl_ShouldFillDockForWebBrowser_WhenDoPreviewCalled()
{
// Arrange
var svgPreviewControl = new SvgPreviewControl();
var mockStream = new Mock<IStream>();

// Act
svgPreviewControl.DoPreview(mockStream.Object);

// Assert
Assert.AreEqual(((WebBrowser)svgPreviewControl.Controls[0]).Dock, DockStyle.Fill);
}

[TestMethod]
public void SvgPreviewControl_ShouldSetScriptErrorsSuppressedProperty_WhenDoPreviewCalled()
{
// Arrange
var svgPreviewControl = new SvgPreviewControl();
var mockStream = new Mock<IStream>();

// Act
svgPreviewControl.DoPreview(mockStream.Object);

// Assert
Assert.AreEqual(((WebBrowser)svgPreviewControl.Controls[0]).ScriptErrorsSuppressed, true);
}

[TestMethod]
public void SvgPreviewControl_ShouldSetScrollBarsEnabledProperty_WhenDoPreviewCalled()
{
// Arrange
var svgPreviewControl = new SvgPreviewControl();
var mockStream = new Mock<IStream>();

// Act
svgPreviewControl.DoPreview(mockStream.Object);

// Assert
Assert.AreEqual(((WebBrowser)svgPreviewControl.Controls[0]).ScrollBarsEnabled, true);
}
}
}
Loading

0 comments on commit 782c97a

Please sign in to comment.