1 | #include <QtTest/QTest> |
2 | |
3 | #include <poppler-qt6.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 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 | std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/unittestcases/WithAttachments.pdf" ); |
32 | QVERIFY(doc); |
33 | |
34 | std::unique_ptr<Poppler::LinkDestination> dest = doc->linkDestination(QStringLiteral("no.dests.in.this.document" )); |
35 | QVERIFY(!isDestinationValid_pageNumber(dest.get(), doc.get())); |
36 | QVERIFY(isDestinationValid_name(dest.get())); |
37 | } |
38 | |
39 | void TestLinks::checkDests_xr01() |
40 | { |
41 | std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/unittestcases/xr01.pdf" ); |
42 | QVERIFY(doc); |
43 | |
44 | std::unique_ptr<Poppler::Page> page = doc->page(index: 0); |
45 | QVERIFY(page); |
46 | |
47 | std::vector<std::unique_ptr<Poppler::Link>> links = page->links(); |
48 | QCOMPARE(links.size(), 2); |
49 | |
50 | { |
51 | QCOMPARE(links.at(0)->linkType(), Poppler::Link::Goto); |
52 | Poppler::LinkGoto *link = static_cast<Poppler::LinkGoto *>(links.at(n: 0).get()); |
53 | const Poppler::LinkDestination dest = link->destination(); |
54 | QVERIFY(!isDestinationValid_pageNumber(&dest, doc.get())); |
55 | QVERIFY(isDestinationValid_name(&dest)); |
56 | QCOMPARE(dest.destinationName(), QLatin1String("section.1" )); |
57 | } |
58 | |
59 | { |
60 | QCOMPARE(links.at(1)->linkType(), Poppler::Link::Goto); |
61 | Poppler::LinkGoto *link = static_cast<Poppler::LinkGoto *>(links.at(n: 1).get()); |
62 | const Poppler::LinkDestination dest = link->destination(); |
63 | QVERIFY(!isDestinationValid_pageNumber(&dest, doc.get())); |
64 | QVERIFY(isDestinationValid_name(&dest)); |
65 | QCOMPARE(dest.destinationName(), QLatin1String("section.2" )); |
66 | } |
67 | } |
68 | |
69 | void TestLinks::checkDests_xr02() |
70 | { |
71 | std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/unittestcases/xr02.pdf" ); |
72 | QVERIFY(doc); |
73 | |
74 | std::unique_ptr<Poppler::LinkDestination> dest = doc->linkDestination(QStringLiteral("section.1" )); |
75 | QVERIFY(isDestinationValid_pageNumber(dest.get(), doc.get())); |
76 | QVERIFY(!isDestinationValid_name(dest.get())); |
77 | dest = doc->linkDestination(QStringLiteral("section.2" )); |
78 | QVERIFY(isDestinationValid_pageNumber(dest.get(), doc.get())); |
79 | QVERIFY(!isDestinationValid_name(dest.get())); |
80 | dest = doc->linkDestination(QStringLiteral("section.3" )); |
81 | QVERIFY(!isDestinationValid_pageNumber(dest.get(), doc.get())); |
82 | QVERIFY(isDestinationValid_name(dest.get())); |
83 | } |
84 | |
85 | void TestLinks::checkDocumentURILink() |
86 | { |
87 | std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/unittestcases/checkbox_issue_159.pdf" ); |
88 | QVERIFY(doc); |
89 | |
90 | std::unique_ptr<Poppler::Page> page = doc->page(index: 0); |
91 | QVERIFY(page); |
92 | |
93 | std::vector<std::unique_ptr<Poppler::Link>> links = page->links(); |
94 | QCOMPARE(links.size(), 1); |
95 | |
96 | QCOMPARE(links.at(0)->linkType(), Poppler::Link::Browse); |
97 | Poppler::LinkBrowse *link = static_cast<Poppler::LinkBrowse *>(links.at(n: 0).get()); |
98 | QCOMPARE(link->url(), QLatin1String("http://www.tcpdf.org" )); |
99 | } |
100 | |
101 | QTEST_GUILESS_MAIN(TestLinks) |
102 | |
103 | #include "check_links.moc" |
104 | |