Skip to content

Commit 94ff2fb

Browse files
Kate IvanovaiText-CI
authored andcommitted
Add langAttributeHtmlForTaggedPdfTest
DEVSIX-3243
1 parent 27a8e8c commit 94ff2fb

17 files changed

+555
-0
lines changed
Lines changed: 333 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,333 @@
1+
package com.itextpdf.html2pdf.attribute;
2+
3+
import com.itextpdf.html2pdf.ConverterProperties;
4+
import com.itextpdf.html2pdf.ExtendedHtmlConversionITextTest;
5+
import com.itextpdf.html2pdf.HtmlConverter;
6+
import com.itextpdf.kernel.pdf.PdfDocument;
7+
import com.itextpdf.kernel.pdf.PdfReader;
8+
import com.itextpdf.kernel.pdf.PdfWriter;
9+
import com.itextpdf.kernel.pdf.tagging.StandardRoles;
10+
import com.itextpdf.kernel.pdf.tagutils.TagTreePointer;
11+
import com.itextpdf.kernel.utils.CompareTool;
12+
import com.itextpdf.test.annotations.type.IntegrationTest;
13+
import org.junit.Assert;
14+
import org.junit.BeforeClass;
15+
import org.junit.Test;
16+
import org.junit.experimental.categories.Category;
17+
import org.xml.sax.SAXException;
18+
19+
import javax.xml.parsers.ParserConfigurationException;
20+
import java.io.FileInputStream;
21+
import java.io.IOException;
22+
23+
24+
@Category(IntegrationTest.class)
25+
public class LangAttributeTest extends ExtendedHtmlConversionITextTest {
26+
27+
public static final String sourceFolder = "./src/test/resources/com/itextpdf/html2pdf/attribute/LangAttributeTest/";
28+
public static final String destinationFolder = "./target/test/com/itextpdf/html2pdf/attribute/LangAttributeTest/";
29+
30+
@BeforeClass
31+
public static void beforeClass() {
32+
createDestinationFolder(destinationFolder);
33+
}
34+
35+
@Test
36+
//TODO: DEVSIX-3243 Assert exact language
37+
public void langAttrInElementForTaggedPdfTest() throws IOException {
38+
String html = sourceFolder + "langAttrInElementForTaggedPdfTest.html";
39+
String outFile = destinationFolder + "langAttrInElementForTaggedPdfTest.pdf";
40+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile));
41+
pdfDocument.setTagged();
42+
43+
HtmlConverter.convertToPdf(new FileInputStream(html), pdfDocument, null);
44+
printOutputPdfNameAndDir(outFile);
45+
46+
PdfDocument document = new PdfDocument(new PdfReader(outFile));
47+
TagTreePointer tagPointer = new TagTreePointer(document);
48+
tagPointer.moveToKid(StandardRoles.P);
49+
Assert.assertNull(tagPointer.getProperties().getLanguage());
50+
Assert.assertNull(document.getCatalog().getLang());
51+
52+
document.close();
53+
}
54+
55+
@Test
56+
//TODO: DEVSIX-3243 Assert exact language
57+
public void langAttrInHtmlTagForTaggedPdfTest() throws IOException {
58+
String html = sourceFolder + "langAttrInHtmlTagForTaggedPdfTest.html";
59+
String outFile = destinationFolder + "langAttrInHtmlTagForTaggedPdfTest.pdf";
60+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile));
61+
pdfDocument.setTagged();
62+
63+
HtmlConverter.convertToPdf(new FileInputStream(html), pdfDocument, null);
64+
printOutputPdfNameAndDir(outFile);
65+
66+
PdfDocument document = new PdfDocument(new PdfReader(outFile));
67+
TagTreePointer tagPointer = new TagTreePointer(document);
68+
Assert.assertNull(tagPointer.getProperties().getLanguage());
69+
Assert.assertNull(document.getCatalog().getLang());
70+
71+
document.close();
72+
}
73+
74+
@Test
75+
//TODO: DEVSIX-3243 Assert exact language
76+
public void langAttrInvalidTagsTest() throws IOException {
77+
String html = sourceFolder + "langAttrInvalidTagsTest.html";
78+
String outFile = destinationFolder + "langAttrInvalidTagsTest.pdf";
79+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile));
80+
pdfDocument.setTagged();
81+
82+
HtmlConverter.convertToPdf(new FileInputStream(html), pdfDocument, null);
83+
printOutputPdfNameAndDir(outFile);
84+
85+
PdfDocument document = new PdfDocument(new PdfReader(outFile));
86+
TagTreePointer tagPointer = new TagTreePointer(document);
87+
tagPointer.moveToKid(1, StandardRoles.P);
88+
Assert.assertNull(tagPointer.getProperties().getLanguage());
89+
90+
tagPointer.moveToRoot().moveToKid(2, StandardRoles.P);
91+
Assert.assertNull(tagPointer.getProperties().getLanguage());
92+
93+
tagPointer.moveToRoot().moveToKid(3, StandardRoles.P);
94+
Assert.assertNull(tagPointer.getProperties().getLanguage());
95+
96+
tagPointer.moveToRoot().moveToKid(4, StandardRoles.P);
97+
Assert.assertNull(tagPointer.getProperties().getLanguage());
98+
99+
tagPointer.moveToRoot().moveToKid(5, StandardRoles.P);
100+
Assert.assertNull(tagPointer.getProperties().getLanguage());
101+
102+
tagPointer.moveToRoot().moveToKid(6, StandardRoles.P);
103+
Assert.assertNull(tagPointer.getProperties().getLanguage());
104+
105+
Assert.assertNull(document.getCatalog().getLang());
106+
document.close();
107+
}
108+
109+
@Test
110+
//TODO: DEVSIX-3243 Assert exact language
111+
public void langAttrEmptyTagTest() throws IOException {
112+
String html = sourceFolder + "langAttrEmptyTagTest.html";
113+
String outFile = destinationFolder + "langAttrEmptyTagTest.pdf";
114+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile));
115+
pdfDocument.setTagged();
116+
117+
HtmlConverter.convertToPdf(new FileInputStream(html), pdfDocument, null);
118+
printOutputPdfNameAndDir(outFile);
119+
120+
PdfDocument document = new PdfDocument(new PdfReader(outFile));
121+
TagTreePointer tagPointer = new TagTreePointer(document);
122+
tagPointer.moveToKid(StandardRoles.P);
123+
124+
Assert.assertNull(tagPointer.getProperties().getLanguage());
125+
Assert.assertNull(document.getCatalog().getLang());
126+
document.close();
127+
}
128+
129+
@Test
130+
//TODO: DEVSIX-3243 Assert exact language
131+
public void langAttrRegionSubtagTest() throws IOException {
132+
String html = sourceFolder + "langAttrRegionSubtagTest.html";
133+
String outFile = destinationFolder + "langAttrRegionSubtagTest.pdf";
134+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile));
135+
pdfDocument.setTagged();
136+
137+
HtmlConverter.convertToPdf(new FileInputStream(html), pdfDocument, null);
138+
printOutputPdfNameAndDir(outFile);
139+
140+
PdfDocument document = new PdfDocument(new PdfReader(outFile));
141+
TagTreePointer tagPointer = new TagTreePointer(document);
142+
tagPointer.moveToKid(1, StandardRoles.P);
143+
Assert.assertNull(tagPointer.getProperties().getLanguage());
144+
145+
tagPointer.moveToRoot().moveToKid(2, StandardRoles.P);
146+
Assert.assertNull(tagPointer.getProperties().getLanguage());
147+
148+
tagPointer.moveToRoot().moveToKid(3, StandardRoles.P);
149+
Assert.assertNull(tagPointer.getProperties().getLanguage());
150+
151+
Assert.assertNull(document.getCatalog().getLang());
152+
153+
document.close();
154+
}
155+
156+
@Test
157+
//TODO: DEVSIX-3243 Assert exact language
158+
public void langAttrScriptSubtagTest() throws IOException {
159+
String html = sourceFolder + "langAttrScriptSubtagTest.html";
160+
String outFile = destinationFolder + "langAttrScriptSubtagTest.pdf";
161+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile));
162+
pdfDocument.setTagged();
163+
164+
HtmlConverter.convertToPdf(new FileInputStream(html), pdfDocument, null);
165+
printOutputPdfNameAndDir(outFile);
166+
167+
PdfDocument document = new PdfDocument(new PdfReader(outFile));
168+
TagTreePointer tagPointer = new TagTreePointer(document);
169+
tagPointer.moveToKid(1, StandardRoles.P);
170+
Assert.assertNull(tagPointer.getProperties().getLanguage());
171+
172+
tagPointer.moveToRoot().moveToKid(2, StandardRoles.P);
173+
Assert.assertNull(tagPointer.getProperties().getLanguage());
174+
175+
tagPointer.moveToRoot().moveToKid(3, StandardRoles.P);
176+
Assert.assertNull(tagPointer.getProperties().getLanguage());
177+
178+
Assert.assertNull(document.getCatalog().getLang());
179+
document.close();
180+
}
181+
182+
@Test
183+
//TODO: DEVSIX-3243 Assert exact language
184+
public void langAttrScriptRegionSubtagTest() throws IOException {
185+
String html = sourceFolder + "langAttrScriptRegionSubtagTest.html";
186+
String outFile = destinationFolder + "langAttrScriptRegionSubtagTest.pdf";
187+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile));
188+
pdfDocument.setTagged();
189+
190+
HtmlConverter.convertToPdf(new FileInputStream(html), pdfDocument, null);
191+
printOutputPdfNameAndDir(outFile);
192+
193+
PdfDocument document = new PdfDocument(new PdfReader(outFile));
194+
TagTreePointer tagPointer = new TagTreePointer(document);
195+
tagPointer.moveToKid(1, StandardRoles.P);
196+
Assert.assertNull(tagPointer.getProperties().getLanguage());
197+
198+
tagPointer.moveToRoot().moveToKid(2, StandardRoles.P);
199+
Assert.assertNull(tagPointer.getProperties().getLanguage());
200+
201+
Assert.assertNull( document.getCatalog().getLang());
202+
document.close();
203+
}
204+
205+
@Test
206+
//TODO: after DEVSIX-3243 fixed need to assert exact language and check assertEquals
207+
public void langAttrInTableForTaggedPdfTest() throws IOException, InterruptedException {
208+
String html = sourceFolder + "langAttrInTableForTaggedPdfTest.html";
209+
String outFile = destinationFolder + "langAttrInTableForTaggedPdfTest.pdf";
210+
String cmp = sourceFolder + "cmp_langAttrInTableForTaggedPdfTest.pdf";
211+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile));
212+
pdfDocument.setTagged();
213+
214+
HtmlConverter.convertToPdf(new FileInputStream(html), pdfDocument, null);
215+
printOutputPdfNameAndDir(outFile);
216+
217+
PdfDocument document = new PdfDocument(new PdfReader(outFile));
218+
//compareByContent is used here to check the complete logical structure tree to notice all the differences.
219+
Assert.assertNull(new CompareTool().compareByContent(outFile, cmp, destinationFolder,
220+
"diff_Table"));
221+
Assert.assertNull(document.getCatalog().getLang());
222+
223+
pdfDocument.close();
224+
225+
}
226+
227+
@Test
228+
//TODO: after DEVSIX-3243 fixed need to assert exact language and check assertEquals
229+
public void langAttrInSvgForTaggedPdfTest() throws IOException {
230+
String html = sourceFolder + "langAttrInSvgForTaggedPdfTest.html";
231+
String outFile = destinationFolder + "langAttrInSvgForTaggedPdfTest.pdf";
232+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile));
233+
pdfDocument.setTagged();
234+
235+
HtmlConverter.convertToPdf(new FileInputStream(html), pdfDocument, new ConverterProperties().setBaseUri(sourceFolder));
236+
printOutputPdfNameAndDir(outFile);
237+
238+
PdfDocument document = new PdfDocument(new PdfReader(outFile));
239+
TagTreePointer tagPointer = new TagTreePointer(document);
240+
tagPointer.moveToKid(StandardRoles.FIGURE);
241+
242+
Assert.assertNull(tagPointer.getProperties().getLanguage());
243+
244+
tagPointer
245+
.moveToParent()
246+
.moveToKid(2, StandardRoles.P)
247+
.moveToKid(StandardRoles.FIGURE);
248+
249+
Assert.assertNull(tagPointer.getProperties().getLanguage());
250+
Assert.assertNull(document.getCatalog().getLang());
251+
document.close();
252+
}
253+
254+
@Test
255+
//TODO: after DEVSIX-3243 fixed need to assert exact language and check assertEquals
256+
public void langAttrInListsForTaggedPdfTest() throws IOException {
257+
String html = sourceFolder + "langAttrInListsForTaggedPdfTest.html";
258+
String outFile = destinationFolder + "langAttrInListsForTaggedPdfTest.pdf";
259+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile));
260+
pdfDocument.setTagged();
261+
262+
HtmlConverter.convertToPdf(new FileInputStream(html), pdfDocument, null);
263+
printOutputPdfNameAndDir(outFile);
264+
265+
PdfDocument document = new PdfDocument(new PdfReader(outFile));
266+
TagTreePointer tagPointer = new TagTreePointer(document);
267+
tagPointer.moveToKid(0, StandardRoles.L);
268+
269+
Assert.assertNull(tagPointer.getProperties().getLanguage());
270+
tagPointer.moveToParent();
271+
272+
tagPointer.moveToKid(1, StandardRoles.L);
273+
tagPointer.getProperties().getLanguage();
274+
Assert.assertNull(tagPointer.getProperties().getLanguage());
275+
tagPointer.moveToParent();
276+
277+
tagPointer
278+
.moveToKid(2, StandardRoles.L)
279+
.moveToKid(0, StandardRoles.LI);
280+
281+
Assert.assertNull(tagPointer.getProperties().getLanguage());
282+
tagPointer.moveToRoot();
283+
284+
tagPointer
285+
.moveToKid(2, StandardRoles.L)
286+
.moveToKid(1, StandardRoles.LI);
287+
288+
Assert.assertNull(tagPointer.getProperties().getLanguage());
289+
Assert.assertNull(document.getCatalog().getLang());
290+
document.close();
291+
}
292+
293+
@Test
294+
//TODO: after DEVSIX-3243 fixed need to assert exact language and check assertEquals
295+
public void langAttrInDivAndSpanForTagPdfTest() throws IOException, ParserConfigurationException, SAXException, InterruptedException {
296+
String html = sourceFolder + "langAttrInDivAndSpanForTagPdfTest.html";
297+
String outFile = destinationFolder + "langAttrInDivAndSpanForTagPdfTest.pdf";
298+
String cmp = sourceFolder + "cmp_langAttrInDivAndSpanForTagPdfTest.pdf";
299+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile));
300+
pdfDocument.setTagged();
301+
302+
HtmlConverter.convertToPdf(new FileInputStream(html), pdfDocument, null);
303+
printOutputPdfNameAndDir(outFile);
304+
305+
PdfDocument document = new PdfDocument(new PdfReader(outFile));
306+
//compareByContent is used here to check the complete logical structure tree to notice all the differences.
307+
Assert.assertNull(new CompareTool().compareByContent(outFile, cmp, destinationFolder, "diff_Table"));
308+
Assert.assertNull(document.getCatalog().getLang());
309+
pdfDocument.close();
310+
}
311+
312+
@Test
313+
//TODO: after DEVSIX-3243 fixed need to assert exact language and check assertEquals
314+
public void langAttrInFormForTaggedPdfTest() throws IOException, InterruptedException {
315+
String html = sourceFolder + "langAttrInFormForTaggedPdfTest.html";
316+
String outFile = destinationFolder + "langAttrInFormForTaggedPdfTest.pdf";
317+
String cmp = sourceFolder + "cmp_langAttrInFormForTaggedPdfTest.pdf";
318+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile));
319+
pdfDocument.setTagged();
320+
321+
HtmlConverter.convertToPdf(new FileInputStream(html), pdfDocument,
322+
new ConverterProperties().setBaseUri(sourceFolder));
323+
printOutputPdfNameAndDir(outFile);
324+
325+
PdfDocument document = new PdfDocument(new PdfReader(outFile));
326+
//compareByContent is used here to check the complete logical structure tree to notice all the differences.
327+
Assert.assertNull(new CompareTool().compareByContent(outFile, cmp, destinationFolder,
328+
"diff_forms"));
329+
Assert.assertNull(document.getCatalog().getLang());
330+
331+
document.close();
332+
}
333+
}
Lines changed: 10 additions & 0 deletions
Loading
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
</head>
6+
<body>
7+
<p lang="">Good morning</p>
8+
</body>
9+
</html>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Тег DIV</title>
6+
<style type="text/css">
7+
.b1 {
8+
width: 200px;
9+
height:200px;
10+
border: solid 1px red;
11+
}
12+
.b2 {
13+
width: 100px;
14+
height: 250px;
15+
border: solid 10px black;
16+
}
17+
</style>
18+
</head>
19+
<body>
20+
21+
<div class="b1" lang="la">Lorem ipsum dolor sit, amet consectetuer
22+
adipiscing elit, sed diem nonummy nibh euismod tincidunt ut lacreet
23+
dolore magna aliguam erat volutpat.<div class="b2" lang="en">I have a black cat</div></div>
24+
25+
<p>Lorem ipsum <span color="red" lang="ru">непонятный <span lang="en">text </span>текст</span> amet</p>
26+
27+
</body>
28+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Lang attribute</title>
6+
</head>
7+
<body>
8+
<p lang="en">English: Good day</p>
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)