Skip to content

Commit c7688af

Browse files
Add tests to reset outlineHandler and cover resolveAgainstBaseUri. Refactor Html2PdfTest
DEVSIX-6137
1 parent f313eeb commit c7688af

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+122
-5585
lines changed

itext.tests/itext.html2pdf.tests/itext/html2pdf/Html2PdfTest.cs

Lines changed: 18 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -41,163 +41,92 @@ source product.
4141
4242
*/
4343
using System;
44-
using System.IO;
45-
using iText.Html2pdf.Attach;
46-
using iText.Html2pdf.Attach.Impl;
4744
using iText.Html2pdf.Logs;
48-
using iText.Html2pdf.Resolver.Font;
49-
using iText.IO.Util;
50-
using iText.Kernel.Pdf;
51-
using iText.Kernel.Utils;
52-
using iText.Layout;
53-
using iText.Layout.Font;
54-
using iText.StyledXmlParser;
55-
using iText.StyledXmlParser.Css.Media;
56-
using iText.StyledXmlParser.Node;
57-
using iText.StyledXmlParser.Node.Impl.Jsoup;
58-
using iText.Test;
5945
using iText.Test.Attributes;
6046

6147
namespace iText.Html2pdf {
62-
public class Html2PdfTest : ExtendedITextTest {
63-
public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
48+
public class Html2PdfTest : ExtendedHtmlConversionITextTest {
49+
public static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
6450
.CurrentContext.TestDirectory) + "/resources/itext/html2pdf/Html2PdfTest/";
6551

66-
public static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
52+
public static readonly String DESTINATION_FOLDER = NUnit.Framework.TestContext.CurrentContext.TestDirectory
6753
+ "/test/itext/html2pdf/Html2PdfTest/";
6854

6955
[NUnit.Framework.OneTimeSetUp]
7056
public static void BeforeClass() {
71-
CreateDestinationFolder(destinationFolder);
57+
CreateOrClearDestinationFolder(DESTINATION_FOLDER);
7258
}
7359

7460
[NUnit.Framework.Test]
7561
public virtual void HelloWorldParagraphTest() {
76-
ConvertAndCompare(sourceFolder + "hello_paragraph.html", destinationFolder + "hello_paragraph.pdf", sourceFolder
77-
+ "cmp_hello_paragraph.pdf", destinationFolder, "diff01_");
62+
ConvertToPdfAndCompare("hello_paragraph", SOURCE_FOLDER, DESTINATION_FOLDER);
7863
}
7964

8065
[NUnit.Framework.Test]
8166
public virtual void HelloParagraphTableTest() {
82-
ConvertAndCompare(sourceFolder + "hello_paragraph_table.html", destinationFolder + "hello_paragraph_table.pdf"
83-
, sourceFolder + "cmp_hello_paragraph_table.pdf", destinationFolder, "diff02_");
67+
ConvertToPdfAndCompare("hello_paragraph_table", SOURCE_FOLDER, DESTINATION_FOLDER);
8468
}
8569

8670
[NUnit.Framework.Test]
8771
public virtual void HelloMalformedDocumentTest() {
88-
ConvertAndCompare(sourceFolder + "hello_malformed.html", destinationFolder + "hello_malformed.pdf", sourceFolder
89-
+ "cmp_hello_malformed.pdf", destinationFolder, "diff03_");
72+
ConvertToPdfAndCompare("hello_malformed", SOURCE_FOLDER, DESTINATION_FOLDER);
9073
}
9174

9275
[NUnit.Framework.Test]
9376
public virtual void HelloParagraphJunkSpacesDocumentTest() {
94-
ConvertAndCompare(sourceFolder + "hello_paragraph_junk_spaces.html", destinationFolder + "hello_paragraph_junk_spaces.pdf"
95-
, sourceFolder + "cmp_hello_paragraph_junk_spaces.pdf", destinationFolder, "diff03_");
77+
ConvertToPdfAndCompare("hello_paragraph_junk_spaces", SOURCE_FOLDER, DESTINATION_FOLDER);
9678
}
9779

9880
[NUnit.Framework.Test]
9981
public virtual void HelloParagraphNestedInTableDocumentTest() {
100-
ConvertAndCompare(sourceFolder + "hello_paragraph_nested_in_table.html", destinationFolder + "hello_paragraph_nested_in_table.pdf"
101-
, sourceFolder + "cmp_hello_paragraph_nested_in_table.pdf", destinationFolder, "diff03_");
82+
ConvertToPdfAndCompare("hello_paragraph_nested_in_table", SOURCE_FOLDER, DESTINATION_FOLDER);
10283
}
10384

10485
[NUnit.Framework.Test]
10586
public virtual void HelloParagraphWithSpansDocumentTest() {
106-
ConvertAndCompare(sourceFolder + "hello_paragraph_with_span.html", destinationFolder + "hello_paragraph_with_span.pdf"
107-
, sourceFolder + "cmp_hello_paragraph_with_span.pdf", destinationFolder, "diff03_");
87+
ConvertToPdfAndCompare("hello_paragraph_with_span", SOURCE_FOLDER, DESTINATION_FOLDER);
10888
}
10989

11090
[NUnit.Framework.Test]
11191
public virtual void HelloDivDocumentTest() {
112-
ConvertAndCompare(sourceFolder + "hello_div.html", destinationFolder + "hello_div.pdf", sourceFolder + "cmp_hello_div.pdf"
113-
, destinationFolder, "diff03_");
92+
ConvertToPdfAndCompare("hello_div", SOURCE_FOLDER, DESTINATION_FOLDER);
11493
}
11594

11695
[NUnit.Framework.Test]
11796
public virtual void ABlockInPTagTest() {
118-
ConvertAndCompare(sourceFolder + "aBlockInPTag.html", destinationFolder + "aBlockInPTag.pdf", sourceFolder
119-
+ "cmp_aBlockInPTag.pdf", destinationFolder, "diff03_");
97+
ConvertToPdfAndCompare("aBlockInPTag", SOURCE_FOLDER, DESTINATION_FOLDER);
12098
}
12199

122100
[NUnit.Framework.Test]
123101
public virtual void Base64svgTest() {
124-
HtmlConverter.ConvertToPdf(new FileInfo(sourceFolder + "objectTag_base64svg.html"), new FileInfo(destinationFolder
125-
+ "objectTag_base64svg.pdf"));
126-
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + "objectTag_base64svg.pdf"
127-
, sourceFolder + "cmp_objectTag_base64svg.pdf", destinationFolder, "diff01_"));
102+
ConvertToPdfAndCompare("objectTag_base64svg", SOURCE_FOLDER, DESTINATION_FOLDER);
128103
}
129104

