File indexing completed on 2024-06-02 05:26:28

0001 /*
0002    SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "richtextcomposercontrolertest.h"
0008 #include "../richtextcomposer.h"
0009 #include "../richtextcomposercontroler.h"
0010 #include <KActionCollection>
0011 #include <QStandardPaths>
0012 #include <QTest>
0013 
0014 RichTextComposerControlerTest::RichTextComposerControlerTest(QObject *parent)
0015     : QObject(parent)
0016 {
0017     QStandardPaths::setTestModeEnabled(true);
0018 }
0019 
0020 RichTextComposerControlerTest::~RichTextComposerControlerTest() = default;
0021 
0022 void RichTextComposerControlerTest::shouldAlignLeft()
0023 {
0024     KPIMTextEdit::RichTextComposer composer;
0025     auto actionCollection = new KActionCollection(&composer);
0026     composer.createActions(actionCollection);
0027     KPIMTextEdit::RichTextComposerControler controller(&composer);
0028     composer.show();
0029     QVERIFY(QTest::qWaitForWindowExposed(&composer));
0030     controller.alignLeft();
0031     // QVERIFY(controller.richTextComposer()->hasFocus());
0032     QCOMPARE(controller.richTextComposer()->alignment(), Qt::AlignLeft);
0033     QVERIFY(controller.richTextComposer()->acceptRichText());
0034 }
0035 
0036 void RichTextComposerControlerTest::shouldAlignRight()
0037 {
0038     KPIMTextEdit::RichTextComposer composer;
0039     auto actionCollection = new KActionCollection(&composer);
0040     composer.createActions(actionCollection);
0041     KPIMTextEdit::RichTextComposerControler controller(&composer);
0042     composer.show();
0043     QVERIFY(QTest::qWaitForWindowExposed(&composer));
0044     controller.alignRight();
0045     QVERIFY(controller.richTextComposer()->hasFocus());
0046     QCOMPARE(controller.richTextComposer()->alignment(), Qt::AlignRight);
0047     QVERIFY(controller.richTextComposer()->acceptRichText());
0048 }
0049 
0050 void RichTextComposerControlerTest::shouldAlignJustify()
0051 {
0052     KPIMTextEdit::RichTextComposer composer;
0053     auto actionCollection = new KActionCollection(&composer);
0054     composer.createActions(actionCollection);
0055     KPIMTextEdit::RichTextComposerControler controller(&composer);
0056     composer.show();
0057     QVERIFY(QTest::qWaitForWindowExposed(&composer));
0058     controller.alignJustify();
0059     // QVERIFY(controller.richTextComposer()->hasFocus());
0060     QCOMPARE(controller.richTextComposer()->alignment(), Qt::AlignJustify);
0061     QVERIFY(controller.richTextComposer()->acceptRichText());
0062 }
0063 
0064 void RichTextComposerControlerTest::shouldAlignCenter()
0065 {
0066     KPIMTextEdit::RichTextComposer composer;
0067     auto actionCollection = new KActionCollection(&composer);
0068     composer.createActions(actionCollection);
0069     KPIMTextEdit::RichTextComposerControler controller(&composer);
0070     composer.show();
0071     QVERIFY(QTest::qWaitForWindowExposed(&composer));
0072     controller.alignCenter();
0073     // QVERIFY(controller.richTextComposer()->hasFocus());
0074     QCOMPARE(controller.richTextComposer()->alignment(), Qt::AlignHCenter);
0075     QVERIFY(controller.richTextComposer()->acceptRichText());
0076 }
0077 
0078 void RichTextComposerControlerTest::shouldHaveDefaultValue()
0079 {
0080     KPIMTextEdit::RichTextComposer composer;
0081     KPIMTextEdit::RichTextComposerControler controller(&composer);
0082     QVERIFY(!controller.painterActive());
0083     QVERIFY(!controller.richTextComposer()->acceptRichText());
0084 }
0085 
0086 void RichTextComposerControlerTest::shouldBoldText()
0087 {
0088     KPIMTextEdit::RichTextComposer composer;
0089     auto actionCollection = new KActionCollection(&composer);
0090     composer.createActions(actionCollection);
0091     KPIMTextEdit::RichTextComposerControler controller(&composer);
0092     composer.show();
0093     QVERIFY(QTest::qWaitForWindowExposed(&composer));
0094     controller.setTextBold(true);
0095     // QVERIFY(controller.richTextComposer()->hasFocus());
0096     QVERIFY(controller.richTextComposer()->acceptRichText());
0097     // TODO text format.
0098 }
0099 
0100 void RichTextComposerControlerTest::shouldItalicText()
0101 {
0102     KPIMTextEdit::RichTextComposer composer;
0103     auto actionCollection = new KActionCollection(&composer);
0104     composer.createActions(actionCollection);
0105     KPIMTextEdit::RichTextComposerControler controller(&composer);
0106     composer.show();
0107     QVERIFY(QTest::qWaitForWindowExposed(&composer));
0108     controller.setTextItalic(true);
0109     // QVERIFY(controller.richTextComposer()->hasFocus());
0110     QVERIFY(controller.richTextComposer()->acceptRichText());
0111     // TODO text format.
0112 }
0113 
0114 void RichTextComposerControlerTest::shouldTextUnderline()
0115 {
0116     KPIMTextEdit::RichTextComposer composer;
0117     auto actionCollection = new KActionCollection(&composer);
0118     composer.createActions(actionCollection);
0119     KPIMTextEdit::RichTextComposerControler controller(&composer);
0120     composer.show();
0121     QVERIFY(QTest::qWaitForWindowExposed(&composer));
0122     controller.setTextUnderline(true);
0123     // QVERIFY(controller.richTextComposer()->hasFocus());
0124     QVERIFY(controller.richTextComposer()->acceptRichText());
0125     // TODO text format.
0126 }
0127 
0128 void RichTextComposerControlerTest::shouldTextStrikeOut()
0129 {
0130     KPIMTextEdit::RichTextComposer composer;
0131     auto actionCollection = new KActionCollection(&composer);
0132     composer.createActions(actionCollection);
0133     KPIMTextEdit::RichTextComposerControler controller(&composer);
0134     composer.show();
0135     QVERIFY(QTest::qWaitForWindowExposed(&composer));
0136     controller.setTextStrikeOut(true);
0137     // QVERIFY(controller.richTextComposer()->hasFocus());
0138     QVERIFY(controller.richTextComposer()->acceptRichText());
0139     // TODO text format.
0140 }
0141 
0142 void RichTextComposerControlerTest::shouldFontFamily()
0143 {
0144     KPIMTextEdit::RichTextComposer composer;
0145     auto actionCollection = new KActionCollection(&composer);
0146     composer.createActions(actionCollection);
0147     KPIMTextEdit::RichTextComposerControler controller(&composer);
0148     composer.show();
0149     QVERIFY(QTest::qWaitForWindowExposed(&composer));
0150     // TODO
0151 }
0152 
0153 void RichTextComposerControlerTest::shouldFontSize()
0154 {
0155     KPIMTextEdit::RichTextComposer composer;
0156     auto actionCollection = new KActionCollection(&composer);
0157     composer.createActions(actionCollection);
0158     KPIMTextEdit::RichTextComposerControler controller(&composer);
0159     composer.show();
0160     QVERIFY(QTest::qWaitForWindowExposed(&composer));
0161     // TODO
0162 }
0163 
0164 void RichTextComposerControlerTest::shouldFont()
0165 {
0166     KPIMTextEdit::RichTextComposer composer;
0167     auto actionCollection = new KActionCollection(&composer);
0168     composer.createActions(actionCollection);
0169     KPIMTextEdit::RichTextComposerControler controller(&composer);
0170     composer.show();
0171     QVERIFY(QTest::qWaitForWindowExposed(&composer));
0172     // TODO
0173 }
0174 
0175 void RichTextComposerControlerTest::shouldTextSuperScript()
0176 {
0177     KPIMTextEdit::RichTextComposer composer;
0178     auto actionCollection = new KActionCollection(&composer);
0179     composer.createActions(actionCollection);
0180     KPIMTextEdit::RichTextComposerControler controller(&composer);
0181     composer.show();
0182     QVERIFY(QTest::qWaitForWindowExposed(&composer));
0183     controller.setTextSuperScript(true);
0184     QVERIFY(controller.richTextComposer()->hasFocus());
0185     QVERIFY(controller.richTextComposer()->acceptRichText());
0186     // TODO
0187 }
0188 
0189 void RichTextComposerControlerTest::shouldTextSubScript()
0190 {
0191     KPIMTextEdit::RichTextComposer composer;
0192     auto actionCollection = new KActionCollection(&composer);
0193     composer.createActions(actionCollection);
0194     KPIMTextEdit::RichTextComposerControler controller(&composer);
0195     composer.show();
0196     QVERIFY(QTest::qWaitForWindowExposed(&composer));
0197     controller.setTextSubScript(true);
0198     QVERIFY(controller.richTextComposer()->hasFocus());
0199     QVERIFY(controller.richTextComposer()->acceptRichText());
0200 
0201     // TODO
0202 }
0203 
0204 void RichTextComposerControlerTest::shouldRemoveQuote_data()
0205 {
0206     QTest::addColumn<QString>("input");
0207     QTest::addColumn<QString>("output");
0208     QTest::newRow("removequote1") << QStringLiteral(">foo\n>bla\n>blo\n") << QStringLiteral("foo\nbla\nblo\n");
0209     QTest::newRow("withoutquote") << QStringLiteral("bli\nblo\bla\n") << QStringLiteral("bli\nblo\bla\n");
0210     QTest::newRow("removequote2") << QStringLiteral(">foo\n>bla\n>blo") << QStringLiteral("foo\nbla\nblo");
0211     QTest::newRow("empty") << QString() << QString();
0212     // Bug David, new line with quote
0213     QTest::newRow("removequotewithnewline") << QStringLiteral(">foo\n>\n>bla\n>blo\n") << QStringLiteral("foo\n\nbla\nblo\n");
0214 
0215     QTest::newRow("removequote2") << QStringLiteral(">foo\n\nbla\n>blo\nbli") << QStringLiteral("foo\n\nbla\nblo\nbli");
0216     QTest::newRow("removequote2withspace") << QStringLiteral("> foo\n\nbla\n> blo\nbli") << QStringLiteral("foo\n\nbla\nblo\nbli");
0217 
0218     QTest::newRow("twoquotes") << QStringLiteral(">>foo\n\nbla\n>blo\nbli") << QStringLiteral(">foo\n\nbla\nblo\nbli");
0219 
0220     QTest::newRow("quoteintext") << QStringLiteral("foo>>\n\nbla\n>blo\nbli") << QStringLiteral("foo>>\n\nbla\nblo\nbli");
0221     QTest::newRow("emptyline") << QStringLiteral("foo>>\n\n\nbla\n>blo\nbli") << QStringLiteral("foo>>\n\n\nbla\nblo\nbli");
0222 }
0223 
0224 void RichTextComposerControlerTest::shouldRemoveQuote()
0225 {
0226     QFETCH(QString, input);
0227     QFETCH(QString, output);
0228     KPIMTextEdit::RichTextComposer composer;
0229     auto actionCollection = new KActionCollection(&composer);
0230     composer.createActions(actionCollection);
0231     KPIMTextEdit::RichTextComposerControler controller(&composer);
0232     composer.show();
0233     QVERIFY(QTest::qWaitForWindowExposed(&composer));
0234 
0235     composer.setPlainText(input);
0236     controller.slotRemoveQuotes();
0237     QCOMPARE(composer.toPlainText(), output);
0238 }
0239 
0240 void RichTextComposerControlerTest::shouldRemoveQuoteWithSpecificQuote_data()
0241 {
0242     QTest::addColumn<QString>("input");
0243     QTest::addColumn<QString>("output");
0244     QTest::addColumn<QString>("quote");
0245     QTest::newRow("empty") << QString() << QString() << QString();
0246     QTest::newRow("empty1") << QString() << QString() << QStringLiteral("QT");
0247     QTest::newRow("removequote1") << QStringLiteral(">foo\n>bla\n>blo\n") << QStringLiteral(">foo\n>bla\n>blo\n") << QStringLiteral("QT");
0248     QTest::newRow("removequote2") << QStringLiteral("QTfoo\nQTbla\n>blo\n") << QStringLiteral("foo\nbla\n>blo\n") << QStringLiteral("QT");
0249     QTest::newRow("removequote3") << QStringLiteral("|foo\n|bla\n>blo\n") << QStringLiteral("foo\nbla\n>blo\n") << QStringLiteral("|");
0250 }
0251 
0252 void RichTextComposerControlerTest::shouldRemoveQuoteWithSpecificQuote()
0253 {
0254     QFETCH(QString, input);
0255     QFETCH(QString, output);
0256     QFETCH(QString, quote);
0257     KPIMTextEdit::RichTextComposer composer;
0258     composer.setQuotePrefixName(quote);
0259     auto actionCollection = new KActionCollection(&composer);
0260     composer.createActions(actionCollection);
0261     KPIMTextEdit::RichTextComposerControler controller(&composer);
0262     composer.show();
0263     QVERIFY(QTest::qWaitForWindowExposed(&composer));
0264 
0265     composer.setPlainText(input);
0266     controller.slotRemoveQuotes();
0267     QCOMPARE(composer.toPlainText(), output);
0268 }
0269 
0270 void RichTextComposerControlerTest::shouldAddQuote_data()
0271 {
0272     //    QTest::addColumn<QString>("input");
0273     //    QTest::addColumn<QString>("output");
0274     //    QTest::addColumn<QString>("quote");
0275     //    QTest::newRow("empty") << QString() << QString() << QString();
0276     //    QTest::newRow("empty1") << QString() << QString() << QStringLiteral("QT");
0277     //    QTest::newRow("removequote1") << QStringLiteral(">foo\n>bla\n>blo\n") << QStringLiteral("QT>foo\nQT>bla\nQT>blo\n") << QStringLiteral("QT");
0278     //    QTest::newRow("removequote2") << QStringLiteral("QTfoo\nQTbla\n>blo\n") << QStringLiteral("QTQTfoo\nQTQTbla\n>QTblo\n") << QStringLiteral("QT");
0279     //    QTest::newRow("removequote3") << QStringLiteral("|foo\n|bla\n>blo\n") << QStringLiteral("||foo\n||bla\n|>blo\n") << QStringLiteral("|");
0280 }
0281 
0282 void RichTextComposerControlerTest::shouldAddQuote()
0283 {
0284     //    QFETCH(QString, input);
0285     //    QFETCH(QString, output);
0286     //    QFETCH(QString, quote);
0287     //    KPIMTextEdit::RichTextComposer composer;
0288     //    composer.setQuotePrefixName(quote);
0289     //    KActionCollection *actionCollection = new KActionCollection(&composer);
0290     //    composer.createActions(actionCollection);
0291     //    KPIMTextEdit::RichTextComposerControler controller(&composer);
0292     //    composer.show();
0293     //    QVERIFY(QTest::qWaitForWindowExposed(&composer));
0294 
0295     //    composer.setPlainText(input);
0296     //    controller.slotAddQuotes();
0297     //    QCOMPARE(composer.toPlainText(), QString(output + QChar::ParagraphSeparator));
0298 }
0299 
0300 QTEST_MAIN(RichTextComposerControlerTest)
0301 
0302 #include "moc_richtextcomposercontrolertest.cpp"