Skip to content

Commit 4b7a54d

Browse files
committed
Add tests for text direction
DEVSIX-8195
1 parent 5191efc commit 4b7a54d

File tree

9 files changed

+250
-0
lines changed

9 files changed

+250
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2024 Apryse Group NV
4+
Authors: Apryse 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+
package com.itextpdf.html2pdf.css;
24+
25+
import com.itextpdf.html2pdf.ExtendedHtmlConversionITextTest;
26+
import com.itextpdf.html2pdf.HtmlConverter;
27+
import com.itextpdf.io.logs.IoLogMessageConstant;
28+
import com.itextpdf.io.util.UrlUtil;
29+
import com.itextpdf.kernel.pdf.PdfDocument;
30+
import com.itextpdf.kernel.pdf.PdfReader;
31+
import com.itextpdf.kernel.pdf.canvas.parser.PdfTextExtractor;
32+
import com.itextpdf.test.LogLevelConstants;
33+
import com.itextpdf.test.annotations.LogMessage;
34+
import com.itextpdf.test.annotations.LogMessages;
35+
import com.itextpdf.test.annotations.type.IntegrationTest;
36+
37+
import java.io.File;
38+
import java.io.IOException;
39+
import org.junit.Assert;
40+
import org.junit.BeforeClass;
41+
import org.junit.Test;
42+
import org.junit.experimental.categories.Category;
43+
44+
@Category(IntegrationTest.class)
45+
public class DirectionTest extends ExtendedHtmlConversionITextTest {
46+
private static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/html2pdf/css/DirectionTest/";
47+
private static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/html2pdf/css/DirectionTest/";
48+
49+
@BeforeClass
50+
public static void beforeClass() {
51+
createDestinationFolder(DESTINATION_FOLDER);
52+
}
53+
54+
@Test
55+
@LogMessages(messages = {
56+
@LogMessage(messageTemplate = IoLogMessageConstant.TYPOGRAPHY_NOT_FOUND, count = 2, logLevel =
57+
LogLevelConstants.WARN)
58+
})
59+
public void simpleLtrDocTest() throws IOException {
60+
Assert.assertTrue(getTextFromDocument(convertToHtmlDocument("SimpleLtrDoc"),
61+
1).contains("123456789."));
62+
}
63+
64+
@Test
65+
public void simpleLtrElementDocTest() throws IOException {
66+
Assert.assertTrue(getTextFromDocument(convertToHtmlDocument("SimpleLtrElementDoc"),
67+
1).contains("123456789."));
68+
}
69+
70+
//TODO DEVSIX-1920: RTL ignored. Change test after fix
71+
@Test
72+
public void simpleRtlElementDocTest() throws IOException {
73+
Assert.assertFalse(getTextFromDocument(convertToHtmlDocument("SimpleRtlElementDoc"),
74+
1).contains(".Right to left text"));
75+
}
76+
77+
//TODO DEVSIX-2437 : Change test after fix
78+
@Test
79+
@LogMessages(messages = {
80+
@LogMessage(messageTemplate = IoLogMessageConstant.TYPOGRAPHY_NOT_FOUND, count = 4, logLevel =
81+
LogLevelConstants.WARN)
82+
})
83+
public void ltrInRtlDocTest() throws IOException {
84+
Assert.assertFalse(getTextFromDocument(convertToHtmlDocument("LtrInRtlDoc"),
85+
1).contains("!Right to left text"));
86+
}
87+
88+
//TODO DEVSIX-2437 : Change test after fix
89+
@Test
90+
@LogMessages(messages = {
91+
@LogMessage(messageTemplate = IoLogMessageConstant.TYPOGRAPHY_NOT_FOUND, count = 4, logLevel =
92+
LogLevelConstants.WARN)
93+
})
94+
public void rtlInLtrDocTest() throws IOException {
95+
Assert.assertFalse(getTextFromDocument(convertToHtmlDocument("RtlInLtrDoc"),
96+
1).contains("!Right to left text"));
97+
}
98+
99+
//TODO DEVSIX-3069: Change test after fix
100+
@Test
101+
@LogMessages(messages = {
102+
@LogMessage(messageTemplate = IoLogMessageConstant.TYPOGRAPHY_NOT_FOUND, count = 34, logLevel =
103+
LogLevelConstants.WARN),
104+
@LogMessage(messageTemplate = IoLogMessageConstant.TABLE_WIDTH_IS_MORE_THAN_EXPECTED_DUE_TO_MIN_WIDTH,
105+
count = 1, logLevel =
106+
LogLevelConstants.WARN)
107+
})
108+
public void bigTableTest() throws IOException, InterruptedException {
109+
convertToPdfAndCompare("TooLargeTable", SOURCE_FOLDER, DESTINATION_FOLDER);
110+
}
111+
112+
113+
private PdfDocument convertToHtmlDocument(String fileName) throws IOException {
114+
String sourceHtml = SOURCE_FOLDER + fileName + ".html";
115+
String destPdf = DESTINATION_FOLDER + fileName + ".pdf";
116+
HtmlConverter.convertToPdf(new File(sourceHtml), new File(destPdf));
117+
System.out.println("html: " + UrlUtil.getNormalizedFileUriString(sourceHtml) + "\n"
118+
+ "Out pdf: "+ UrlUtil.getNormalizedFileUriString(destPdf));
119+
return new PdfDocument(new PdfReader(destPdf));
120+
}
121+
122+
private String getTextFromDocument(PdfDocument document, int pageNum) {
123+
return PdfTextExtractor.getTextFromPage(document.getPage(pageNum));
124+
}
125+
}

