File indexing completed on 2024-09-22 04:49:58

0001 /*
0002   SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-only
0005 */
0006 
0007 #include "filteractionwithurltest.h"
0008 #include "../filteractions/filteractionwithurl.h"
0009 #include <KLineEdit>
0010 #include <KUrlRequester>
0011 #include <QTest>
0012 
0013 class TestFilterActionWithUrl : public MailCommon::FilterActionWithUrl
0014 {
0015 public:
0016     TestFilterActionWithUrl()
0017         : MailCommon::FilterActionWithUrl(QStringLiteral("test"), QStringLiteral("label"))
0018     {
0019     }
0020 
0021     FilterAction::ReturnCode process(MailCommon::ItemContext &, bool) const override
0022     {
0023         return GoOn;
0024     }
0025 
0026     MailCommon::SearchRule::RequiredPart requiredPart() const override
0027     {
0028         return MailCommon::SearchRule::CompleteMessage;
0029     }
0030 };
0031 
0032 FilterActionWithUrlTest::FilterActionWithUrlTest(QObject *parent)
0033     : QObject(parent)
0034 {
0035 }
0036 
0037 FilterActionWithUrlTest::~FilterActionWithUrlTest() = default;
0038 
0039 void FilterActionWithUrlTest::shouldHaveDefaultValue()
0040 {
0041     TestFilterActionWithUrl filter;
0042     QWidget *w = filter.createParamWidget(nullptr);
0043     auto requester = w->findChild<KUrlRequester *>(QStringLiteral("requester"));
0044     QVERIFY(requester);
0045     auto toolButton = w->findChild<QToolButton *>(QStringLiteral("helpbutton"));
0046     QVERIFY(toolButton);
0047 }
0048 
0049 void FilterActionWithUrlTest::shouldClearWidget()
0050 {
0051     TestFilterActionWithUrl filter;
0052     QWidget *w = filter.createParamWidget(nullptr);
0053     auto requester = w->findChild<KUrlRequester *>(QStringLiteral("requester"));
0054     requester->setUrl(QUrl::fromLocalFile(QStringLiteral("/foo/bla")));
0055     QVERIFY(!requester->url().isEmpty());
0056     filter.clearParamWidget(w);
0057     QVERIFY(requester->url().isEmpty());
0058 }
0059 
0060 void FilterActionWithUrlTest::shouldAddValue()
0061 {
0062     TestFilterActionWithUrl filter;
0063     QWidget *w = filter.createParamWidget(nullptr);
0064     auto requester = w->findChild<KUrlRequester *>(QStringLiteral("requester"));
0065     filter.argsFromString(QStringLiteral("/foo"));
0066     filter.setParamWidgetValue(w);
0067     QCOMPARE(requester->lineEdit()->text(), QStringLiteral("/foo"));
0068 }
0069 
0070 void FilterActionWithUrlTest::shouldApplyValue()
0071 {
0072     TestFilterActionWithUrl filter;
0073     QWidget *w = filter.createParamWidget(nullptr);
0074     filter.argsFromString(QStringLiteral("foo"));
0075     filter.setParamWidgetValue(w);
0076     filter.applyParamWidgetValue(w);
0077     QCOMPARE(filter.argsAsString(), QStringLiteral("foo"));
0078 }
0079 
0080 void FilterActionWithUrlTest::shouldTestUrl_data()
0081 {
0082     QTest::addColumn<QString>("urlstr");
0083     QTest::addColumn<QString>("output");
0084     QTest::newRow("fullpath") << QStringLiteral("/usr/bin/ls") << QStringLiteral("/usr/bin/ls");
0085     QTest::newRow("local") << QStringLiteral("ls") << QStringLiteral("ls");
0086     QTest::newRow("localwithargument") << QStringLiteral("ls -l") << QStringLiteral("ls -l");
0087     QTest::newRow("fullpathwithargument") << QStringLiteral("/usr/bin/ls -l") << QStringLiteral("/usr/bin/ls -l");
0088     QTest::newRow("url") << QStringLiteral("file:///usr/bin/ls -l") << QStringLiteral("file:///usr/bin/ls -l");
0089     QTest::newRow("url2") << QStringLiteral("/usr/bin/ls -l") << QStringLiteral("/usr/bin/ls -l");
0090 }
0091 
0092 void FilterActionWithUrlTest::shouldTestUrl()
0093 {
0094     QFETCH(QString, urlstr);
0095     QFETCH(QString, output);
0096     TestFilterActionWithUrl filter;
0097     QWidget *w = filter.createParamWidget(nullptr);
0098     filter.argsFromString(urlstr);
0099     filter.setParamWidgetValue(w);
0100     filter.applyParamWidgetValue(w);
0101     QCOMPARE(filter.argsAsString(), output);
0102 }
0103 
0104 QTEST_MAIN(FilterActionWithUrlTest)
0105 
0106 #include "moc_filteractionwithurltest.cpp"