File indexing completed on 2024-05-19 05:17:51

0001 /*
0002     SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <KItinerary/PdfDocument>
0008 #include <KItinerary/PdfLink>
0009 
0010 #include <QFile>
0011 #include <QObject>
0012 #include <QTest>
0013 
0014 using namespace KItinerary;
0015 
0016 #ifdef Q_OS_WINDOWS
0017 #define LINEBREAK "\r\n"
0018 #else
0019 #define LINEBREAK "\n"
0020 #endif
0021 
0022 class PdfDocumentTest : public QObject
0023 {
0024     Q_OBJECT
0025 private Q_SLOTS:
0026     void initTestCase()
0027     {
0028         qputenv("TZ", "UTC");
0029     }
0030 
0031     void testPdfDocument()
0032     {
0033         QFile f(QStringLiteral(SOURCE_DIR "/misc/test.pdf"));
0034         QVERIFY(f.open(QFile::ReadOnly));
0035         QVERIFY(PdfDocument::maybePdf(f.readAll()));
0036         f.seek(0);
0037         std::unique_ptr<PdfDocument> doc(PdfDocument::fromData(f.readAll()));
0038         QVERIFY(doc);
0039         QCOMPARE(doc->text(),
0040                  QLatin1StringView("This is the first page." LINEBREAK
0041                                    "It contains a PDF 417 barcode." LINEBREAK
0042                                    "This is the second page." LINEBREAK
0043                                    "It contains an Aztec code." LINEBREAK));
0044         QCOMPARE(doc->pageCount(), 2);
0045         QCOMPARE(doc->property("pages").toList().size(), 2);
0046 
0047         auto page = doc->page(0);
0048         QCOMPARE(page.text(),
0049                  QLatin1StringView("This is the first page." LINEBREAK
0050                                    "It contains a PDF 417 barcode." LINEBREAK));
0051         QCOMPARE(page.imageCount(), 1);
0052         QCOMPARE(PdfPage::staticMetaObject.property(1).readOnGadget(&page).toList().size(), 1);
0053         QCOMPARE(page.width(), 210);
0054         QCOMPARE(page.height(), 296);
0055 
0056         QCOMPARE(page.textInRect(0, 0, 1, 0.5),
0057                  QLatin1StringView("This is the first page." LINEBREAK
0058                                    "It contains a PDF 417 barcode." LINEBREAK));
0059         QCOMPARE(page.textInRect(0, 0.5, 1, 1), QString());
0060 
0061         auto img = page.image(0);
0062         QCOMPARE(img.width(), 212);
0063         QCOMPARE(img.height(), 92);
0064         QCOMPARE(img.sourceHeight(), 152);
0065         QCOMPARE(img.sourceWidth(), 350);
0066         QCOMPARE(img.image().width(), 350);
0067         QCOMPARE(img.image().height(), 152);
0068 
0069         page = doc->page(1);
0070         QCOMPARE(page.text(),
0071                  QLatin1StringView("This is the second page." LINEBREAK
0072                                    "It contains an Aztec code." LINEBREAK));
0073         QCOMPARE(page.imageCount(), 1);
0074         img = page.image(0);
0075         QCOMPARE(img.width(), 93);
0076         QCOMPARE(img.height(), 93);
0077         QCOMPARE(img.image().width(), 276);
0078         QCOMPARE(img.image().height(), 276);
0079         QCOMPARE(img.sourceHeight(), 276);
0080         QCOMPARE(img.sourceWidth(), 276);
0081 
0082         QVERIFY(page.imagesInRect(0, 0, 0.5, 1).isEmpty());
0083         QCOMPARE(page.imagesInRect(0, 0.5, 1, 1).size(), 1);
0084 
0085         QCOMPARE(doc->creationTime(), QDateTime({2018, 4, 29}, {11, 41, 28}, QTimeZone::fromSecondsAheadOfUtc(7200)));
0086         QCOMPARE(doc->modificationTime(), QDateTime());
0087     }
0088 
0089     void testPdfLink()
0090     {
0091         QFile f(QStringLiteral(SOURCE_DIR "/misc/link.pdf"));
0092         QVERIFY(f.open(QFile::ReadOnly));
0093         QVERIFY(PdfDocument::maybePdf(f.readAll()));
0094         f.seek(0);
0095         std::unique_ptr<PdfDocument> doc(PdfDocument::fromData(f.readAll()));
0096         QVERIFY(doc);
0097 
0098         QCOMPARE(doc->pageCount(), 1);
0099         const auto page = doc->page(0);
0100         QCOMPARE(page.linkCount(), 1);
0101         auto link = page.link(0);
0102         QCOMPARE(link.url(), QLatin1StringView("https://kde.org"));
0103         qDebug() << link.area();
0104         QVERIFY(link.area().isValid());
0105 
0106         QCOMPARE(page.linksInRect(0.0, 0.0, 1.0, 1.0).size(), 1);
0107         QCOMPARE(page.linksInRect(0.0, 0.0, 1.0, 0.5).size(), 1);
0108         link = page.linksInRect(0.0, 0.0, 0.5, 0.5).at(0).value<PdfLink>();
0109         QCOMPARE(link.url(), QLatin1StringView("https://kde.org"));
0110         QCOMPARE(page.linksInRect(0.0, 0.0, 0.2, 0.5).size(), 1);
0111         QCOMPARE(page.linksInRect(0.0, 0.5, 1.0, 1.0).size(), 0);
0112     }
0113 
0114     void testInvalidPdfDocument()
0115     {
0116         QVERIFY(!PdfDocument::maybePdf(QByteArray()));
0117         QVERIFY(!PdfDocument::fromData(QByteArray()));
0118         QVERIFY(!PdfDocument::maybePdf(QByteArray("HELLO")));
0119         QVERIFY(!PdfDocument::fromData(QByteArray("HELLO")));
0120 
0121         QFile f(QStringLiteral(SOURCE_DIR "/misc/test.pdf"));
0122         QVERIFY(f.open(QFile::ReadOnly));
0123         QVERIFY(!PdfDocument::fromData(f.readAll().left(f.size() / 2)));
0124     }
0125 };
0126 
0127 QTEST_GUILESS_MAIN(PdfDocumentTest)
0128 
0129 #include "pdfdocumenttest.moc"