File indexing completed on 2024-05-12 05:17:30
0001 /* 0002 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include <KItinerary/ExtractorDocumentNode> 0008 #include <KItinerary/ExtractorDocumentNodeFactory> 0009 #include <KItinerary/ExtractorDocumentProcessor> 0010 #include <KItinerary/ExtractorEngine> 0011 #include <KItinerary/PdfDocument> 0012 0013 #include <QFile> 0014 #include <QTest> 0015 0016 using namespace KItinerary; 0017 0018 #define s(x) QStringLiteral(x) 0019 0020 #ifdef Q_OS_WINDOWS 0021 #define LINEBREAK "\r\n" 0022 #else 0023 #define LINEBREAK "\n" 0024 #endif 0025 0026 class ExtractorDocumentNodeTest : public QObject 0027 { 0028 Q_OBJECT 0029 private Q_SLOTS: 0030 void testBasics() 0031 { 0032 ExtractorDocumentNodeFactory factory; 0033 ExtractorDocumentNode node; 0034 QVERIFY(node.isNull()); 0035 node = {}; 0036 QVERIFY(node.isNull()); 0037 0038 node.setContent(s("a plain text content")); 0039 node.setMimeType(s("text/plain")); 0040 node.setContextDateTime(QDateTime::currentDateTime()); 0041 QVERIFY(node.isNull()); // not properly constructed 0042 0043 auto child = factory.createNode(QVariant::fromValue(QByteArray("data")), u"application/octet-stream"); 0044 QVERIFY(!child.isNull()); 0045 node.appendChild(child); 0046 QCOMPARE(node.childNodes().size(), 1); 0047 QCOMPARE(child.parent().mimeType(), QLatin1StringView("text/plain")); 0048 QVERIFY(child.contextDateTime().isValid()); 0049 QCOMPARE(child.contextDateTime(), node.contextDateTime()); 0050 0051 QVERIFY(node.parent().isNull()); 0052 QVERIFY(node.parent().parent().isNull()); 0053 } 0054 0055 void testPdfFromData() 0056 { 0057 ExtractorEngine engine; 0058 0059 QFile f(QStringLiteral(SOURCE_DIR "/misc/test.pdf")); 0060 QVERIFY(f.open(QFile::ReadOnly)); 0061 auto root = engine.documentNodeFactory()->createNode(f.readAll()); 0062 QVERIFY(!root.isNull()); 0063 QCOMPARE(root.mimeType(), QLatin1StringView("application/pdf")); 0064 QCOMPARE(root.childNodes().size(), 0); 0065 QVERIFY(root.location().isNull()); 0066 0067 root.processor()->expandNode(root, &engine); 0068 QCOMPARE(root.childNodes().size(), 3); 0069 0070 auto c3 = root.childNodes()[2]; 0071 QVERIFY(!c3.isNull()); 0072 QCOMPARE(c3.mimeType(), QLatin1StringView("text/plain")); 0073 QCOMPARE(c3.content<QString>(), 0074 QLatin1StringView("This is the first page." LINEBREAK 0075 "It contains a PDF 417 barcode." LINEBREAK 0076 "This is the second page." LINEBREAK 0077 "It contains an Aztec code." LINEBREAK)); 0078 0079 auto c1 = root.childNodes()[0]; 0080 QVERIFY(!c1.isNull()); 0081 QCOMPARE(c1.mimeType(), QLatin1StringView("internal/qimage")); 0082 QVERIFY(!c1.parent().isNull()); 0083 QCOMPARE(c1.content().userType(), qMetaTypeId<QImage>()); 0084 QCOMPARE(c1.childNodes().size(), 1); // barcode already expanded by the PDF processor 0085 QCOMPARE(c1.location().toInt(), 0); 0086 0087 c1.processor()->expandNode(c1, &engine); 0088 QCOMPARE(c1.childNodes().size(), 1); 0089 auto c11 = c1.childNodes()[0]; 0090 QVERIFY(!c11.isNull()); 0091 QCOMPARE(c11.mimeType(), QLatin1StringView("text/plain")); 0092 QCOMPARE(c11.content<QString>(), 0093 QLatin1StringView( 0094 "PDF417 is a stacked linear barcode symbol format used in " 0095 "a variety of applications, primarily transport, " 0096 "identification cards, and inventory management.")); 0097 QCOMPARE(c11.location().toInt(), 0); 0098 } 0099 0100 void testPdfFromContent() 0101 { 0102 ExtractorEngine engine; 0103 0104 QFile f(QStringLiteral(SOURCE_DIR "/misc/test.pdf")); 0105 QVERIFY(f.open(QFile::ReadOnly)); 0106 std::unique_ptr<PdfDocument> pdf(PdfDocument::fromData(f.readAll())); 0107 auto root = engine.documentNodeFactory()->createNode(QVariant::fromValue(pdf.get()), u"application/pdf"); 0108 QVERIFY(!root.isNull()); 0109 QCOMPARE(root.mimeType(), QLatin1StringView("application/pdf")); 0110 0111 root.processor()->expandNode(root, &engine); 0112 QCOMPARE(root.childNodes().size(), 3); 0113 auto c2 = root.childNodes()[1]; 0114 QVERIFY(!c2.isNull()); 0115 QCOMPARE(c2.mimeType(), QLatin1StringView("internal/qimage")); 0116 QCOMPARE(c2.location().toInt(), 1); 0117 0118 c2.processor()->expandNode(c2, &engine); 0119 QCOMPARE(c2.childNodes().size(), 1); 0120 auto c21 = c2.childNodes()[0]; 0121 QVERIFY(!c21.isNull()); 0122 QCOMPARE(c21.mimeType(), QLatin1StringView("text/plain")); 0123 QCOMPARE(c21.content<QString>(), 0124 QLatin1StringView( 0125 "This is an example Aztec symbol for Wikipedia.")); 0126 QCOMPARE(c21.location().toInt(), 1); 0127 } 0128 0129 void testPdfExternal() 0130 { 0131 ExtractorEngine engine; 0132 engine.setUseSeparateProcess(true); 0133 0134 QFile f(QStringLiteral(SOURCE_DIR "/misc/test.pdf")); 0135 QVERIFY(f.open(QFile::ReadOnly)); 0136 auto root = engine.documentNodeFactory()->createNode(f.readAll()); 0137 QVERIFY(!root.isNull()); 0138 QCOMPARE(root.mimeType(), QLatin1StringView("application/pdf")); 0139 root.processor()->expandNode(root, &engine); 0140 QCOMPARE(root.childNodes().size(), 0); 0141 } 0142 }; 0143 0144 QTEST_GUILESS_MAIN(ExtractorDocumentNodeTest) 0145 0146 #include "extractordocumentnodetest.moc"