| 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 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 |
std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(QStringLiteral(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(QStringLiteral(TESTDATADIR "/unittestcases/xr01.pdf")); |
| 42 |
QVERIFY(doc); |
| 43 |
|
| 44 |
std::unique_ptr<Poppler::Page> page = doc->page(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(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(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(QStringLiteral(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 |
// Better check that a link that should be in the middle of the sorted names does not give one of the neighboring ones |
| 84 |
dest = doc->linkDestination(QStringLiteral("section.15")); |
| 85 |
QVERIFY(!isDestinationValid_pageNumber(dest.get(), doc.get())); |
| 86 |
QVERIFY(isDestinationValid_name(dest.get())); |
| 87 |
} |
| 88 |
|
| 89 |
void TestLinks::checkDocumentURILink() |
| 90 |
{ |
| 91 |
std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(QStringLiteral(TESTDATADIR "/unittestcases/checkbox_issue_159.pdf")); |
| 92 |
QVERIFY(doc); |
| 93 |
|
| 94 |
std::unique_ptr<Poppler::Page> page = doc->page(0); |
| 95 |
QVERIFY(page); |
| 96 |
|
| 97 |
std::vector<std::unique_ptr<Poppler::Link>> links = page->links(); |
| 98 |
QCOMPARE(links.size(), 1); |
| 99 |
|
| 100 |
QCOMPARE(links.at(0)->linkType(), Poppler::Link::Browse); |
| 101 |
Poppler::LinkBrowse *link = static_cast<Poppler::LinkBrowse *>(links.at(0).get()); |
| 102 |
QCOMPARE(link->url(), QLatin1String("http://www.tcpdf.org")); |
| 103 |
} |
| 104 |
|
| 105 |
QTEST_GUILESS_MAIN(TestLinks) |
| 106 |
|
| 107 |
#include "check_links.moc" |