Skip to content

Commit

Permalink
Cover list's alignment and direction with tests
Browse files Browse the repository at this point in the history
DEVSIX-5423
  • Loading branch information
ars18wrw committed Aug 4, 2021
1 parent 6b61c85 commit 0898785
Show file tree
Hide file tree
Showing 104 changed files with 267 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2021 iText Group NV
Authors: iText Software.
This program is offered under a commercial and under the AGPL license.
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
AGPL licensing:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itextpdf.layout;

import com.itextpdf.io.LogMessageConstant;
import com.itextpdf.io.util.MessageFormatUtil;
import com.itextpdf.io.util.UrlUtil;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.utils.CompareTool;
import com.itextpdf.layout.element.List;
import com.itextpdf.layout.element.ListItem;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.properties.BaseDirection;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.test.ExtendedITextTest;
import com.itextpdf.test.annotations.LogMessage;
import com.itextpdf.test.annotations.LogMessages;
import com.itextpdf.test.annotations.type.IntegrationTest;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
@Category(IntegrationTest.class)
public class ListAlignmentDirectionTest extends ExtendedITextTest {
public static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/layout/ListAlignmentDirectionTest/";
public static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/layout/ListAlignmentDirectionTest/";

private static final String PARAMETERS_NAME_PATTERN = "item-text-align: {0}; item-direction: {1}, "
+ "list-text-align: {2}; list-direction: {3}";
private static final String RESULTANT_FILE_NAME_PATTERN
= "item-text-align-{0}_item-dir-{1}_list-text-align-{2}_list-dir-{3}";

private static final String HTML_PATTERN =
"<ul style=\"background-color: green; width: 300pt; margin-left: 150pt; text-align: {2}; direction: {3}\">"
+ " <li style=\"background-color: blue;\">Usual line</li>"
+ " <li style=\"background-color: yellow; text-align: {0}; direction: {1}\">Specific line</li>"
+ "</ul>";

private TextAlignment itemTextAlignment;
private BaseDirection itemBaseDirection;
private TextAlignment listTextAlignment;
private BaseDirection listBaseDirection;

public ListAlignmentDirectionTest(Object itemTextAlignment, Object itemBaseDirection,
Object listTextAlignment, Object listBaseDirection) throws IOException {
this.itemTextAlignment = (TextAlignment) itemTextAlignment;
this.itemBaseDirection = (BaseDirection) itemBaseDirection;
this.listTextAlignment = (TextAlignment) listTextAlignment;
this.listBaseDirection = (BaseDirection) listBaseDirection;

createOrClearDestinationFolder(DESTINATION_FOLDER);
// Create an HTML for this test
createHtml();
}

@Parameterized.Parameters(name = PARAMETERS_NAME_PATTERN)
public static Iterable<Object[]> alignItemsAndJustifyContentProperties() {
TextAlignment[] alignmentTestValues = new TextAlignment[] {TextAlignment.LEFT, TextAlignment.CENTER,
TextAlignment.RIGHT, TextAlignment.JUSTIFIED, TextAlignment.JUSTIFIED_ALL};
BaseDirection[] directionTestValues = new BaseDirection[] {BaseDirection.LEFT_TO_RIGHT,
BaseDirection.RIGHT_TO_LEFT};
java.util.List<Object[]> objectList = new ArrayList<Object[]>();
for (TextAlignment itemTA : alignmentTestValues) {
for (BaseDirection itemBA : directionTestValues) {
for (TextAlignment listTA : alignmentTestValues) {
for (BaseDirection listBA : directionTestValues) {
objectList.add(new Object[] {itemTA, itemBA, listTA, listBA});
}
}
}
}
return objectList;
}

@Test
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.TYPOGRAPHY_NOT_FOUND, count = 8))
// TODO DEVSIX-5727 direction of the first list-item should define the symbol indent's side. Once the issue
// is fixed, the corresponding cmps should be updated.
public void alignmentDirectionTest() throws Exception {
String fileName = MessageFormatUtil.format(
RESULTANT_FILE_NAME_PATTERN,
formatTextAlignment(itemTextAlignment),
formatBaseDirection(itemBaseDirection),
formatTextAlignment(listTextAlignment),
formatBaseDirection(listBaseDirection));
String outFileName = DESTINATION_FOLDER + fileName + ".pdf";
String cmpFileName = SOURCE_FOLDER + "cmp_" + fileName + ".pdf";

PdfDocument pdf = new PdfDocument(new PdfWriter(outFileName));
Document document = new Document(pdf);

Style style = new Style()
.setTextAlignment(itemTextAlignment)
.setBaseDirection(itemBaseDirection);
List list = createTestList(style);
list.setTextAlignment(listTextAlignment);
list.setBaseDirection(listBaseDirection);

document.add(list);
document.close();

System.out.println("HTML: " + UrlUtil.getNormalizedFileUriString(DESTINATION_FOLDER + fileName + ".html") + "\n");
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, DESTINATION_FOLDER, "diff_"));
}

