Skip to content

Commit 1bf3e35

Browse files
ars18wrwiText-CI
authored andcommitted
Cover layer related logic with tests.
Implemented during the second technical debt session.
1 parent 7450025 commit 1bf3e35

14 files changed

+517
-0
lines changed

kernel/src/test/java/com/itextpdf/kernel/pdf/layer/PdfLayerMembershipTest.java

Lines changed: 370 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.itextpdf.kernel.pdf.layer;
2+
3+
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
4+
5+
class PdfLayerTestUtils {
6+
7+
public static void addTextInsideLayer(IPdfOCG layer, PdfCanvas canvas, String text, float x, float y) {
8+
canvas
9+
.beginLayer(layer)
10+
.beginText()
11+
.moveText(x, y)
12+
.showText(text)
13+
.endText()
14+
.endLayer();
15+
}
16+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2019 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
package com.itextpdf.kernel.pdf.layer;
44+
45+
import com.itextpdf.kernel.pdf.PdfArray;
46+
import com.itextpdf.kernel.pdf.PdfDictionary;
47+
import com.itextpdf.kernel.pdf.PdfDocument;
48+
import com.itextpdf.kernel.pdf.PdfName;
49+
import com.itextpdf.kernel.pdf.PdfObject;
50+
import com.itextpdf.kernel.pdf.PdfWriter;
51+
import com.itextpdf.test.ExtendedITextTest;
52+
import com.itextpdf.test.annotations.type.UnitTest;
53+
import org.junit.Assert;
54+
import org.junit.Test;
55+
import org.junit.experimental.categories.Category;
56+
57+
import java.io.ByteArrayOutputStream;
58+
import java.io.IOException;
59+
60+
@Category(UnitTest.class)
61+
public class PdfVisibilityExpressionTest extends ExtendedITextTest {
62+
63+
@Test
64+
public void expressionByArrayTest() {
65+
PdfDocument tempDoc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
66+
67+
PdfArray array = new PdfArray();
68+
69+
// add the AND operator as the first parameter of the expression
70+
array.add(PdfName.And);
71+
72+
// add two empty dictionaries as the other parameters
73+
array.add(new PdfLayer((PdfDictionary) new PdfDictionary().makeIndirect(tempDoc)).getPdfObject());
74+
array.add(new PdfLayer((PdfDictionary) new PdfDictionary().makeIndirect(tempDoc)).getPdfObject());
75+
76+
// create visibility expression
77+
PdfVisibilityExpression expression = new PdfVisibilityExpression(array);
78+
79+
PdfObject expressionObject = expression.getPdfObject();
80+
Assert.assertTrue(expressionObject instanceof PdfArray);
81+
Assert.assertEquals(3, ((PdfArray) expressionObject).size());
82+
Assert.assertEquals(PdfName.And, ((PdfArray) expressionObject).getAsName(0));
83+
}
84+
85+
@Test
86+
public void andExpressionTest() {
87+
PdfDocument tempDoc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
88+
89+
// create expression with the AND operator as the first parameter
90+
PdfVisibilityExpression expression = new PdfVisibilityExpression(PdfName.And);
91+
92+
// add two empty dictionaries as the other parameters
93+
expression.addOperand(new PdfLayer((PdfDictionary) new PdfDictionary().makeIndirect(tempDoc)));
94+
expression.addOperand(new PdfLayer((PdfDictionary) new PdfDictionary().makeIndirect(tempDoc)));
95+
96+
PdfObject expressionObject = expression.getPdfObject();
97+
Assert.assertTrue(expressionObject instanceof PdfArray);
98+
Assert.assertEquals(3, ((PdfArray) expressionObject).size());
99+
Assert.assertEquals(PdfName.And, ((PdfArray) expressionObject).getAsName(0));
100+
}
101+
102+
@Test
103+
public void nestedExpressionTest() {
104+
PdfDocument tempDoc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
105+
106+
// create expression with the OR operator as the first parameter
107+
PdfVisibilityExpression expression = new PdfVisibilityExpression(PdfName.Or);
108+
109+
// add an empty dictionary as the second parameter
110+
expression.addOperand(new PdfLayer((PdfDictionary) new PdfDictionary().makeIndirect(tempDoc)));
111+
112+
// create a nested expression with the AND operator and two empty dictionaries as parameters
113+
PdfVisibilityExpression nestedExpression = new PdfVisibilityExpression(PdfName.And);
114+
nestedExpression.addOperand(new PdfLayer((PdfDictionary) new PdfDictionary().makeIndirect(tempDoc)));
115+
nestedExpression.addOperand(new PdfLayer((PdfDictionary) new PdfDictionary().makeIndirect(tempDoc)));
116+
117+
// add another expression as the third parameter
118+
expression.addOperand(nestedExpression);
119+
120+
PdfObject expressionObject = expression.getPdfObject();
121+
Assert.assertTrue(expressionObject instanceof PdfArray);
122+
Assert.assertEquals(3, ((PdfArray) expressionObject).size());
123+
Assert.assertEquals(PdfName.Or, ((PdfArray) expressionObject).getAsName(0));
124+
125+
PdfObject child = ((PdfArray) expressionObject).get(2);
126+
Assert.assertTrue(child instanceof PdfArray);
127+
Assert.assertEquals(3, ((PdfArray) child).size());
128+
Assert.assertEquals(PdfName.And, ((PdfArray) child).get(0));
129+
}
130+
131+
}

0 commit comments

Comments
 (0)