Skip to content

Commit f1d519b

Browse files
Kate IvanovaiText-CI
authored andcommitted
Refactor TaggedPdfFormTest to decrease code duplication
DEVSIX-4613 Autoported commit. Original commit hash: [4879291a]
1 parent 5f3de3f commit f1d519b

File tree

13 files changed

+14
-56
lines changed

13 files changed

+14
-56
lines changed

itext.tests/itext.html2pdf.tests/itext/html2pdf/element/TaggedPdfFormTest.cs

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,11 @@ source product.
4141
4242
*/
4343
using System;
44-
using System.IO;
45-
using iText.Forms;
4644
using iText.Html2pdf;
4745
using iText.Kernel;
48-
using iText.Kernel.Pdf;
49-
using iText.Kernel.Utils;
50-
using iText.Test;
5146

5247
namespace iText.Html2pdf.Element {
53-
public class TaggedPdfFormTest : ExtendedITextTest {
48+
public class TaggedPdfFormTest : ExtendedHtmlConversionITextTest {
5449
public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
5550
.CurrentContext.TestDirectory) + "/resources/itext/html2pdf/element/TaggedPdfFormTest/";
5651

@@ -64,110 +59,73 @@ public static void BeforeClass() {
6459

6560
[NUnit.Framework.Test]
6661
public virtual void SimpleTextFieldTagged() {
67-
RunTest("simpleTextFieldTagged");
62+
ConvertToPdfAcroformFlattenAndCompare("simpleTextField", sourceFolder, destinationFolder, true);
6863
}
6964

7065
[NUnit.Framework.Test]
7166
public virtual void SimpleTextareaTagged() {
72-
RunTest("simpleTextareaTagged");
67+
ConvertToPdfAcroformFlattenAndCompare("simpleTextarea", sourceFolder, destinationFolder, true);
7368
}
7469

7570
[NUnit.Framework.Test]
7671
public virtual void SimpleButtonTagged() {
77-
RunTest("simpleButtonTagged");
72+
ConvertToPdfAcroformFlattenAndCompare("simpleButton", sourceFolder, destinationFolder, true);
7873
}
7974

8075
[NUnit.Framework.Test]
8176
public virtual void SimpleLabelTagged() {
82-
RunTest("simpleLabelTagged");
77+
ConvertToPdfAcroformFlattenAndCompare("simpleLabel", sourceFolder, destinationFolder, true);
8378
}
8479

8580
[NUnit.Framework.Test]
8681
public virtual void SimpleCheckboxTagged() {
87-
RunTest("simpleCheckboxTagged");
82+
ConvertToPdfAcroformFlattenAndCompare("simpleCheckbox", sourceFolder, destinationFolder, true);
8883
}
8984

9085
[NUnit.Framework.Test]
9186
[NUnit.Framework.Ignore("DEVSIX-1901")]
9287
public virtual void SimpleSelectTagged() {
93-
RunTest("simpleSelectTagged");
88+
ConvertToPdfAcroformFlattenAndCompare("simpleSelect", sourceFolder, destinationFolder, true);
9489
}
9590

9691
[NUnit.Framework.Test]
9792
[NUnit.Framework.Ignore("DEVSIX-1901")]
9893
public virtual void ListBoxSelectTagged() {
99-
RunTest("listBoxSelectTagged");
94+
ConvertToPdfAcroformFlattenAndCompare("listBoxSelect", sourceFolder, destinationFolder, true);
10095
}
10196

10297
[NUnit.Framework.Test]
10398
[NUnit.Framework.Ignore("DEVSIX-1901")]
10499
public virtual void ListBoxOptGroupSelectTagged() {
105-
RunTest("listBoxOptGroupSelectTagged");
100+
ConvertToPdfAcroformFlattenAndCompare("listBoxOptGroupSelect", sourceFolder, destinationFolder, true);
106101
}
107102

108103
[NUnit.Framework.Test]
109104
[NUnit.Framework.Ignore("DEVSIX-1901")]
110105
public virtual void SimpleRadioFormTagged() {
111-
RunTest("simpleRadioFormTagged");
106+
ConvertToPdfAcroformFlattenAndCompare("simpleRadioForm", sourceFolder, destinationFolder, true);
112107
}
113108

114109
[NUnit.Framework.Test]
115110
[NUnit.Framework.Ignore("DEVSIX-980. DefaultHtmlProcessor ERROR No worker found for tag datalist")]
116111
public virtual void DataListFormTagged() {
117-
RunTest("dataListFormTagged");
112+
ConvertToPdfAcroformFlattenAndCompare("dataListForm", sourceFolder, destinationFolder, true);
118113
}
119114

120115
[NUnit.Framework.Test]
121116
public virtual void FieldSetFormTagged() {
122-
RunTest("fieldSetFormTagged");
117+
ConvertToPdfAcroformFlattenAndCompare("fieldSetForm", sourceFolder, destinationFolder, true);
123118
}
124119

125120
[NUnit.Framework.Test]
126121
public virtual void InputFormPrematureFlush() {
127122
NUnit.Framework.Assert.That(() => {
128123
// TODO DEVSIX-4601
129124
// exception is thrown on "convert tagged PDF with acroform" stage
130-
RunTest("inputFormPrematureFlush");
125+
ConvertToPdfAcroformFlattenAndCompare("inputFormPrematureFlush", sourceFolder, destinationFolder, true);
131126
}
132127
, NUnit.Framework.Throws.InstanceOf<PdfException>().With.Message.EqualTo(PdfException.TagStructureFlushingFailedItMightBeCorrupted))
133128
;
134129
}
135-
136-
private void RunTest(String name) {
137-
String htmlPath = sourceFolder + name + ".html";
138-
String outTaggedPdfPath = destinationFolder + name + ".pdf";
139-
String outTaggedPdfPathAcro = destinationFolder + name + "_acro.pdf";
140-
String outTaggedPdfPathFlatted = destinationFolder + name + "_acro_flatten.pdf";
141-
String cmpPdfPath = sourceFolder + "cmp_" + name + ".pdf";
142-
String cmpPdfPathAcro = sourceFolder + "cmp_" + name + "_acro.pdf";
143-
String cmpPdfPathAcroFlatten = sourceFolder + "cmp_" + name + "_acro_flatten.pdf";
144-
String diff1 = "diff1_" + name;
145-
String diff2 = "diff2_" + name;
146-
String diff3 = "diff3_" + name;
147-
//convert tagged PDF without acroform (from html with form elements)
148-
PdfWriter taggedWriter = new PdfWriter(outTaggedPdfPath);
149-
PdfDocument pdfTagged = new PdfDocument(taggedWriter);
150-
pdfTagged.SetTagged();
151-
HtmlConverter.ConvertToPdf(new FileStream(htmlPath, FileMode.Open, FileAccess.Read), pdfTagged);
152-
//convert tagged PDF with acroform
153-
PdfWriter taggedWriterAcro = new PdfWriter(outTaggedPdfPathAcro);
154-
PdfDocument pdfTaggedAcro = new PdfDocument(taggedWriterAcro);
155-
pdfTaggedAcro.SetTagged();
156-
ConverterProperties converterPropertiesAcro = new ConverterProperties();
157-
converterPropertiesAcro.SetCreateAcroForm(true);
158-
HtmlConverter.ConvertToPdf(new FileStream(htmlPath, FileMode.Open, FileAccess.Read), pdfTaggedAcro, converterPropertiesAcro
159-
);
160-
//flatted created tagged PDF with acroform
161-
PdfDocument document = new PdfDocument(new PdfReader(outTaggedPdfPathAcro), new PdfWriter(outTaggedPdfPathFlatted
162-
));
163-
PdfAcroForm.GetAcroForm(document, false).FlattenFields();
164-
document.Close();
165-
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outTaggedPdfPath, cmpPdfPath, destinationFolder
166-
, diff1));
167-
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outTaggedPdfPathAcro, cmpPdfPathAcro, destinationFolder
168-
, diff2));
169-
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outTaggedPdfPathFlatted, cmpPdfPathAcroFlatten
170-
, destinationFolder, diff3));
171-
}
172130
}
173131
}

0 commit comments

Comments
 (0)