Debian Codegraph @

qt5/tests/check_links.cpp

raw
1 #include <QtTest/QTest>
2
3 #include <poppler-qt5.h>
4
5 #include <memory>
6
7 class TestLinks : public QObject
8 {
9 Q_OBJECT
10 public:
11 explicit TestLinks(QObject *parent = nullptr) : QObject(parent) { }
12 private Q_SLOTS:
13 void checkDocumentWithNoDests();
14 void checkDests_xr01();
15 void checkDests_xr02();
16 void checkDocumentURILink();
17 };
18
19 static bool isDestinationValid_pageNumber(const Poppler::LinkDestination *dest, const Poppler::Document *doc)
20 {
21 return dest->pageNumber() > 0 && dest->pageNumber() <= doc->numPages();
22 }
23
24 static bool isDestinationValid_name(const Poppler::LinkDestination *dest)
25 {
26 return !dest->destinationName().isEmpty();
27 }
28
29 void TestLinks::checkDocumentWithNoDests()
30 {
31 Poppler::Document *doc;
32 doc = Poppler::Document::load(QStringLiteral(TESTDATADIR "/unittestcases/WithAttachments.pdf"));
33 QVERIFY(doc);
34
35 std::unique_ptr<Poppler::LinkDestination> dest;
36 dest.reset(doc->linkDestination(QStringLiteral("no.dests.in.this.document")));
37 QVERIFY(!isDestinationValid_pageNumber(dest.get(), doc));
38 QVERIFY(isDestinationValid_name(dest.get()));
39
40 delete doc;
41 }
42
43 void TestLinks::checkDests_xr01()
44 {
45 Poppler::Document *doc;
46 doc = Poppler::Document::load(QStringLiteral(TESTDATADIR "/unittestcases/xr01.pdf"));
47 QVERIFY(doc);
48
49 Poppler::Page *page = doc->page(0);
50 QVERIFY(page);
51
52 QList<Poppler::Link *> links = page->links();
53 QCOMPARE(links.count(), 2);
54
55 {
56 QCOMPARE(links.at(0)->linkType(), Poppler::Link::Goto);
57 Poppler::LinkGoto *link = static_cast<Poppler::LinkGoto *>(links.at(0));
58 const Poppler::LinkDestination dest = link->destination();
59 QVERIFY(!isDestinationValid_pageNumber(&dest, doc));
60 QVERIFY(isDestinationValid_name(&dest));
61 QCOMPARE(dest.destinationName(), QLatin1String("section.1"));
62 }
63
64 {
65 QCOMPARE(links.at(1)->linkType(), Poppler::Link::Goto);
66 Poppler::LinkGoto *link = static_cast<Poppler::LinkGoto *>(links.at(1));
67 const Poppler::LinkDestination dest = link->destination();
68 QVERIFY(!isDestinationValid_pageNumber(&dest, doc));
69 QVERIFY(isDestinationValid_name(&dest));
70 QCOMPARE(dest.destinationName(), QLatin1String("section.2"));
71 }
72
73 qDeleteAll(links);
74 delete page;
75 delete doc;
76 }
77
78 void TestLinks::checkDests_xr02()
79 {
80 Poppler::Document *doc;
81 doc = Poppler::Document::load(QStringLiteral(TESTDATADIR "/unittestcases/xr02.pdf"));
82 QVERIFY(doc);
83
84 std::unique_ptr<Poppler::LinkDestination> dest;
85 dest.reset(doc->linkDestination(QStringLiteral("section.1")));
86 QVERIFY(isDestinationValid_pageNumber(dest.get(), doc));
87 QVERIFY(!isDestinationValid_name(dest.get()));
88 dest.reset(doc->linkDestination(QStringLiteral("section.2")));
89 QVERIFY(isDestinationValid_pageNumber(dest.get(), doc));
90 QVERIFY(!isDestinationValid_name(dest.get()));
91 dest.reset(doc->linkDestination(QStringLiteral("section.3")));
92 QVERIFY(!isDestinationValid_pageNumber(dest.get(), doc));
93 QVERIFY(isDestinationValid_name(dest.get()));
94
95 delete doc;
96 }
97
98 void TestLinks::checkDocumentURILink()
99 {
100 Poppler::Document *doc;
101 doc = Poppler::Document::load(QStringLiteral(TESTDATADIR "/unittestcases/checkbox_issue_159.pdf"));
102 QVERIFY(doc);
103
104 Poppler::Page *page = doc->page(0);
105 QVERIFY(page);
106
107 QList<Poppler::Link *> links = page->links();
108 QCOMPARE(links.count(), 1);
109
110 QCOMPARE(links.at(0)->linkType(), Poppler::Link::Browse);
111 Poppler::LinkBrowse *link = static_cast<Poppler::LinkBrowse *>(links.at(0));
112 QCOMPARE(link->url(), QLatin1String("http://www.tcpdf.org"));
113
114 qDeleteAll(links);
115 delete page;
116 delete doc;
117 }
118
119 QTEST_GUILESS_MAIN(TestLinks)
120
121 #include "check_links.moc"