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

0001 /*
0002  * SPDX-FileCopyrightText: 2015 Franck Arrecot <franck.arrecot@gmail.com>
0003  SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 
0007 
0008 #include <testlib/qtest_gui_zanshin.h>
0009 
0010 #include <QHeaderView>
0011 #include <QLabel>
0012 #include <QSignalSpy>
0013 #include <QStandardItem>
0014 #include <QStandardItemModel>
0015 #include <QTestEventList>
0016 #include <QTimer>
0017 #include <QTreeView>
0018 #include <QList>
0019 
0020 #include "widgets/quickselectdialog.h"
0021 
0022 Q_DECLARE_METATYPE(QStandardItemModel*)
0023 
0024 class QuickSelectDialogTest : public QObject
0025 {
0026     Q_OBJECT
0027 private:
0028     template <typename T>
0029     T *widgetFromQuickSelectDialog(Widgets::QuickSelectDialog *dlg)
0030     {
0031         auto widgets = dlg->findChildren<T*>();
0032         Q_ASSERT(widgets.size() == 1);
0033         return widgets.first();
0034     }
0035 
0036     QModelIndex modelIndexFromNameHelper(QAbstractItemModel *model, const QString &name, const QModelIndex &root = QModelIndex())
0037     {
0038         for (int i = 0; i < model->rowCount(root); i++)
0039         {
0040             auto idx = model->index(i, 0, root);
0041             if (idx.data().toString() == name) {
0042                 return idx;
0043             } else if (model->rowCount(idx) > 0) {
0044                 auto idxChild = modelIndexFromNameHelper(model, name, idx);
0045                 if (idxChild.isValid()) {
0046                     return idxChild;
0047                 }
0048             }
0049         }
0050         return {};
0051     }
0052 
0053     QAbstractItemModel *prepareQuickSelectDialogData()
0054     {
0055         // GIVEN
0056 
0057         // a model with items
0058         auto model = new QStandardItemModel(this);
0059         auto inbox = new QStandardItem(QStringLiteral("Inbox"));
0060         auto workday = new QStandardItem(QStringLiteral("Workday"));
0061         auto projects= new QStandardItem(QStringLiteral("Projects"));
0062         auto contexts = new QStandardItem(QStringLiteral("Contexts"));
0063 
0064         auto structureNodeFlags = Qt::NoItemFlags;
0065         projects->setFlags(structureNodeFlags);
0066         contexts->setFlags(structureNodeFlags);
0067 
0068         // with items children
0069         auto projectChildOne = new QStandardItem(QStringLiteral("ProjectOne"));
0070         auto projectChildTwo = new QStandardItem(QStringLiteral("ProjectTwo"));
0071         projects->setChild(0, 0, projectChildOne);
0072         projects->setChild(1, 0,projectChildTwo);
0073 
0074         auto contextChildOne = new QStandardItem(QStringLiteral("ContextOne"));
0075         auto contextChildTwo = new QStandardItem(QStringLiteral("ContextTwo"));
0076         contexts->setChild(0, 0, contextChildOne);
0077         contexts->setChild(1, 0, contextChildTwo);
0078 
0079         auto items = QList<QStandardItem*>();
0080         items << inbox << workday << projects << contexts;
0081 
0082         for (int i = 0; i < items.size(); ++i)
0083             model->setItem(i, items.at(i));
0084 
0085         return model;
0086     }
0087 
0088 private slots:
0089     void shouldHaveDefaultState()
0090     {
0091         Widgets::QuickSelectDialog dlg;
0092 
0093         auto pagesView = dlg.findChild<QTreeView*>(QStringLiteral("pagesView"));
0094         QVERIFY(pagesView);
0095         QVERIFY(pagesView->isVisibleTo(&dlg));
0096         QVERIFY(!pagesView->header()->isVisibleTo(&dlg));
0097     }
0098 
0099     void shouldCloseDialogOnOk()
0100     {
0101         // GIVEN
0102         auto model = prepareQuickSelectDialogData();
0103         Widgets::QuickSelectDialog dlg;
0104         dlg.setModel(model);
0105         auto treeView = widgetFromQuickSelectDialog<QTreeView>(&dlg);
0106         dlg.show();
0107 
0108         // WHEN
0109         QTest::keyClick(treeView, Qt::Key_Enter);
0110 
0111         //THEN
0112         QCOMPARE(dlg.result(), static_cast<int>(QDialog::Accepted));
0113     }
0114 
0115     void shouldDisplayTheProperTreeModel()
0116     {
0117         // GIVEN
0118         auto model = prepareQuickSelectDialogData();
0119         Widgets::QuickSelectDialog dlg;
0120         dlg.setModel(model);
0121 
0122         auto treeView = widgetFromQuickSelectDialog<QTreeView>(&dlg);
0123         auto displayModel = treeView->model();
0124 
0125         // WHEN
0126         dlg.show();
0127 
0128         // THEN
0129         auto inbox = displayModel->index(0,0);
0130         auto workday = displayModel->index(1,0);
0131         auto projects = displayModel->index(2,0);
0132         auto contexts = displayModel->index(3,0);
0133 
0134         QCOMPARE(inbox.data().toString(), QStringLiteral("Inbox"));
0135         QCOMPARE(workday.data().toString(), QStringLiteral("Workday"));
0136         QCOMPARE(projects.data().toString(), QStringLiteral("Projects"));
0137         QCOMPARE(contexts.data().toString(), QStringLiteral("Contexts"));
0138 
0139         QCOMPARE(displayModel->columnCount(), 1);
0140         QCOMPARE(displayModel->rowCount(), 4); // inbox, workday, projects, contexts
0141         QCOMPARE(displayModel->rowCount(inbox), 0); // inbox do not expose any children
0142         QCOMPARE(displayModel->rowCount(workday), 0); // worday do not expose any children
0143         QCOMPARE(displayModel->rowCount(projects), 2); // two children projects created
0144         QCOMPARE(displayModel->rowCount(contexts), 2); // two children contexts created
0145     }
0146 
0147     void shouldFilterTreeOnTyping()
0148     {
0149         // GIVEN
0150         auto model = prepareQuickSelectDialogData();
0151         Widgets::QuickSelectDialog dlg;
0152         dlg.setModel(model);
0153 
0154         auto treeView = widgetFromQuickSelectDialog<QTreeView>(&dlg);
0155         auto displayModel = treeView->model();
0156         auto labelFilter = widgetFromQuickSelectDialog<QLabel>(&dlg);
0157 
0158         // WHEN
0159         QTest::keyClick(treeView, Qt::Key_O, Qt::NoModifier, 20);
0160         QTest::keyClick(treeView, Qt::Key_N, Qt::NoModifier, 20);
0161         QTest::keyClick(treeView, Qt::Key_E, Qt::NoModifier, 20);
0162 
0163         dlg.show();
0164 
0165         // THEN
0166         auto projects = displayModel->index(0,0);
0167         auto contexts = displayModel->index(1,0);
0168 
0169         QCOMPARE(labelFilter->text(), QStringLiteral("Path: one"));
0170         QCOMPARE(displayModel->columnCount(), 1);
0171         QCOMPARE(displayModel->rowCount(), 2);
0172         QCOMPARE(projects.data().toString(), QStringLiteral("Projects"));
0173         QCOMPARE(contexts.data().toString(), QStringLiteral("Contexts"));
0174 
0175         QCOMPARE(displayModel->rowCount(projects), 1);
0176         auto projectsChild = displayModel->index(0, 0, projects);
0177         QCOMPARE(projectsChild.data().toString(), QStringLiteral("ProjectOne"));
0178 
0179         QCOMPARE(displayModel->rowCount(contexts), 1);
0180         auto contextsChild = displayModel->index(0, 0, contexts);
0181         QCOMPARE(contextsChild.data().toString(), QStringLiteral("ContextOne"));
0182     }
0183 
0184     void shouldReturnTheSelectedIndex()
0185     {
0186         // GIVEN
0187         auto model = prepareQuickSelectDialogData();
0188         Widgets::QuickSelectDialog dlg;
0189         dlg.setModel(model);
0190         dlg.show();
0191 
0192         auto selectedItemNames = QList<QString>();
0193         selectedItemNames  << QStringLiteral("Inbox") << QStringLiteral("Workday") << QStringLiteral("ProjectOne") << QStringLiteral("ProjectTwo") <<  QStringLiteral("ContextOne") << QStringLiteral("ContextTwo") << QStringLiteral("TagOne") << QStringLiteral("TagTwo");
0194 
0195         foreach (const auto &itemName, selectedItemNames) {
0196             auto treeview = widgetFromQuickSelectDialog<QTreeView>(&dlg);
0197             auto selectedItem = modelIndexFromNameHelper(treeview->model(), itemName);
0198 
0199             // WHEN
0200             treeview->setCurrentIndex(selectedItem);
0201 
0202             // THEN
0203             QCOMPARE(dlg.selectedIndex().data(), selectedItem.data() );
0204         }
0205     }
0206 };
0207 
0208 ZANSHIN_TEST_MAIN(QuickSelectDialogTest)
0209 
0210 #include "quickselectdialogtest.moc"