Enable PDF files in preview pane (#9088)

## Summary of the Pull Request
This PR enables user to preview PDF files in the Explorer preview pane
and in Outlook. 

**What is this about:**
Windows does not support out of the box experience for previewing PDF
files in the preview pane. Users need to install third-party software
like Adobe Acrobat reader. The PdfPreviewHandler module enbales the user
to preview PDF files.

**How does someone test / validate:** 
Run the installer, open Explorer and select a PDF file, enable the
preview pane. Maybe need to remove third-party PDF software.

## Quality Checklist

- [X] **Linked issue:** #3548
- [ ] **Communication:** I've discussed this with core contributors in the issue. 
- [X] **Tests:** Added/updated and all pass
- [X] **Installer:** Added/updated and all pass
- [X] **Localization:** All end user facing strings can be localized
- [ ] **Docs:** Added/ updated
- [x] **Binaries:** Any new files are added to WXS / YML
   - [ ] No new binaries
   - [x] YML for signing
   - [x] WXS for installer
This commit is contained in:
R. de Veen
2021-08-26 23:43:26 +02:00
committed by GitHub
parent da46b90457
commit 4177708e49
34 changed files with 1073 additions and 14 deletions

View File

@@ -0,0 +1,89 @@
// 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;
using System.Runtime.InteropServices.ComTypes;
using System.Windows.Forms;
using Microsoft.PowerToys.PreviewHandler.Pdf;
using Microsoft.PowerToys.STATestExtension;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace PdfPreviewHandlerUnitTests
{
[STATestClass]
public class PdfPreviewHandlerTest
{
[TestMethod]
public void PdfPreviewHandlerControlAddsControlsToFormWhenDoPreviewIsCalled()
{
// Arrange
using (var pdfPreviewHandlerControl = new PdfPreviewHandlerControl())
{
// Act
var file = File.ReadAllBytes("HelperFiles/sample.pdf");
pdfPreviewHandlerControl.DoPreview<IStream>(GetMockStream(file));
var flowLayoutPanel = pdfPreviewHandlerControl.Controls[0] as FlowLayoutPanel;
// Assert
Assert.AreEqual(1, pdfPreviewHandlerControl.Controls.Count);
}
}
[TestMethod]
public void PdfPreviewHandlerControlShouldAddValidInfoBarIfPdfPreviewThrows()
{
// Arrange
using (var pdfPreviewHandlerControl = new PdfPreviewHandlerControl())
{
var mockStream = new Mock<IStream>();
mockStream
.Setup(x => x.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
.Throws(new Exception());
// Act
pdfPreviewHandlerControl.DoPreview(mockStream.Object);
var textBox = pdfPreviewHandlerControl.Controls[0] as RichTextBox;
// Assert
Assert.IsFalse(string.IsNullOrWhiteSpace(textBox.Text));
Assert.AreEqual(1, pdfPreviewHandlerControl.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);
}
}
private static IStream GetMockStream(byte[] sourceArray)
{
var streamMock = new Mock<IStream>();
var firstCall = true;
streamMock
.Setup(x => x.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
.Callback<byte[], int, IntPtr>((buffer, countToRead, bytesReadPtr) =>
{
if (firstCall)
{
Array.Copy(sourceArray, 0, buffer, 0, sourceArray.Length);
Marshal.WriteInt32(bytesReadPtr, sourceArray.Length);
firstCall = false;
}
else
{
Marshal.WriteInt32(bytesReadPtr, 0);
}
});
return streamMock.Object;
}
}
}

View File

@@ -0,0 +1,67 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Platforms>x64</Platforms>
<AssemblyTitle>UnitTests-PdfPreviewHandler</AssemblyTitle>
<AssemblyDescription>PowerToys UnitTests-PdfPreviewHandler</AssemblyDescription>
<AssemblyCompany>Microsoft Corp.</AssemblyCompany>
<AssemblyCopyright>Copyright (C) 2020 Microsoft Corp.</AssemblyCopyright>
<AssemblyProduct>PowerToys</AssemblyProduct>
<AssemblyTitle>UnitTests-PdfPreviewHandler</AssemblyTitle>
<Company>Microsoft Corp.</Company>
<Product>PowerToys</Product>
<NeutralLanguage>en-US</NeutralLanguage>
<Description>PowerToys UnitTests-PdfPreviewHandler</Description>
<Copyright>Copyright (C) 2020 Microsoft Corp.</Copyright>
</PropertyGroup>
<PropertyGroup>
<ProjectGuid>{ECC20689-002A-4354-95A6-B58DF089C6FF}</ProjectGuid>
<RootNamespace>PreviewPaneUnitTests</RootNamespace>
<AssemblyName>PreviewPaneUnitTests</AssemblyName>
<TargetFramework>netcoreapp3.1</TargetFramework>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
</PropertyGroup>
<Import Project="..\..\..\Version.props" />
<ItemGroup>
<None Remove="HelperFiles\dummy-password.pdf" />
<None Remove="HelperFiles\dummy.pdf" />
<None Remove="HelperFiles\sample.pdf" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Moq" Version="4.14.4" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\common\PreviewHandlerCommon.csproj" />
<ProjectReference Include="..\PdfPreviewHandler\PdfPreviewHandler.csproj" />
</ItemGroup>
<ItemGroup>
<Content Include="..\..\..\codeAnalysis\GlobalSuppressions.cs" Link="GlobalSuppressions.cs" />
<Content Include="HelperFiles\sample.pdf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Compile Include="..\STATestClassAttribute.cs" Link="STATestClassAttribute.cs" />
<Compile Include="..\STATestMethodAttribute.cs" Link="STATestMethodAttribute.cs" />
<AdditionalFiles Include="..\..\..\codeAnalysis\StyleCop.json">
<Link>StyleCop.json</Link>
</AdditionalFiles>
</ItemGroup>
<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>