/ src / modules / previewpane / UnitTests-PdfPreviewHandler / PdfPreviewHandlerTest.cs
PdfPreviewHandlerTest.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System;
 6  using System.Drawing;
 7  using System.IO;
 8  using System.Runtime.InteropServices;
 9  using System.Runtime.InteropServices.ComTypes;
10  using System.Windows.Forms;
11  
12  using Microsoft.PowerToys.PreviewHandler.Pdf;
13  using Microsoft.VisualStudio.TestTools.UnitTesting;
14  using Moq;
15  
16  namespace PdfPreviewHandlerUnitTests
17  {
18      [STATestClass]
19      [System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2201:Do not raise reserved exception types", Justification = "new Exception() is fine in test projects.")]
20      public class PdfPreviewHandlerTest
21      {
22          [TestMethod]
23          public void PdfPreviewHandlerControlAddsControlsToFormWhenDoPreviewIsCalled()
24          {
25              // Arrange
26              using (var pdfPreviewHandlerControl = new PdfPreviewHandlerControl())
27              {
28                  // Act
29                  var file = File.ReadAllBytes("HelperFiles/sample.pdf");
30  
31                  pdfPreviewHandlerControl.DoPreview<IStream>(GetMockStream(file));
32  
33                  var flowLayoutPanel = pdfPreviewHandlerControl.Controls[0] as FlowLayoutPanel;
34  
35                  // Assert
36                  Assert.AreEqual(1, pdfPreviewHandlerControl.Controls.Count);
37              }
38          }
39  
40          [TestMethod]
41          public void PdfPreviewHandlerControlShouldAddValidInfoBarIfPdfPreviewThrows()
42          {
43              // Arrange
44              using (var pdfPreviewHandlerControl = new PdfPreviewHandlerControl())
45              {
46                  var mockStream = new Mock<IStream>();
47                  mockStream
48                      .Setup(x => x.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
49                      .Throws(new Exception());
50  
51                  // Act
52                  pdfPreviewHandlerControl.DoPreview(mockStream.Object);
53                  var textBox = pdfPreviewHandlerControl.Controls[0] as RichTextBox;
54  
55                  // Assert
56                  Assert.IsFalse(string.IsNullOrWhiteSpace(textBox.Text));
57                  Assert.AreEqual(1, pdfPreviewHandlerControl.Controls.Count);
58                  Assert.AreEqual(DockStyle.Top, textBox.Dock);
59                  Assert.AreEqual(Color.LightYellow, textBox.BackColor);
60                  Assert.IsTrue(textBox.Multiline);
61                  Assert.IsTrue(textBox.ReadOnly);
62                  Assert.AreEqual(RichTextBoxScrollBars.None, textBox.ScrollBars);
63                  Assert.AreEqual(BorderStyle.None, textBox.BorderStyle);
64              }
65          }
66  
67          private static IStream GetMockStream(byte[] sourceArray)
68          {
69              var streamMock = new Mock<IStream>();
70              int bytesRead = 0;
71  
72              streamMock
73                  .Setup(x => x.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
74                  .Callback<byte[], int, IntPtr>((buffer, countToRead, bytesReadPtr) =>
75                  {
76                      int actualCountToRead = Math.Min(sourceArray.Length - bytesRead, countToRead);
77                      if (actualCountToRead > 0)
78                      {
79                          Array.Copy(sourceArray, bytesRead, buffer, 0, actualCountToRead);
80                          Marshal.WriteInt32(bytesReadPtr, actualCountToRead);
81                          bytesRead += actualCountToRead;
82                      }
83                      else
84                      {
85                          Marshal.WriteInt32(bytesReadPtr, 0);
86                      }
87                  });
88  
89              return streamMock.Object;
90          }
91      }
92  }