private static List createTestList(Style secondItemStyle) {
List list = new List();
list.setSymbolIndent(20);
list.setListSymbol("\u2022");
list.setBackgroundColor(ColorConstants.GREEN);
list.setWidth(300);
list.setMarginLeft(150);

ListItem listItem1 = new ListItem();
listItem1.add(new Paragraph("Usual item"));
listItem1.setBackgroundColor(ColorConstants.BLUE);
list.add(listItem1);

ListItem listItem2 = new ListItem();
listItem2.addStyle(secondItemStyle);
listItem2.add(new Paragraph("Specific item"));
listItem2.setBackgroundColor(ColorConstants.YELLOW);
list.add(listItem2);

return list;
}

private void createHtml() throws IOException {
String fileName = MessageFormatUtil.format(
RESULTANT_FILE_NAME_PATTERN,
formatTextAlignment(itemTextAlignment),
formatBaseDirection(itemBaseDirection),
formatTextAlignment(listTextAlignment),
formatBaseDirection(listBaseDirection));

String htmlString = MessageFormatUtil.format(
HTML_PATTERN,
formatTextAlignment(itemTextAlignment, true),
formatBaseDirection(itemBaseDirection),
formatTextAlignment(listTextAlignment, true),
formatBaseDirection(listBaseDirection));
try (FileOutputStream htmlFile =
new FileOutputStream(DESTINATION_FOLDER + fileName + ".html")) {
byte[] htmlBytes = htmlString.getBytes(StandardCharsets.UTF_8);
htmlFile.write(htmlBytes, 0, htmlBytes.length);
}
}

private static String formatTextAlignment(TextAlignment alignment) {
return formatTextAlignment(alignment, false);
}

private static String formatTextAlignment(TextAlignment alignment, boolean isHtml) {
switch (alignment) {
case LEFT:
return "left";
case RIGHT:
return "right";
case CENTER:
return "center";
case JUSTIFIED:
return "justify";
case JUSTIFIED_ALL:
return isHtml ? "justify" : "justify-all";
default:
Assert.fail("Unexpected text alignment");
return null;
}
}

private static String formatBaseDirection(BaseDirection direction) {
switch (direction) {
case LEFT_TO_RIGHT:
return "ltr";
case RIGHT_TO_LEFT:
return "rtl";
default:
Assert.fail("Unexpected base direction");
return null;
}
}
}
55 changes: 55 additions & 0 deletions layout/src/test/java/com/itextpdf/layout/ListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -615,4 +615,59 @@ public void listSymbolForcedPlacement01() throws Exception {
// TODO DEVSIX-1655: partially not fitting list symbol not shown at all, however this might be improved
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff_"));
}

@Test
// There is no symbol indent in html: one uses margins for such a purpose.
public void bothSymbolIndentAndMarginAreSetTest() throws Exception {
String outFileName = destinationFolder + "bothSymbolIndentAndMarginAreSetTest.pdf";
String cmpFileName = sourceFolder + "cmp_bothSymbolIndentAndMarginAreSetTest.pdf";

PdfDocument pdf = new PdfDocument(new PdfWriter(outFileName));
Document document = new Document(pdf);

List l = createTestList();

ListItem li = new ListItem("Only symbol indent: 50pt");
li.setBackgroundColor(ColorConstants.BLUE);
l.add(li);
li = new ListItem("Symbol indent: 50pt and margin-left: 50pt = 100pt");
li.setMarginLeft(50);
li.setBackgroundColor(ColorConstants.YELLOW);
l.add(li);

document.add(l);
document.close();
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff_"));
}

@Test
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.PROPERTY_IN_PERCENTS_NOT_SUPPORTED))
public void listItemMarginInPercentTest() throws Exception {
String outFileName = destinationFolder + "listItemMarginInPercentTest.pdf";
String cmpFileName = sourceFolder + "cmp_listItemMarginInPercentTest.pdf";

PdfDocument pdf = new PdfDocument(new PdfWriter(outFileName));
Document document = new Document(pdf);

List l = createTestList();

ListItem li = new ListItem("Left margin in percent: 50%");
li.setProperty(Property.MARGIN_LEFT, UnitValue.createPercentValue(50));
li.setBackgroundColor(ColorConstants.BLUE);
l.add(li);

document.add(l);
document.close();
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff_"));
}

private static List createTestList() {
List l = new List();
l.setWidth(300);
l.setBackgroundColor(ColorConstants.RED);
l.setSymbolIndent(50);
l.setListSymbol("\u2022");

return l;
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 0898785

Please sign in to comment.