Skip to content

Commit 2231c4a

Browse files
pkjuakpavel-alay
authored andcommitted
Create tests for FontSelector with different font families
DEVSIX-2097
1 parent 291774e commit 2231c4a

30 files changed

+4323
-3
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package com.itextpdf.html2pdf;
2+
3+
import com.itextpdf.io.util.UrlUtil;
4+
import com.itextpdf.kernel.pdf.PdfDocument;
5+
import com.itextpdf.kernel.pdf.PdfWriter;
6+
import com.itextpdf.kernel.utils.CompareTool;
7+
import com.itextpdf.layout.Document;
8+
import com.itextpdf.layout.element.IBlockElement;
9+
import com.itextpdf.layout.element.IElement;
10+
import com.itextpdf.test.ExtendedITextTest;
11+
import java.io.ByteArrayInputStream;
12+
import java.io.FileOutputStream;
13+
import java.io.IOException;
14+
import java.io.InputStream;
15+
import java.nio.charset.StandardCharsets;
16+
import java.util.List;
17+
import org.junit.Assert;
18+
19+
public class ExtendedFontPropertiesTest extends ExtendedITextTest {
20+
21+
private static final String HTML_TOP = "<html><head>";
22+
private static final String HTML_BEGIN_BODY = "</head><body>";
23+
private static final String HTML_BOTTOM = "</body></html>";
24+
private static final String TR_OPEN = "<tr>";
25+
private static final String TD_OPEN = "<td>";
26+
private static final String TD_CLOSE = "</td>";
27+
private static final String TR_CLOSE = "</tr>";
28+
private static final String TABLE_OPEN = "<table>";
29+
private static final String TABLE_CLOSE = "</table>";
30+
private static final String DOCUMENT_PREFIX = "FOR_VISUAL_CMP_";
31+
private static final String COLUMN_DECLARATIONS = "<col width='50'><col width='50'><col width='50'><col width='300'>";
32+
private static final String TH_FONT_FAMILY = "<th scope='col'>Font-family</th>";
33+
private static final String TH_FONT_WEIGHT = "<th scope='col'>Font-weight</th>";
34+
private static final String TH_FONT_STYLE = "<th scope='col'>Font-style</th>";
35+
private static final String TH_RESULT = "<th scope='col' >Result</th>";
36+
private static final String TITLE_TAG = "<title>Font Family Test</title>";
37+
private static final String HTML_TITLE = "<h1>Font Family Test</h1>";
38+
private static final String TD_STYLE = "<td style = \"";
39+
private static final String FONT_FAMILY = " font-family: '";
40+
private static final String FONT_STYLE = "; font-style: ";
41+
private static final String FONT_WEIGHT = "'; font-weight: ";
42+
43+
public void runTest(String htmlString, String sourceFolder, String destinationFolder, String fileName, String testName) throws IOException, InterruptedException {
44+
String outPdf = destinationFolder + fileName + ".pdf";
45+
String cmpPdf = sourceFolder + "cmp_" + fileName + ".pdf";
46+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outPdf));
47+
Document doc = new Document(pdfDoc);
48+
byte[] bytes = htmlString.getBytes(StandardCharsets.UTF_8);
49+
50+
// save to html
51+
generateTestHtml(destinationFolder, fileName, bytes);
52+
53+
// Convert to elements
54+
writeToDocument(doc, bytes);
55+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff_" + testName + "_"));
56+
}
57+
58+
private void writeToDocument(Document doc, byte[] bytes) throws IOException {
59+
InputStream in = new ByteArrayInputStream(bytes);
60+
List<IElement> arrayList = HtmlConverter.convertToElements(in);
61+
for (IElement element : arrayList) {
62+
if (element instanceof IBlockElement) {
63+
doc.add((IBlockElement) element);
64+
}
65+
}
66+
doc.close();
67+
}
68+
69+
private void generateTestHtml(String destinationFolder, String fileName, byte[] bytes) throws IOException {
70+
String htmlPath = destinationFolder + DOCUMENT_PREFIX + fileName + ".html";
71+
FileOutputStream out = new FileOutputStream(htmlPath);
72+
System.out.println("html: file:///" + UrlUtil.toNormalizedURI(htmlPath).getPath() + "\n");
73+
out.close();
74+
}
75+
76+
public String buildDocumentTree(String[] fontFamilies, String[] fontWeights, String[] fontStyles, String[] cssFiles, String text) {
77+
StringBuilder builder = new StringBuilder();
78+
builder.append(HTML_TOP);
79+
if (cssFiles != null) {
80+
for (String css : cssFiles) {
81+
builder.append(" <link href='").append(css).append("' rel='stylesheet' type='text/css'>\n ");
82+
}
83+
}
84+
//Build Html top
85+
String styleTag = "<style>\n" +
86+
" th, td {text-align: center;\n" +
87+
" height: 45px; border: 1px solid black; }\n" +
88+
" table {font-family: Courier; }" +
89+
" </style>\n";
90+
91+
builder.append(TITLE_TAG);
92+
builder.append(styleTag);
93+
builder.append(HTML_BEGIN_BODY);
94+
builder.append(HTML_TITLE);
95+
builder.append(TABLE_OPEN);
96+
builder.append(COLUMN_DECLARATIONS);
97+
builder.append(TR_OPEN);
98+
builder.append(TH_FONT_FAMILY);
99+
builder.append(TH_FONT_WEIGHT);
100+
builder.append(TH_FONT_STYLE);
101+
builder.append(TH_RESULT);
102+
builder.append(TR_CLOSE);
103+
104+
//Build body content
105+
for (String name : fontFamilies) {
106+
for (String weight : fontWeights) {
107+
for (String style : fontStyles) {
108+
// the tr
109+
builder
110+
.append(TR_OPEN);
111+
// the first td
112+
builder
113+
.append(TD_OPEN)
114+
.append(name)
115+
.append(TD_CLOSE);
116+
// the second td
117+
builder
118+
.append(TD_OPEN)
119+
.append(weight)
120+
.append(TD_CLOSE);
121+
// the third td
122+
builder
123+
.append(TD_OPEN)
124+
.append(style)
125+
.append(TD_CLOSE);
126+
// the fourth td
127+
builder
128+
.append(TD_STYLE)
129+
.append(FONT_FAMILY)
130+
.append(name)
131+
.append(FONT_WEIGHT)
132+
.append(weight)
133+
.append(FONT_STYLE)
134+
.append(style)
135+
.append("\";>")
136+
.append(text)
137+
.append(TD_CLOSE);
138+
// the tr
139+
builder.append(TR_CLOSE);
140+
}
141+
}
142+
}
143+
builder.append(TABLE_CLOSE);
144+
builder.append(HTML_BOTTOM);
145+
return builder.toString();
146+
}
147+
}

