1 | #include <QtTest/QTest> |
2 | |
3 | #include <poppler-qt6.h> |
4 | |
5 | class TestMetaData : public QObject |
6 | { |
7 | Q_OBJECT |
8 | public: |
9 | explicit TestMetaData(QObject *parent = nullptr) : QObject(parent) { } |
10 | private slots: |
11 | void checkStrings_data(); |
12 | void checkStrings(); |
13 | void checkStrings2_data(); |
14 | void checkStrings2(); |
15 | void checkStringKeys(); |
16 | void checkLinearised(); |
17 | void checkNumPages(); |
18 | void checkDate(); |
19 | void checkPageSize(); |
20 | void checkPortraitOrientation(); |
21 | void checkLandscapeOrientation(); |
22 | void checkUpsideDownOrientation(); |
23 | void checkSeascapeOrientation(); |
24 | void checkVersion(); |
25 | void checkPdfId(); |
26 | void checkNoPdfId(); |
27 | }; |
28 | |
29 | void TestMetaData::checkStrings_data() |
30 | { |
31 | QTest::addColumn<QString>(name: "key" ); |
32 | QTest::addColumn<QString>(name: "value" ); |
33 | |
34 | QTest::newRow(dataTag: "Author" ) << "Author" |
35 | << "Brad Hards" ; |
36 | QTest::newRow(dataTag: "Title" ) << "Title" |
37 | << "Two pages" ; |
38 | QTest::newRow(dataTag: "Subject" ) << "Subject" |
39 | << "A two page layout for poppler testing" ; |
40 | QTest::newRow(dataTag: "Keywords" ) << "Keywords" |
41 | << "Qt4 bindings" ; |
42 | QTest::newRow(dataTag: "Creator" ) << "Creator" |
43 | << "iText: cgpdftops CUPS filter" ; |
44 | QTest::newRow(dataTag: "Producer" ) << "Producer" |
45 | << "Acrobat Distiller 7.0 for Macintosh" ; |
46 | } |
47 | |
48 | void TestMetaData::checkStrings() |
49 | { |
50 | std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/unittestcases/doublepage.pdf" ); |
51 | QVERIFY(doc); |
52 | |
53 | QFETCH(QString, key); |
54 | QFETCH(QString, value); |
55 | QCOMPARE(doc->info(key), value); |
56 | } |
57 | |
58 | void TestMetaData::checkStrings2_data() |
59 | { |
60 | QTest::addColumn<QString>(name: "key" ); |
61 | QTest::addColumn<QString>(name: "value" ); |
62 | |
63 | QTest::newRow(dataTag: "Title" ) << "Title" |
64 | << "Malaga hotels" ; |
65 | QTest::newRow(dataTag: "Author" ) << "Author" |
66 | << "Brad Hards" ; |
67 | QTest::newRow(dataTag: "Creator" ) << "Creator" |
68 | << "Safari: cgpdftops CUPS filter" ; |
69 | QTest::newRow(dataTag: "Producer" ) << "Producer" |
70 | << "Acrobat Distiller 7.0 for Macintosh" ; |
71 | QTest::newRow(dataTag: "Keywords" ) << "Keywords" |
72 | << "First\rSecond\rthird" ; |
73 | QTest::newRow(dataTag: "Custom1" ) << "Custom1" |
74 | << "CustomValue1" ; |
75 | QTest::newRow(dataTag: "Custom2" ) << "Custom2" |
76 | << "CustomValue2" ; |
77 | } |
78 | |
79 | void TestMetaData::checkStrings2() |
80 | { |
81 | std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/unittestcases/truetype.pdf" ); |
82 | QVERIFY(doc); |
83 | |
84 | QFETCH(QString, key); |
85 | QFETCH(QString, value); |
86 | QCOMPARE(doc->info(key), value); |
87 | } |
88 | |
89 | void TestMetaData::checkStringKeys() |
90 | { |
91 | std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/unittestcases/truetype.pdf" ); |
92 | QVERIFY(doc); |
93 | |
94 | QStringList keyList; |
95 | keyList << QStringLiteral("Title" ) << QStringLiteral("Author" ) << QStringLiteral("Creator" ) << QStringLiteral("Keywords" ) << QStringLiteral("CreationDate" ); |
96 | keyList << QStringLiteral("Producer" ) << QStringLiteral("ModDate" ) << QStringLiteral("Custom1" ) << QStringLiteral("Custom2" ); |
97 | keyList.sort(); |
98 | QStringList keysInDoc = doc->infoKeys(); |
99 | keysInDoc.sort(); |
100 | QCOMPARE(keysInDoc, keyList); |
101 | } |
102 | |
103 | void TestMetaData::checkLinearised() |
104 | { |
105 | std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/unittestcases/orientation.pdf" ); |
106 | QVERIFY(doc); |
107 | |
108 | QVERIFY(doc->isLinearized()); |
109 | |
110 | doc = Poppler::Document::load(TESTDATADIR "/unittestcases/truetype.pdf" ); |
111 | QVERIFY(doc); |
112 | QCOMPARE(doc->isLinearized(), false); |
113 | } |
114 | |
115 | void TestMetaData::checkPortraitOrientation() |
116 | { |
117 | std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/unittestcases/orientation.pdf" ); |
118 | QVERIFY(doc); |
119 | |
120 | std::unique_ptr<Poppler::Page> page = doc->page(index: 0); |
121 | QCOMPARE(page->orientation(), Poppler::Page::Portrait); |
122 | } |
123 | |
124 | void TestMetaData::checkNumPages() |
125 | { |
126 | std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/unittestcases/doublepage.pdf" ); |
127 | QVERIFY(doc); |
128 | QCOMPARE(doc->numPages(), 2); |
129 | |
130 | doc = Poppler::Document::load(TESTDATADIR "/unittestcases/truetype.pdf" ); |
131 | QVERIFY(doc); |
132 | QCOMPARE(doc->numPages(), 1); |
133 | } |
134 | |
135 | void TestMetaData::checkDate() |
136 | { |
137 | std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/unittestcases/truetype.pdf" ); |
138 | QVERIFY(doc); |
139 | QCOMPARE(doc->date(QStringLiteral("ModDate" )), QDateTime(QDate(2005, 12, 5), QTime(9, 44, 46), Qt::UTC)); |
140 | QCOMPARE(doc->date(QStringLiteral("CreationDate" )), QDateTime(QDate(2005, 8, 13), QTime(1, 12, 11), Qt::UTC)); |
141 | } |
142 | |
143 | void TestMetaData::checkPageSize() |
144 | { |
145 | std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/unittestcases/truetype.pdf" ); |
146 | QVERIFY(doc); |
147 | std::unique_ptr<Poppler::Page> page = doc->page(index: 0); |
148 | QCOMPARE(page->pageSize(), QSize(595, 842)); |
149 | QCOMPARE(page->pageSizeF(), QSizeF(595.22, 842)); |
150 | } |
151 | |
152 | void TestMetaData::checkLandscapeOrientation() |
153 | { |
154 | std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/unittestcases/orientation.pdf" ); |
155 | QVERIFY(doc); |
156 | |
157 | std::unique_ptr<Poppler::Page> page = doc->page(index: 1); |
158 | QCOMPARE(page->orientation(), Poppler::Page::Landscape); |
159 | } |
160 | |
161 | void TestMetaData::checkUpsideDownOrientation() |
162 | { |
163 | std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/unittestcases/orientation.pdf" ); |
164 | QVERIFY(doc); |
165 | |
166 | std::unique_ptr<Poppler::Page> page = doc->page(index: 2); |
167 | QCOMPARE(page->orientation(), Poppler::Page::UpsideDown); |
168 | } |
169 | |
170 | void TestMetaData::checkSeascapeOrientation() |
171 | { |
172 | std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/unittestcases/orientation.pdf" ); |
173 | QVERIFY(doc); |
174 | |
175 | std::unique_ptr<Poppler::Page> page = doc->page(index: 3); |
176 | QCOMPARE(page->orientation(), Poppler::Page::Seascape); |
177 | } |
178 | |
179 | void TestMetaData::checkVersion() |
180 | { |
181 | std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/unittestcases/doublepage.pdf" ); |
182 | QVERIFY(doc); |
183 | |
184 | auto pdfVersion = doc->getPdfVersion(); |
185 | QCOMPARE(pdfVersion.major, 1); |
186 | QCOMPARE(pdfVersion.minor, 6); |
187 | } |
188 | |
189 | void TestMetaData::checkPdfId() |
190 | { |
191 | std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/unittestcases/A6EmbeddedFiles.pdf" ); |
192 | QVERIFY(doc); |
193 | |
194 | const QByteArray referencePermanentId("00C9D5B6D8FB11D7A902003065D630AA" ); |
195 | const QByteArray referenceUpdateId("39AECAE6D8FB11D7A902003065D630AA" ); |
196 | |
197 | { |
198 | // no IDs wanted, just existance check |
199 | QVERIFY(doc->getPdfId(nullptr, nullptr)); |
200 | } |
201 | { |
202 | // only permanent ID |
203 | QByteArray permanentId; |
204 | QVERIFY(doc->getPdfId(&permanentId, nullptr)); |
205 | QCOMPARE(permanentId.toUpper(), referencePermanentId); |
206 | } |
207 | { |
208 | // only update ID |
209 | QByteArray updateId; |
210 | QVERIFY(doc->getPdfId(nullptr, &updateId)); |
211 | QCOMPARE(updateId.toUpper(), referenceUpdateId); |
212 | } |
213 | { |
214 | // both IDs |
215 | QByteArray permanentId; |
216 | QByteArray updateId; |
217 | QVERIFY(doc->getPdfId(&permanentId, &updateId)); |
218 | QCOMPARE(permanentId.toUpper(), referencePermanentId); |
219 | QCOMPARE(updateId.toUpper(), referenceUpdateId); |
220 | } |
221 | } |
222 | |
223 | void TestMetaData::checkNoPdfId() |
224 | { |
225 | std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/unittestcases/WithActualText.pdf" ); |
226 | QVERIFY(doc); |
227 | |
228 | QVERIFY(!doc->getPdfId(nullptr, nullptr)); |
229 | } |
230 | |
231 | QTEST_GUILESS_MAIN(TestMetaData) |
232 | #include "check_metadata.moc" |
233 | |