BgcodePreviewHandlerTest.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.Bgcode; 13 using Microsoft.VisualStudio.TestTools.UnitTesting; 14 using Moq; 15 16 namespace BgcodePreviewHandlerUnitTests 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 BgcodePreviewHandlerTest 21 { 22 [DataTestMethod] 23 [DataRow("HelperFiles/sample.bgcode")] 24 public void BgcodePreviewHandlerControlAddsControlsToFormWhenDoPreviewIsCalled(string filePath) 25 { 26 // Arrange 27 using (var bgcodePreviewHandlerControl = new BgcodePreviewHandlerControl()) 28 { 29 // Act 30 var file = File.ReadAllBytes(filePath); 31 32 bgcodePreviewHandlerControl.DoPreview<IStream>(GetMockStream(file)); 33 34 var flowLayoutPanel = bgcodePreviewHandlerControl.Controls[0] as FlowLayoutPanel; 35 36 // Assert 37 Assert.AreEqual(1, bgcodePreviewHandlerControl.Controls.Count); 38 } 39 } 40 41 [TestMethod] 42 public void BgcodePreviewHandlerControlShouldAddValidInfoBarIfBgcodePreviewThrows() 43 { 44 // Arrange 45 using (var bgcodePreviewHandlerControl = new BgcodePreviewHandlerControl()) 46 { 47 var mockStream = new Mock<IStream>(); 48 mockStream 49 .Setup(x => x.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<IntPtr>())) 50 .Throws(new Exception()); 51 52 // Act 53 bgcodePreviewHandlerControl.DoPreview(mockStream.Object); 54 var textBox = bgcodePreviewHandlerControl.Controls[0] as RichTextBox; 55 56 // Assert 57 Assert.IsFalse(string.IsNullOrWhiteSpace(textBox.Text)); 58 Assert.AreEqual(1, bgcodePreviewHandlerControl.Controls.Count); 59 Assert.AreEqual(DockStyle.Top, textBox.Dock); 60 Assert.AreEqual(Color.LightYellow, textBox.BackColor); 61 Assert.IsTrue(textBox.Multiline); 62 Assert.IsTrue(textBox.ReadOnly); 63 Assert.AreEqual(RichTextBoxScrollBars.None, textBox.ScrollBars); 64 Assert.AreEqual(BorderStyle.None, textBox.BorderStyle); 65 } 66 } 67 68 private static IStream GetMockStream(byte[] sourceArray) 69 { 70 var streamMock = new Mock<IStream>(); 71 int bytesRead = 0; 72 73 streamMock 74 .Setup(x => x.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<IntPtr>())) 75 .Callback<byte[], int, IntPtr>((buffer, countToRead, bytesReadPtr) => 76 { 77 int actualCountToRead = Math.Min(sourceArray.Length - bytesRead, countToRead); 78 if (actualCountToRead > 0) 79 { 80 Array.Copy(sourceArray, bytesRead, buffer, 0, actualCountToRead); 81 Marshal.WriteInt32(bytesReadPtr, actualCountToRead); 82 bytesRead += actualCountToRead; 83 } 84 else 85 { 86 Marshal.WriteInt32(bytesReadPtr, 0); 87 } 88 }); 89 90 return streamMock.Object; 91 } 92 } 93 }