Skip to content

Commit ff2f221

Browse files
committed
Cover keepTogether + multicol layouting with tests
DEVSIX-7550
1 parent 5e8bcae commit ff2f221

File tree

6 files changed

+106
-7
lines changed

6 files changed

+106
-7
lines changed

layout/src/main/java/com/itextpdf/layout/renderer/MulticolRenderer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ This file is part of the iText (R) project.
3434

3535
import java.util.ArrayList;
3636
import java.util.List;
37-
import java.util.function.BiFunction;
3837

3938
/**
4039
* Represents a renderer for columns.
@@ -136,6 +135,8 @@ protected AbstractRenderer createSplitRenderer(List<IRenderer> children) {
136135
/**
137136
* Creates an overflow renderer.
138137
*
138+
* @param overflowedContentRenderer an overflowed content renderer
139+
*
139140
* @return a new {@link AbstractRenderer} instance
140141
*/
141142
protected AbstractRenderer createOverflowRenderer(IRenderer overflowedContentRenderer) {

layout/src/test/java/com/itextpdf/layout/element/MulticolContainerTest.java

Lines changed: 104 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ This file is part of the iText (R) project.
3939
import com.itextpdf.layout.properties.Property;
4040
import com.itextpdf.layout.properties.UnitValue;
4141
import com.itextpdf.test.ExtendedITextTest;
42+
import com.itextpdf.test.LogLevelConstants;
4243
import com.itextpdf.test.annotations.LogMessage;
4344
import com.itextpdf.test.annotations.LogMessages;
4445
import com.itextpdf.test.annotations.type.IntegrationTest;
@@ -366,6 +367,104 @@ public void continuousColumContainerMultipleElementsBorder() throws IOException,
366367
});
367368
}
368369

