File indexing completed on 2025-01-19 04:56:59

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Kevin Ottens <ervin@kde.org>
0003  SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 
0007 #include <testlib/qtest_gui_zanshin.h>
0008 
0009 #include <QCheckBox>
0010 #include <QComboBox>
0011 #include <QLineEdit>
0012 #include <QToolButton>
0013 
0014 #include <KLocalizedString>
0015 
0016 #include "widgets/filterwidget.h"
0017 
0018 #include "presentation/taskfilterproxymodel.h"
0019 
0020 class FilterWidgetTest : public QObject
0021 {
0022     Q_OBJECT
0023 private slots:
0024     void shouldHaveDefaultState()
0025     {
0026         Widgets::FilterWidget filter;
0027 
0028         QVERIFY(filter.proxyModel());
0029         QVERIFY(!filter.proxyModel()->sourceModel());
0030         QCOMPARE(filter.proxyModel()->filterRegularExpression(), QRegularExpression());
0031         QCOMPARE(filter.proxyModel()->sortOrder(), Qt::AscendingOrder);
0032         QCOMPARE(filter.proxyModel()->sortType(), Presentation::TaskFilterProxyModel::TitleSort);
0033 
0034         QLineEdit *filterEdit = filter.findChild<QLineEdit*>(QStringLiteral("filterEdit"));
0035         QVERIFY(filterEdit);
0036         QVERIFY(filterEdit->isVisibleTo(&filter));
0037         QVERIFY(filterEdit->text().isEmpty());
0038         QCOMPARE(filterEdit->placeholderText(), i18n("Filter..."));
0039 
0040         auto extensionButton = filter.findChild<QToolButton*>(QStringLiteral("extensionButton"));
0041         QVERIFY(extensionButton);
0042         QVERIFY(extensionButton->isVisibleTo(&filter));
0043         QVERIFY(!extensionButton->isChecked());
0044         QVERIFY(extensionButton->autoRaise());
0045 
0046         QComboBox *sortTypeCombo = filter.findChild<QComboBox*>(QStringLiteral("sortTypeCombo"));
0047         QVERIFY(sortTypeCombo);
0048         QVERIFY(!sortTypeCombo->isVisibleTo(&filter));
0049         QCOMPARE(sortTypeCombo->currentIndex(), 0);
0050 
0051         auto ascendingButton = filter.findChild<QToolButton*>(QStringLiteral("ascendingButton"));
0052         QVERIFY(ascendingButton);
0053         QVERIFY(!ascendingButton->isVisibleTo(&filter));
0054         QVERIFY(ascendingButton->isChecked());
0055         QVERIFY(ascendingButton->autoRaise());
0056 
0057         auto descendingButton = filter.findChild<QToolButton*>(QStringLiteral("descendingButton"));
0058         QVERIFY(descendingButton);
0059         QVERIFY(!descendingButton->isVisibleTo(&filter));
0060         QVERIFY(!descendingButton->isChecked());
0061         QVERIFY(descendingButton->autoRaise());
0062     }
0063 
0064     void shouldChangeAppliedFilter()
0065     {
0066         // GIVEN
0067         Widgets::FilterWidget filter;
0068 
0069         QLineEdit *filterEdit = filter.findChild<QLineEdit*>(QStringLiteral("filterEdit"));
0070         QVERIFY(filterEdit);
0071 
0072         // WHEN
0073         QTest::keyClicks(filterEdit, QStringLiteral("find me"));
0074 
0075         // THEN
0076         QCOMPARE(filter.proxyModel()->filterRegularExpression().pattern(), QStringLiteral("find\\ me"));
0077     }
0078 
0079     void shouldClearFilter()
0080     {
0081         // GIVEN
0082         Widgets::FilterWidget filter;
0083 
0084         QLineEdit *filterEdit = filter.findChild<QLineEdit*>(QStringLiteral("filterEdit"));
0085         QVERIFY(filterEdit);
0086         filterEdit->setText("Foo");
0087 
0088         // WHEN
0089         filter.clear();
0090 
0091         // THEN
0092         QVERIFY(filterEdit->text().isEmpty());
0093         QVERIFY(filter.proxyModel()->filterRegularExpression().pattern().isEmpty());
0094     }
0095 
0096     void shouldShowExtension()
0097     {
0098         // GIVEN
0099         Widgets::FilterWidget filter;
0100 
0101         QAbstractButton *extensionButton = filter.findChild<QAbstractButton*>(QStringLiteral("extensionButton"));
0102         QVERIFY(extensionButton);
0103 
0104         QComboBox *sortTypeCombo = filter.findChild<QComboBox*>(QStringLiteral("sortTypeCombo"));
0105         QVERIFY(sortTypeCombo);
0106 
0107         QAbstractButton *ascendingButton = filter.findChild<QAbstractButton*>(QStringLiteral("ascendingButton"));
0108         QVERIFY(ascendingButton);
0109 
0110         QAbstractButton *descendingButton = filter.findChild<QAbstractButton*>(QStringLiteral("descendingButton"));
0111         QVERIFY(descendingButton);
0112 
0113         // WHEN
0114         extensionButton->click();
0115 
0116         // THEN
0117         QVERIFY(extensionButton->isChecked());
0118         QVERIFY(sortTypeCombo->isVisibleTo(&filter));
0119         QVERIFY(descendingButton->isVisibleTo(&filter));
0120         QVERIFY(descendingButton->isVisibleTo(&filter));
0121 
0122         // WHEN
0123         extensionButton->click();
0124 
0125         // THEN
0126         QVERIFY(!extensionButton->isChecked());
0127         QVERIFY(!sortTypeCombo->isVisibleTo(&filter));
0128         QVERIFY(!descendingButton->isVisibleTo(&filter));
0129         QVERIFY(!descendingButton->isVisibleTo(&filter));
0130     }
0131 
0132     void shouldChangeSortType()
0133     {
0134         // GIVEN
0135         Widgets::FilterWidget filter;
0136 
0137         QComboBox *sortTypeCombo = filter.findChild<QComboBox*>(QStringLiteral("sortTypeCombo"));
0138         QVERIFY(sortTypeCombo);
0139 
0140         // WHEN
0141         sortTypeCombo->setCurrentIndex(1);
0142 
0143         // THEN
0144         QCOMPARE(filter.proxyModel()->sortType(), Presentation::TaskFilterProxyModel::DateSort);
0145 
0146         // WHEN
0147         sortTypeCombo->setCurrentIndex(0);
0148 
0149         // THEN
0150         QCOMPARE(filter.proxyModel()->sortType(), Presentation::TaskFilterProxyModel::TitleSort);
0151     }
0152 
0153     void shouldChangeSortOrder()
0154     {
0155         // GIVEN
0156         Widgets::FilterWidget filter;
0157 
0158         QAbstractButton *ascendingButton = filter.findChild<QAbstractButton*>(QStringLiteral("ascendingButton"));
0159         QVERIFY(ascendingButton);
0160 
0161         QAbstractButton *descendingButton = filter.findChild<QAbstractButton*>(QStringLiteral("descendingButton"));
0162         QVERIFY(descendingButton);
0163 
0164         // WHEN
0165         descendingButton->click();
0166 
0167         // THEN
0168         QVERIFY(!ascendingButton->isChecked());
0169         QVERIFY(descendingButton->isChecked());
0170         QCOMPARE(filter.proxyModel()->sortOrder(), Qt::DescendingOrder);
0171 
0172         // WHEN
0173         ascendingButton->click();
0174 
0175         // THEN
0176         QVERIFY(ascendingButton->isChecked());
0177         QVERIFY(!descendingButton->isChecked());
0178         QCOMPARE(filter.proxyModel()->sortOrder(), Qt::AscendingOrder);
0179     }
0180 
0181     void shouldShowFutureTasks()
0182     {
0183         // GIVEN
0184         Widgets::FilterWidget filter;
0185         filter.setShowFutureTasks(false);
0186 
0187         // THEN
0188         QVERIFY(!filter.proxyModel()->showFutureTasks());
0189 
0190         // WHEN
0191         filter.setShowFutureTasks(true);
0192 
0193         // THEN
0194         QVERIFY(filter.proxyModel()->showFutureTasks());
0195     }
0196 
0197     void shouldShowDoneTasks()
0198     {
0199         // GIVEN
0200         Widgets::FilterWidget filter;
0201         filter.setShowDoneTasks(false);
0202 
0203         // THEN
0204         QVERIFY(!filter.proxyModel()->showDoneTasks());
0205 
0206         // WHEN
0207         filter.setShowDoneTasks(true);
0208 
0209         // THEN
0210         QVERIFY(filter.proxyModel()->showDoneTasks());
0211     }
0212 };
0213 
0214 ZANSHIN_TEST_MAIN(FilterWidgetTest)
0215 
0216 #include "filterwidgettest.moc"