130105
[NUnit.Framework.Test]
131106
[LogMessage(iText.StyledXmlParser.Logs.StyledXmlParserLogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI
132107
, Count = 1)]
133108
[LogMessage(Html2PdfLogMessageConstant.WORKER_UNABLE_TO_PROCESS_OTHER_WORKER, Count = 1)]
134109
public virtual void HtmlObjectIncorrectBase64Test() {
135-
HtmlConverter.ConvertToPdf(new FileInfo(sourceFolder + "objectTag_incorrectBase64svg.html"), new FileInfo(
136-
destinationFolder + "objectTag_incorrectBase64svg.pdf"));
137-
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + "objectTag_incorrectBase64svg.pdf"
138-
, sourceFolder + "cmp_objectTag_incorrectBase64svg.pdf", destinationFolder, "diff01_"));
110+
ConvertToPdfAndCompare("objectTag_incorrectBase64svg", SOURCE_FOLDER, DESTINATION_FOLDER);
139111
}
140112

141113
[NUnit.Framework.Test]
114+
//TODO: update after DEVSIX-1346
142115
[LogMessage(Html2PdfLogMessageConstant.WORKER_UNABLE_TO_PROCESS_IT_S_TEXT_CONTENT, Count = 1)]
143116
[LogMessage(Html2PdfLogMessageConstant.WORKER_UNABLE_TO_PROCESS_OTHER_WORKER, Count = 2)]
144117
public virtual void HtmlObjectAltTextTest() {
145-
//update after DEVSIX-1346
146-
HtmlConverter.ConvertToPdf(new FileInfo(sourceFolder + "objectTag_altText.html"), new FileInfo(destinationFolder
147-
+ "objectTag_altText.pdf"));
148-
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + "objectTag_altText.pdf"
149-
, sourceFolder + "cmp_objectTag_altText.pdf", destinationFolder, "diff01_"));
118+
ConvertToPdfAndCompare("objectTag_altText", SOURCE_FOLDER, DESTINATION_FOLDER);
150119
}
151120

