summaryrefslogtreecommitdiff
path: root/qt5/tests/check_metadata.cpp
blob: 235a4ea2f72e63e3082ff7f119389b36aa55d078 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <QtTest/QTest>

#include <poppler-qt5.h>

class TestMetaData : public QObject
{
    Q_OBJECT
public:
    explicit TestMetaData(QObject *parent = nullptr) : QObject(parent) { }
private slots:
    void checkStrings_data();
    void checkStrings();
    void checkStrings2_data();
    void checkStrings2();
    void checkStringKeys();
    void checkLinearised();
    void checkNumPages();
    void checkDate();
    void checkPageSize();
    void checkPortraitOrientation();
    void checkLandscapeOrientation();
    void checkUpsideDownOrientation();
    void checkSeascapeOrientation();
    void checkVersion();
    void checkPdfId();
    void checkNoPdfId();
};

void TestMetaData::checkStrings_data()
{
    QTest::addColumn<QString>("key");
    QTest::addColumn<QString>("value");

    QTest::newRow("Author") << "Author"
                            << "Brad Hards";
    QTest::newRow("Title") << "Title"
                           << "Two pages";
    QTest::newRow("Subject") << "Subject"
                             << "A two page layout for poppler testing";
    QTest::newRow("Keywords") << "Keywords"
                              << "Qt4 bindings";
    QTest::newRow("Creator") << "Creator"
                             << "iText: cgpdftops CUPS filter";
    QTest::newRow("Producer") << "Producer"
                              << "Acrobat Distiller 7.0 for Macintosh";
}

void TestMetaData::checkStrings()
{
    Poppler::Document *doc;
    doc = Poppler::Document::load(TESTDATADIR "/unittestcases/doublepage.pdf");
    QVERIFY(doc);

    QFETCH(QString, key);
    QFETCH(QString, value);
    QCOMPARE(doc->info(key), value);

    delete doc;
}

void TestMetaData::checkStrings2_data()
{
    QTest::addColumn<QString>("key");
    QTest::addColumn<QString>("value");

    QTest::newRow("Title") << "Title"
                           << "Malaga hotels";
    QTest::newRow("Author") << "Author"
                            << "Brad Hards";
    QTest::newRow("Creator") << "Creator"
                             << "Safari: cgpdftops CUPS filter";
    QTest::newRow("Producer") << "Producer"
                              << "Acrobat Distiller 7.0 for Macintosh";
    QTest::newRow("Keywords") << "Keywords"
                              << "First\rSecond\rthird";
    QTest::newRow("Custom1") << "Custom1"
                             << "CustomValue1";
    QTest::newRow("Custom2") << "Custom2"
                             << "CustomValue2";
}

void TestMetaData::checkStrings2()
{
    Poppler::Document *doc;
    doc = Poppler::Document::load(TESTDATADIR "/unittestcases/truetype.pdf");
    QVERIFY(doc);

    QFETCH(QString, key);
    QFETCH(QString, value);
    QCOMPARE(doc->info(key), value);

    delete doc;
}

void TestMetaData::checkStringKeys()
{
    Poppler::Document *doc;
    doc = Poppler::Document::load(TESTDATADIR "/unittestcases/truetype.pdf");
    QVERIFY(doc);

    QStringList keyList;
    keyList << QStringLiteral("Title") << QStringLiteral("Author") << QStringLiteral("Creator") << QStringLiteral("Keywords") << QStringLiteral("CreationDate");
    keyList << QStringLiteral("Producer") << QStringLiteral("ModDate") << QStringLiteral("Custom1") << QStringLiteral("Custom2");
    keyList.sort();
    QStringList keysInDoc = doc->infoKeys();
    keysInDoc.sort();
    QCOMPARE(keysInDoc, keyList);

    delete doc;
}

void TestMetaData::checkLinearised()
{
    Poppler::Document *doc;
    doc = Poppler::Document::load(TESTDATADIR "/unittestcases/orientation.pdf");
    QVERIFY(doc);

    QVERIFY(doc->isLinearized());

    delete doc;

    doc = Poppler::Document::load(TESTDATADIR "/unittestcases/truetype.pdf");
    QVERIFY(doc);
    QCOMPARE(doc->isLinearized(), false);

    delete doc;
}

void TestMetaData::checkPortraitOrientation()
{
    Poppler::Document *doc;
    doc = Poppler::Document::load(TESTDATADIR "/unittestcases/orientation.pdf");
    QVERIFY(doc);

    Poppler::Page *page = doc->page(0);
    QCOMPARE(page->orientation(), Poppler::Page::Portrait);

    delete page;
    delete doc;
}

