Debian Codegraph @

qt5/tests/check_optcontent.cpp

raw
1 #include <QtTest/QTest>
2
3 #include "PDFDoc.h"
4 #include "GlobalParams.h"
5
6 #include <poppler-qt5.h>
7 #include <poppler-optcontent-private.h>
8
9 class TestOptionalContent : public QObject
10 {
11 Q_OBJECT
12 public:
13 explicit TestOptionalContent(QObject *parent = nullptr) : QObject(parent) { }
14 private Q_SLOTS:
15 void checkVisPolicy();
16 void checkNestedLayers();
17 void checkNoOptionalContent();
18 void checkIsVisible();
19 void checkVisibilitySetting();
20 void checkRadioButtons();
21 };
22
23 void TestOptionalContent::checkVisPolicy()
24 {
25 Poppler::Document *doc;
26 doc = Poppler::Document::load(QStringLiteral(TESTDATADIR "/unittestcases/vis_policy_test.pdf"));
27 QVERIFY(doc);
28
29 QVERIFY(doc->hasOptionalContent());
30
31 Poppler::OptContentModel *optContent = doc->optionalContentModel();
32 QModelIndex index;
33 index = optContent->index(0, 0, QModelIndex());
34 QCOMPARE(optContent->data(index, Qt::DisplayRole).toString(), QLatin1String("A"));
35 QCOMPARE(static_cast<Qt::CheckState>(optContent->data(index, Qt::CheckStateRole).toInt()), Qt::Checked);
36 index = optContent->index(1, 0, QModelIndex());
37 QCOMPARE(optContent->data(index, Qt::DisplayRole).toString(), QLatin1String("B"));
38 QCOMPARE(static_cast<Qt::CheckState>(optContent->data(index, Qt::CheckStateRole).toInt()), Qt::Checked);
39
40 delete doc;
41 }
42
43 void TestOptionalContent::checkNestedLayers()
44 {
45 Poppler::Document *doc;
46 doc = Poppler::Document::load(QStringLiteral(TESTDATADIR "/unittestcases/NestedLayers.pdf"));
47 QVERIFY(doc);
48
49 QVERIFY(doc->hasOptionalContent());
50
51 Poppler::OptContentModel *optContent = doc->optionalContentModel();
52 QModelIndex index;
53
54 index = optContent->index(0, 0, QModelIndex());
55 QCOMPARE(optContent->data(index, Qt::DisplayRole).toString(), QLatin1String("Black Text and Green Snow"));
56 QCOMPARE(static_cast<Qt::CheckState>(optContent->data(index, Qt::CheckStateRole).toInt()), Qt::Unchecked);
57
58 index = optContent->index(1, 0, QModelIndex());
59 QCOMPARE(optContent->data(index, Qt::DisplayRole).toString(), QLatin1String("Mountains and Image"));
60 QCOMPARE(static_cast<Qt::CheckState>(optContent->data(index, Qt::CheckStateRole).toInt()), Qt::Checked);
61
62 // This is a sub-item of "Mountains and Image"
63 QModelIndex subindex = optContent->index(0, 0, index);
64 QCOMPARE(optContent->data(subindex, Qt::DisplayRole).toString(), QLatin1String("Image"));
65 QCOMPARE(static_cast<Qt::CheckState>(optContent->data(index, Qt::CheckStateRole).toInt()), Qt::Checked);
66
67 index = optContent->index(2, 0, QModelIndex());
68 QCOMPARE(optContent->data(index, Qt::DisplayRole).toString(), QLatin1String("Starburst"));
69 QCOMPARE(static_cast<Qt::CheckState>(optContent->data(index, Qt::CheckStateRole).toInt()), Qt::Checked);
70
71 index = optContent->index(3, 0, QModelIndex());
72 QCOMPARE(optContent->data(index, Qt::DisplayRole).toString(), QLatin1String("Watermark"));
73 QCOMPARE(static_cast<Qt::CheckState>(optContent->data(index, Qt::CheckStateRole).toInt()), Qt::Unchecked);
74
75 delete doc;
76 }
77
78 void TestOptionalContent::checkNoOptionalContent()
79 {
80 Poppler::Document *doc;
81 doc = Poppler::Document::load(QStringLiteral(TESTDATADIR "/unittestcases/orientation.pdf"));
82 QVERIFY(doc);
83
84 QCOMPARE(doc->hasOptionalContent(), false);
85
86 delete doc;
87 }
88
89 void TestOptionalContent::checkIsVisible()
90 {
91 globalParams = std::make_unique<GlobalParams>();
92 PDFDoc *doc = new PDFDoc(std::make_unique<GooString>(TESTDATADIR "/unittestcases/vis_policy_test.pdf"));
93 QVERIFY(doc);
94
95 const OCGs *ocgs = doc->getOptContentConfig();
96 QVERIFY(ocgs);
97
98 XRef *xref = doc->getXRef();
99
100 Object obj;
101
102 // In this test, both Ref(21,0) and Ref(2,0) are set to On
103
104 // AnyOn, one element array:
105 // 22 0 obj<</Type/OCMD/OCGs[21 0 R]/P/AnyOn>>endobj
106 obj = xref->fetch(22, 0);
107 QVERIFY(obj.isDict());
108 QVERIFY(ocgs->optContentIsVisible(&obj));
109
110 // Same again, looking for any leaks or dubious free()'s
111 obj = xref->fetch(22, 0);
112 QVERIFY(obj.isDict());
113 QVERIFY(ocgs->optContentIsVisible(&obj));
114
115 // AnyOff, one element array:
116 // 29 0 obj<</Type/OCMD/OCGs[21 0 R]/P/AnyOff>>endobj
117 obj = xref->fetch(29, 0);
118 QVERIFY(obj.isDict());
119 QCOMPARE(ocgs->optContentIsVisible(&obj), false);
120
121 // AllOn, one element array:
122 // 36 0 obj<</Type/OCMD/OCGs[28 0 R]/P/AllOn>>endobj
123 obj = xref->fetch(36, 0);
124 QVERIFY(obj.isDict());
125 QCOMPARE(ocgs->optContentIsVisible(&obj), true);
126
127 // AllOff, one element array:
128 // 43 0 obj<</Type/OCMD/OCGs[28 0 R]/P/AllOff>>endobj
129 obj = xref->fetch(43, 0);
130 QVERIFY(obj.isDict());
131 QCOMPARE(ocgs->optContentIsVisible(&obj), false);
132
133 // AnyOn, multi-element array:
134 // 50 0 obj<</Type/OCMD/OCGs[21 0 R 28 0 R]/P/AnyOn>>endobj
135 obj = xref->fetch(50, 0);
136 QVERIFY(obj.isDict());
137 QCOMPARE(ocgs->optContentIsVisible(&obj), true);
138
139 // AnyOff, multi-element array:
140 // 57 0 obj<</Type/OCMD/P/AnyOff/OCGs[21 0 R 28 0 R]>>endobj
141 obj = xref->fetch(57, 0);
142 QVERIFY(obj.isDict());
143 QCOMPARE(ocgs->optContentIsVisible(&obj), false);
144
145 // AllOn, multi-element array:
146 // 64 0 obj<</Type/OCMD/P/AllOn/OCGs[21 0 R 28 0 R]>>endobj
147 obj = xref->fetch(64, 0);
148 QVERIFY(obj.isDict());
149 QCOMPARE(ocgs->optContentIsVisible(&obj), true);
150
151 // AllOff, multi-element array:
152 // 71 0 obj<</Type/OCMD/P/AllOff/OCGs[21 0 R 28 0 R]>>endobj
153 obj = xref->fetch(71, 0);
154 QVERIFY(obj.isDict());
155 QCOMPARE(ocgs->optContentIsVisible(&obj), false);
156
157 delete doc;
158 globalParams.reset();
159 }
160
161 void TestOptionalContent::checkVisibilitySetting()
162 {
163 globalParams = std::make_unique<GlobalParams>();
164 PDFDoc *doc = new PDFDoc(std::make_unique<GooString>(TESTDATADIR "/unittestcases/vis_policy_test.pdf"));
165 QVERIFY(doc);
166
167 const OCGs *ocgs = doc->getOptContentConfig();
168 QVERIFY(ocgs);
169
170 XRef *xref = doc->getXRef();
171
172 Object obj;
173
174 // In this test, both Ref(21,0) and Ref(28,0) start On,
175 // based on the file settings
176 Object ref21obj(Ref { 21, 0 });
177 Ref ref21 = ref21obj.getRef();
178 OptionalContentGroup *ocgA = ocgs->findOcgByRef(ref21);
179 QVERIFY(ocgA);
180
181 QVERIFY((ocgA->getName()->cmp("A")) == 0);
182 QCOMPARE(ocgA->getState(), OptionalContentGroup::On);
183
184 Object ref28obj(Ref { 28, 0 });
185 Ref ref28 = ref28obj.getRef();
186 OptionalContentGroup *ocgB = ocgs->findOcgByRef(ref28);
187 QVERIFY(ocgB);
188
189 QVERIFY((ocgB->getName()->cmp("B")) == 0);
190 QCOMPARE(ocgB->getState(), OptionalContentGroup::On);
191
192 // turn one Off
193 ocgA->setState(OptionalContentGroup::Off);
194
195 // AnyOn, one element array:
196 // 22 0 obj<</Type/OCMD/OCGs[21 0 R]/P/AnyOn>>endobj
197 obj = xref->fetch(22, 0);
198 QVERIFY(obj.isDict());
199 QCOMPARE(ocgs->optContentIsVisible(&obj), false);
200
201 // Same again, looking for any leaks or dubious free()'s
202 obj = xref->fetch(22, 0);
203 QVERIFY(obj.isDict());
204 QCOMPARE(ocgs->optContentIsVisible(&obj), false);
205
206 // AnyOff, one element array:
207 // 29 0 obj<</Type/OCMD/OCGs[21 0 R]/P/AnyOff>>endobj
208 obj = xref->fetch(29, 0);
209 QVERIFY(obj.isDict());
210 QCOMPARE(ocgs->optContentIsVisible(&obj), true);
211
212 // AllOn, one element array:
213 // 36 0 obj<</Type/OCMD/OCGs[28 0 R]/P/AllOn>>endobj
214 obj = xref->fetch(36, 0);
215 QVERIFY(obj.isDict());
216 QCOMPARE(ocgs->optContentIsVisible(&obj), true);
217
218 // AllOff, one element array:
219 // 43 0 obj<</Type/OCMD/OCGs[28 0 R]/P/AllOff>>endobj
220 obj = xref->fetch(43, 0);
221 QVERIFY(obj.isDict());
222 QCOMPARE(ocgs->optContentIsVisible(&obj), false);
223
224 // AnyOn, multi-element array:
225 // 50 0 obj<</Type/OCMD/OCGs[21 0 R 28 0 R]/P/AnyOn>>endobj
226 obj = xref->fetch(50, 0);
227 QVERIFY(obj.isDict());
228 QCOMPARE(ocgs->optContentIsVisible(&obj), true);
229
230 // AnyOff, multi-element array:
231 // 57 0 obj<</Type/OCMD/P/AnyOff/OCGs[21 0 R 28 0 R]>>endobj
232 obj = xref->fetch(57, 0);
233 QVERIFY(obj.isDict());
234 QCOMPARE(ocgs->optContentIsVisible(&obj), true);
235
236 // AllOn, multi-element array:
237 // 64 0 obj<</Type/OCMD/P/AllOn/OCGs[21 0 R 28 0 R]>>endobj
238 obj = xref->fetch(64, 0);
239 QVERIFY(obj.isDict());
240 QCOMPARE(ocgs->optContentIsVisible(&obj), false);
241
242 // AllOff, multi-element array:
243 // 71 0 obj<</Type/OCMD/P/AllOff/OCGs[21 0 R 28 0 R]>>endobj
244 obj = xref->fetch(71, 0);
245 QVERIFY(obj.isDict());
246 QCOMPARE(ocgs->optContentIsVisible(&obj), false);
247
248 // Turn the other one off as well (i.e. both are Off)
249 ocgB->setState(OptionalContentGroup::Off);
250
251 // AnyOn, one element array:
252 // 22 0 obj<</Type/OCMD/OCGs[21 0 R]/P/AnyOn>>endobj
253 obj = xref->fetch(22, 0);
254 QVERIFY(obj.isDict());
255 QCOMPARE(ocgs->optContentIsVisible(&obj), false);
256
257 // Same again, looking for any leaks or dubious free()'s
258 obj = xref->fetch(22, 0);
259 QVERIFY(obj.isDict());
260 QCOMPARE(ocgs->optContentIsVisible(&obj), false);
261
262 // AnyOff, one element array:
263 // 29 0 obj<</Type/OCMD/OCGs[21 0 R]/P/AnyOff>>endobj
264 obj = xref->fetch(29, 0);
265 QVERIFY(obj.isDict());
266 QCOMPARE(ocgs->optContentIsVisible(&obj), true);
267
268 // AllOn, one element array:
269 // 36 0 obj<</Type/OCMD/OCGs[28 0 R]/P/AllOn>>endobj
270 obj = xref->fetch(36, 0);
271 QVERIFY(obj.isDict());
272 QCOMPARE(ocgs->optContentIsVisible(&obj), false);
273
274 // AllOff, one element array:
275 // 43 0 obj<</Type/OCMD/OCGs[28 0 R]/P/AllOff>>endobj
276 obj = xref->fetch(43, 0);
277 QVERIFY(obj.isDict());
278 QCOMPARE(ocgs->optContentIsVisible(&obj), true);
279
280 // AnyOn, multi-element array:
281 // 50 0 obj<</Type/OCMD/OCGs[21 0 R 28 0 R]/P/AnyOn>>endobj
282 obj = xref->fetch(50, 0);
283 QVERIFY(obj.isDict());
284 QCOMPARE(ocgs->optContentIsVisible(&obj), false);
285
286 // AnyOff, multi-element array:
287 // 57 0 obj<</Type/OCMD/P/AnyOff/OCGs[21 0 R 28 0 R]>>endobj
288 obj = xref->fetch(57, 0);
289 QVERIFY(obj.isDict());
290 QCOMPARE(ocgs->optContentIsVisible(&obj), true);
291
292 // AllOn, multi-element array:
293 // 64 0 obj<</Type/OCMD/P/AllOn/OCGs[21 0 R 28 0 R]>>endobj
294 obj = xref->fetch(64, 0);
295 QVERIFY(obj.isDict());
296 QCOMPARE(ocgs->optContentIsVisible(&obj), false);
297
298 // AllOff, multi-element array:
299 // 71 0 obj<</Type/OCMD/P/AllOff/OCGs[21 0 R 28 0 R]>>endobj
300 obj = xref->fetch(71, 0);
301 QVERIFY(obj.isDict());
302 QCOMPARE(ocgs->optContentIsVisible(&obj), true);
303
304 // Turn the first one on again (21 is On, 28 is Off)
305 ocgA->setState(OptionalContentGroup::On);
306
307 // AnyOn, one element array:
308 // 22 0 obj<</Type/OCMD/OCGs[21 0 R]/P/AnyOn>>endobj
309 obj = xref->fetch(22, 0);
310 QVERIFY(obj.isDict());
311 QCOMPARE(ocgs->optContentIsVisible(&obj), true);
312
313 // Same again, looking for any leaks or dubious free()'s
314 obj = xref->fetch(22, 0);
315 QVERIFY(obj.isDict());
316 QCOMPARE(ocgs->optContentIsVisible(&obj), true);
317
318 // AnyOff, one element array:
319 // 29 0 obj<</Type/OCMD/OCGs[21 0 R]/P/AnyOff>>endobj
320 obj = xref->fetch(29, 0);
321 QVERIFY(obj.isDict());
322 QCOMPARE(ocgs->optContentIsVisible(&obj), false);
323
324 // AllOn, one element array:
325 // 36 0 obj<</Type/OCMD/OCGs[28 0 R]/P/AllOn>>endobj
326 obj = xref->fetch(36, 0);
327 QVERIFY(obj.isDict());
328 QCOMPARE(ocgs->optContentIsVisible(&obj), false);
329
330 // AllOff, one element array:
331 // 43 0 obj<</Type/OCMD/OCGs[28 0 R]/P/AllOff>>endobj
332 obj = xref->fetch(43, 0);
333 QVERIFY(obj.isDict());
334 QCOMPARE(ocgs->optContentIsVisible(&obj), true);
335
336 // AnyOn, multi-element array:
337 // 50 0 obj<</Type/OCMD/OCGs[21 0 R 28 0 R]/P/AnyOn>>endobj
338 obj = xref->fetch(50, 0);
339 QVERIFY(obj.isDict());
340 QCOMPARE(ocgs->optContentIsVisible(&obj), true);
341
342 // AnyOff, multi-element array:
343 // 57 0 obj<</Type/OCMD/P/AnyOff/OCGs[21 0 R 28 0 R]>>endobj
344 obj = xref->fetch(57, 0);
345 QVERIFY(obj.isDict());
346 QCOMPARE(ocgs->optContentIsVisible(&obj), true);
347
348 // AllOn, multi-element array:
349 // 64 0 obj<</Type/OCMD/P/AllOn/OCGs[21 0 R 28 0 R]>>endobj
350 obj = xref->fetch(64, 0);
351 QVERIFY(obj.isDict());
352 QCOMPARE(ocgs->optContentIsVisible(&obj), false);
353
354 // AllOff, multi-element array:
355 // 71 0 obj<</Type/OCMD/P/AllOff/OCGs[21 0 R 28 0 R]>>endobj
356 obj = xref->fetch(71, 0);
357 QVERIFY(obj.isDict());
358 QCOMPARE(ocgs->optContentIsVisible(&obj), false);
359
360 delete doc;
361 globalParams.reset();
362 }
363
364 void TestOptionalContent::checkRadioButtons()
365 {
366 Poppler::Document *doc;
367 doc = Poppler::Document::load(QStringLiteral(TESTDATADIR "/unittestcases/ClarityOCGs.pdf"));
368 QVERIFY(doc);
369
370 QVERIFY(doc->hasOptionalContent());
371
372 Poppler::OptContentModel *optContent = doc->optionalContentModel();
373 QModelIndex index;
374
375 index = optContent->index(0, 0, QModelIndex());
376 QCOMPARE(optContent->data(index, Qt::DisplayRole).toString(), QLatin1String("Languages"));
377 QCOMPARE(static_cast<Qt::CheckState>(optContent->data(index, Qt::CheckStateRole).toInt()), Qt::Unchecked);
378
379 // These are sub-items of the "Languages" label
380 QModelIndex subindex = optContent->index(0, 0, index);
381 QCOMPARE(optContent->data(subindex, Qt::DisplayRole).toString(), QLatin1String("English"));
382 QCOMPARE(static_cast<Qt::CheckState>(optContent->data(subindex, Qt::CheckStateRole).toInt()), Qt::Checked);
383
384 subindex = optContent->index(1, 0, index);
385 QCOMPARE(optContent->data(subindex, Qt::DisplayRole).toString(), QLatin1String("French"));
386 QCOMPARE(static_cast<Qt::CheckState>(optContent->data(subindex, Qt::CheckStateRole).toInt()), Qt::Unchecked);
387
388 subindex = optContent->index(2, 0, index);
389 QCOMPARE(optContent->data(subindex, Qt::DisplayRole).toString(), QLatin1String("Japanese"));
390 QCOMPARE(static_cast<Qt::CheckState>(optContent->data(subindex, Qt::CheckStateRole).toInt()), Qt::Unchecked);
391
392 // RBGroup of languages, so turning on Japanese should turn off English
393 QVERIFY(optContent->setData(subindex, QVariant(true), Qt::CheckStateRole));
394
395 subindex = optContent->index(0, 0, index);
396 QCOMPARE(optContent->data(subindex, Qt::DisplayRole).toString(), QLatin1String("English"));
397 QCOMPARE(static_cast<Qt::CheckState>(optContent->data(subindex, Qt::CheckStateRole).toInt()), Qt::Unchecked);
398 QCOMPARE(static_cast<Poppler::OptContentItem *>(subindex.internalPointer())->group()->getState(), OptionalContentGroup::Off);
399
400 subindex = optContent->index(2, 0, index);
401 QCOMPARE(optContent->data(subindex, Qt::DisplayRole).toString(), QLatin1String("Japanese"));
402 QCOMPARE(static_cast<Qt::CheckState>(optContent->data(subindex, Qt::CheckStateRole).toInt()), Qt::Checked);
403 QCOMPARE(static_cast<Poppler::OptContentItem *>(subindex.internalPointer())->group()->getState(), OptionalContentGroup::On);
404
405 subindex = optContent->index(1, 0, index);
406 QCOMPARE(optContent->data(subindex, Qt::DisplayRole).toString(), QLatin1String("French"));
407 QCOMPARE(static_cast<Qt::CheckState>(optContent->data(subindex, Qt::CheckStateRole).toInt()), Qt::Unchecked);
408 QCOMPARE(static_cast<Poppler::OptContentItem *>(subindex.internalPointer())->group()->getState(), OptionalContentGroup::Off);
409
410 // and turning on French should turn off Japanese
411 QVERIFY(optContent->setData(subindex, QVariant(true), Qt::CheckStateRole));
412
413 subindex = optContent->index(0, 0, index);
414 QCOMPARE(optContent->data(subindex, Qt::DisplayRole).toString(), QLatin1String("English"));
415 QCOMPARE(static_cast<Qt::CheckState>(optContent->data(subindex, Qt::CheckStateRole).toInt()), Qt::Unchecked);
416 QCOMPARE(static_cast<Poppler::OptContentItem *>(subindex.internalPointer())->group()->getState(), OptionalContentGroup::Off);
417
418 subindex = optContent->index(2, 0, index);
419 QCOMPARE(optContent->data(subindex, Qt::DisplayRole).toString(), QLatin1String("Japanese"));
420 QCOMPARE(static_cast<Qt::CheckState>(optContent->data(subindex, Qt::CheckStateRole).toInt()), Qt::Unchecked);
421 QCOMPARE(static_cast<Poppler::OptContentItem *>(subindex.internalPointer())->group()->getState(), OptionalContentGroup::Off);
422
423 subindex = optContent->index(1, 0, index);
424 QCOMPARE(optContent->data(subindex, Qt::DisplayRole).toString(), QLatin1String("French"));
425 QCOMPARE(static_cast<Qt::CheckState>(optContent->data(subindex, Qt::CheckStateRole).toInt()), Qt::Checked);
426 QCOMPARE(static_cast<Poppler::OptContentItem *>(subindex.internalPointer())->group()->getState(), OptionalContentGroup::On);
427
428 // and turning off French should leave them all off
429 QVERIFY(optContent->setData(subindex, QVariant(false), Qt::CheckStateRole));
430
431 subindex = optContent->index(0, 0, index);
432 QCOMPARE(optContent->data(subindex, Qt::DisplayRole).toString(), QLatin1String("English"));
433 QCOMPARE(static_cast<Qt::CheckState>(optContent->data(subindex, Qt::CheckStateRole).toInt()), Qt::Unchecked);
434 QCOMPARE(static_cast<Poppler::OptContentItem *>(subindex.internalPointer())->group()->getState(), OptionalContentGroup::Off);
435
436 subindex = optContent->index(2, 0, index);
437 QCOMPARE(optContent->data(subindex, Qt::DisplayRole).toString(), QLatin1String("Japanese"));
438 QCOMPARE(static_cast<Qt::CheckState>(optContent->data(subindex, Qt::CheckStateRole).toInt()), Qt::Unchecked);
439 QCOMPARE(static_cast<Poppler::OptContentItem *>(subindex.internalPointer())->group()->getState(), OptionalContentGroup::Off);
440
441 subindex = optContent->index(1, 0, index);
442 QCOMPARE(optContent->data(subindex, Qt::DisplayRole).toString(), QLatin1String("French"));
443 QCOMPARE(static_cast<Qt::CheckState>(optContent->data(subindex, Qt::CheckStateRole).toInt()), Qt::Unchecked);
444 QCOMPARE(static_cast<Poppler::OptContentItem *>(subindex.internalPointer())->group()->getState(), OptionalContentGroup::Off);
445
446 delete doc;
447 }
448
449 QTEST_GUILESS_MAIN(TestOptionalContent)
450
451 #include "check_optcontent.moc"