Skip to content

Commit 09cd3c2

Browse files
Kate IvanovaiText-CI
authored andcommitted
Add tests for /Separation CS support
DEVSIX-5931 Autoported commit. Original commit hash: [44c5b1f54]
1 parent e9262c1 commit 09cd3c2

File tree

5 files changed

+56
-34
lines changed

5 files changed

+56
-34
lines changed

itext.tests/itext.kernel.tests/itext/kernel/pdf/xobject/GetImageBytesTest.cs

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ source product.
5454

5555
namespace iText.Kernel.Pdf.Xobject {
5656
public class GetImageBytesTest : ExtendedITextTest {
57-
private static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
57+
private static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
5858
.CurrentContext.TestDirectory) + "/resources/itext/kernel/pdf/xobject/GetImageBytesTest/";
5959

60-
private static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
60+
private static readonly String DESTINATION_FOLDER = NUnit.Framework.TestContext.CurrentContext.TestDirectory
6161
+ "/test/itext/kernel/pdf/xobject/GetImageBytesTest/";
6262

6363
[NUnit.Framework.OneTimeSetUp]
6464
public static void BeforeClass() {
65-
CreateOrClearDestinationFolder(destinationFolder);
65+
CreateOrClearDestinationFolder(DESTINATION_FOLDER);
6666
}
6767

6868
[NUnit.Framework.Test]
@@ -134,11 +134,38 @@ public virtual void TestJPXDecode() {
134134
TestFile("JPXDecode.pdf", "Im1", "jp2");
135135
}
136136

137+
[NUnit.Framework.Test]
138+
public virtual void TestSeparationCSWithICCBasedAsAlternative() {
139+
// TODO: DEVSIX-3538 (update test after fix)
140+
Exception e = NUnit.Framework.Assert.Catch(typeof(iText.IO.Exceptions.IOException), () => TestFile("separationCSWithICCBasedAsAlternative.pdf"
141+
, "Im1", "tif"));
142+
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(iText.IO.Exceptions.IOException.ColorSpaceIsNotSupported
143+
, PdfName.Separation), e.Message);
144+
}
145+
146+
[NUnit.Framework.Test]
147+
public virtual void TestSeparationCSWithDeviceCMYKAsAlternative() {
148+
// TODO: DEVSIX-3538 (update test after fix)
149+
Exception e = NUnit.Framework.Assert.Catch(typeof(iText.IO.Exceptions.IOException), () => TestFile("separationCSWithDeviceCMYKAsAlternative.pdf"
150+
, "Im1", "tif"));
151+
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(iText.IO.Exceptions.IOException.ColorSpaceIsNotSupported
152+
, PdfName.Separation), e.Message);
153+
}
154+
155+
[NUnit.Framework.Test]
156+
public virtual void TestSeparationCSWithDeviceRGBAsAlternative() {
157+
// TODO: DEVSIX-3538 (update test after fix)
158+
Exception e = NUnit.Framework.Assert.Catch(typeof(iText.IO.Exceptions.IOException), () => TestFile("separationCSWithDeviceRgbAsAlternative.pdf"
159+
, "Im1", "tif"));
160+
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(iText.IO.Exceptions.IOException.ColorSpaceIsNotSupported
161+
, PdfName.Separation), e.Message);
162+
}
163+
137164
[NUnit.Framework.Test]
138165
public virtual void ExtractByteAlignedG4TiffImageTest() {
139-
String inFileName = sourceFolder + "extractByteAlignedG4TiffImage.pdf";
140-
String outImageFileName = destinationFolder + "extractedByteAlignedImage.png";
141-
String cmpImageFileName = sourceFolder + "cmp_extractByteAlignedG4TiffImage.png";
166+
String inFileName = SOURCE_FOLDER + "extractByteAlignedG4TiffImage.pdf";
167+
String outImageFileName = DESTINATION_FOLDER + "extractedByteAlignedImage.png";
168+
String cmpImageFileName = SOURCE_FOLDER + "cmp_extractByteAlignedG4TiffImage.png";
142169
PdfDocument pdfDocument = new PdfDocument(new PdfReader(inFileName));
143170
GetImageBytesTest.ImageExtractor listener = new GetImageBytesTest.ImageExtractor(this);
144171
PdfCanvasProcessor processor = new PdfCanvasProcessor(listener);
@@ -163,7 +190,7 @@ public virtual void ExtractByteAlignedG4TiffImageTest() {
163190
[NUnit.Framework.Test]
164191
public virtual void ExpectedByteAlignedTiffImageExtractionTest() {
165192
//Byte-aligned image is expected in pdf file, but in fact it's not
166-
String inFileName = sourceFolder + "expectedByteAlignedTiffImageExtraction.pdf";
193+
String inFileName = SOURCE_FOLDER + "expectedByteAlignedTiffImageExtraction.pdf";
167194
PdfDocument pdfDocument = new PdfDocument(new PdfReader(inFileName));
168195
GetImageBytesTest.ImageExtractor listener = new GetImageBytesTest.ImageExtractor(this);
169196
PdfCanvasProcessor processor = new PdfCanvasProcessor(listener);
@@ -207,31 +234,28 @@ internal ImageExtractor(GetImageBytesTest _enclosing) {
207234
}
208235

209236
private void TestFile(String filename, String objectid, String expectedImageFormat) {
210-
PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + filename));
211-
try {
212-
PdfResources resources = pdfDocument.GetPage(1).GetResources();
213-
PdfDictionary xobjects = resources.GetResource(PdfName.XObject);
214-
PdfObject obj = xobjects.Get(new PdfName(objectid));
215-
if (obj == null) {
216-
throw new ArgumentException("Reference " + objectid + " not found - Available keys are " + xobjects.KeySet
217-
());
218-
}
219-
PdfImageXObject img = new PdfImageXObject((PdfStream)(obj.IsIndirectReference() ? ((PdfIndirectReference)obj
220-
).GetRefersTo() : obj));
221-
NUnit.Framework.Assert.AreEqual(expectedImageFormat, img.IdentifyImageFileExtension());
222-
byte[] result = img.GetImageBytes(true);
223-
byte[] cmpBytes = File.ReadAllBytes(System.IO.Path.Combine(sourceFolder, filename.JSubstring(0, filename.Length
224-
- 4) + "." + expectedImageFormat));
225-
if (img.IdentifyImageFileExtension().Equals("tif")) {
226-
CompareTiffImages(cmpBytes, result);
227-
}
228-
else {
229-
NUnit.Framework.Assert.AreEqual(cmpBytes, result);
237+
using (PdfReader reader = new PdfReader(SOURCE_FOLDER + filename)) {
238+
using (PdfDocument pdfDocument = new PdfDocument(reader)) {
239+
PdfResources resources = pdfDocument.GetPage(1).GetResources();
240+
PdfDictionary xobjects = resources.GetResource(PdfName.XObject);
241+
PdfObject obj = xobjects.Get(new PdfName(objectid));
242+
if (obj == null) {
243+
throw new ArgumentException("Reference " + objectid + " not found - Available keys are " + xobjects.KeySet
244+
());
245+
}
246+
PdfImageXObject img = new PdfImageXObject((PdfStream)obj);
247+
NUnit.Framework.Assert.AreEqual(expectedImageFormat, img.IdentifyImageFileExtension());
248+
byte[] result = img.GetImageBytes(true);
249+
byte[] cmpBytes = File.ReadAllBytes(System.IO.Path.Combine(SOURCE_FOLDER, filename.JSubstring(0, filename.
250+
Length - 4) + "." + expectedImageFormat));
251+
if (img.IdentifyImageFileExtension().Equals("tif")) {
252+
CompareTiffImages(cmpBytes, result);
253+
}
254+
else {
255+
NUnit.Framework.Assert.AreEqual(cmpBytes, result);
256+
}
230257
}
231258
}
232-
finally {
233-
pdfDocument.Close();
234-
}
235259
}
236260

237261
private void CompareTiffImages(byte[] cmpBytes, byte[] resultBytes) {
@@ -253,9 +277,7 @@ private void CompareTiffImages(byte[] cmpBytes, byte[] resultBytes) {
253277
NUnit.Framework.Assert.AreEqual(cmpDir.IsTagPresent(tag), resultDir.IsTagPresent(tag));
254278
TIFFField cmpField = cmpDir.GetField(tag);
255279
TIFFField resultField = resultDir.GetField(tag);
256-
if (tag == TIFFConstants.TIFFTAG_SOFTWARE) {
257-
}
258-
else {
280+
if (tag != TIFFConstants.TIFFTAG_SOFTWARE) {
259281
CompareFields(cmpField, resultField);
260282
}
261283
}

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0ce78fbf9d9b8168340f966be66e176e396ef3f9
1+
44c5b1f5436a306c7185caa81e63f41c86ca0c33

0 commit comments

Comments
 (0)