[FileExplorer]Add QoiThumbnailProvider and QoiPreviewHandler (#29735)

* [FileExplorer]QoiThumbnailProvider

* Corrects code documentation

* Adds QoiPreviewHandler

* Corrects GUIDs

* Ensure returned thumbnail image is 32bit ARGB

* Updates following review comments

* More updates following review comments

* Updates following PR comments

* Fix dark theme background in QoiPreviewHandler

* Updates attribution text
This commit is contained in:
Pedro Lamas
2023-11-14 15:41:09 +00:00
committed by GitHub
parent 9d2f9bcff2
commit 0990724e44
70 changed files with 2970 additions and 8 deletions

View File

@@ -0,0 +1,92 @@
// 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.Qoi;
using Microsoft.PowerToys.STATestExtension;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace QoiPreviewHandlerUnitTests
{
[STATestClass]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2201:Do not raise reserved exception types", Justification = "new Exception() is fine in test projects.")]
public class QoiPreviewHandlerTest
{
[TestMethod]
public void QoiPreviewHandlerControlAddsControlsToFormWhenDoPreviewIsCalled()
{
// Arrange
using (var qoiPreviewHandlerControl = new QoiPreviewHandlerControl())
{
// Act
var file = File.ReadAllBytes("HelperFiles/sample.qoi");
qoiPreviewHandlerControl.DoPreview<IStream>(GetMockStream(file));
var flowLayoutPanel = qoiPreviewHandlerControl.Controls[0] as FlowLayoutPanel;
// Assert
Assert.AreEqual(1, qoiPreviewHandlerControl.Controls.Count);
}
}
[TestMethod]
public void QoiPreviewHandlerControlShouldAddValidInfoBarIfQoiPreviewThrows()
{
// Arrange
using (var qoiPreviewHandlerControl = new QoiPreviewHandlerControl())
{
var mockStream = new Mock<IStream>();
mockStream
.Setup(x => x.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
.Throws(new Exception());
// Act
qoiPreviewHandlerControl.DoPreview(mockStream.Object);
var textBox = qoiPreviewHandlerControl.Controls[0] as RichTextBox;
// Assert
Assert.IsFalse(string.IsNullOrWhiteSpace(textBox.Text));
Assert.AreEqual(1, qoiPreviewHandlerControl.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>();
int bytesRead = 0;
streamMock
.Setup(x => x.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
.Callback<byte[], int, IntPtr>((buffer, countToRead, bytesReadPtr) =>
{
int actualCountToRead = Math.Min(sourceArray.Length - bytesRead, countToRead);
if (actualCountToRead > 0)
{
Array.Copy(sourceArray, bytesRead, buffer, 0, actualCountToRead);
Marshal.WriteInt32(bytesReadPtr, actualCountToRead);
bytesRead += actualCountToRead;
}
else
{
Marshal.WriteInt32(bytesReadPtr, 0);
}
});
return streamMock.Object;
}
}
}

View File

@@ -0,0 +1,47 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyTitle>UnitTests-QoiPreviewHandler</AssemblyTitle>
<AssemblyDescription>PowerToys UnitTests-QoiPreviewHandler</AssemblyDescription>
<AssemblyTitle>UnitTests-QoiPreviewHandler</AssemblyTitle>
<Description>PowerToys UnitTests-QoiPreviewHandler</Description>
</PropertyGroup>
<PropertyGroup>
<ProjectGuid>{3940AD4D-F748-4BE4-9083-85769CD553EF}</ProjectGuid>
<RootNamespace>PdfPreviewHandlerUnitTests</RootNamespace>
<TargetFramework>net7.0-windows10.0.20348.0</TargetFramework>
<TargetPlatformMinVersion>10.0.19041.0</TargetPlatformMinVersion>
<SupportedOSPlatformVersion>10.0.19041.0</SupportedOSPlatformVersion>
<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\sample.qoi" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Windows.CsWinRT" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Moq" />
<PackageReference Include="MSTest.TestAdapter" />
<PackageReference Include="MSTest.TestFramework" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\common\PreviewHandlerCommon.csproj" />
<ProjectReference Include="..\QoiPreviewHandler\QoiPreviewHandler.csproj" />
</ItemGroup>
<ItemGroup>
<Content Include="HelperFiles\sample.qoi">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Compile Include="..\STATestClassAttribute.cs" Link="STATestClassAttribute.cs" />
<Compile Include="..\STATestMethodAttribute.cs" Link="STATestMethodAttribute.cs" />
</ItemGroup>
</Project>