void TestMetaData::checkNumPages()
{
    Poppler::Document *doc;
    doc = Poppler::Document::load(TESTDATADIR "/unittestcases/doublepage.pdf");
    QVERIFY(doc);
    QCOMPARE(doc->numPages(), 2);

    delete doc;

    doc = Poppler::Document::load(TESTDATADIR "/unittestcases/truetype.pdf");
    QVERIFY(doc);
    QCOMPARE(doc->numPages(), 1);

    delete doc;
}

void TestMetaData::checkDate()
{
    Poppler::Document *doc;

    doc = Poppler::Document::load(TESTDATADIR "/unittestcases/truetype.pdf");
    QVERIFY(doc);
    QCOMPARE(doc->date(QStringLiteral("ModDate")), QDateTime(QDate(2005, 12, 5), QTime(9, 44, 46), Qt::UTC));
    QCOMPARE(doc->date(QStringLiteral("CreationDate")), QDateTime(QDate(2005, 8, 13), QTime(1, 12, 11), Qt::UTC));

    delete doc;
}

void TestMetaData::checkPageSize()
{
    Poppler::Document *doc;

    doc = Poppler::Document::load(TESTDATADIR "/unittestcases/truetype.pdf");
    QVERIFY(doc);
    Poppler::Page *page = doc->page(0);
    QCOMPARE(page->pageSize(), QSize(595, 842));
    QCOMPARE(page->pageSizeF(), QSizeF(595.22, 842));

    delete page;
    delete doc;
}

void TestMetaData::checkLandscapeOrientation()
{
    Poppler::Document *doc;
    doc = Poppler::Document::load(TESTDATADIR "/unittestcases/orientation.pdf");
    QVERIFY(doc);

    Poppler::Page *page = doc->page(1);
    QCOMPARE(page->orientation(), Poppler::Page::Landscape);

    delete page;
    delete doc;
}

void TestMetaData::checkUpsideDownOrientation()
{
    Poppler::Document *doc;
    doc = Poppler::Document::load(TESTDATADIR "/unittestcases/orientation.pdf");
    QVERIFY(doc);

    Poppler::Page *page = doc->page(2);
    QCOMPARE(page->orientation(), Poppler::Page::UpsideDown);

    delete page;
    delete doc;
}

void TestMetaData::checkSeascapeOrientation()
{
    Poppler::Document *doc;
    doc = Poppler::Document::load(TESTDATADIR "/unittestcases/orientation.pdf");
    QVERIFY(doc);

    Poppler::Page *page = doc->page(3);
    QCOMPARE(page->orientation(), Poppler::Page::Seascape);

    delete page;
    delete doc;
}

void TestMetaData::checkVersion()
{
    Poppler::Document *doc;
    doc = Poppler::Document::load(TESTDATADIR "/unittestcases/doublepage.pdf");
    QVERIFY(doc);

    auto pdfVersion = doc->getPdfVersion();
    QCOMPARE(pdfVersion.major, 1);
    QCOMPARE(pdfVersion.minor, 6);

    delete doc;
}

void TestMetaData::checkPdfId()
{
    Poppler::Document *doc;
    doc = Poppler::Document::load(TESTDATADIR "/unittestcases/A6EmbeddedFiles.pdf");
    QVERIFY(doc);

    const QByteArray referencePermanentId("00C9D5B6D8FB11D7A902003065D630AA");
    const QByteArray referenceUpdateId("39AECAE6D8FB11D7A902003065D630AA");

    {
        // no IDs wanted, just existance check
        QVERIFY(doc->getPdfId(nullptr, nullptr));
    }
    {
        // only permanent ID
        QByteArray permanentId;
        QVERIFY(doc->getPdfId(&permanentId, nullptr));
        QCOMPARE(permanentId.toUpper(), referencePermanentId);
    }
    {
        // only update ID
        QByteArray updateId;
        QVERIFY(doc->getPdfId(nullptr, &updateId));
        QCOMPARE(updateId.toUpper(), referenceUpdateId);
    }
    {
        // both IDs
        QByteArray permanentId;
        QByteArray updateId;
        QVERIFY(doc->getPdfId(&permanentId, &updateId));
        QCOMPARE(permanentId.toUpper(), referencePermanentId);
        QCOMPARE(updateId.toUpper(), referenceUpdateId);
    }

    delete doc;
}

void TestMetaData::checkNoPdfId()
{
    Poppler::Document *doc;
    doc = Poppler::Document::load(TESTDATADIR "/unittestcases/WithActualText.pdf");
    QVERIFY(doc);

    QVERIFY(!doc->getPdfId(nullptr, nullptr));

    delete doc;
}

QTEST_GUILESS_MAIN(TestMetaData)
#include "check_metadata.moc"