Skip to content

Commit 41c0097

Browse files
committed
Cover list's alignment and direction with tests
DEVSIX-5423
1 parent c4ff48d commit 41c0097

File tree

104 files changed

+267
-0
lines changed

Some content is hidden

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

104 files changed

+267
-0
lines changed
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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+
package com.itextpdf.layout;
24+
25+
import com.itextpdf.io.LogMessageConstant;
26+
import com.itextpdf.io.util.MessageFormatUtil;
27+
import com.itextpdf.io.util.UrlUtil;
28+
import com.itextpdf.kernel.colors.ColorConstants;
29+
import com.itextpdf.kernel.pdf.PdfDocument;
30+
import com.itextpdf.kernel.pdf.PdfWriter;
31+
import com.itextpdf.kernel.utils.CompareTool;
32+
import com.itextpdf.layout.element.List;
33+
import com.itextpdf.layout.element.ListItem;
34+
import com.itextpdf.layout.element.Paragraph;
35+
import com.itextpdf.layout.property.BaseDirection;
36+
import com.itextpdf.layout.property.TextAlignment;
37+
import com.itextpdf.test.ExtendedITextTest;
38+
import com.itextpdf.test.annotations.LogMessage;
39+
import com.itextpdf.test.annotations.LogMessages;
40+
import com.itextpdf.test.annotations.type.IntegrationTest;
41+
42+
import java.io.FileOutputStream;
43+
import java.io.IOException;
44+
import java.nio.charset.StandardCharsets;
45+
import java.util.ArrayList;
46+
import org.junit.Assert;
47+
import org.junit.Test;
48+
import org.junit.experimental.categories.Category;
49+
import org.junit.runner.RunWith;
50+
import org.junit.runners.Parameterized;
51+
52+
@RunWith(Parameterized.class)
53+
@Category(IntegrationTest.class)
54+
public class ListAlignmentDirectionTest extends ExtendedITextTest {
55+
public static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/layout/ListAlignmentDirectionTest/";
56+
public static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/layout/ListAlignmentDirectionTest/";
57+
58+
private static final String PARAMETERS_NAME_PATTERN = "item-text-align: {0}; item-direction: {1}, "
59+
+ "list-text-align: {2}; list-direction: {3}";
60+
private static final String RESULTANT_FILE_NAME_PATTERN
61+
= "item-text-align-{0}_item-dir-{1}_list-text-align-{2}_list-dir-{3}";
62+
63+
private static final String HTML_PATTERN =
64+
"<ul style=\"background-color: green; width: 300pt; margin-left: 150pt; text-align: {2}; direction: {3}\">"
65+
+ " <li style=\"background-color: blue;\">Usual line</li>"
66+
+ " <li style=\"background-color: yellow; text-align: {0}; direction: {1}\">Specific line</li>"
67+
+ "</ul>";
68+
69+
private TextAlignment itemTextAlignment;
70+
private BaseDirection itemBaseDirection;
71+
private TextAlignment listTextAlignment;
72+
private BaseDirection listBaseDirection;
73+
74+
public ListAlignmentDirectionTest(Object itemTextAlignment, Object itemBaseDirection,
75+
Object listTextAlignment, Object listBaseDirection) throws IOException {
76+
this.itemTextAlignment = (TextAlignment) itemTextAlignment;
77+
this.itemBaseDirection = (BaseDirection) itemBaseDirection;
78+
this.listTextAlignment = (TextAlignment) listTextAlignment;
79+
this.listBaseDirection = (BaseDirection) listBaseDirection;
80+
81+
createOrClearDestinationFolder(DESTINATION_FOLDER);
82+
// Create an HTML for this test
83+
createHtml();
84+
}
85+
86+
@Parameterized.Parameters(name = PARAMETERS_NAME_PATTERN)
87+
public static Iterable<Object[]> alignItemsAndJustifyContentProperties() {
88+
TextAlignment[] alignmentTestValues = new TextAlignment[] {TextAlignment.LEFT, TextAlignment.CENTER,
89+
TextAlignment.RIGHT, TextAlignment.JUSTIFIED, TextAlignment.JUSTIFIED_ALL};
90+
BaseDirection[] directionTestValues = new BaseDirection[] {BaseDirection.LEFT_TO_RIGHT,
91+
BaseDirection.RIGHT_TO_LEFT};
92+
java.util.List<Object[]> objectList = new ArrayList<Object[]>();
93+
for (TextAlignment itemTA : alignmentTestValues) {
94+
for (BaseDirection itemBA : directionTestValues) {
95+
for (TextAlignment listTA : alignmentTestValues) {
96+
for (BaseDirection listBA : directionTestValues) {
97+
objectList.add(new Object[] {itemTA, itemBA, listTA, listBA});
98+
}
99+
}
100+
}
101+
}
102+
return objectList;
103+
}
104+
105+
@Test
106+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.TYPOGRAPHY_NOT_FOUND, count = 8))
107+
// TODO DEVSIX-5727 direction of the first list-item should define the symbol indent's side. Once the issue
108+
// is fixed, the corresponding cmps should be updated.
109+
public void alignmentDirectionTest() throws Exception {
110+
String fileName = MessageFormatUtil.format(
111+
RESULTANT_FILE_NAME_PATTERN,
112+
formatTextAlignment(itemTextAlignment),
113+
formatBaseDirection(itemBaseDirection),
114+
formatTextAlignment(listTextAlignment),
115+
formatBaseDirection(listBaseDirection));
116+
String outFileName = DESTINATION_FOLDER + fileName + ".pdf";
117+
String cmpFileName = SOURCE_FOLDER + "cmp_" + fileName + ".pdf";
118+
119+
PdfDocument pdf = new PdfDocument(new PdfWriter(outFileName));
120+
Document document = new Document(pdf);
121+
122+
Style style = new Style()
123+
.setTextAlignment(itemTextAlignment)
124+
.setBaseDirection(itemBaseDirection);
125+
List list = createTestList(style);
126+
list.setTextAlignment(listTextAlignment);
127+
list.setBaseDirection(listBaseDirection);
128+
129+
document.add(list);
130+
document.close();
131+
132+
System.out.println("HTML: " + UrlUtil.getNormalizedFileUriString(DESTINATION_FOLDER + fileName + ".html") + "\n");
133+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, DESTINATION_FOLDER, "diff_"));
134+
}
135+
136+
private static List createTestList(Style secondItemStyle) {
137+
List list = new List();
138+
list.setSymbolIndent(20);
139+
list.setListSymbol("\u2022");
140+
list.setBackgroundColor(ColorConstants.GREEN);
141+
list.setWidth(300);
142+
list.setMarginLeft(150);
143+
144+
ListItem listItem1 = new ListItem();
145+
listItem1.add(new Paragraph("Usual item"));
146+
listItem1.setBackgroundColor(ColorConstants.BLUE);
147+
list.add(listItem1);
148+
149+
ListItem listItem2 = new ListItem();
150+
listItem2.addStyle(secondItemStyle);
151+
listItem2.add(new Paragraph("Specific item"));
152+
listItem2.setBackgroundColor(ColorConstants.YELLOW);
153+
list.add(listItem2);
154+
155+
return list;
156+
}
157+
158+
private void createHtml() throws IOException {
159+
String fileName = MessageFormatUtil.format(
160+
RESULTANT_FILE_NAME_PATTERN,
161+
formatTextAlignment(itemTextAlignment),
162+
formatBaseDirection(itemBaseDirection),
163+
formatTextAlignment(listTextAlignment),
164+
formatBaseDirection(listBaseDirection));
165+
166+
String htmlString = MessageFormatUtil.format(
167+
HTML_PATTERN,
168+
formatTextAlignment(itemTextAlignment, true),
169+
formatBaseDirection(itemBaseDirection),
170+
formatTextAlignment(listTextAlignment, true),
171+
formatBaseDirection(listBaseDirection));
172+
try (FileOutputStream htmlFile =
173+
new FileOutputStream(DESTINATION_FOLDER + fileName + ".html")) {
174+
byte[] htmlBytes = htmlString.getBytes(StandardCharsets.UTF_8);
175+
htmlFile.write(htmlBytes, 0, htmlBytes.length);
176+
}
177+
}
178+
179+
private static String formatTextAlignment(TextAlignment alignment) {
180+
return formatTextAlignment(alignment, false);
181+
}
182+
183+
private static String formatTextAlignment(TextAlignment alignment, boolean isHtml) {
184+
switch (alignment) {
185+
case LEFT:
186+
return "left";
187+
case RIGHT:
188+
return "right";
189+
case CENTER:
190+
return "center";
191+
case JUSTIFIED:
192+
return "justify";
193+
case JUSTIFIED_ALL:
194+
return isHtml ? "justify" : "justify-all";
195+
default:
196+
Assert.fail("Unexpected text alignment");
197+
return null;
198+
}
199+
}
200+
201+
private static String formatBaseDirection(BaseDirection direction) {
202+
switch (direction) {
203+
case LEFT_TO_RIGHT:
204+
return "ltr";
205+
case RIGHT_TO_LEFT:
206+
return "rtl";
207+
default:
208+
Assert.fail("Unexpected base direction");
209+
return null;
210+
}
211+
}
212+
}

