File indexing completed on 2025-01-19 04:57:00

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 <QComboBox>
0010 #include <QDialogButtonBox>
0011 #include <QLineEdit>
0012 #include <QLabel>
0013 #include <QPushButton>
0014 #include <QStandardItemModel>
0015 #include <QTimer>
0016 
0017 #include "presentation/querytreemodelbase.h"
0018 
0019 #include "widgets/nameanddatasourcedialog.h"
0020 
0021 class UserInputSimulator : public QObject
0022 {
0023     Q_OBJECT
0024 public:
0025     explicit UserInputSimulator(QObject *parent = nullptr)
0026         : QObject(parent), dialog(nullptr), reject(false), sourceComboIndex(-1) {}
0027 
0028     void exec()
0029     {
0030         Q_ASSERT(dialog);
0031         QTimer::singleShot(50, Qt::PreciseTimer, this, &UserInputSimulator::onTimeout);
0032         dialog->exec();
0033     }
0034 
0035 private slots:
0036     void onTimeout()
0037     {
0038         if (!nameInput.isEmpty()) {
0039             auto nameEdit = dialog->findChild<QLineEdit*>(QStringLiteral("nameEdit"));
0040             QTest::keyClicks(nameEdit, nameInput);
0041         }
0042 
0043         auto sourceCombo = dialog->findChild<QComboBox*>(QStringLiteral("sourceCombo"));
0044         sourceCombo->setCurrentIndex(sourceComboIndex);
0045 
0046         auto buttonBox = dialog->findChild<QDialogButtonBox*>(QStringLiteral("buttonBox"));
0047         if (reject)
0048             buttonBox->button(QDialogButtonBox::Cancel)->click();
0049         else
0050             buttonBox->button(QDialogButtonBox::Ok)->click();
0051     }
0052 
0053 public:
0054     Widgets::NameAndDataSourceDialog *dialog;
0055     bool reject;
0056     QString nameInput;
0057     int sourceComboIndex;
0058 };
0059 
0060 class NameAndDataSourceDialogTest : public QObject
0061 {
0062     Q_OBJECT
0063 private:
0064     QStandardItem *createSourceItem(const QString &name, QStandardItem *parent = nullptr)
0065     {
0066         auto source = Domain::DataSource::Ptr::create();
0067         source->setName(name);
0068 
0069         auto item = new QStandardItem(name);
0070         item->setData(QVariant::fromValue(source), Presentation::QueryTreeModelBase::ObjectRole);
0071         if (parent)
0072             parent->appendRow(item);
0073         return item;
0074     }
0075 
0076     QStandardItem *createTaskSourceItem(const QString &name, QStandardItem *parent = nullptr)
0077     {
0078         auto item = createSourceItem(name, parent);
0079         auto source = item->data(Presentation::QueryTreeModelBase::ObjectRole).value<Domain::DataSource::Ptr>();
0080         source->setContentTypes(Domain::DataSource::Tasks);
0081         return item;
0082     }
0083 
0084     QStandardItem *createDefaultSourceItem(const QString &name, QStandardItem *parent = nullptr)
0085     {
0086         auto item = createTaskSourceItem(name, parent);
0087         item->setData(true, Presentation::QueryTreeModelBase::IsDefaultRole);
0088         return item;
0089     }
0090 
0091     QStandardItemModel *createSourceModel()
0092     {
0093         auto model = new QStandardItemModel(this);
0094 
0095         auto root1 = createSourceItem(QStringLiteral("Root 1"));
0096         createSourceItem(QStringLiteral("Null"), root1);
0097         createTaskSourceItem(QStringLiteral("Task 1.1"), root1);
0098         createTaskSourceItem(QStringLiteral("Task 1.2"), root1);
0099         model->appendRow(root1);
0100 
0101         auto root2 = createSourceItem(QStringLiteral("Root 2"));
0102         createDefaultSourceItem(QStringLiteral("Task 2.1"), root2);
0103         createTaskSourceItem(QStringLiteral("Task 2.2"), root2);
0104         model->appendRow(root2);
0105 
0106         return model;
0107     }
0108 
0109 private slots:
0110     void shouldHaveDefaultState()
0111     {
0112         Widgets::NameAndDataSourceDialog dialog;
0113 
0114         QVERIFY(dialog.name().isEmpty());
0115         QVERIFY(dialog.dataSource().isNull());
0116 
0117         auto nameEdit = dialog.findChild<QLineEdit*>(QStringLiteral("nameEdit"));
0118         QVERIFY(nameEdit);
0119         QVERIFY(nameEdit->isVisibleTo(&dialog));
0120 
0121         auto sourceCombo = dialog.findChild<QComboBox*>(QStringLiteral("sourceCombo"));
0122         QVERIFY(sourceCombo);
0123         QVERIFY(sourceCombo->isVisibleTo(&dialog));
0124 
0125         auto buttonBox = dialog.findChild<QDialogButtonBox*>(QStringLiteral("buttonBox"));
0126         QVERIFY(buttonBox);
0127         QVERIFY(buttonBox->isVisibleTo(&dialog));
0128         QVERIFY(buttonBox->button(QDialogButtonBox::Ok));
0129         QVERIFY(buttonBox->button(QDialogButtonBox::Cancel));
0130     }
0131 
0132     void shouldPositionDefaultProperties()
0133     {
0134         // GIVEN
0135         Widgets::NameAndDataSourceDialog dialog;
0136         auto sourceModel = createSourceModel();
0137         auto sourceCombo = dialog.findChild<QComboBox*>(QStringLiteral("sourceCombo"));
0138 
0139         // WHEN
0140         dialog.setDataSourcesModel(sourceModel);
0141 
0142         // THEN
0143         QCOMPARE(sourceCombo->currentIndex(), 2);
0144         QCOMPARE(sourceCombo->currentText(), QStringLiteral("Root 2 / Task 2.1"));
0145     }
0146 
0147     void shouldProvideUserInputWhenAccepted()
0148     {
0149         // GIVEN
0150         Widgets::NameAndDataSourceDialog dialog;
0151 
0152         auto sourceModel = createSourceModel();
0153         dialog.setDataSourcesModel(sourceModel);
0154 
0155         UserInputSimulator userInput;
0156         userInput.dialog = &dialog;
0157         userInput.sourceComboIndex = 1;
0158         userInput.nameInput = QStringLiteral("name");
0159 
0160         auto expectedSource = sourceModel->item(0)
0161                                          ->child(2)
0162                                          ->data(Presentation::QueryTreeModelBase::ObjectRole)
0163                                          .value<Domain::DataSource::Ptr>();
0164 
0165         // WHEN
0166         userInput.exec();
0167 
0168         // THEN
0169         QCOMPARE(dialog.name(), userInput.nameInput);
0170         QVERIFY(dialog.dataSource());
0171         QCOMPARE(dialog.dataSource(), expectedSource);
0172     }
0173 
0174     void shouldNotProvideUserInputWhenReject()
0175     {
0176         // GIVEN
0177         Widgets::NameAndDataSourceDialog dialog;
0178 
0179         auto sourceModel = createSourceModel();
0180         dialog.setDataSourcesModel(sourceModel);
0181 
0182         UserInputSimulator userInput;
0183         userInput.dialog = &dialog;
0184         userInput.sourceComboIndex = 1;
0185         userInput.nameInput = QStringLiteral("name");
0186         userInput.reject = true;
0187 
0188         // WHEN
0189         userInput.exec();
0190 
0191         // THEN
0192         QCOMPARE(dialog.name(), QString());
0193         QCOMPARE(dialog.dataSource(), Domain::DataSource::Ptr());
0194     }
0195 
0196     void shouldNotAllowEmptyName()
0197     {
0198         // GIVEN
0199         Widgets::NameAndDataSourceDialog dialog;
0200 
0201         auto sourceModel = createSourceModel();
0202         dialog.setDataSourcesModel(sourceModel);
0203 
0204         UserInputSimulator userInput;
0205         userInput.dialog = &dialog;
0206         userInput.sourceComboIndex = 0;
0207         userInput.nameInput = QString();
0208         userInput.reject = true;
0209 
0210         // WHEN
0211         userInput.exec();
0212 
0213         // THEN
0214         auto buttonOk = dialog.findChild<QDialogButtonBox*>(QStringLiteral("buttonBox"))->button(QDialogButtonBox::Ok);
0215         QVERIFY(!buttonOk->isEnabled());
0216         QCOMPARE(dialog.name(), QString());
0217         QCOMPARE(dialog.dataSource(), Domain::DataSource::Ptr());
0218     }
0219 
0220     void shouldNotAllowNoSelectedSource()
0221     {
0222         // GIVEN
0223         Widgets::NameAndDataSourceDialog dialog;
0224 
0225         auto sourceModel = createSourceModel();
0226         dialog.setDataSourcesModel(sourceModel);
0227 
0228         UserInputSimulator userInput;
0229         userInput.dialog = &dialog;
0230         userInput.sourceComboIndex = -1;
0231         userInput.nameInput = QStringLiteral("name");
0232         userInput.reject = true;
0233 
0234         // WHEN
0235         userInput.exec();
0236 
0237         // THEN
0238         auto buttonOk = dialog.findChild<QDialogButtonBox*>(QStringLiteral("buttonBox"))->button(QDialogButtonBox::Ok);
0239         QVERIFY(!buttonOk->isEnabled());
0240         QCOMPARE(dialog.name(), QString());
0241         QCOMPARE(dialog.dataSource(), Domain::DataSource::Ptr());
0242     }
0243 };
0244 
0245 ZANSHIN_TEST_MAIN(NameAndDataSourceDialogTest)
0246 
0247 #include "nameanddatasourcedialogtest.moc"