370+
@Test
371+
@LogMessages(messages = {
372+
@LogMessage(messageTemplate = LayoutLogMessageConstant.ELEMENT_DOES_NOT_FIT_AREA, logLevel = LogLevelConstants.WARN)
373+
})
374+
public void multicolElementWithKeepTogetherTest() throws IOException, InterruptedException {
375+
executeTest("multicolElementWithKeepTogether", new MulticolContainer(), ctx -> {
376+
ctx.setProperty(Property.COLUMN_COUNT, 3);
377+
ctx.setBackgroundColor(DEFAULT_BACKGROUND_COLOR);
378+
Div pseudoContainer = new Div();
379+
for (int i = 0; i < 30; i++) {
380+
pseudoContainer.add(new Paragraph("" + i));
381+
}
382+
ctx.setProperty(Property.KEEP_TOGETHER, true);
383+
ctx.add(pseudoContainer);
384+
});
385+
}
386+
387+
@Test
388+
@LogMessages(messages = {
389+
@LogMessage(messageTemplate = LayoutLogMessageConstant.ELEMENT_DOES_NOT_FIT_AREA, logLevel = LogLevelConstants.WARN)
390+
})
391+
public void allChildrenOfMulticolElementWithKeepTogetherTest() throws IOException, InterruptedException {
392+
executeTest("allChildrenOfMulticolElementWithKeepTogether", new MulticolContainer(), ctx -> {
393+
ctx.setProperty(Property.COLUMN_COUNT, 3);
394+
ctx.setBackgroundColor(DEFAULT_BACKGROUND_COLOR);
395+
Div pseudoContainer = new Div();
396+
for (int i = 0; i < 30; i++) {
397+
pseudoContainer.add(new Paragraph("" + i));
398+
}
399+
pseudoContainer.setProperty(Property.KEEP_TOGETHER, true);
400+
ctx.add(pseudoContainer);
401+
});
402+
}
403+
404+
@Test
405+
public void childOfMulticolElementWithKeepTogetherTest() throws IOException, InterruptedException {
406+
executeTest("childOfMulticolElementWithKeepTogether", new MulticolContainer(), ctx -> {
407+
ctx.setProperty(Property.COLUMN_COUNT, 3);
408+
ctx.setBackgroundColor(DEFAULT_BACKGROUND_COLOR);
409+
Div pseudoContainer = new Div();
410+
for (int i = 0; i < 7; i++) {
411+
pseudoContainer.add(new Paragraph("" + i));
412+
}
413+
Div temp = new Div();
414+
temp.add(new Paragraph("7 keep"));
415+
temp.add(new Paragraph("8 keep"));
416+
temp.add(new Paragraph("9 keep"));
417+
temp.add(new Paragraph("10 keep"));
418+
temp.add(new Paragraph("11 keep"));
419+
temp.setProperty(Property.KEEP_TOGETHER, true);
420+
pseudoContainer.add(temp);
421+
422+
for (int i = 12; i < 30; i++) {
423+
pseudoContainer.add(new Paragraph("" + i));
424+
}
425+
ctx.add(pseudoContainer);
426+
});
427+
}
428+
429+
@Test
430+
public void childrenOfMulticolElementWithKeepTogetherTest() throws IOException, InterruptedException {
431+
executeTest("childrenOfMulticolElementWithKeepTogether", new MulticolContainer(), ctx -> {
432+
ctx.setProperty(Property.COLUMN_COUNT, 3);
433+
ctx.setBackgroundColor(DEFAULT_BACKGROUND_COLOR);
434+
Div pseudoContainer = new Div();
435+
for (int i = 0; i < 7; i++) {
436+
pseudoContainer.add(new Paragraph("" + i));
437+
}
438+
Div temp = new Div();
439+
temp.add(new Paragraph("7 keep"));
440+
temp.add(new Paragraph("8 keep"));
441+
temp.add(new Paragraph("9 keep"));
442+
temp.add(new Paragraph("10 keep"));
443+
temp.add(new Paragraph("11 keep"));
444+
temp.setProperty(Property.KEEP_TOGETHER, Boolean.TRUE);
445+
pseudoContainer.add(temp);
446+
447+
for (int i = 12; i < 19; i++) {
448+
pseudoContainer.add(new Paragraph("" + i));
449+
}
450+
temp = new Div();
451+
temp.add(new Paragraph("19 keep"));
452+
temp.add(new Paragraph("20 keep"));
453+
temp.add(new Paragraph("21 keep"));
454+
temp.add(new Paragraph("22 keep"));
455+
temp.add(new Paragraph("23 keep"));
456+
temp.add(new Paragraph("24 keep"));
457+
temp.add(new Paragraph("25 keep"));
458+
temp.setProperty(Property.KEEP_TOGETHER, Boolean.TRUE);
459+
pseudoContainer.add(temp);
460+
461+
for (int i = 26; i < 30; i++) {
462+
pseudoContainer.add(new Paragraph("" + i));
463+
}
464+
ctx.add(pseudoContainer);
465+
});
466+
}
467+
369468
@Test
370469
public void singleParagraphMultiPageTest() throws IOException, InterruptedException {
371470
String outFileName = DESTINATION_FOLDER + "singleParagraphMultiPageTest.pdf";
@@ -551,7 +650,7 @@ public void overflowingDivWithParagraphMultipageTest() throws IOException, Inter
551650
Div columnDiv = new Div();
552651
columnDiv.setProperty(Property.BORDER, new SolidBorder(1));
553652
columnDiv.setProperty(Property.BACKGROUND, new Background(ColorConstants.BLUE));
554-
columnDiv.setProperty(Property.KEEP_TOGETHER, true);
653+
columnDiv.setProperty(Property.KEEP_TOGETHER, Boolean.TRUE);
555654
columnDiv.setProperty(Property.WIDTH, UnitValue.createPointValue(50));
556655
columnDiv.setProperty(Property.HEIGHT, UnitValue.createPointValue(150));
557656

@@ -582,7 +681,7 @@ public void marginCantFitCurrentPageTest() throws IOException, InterruptedExcept
582681
Div columnDiv = new Div();
583682
columnDiv.setProperty(Property.BORDER, new SolidBorder(1));
584683
columnDiv.setProperty(Property.BACKGROUND, new Background(ColorConstants.BLUE));
585-
columnDiv.setProperty(Property.KEEP_TOGETHER, true);
684+
columnDiv.setProperty(Property.KEEP_TOGETHER, Boolean.TRUE);
586685
columnDiv.setProperty(Property.MARGIN_BOTTOM, UnitValue.createPointValue(40));
587686
columnDiv.setProperty(Property.WIDTH, UnitValue.createPointValue(60));
588687
columnDiv.setProperty(Property.HEIGHT, UnitValue.createPointValue(60));
@@ -614,7 +713,7 @@ public void paddingCantFitCurrentPageTest() throws IOException, InterruptedExcep
614713
Div columnDiv = new Div();
615714
columnDiv.setProperty(Property.BORDER, new SolidBorder(1));
616715
columnDiv.setProperty(Property.BACKGROUND, new Background(ColorConstants.BLUE));
617-
columnDiv.setProperty(Property.KEEP_TOGETHER, true);
716+
columnDiv.setProperty(Property.KEEP_TOGETHER, Boolean.TRUE);
618717
columnDiv.setProperty(Property.PADDING_BOTTOM, UnitValue.createPointValue(40));
619718
columnDiv.setProperty(Property.WIDTH, UnitValue.createPointValue(60));
620719
columnDiv.setProperty(Property.HEIGHT, UnitValue.createPointValue(60));
@@ -649,7 +748,7 @@ public void keepTogetherBlockingLayoutTest() throws IOException, InterruptedExce
649748
"id est laborum.");
650749
paragraph.setBorder(new SolidBorder(2));
651750
paragraph.setFontSize(20);
652-
paragraph.setProperty(Property.KEEP_TOGETHER, true);
751+
paragraph.setProperty(Property.KEEP_TOGETHER, Boolean.TRUE);
653752

654753
columnContainer.setBorder(new SolidBorder(ColorConstants.RED, 3));
655754
Div div = new Div();
@@ -661,14 +760,13 @@ public void keepTogetherBlockingLayoutTest() throws IOException, InterruptedExce
661760
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, DESTINATION_FOLDER, "diff"));
662761
}
663762

664-
private <T extends IBlockElement> void executeTest(String testName, T container, Consumer<T> executor)
763+
private void executeTest(String testName, MulticolContainer container, Consumer<MulticolContainer> executor)
665764
throws IOException, InterruptedException {
666765
String filename = DESTINATION_FOLDER + testName + ".pdf";
667766
String cmpName = SOURCE_FOLDER + "cmp_" + testName + ".pdf";
668767
try (PdfDocument pdfDoc = new PdfDocument(new com.itextpdf.kernel.pdf.PdfWriter(filename))) {
669768
Document doc = new Document(pdfDoc);
670769

671-
container.setProperty(Property.TREAT_AS_CONTINUOUS_CONTAINER, true);
672770
executor.accept(container);
673771

674772
doc.add(new Paragraph("ELEMENT ABOVE").setBackgroundColor(ColorConstants.YELLOW));

0 commit comments

Comments
 (0)