layout/src/test/java/com/itextpdf/layout/ListTest.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,4 +615,59 @@ public void listSymbolForcedPlacement01() throws Exception {
615615
// TODO DEVSIX-1655: partially not fitting list symbol not shown at all, however this might be improved
616616
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff_"));
617617
}
618+
619+
@Test
620+
// There is no symbol indent in html: one uses margins for such a purpose.
621+
public void bothSymbolIndentAndMarginAreSetTest() throws Exception {
622+
String outFileName = destinationFolder + "bothSymbolIndentAndMarginAreSetTest.pdf";
623+
String cmpFileName = sourceFolder + "cmp_bothSymbolIndentAndMarginAreSetTest.pdf";
624+
625+
PdfDocument pdf = new PdfDocument(new PdfWriter(outFileName));
626+
Document document = new Document(pdf);
627+
628+
List l = createTestList();
629+
630+
ListItem li = new ListItem("Only symbol indent: 50pt");
631+
li.setBackgroundColor(ColorConstants.BLUE);
632+
l.add(li);
633+
li = new ListItem("Symbol indent: 50pt and margin-left: 50pt = 100pt");
634+
li.setMarginLeft(50);
635+
li.setBackgroundColor(ColorConstants.YELLOW);
636+
l.add(li);
637+
638+
document.add(l);
639+
document.close();
640+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff_"));
641+
}
642+
643+
@Test
644+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.PROPERTY_IN_PERCENTS_NOT_SUPPORTED))
645+
public void listItemMarginInPercentTest() throws Exception {
646+
String outFileName = destinationFolder + "listItemMarginInPercentTest.pdf";
647+
String cmpFileName = sourceFolder + "cmp_listItemMarginInPercentTest.pdf";
648+
649+
PdfDocument pdf = new PdfDocument(new PdfWriter(outFileName));
650+
Document document = new Document(pdf);
651+
652+
List l = createTestList();
653+
654+
ListItem li = new ListItem("Left margin in percent: 50%");
655+
li.setProperty(Property.MARGIN_LEFT, UnitValue.createPercentValue(50));
656+
li.setBackgroundColor(ColorConstants.BLUE);
657+
l.add(li);
658+
659+
document.add(l);
660+
document.close();
661+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff_"));
662+
}
663+
664+
private static List createTestList() {
665+
List l = new List();
666+
l.setWidth(300);
667+
l.setBackgroundColor(ColorConstants.RED);
668+
l.setSymbolIndent(50);
669+
l.setListSymbol("\u2022");
670+
671+
return l;
672+
}
618673
}

0 commit comments

Comments
 (0)