152121
[NUnit.Framework.Test]
153122
[LogMessage(Html2PdfLogMessageConstant.WORKER_UNABLE_TO_PROCESS_OTHER_WORKER, Count = 1)]
154123
public virtual void HtmlObjectNestedObjectTest() {
155-
HtmlConverter.ConvertToPdf(new FileInfo(sourceFolder + "objectTag_nestedTag.html"), new FileInfo(destinationFolder
156-
+ "objectTag_nestedTag.pdf"));
157-
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + "objectTag_nestedTag.pdf"
158-
, sourceFolder + "cmp_objectTag_nestedTag.pdf", destinationFolder, "diff01_"));
159-
}
160-
161-
[NUnit.Framework.Test]
162-
[LogMessage(iText.StyledXmlParser.Logs.StyledXmlParserLogMessageConstant.RULE_IS_NOT_SUPPORTED, Ignore = true
163-
)]
164-
[LogMessage(Html2PdfLogMessageConstant.CSS_PROPERTY_IN_PERCENTS_NOT_SUPPORTED, Ignore = true)]
165-
[LogMessage(Html2PdfLogMessageConstant.PADDING_VALUE_IN_PERCENT_NOT_SUPPORTED, Ignore = true)]
166-
[LogMessage(Html2PdfLogMessageConstant.MARGIN_VALUE_IN_PERCENT_NOT_SUPPORTED, Ignore = true)]
167-
[LogMessage(Html2PdfLogMessageConstant.ERROR_PARSING_CSS_SELECTOR, Ignore = true)]
168-
[LogMessage(iText.IO.Logs.IoLogMessageConstant.WIDOWS_CONSTRAINT_VIOLATED, Ignore = true)]
169-
public virtual void BatchConversionTest() {
170-
ConverterProperties properties = new ConverterProperties().SetBaseUri(sourceFolder).SetMediaDeviceDescription
171-
(new MediaDeviceDescription(MediaType.PRINT));
172-
FontProvider fontProvider = new DefaultFontProvider(true, false, false);
173-
fontProvider.AddDirectory(sourceFolder + "fonts/");
174-
properties.SetFontProvider(fontProvider);
175-
IHtmlProcessor processor = new DefaultHtmlProcessor(properties);
176-
IXmlParser parser = new JsoupHtmlParser();
177-
String outPdfPath = destinationFolder + "smashing1.pdf";
178-
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outPdfPath));
179-
IDocumentNode doc = parser.Parse(new FileStream(sourceFolder + "smashing01.html", FileMode.Open, FileAccess.Read
180-
), properties.GetCharset());
181-
Document document = processor.ProcessDocument(doc, pdfDocument);
182-
document.Close();
183-
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdfPath, sourceFolder + "cmp_smashing1.pdf"
184-
, destinationFolder, "diff01_"));
124+
ConvertToPdfAndCompare("objectTag_nestedTag", SOURCE_FOLDER, DESTINATION_FOLDER);
185125
}
186126