src/test/java/com/itextpdf/html2pdf/css/FontFaceTest.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,6 @@ public void incorrectFontNameTest01() throws IOException, InterruptedException {
226226
}
227227

228228
@Test
229-
// The result of te test is FAIL. However we consider it to be correct.
230-
// Although the font-family specified by the paragraph's class doesn't match the one of fontface,
231-
// font's full name contains specified font-family and iText takes it into account.
232229
public void incorrectFontNameTest02() throws IOException, InterruptedException {
233230
runTest("incorrectFontNameTest02");
234231
}
@@ -256,6 +253,30 @@ public void fontFamilyTest01() throws IOException, InterruptedException {
256253
runTest("fontFamilyTest01");
257254
}
258255

256+
@Test
257+
//TODO DEVSIX-2122
258+
public void fontFaceFontWeightTest() throws IOException, InterruptedException {
259+
runTest("fontFaceFontWeightTest");
260+
}
261+
262+
@Test
263+
//TODO DEVSIX-2122
264+
public void fontFaceFontWeightWrongTest() throws IOException, InterruptedException {
265+
runTest("fontFaceFontWeightWrongWeightsTest");
266+
}
267+
268+
@Test
269+
//TODO DEVSIX-2122
270+
public void fontFaceFontWeightInvalidTest() throws IOException, InterruptedException {
271+
runTest("fontFaceFontWeightInvalidWeightsTest");
272+
}
273+
274+
@Test
275+
// TODO DEVSIX-1953
276+
public void texFonts01() throws IOException, InterruptedException {
277+
runTest("texFonts01");
278+
}
279+
259280
private void runTest(String name) throws IOException, InterruptedException {
260281
String htmlPath = sourceFolder + name + ".html";
261282
String pdfPath = destinationFolder + name + ".pdf";
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.itextpdf.html2pdf.css;
2+
3+
import com.itextpdf.html2pdf.ConverterProperties;
4+
import com.itextpdf.html2pdf.HtmlConverter;
5+
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
6+
import com.itextpdf.io.font.constants.StandardFonts;
7+
import com.itextpdf.io.util.UrlUtil;
8+
import com.itextpdf.kernel.utils.CompareTool;
9+
import com.itextpdf.layout.font.FontProvider;
10+
import com.itextpdf.styledxmlparser.css.media.MediaDeviceDescription;
11+
import com.itextpdf.styledxmlparser.css.media.MediaType;
12+
import com.itextpdf.test.ExtendedITextTest;
13+
import com.itextpdf.test.annotations.type.IntegrationTest;
14+
15+
import java.io.File;
16+
import java.io.IOException;
17+
18+
import org.junit.Assert;
19+
import org.junit.BeforeClass;
20+
import org.junit.Test;
21+
import org.junit.experimental.categories.Category;
22+
23+
@Category(IntegrationTest.class)
24+
public class FontSelectorArialFontTest extends ExtendedITextTest {
25+
public static final String sourceFolder = "./src/test/resources/com/itextpdf/html2pdf/css/FontSelectorArialFontTest/";
26+
public static final String destinationFolder = "./target/test/com/itextpdf/html2pdf/css/FontSelectorArialFontTest/";
27+
28+
public static final String SOURCE_HTML_NAME = "arialTest";
29+
30+
@BeforeClass
31+
public static void beforeClass() {
32+
createDestinationFolder(destinationFolder);
33+
createDestinationFolder(sourceFolder);
34+
}
35+
36+
@Test
37+
public void testArial() throws IOException, InterruptedException {
38+
String fileName = "testArial";
39+
ConverterProperties converterProperties = new ConverterProperties()
40+
.setMediaDeviceDescription(new MediaDeviceDescription(MediaType.PRINT))
41+
.setFontProvider(new DefaultFontProvider());
42+
runTest(fileName, converterProperties);
43+
}
44+
45+
@Test
46+
public void testArialWithHelveticaAsAnAlias() throws IOException, InterruptedException {
47+
String fileName = "testArialWithHelveticaAsAnAlias";
48+
FontProvider fontProvider = new DefaultFontProvider();
49+
fontProvider.getFontSet().addFont(sourceFolder + "FreeSans.ttf", null, "Arial");
50+
ConverterProperties converterProperties = new ConverterProperties()
51+
.setMediaDeviceDescription(new MediaDeviceDescription(MediaType.PRINT))
52+
.setFontProvider(fontProvider);
53+
runTest(fileName, converterProperties);
54+
}
55+
56+
private void runTest(String name, ConverterProperties converterProperties) throws IOException, InterruptedException {
57+
String htmlPath = sourceFolder + SOURCE_HTML_NAME + ".html";
58+
String pdfPath = destinationFolder + name + ".pdf";
59+
String cmpPdfPath = sourceFolder + "cmp_" + name + ".pdf";
60+
String diffPrefix = "diff_" + name + "_";
61+
HtmlConverter.convertToPdf(new File(htmlPath), new File(pdfPath), converterProperties);
62+
Assert.assertNull(new CompareTool().compareByContent(pdfPath, cmpPdfPath, destinationFolder, diffPrefix));
63+
}
64+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.itextpdf.html2pdf.css;
2+
3+
import com.itextpdf.html2pdf.ConverterProperties;
4+
import com.itextpdf.html2pdf.HtmlConverter;
5+
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
6+
import com.itextpdf.io.util.UrlUtil;
7+
import com.itextpdf.kernel.pdf.PdfDocument;
8+
import com.itextpdf.kernel.pdf.PdfWriter;
9+
import com.itextpdf.kernel.utils.CompareTool;
10+
import com.itextpdf.layout.Document;
11+
import com.itextpdf.layout.element.IBlockElement;
12+
import com.itextpdf.layout.element.IElement;
13+
import com.itextpdf.layout.font.FontInfo;
14+
import com.itextpdf.styledxmlparser.css.media.MediaDeviceDescription;
15+
import com.itextpdf.styledxmlparser.css.media.MediaType;
16+
import com.itextpdf.styledxmlparser.resolver.font.BasicFontProvider;
17+
import com.itextpdf.test.ExtendedITextTest;
18+
import com.itextpdf.test.annotations.type.IntegrationTest;
19+
import org.junit.Assert;
20+
import org.junit.BeforeClass;
21+
import org.junit.Ignore;
22+
import org.junit.Test;
23+
import org.junit.experimental.categories.Category;
24+
25+
import java.io.ByteArrayInputStream;
26+
import java.io.File;
27+
import java.io.IOException;
28+
import java.io.InputStream;
29+
import java.nio.charset.StandardCharsets;
30+
import java.util.Collections;
31+
import java.util.List;
32+
33+
@Category(IntegrationTest.class)
34+
public class FontSelectorPerformanceTest extends ExtendedITextTest {
35+
public static final String sourceFolder = "./src/test/resources/com/itextpdf/html2pdf/css/FontSelectorPerformanceTest/";
36+
public static final String destinationFolder = "./target/test/com/itextpdf/html2pdf/css/FontSelectorPerformanceTest/";
37+
38+
@BeforeClass
39+
public static void beforeClass() {
40+
createDestinationFolder(destinationFolder);
41+
}
42+
43+
@Test
44+
@Ignore("Each machine has different set of fonts")
45+
public void performanceTest01() throws IOException, InterruptedException {
46+
String name = "performanceTest01";
47+
48+
String htmlPath = sourceFolder + name + ".html";
49+
String pdfPath = destinationFolder + name + ".pdf";
50+
String cmpPdfPath = sourceFolder + "cmp_" + name + ".pdf";
51+
String diffPrefix = "diff_" + name + "_";
52+
53+
System.out.println("html: file:///" + UrlUtil.toNormalizedURI(htmlPath).getPath() + "\n");
54+
55+
ConverterProperties converterProperties = new ConverterProperties()
56+
.setFontProvider(new BasicFontProvider(true, true));
57+
58+
HtmlConverter.convertToPdf(new File(htmlPath), new File(pdfPath), converterProperties);
59+
Assert.assertNull(new CompareTool().compareByContent(pdfPath, cmpPdfPath, destinationFolder, diffPrefix));
60+
}
61+
62+
@Test
63+
@Ignore("Each machine has different set of fonts")
64+
public void performanceTest02() throws IOException, InterruptedException {
65+
String name = "performanceTest02";
66+
67+
String htmlPath = sourceFolder + "performanceTest01" + ".html";
68+
String pdfPath = destinationFolder + name + ".pdf";
69+
String cmpPdfPath = sourceFolder + "cmp_" + "performanceTest01" + ".pdf";
70+
String diffPrefix = "diff_" + name + "_";
71+
72+
System.out.println("html: file:///" + UrlUtil.toNormalizedURI(htmlPath).getPath() + "\n");
73+
74+
ConverterProperties converterProperties = new ConverterProperties()
75+
.setFontProvider(new BasicFontProvider(true, true));
76+
77+
HtmlConverter.convertToPdf(new File(htmlPath), new File(pdfPath), converterProperties);
78+
HtmlConverter.convertToPdf(new File(htmlPath), new File(pdfPath), converterProperties);
79+
HtmlConverter.convertToPdf(new File(htmlPath), new File(pdfPath), converterProperties);
80+
HtmlConverter.convertToPdf(new File(htmlPath), new File(pdfPath), converterProperties);
81+
HtmlConverter.convertToPdf(new File(htmlPath), new File(pdfPath), converterProperties);
82+
HtmlConverter.convertToPdf(new File(htmlPath), new File(pdfPath), converterProperties);
83+
HtmlConverter.convertToPdf(new File(htmlPath), new File(pdfPath), converterProperties);
84+
HtmlConverter.convertToPdf(new File(htmlPath), new File(pdfPath), converterProperties);
85+
HtmlConverter.convertToPdf(new File(htmlPath), new File(pdfPath), converterProperties);
86+
HtmlConverter.convertToPdf(new File(htmlPath), new File(pdfPath), converterProperties);
87+
88+
Assert.assertNull(new CompareTool().compareByContent(pdfPath, cmpPdfPath, destinationFolder, diffPrefix));
89+
}
90+
91+
@Test
92+
public void performanceTest03() throws IOException, InterruptedException {
93+
String name = "performanceTest03";
94+
95+
String htmlPath = sourceFolder + name + ".html";
96+
String pdfPath = destinationFolder + name + ".pdf";
97+
String cmpPdfPath = sourceFolder + "cmp_" + name + ".pdf";
98+
String diffPrefix = "diff_" + name + "_";
99+
100+
System.out.println("html: file:///" + UrlUtil.toNormalizedURI(htmlPath).getPath() + "\n");
101+
102+
ConverterProperties converterProperties = new ConverterProperties()
103+
.setFontProvider(new BasicFontProvider(true, true));
104+
105+
HtmlConverter.convertToPdf(new File(htmlPath), new File(pdfPath), converterProperties);
106+
107+
Assert.assertNull(new CompareTool().compareByContent(pdfPath, cmpPdfPath, destinationFolder, diffPrefix));
108+
}
109+
110+
}

0 commit comments

Comments
 (0)