src/test/java/com/itextpdf/html2pdf/element/BdoTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public static void beforeClass() {
4242
createDestinationFolder(destinationFolder);
4343
}
4444

45+
//TODO DEVSIX-2091: Change after fixing
4546
@Test
4647
public void bdo01Test() throws IOException, InterruptedException {
4748
convertToPdfAndCompare("bdoTest01", sourceFolder, destinationFolder);}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en" dir="rtl">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>LTR commbined with RTL</title>
6+
</head>
7+
<body>
8+
<div>Right to left text!</div>
9+
<span dir="ltr">Left to right text 12345!</span>
10+
</body>
11+
</html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>RTL commbined with LTR</title>
6+
</head>
7+
<body dir="ltr">
8+
<div>Left to right text!</div>
9+
<span dir="rtl">Right to left text 12345!</span>
10+
</body>
11+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>LTR document</title>
6+
</head>
7+
<body dir="ltr">
8+
<div>Right to left text! 123456789.</div>
9+
</body>
10+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>LTR document</title>
6+
</head>
7+
<body>
8+
<span dir="ltr">Right to left text! 123456789.</span>
9+
</body>
10+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>RTL document</title>
6+
</head>
7+
<body>
8+
<span dir="rtl">Right to left text! 123456789.</span>
9+
</body>
10+
</html>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<style>
5+
.header
6+
{
7+
direction:rtl;
8+
font-size: 12pt ;
9+
margin: 40px
10+
}
11+
</style>
12+
</head>
13+
<body>
14+
15+
<h2 align="center">
16+
<strong><u>Test with big table</u></strong>
17+
</h2>
18+
<p class="header">testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest
19+
</p>
20+
<div align="ceneter" dir="rtl" >
21+
<table dir="rtl" border="1" cellspacing="0" cellpadding="3px" width="94%">
22+
<tbody dir="rtl">
23+
<tr dir="rtl">
24+
<td width="57" rowspan="2" valign="top">
25+
<p class="header">
26+
<strong>test</strong>
27+
</p>
28+
</td>
29+
<td width="132" rowspan="2" valign="top">
30+
<p class="header">
31+
<strong>test</strong>
32+
</p>
33+
</td>
34+
<td width="104" rowspan="2" valign="top">
35+
<p class="header">
36+
<strong>test</strong>
37+
</p>
38+
</td>
39+
<td width="85" rowspan="2" valign="top">
40+
<p class="header">
41+
<strong>test</strong>
42+
</p>
43+
</td>
44+
<td width="302" colspan="3" valign="top">
45+
<p class="header">
46+
<strong>test</strong>
47+
</p>
48+
</td>
49+
</tr>
50+
<tr dir="rtl">
51+
<td width="113" valign="top">
52+
<p class="header">
53+
<strong>test</strong>
54+
</p>
55+
</td>
56+
<td width="86" valign="top">
57+
<p class="header">
58+
<strong>test</strong>
59+
</p>
60+
</td>
61+
<td width="103" valign="top">
62+
<p class="header">
63+
<strong>test</strong>
64+
</p>
65+
</td>
66+
</tr>
67+
</tbody>
68+
</table>
69+
</div>
70+
71+
</body>
72+
</html>
Binary file not shown.

0 commit comments

Comments
 (0)