|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Text; |
| 4 | +using DocumentFormat.OpenXml; |
| 5 | +using DocumentFormat.OpenXml.Packaging; |
| 6 | + |
| 7 | +namespace vbamc.Tests.Streams |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Integration tests that validate generated PowerPoint add-in packages |
| 11 | + /// have correct OpenXML structure and contain valid VBA project parts. |
| 12 | + /// Addresses issue #40: https://github.com/NetOfficeFw/vbamc/issues/40 |
| 13 | + /// </summary> |
| 14 | + class PowerPointAddInValidationTests |
| 15 | + { |
| 16 | + [SetUp] |
| 17 | + public void Setup() |
| 18 | + { |
| 19 | + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); |
| 20 | + } |
| 21 | + |
| 22 | + private static (string modulePath, string classPath) GetTestDataPaths() |
| 23 | + { |
| 24 | + var sourcePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "data"); |
| 25 | + var classPath = Path.Combine(sourcePath, "Class.vb"); |
| 26 | + var modulePath = Path.Combine(sourcePath, "Module.vb"); |
| 27 | + return (modulePath, classPath); |
| 28 | + } |
| 29 | + |
| 30 | + private static VbaCompiler CreateTestCompiler(string projectName) |
| 31 | + { |
| 32 | + return new VbaCompiler |
| 33 | + { |
| 34 | + ProjectId = Guid.NewGuid(), |
| 35 | + ProjectName = projectName, |
| 36 | + ProjectVersion = "1.0.0", |
| 37 | + CompanyName = "TestCompany" |
| 38 | + }; |
| 39 | + } |
| 40 | + |
| 41 | + private static MemoryStream CompilePowerPointFile( |
| 42 | + VbaCompiler compiler, |
| 43 | + PresentationDocumentType documentType) |
| 44 | + { |
| 45 | + var vbaProjectStream = new MemoryStream(); |
| 46 | + var outputStream = new MemoryStream(); |
| 47 | + |
| 48 | + compiler.CompileVbaProject(vbaProjectStream); |
| 49 | + compiler.CompilePowerPointMacroFile(outputStream, vbaProjectStream, documentType); |
| 50 | + |
| 51 | + outputStream.Position = 0; |
| 52 | + return outputStream; |
| 53 | + } |
| 54 | + |
| 55 | + [Test] |
| 56 | + public void CompilePowerPointAddin_ShouldProduceValidOpenXmlPackage() |
| 57 | + { |
| 58 | + // Arrange |
| 59 | + var (modulePath, classPath) = GetTestDataPaths(); |
| 60 | + var compiler = CreateTestCompiler("TestAddin"); |
| 61 | + compiler.AddModule(modulePath); |
| 62 | + compiler.AddClass(classPath); |
| 63 | + |
| 64 | + // Act |
| 65 | + using var addinStream = CompilePowerPointFile(compiler, PresentationDocumentType.AddIn); |
| 66 | + using var presentation = PresentationDocument.Open(addinStream, false); |
| 67 | + |
| 68 | + // Assert |
| 69 | + ClassicAssert.AreEqual(PresentationDocumentType.AddIn, presentation.DocumentType, |
| 70 | + "Generated file should be a PowerPoint add-in"); |
| 71 | + |
| 72 | + ClassicAssert.IsNotNull(presentation.PresentationPart, |
| 73 | + "Add-in should have a PresentationPart"); |
| 74 | + } |
| 75 | + |
| 76 | + [Test] |
| 77 | + public void CompilePowerPointAddin_ShouldContainVbaProjectPart() |
| 78 | + { |
| 79 | + // Arrange |
| 80 | + var (modulePath, classPath) = GetTestDataPaths(); |
| 81 | + var compiler = CreateTestCompiler("TestAddin"); |
| 82 | + compiler.AddModule(modulePath); |
| 83 | + compiler.AddClass(classPath); |
| 84 | + |
| 85 | + // Act |
| 86 | + using var addinStream = CompilePowerPointFile(compiler, PresentationDocumentType.AddIn); |
| 87 | + using var presentation = PresentationDocument.Open(addinStream, false); |
| 88 | + |
| 89 | + // Assert |
| 90 | + ClassicAssert.IsNotNull(presentation.PresentationPart?.VbaProjectPart, |
| 91 | + "Add-in should have a VbaProjectPart containing the VBA macros"); |
| 92 | + |
| 93 | + var vbaStream = presentation.PresentationPart!.VbaProjectPart!.GetStream(); |
| 94 | + ClassicAssert.Greater(vbaStream.Length, 0, |
| 95 | + "VbaProjectPart stream should contain non-zero bytes"); |
| 96 | + } |
| 97 | + |
| 98 | + [Test] |
| 99 | + public void CompilePowerPointMacroPresentation_ShouldProduceValidOpenXmlPackage() |
| 100 | + { |
| 101 | + // Arrange |
| 102 | + var (modulePath, classPath) = GetTestDataPaths(); |
| 103 | + var compiler = CreateTestCompiler("TestPresentation"); |
| 104 | + compiler.AddModule(modulePath); |
| 105 | + compiler.AddClass(classPath); |
| 106 | + |
| 107 | + // Act |
| 108 | + using var presentationStream = CompilePowerPointFile(compiler, PresentationDocumentType.MacroEnabledPresentation); |
| 109 | + using var presentation = PresentationDocument.Open(presentationStream, false); |
| 110 | + |
| 111 | + // Assert |
| 112 | + ClassicAssert.AreEqual(PresentationDocumentType.MacroEnabledPresentation, presentation.DocumentType, |
| 113 | + "Generated file should be a macro-enabled presentation"); |
| 114 | + |
| 115 | + ClassicAssert.IsNotNull(presentation.PresentationPart?.VbaProjectPart, |
| 116 | + "Macro-enabled presentation should have a VbaProjectPart"); |
| 117 | + |
| 118 | + var vbaStream = presentation.PresentationPart!.VbaProjectPart!.GetStream(); |
| 119 | + ClassicAssert.Greater(vbaStream.Length, 0, |
| 120 | + "VbaProjectPart stream should contain non-zero bytes"); |
| 121 | + } |
| 122 | + |
| 123 | + [Test] |
| 124 | + public void CompilePowerPointAddin_WithMultipleModules_ShouldProduceValidPackage() |
| 125 | + { |
| 126 | + // Arrange |
| 127 | + var (modulePath, classPath) = GetTestDataPaths(); |
| 128 | + var compiler = CreateTestCompiler("MultiModuleAddin"); |
| 129 | + compiler.AddModule(modulePath); |
| 130 | + compiler.AddClass(classPath); |
| 131 | + |
| 132 | + // Act |
| 133 | + using var addinStream = CompilePowerPointFile(compiler, PresentationDocumentType.AddIn); |
| 134 | + using var presentation = PresentationDocument.Open(addinStream, false); |
| 135 | + |
| 136 | + // Assert |
| 137 | + ClassicAssert.AreEqual(PresentationDocumentType.AddIn, presentation.DocumentType); |
| 138 | + ClassicAssert.IsNotNull(presentation.PresentationPart?.VbaProjectPart); |
| 139 | + |
| 140 | + var vbaStream = presentation.PresentationPart!.VbaProjectPart!.GetStream(); |
| 141 | + ClassicAssert.Greater(vbaStream.Length, 0); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments