File indexing completed on 2024-05-12 04:33:28

0001 /*
0002     SPDX-FileCopyrightText: 2020 Markus Brenneis <support.gulp21+kde@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <QTest>
0008 
0009 #include "../settings_core.h"
0010 #include "core/document.h"
0011 #include "generators/markdown/converter.h"
0012 #include <QMimeDatabase>
0013 #include <QMimeType>
0014 #include <QTextDocument>
0015 
0016 class MarkdownTest : public QObject
0017 {
0018     Q_OBJECT
0019 
0020 private Q_SLOTS:
0021     void initTestCase();
0022 
0023     void testFancyPantsEnabled();
0024     void testFancyPantsDisabled();
0025     void testImageSizes();
0026     void testSpecialCharsInImageFileName();
0027 
0028 private:
0029     void findImages(QTextFrame *parent, QVector<QTextImageFormat> &images);
0030     void findImages(const QTextBlock &parent, QVector<QTextImageFormat> &images);
0031 };
0032 
0033 void MarkdownTest::initTestCase()
0034 {
0035     Okular::SettingsCore::instance(QStringLiteral("markdowntest"));
0036 }
0037 
0038 void MarkdownTest::testFancyPantsEnabled()
0039 {
0040     Markdown::Converter converter;
0041     converter.setFancyPantsEnabled(true);
0042     QTextDocument *document = converter.convert(QStringLiteral(KDESRCDIR "data/imageSizes.md"));
0043 
0044     QTextFrame::iterator secondFrame = ++(document->rootFrame()->begin());
0045     QVERIFY(secondFrame.currentBlock().text().startsWith(QStringLiteral("©")));
0046 }
0047 
0048 void MarkdownTest::testFancyPantsDisabled()
0049 {
0050     Markdown::Converter converter;
0051     converter.setFancyPantsEnabled(false);
0052     QTextDocument *document = converter.convert(QStringLiteral(KDESRCDIR "data/imageSizes.md"));
0053 
0054     QTextFrame::iterator secondFrame = ++(document->rootFrame()->begin());
0055     QVERIFY(secondFrame.currentBlock().text().startsWith(QStringLiteral("(c)")));
0056 }
0057 
0058 void MarkdownTest::testImageSizes()
0059 {
0060     Markdown::Converter converter;
0061     QTextDocument *document = converter.convert(QStringLiteral(KDESRCDIR "data/imageSizes.md"));
0062 
0063     QTextFrame *parent = document->rootFrame();
0064 
0065     QVector<QTextImageFormat> images;
0066     findImages(parent, images);
0067 
0068     QCOMPARE(images.size(), 17);
0069 
0070     qreal expectedSizes[][2] = {// width, height
0071                                 // small image
0072                                 {412, 349},
0073                                 {100, 84.70873786407767},
0074                                 {118.0515759312321, 100},
0075                                 {100, 100},
0076                                 {890, 753.9077669902913},
0077                                 {890, 890},
0078                                 // wide image
0079                                 {890, 178},
0080                                 {100, 20},
0081                                 {500, 100},
0082                                 {100, 100},
0083                                 {890, 178},
0084                                 {890, 890},
0085                                 // tall image
0086                                 {300, 1500},
0087                                 {100, 500},
0088                                 {20, 100},
0089                                 {100, 100},
0090                                 {890, 890}};
0091 
0092     for (int i = 0; i < images.size(); i++) {
0093         QCOMPARE(images[i].width(), expectedSizes[i][0]);
0094         QCOMPARE(images[i].height(), expectedSizes[i][1]);
0095     }
0096 }
0097 
0098 void MarkdownTest::findImages(QTextFrame *parent, QVector<QTextImageFormat> &images)
0099 {
0100     for (QTextFrame::iterator it = parent->begin(); !it.atEnd(); ++it) {
0101         QTextFrame *textFrame = it.currentFrame();
0102         const QTextBlock textBlock = it.currentBlock();
0103 
0104         if (textFrame) {
0105             findImages(textFrame, images);
0106         } else if (textBlock.isValid()) {
0107             findImages(textBlock, images);
0108         }
0109     }
0110 }
0111 
0112 void MarkdownTest::findImages(const QTextBlock &parent, QVector<QTextImageFormat> &images)
0113 {
0114     for (QTextBlock::iterator it = parent.begin(); !it.atEnd(); ++it) {
0115         const QTextFragment textFragment = it.fragment();
0116         if (textFragment.isValid()) {
0117             const QTextCharFormat textCharFormat = textFragment.charFormat();
0118             if (textCharFormat.isImageFormat()) {
0119                 images.append(textCharFormat.toImageFormat());
0120             }
0121         }
0122     }
0123 }
0124 
0125 void MarkdownTest::testSpecialCharsInImageFileName()
0126 {
0127     Markdown::Converter converter;
0128     QTextDocument *document = converter.convert(QStringLiteral(KDESRCDIR "data/imageUrlsWithSpecialChars.md"));
0129 
0130     QTextFrame *parent = document->rootFrame();
0131 
0132     QVector<QTextImageFormat> images;
0133     findImages(parent, images);
0134 
0135     QCOMPARE(images.size(), 1);
0136     QVERIFY(images[0].name().endsWith(QStringLiteral("kartöffelchen.jpg")));
0137     QVERIFY(!images[0].name().contains(QStringLiteral("kart%C3%B6ffelchen.jpg")));
0138 }
0139 
0140 QTEST_MAIN(MarkdownTest)
0141 #include "markdowntest.moc"