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 <QAction>
0010 #include <QHeaderView>
0011 #include <QSortFilterProxyModel>
0012 #include <QStandardItemModel>
0013 #include <QStringListModel>
0014 #include <QToolBar>
0015 #include <QTreeView>
0016 
0017 #include "presentation/metatypes.h"
0018 #include "presentation/querytreemodelbase.h"
0019 
0020 #include "widgets/availablesourcesview.h"
0021 #include "widgets/datasourcedelegate.h"
0022 
0023 class AvailableSourcesModelStub : public QObject
0024 {
0025     Q_OBJECT
0026 public:
0027     explicit AvailableSourcesModelStub(QObject *parent = nullptr)
0028         : QObject(parent),
0029           settingsCalled(false)
0030     {
0031     }
0032 
0033 public slots:
0034     void showConfigDialog()
0035     {
0036         settingsCalled = true;
0037     }
0038 
0039     void setDefaultItem(const QModelIndex &index)
0040     {
0041         defaultIndex = index;
0042     }
0043 
0044 public:
0045     bool settingsCalled;
0046     QPersistentModelIndex defaultIndex;
0047 };
0048 
0049 
0050 class AvailableSourcesViewTest : public QObject
0051 {
0052     Q_OBJECT
0053 private slots:
0054     void shouldHaveDefaultState()
0055     {
0056         Widgets::AvailableSourcesView available;
0057 
0058         QVERIFY(!available.model());
0059 
0060         auto sourcesView = available.findChild<QTreeView*>(QStringLiteral("sourcesView"));
0061         QVERIFY(sourcesView);
0062         QVERIFY(sourcesView->isVisibleTo(&available));
0063         QVERIFY(!sourcesView->header()->isVisibleTo(&available));
0064         auto delegate = qobject_cast<Widgets::DataSourceDelegate*>(sourcesView->itemDelegate());
0065         QVERIFY(delegate);
0066 
0067         auto proxy = qobject_cast<QSortFilterProxyModel*>(sourcesView->model());
0068         QVERIFY(proxy);
0069         QVERIFY(proxy->dynamicSortFilter());
0070         QCOMPARE(proxy->sortColumn(), 0);
0071         QCOMPARE(proxy->sortOrder(), Qt::AscendingOrder);
0072 
0073         auto actionBar = available.findChild<QToolBar*>(QStringLiteral("actionBar"));
0074         QVERIFY(actionBar);
0075         QVERIFY(actionBar->isVisibleTo(&available));
0076 
0077         auto defaultAction = available.findChild<QAction*>(QStringLiteral("defaultAction"));
0078         QVERIFY(defaultAction);
0079 
0080         auto settingsAction = available.findChild<QAction*>(QStringLiteral("settingsAction"));
0081         QVERIFY(settingsAction);
0082 
0083         auto actions = available.globalActions();
0084         QCOMPARE(actions.value(QStringLiteral("options_configure")), settingsAction);
0085     }
0086 
0087     void shouldDisplayListFromPageModel()
0088     {
0089         // GIVEN
0090         QStringListModel model(QStringList() << QStringLiteral("A") << QStringLiteral("B") << QStringLiteral("C") );
0091 
0092         QObject stubPagesModel;
0093         stubPagesModel.setProperty("sourceListModel", QVariant::fromValue(static_cast<QAbstractItemModel*>(&model)));
0094 
0095         Widgets::AvailableSourcesView available;
0096         auto sourcesView = available.findChild<QTreeView*>(QStringLiteral("sourcesView"));
0097         QVERIFY(sourcesView);
0098         auto proxy = qobject_cast<QSortFilterProxyModel*>(sourcesView->model());
0099         QVERIFY(proxy);
0100         QVERIFY(!proxy->sourceModel());
0101 
0102         // WHEN
0103         available.setModel(&stubPagesModel);
0104         QTest::qWait(10);
0105 
0106         // THEN
0107         QCOMPARE(proxy->sourceModel(), &model);
0108     }
0109 
0110     void shouldNotCrashWithNullModel()
0111     {
0112         // GIVEN
0113         QStringListModel model(QStringList() << QStringLiteral("A") << QStringLiteral("B") << QStringLiteral("C") );
0114 
0115         QObject stubPagesModel;
0116         stubPagesModel.setProperty("sourceListModel", QVariant::fromValue(static_cast<QAbstractItemModel*>(&model)));
0117 
0118         Widgets::AvailableSourcesView available;
0119         auto sourcesView = available.findChild<QTreeView*>(QStringLiteral("sourcesView"));
0120         QVERIFY(sourcesView);
0121         auto proxy = qobject_cast<QSortFilterProxyModel*>(sourcesView->model());
0122         QVERIFY(proxy);
0123         QVERIFY(!proxy->sourceModel());
0124 
0125         available.setModel(&stubPagesModel);
0126         QTest::qWait(10);
0127 
0128         // WHEN
0129         available.setModel(nullptr);
0130         QTest::qWait(10);
0131 
0132         // THEN
0133         QVERIFY(!available.isEnabled());
0134         QVERIFY(!proxy->sourceModel());
0135     }
0136 
0137     void shouldSetSelectedAsDefault()
0138     {
0139         // GIVEN
0140         QStandardItemModel model;
0141         auto itemA = new QStandardItem(QStringLiteral("A"));
0142         auto sourceA = Domain::DataSource::Ptr::create();
0143         sourceA->setContentTypes(Domain::DataSource::Tasks);
0144         itemA->setData(QVariant::fromValue(sourceA), Presentation::QueryTreeModelBase::ObjectRole);
0145         model.appendRow(itemA);
0146 
0147         auto itemB = new QStandardItem(QStringLiteral("B"));
0148         auto sourceB = Domain::DataSource::Ptr::create();
0149         sourceB->setContentTypes(Domain::DataSource::Tasks);
0150         itemB->setData(QVariant::fromValue(sourceB), Presentation::QueryTreeModelBase::ObjectRole);
0151         model.appendRow(itemB);
0152 
0153         auto itemC = new QStandardItem(QStringLiteral("C"));
0154         auto sourceC = Domain::DataSource::Ptr::create();
0155         sourceC->setContentTypes(Domain::DataSource::NoContent);
0156         itemC->setData(QVariant::fromValue(sourceC), Presentation::QueryTreeModelBase::ObjectRole);
0157         model.appendRow(itemC);
0158 
0159         AvailableSourcesModelStub stubSourcesModel;
0160         stubSourcesModel.setProperty("sourceListModel", QVariant::fromValue(static_cast<QAbstractItemModel*>(&model)));
0161 
0162         Widgets::AvailableSourcesView available;
0163         available.setModel(&stubSourcesModel);
0164 
0165         auto sourcesView = available.findChild<QTreeView*>(QStringLiteral("sourcesView"));
0166         QVERIFY(sourcesView);
0167         auto proxy = qobject_cast<QSortFilterProxyModel*>(sourcesView->model());
0168         QVERIFY(proxy);
0169 
0170         auto defaultAction = available.findChild<QAction*>(QStringLiteral("defaultAction"));
0171         QVERIFY(defaultAction);
0172 
0173         // WHEN
0174         auto selectedIndex = QPersistentModelIndex(model.index(1, 0));
0175         sourcesView->setCurrentIndex(proxy->mapFromSource(selectedIndex));
0176 
0177         // THEN
0178         QVERIFY(defaultAction->isEnabled());
0179 
0180         // WHEN
0181         defaultAction->trigger();
0182 
0183         // THEN
0184         QCOMPARE(stubSourcesModel.defaultIndex, selectedIndex);
0185 
0186         // WHEN
0187         sourcesView->selectionModel()->clear();
0188 
0189         // THEN
0190         QVERIFY(!defaultAction->isEnabled());
0191 
0192         // WHEN
0193         selectedIndex = QPersistentModelIndex(model.index(2, 0));
0194         sourcesView->setCurrentIndex(proxy->mapFromSource(selectedIndex));
0195 
0196         // THEN
0197         QVERIFY(!defaultAction->isEnabled());
0198     }
0199 
0200     void shouldInvokeModelSettingsDialog()
0201     {
0202         // GIVEN
0203         AvailableSourcesModelStub stubSourcesModel;
0204         QVERIFY(!stubSourcesModel.settingsCalled);
0205 
0206         Widgets::AvailableSourcesView available;
0207         available.setModel(&stubSourcesModel);
0208 
0209         auto settingsAction = available.findChild<QAction*>(QStringLiteral("settingsAction"));
0210         QVERIFY(settingsAction);
0211 
0212         // WHEN
0213         settingsAction->trigger();
0214 
0215         // THEN
0216         QVERIFY(stubSourcesModel.settingsCalled);
0217     }
0218 };
0219 
0220 ZANSHIN_TEST_MAIN(AvailableSourcesViewTest)
0221 
0222 #include "availablesourcesviewtest.moc"