File size: 3,685 Bytes
5cee033
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include <QtTest/QTest>

#include <poppler-qt5.h>

#include <memory>

class TestLinks : public QObject
{
    Q_OBJECT
public:
    explicit TestLinks(QObject *parent = nullptr) : QObject(parent) { }
private slots:
    void checkDocumentWithNoDests();
    void checkDests_xr01();
    void checkDests_xr02();
    void checkDocumentURILink();
};

static bool isDestinationValid_pageNumber(const Poppler::LinkDestination *dest, const Poppler::Document *doc)
{
    return dest->pageNumber() > 0 && dest->pageNumber() <= doc->numPages();
}

static bool isDestinationValid_name(const Poppler::LinkDestination *dest)
{
    return !dest->destinationName().isEmpty();
}

void TestLinks::checkDocumentWithNoDests()
{
    Poppler::Document *doc;
    doc = Poppler::Document::load(TESTDATADIR "/unittestcases/WithAttachments.pdf");
    QVERIFY(doc);

    std::unique_ptr<Poppler::LinkDestination> dest;
    dest.reset(doc->linkDestination(QStringLiteral("no.dests.in.this.document")));
    QVERIFY(!isDestinationValid_pageNumber(dest.get(), doc));
    QVERIFY(isDestinationValid_name(dest.get()));

    delete doc;
}

void TestLinks::checkDests_xr01()
{
    Poppler::Document *doc;
    doc = Poppler::Document::load(TESTDATADIR "/unittestcases/xr01.pdf");
    QVERIFY(doc);

    Poppler::Page *page = doc->page(0);
    QVERIFY(page);

    QList<Poppler::Link *> links = page->links();
    QCOMPARE(links.count(), 2);

    {
        QCOMPARE(links.at(0)->linkType(), Poppler::Link::Goto);
        Poppler::LinkGoto *link = static_cast<Poppler::LinkGoto *>(links.at(0));
        const Poppler::LinkDestination dest = link->destination();
        QVERIFY(!isDestinationValid_pageNumber(&dest, doc));
        QVERIFY(isDestinationValid_name(&dest));
        QCOMPARE(dest.destinationName(), QLatin1String("section.1"));
    }

    {
        QCOMPARE(links.at(1)->linkType(), Poppler::Link::Goto);
        Poppler::LinkGoto *link = static_cast<Poppler::LinkGoto *>(links.at(1));
        const Poppler::LinkDestination dest = link->destination();
        QVERIFY(!isDestinationValid_pageNumber(&dest, doc));
        QVERIFY(isDestinationValid_name(&dest));
        QCOMPARE(dest.destinationName(), QLatin1String("section.2"));
    }

    qDeleteAll(links);
    delete page;
    delete doc;
}

void TestLinks::checkDests_xr02()
{
    Poppler::Document *doc;
    doc = Poppler::Document::load(TESTDATADIR "/unittestcases/xr02.pdf");
    QVERIFY(doc);

    std::unique_ptr<Poppler::LinkDestination> dest;
    dest.reset(doc->linkDestination(QStringLiteral("section.1")));
    QVERIFY(isDestinationValid_pageNumber(dest.get(), doc));
    QVERIFY(!isDestinationValid_name(dest.get()));
    dest.reset(doc->linkDestination(QStringLiteral("section.2")));
    QVERIFY(isDestinationValid_pageNumber(dest.get(), doc));
    QVERIFY(!isDestinationValid_name(dest.get()));
    dest.reset(doc->linkDestination(QStringLiteral("section.3")));
    QVERIFY(!isDestinationValid_pageNumber(dest.get(), doc));
    QVERIFY(isDestinationValid_name(dest.get()));

    delete doc;
}

void TestLinks::checkDocumentURILink()
{
    Poppler::Document *doc;
    doc = Poppler::Document::load(TESTDATADIR "/unittestcases/checkbox_issue_159.pdf");
    QVERIFY(doc);

    Poppler::Page *page = doc->page(0);
    QVERIFY(page);

    QList<Poppler::Link *> links = page->links();
    QCOMPARE(links.count(), 1);

    QCOMPARE(links.at(0)->linkType(), Poppler::Link::Browse);
    Poppler::LinkBrowse *link = static_cast<Poppler::LinkBrowse *>(links.at(0));
    QCOMPARE(link->url(), QLatin1String("http://www.tcpdf.org"));

    qDeleteAll(links);
    delete page;
    delete doc;
}

QTEST_GUILESS_MAIN(TestLinks)

#include "check_links.moc"