187127
[NUnit.Framework.Test]
188128
public virtual void HtmlImgBase64SVGTest() {
189-
HtmlConverter.ConvertToPdf(new FileInfo(sourceFolder + "imgTag_base64svg.html"), new FileInfo(destinationFolder
190-
+ "imgTag_base64svg.pdf"));
191-
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + "imgTag_base64svg.pdf"
192-
, sourceFolder + "cmp_imgTag_base64svg.pdf", destinationFolder, "diff01_"));
193-
}
194-
195-
private void ConvertAndCompare(String srcFilename, String outFilename, String cmpFilename, String outFolder
196-
, String diff) {
197-
HtmlConverter.ConvertToPdf(new FileInfo(srcFilename), new FileInfo(outFilename));
198-
System.Console.Out.WriteLine("html: " + UrlUtil.GetNormalizedFileUriString(srcFilename) + "\n");
199-
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFilename, cmpFilename, outFolder, diff
200-
));
129+
ConvertToPdfAndCompare("imgTag_base64svg", SOURCE_FOLDER, DESTINATION_FOLDER);
201130
}
202131
}
203132
}

itext.tests/itext.html2pdf.tests/itext/html2pdf/attach/impl/OutlineHandlerTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,19 @@ public virtual void DefaultOutlineHandlerWithHTagHavingIdTest() {
104104
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFile, cmpFile, DESTINATION_FOLDER, "diff_defaultOutlineHandlerWithHTagHavingIdTest"
105105
));
106106
}
107+
108+
[NUnit.Framework.Test]
109+
public virtual void ResetOutlineHandlerTest() {
110+
OutlineHandler outlineHandler = OutlineHandler.CreateStandardHandler();
111+
for (int i = 1; i <= 3; i++) {
112+
String srcHtml = SOURCE_FOLDER + "outlines0" + i + ".html";
113+
String outPdf = DESTINATION_FOLDER + "outlines0" + i + ".pdf";
114+
String cmpPdf = SOURCE_FOLDER + "cmp_outlines0" + i + ".pdf";
115+
HtmlConverter.ConvertToPdf(new FileInfo(srcHtml), new FileInfo(outPdf), new ConverterProperties().SetOutlineHandler
116+
(outlineHandler));
117+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdf, cmpPdf, DESTINATION_FOLDER, "diff_"
118+
+ i));
119+
}
120+
}
107121
}
108122
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2021 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
using System;
24+
using System.IO;
25+
using iText.Html2pdf;
26+
using iText.Kernel.Utils;
27+
using iText.Test;
28+
using iText.Test.Attributes;
29+
30+
namespace iText.Html2pdf.Resolver {
31+
public class UriResolverTest : ExtendedITextTest {
32+
private static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
33+
.CurrentContext.TestDirectory) + "/resources/itext/html2pdf/resolver/UriResolverTest/";
34+
35+
private static readonly String DESTINATION_FOLDER = NUnit.Framework.TestContext.CurrentContext.TestDirectory
36+
+ "/test/resources/itext/html2pdf/resolver/UriResolverTest/";
37+
38+
[NUnit.Framework.OneTimeSetUp]
39+
public static void Before() {
40+
CreateOrClearDestinationFolder(DESTINATION_FOLDER);
41+
}
42+
43+
[NUnit.Framework.Test]
44+
[LogMessage(iText.StyledXmlParser.Logs.StyledXmlParserLogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_BASE_URI
45+
)]
46+
public virtual void IllegalColonCharTest() {
47+
String srcHtml = SOURCE_FOLDER + "illegalColonChar.html";
48+
String outPdf = DESTINATION_FOLDER + "illegalColonChar.pdf";
49+
String cmpPdf = SOURCE_FOLDER + "cmp_illegalColonChar.pdf";
50+
ConverterProperties properties = new ConverterProperties().SetBaseUri(SOURCE_FOLDER);
51+
HtmlConverter.ConvertToPdf(new FileInfo(srcHtml), new FileInfo(outPdf), properties);
52+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdf, cmpPdf, DESTINATION_FOLDER, "diff_"
53+
));
54+
}
55+
}
56+
}

itext.tests/itext.html2pdf.tests/resources/itext/html2pdf/Html2PdfTest/fonts/LICENSE_OFL.txt

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)